-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMesh.pde
287 lines (239 loc) · 7.68 KB
/
Mesh.pde
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
class Mesh {
String name = "Mesh";
int[][][] faces;
Coord[] coords;
Coord[] texCoords;
Normal[] normals;
Mesh() {
}
Mesh(int[][][] faces, Coord[] coords,
Coord[] texCoords, Normal[] normals) {
referTo(name, faces, coords, texCoords, normals);
}
Mesh copyFrom(Mesh m) {
return copyFrom(m.name, m.faces, m.coords, m.texCoords, m.normals);
}
Mesh copyFrom(String name, int[][][] faces, Coord[] coords,
Coord[] texCoords, Normal[] normals) {
this.name = name;
int sz = faces.length;
this.faces = new int[sz][][];
for (int i = 0, j, k; i < sz; ++i) {
int sz1 = faces[i].length;
this.faces[i] = new int[sz1][];
for (j = 0; j < sz1; ++j) {
int sz2 = faces[i][j].length;
this.faces[i][j] = new int[sz2];
for (k = 0; k < sz2; ++k) {
this.faces[i][j][k] = faces[i][j][k];
}
}
}
sz = coords.length;
this.coords = new Coord[sz];
for (int i = 0; i < sz; ++i) {
this.coords[i] = new Coord();
this.coords[i].set(coords[i]);
}
sz = texCoords.length;
this.texCoords = new Coord[sz];
for (int i = 0; i < sz; ++i) {
this.texCoords[i] = new Coord();
this.texCoords[i].set(texCoords[i]);
}
sz = normals.length;
this.normals = new Normal[sz];
for (int i = 0; i < sz; ++i) {
this.normals[i] = new Normal();
this.normals[i].set(normals[i]);
}
return this;
}
Mesh fromObj(String filepath, Transform coordSystem,
Transform texCoordSystem) {
fromObj(filepath);
int sz = coords.length;
for (int i = 0; i < sz; ++i) {
coords[i].mult(coordSystem);
}
sz = texCoords.length;
for (int i = 0; i < sz; ++i) {
texCoords[i].mult(texCoordSystem);
}
sz = normals.length;
for (int i = 0; i < sz; ++i) {
normals[i].mult(coordSystem);
}
return this;
}
Mesh fromObj(String filepath) {
String[] lines = loadStrings(filepath);
String[] tokens;
String[] facetokens;
ArrayList < Coord > coords = new ArrayList < Coord > ();
ArrayList < Coord > texCoords = new ArrayList < Coord > ();
ArrayList < Normal > normals = new ArrayList < Normal > ();
ArrayList < int[][] > faces = new ArrayList < int[][] > ();
for (int i = 0, sz = lines.length, j; i < sz; ++i) {
// Split line by spaces.
tokens = lines[i].split("\\s+");
// Skip empty lines.
if (tokens.length > 0) {
if (tokens[0].equals("o")) {
// Assign name.
name = tokens[1];
} else if (tokens[0].equals("v")) {
// Coordinate.
Coord read = new Coord(
float(tokens[1]), float(tokens[2]), float(tokens[3]));
coords.add(read);
} else if (tokens[0].equals("vt")) {
// Texture coordinate.
Coord read = new Coord(float(tokens[1]), float(tokens[2]));
texCoords.add(read);
} else if (tokens[0].equals("vn")) {
// Normal.
Normal read = new Normal(
float(tokens[1]), float(tokens[2]), float(tokens[3]));
normals.add(read);
} else if (tokens[0].equals("f")) {
// Face.
int count = tokens.length;
// tokens length includes "f", and so is 1 longer.
int[][] indices = new int[count - 1][3];
// Simplified version. Assumes (incorrectly) that face
// will always be formatted as "v/vt/vn".
for (j = 1; j < count; ++j) {
facetokens = tokens[j].split("/");
// Indices in .obj file start at 1, not 0.
indices[j - 1][0] = int(facetokens[0]) - 1;
indices[j - 1][1] = int(facetokens[1]) - 1;
indices[j - 1][2] = int(facetokens[2]) - 1;
}
faces.add(indices);
}
}
}
// Convert to fixed-sized array.
this.faces = faces.toArray(new int[faces.size()][][]);
this.coords = coords.toArray(new Coord[coords.size()]);
this.texCoords = texCoords.toArray(new Coord[texCoords.size()]);
this.normals = normals.toArray(new Normal[normals.size()]);
return this;
}
Face getFace(int i) {
return getFace(i, new Face());
}
Face getFace(int i, Face out) {
int sz = faces[i].length;
Vertex[] vertices = new Vertex[sz];
for (int j = 0; j < sz; ++j) {
vertices[j] = getVertex(i, j, new Vertex());
}
return out.referTo(vertices);
}
Face[] getFaces() {
int sz0 = faces.length;
Face[] result = new Face[sz0];
for (int i = 0, j, sz1; i < sz0; ++i) {
sz1 = faces[i].length;
Vertex[] verts = new Vertex[sz1];
for (j = 0; j < sz1; ++j) {
verts[j] = getVertex(i, j, new Vertex());
}
result[i] = new Face(verts);
}
return result;
}
Face[] getFaces(Comparator < Face > sortingMethod) {
Face[] result = getFaces();
Arrays.sort(result, sortingMethod);
return result;
}
Vertex getVertex(int i, int j) {
return getVertex(i, j, new Vertex());
}
Vertex getVertex(int i, int j, Vertex out) {
return out.referTo(
coords[faces[i][j][0]],
texCoords[faces[i][j][1]],
normals[faces[i][j][2]]);
}
Vertex[] getVertices() {
ArrayList < Vertex > result = new ArrayList < Vertex > ();
Vertex v;
for (int i = 0, sz0 = faces.length, j, sz1; i < sz0; ++i) {
for (j = 0, sz1 = faces[i].length; j < sz1; ++j) {
v = getVertex(i, j, new Vertex());
if (!result.contains(v)) {
result.add(v);
}
}
}
return result.toArray(new Vertex[result.size()]);
}
Vertex[] getVertices(Comparator < Vertex > sortingMethod) {
Vertex[] result = getVertices();
Arrays.sort(result, sortingMethod);
return result;
}
Mesh referTo(Mesh m) {
return referTo(m.name, m.faces, m.coords, m.texCoords, m.normals);
}
Mesh referTo(String name, int[][][] faces, Coord[] coords,
Coord[] texCoords, Normal[] normals) {
this.name = name;
this.faces = faces;
this.coords = coords;
this.texCoords = texCoords;
this.normals = normals;
return this;
}
void toObj(String filename) {
saveStrings(filename, toObjStrings());
println(filename, "saved", Calendar.getInstance().getTime());
}
String[] toObjStrings() {
int coordsLen = coords.length;
int texCoordsLen = texCoords.length;
int normalsLen = normals.length;
int facesLen = faces.length;
String[] result = new String[2 +
coordsLen + texCoordsLen + normalsLen + facesLen];
result[0] = String.format("# v: %d, vt: %d, vn: %d, f: %d",
coordsLen, texCoordsLen, normalsLen, facesLen);
result[1] = "o " + name;
// Write coordinates.
int offset = 2;
for (int i = 0; i < coordsLen; ++i) {
result[offset + i] = String.format("v %.6f %.6f %.6f",
coords[i].x, coords[i].y, coords[i].z);
}
// Write texture coordinates.
offset += coordsLen;
for (int i = 0; i < texCoordsLen; ++i) {
result[offset + i] = String.format("vt %.6f %.6f",
texCoords[i].x, texCoords[i].y);
}
// Write normals.
offset += texCoordsLen;
for (int i = 0; i < normalsLen; ++i) {
result[offset + i] = String.format("vn %.6f %.6f %.6f",
normals[i].x, normals[i].y, normals[i].z);
}
// Write face indices.
StringBuilder sb;
String faceFmt = "%d/%d/%d ";
offset += normalsLen;
for (int i = 0, j, vcount; i < facesLen; ++i) {
sb = new StringBuilder("f ");
for (j = 0, vcount = faces[i].length; j < vcount; ++j) {
// Indices in .obj file start at 1, not 0.
sb.append(String.format(faceFmt,
faces[i][j][0] + 1, faces[i][j][1] + 1, faces[i][j][2] + 1));
}
result[offset + i] = sb.toString();
}
return result;
}
}