]> gerrit.simantics Code Review - simantics/3d.git/commitdiff
Mesh API to use Tuple3d instead of Vector3d 46/2846/1 release/1.38.0
authorMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 8 Apr 2019 15:48:28 +0000 (18:48 +0300)
committerMarko Luukkainen <marko.luukkainen@semantum.fi>
Mon, 8 Apr 2019 15:48:28 +0000 (18:48 +0300)
Added ArcCylinder for creating cylinder shapes bend on an arc of circle

Change-Id: I09713df59e7cbf0d6805f055a039af46d57934d6

org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/shape/MeshActor.java
org.simantics.g3d.vtk/src/org/simantics/g3d/vtk/utils/vtkUtil.java
org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java [new file with mode: 0644]
org.simantics.g3d/src/org/simantics/g3d/shape/Box.java
org.simantics.g3d/src/org/simantics/g3d/shape/Cone.java
org.simantics.g3d/src/org/simantics/g3d/shape/Cylinder.java
org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java
org.simantics.g3d/src/org/simantics/g3d/shape/Sphere.java
org.simantics.g3d/src/org/simantics/g3d/shape/Tube.java

index 042142fccdb9af1898a75785db6d00b9b012b19d..0cfc8246bcc155d43e25edf0dcb550800369669a 100644 (file)
@@ -1,97 +1,98 @@
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.vtk.shape;\r
-\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.shape.Color4d;\r
-import org.simantics.g3d.shape.Mesh;\r
-\r
-import vtk.vtkActor;\r
-import vtk.vtkDataSetMapper;\r
-import vtk.vtkIdList;\r
-import vtk.vtkPoints;\r
-import vtk.vtkPolyData;\r
-import vtk.vtkPolyDataMapper;\r
-import vtk.vtkPolyDataNormals;\r
-import vtk.vtkTriangle;\r
-import vtk.vtkUnsignedCharArray;\r
-\r
-public class MeshActor extends vtkActor {\r
-       \r
-       public void setMesh(Mesh mesh) {\r
-               \r
-               vtkPolyDataMapper mapper = new vtkPolyDataMapper();\r
-               \r
-               vtkPolyData polyData = new vtkPolyData();\r
-               polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);\r
-               \r
-               vtkTriangle triangle = new vtkTriangle();\r
-        vtkIdList list = triangle.GetPointIds();\r
-        for (int i = 0; i < mesh.getIndices().size(); i+=3) {\r
-               list.SetId(0, mesh.getIndices().get(i));\r
-               list.SetId(1, mesh.getIndices().get(i+1));\r
-               list.SetId(2, mesh.getIndices().get(i+2));\r
-               polyData.InsertNextCell(triangle.GetCellType(), list);\r
-        }\r
-        list.Delete();\r
-        triangle.Delete();\r
-               \r
-        \r
-        vtkPoints points = new vtkPoints();\r
-        for (int i = 0; i < mesh.getVertices().size(); i++) {\r
-               Vector3d p = mesh.getVertices().get(i);\r
-               points.InsertPoint(i, p.x, p.y, p.z);\r
-        }\r
-        \r
-        polyData.SetPoints(points);\r
-        points.Delete();\r
-        \r
-        if (mesh.getColors() != null) {\r
-               vtkUnsignedCharArray colors = new vtkUnsignedCharArray();\r
-               colors.SetName("Colors");\r
-               colors.SetNumberOfComponents(3);\r
-               colors.SetNumberOfTuples(mesh.getColors().size());\r
-               for (int i = 0; i < mesh.getColors().size(); i++) {\r
-                       Color4d c = mesh.getColors().get(i);\r
-                       colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);\r
-               }\r
-               polyData.GetPointData().AddArray(colors);\r
-               colors.Delete();\r
-               \r
-        }\r
-        \r
-        boolean computeNormals = true;\r
-        if (computeNormals) {\r
-               vtkPolyDataNormals normals = new vtkPolyDataNormals();\r
-               normals.SetInput(polyData);\r
-               mapper.SetInputConnection(normals.GetOutputPort());\r
-               normals.GetOutputPort().Delete();\r
-               normals.Delete();\r
-        } else {\r
-               mapper.SetInput(polyData);\r
-        }\r
-        \r
-        if (mesh.getColors() != null) {\r
-                mapper.ScalarVisibilityOn();\r
-                mapper.SetScalarModeToUsePointFieldData();\r
-                mapper.SelectColorArray("Colors");\r
-        }\r
-        \r
-        SetMapper(mapper);\r
-        mapper.Delete();\r
-        polyData.GetPointData().Delete();\r
-        polyData.Delete();\r
-        \r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.shape;
+
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.shape.Color4d;
+import org.simantics.g3d.shape.Mesh;
+
+import vtk.vtkActor;
+import vtk.vtkDataSetMapper;
+import vtk.vtkIdList;
+import vtk.vtkPoints;
+import vtk.vtkPolyData;
+import vtk.vtkPolyDataMapper;
+import vtk.vtkPolyDataNormals;
+import vtk.vtkTriangle;
+import vtk.vtkUnsignedCharArray;
+
+public class MeshActor extends vtkActor {
+       
+       public void setMesh(Mesh mesh) {
+               
+               vtkPolyDataMapper mapper = new vtkPolyDataMapper();
+               
+               vtkPolyData polyData = new vtkPolyData();
+               polyData.Allocate(mesh.getIndices().size()/3, mesh.getIndices().size()/3);
+               
+               vtkTriangle triangle = new vtkTriangle();
+        vtkIdList list = triangle.GetPointIds();
+        for (int i = 0; i < mesh.getIndices().size(); i+=3) {
+               list.SetId(0, mesh.getIndices().get(i));
+               list.SetId(1, mesh.getIndices().get(i+1));
+               list.SetId(2, mesh.getIndices().get(i+2));
+               polyData.InsertNextCell(triangle.GetCellType(), list);
+        }
+        list.Delete();
+        triangle.Delete();
+               
+        
+        vtkPoints points = new vtkPoints();
+        for (int i = 0; i < mesh.getVertices().size(); i++) {
+               Tuple3d p = mesh.getVertices().get(i);
+               points.InsertPoint(i, p.x, p.y, p.z);
+        }
+        
+        polyData.SetPoints(points);
+        points.Delete();
+        
+        if (mesh.getColors() != null) {
+               vtkUnsignedCharArray colors = new vtkUnsignedCharArray();
+               colors.SetName("Colors");
+               colors.SetNumberOfComponents(3);
+               colors.SetNumberOfTuples(mesh.getColors().size());
+               for (int i = 0; i < mesh.getColors().size(); i++) {
+                       Color4d c = mesh.getColors().get(i);
+                       colors.InsertTuple3(i, 255.0* c.x, 255.0 * c.y, 255.0 * c.z);
+               }
+               polyData.GetPointData().AddArray(colors);
+               colors.Delete();
+               
+        }
+        
+        boolean computeNormals = true;
+        if (computeNormals) {
+               vtkPolyDataNormals normals = new vtkPolyDataNormals();
+               normals.SetInput(polyData);
+               mapper.SetInputConnection(normals.GetOutputPort());
+               normals.GetOutputPort().Delete();
+               normals.Delete();
+        } else {
+               mapper.SetInput(polyData);
+        }
+        
+        if (mesh.getColors() != null) {
+                mapper.ScalarVisibilityOn();
+                mapper.SetScalarModeToUsePointFieldData();
+                mapper.SelectColorArray("Colors");
+        }
+        
+        SetMapper(mapper);
+        mapper.Delete();
+        polyData.GetPointData().Delete();
+        polyData.Delete();
+        
+       }
+
+}
index 6a9b6b9132f8d20d6f772fdffa55777d57c08f4f..71710def6d3df76170582933db615050f6589c04 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.vtk.utils;\r
-\r
-import java.util.Collection;\r
-\r
-import javax.vecmath.AxisAngle4d;\r
-import javax.vecmath.Matrix4d;\r
-import javax.vecmath.Point2d;\r
-import javax.vecmath.Point3d;\r
-import javax.vecmath.Quat4d;\r
-import javax.vecmath.Tuple3d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-import org.simantics.g3d.math.Ray;\r
-\r
-import vtk.vtkMatrix4x4;\r
-import vtk.vtkProp3D;\r
-import vtk.vtkRenderer;\r
-\r
-public class vtkUtil {\r
-\r
-       public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {\r
-               Point2d screenPos = new Point2d(x,y);\r
-               Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);\r
-               Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);\r
-               Vector3d dir = new Vector3d(worldCoords2);\r
-               dir.sub(worldCoords);\r
-               return new Ray(worldCoords, dir);\r
-       }\r
-       \r
-       public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {\r
-                               \r
-                ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);\r
-                ren1.DisplayToWorld();\r
-                double world[] = ren1.GetWorldPoint();  \r
-\r
-               return new Point3d(world);\r
-                \r
-       }\r
-       \r
-       public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {\r
-                ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);\r
-                ren1.WorldToDisplay();\r
-                double screen[] = ren1.GetDisplayPoint();\r
-                \r
-                return new Point2d(screen);\r
-                \r
-       }\r
-\r
-       public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {\r
-               Matrix4d mat = new Matrix4d();\r
-               for (int i = 0; i < 4; i++) {\r
-                       for (int j = 0; j < 4; j++) {\r
-                               mat.setElement(i, j, ptm.GetElement(i, j));\r
-                       }\r
-               }\r
-\r
-               return mat;\r
-       }\r
-       \r
-       public static vtkMatrix4x4 getMatrix(Matrix4d m) {\r
-               vtkMatrix4x4 mat= new vtkMatrix4x4();\r
-               for (int i = 0; i < 4; i++) {\r
-                       for (int j = 0; j < 4; j++) {\r
-                               mat.SetElement(i, j, m.getElement(i, j));\r
-                       }\r
-               }\r
-               return mat;\r
-       }\r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {\r
-               AxisAngle4d aa = new AxisAngle4d();\r
-               aa.set(q);\r
-               updateTransform(props, pos, aa);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetPosition(pos);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-       }\r
-       \r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {\r
-               for (vtkProp3D actor : props) {\r
-                       actor.SetOrientation(0, 0, 0);\r
-                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-                       actor.SetPosition(pos.x, pos.y, pos.z);\r
-               }\r
-       }\r
-       \r
-       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {\r
-               for (vtkProp3D actor : props) {\r
-                       actor.SetOrientation(0, 0, 0);\r
-                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-                       actor.SetScale(scale);\r
-                       actor.SetPosition(pos.x,pos.y,pos.z);\r
-               }\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetScale(scale);\r
-               actor.SetPosition(pos.x,pos.y,pos.z);\r
-       }\r
-       \r
-       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {\r
-               actor.SetOrientation(0, 0, 0);\r
-               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);\r
-               actor.SetScale(scalex,scaley, scalez);\r
-               actor.SetPosition(pos.x,pos.y,pos.z);\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.vtk.utils;
+
+import java.util.Collection;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Matrix4d;
+import javax.vecmath.Point2d;
+import javax.vecmath.Point3d;
+import javax.vecmath.Quat4d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+import org.simantics.g3d.math.Ray;
+
+import vtk.vtkMatrix4x4;
+import vtk.vtkProp3D;
+import vtk.vtkRenderer;
+
+public class vtkUtil {
+
+       public static Ray createMouseRay(vtkRenderer ren1, double x, double y) {
+               Point2d screenPos = new Point2d(x,y);
+               Point3d worldCoords = getWorldCoordinates(ren1, screenPos, 0);
+               Point3d worldCoords2 = getWorldCoordinates(ren1, screenPos, 1);
+               Vector3d dir = new Vector3d(worldCoords2);
+               dir.sub(worldCoords);
+               return new Ray(worldCoords, dir);
+       }
+       
+       public static Point3d getWorldCoordinates(vtkRenderer ren1, Point2d screenPosition, double zPos) {
+                               
+                ren1.SetDisplayPoint(screenPosition.x, ren1.GetSize()[1]-screenPosition.y, zPos);
+                ren1.DisplayToWorld();
+                double world[] = ren1.GetWorldPoint();  
+
+               return new Point3d(world);
+                
+       }
+       
+       public static Point2d getScreenCoordinates(vtkRenderer ren1, Tuple3d worldPos) {
+                ren1.SetWorldPoint(worldPos.x, worldPos.y, worldPos.z, 0.0);
+                ren1.WorldToDisplay();
+                double screen[] = ren1.GetDisplayPoint();
+                
+                return new Point2d(screen);
+                
+       }
+
+       public static Matrix4d getMatrix(vtkMatrix4x4 ptm) {
+               Matrix4d mat = new Matrix4d();
+               for (int i = 0; i < 4; i++) {
+                       for (int j = 0; j < 4; j++) {
+                               mat.setElement(i, j, ptm.GetElement(i, j));
+                       }
+               }
+
+               return mat;
+       }
+       
+       public static vtkMatrix4x4 getMatrix(Matrix4d m) {
+               vtkMatrix4x4 mat= new vtkMatrix4x4();
+               for (int i = 0; i < 4; i++) {
+                       for (int j = 0; j < 4; j++) {
+                               mat.SetElement(i, j, m.getElement(i, j));
+                       }
+               }
+               return mat;
+       }
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, Quat4d q) {
+               AxisAngle4d aa = new AxisAngle4d();
+               aa.set(q);
+               updateTransform(props, pos, aa);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, double pos[], AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetPosition(pos);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Tuple3d pos, AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetPosition(new double[] {pos.x,pos.y,pos.z});
+       }
+       
+       public static void updateTransform(vtkProp3D actor, AxisAngle4d aa) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+       }
+       
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa) {
+               for (vtkProp3D actor : props) {
+                       actor.SetOrientation(0, 0, 0);
+                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+                       actor.SetPosition(pos.x, pos.y, pos.z);
+               }
+       }
+       
+       public static void updateTransform(Collection<vtkProp3D> props, Vector3d pos, AxisAngle4d aa, double scale) {
+               for (vtkProp3D actor : props) {
+                       actor.SetOrientation(0, 0, 0);
+                       actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+                       actor.SetScale(scale);
+                       actor.SetPosition(pos.x,pos.y,pos.z);
+               }
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scale) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetScale(scale);
+               actor.SetPosition(pos.x,pos.y,pos.z);
+       }
+       
+       public static void updateTransform(vtkProp3D actor, Vector3d pos, AxisAngle4d aa, double scalex, double scaley, double scalez) {
+               actor.SetOrientation(0, 0, 0);
+               actor.RotateWXYZ(MathTools.radToDeg(aa.angle), aa.x, aa.y, aa.z);
+               actor.SetScale(scalex,scaley, scalez);
+               actor.SetPosition(pos.x,pos.y,pos.z);
+       }
+}
diff --git a/org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java b/org.simantics.g3d/src/org/simantics/g3d/shape/ArcCylinder.java
new file mode 100644 (file)
index 0000000..ab61784
--- /dev/null
@@ -0,0 +1,152 @@
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Point3d;
+import javax.vecmath.Quat4d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+
+public class ArcCylinder {
+
+       public Mesh create(Point3d s, Point3d v, Point3d e, double rad, int res) {
+               
+               Vector3d v1 = new Vector3d(s);
+               v1.sub(v);
+               
+               Vector3d v2 = new Vector3d(e);
+               v2.sub(v);
+               
+               double a = v2.angle(v1);
+               int steps = 0;
+               double sa = 0.0;
+               
+               Vector3d rn = new Vector3d();
+               Vector3d r1 = null;
+               Vector3d c = null;
+               
+               if ((a +0.0001) > Math.PI) {
+                       steps = 1;
+               } else {
+                       c = new Vector3d(v2);
+                       c.add(v1);
+                       c.normalize();
+                       c.scale(v1.length() * (1.0/Math.cos(a*0.5)));
+                       c.add(v);
+                       
+                       r1 = new Vector3d(s);
+                       r1.sub(c);
+                       
+                       Vector3d r2 = new Vector3d(e);
+                       r2.sub(c);
+                       
+                       a = r2.angle(r1);
+                       
+                       rn.cross(v2, v1);
+                       rn.normalize();
+                       
+                       steps = (int)(Math.ceil(a/0.1));
+                       if (steps == 0)
+                               steps = 1;
+                       sa = a/steps;
+               }
+
+               
+               List<Tuple3d> vertices = new ArrayList<Tuple3d>(res * (steps+1));
+               List<Tuple3d> normals = new ArrayList<Tuple3d>(res * (steps+1));
+               List<Integer> indices = new ArrayList<Integer>();
+               
+               for (int i = 0; i <= steps; i++) {
+                       Vector3d p;
+                       Vector3d t;
+                       if (i == 0) {
+                               p = new Vector3d(s);
+                               t = new Vector3d(v1);
+                               t.negate();
+                               t.normalize();
+                       } else if (i == steps) {
+                               p = new Vector3d(e);
+                               t = new Vector3d(v2);
+                               t.normalize();
+                       } else {
+                               p = new Vector3d();
+                               double ca = sa * i;
+                               Quat4d q = MathTools.getQuat(new AxisAngle4d(rn, ca));
+                               MathTools.rotate(q, r1, p);
+                               t = new Vector3d();
+                               t.cross(rn,p);
+                               t.normalize();
+                               p.add(c);
+                               
+                       }
+                       createCircle(vertices, normals, p, t, rn, res, rad);
+               }
+               int count = steps*res*6;
+               for (int i = 0; i < count; i++) {
+                       indices.add(-1);
+               }       
+               createIndices(steps, res, indices);
+               return new Mesh(vertices, normals, indices);
+       }
+       
+       private static void createCircle(List<Tuple3d> points, List<Tuple3d> normals, Tuple3d p, Vector3d t, Vector3d n, int res, double radius) {
+               n = new Vector3d(n);
+               n.scale(radius);
+               
+               for (int index = 0; index < res; index ++) {
+                       Vector3d v;
+                       if (index == 0) {
+                               v = new Vector3d(n);
+                               
+                       } else {
+                               AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)res);
+                               v = new Vector3d();
+                               MathTools.rotate(MathTools.getQuat(aa), n, v);
+                       }
+                       //int vIndex = (i*resolution + index)*3;
+                       Vector3d pt = new Vector3d(p);
+                       pt.add(v);
+                       //points.set(vIndex, pt);
+                       points.add(pt);
+                       v.normalize();
+                       //normals.set(vIndex, v);
+                       normals.add(v);
+               }
+       }
+       private static void createIndices(int steps, int resolution, List<Integer> index) {
+               for (int c = 0; c < steps; c++) {
+                       for (int s = 0; s < resolution; s++) {
+                               int ii = (c * resolution + s) * 6;
+                               int iv = c*resolution + s;
+                               
+                               /*
+                            iv+1 ---- iv + resolution + 1
+                                     | /|
+                                     |/ |
+                                  iv ---- iv + resolution 
+                               */
+                               if (s < resolution - 1) {
+                                       index.set(ii+2,iv);
+                                       index.set(ii+1,iv+resolution);
+                                       index.set(ii+0,iv+resolution+1);
+                                       
+                                       index.set(ii+5,iv);
+                                       index.set(ii+4,iv+resolution+1);
+                                       index.set(ii+3,iv+1);
+                               } else {
+                                       index.set(ii+2,iv);
+                                       index.set(ii+1,iv+resolution);
+                                       index.set(ii+0,iv+1);
+                                       
+                                       index.set(ii+5,iv);
+                                       index.set(ii+4,iv+1);
+                                       index.set(ii+3,iv+1-resolution);
+                               }
+                       }
+               }
+       }
+}
index 6fab2b80d29094e06af04085c20c79ab9abe54a3..fa716b7438266214829a6d61724f91b36aaa0f7d 100644 (file)
@@ -1,96 +1,96 @@
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.Vector3d;\r
-\r
-public class Box {\r
-       \r
-       Vector3d min;\r
-       Vector3d max;\r
-       \r
-       public Vector3d getMin() {\r
-               return min;\r
-       }\r
-       public Vector3d getMax() {\r
-               return max;\r
-       }\r
-       \r
-       public Box(Vector3d min, Vector3d max) {\r
-               this.max = max;\r
-               this.min = min;\r
-       }\r
-       \r
-       public Box(double minx, double miny, double minz, double maxx, double maxy, double maxz) {\r
-               min = new Vector3d(minx, miny, minz);\r
-               max = new Vector3d(maxx, maxy, maxz);\r
-       }\r
-       \r
-       public Box(double min[], double max[]) {\r
-               this.min = new Vector3d(min);\r
-               this.max = new Vector3d(max);\r
-       }\r
-       \r
-       public Mesh createMesh() {\r
-               List<Vector3d> vertices = new ArrayList<Vector3d>();\r
-               vertices.add(new Vector3d(min.x, min.y, min.z));\r
-               vertices.add(new Vector3d(min.x, min.y, max.z));\r
-               vertices.add(new Vector3d(min.x, max.y, min.z));\r
-               vertices.add(new Vector3d(min.x, max.y, max.z));\r
-               vertices.add(new Vector3d(max.x, min.y, min.z));\r
-               vertices.add(new Vector3d(max.x, min.y, max.z));\r
-               vertices.add(new Vector3d(max.x, max.y, min.z));\r
-               vertices.add(new Vector3d(max.x, max.y, max.z));\r
-               List<Integer> indices = new ArrayList<Integer>();\r
-               indices.add(0);\r
-               indices.add(2);\r
-               indices.add(1);\r
-               \r
-               indices.add(1);\r
-               indices.add(2);\r
-               indices.add(3);\r
-               \r
-               indices.add(2);\r
-               indices.add(6);\r
-               indices.add(3);\r
-               \r
-               indices.add(3);\r
-               indices.add(6);\r
-               indices.add(7);\r
-               \r
-               indices.add(5);\r
-               indices.add(1);\r
-               indices.add(7);\r
-               \r
-               indices.add(1);\r
-               indices.add(3);\r
-               indices.add(7);\r
-               \r
-               indices.add(4);\r
-               indices.add(5);\r
-               indices.add(6);\r
-               \r
-               indices.add(5);\r
-               indices.add(7);\r
-               indices.add(6);\r
-               \r
-               indices.add(0);\r
-               indices.add(4);\r
-               indices.add(2);\r
-               \r
-               indices.add(2);\r
-               indices.add(4);\r
-               indices.add(6);\r
-               \r
-               indices.add(0);\r
-               indices.add(1);\r
-               indices.add(4);\r
-               \r
-               indices.add(5);\r
-               indices.add(4);\r
-               indices.add(1);\r
-               return new Mesh(vertices, indices);\r
-       }\r
-\r
-}\r
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.Vector3d;
+
+public class Box {
+       
+       Vector3d min;
+       Vector3d max;
+       
+       public Vector3d getMin() {
+               return min;
+       }
+       public Vector3d getMax() {
+               return max;
+       }
+       
+       public Box(Vector3d min, Vector3d max) {
+               this.max = max;
+               this.min = min;
+       }
+       
+       public Box(double minx, double miny, double minz, double maxx, double maxy, double maxz) {
+               min = new Vector3d(minx, miny, minz);
+               max = new Vector3d(maxx, maxy, maxz);
+       }
+       
+       public Box(double min[], double max[]) {
+               this.min = new Vector3d(min);
+               this.max = new Vector3d(max);
+       }
+       
+       public Mesh createMesh() {
+               List<Vector3d> vertices = new ArrayList<Vector3d>();
+               vertices.add(new Vector3d(min.x, min.y, min.z));
+               vertices.add(new Vector3d(min.x, min.y, max.z));
+               vertices.add(new Vector3d(min.x, max.y, min.z));
+               vertices.add(new Vector3d(min.x, max.y, max.z));
+               vertices.add(new Vector3d(max.x, min.y, min.z));
+               vertices.add(new Vector3d(max.x, min.y, max.z));
+               vertices.add(new Vector3d(max.x, max.y, min.z));
+               vertices.add(new Vector3d(max.x, max.y, max.z));
+               List<Integer> indices = new ArrayList<Integer>();
+               indices.add(0);
+               indices.add(2);
+               indices.add(1);
+               
+               indices.add(1);
+               indices.add(2);
+               indices.add(3);
+               
+               indices.add(2);
+               indices.add(6);
+               indices.add(3);
+               
+               indices.add(3);
+               indices.add(6);
+               indices.add(7);
+               
+               indices.add(5);
+               indices.add(1);
+               indices.add(7);
+               
+               indices.add(1);
+               indices.add(3);
+               indices.add(7);
+               
+               indices.add(4);
+               indices.add(5);
+               indices.add(6);
+               
+               indices.add(5);
+               indices.add(7);
+               indices.add(6);
+               
+               indices.add(0);
+               indices.add(4);
+               indices.add(2);
+               
+               indices.add(2);
+               indices.add(4);
+               indices.add(6);
+               
+               indices.add(0);
+               indices.add(1);
+               indices.add(4);
+               
+               indices.add(5);
+               indices.add(4);
+               indices.add(1);
+               return Mesh.create(vertices, indices);
+       }
+
+}
index 2f37840537cb1c7c051fe3d5ff7a43a14531c531..5283eb444fac8785023648c3fe351e53a3cdd5a3 100644 (file)
@@ -1,75 +1,75 @@
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.AxisAngle4d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-\r
-public class Cone {\r
-\r
-       public static Mesh create(double radius, int s) {\r
-               if (s < 3 || radius < MathTools.NEAR_ZERO)\r
-                       throw new IllegalArgumentException();\r
-               List<Vector3d> vertices = new ArrayList<Vector3d>(s+2);\r
-               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());\r
-               List<Integer> indices = new ArrayList<Integer>();\r
-               \r
-               vertices.add(new Vector3d(0.0,0.0,0.0));\r
-               normals.add(new Vector3d(0.0,-1.0,0.0));\r
-               vertices.add(new Vector3d(0.0,radius*2.0,0.0));\r
-               normals.add(new Vector3d(0.0,1.0,0.0));\r
-               \r
-               Vector3d v = new Vector3d(radius,0,0);\r
-               for (int i = 0; i < s; i++) {\r
-                       AxisAngle4d aa = new AxisAngle4d(0,1,0,((double)i/(double)s)*Math.PI * 2);\r
-                       Vector3d t = new Vector3d();\r
-                       MathTools.rotate(MathTools.getQuat(aa), v, t);\r
-                       vertices.add(t);\r
-                       Vector3d n = new Vector3d(t);\r
-                       n.normalize();\r
-                       normals.add(n);\r
-               }\r
-               \r
-               for (int i = 0; i < s; i++) {\r
-                       indices.add(0);\r
-                       \r
-                       if (i < s - 1)\r
-                               indices.add(i + 3);\r
-                       else\r
-                               indices.add(2);\r
-                       indices.add(i + 2);\r
-               }\r
-               \r
-               for (int i = 0; i < s; i++) {\r
-                       indices.add(1);\r
-                       indices.add(i + 2);\r
-                       if (i < s - 1)\r
-                               indices.add(i + 3);\r
-                       else\r
-                               indices.add(2);\r
-                       \r
-               }\r
-               return new Mesh(vertices,normals,indices);\r
-               \r
-       }\r
-       \r
-       public static void main(String arg[]) {\r
-               Mesh s1 = create(1.0, 3);\r
-               Mesh s2 = create(1.0, 4);\r
-               System.out.println("debug " + s1 + "\n" + s2);\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+
+public class Cone {
+
+       public static Mesh create(double radius, int s) {
+               if (s < 3 || radius < MathTools.NEAR_ZERO)
+                       throw new IllegalArgumentException();
+               List<Vector3d> vertices = new ArrayList<Vector3d>(s+2);
+               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());
+               List<Integer> indices = new ArrayList<Integer>();
+               
+               vertices.add(new Vector3d(0.0,0.0,0.0));
+               normals.add(new Vector3d(0.0,-1.0,0.0));
+               vertices.add(new Vector3d(0.0,radius*2.0,0.0));
+               normals.add(new Vector3d(0.0,1.0,0.0));
+               
+               Vector3d v = new Vector3d(radius,0,0);
+               for (int i = 0; i < s; i++) {
+                       AxisAngle4d aa = new AxisAngle4d(0,1,0,((double)i/(double)s)*Math.PI * 2);
+                       Vector3d t = new Vector3d();
+                       MathTools.rotate(MathTools.getQuat(aa), v, t);
+                       vertices.add(t);
+                       Vector3d n = new Vector3d(t);
+                       n.normalize();
+                       normals.add(n);
+               }
+               
+               for (int i = 0; i < s; i++) {
+                       indices.add(0);
+                       
+                       if (i < s - 1)
+                               indices.add(i + 3);
+                       else
+                               indices.add(2);
+                       indices.add(i + 2);
+               }
+               
+               for (int i = 0; i < s; i++) {
+                       indices.add(1);
+                       indices.add(i + 2);
+                       if (i < s - 1)
+                               indices.add(i + 3);
+                       else
+                               indices.add(2);
+                       
+               }
+               return Mesh.create(vertices,normals,indices);
+               
+       }
+       
+       public static void main(String arg[]) {
+               Mesh s1 = create(1.0, 3);
+               Mesh s2 = create(1.0, 4);
+               System.out.println("debug " + s1 + "\n" + s2);
+       }
+}
index 5398512ef3d36b3122f01db2e478b4ae3e765b94..4fe564bfaddbd9f080328844375c5bf11e3376ae 100644 (file)
@@ -1,34 +1,35 @@
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.Vector3d;\r
-\r
-public class Cylinder {\r
-       \r
-       public static Mesh create(Vector3d start, Vector3d dir, double r, int s) {\r
-               Tube tube = new Tube();\r
-               tube.setResolution(s);\r
-               tube.setRadius(r);\r
-               List<Vector3d> vertices = new ArrayList<Vector3d>();\r
-               vertices.add(start);\r
-               Vector3d t = new Vector3d(start);\r
-               t.add(dir);\r
-               vertices.add(dir);\r
-               tube.setVertices(vertices);\r
-               return tube.create();\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+public class Cylinder {
+       
+       public static Mesh create(Vector3d start, Vector3d dir, double r, int s) {
+               Tube tube = new Tube();
+               tube.setResolution(s);
+               tube.setRadius(r);
+               List<Tuple3d> vertices = new ArrayList<Tuple3d>();
+               vertices.add(start);
+               Vector3d t = new Vector3d(start);
+               t.add(dir);
+               vertices.add(dir);
+               tube.setVertices(vertices);
+               return tube.create();
+       }
+
+}
index ea6ec593f5702d3b17ab37ed79e9670b17446216..a45cf812adf90c6d237deeb6e7ec17767830a769 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.Quat4d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-\r
-public class Mesh {\r
-       private List<Vector3d> vertices;\r
-       private List<Vector3d> normals;\r
-       private List<Color4d> colors;\r
-       private List<Integer> indices;\r
-       \r
-       \r
-       public Mesh(List<Vector3d> vertices, List<Vector3d> normals,\r
-                       List<Color4d> colors, List<Integer> indices) {\r
-               this.vertices = vertices;\r
-               this.normals = normals;\r
-               this.colors = colors;\r
-               this.indices = indices;\r
-       }\r
-       \r
-       public Mesh(List<Vector3d> vertices, List<Vector3d> normals, List<Integer> indices) {\r
-               this.vertices = vertices;\r
-               this.normals = normals;\r
-               this.indices = indices;\r
-       }\r
-       \r
-       public Mesh(List<Vector3d> vertices, List<Integer> indices) {\r
-               this.vertices = vertices;\r
-               this.indices = indices;\r
-       }\r
-\r
-       public List<Vector3d> getVertices() {\r
-               return vertices;\r
-       }\r
-       \r
-       public List<Vector3d> getNormals() {\r
-               return normals;\r
-       }\r
-       \r
-       public List<Integer> getIndices() {\r
-               return indices;\r
-       }\r
-       \r
-       public List<Color4d> getColors() {\r
-               return colors;\r
-       }\r
-       \r
-       public void createNormals() {\r
-               normals = new ArrayList<Vector3d>(vertices.size());\r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       normals.add(new Vector3d());\r
-               }\r
-        Vector3d v1 = new Vector3d();\r
-        Vector3d v2 = new Vector3d();\r
-        Vector3d v3 = new Vector3d();\r
-        Vector3d t1 = new Vector3d();\r
-        Vector3d t2 = new Vector3d();\r
-        Vector3d n = new Vector3d();\r
-        for (int i = 0; i < indices.size(); i+=3) {\r
-            v1.set(vertices.get(i));\r
-            v2.set(vertices.get(i+1));\r
-            v3.set(vertices.get(i+2));\r
-            t1.sub(v3,v1);\r
-            t2.sub(v2,v1);\r
-            n.cross(t2, t1);\r
-            normals.get(i).add(n);\r
-            normals.get(i+1).add(n);\r
-            normals.get(i+2).add(n);\r
-        }\r
-        for (int i = 0; i < normals.size(); i++) {\r
-               normals.get(i).normalize();\r
-        }\r
-    }\r
-       \r
-       public void translate(Vector3d v) {\r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       vertices.get(i).add(v);\r
-               }\r
-       }\r
-       \r
-       public void rotate(Quat4d q) {\r
-               Vector3d t = new Vector3d();\r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       MathTools.rotate(q, vertices.get(i), t);\r
-                       vertices.get(i).set(t);\r
-               }\r
-               \r
-               if (normals != null) {\r
-                       for (int i = 0; i < normals.size(); i++) {\r
-                               MathTools.rotate(q, normals.get(i), t);\r
-                               t.normalize();\r
-                               normals.get(i).set(t);\r
-                       }       \r
-               }\r
-       }\r
-       \r
-       public void setColor(Color4d color) {\r
-               colors = new ArrayList<Color4d>(vertices.size());\r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       colors.add(color);\r
-               }\r
-       }\r
-       \r
-       public void add(Mesh mesh) {\r
-               int vindex = vertices.size();\r
-               int triIndex = indices.size();\r
-               vertices.addAll(mesh.getVertices());\r
-               indices.addAll(mesh.indices);\r
-               for (int i = triIndex; i < indices.size(); i++) {\r
-                       indices.set(i, indices.get(i)+vindex);\r
-               }\r
-               if (normals != null) {\r
-                       boolean hasNormals = true;\r
-                       if (mesh.getNormals() == null) {\r
-                               mesh.createNormals();\r
-                               hasNormals = false;\r
-                       }\r
-                       normals.addAll(mesh.getNormals());\r
-                       if (!hasNormals)\r
-                               mesh.normals = null;\r
-               }\r
-               if (colors != null) {\r
-                       if (mesh.getColors() != null) {\r
-                               colors.addAll(mesh.getColors());\r
-                       } else {\r
-                               for (int i = 0; i < mesh.getVertices().size(); i++) {\r
-                                       colors.add(new Color4d(1,1,1,0));\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.Quat4d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+
+public class Mesh {
+       private List<Tuple3d> vertices;
+       private List<Tuple3d> normals;
+       private List<Color4d> colors;
+       private List<Integer> indices;
+       
+       
+       public Mesh(List<Tuple3d> vertices, List<Tuple3d> normals,
+                       List<Color4d> colors, List<Integer> indices) {
+               this.vertices = vertices;
+               this.normals = normals;
+               this.colors = colors;
+               this.indices = indices;
+       }
+       
+       public Mesh(List<Tuple3d> vertices, List<Tuple3d> normals, List<Integer> indices) {
+               this.vertices = vertices;
+               this.normals = normals;
+               this.indices = indices;
+       }
+       
+       public static Mesh create(List<Vector3d> vertices, List<Vector3d> normals, List<Integer> indices) {
+               
+               List<Tuple3d> v = new ArrayList<Tuple3d>();
+               List<Tuple3d> n = new ArrayList<Tuple3d>();
+               v.addAll(vertices);
+               n.addAll(normals);
+               return new Mesh(v, n, indices);
+       }
+       
+       public Mesh(List<Tuple3d> vertices, List<Integer> indices) {
+               this.vertices = vertices;
+               this.indices = indices;
+       }
+       
+       public static Mesh create(List<Vector3d> vertices, List<Integer> indices) {
+               List<Tuple3d> v = new ArrayList<Tuple3d>();
+               v.addAll(vertices);
+               return new Mesh(v, indices);
+       }
+
+       public List<Tuple3d> getVertices() {
+               return vertices;
+       }
+       
+       public List<Tuple3d> getNormals() {
+               return normals;
+       }
+       
+       public List<Integer> getIndices() {
+               return indices;
+       }
+       
+       public List<Color4d> getColors() {
+               return colors;
+       }
+       
+       public void createNormals() {
+               normals = new ArrayList<Tuple3d>(vertices.size());
+               for (int i = 0; i < vertices.size(); i++) {
+                       normals.add(new Vector3d());
+               }
+        Vector3d v1 = new Vector3d();
+        Vector3d v2 = new Vector3d();
+        Vector3d v3 = new Vector3d();
+        Vector3d t1 = new Vector3d();
+        Vector3d t2 = new Vector3d();
+        Vector3d n = new Vector3d();
+        for (int i = 0; i < indices.size(); i+=3) {
+            v1.set(vertices.get(i));
+            v2.set(vertices.get(i+1));
+            v3.set(vertices.get(i+2));
+            t1.sub(v3,v1);
+            t2.sub(v2,v1);
+            n.cross(t2, t1);
+            normals.get(i).add(n);
+            normals.get(i+1).add(n);
+            normals.get(i+2).add(n);
+        }
+        for (int i = 0; i < normals.size(); i++) {
+               ((Vector3d)normals.get(i)).normalize();
+        }
+    }
+       
+       public void translate(Vector3d v) {
+               for (int i = 0; i < vertices.size(); i++) {
+                       vertices.get(i).add(v);
+               }
+       }
+       
+       public void rotate(Quat4d q) {
+               Vector3d t = new Vector3d();
+               for (int i = 0; i < vertices.size(); i++) {
+                       MathTools.rotate(q, vertices.get(i), t);
+                       vertices.get(i).set(t);
+               }
+               
+               if (normals != null) {
+                       for (int i = 0; i < normals.size(); i++) {
+                               MathTools.rotate(q, normals.get(i), t);
+                               t.normalize();
+                               normals.get(i).set(t);
+                       }       
+               }
+       }
+       
+       public void setColor(Color4d color) {
+               colors = new ArrayList<Color4d>(vertices.size());
+               for (int i = 0; i < vertices.size(); i++) {
+                       colors.add(color);
+               }
+       }
+       
+       public void add(Mesh mesh) {
+               int vindex = vertices.size();
+               int triIndex = indices.size();
+               vertices.addAll(mesh.getVertices());
+               indices.addAll(mesh.indices);
+               for (int i = triIndex; i < indices.size(); i++) {
+                       indices.set(i, indices.get(i)+vindex);
+               }
+               if (normals != null) {
+                       boolean hasNormals = true;
+                       if (mesh.getNormals() == null) {
+                               mesh.createNormals();
+                               hasNormals = false;
+                       }
+                       normals.addAll(mesh.getNormals());
+                       if (!hasNormals)
+                               mesh.normals = null;
+               }
+               if (colors != null) {
+                       if (mesh.getColors() != null) {
+                               colors.addAll(mesh.getColors());
+                       } else {
+                               for (int i = 0; i < mesh.getVertices().size(); i++) {
+                                       colors.add(new Color4d(1,1,1,0));
+                               }
+                       }
+               }
+       }
+}
index 6eb26dfafd367ea4b76bafd11658b56307e2e55e..d007175a6915b30bfc66dad23fb3c3836478777f 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.AxisAngle4d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-\r
-public class Sphere {\r
-\r
-       public static Mesh create(double radius, int s, int p) {\r
-               if (s < 3 || p < 3 || radius < MathTools.NEAR_ZERO)\r
-                       throw new IllegalArgumentException();\r
-               List<Vector3d> vertices = new ArrayList<Vector3d>((s-2)*p + 2);\r
-               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());\r
-               List<Integer> indices = new ArrayList<Integer>(((s-3)*p*2 + p * 2)*3);\r
-               Vector3d v = new Vector3d(0.0,-radius,0.0);\r
-               Vector3d vp = new Vector3d();\r
-               for (int ip = 0; ip < p; ip++) {\r
-                       if (ip == 0) {\r
-                               vertices.add(new Vector3d(0.0,-radius,0.0));\r
-                       } else if (ip == p - 1) {\r
-                               vertices.add(new Vector3d(0.0, radius,0.0));\r
-                               int off = 1 + (ip-2)*s;\r
-                               for (int is = 0; is < s; is++) {\r
-                                       indices.add(vertices.size() - 1);\r
-                                       indices.add(is+off);\r
-                                       if (is < s -1)\r
-                                               indices.add(is+off+1);\r
-                                       else\r
-                                               indices.add(off);\r
-                                       \r
-                                       \r
-                               }\r
-                       } else {\r
-                               AxisAngle4d aa = new AxisAngle4d(1, 0, 0, ((double)ip/(double)(p-1))*Math.PI);\r
-                               MathTools.rotate(MathTools.getQuat(aa), v, vp);\r
-                               for (int is = 0; is < s; is++) {\r
-                                       aa = new AxisAngle4d(0, 1, 0, ((double)is/(double)s)*Math.PI*2);\r
-                                       Vector3d vs = new Vector3d();\r
-                                       MathTools.rotate(MathTools.getQuat(aa), vp, vs);\r
-                                       vertices.add(vs);\r
-                               }\r
-                               if (ip == 1) {\r
-                                       for (int is = 0; is < s; is++) {\r
-                                               indices.add(0);\r
-                                               if (is < s -1)\r
-                                                       indices.add(is+2);\r
-                                               else\r
-                                                       indices.add(1);\r
-                                               indices.add(is+1);\r
-                                       }\r
-                               } else {\r
-                                       int off = 1 + (ip-1)*s;\r
-                                       for (int is = 0; is < s-1; is++) {\r
-                                               indices.add(off + is - s);\r
-                                               indices.add(off + is+1);\r
-                                               indices.add(off + is);\r
-                                               \r
-                                               \r
-                                               indices.add(off + is - s);\r
-                                               indices.add(off + is + 1 - s);\r
-                                               indices.add(off + is + 1);\r
-                                               \r
-                                       }\r
-                                       indices.add(off - 1);\r
-                                       indices.add(off);\r
-                                       indices.add(off + s - 1);\r
-                                       \r
-                                       indices.add(off -1);\r
-                                       indices.add(off - s);\r
-                                       indices.add(off);\r
-                                       \r
-                               }\r
-                       }\r
-               }\r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       Vector3d n = new Vector3d(vertices.get(i));\r
-                       n.normalize();\r
-                       normals.add(n);\r
-               }\r
-               \r
-               return new Mesh(vertices,normals,indices);\r
-               \r
-       }\r
-       \r
-       public static void main(String arg[]) {\r
-               Mesh s1 = create(1.0, 3, 3);\r
-               Mesh s2 = create(1.0, 4, 4);\r
-               System.out.println("debug " + s1 + " " + s2);\r
-       }\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+
+public class Sphere {
+
+       public static Mesh create(double radius, int s, int p) {
+               if (s < 3 || p < 3 || radius < MathTools.NEAR_ZERO)
+                       throw new IllegalArgumentException();
+               List<Vector3d> vertices = new ArrayList<Vector3d>((s-2)*p + 2);
+               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size());
+               List<Integer> indices = new ArrayList<Integer>(((s-3)*p*2 + p * 2)*3);
+               Vector3d v = new Vector3d(0.0,-radius,0.0);
+               Vector3d vp = new Vector3d();
+               for (int ip = 0; ip < p; ip++) {
+                       if (ip == 0) {
+                               vertices.add(new Vector3d(0.0,-radius,0.0));
+                       } else if (ip == p - 1) {
+                               vertices.add(new Vector3d(0.0, radius,0.0));
+                               int off = 1 + (ip-2)*s;
+                               for (int is = 0; is < s; is++) {
+                                       indices.add(vertices.size() - 1);
+                                       indices.add(is+off);
+                                       if (is < s -1)
+                                               indices.add(is+off+1);
+                                       else
+                                               indices.add(off);
+                                       
+                                       
+                               }
+                       } else {
+                               AxisAngle4d aa = new AxisAngle4d(1, 0, 0, ((double)ip/(double)(p-1))*Math.PI);
+                               MathTools.rotate(MathTools.getQuat(aa), v, vp);
+                               for (int is = 0; is < s; is++) {
+                                       aa = new AxisAngle4d(0, 1, 0, ((double)is/(double)s)*Math.PI*2);
+                                       Vector3d vs = new Vector3d();
+                                       MathTools.rotate(MathTools.getQuat(aa), vp, vs);
+                                       vertices.add(vs);
+                               }
+                               if (ip == 1) {
+                                       for (int is = 0; is < s; is++) {
+                                               indices.add(0);
+                                               if (is < s -1)
+                                                       indices.add(is+2);
+                                               else
+                                                       indices.add(1);
+                                               indices.add(is+1);
+                                       }
+                               } else {
+                                       int off = 1 + (ip-1)*s;
+                                       for (int is = 0; is < s-1; is++) {
+                                               indices.add(off + is - s);
+                                               indices.add(off + is+1);
+                                               indices.add(off + is);
+                                               
+                                               
+                                               indices.add(off + is - s);
+                                               indices.add(off + is + 1 - s);
+                                               indices.add(off + is + 1);
+                                               
+                                       }
+                                       indices.add(off - 1);
+                                       indices.add(off);
+                                       indices.add(off + s - 1);
+                                       
+                                       indices.add(off -1);
+                                       indices.add(off - s);
+                                       indices.add(off);
+                                       
+                               }
+                       }
+               }
+               for (int i = 0; i < vertices.size(); i++) {
+                       Vector3d n = new Vector3d(vertices.get(i));
+                       n.normalize();
+                       normals.add(n);
+               }
+               
+               return Mesh.create(vertices,normals,indices);
+               
+       }
+       
+       public static void main(String arg[]) {
+               Mesh s1 = create(1.0, 3, 3);
+               Mesh s2 = create(1.0, 4, 4);
+               System.out.println("debug " + s1 + " " + s2);
+       }
+}
index e4d89bb29037a809958d16d5362a3eb07f0a5e4b..828fd2690c95e648d1c162c4701aca97eb68c67a 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
- * Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.g3d.shape;\r
-\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
-import javax.vecmath.AxisAngle4d;\r
-import javax.vecmath.Vector3d;\r
-\r
-import org.simantics.g3d.math.MathTools;\r
-\r
-\r
-public class Tube {\r
-       List<Vector3d> vertices;\r
-       List<Color4d> colors;\r
-       List<Double> radiis;\r
-       Double radius = 1.0;\r
-       int resolution = 8;\r
-       \r
-       \r
-       public void setResolution(int resolution) {\r
-               if (resolution > 2)\r
-                       this.resolution = resolution;\r
-       }\r
-       \r
-       public void setVertices(List<Vector3d> vertices) {\r
-               this.vertices = vertices;\r
-       }\r
-\r
-       public void setColors(List<Color4d> colors) {\r
-               this.colors = colors;\r
-       }\r
-       \r
-       public void setRadiis(List<Double> radiis) {\r
-               this.radiis = radiis;\r
-       }\r
-       \r
-       public void setRadius(Double radius) {\r
-               this.radius = radius;\r
-       }\r
-       \r
-       \r
-       \r
-       public Mesh create() {\r
-               if (vertices.size() < 2 )\r
-                       throw new IllegalArgumentException("Tube must have at least two vertices");\r
-               \r
-               Vector3d t = new Vector3d();\r
-               \r
-               for (int i = 0; i < vertices.size() - 1; i++) {\r
-                       t.set(vertices.get(i+1));\r
-                       t.sub(vertices.get(i));\r
-                       if (t.lengthSquared() < 0.0001)\r
-                               throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");\r
-               }\r
-               \r
-               List<Vector3d> points = new ArrayList<Vector3d>(vertices.size()*resolution);\r
-               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size()*resolution);\r
-               \r
-               for (int i = 0; i < vertices.size(); i++) {\r
-                       createCircle(i,points,normals);\r
-               }\r
-               \r
-               int index[] = new int[(vertices.size()-1)*resolution*6];\r
-               \r
-               createIndices(index);\r
-               List<Integer> indices = new ArrayList<Integer>();\r
-               for (int i = 0; i < index.length; i++) {\r
-                       indices.add(index[i]);\r
-               }\r
-               \r
-               \r
-               \r
-               vertices.clear();\r
-               if (colors != null)\r
-                       colors.clear();\r
-               if (radiis != null)\r
-                       radiis.clear();\r
-               \r
-               return new Mesh(points, normals, indices);\r
-               \r
-       }\r
-       \r
-       private void createCircle(int i, List<Vector3d> points, List<Vector3d> normals) {\r
-               final Vector3d up = new Vector3d(0,1,0);\r
-               final Vector3d up2 = new Vector3d(0,0,1);\r
-               Vector3d p = vertices.get(i);\r
-               Vector3d t = getTangent(i);\r
-               Vector3d n = new Vector3d();\r
-               if (up.dot(t) < 0.99) {\r
-                       n.cross(up, t);\r
-               } else {\r
-                       n.cross(up2, t);\r
-               }\r
-               n.normalize();\r
-               if (radiis != null) {\r
-                       n.scale(radiis.get(i));\r
-               } else {\r
-                       n.scale(radius);\r
-               }\r
-               \r
-               for (int index = 0; index < resolution; index ++) {\r
-                       Vector3d v;\r
-                       if (index == 0) {\r
-                               v = new Vector3d(n);\r
-                               \r
-                       } else {\r
-                               AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution);\r
-                               v = new Vector3d();\r
-                               MathTools.rotate(MathTools.getQuat(aa), n, v);\r
-                       }\r
-                       //int vIndex = (i*resolution + index)*3;\r
-                       Vector3d pt = new Vector3d(p);\r
-                       pt.add(v);\r
-                       //points.set(vIndex, pt);\r
-                       points.add(pt);\r
-                       v.normalize();\r
-                       //normals.set(vIndex, v);\r
-                       normals.add(v);\r
-               }\r
-       }\r
-       \r
-       private Vector3d getTangent(int i) {\r
-               Vector3d p,n;\r
-               if (i == 0) {\r
-                       p = vertices.get(0);\r
-                       n = vertices.get(1);\r
-               } else if (i == vertices.size() - 1) {\r
-                       p = vertices.get(i-1);\r
-                       n = vertices.get(i);\r
-               } else {\r
-                       p = vertices.get(i-1);\r
-                       n = vertices.get(i+1);\r
-               }\r
-               n = new Vector3d(n);\r
-               n.sub(p);\r
-               n.normalize();\r
-               return n;\r
-       }\r
-       \r
-       private void createIndices(int index[]) {\r
-               for (int c = 0; c < vertices.size() - 1; c++) {\r
-                       for (int s = 0; s < resolution; s++) {\r
-                               int ii = (c * resolution + s) * 6;\r
-                               int iv = c*resolution + s;\r
-                               \r
-                               /*\r
-                            iv+1 ---- iv + resolution + 1\r
-                                     | /|\r
-                                     |/ |\r
-                                  iv ---- iv + resolution \r
-                               */\r
-                               if (s < resolution - 1) {\r
-                                       index[ii+2] = iv;\r
-                                       index[ii+1] = iv+resolution;\r
-                                       index[ii+0] = iv+resolution+1;\r
-                                       \r
-                                       index[ii+5] = iv;\r
-                                       index[ii+4] = iv+resolution+1;\r
-                                       index[ii+3] = iv+1;\r
-                               } else {\r
-                                       index[ii+2] = iv;\r
-                                       index[ii+1] = iv+resolution;\r
-                                       index[ii+0] = iv+1;\r
-                                       \r
-                                       index[ii+5] = iv;\r
-                                       index[ii+4] = iv+1;\r
-                                       index[ii+3] = iv+1-resolution;\r
-                               }\r
-                       }\r
-               }\r
-       }\r
-\r
-}\r
+/*******************************************************************************
+ * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
+ * Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.g3d.shape;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.vecmath.AxisAngle4d;
+import javax.vecmath.Tuple3d;
+import javax.vecmath.Vector3d;
+
+import org.simantics.g3d.math.MathTools;
+
+
+public class Tube {
+       List<Tuple3d> vertices;
+       List<Vector3d> tangents;
+       List<Color4d> colors;
+       List<Double> radiis;
+       Double radius = 1.0;
+       int resolution = 8;
+       
+       
+       public void setResolution(int resolution) {
+               if (resolution > 2)
+                       this.resolution = resolution;
+       }
+       
+       public void setVertices(List<Tuple3d> vertices) {
+               this.vertices = vertices;
+       }
+       
+       public void setTangents(List<Vector3d> tangents) {
+               this.tangents = tangents;
+       }
+
+       public void setColors(List<Color4d> colors) {
+               this.colors = colors;
+       }
+       
+       public void setRadiis(List<Double> radiis) {
+               this.radiis = radiis;
+       }
+       
+       public void setRadius(Double radius) {
+               this.radius = radius;
+       }
+       
+       
+       
+       public Mesh create() {
+               if (vertices.size() < 2 )
+                       throw new IllegalArgumentException("Tube must have at least two vertices");
+               
+               Vector3d t = new Vector3d();
+               
+               for (int i = 0; i < vertices.size() - 1; i++) {
+                       t.set(vertices.get(i+1));
+                       t.sub(vertices.get(i));
+                       if (t.lengthSquared() < 0.0001)
+                               throw new IllegalArgumentException("vertices at index " + i + " are too close to each other");
+               }
+               
+               List<Vector3d> points = new ArrayList<Vector3d>(vertices.size()*resolution);
+               List<Vector3d> normals = new ArrayList<Vector3d>(vertices.size()*resolution);
+               
+               for (int i = 0; i < vertices.size(); i++) {
+                       createCircle(i,points,normals);
+               }
+               
+               int index[] = new int[(vertices.size()-1)*resolution*6];
+               
+               createIndices(index);
+               List<Integer> indices = new ArrayList<Integer>();
+               for (int i = 0; i < index.length; i++) {
+                       indices.add(index[i]);
+               }
+               
+               
+               
+               vertices.clear();
+               if (colors != null)
+                       colors.clear();
+               if (radiis != null)
+                       radiis.clear();
+               
+               return Mesh.create(points, normals, indices);
+               
+       }
+       
+       private void createCircle(int i, List<Vector3d> points, List<Vector3d> normals) {
+               final Vector3d up = new Vector3d(0,1,0);
+               final Vector3d up2 = new Vector3d(0,0,1);
+               Tuple3d p = vertices.get(i);
+               Vector3d t = getTangent(i);
+               Vector3d n = new Vector3d();
+               if (Math.abs(up.dot(t)) < 0.99) {
+                       n.cross(up, t);
+               } else {
+                       n.cross(up2, t);
+               }
+               n.normalize();
+               if (radiis != null) {
+                       n.scale(radiis.get(i));
+               } else {
+                       n.scale(radius);
+               }
+               
+               for (int index = 0; index < resolution; index ++) {
+                       Vector3d v;
+                       if (index == 0) {
+                               v = new Vector3d(n);
+                               
+                       } else {
+                               AxisAngle4d aa = new AxisAngle4d(t, (Math.PI * 2 * (double)index)/(double)resolution);
+                               v = new Vector3d();
+                               MathTools.rotate(MathTools.getQuat(aa), n, v);
+                       }
+                       //int vIndex = (i*resolution + index)*3;
+                       Vector3d pt = new Vector3d(p);
+                       pt.add(v);
+                       //points.set(vIndex, pt);
+                       points.add(pt);
+                       v.normalize();
+                       //normals.set(vIndex, v);
+                       normals.add(v);
+               }
+       }
+       
+       private Vector3d getTangent(int i) {
+               Tuple3d p,n;
+               if (tangents != null)
+                       return tangents.get(i);
+               if (i == 0) {
+                       p = vertices.get(0);
+                       n = vertices.get(1);
+               } else if (i == vertices.size() - 1) {
+                       p = vertices.get(i-1);
+                       n = vertices.get(i);
+               } else {
+                       p = vertices.get(i-1);
+                       n = vertices.get(i+1);
+               }
+               Vector3d nn = new Vector3d(n);
+               nn.sub(p);
+               nn.normalize();
+               return nn;
+       }
+       
+       private void createIndices(int index[]) {
+               for (int c = 0; c < vertices.size() - 1; c++) {
+                       for (int s = 0; s < resolution; s++) {
+                               int ii = (c * resolution + s) * 6;
+                               int iv = c*resolution + s;
+                               
+                               /*
+                            iv+1 ---- iv + resolution + 1
+                                     | /|
+                                     |/ |
+                                  iv ---- iv + resolution 
+                               */
+                               if (s < resolution - 1) {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+resolution;
+                                       index[ii+0] = iv+resolution+1;
+                                       
+                                       index[ii+5] = iv;
+                                       index[ii+4] = iv+resolution+1;
+                                       index[ii+3] = iv+1;
+                               } else {
+                                       index[ii+2] = iv;
+                                       index[ii+1] = iv+resolution;
+                                       index[ii+0] = iv+1;
+                                       
+                                       index[ii+5] = iv;
+                                       index[ii+4] = iv+1;
+                                       index[ii+3] = iv+1-resolution;
+                               }
+                       }
+               }
+       }
+
+}