]> gerrit.simantics Code Review - simantics/3d.git/blobdiff - org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java
Mesh API to use Tuple3d instead of Vector3d
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / shape / Mesh.java
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));
+                               }
+                       }
+               }
+       }
+}