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