-
Notifications
You must be signed in to change notification settings - Fork 0
/
geometry.pde
442 lines (390 loc) · 11.3 KB
/
geometry.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// geometry related functions
int pRotX;
int pRotY;
int pRotZ;
int pLocX;
int pLocY;
int pLocZ;
int worldX = 1200;
int worldY = 800;
int worldZ = 1000;
int gridSize = 50;
float tightness = .25;
int segmentRes = 8;
int curveRes = 8;
int cylinderRes = 8;
int sphereRes = 8;
int faceNum = 0;
int vertexNum = 0;
long lastOp;
AABB worldBox;
Vec3D mapDim = new Vec3D(worldX, worldY, worldZ);
AABB[] worldLimits = new AABB[6];
Vec3D[] normalList = new Vec3D[6];
boolean doRebuild = false;
ArrayList <Furniture> furnitureList;
ArrayList <Material> materialList;
class Material {
HE_Mesh heMesh = null; // = new HE_Mesh(); // he_mesh
TriangleMesh toxicMesh = null; // = new TriangleMesh(); // toxic
PShape shapeMesh;// = new PShape();
String name;
int fillColor;
int alphaColor;
boolean softMaterial;
void updateMaterial() {
shapeMesh = new PShape();
if (toxicMesh != null || heMesh != null) {
shapeMesh = meshToRetained(toxicMesh, heMesh, soft&&softMaterial);
}
}
PShape meshToRetained(Mesh3D toxicMesh, HE_Mesh heMesh, boolean smth) {
PShape retained = createShape(TRIANGLES);
retained.enableStyle();
if (solid) {
if (material) {
retained.fill(fillColor, solidAlpha);
}
else {
retained.fill(solidColor, solidAlpha);
}
}
else {
retained.noFill();
}
if (wireframe) {
retained.stroke(wireframeColor, wireframeAlpha);
}
else {
retained.noStroke();
}
retained.ambient(ambientLightColor, ambientLightColor, ambientLightColor);
if (smth) {
if (toxicMesh != null) {
toxicMesh.computeVertexNormals();
for (Face f : toxicMesh.getFaces()) {
retained.normal(f.a.normal.x, f.a.normal.y, f.a.normal.z);
retained.vertex(f.a.x, f.a.y, f.a.z);
retained.normal(f.b.normal.x, f.b.normal.y, f.b.normal.z);
retained.vertex(f.b.x, f.b.y, f.b.z);
retained.normal(f.c.normal.x, f.c.normal.y, f.c.normal.z);
retained.vertex(f.c.x, f.c.y, f.c.z);
}
}
if (heMesh != null) {
HE_Mesh triMesh = heMesh.get();
triMesh.triangulate();
HE_Face thisFace;
for (Iterator<HE_Face> faceItr = triMesh.fItr(); faceItr.hasNext();) {
thisFace = faceItr.next();
HE_Halfedge he = thisFace.getHalfedge();
HE_Vertex vx;
do {
vx = he.getVertex();
WB_Normal3d vn = vx.getVertexNormal();
retained.normal(vn.xf(), vn.yf(), vn.zf());
retained.vertex(vx.xf(), vx.yf(), vx.zf());
he = he.getNextInFace();
}
while (he != thisFace.getHalfedge ());
}
}
}
else {
if (toxicMesh != null) {
for (Face f : toxicMesh.getFaces()) {
retained.normal(f.normal.x, f.normal.y, f.normal.z);
retained.vertex(f.a.x, f.a.y, f.a.z);
retained.vertex(f.b.x, f.b.y, f.b.z);
retained.vertex(f.c.x, f.c.y, f.c.z);
}
}
if (heMesh != null) {
HE_Mesh triMesh = heMesh.get();
triMesh.triangulate();
HE_Face thisFace;
for (Iterator<HE_Face> faceItr = triMesh.fItr(); faceItr.hasNext();) {
thisFace = faceItr.next();
HE_Halfedge he = thisFace.getHalfedge();
HE_Vertex vx;
WB_Normal3d vn = thisFace.getFaceNormal();
retained.normal(vn.xf(), vn.yf(), vn.zf());
do {
vx = he.getVertex();
retained.vertex(vx.xf(), vx.yf(), vx.zf());
he = he.getNextInFace();
}
while (he != thisFace.getHalfedge ());
}
}
}
retained.end();
return retained;
}
}
void addMesh(HE_Mesh thisMesh, String thisName) {
for (Material thisMaterial : materialList) {
if (thisMaterial.name == thisName) {
if (thisMaterial.heMesh == null) {
thisMaterial.heMesh = new HE_Mesh();
}
thisMaterial.heMesh.add(thisMesh);
}
}
}
void addMesh(TriangleMesh thisMesh, String thisName) {
for (Material thisMaterial : materialList) {
if (thisMaterial.name == thisName) {
if (thisMaterial.toxicMesh == null) {
thisMaterial.toxicMesh = new TriangleMesh();
}
thisMaterial.toxicMesh.addMesh(thisMesh);
}
}
}
Material getMaterial(String thisName) {
for (Material thisMaterial : materialList) {
if (thisMaterial.name == thisName) {
return thisMaterial;
}
}
return null;
}
void updateMaterials() {
for (Material thisMaterial : materialList) {
thisMaterial.updateMaterial();
}
}
void initMaterials() {
materialList = new ArrayList();
addMaterial("model", 200, 255, false);
addMaterial("metal", 255, 255, true);
addMaterial("leather", 50, 255, false);
addMaterial("geometry", 200, 255, false);
addMaterial("wood", 150, 255, false);
addMaterial("white", 255, 255, false);
addMaterial("grey", 150, 255, false);
addMaterial("black", 50, 255, false);
addMaterial("red", color(200, 0, 0), 255, false);
addMaterial("blue", color(0, 0, 200), 255, true);
addMaterial("green", color(0, 255, 0), 255, false);
addMaterial("yellow", color(240, 240, 0), 255, false);
addMaterial("glass", 255, 100, true);
}
void addMaterial(String thisName, int thisColor, int thisAlpha, boolean thisSoft) {
Material newMaterial = new Material();
newMaterial.name = thisName;
newMaterial.fillColor = thisColor;
newMaterial.alphaColor = thisAlpha;
newMaterial.softMaterial = thisSoft;
materialList.add(newMaterial);
}
void initGeometry() {
prepareNormalList();
initMaterials();
updateWorld();
furnitureList = new ArrayList<Furniture>();
Furniture test = new Test();
test.name ="test";
furnitureList.add(test);
Furniture breuer = new Breuer();
breuer.name ="breuer";
furnitureList.add(breuer);
// Furniture rietveld = new Rietveld();
// rietveld.name ="rietveld";
// furnitureList.add(rietveld);
//
// Furniture lack = new Lack();
// lack.name ="lack";
// furnitureList.add(lack);
// Furniture thonet = new Thonet();
// thonet.name ="thonet";
// furnitureList.add(thonet);
//
// Furniture vase = new Vase();
// vase.name ="vase";
// furnitureList.add(vase);
//
// Furniture radiolaria = new Radiolaria();
// radiolaria.name ="radiolaria";
// furnitureList.add(radiolaria);
//
// Furniture jewel = new Jewel();
// jewel.name ="jewel";
// furnitureList.add(jewel);
//
// Furniture castiglioni = new Castiglioni();
// castiglioni.name ="castiglioni";
// furnitureList.add(castiglioni);
updateWorld();
generateGeometry();
updateGeometry();
buildGeometry();
}
void changeCollection () {
furniture = (furniture+1)%(furnitureList.size());
updateWorld();
if (!furnitureList.get(furniture).generated) {
generateGeometry();
}
updateGeometry();
}
void resetGeometry() {
furnitureList.get(furniture).reset();
generateGeometry();
updateGeometry();
}
void randomGeometry() {
furnitureList.get(furniture).randomize();
generateGeometry();
updateGeometry();
}
void generateGeometry() {
//gridSize = furnitureList.get(furniture).gridSize;
long startTime = millis();
furnitureList.get(furniture).generate();
lastOp = millis() - startTime;
buildGeometry();
}
boolean isGeometryValidated() {
if (furnitureList.get(furniture).validated) {
return true;
}
return false;
}
void updateGeometry() {
furnitureList.get(furniture).update();
doRebuild = true;
}
void buildGeometry() {
if (solid || wireframe) {
for (Material thisMaterial : materialList) {
thisMaterial.toxicMesh = null; //.clear();
thisMaterial.heMesh = null; //clear();
}
furnitureList.get(furniture).build();
doRebuild = false;
faceNum = 0;
vertexNum = 0;
for (Material thisMaterial : materialList) {
if (thisMaterial.toxicMesh != null) {
faceNum += thisMaterial.toxicMesh.getNumFaces();
vertexNum += thisMaterial.toxicMesh.getNumVertices();
}
}
updateMaterials();
}
else {
doRebuild = true;
}
}
void displayGeometry() {
if (ground) {
pushMatrix();
translate(0, 0, -.1);
if (furnitureList.get(furniture).name.equals("breuer")) {
translate(0, 0, -bTubeR);
}
noStroke();
fill(groundColor);
rect(-mapDim.x/2, -mapDim.y/2, mapDim.x, mapDim.y);
popMatrix();
}
if (grid) {
pushMatrix();
if (furnitureList.get(furniture).name.equals("breuer")) {
translate(0, 0, -bTubeR);
}
drawGrid();
popMatrix();
}
if (axis) {
strokeWeight(thinStroke);
fx.origin(100);
}
if (limits) {
drawLimits();
}
if (light) {
ambientLight(ambientLightColor, ambientLightColor, ambientLightColor);
directionalLight(directionalLightColor, directionalLightColor, directionalLightColor, 0, -.4, -.4);
directionalLight(200, 200, 200, 0, .3, -.3);
}
else {
noLights();
}
if (!furnitureList.get(furniture).generated) {
furnitureList.get(furniture).generate();
doRebuild = true;
}
//if (dots || structure || filled || details || original) { // this so radiolaria animation works, correct
furnitureList.get(furniture).display();
// }
if (wireframe || solid) {
if (doRebuild) {
buildGeometry();
}
for (Material thisMaterial : materialList) {
shape(thisMaterial.shapeMesh);
}
}
}
void updateWorld() {
worldBox = new AABB(new Vec3D(0, 0, mapDim.z/2), mapDim.scale(.5));
worldLimits[0] = new AABB(new Vec3D(0, 0, -gridSize/2), new Vec3D(mapDim.x/2, mapDim.y/2, gridSize/2));
worldLimits[1] = new AABB(new Vec3D(0, 0, mapDim.z+gridSize/2), new Vec3D(mapDim.x/2, mapDim.y/2, gridSize/2));
worldLimits[2] = new AABB(new Vec3D(mapDim.x/2+gridSize/2, 0, mapDim.z/2), new Vec3D(gridSize/2, mapDim.y/2, mapDim.z/2));
worldLimits[3] = new AABB(new Vec3D(-mapDim.x/2-gridSize/2, 0, mapDim.z/2), new Vec3D(gridSize/2, mapDim.y/2, mapDim.z/2));
worldLimits[4] = new AABB(new Vec3D(0, mapDim.y/2+gridSize/2, mapDim.z/2), new Vec3D(mapDim.x/2, gridSize/2, mapDim.z/2));
worldLimits[5] = new AABB(new Vec3D(0, -mapDim.y/2-gridSize/2, mapDim.z/2), new Vec3D(mapDim.x/2, gridSize/2, mapDim.z/2));
}
void saveSTL() {
printConsole("saved model at "+nf(hour(), 2)+":"+nf(minute(), 2)+":"+nf(second(), 2)+";");
for (Material thisMaterial : materialList) {
TriangleMesh thisToxicMesh = thisMaterial.toxicMesh;
if (thisToxicMesh != null) {
//if (thisToxicMesh.getNumFaces()>0) {
// thisToxicMesh.computeFaceNormals();
thisToxicMesh.saveAsSTL(sketchPath("")+"/models/"+getTimeStamp()+"_"+furnitureList.get(furniture).name+"_"+thisMaterial.name+".stl");
// }
}
}
}
class Furniture {
List <Part> partList = new ArrayList<Part>();
Vec3D loc = Vec3D.ZERO.copy();
Vec3D rot = Vec3D.ZERO.copy();
String name;
boolean generated = false;
boolean validated = false;
Object getRef() {
return this;
}
void reset() {
}
void randomize() {
}
void generate() {
}
void update() {
for (Part thisPart : partList) {
thisPart.update();
}
}
void display() {
for (Part thisPart : partList) {
thisPart.display();
}
}
void build() {
for (Part thisPart : partList) {
thisPart.build();
}
}
void setLocation (ReadonlyVec3D loc) {
this.loc = loc.copy(); // not yet implementedx1
}
void setRotation(ReadonlyVec3D rot) {
this.rot = rot.copy(); // not yet implemented
}
}