]> gerrit.simantics Code Review - simantics/3d.git/blob - org.simantics.g3d/src/org/simantics/g3d/shape/Mesh.java
ea6ec593f5702d3b17ab37ed79e9670b17446216
[simantics/3d.git] / org.simantics.g3d / src / org / simantics / g3d / shape / Mesh.java
1 /*******************************************************************************\r
2  * Copyright (c) 2012, 2013 Association for Decentralized Information Management in\r
3  * Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.g3d.shape;\r
13 \r
14 import java.util.ArrayList;\r
15 import java.util.List;\r
16 \r
17 import javax.vecmath.Quat4d;\r
18 import javax.vecmath.Vector3d;\r
19 \r
20 import org.simantics.g3d.math.MathTools;\r
21 \r
22 public class Mesh {\r
23         private List<Vector3d> vertices;\r
24         private List<Vector3d> normals;\r
25         private List<Color4d> colors;\r
26         private List<Integer> indices;\r
27         \r
28         \r
29         public Mesh(List<Vector3d> vertices, List<Vector3d> normals,\r
30                         List<Color4d> colors, List<Integer> indices) {\r
31                 this.vertices = vertices;\r
32                 this.normals = normals;\r
33                 this.colors = colors;\r
34                 this.indices = indices;\r
35         }\r
36         \r
37         public Mesh(List<Vector3d> vertices, List<Vector3d> normals, List<Integer> indices) {\r
38                 this.vertices = vertices;\r
39                 this.normals = normals;\r
40                 this.indices = indices;\r
41         }\r
42         \r
43         public Mesh(List<Vector3d> vertices, List<Integer> indices) {\r
44                 this.vertices = vertices;\r
45                 this.indices = indices;\r
46         }\r
47 \r
48         public List<Vector3d> getVertices() {\r
49                 return vertices;\r
50         }\r
51         \r
52         public List<Vector3d> getNormals() {\r
53                 return normals;\r
54         }\r
55         \r
56         public List<Integer> getIndices() {\r
57                 return indices;\r
58         }\r
59         \r
60         public List<Color4d> getColors() {\r
61                 return colors;\r
62         }\r
63         \r
64         public void createNormals() {\r
65                 normals = new ArrayList<Vector3d>(vertices.size());\r
66                 for (int i = 0; i < vertices.size(); i++) {\r
67                         normals.add(new Vector3d());\r
68                 }\r
69         Vector3d v1 = new Vector3d();\r
70         Vector3d v2 = new Vector3d();\r
71         Vector3d v3 = new Vector3d();\r
72         Vector3d t1 = new Vector3d();\r
73         Vector3d t2 = new Vector3d();\r
74         Vector3d n = new Vector3d();\r
75         for (int i = 0; i < indices.size(); i+=3) {\r
76             v1.set(vertices.get(i));\r
77             v2.set(vertices.get(i+1));\r
78             v3.set(vertices.get(i+2));\r
79             t1.sub(v3,v1);\r
80             t2.sub(v2,v1);\r
81             n.cross(t2, t1);\r
82             normals.get(i).add(n);\r
83             normals.get(i+1).add(n);\r
84             normals.get(i+2).add(n);\r
85         }\r
86         for (int i = 0; i < normals.size(); i++) {\r
87                 normals.get(i).normalize();\r
88         }\r
89     }\r
90         \r
91         public void translate(Vector3d v) {\r
92                 for (int i = 0; i < vertices.size(); i++) {\r
93                         vertices.get(i).add(v);\r
94                 }\r
95         }\r
96         \r
97         public void rotate(Quat4d q) {\r
98                 Vector3d t = new Vector3d();\r
99                 for (int i = 0; i < vertices.size(); i++) {\r
100                         MathTools.rotate(q, vertices.get(i), t);\r
101                         vertices.get(i).set(t);\r
102                 }\r
103                 \r
104                 if (normals != null) {\r
105                         for (int i = 0; i < normals.size(); i++) {\r
106                                 MathTools.rotate(q, normals.get(i), t);\r
107                                 t.normalize();\r
108                                 normals.get(i).set(t);\r
109                         }       \r
110                 }\r
111         }\r
112         \r
113         public void setColor(Color4d color) {\r
114                 colors = new ArrayList<Color4d>(vertices.size());\r
115                 for (int i = 0; i < vertices.size(); i++) {\r
116                         colors.add(color);\r
117                 }\r
118         }\r
119         \r
120         public void add(Mesh mesh) {\r
121                 int vindex = vertices.size();\r
122                 int triIndex = indices.size();\r
123                 vertices.addAll(mesh.getVertices());\r
124                 indices.addAll(mesh.indices);\r
125                 for (int i = triIndex; i < indices.size(); i++) {\r
126                         indices.set(i, indices.get(i)+vindex);\r
127                 }\r
128                 if (normals != null) {\r
129                         boolean hasNormals = true;\r
130                         if (mesh.getNormals() == null) {\r
131                                 mesh.createNormals();\r
132                                 hasNormals = false;\r
133                         }\r
134                         normals.addAll(mesh.getNormals());\r
135                         if (!hasNormals)\r
136                                 mesh.normals = null;\r
137                 }\r
138                 if (colors != null) {\r
139                         if (mesh.getColors() != null) {\r
140                                 colors.addAll(mesh.getColors());\r
141                         } else {\r
142                                 for (int i = 0; i < mesh.getVertices().size(); i++) {\r
143                                         colors.add(new Color4d(1,1,1,0));\r
144                                 }\r
145                         }\r
146                 }\r
147         }\r
148 }\r