-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparts.pde
1140 lines (998 loc) · 32.5 KB
/
parts.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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class Part {
long id;
Vec3D loc = Vec3D.ZERO.copy();
Vec3D rot = Vec3D.ZERO.copy();
String material = "model";
void setLocation(Vec3D loc) {
this.loc = loc;
}
void setRotation(Vec3D rot) {
this.rot = rot;
}
void setMaterial(String material) {
this.material = material;
}
void build() {
}
void display() {
}
void update() {
}
}
class SuperBlock extends Part {
//float axisLength;
Vec3D dim;
AABB myBox;
Line3D myAxis;
AABB topCap; // to paint yellow, faces of the cube on opposite sides of axis !!
AABB bottomCap;
float capWidth = .01;
ArrayList <Vec3D> joints;
ArrayList <Vec3D> options;
ArrayList <Line3D> edgeList;
SuperBlock () {
id = millis();
myBox = new AABB(Vec3D.ZERO, Vec3D.ZERO);
dim = Vec3D.ZERO.copy();
}
void display() {
if (dots) {
// stroke(redColor, 255);
strokeWeight(markerStroke);
// drawMarker(loc);
for (Vec3D thisPoint : joints) {
stroke(redColor, 255);
drawMarker(thisPoint.add(loc));
}
for (Vec3D thisPoint : options) {
stroke(blueColor, 150);
drawMarker(thisPoint.add(loc));
}
}
if (structure) {
stroke(color(strokeColor, 0, 0), strokeAlpha);
strokeWeight(thinStroke);
if (filled) {
fill(fillColor, fillAlpha);
}
else {
noFill();
}
pushMatrix();
translate(loc.x, loc.y, loc.z);
box(dim.x, dim.y, dim.z);
popMatrix();
}
if (details) {
stroke(greenColor, strokeAlpha);
strokeWeight(thickStroke);
for (Line3D thisEdge : edgeList) {
if ( thisEdge != null) {
// fx.line(thisEdge);
}
}
stroke(redColor, strokeAlpha);
strokeWeight(thinStroke);
fx.line(myAxis);
}
}
void build() {
if (material == "black" && furnitureList.get(furniture).name == "rietveld") { // bad hack !
TriangleMesh topCapMesh =(TriangleMesh)topCap.toMesh();
addMesh(topCapMesh, "yellow");
TriangleMesh bottomCapMesh =(TriangleMesh)bottomCap.toMesh();
addMesh(bottomCapMesh, "yellow");
}
TriangleMesh blockMesh=(TriangleMesh)myBox.toMesh();
addMesh(blockMesh, material);
}
void setLocation(Vec3D loc) {
this.loc = loc;
myBox.set(loc);
myBox.updateBounds() ; // important, in javadocs says it's called automatically, but .... check !!!
updateAxis();
}
void setDimension(Vec3D dim) {
this.dim = dim;
myBox.setExtent(dim.scale(.499)); // it should be .5, down to .4999 so adjacent AABB boxes do not collide !!!
updateAxis();
}
void calculatePoints(boolean forceCorner, boolean forceNextCorner, boolean forceCornerPerpendicular) {
joints = new ArrayList<Vec3D>();
options = new ArrayList<Vec3D>();
for (float thisX=(gridSize/2)-dim.x/2; thisX < dim.x/2; thisX += gridSize) {
for (float thisY=(gridSize/2)-dim.y/2; thisY < dim.y/2; thisY += gridSize) {
for (float thisZ=(gridSize/2)-dim.z/2; thisZ < dim.z/2; thisZ += gridSize) {
Vec3D joint = new Vec3D (thisX, thisY, thisZ);
if (!forceCorner && !forceNextCorner) {
joints.add(joint);
}
if (forceCorner && // keep only points on the corner
(thisX == -dim.x/2+gridSize/2 || thisX == dim.x/2-gridSize/2) &&
(thisY == -dim.y/2+gridSize/2 || thisY == dim.y/2-gridSize/2) &&
(thisZ == -dim.z/2+gridSize/2 || thisZ == dim.z/2-gridSize/2)) {
joints.add(joint);
}
if (forceNextCorner && ( // keep only points next to corner
(thisX == (-dim.x/2+gridSize/2)+gridSize || thisX == (dim.x/2-gridSize/2)-gridSize) ||
(thisY == (-dim.y/2+gridSize/2)+gridSize || thisY == (dim.y/2-gridSize/2)-gridSize) ||
(thisZ == (-dim.z/2+gridSize/2)+gridSize || thisZ == (dim.z/2-gridSize/2)-gridSize))) {
joints.add(joint);
}
if (forceCornerPerpendicular && // keep only points perpendicular to corner
(thisX == -dim.x/2+gridSize/2 || thisX == dim.x/2-gridSize/2) &&
(thisY == -dim.y/2+gridSize/2 || thisY == dim.y/2-gridSize/2) &&
(thisZ == -dim.z/2+gridSize/2 || thisZ == dim.z/2-gridSize/2)) {
joints.add(joint);
}
}
}
}
for (Vec3D thisJoint : joints) {
for (int i=0;i<6;i++) {
Vec3D thisNormal = normalList[i];
float ox = thisJoint.x + (gridSize*thisNormal.x);
float oy = thisJoint.y + (gridSize*thisNormal.y);
float oz = thisJoint.z + (gridSize*thisNormal.z);
Vec3D option = new Vec3D (ox, oy, oz);
if (!option.add(loc).isInAABB(myBox)) {
if (checkVectorWithinWorld(option.add(loc))) { // check points are within world limits
if (!forceCornerPerpendicular || checkLinesCoincident(myAxis, new Line3D(myAxis.a, option.add(loc)))) {
options.add(option);
}
}
}
}
}
}
void updateAxis() {
// X
myAxis = null;
if (dim.x > dim.z && dim.x > dim.y) {
Vec3D a = loc.add(new Vec3D(dim.x/2, 0, 0));
Vec3D b = loc.sub(new Vec3D(dim.x/2, 0, 0));
myAxis = new Line3D(a, b);
topCap = new AABB (a, new Vec3D(capWidth, dim.y/2, dim.z/2));
bottomCap = new AABB (b, new Vec3D(capWidth, dim.y/2, dim.z/2));
edgeList = new ArrayList<Line3D>();
edgeList.add(new Line3D(myAxis.a.add(new Vec3D(0, dim.y/2, dim.z/2)), myAxis.b.add(new Vec3D(0, dim.y/2, dim.z/2)))) ;
edgeList.add(new Line3D(myAxis.a.add(new Vec3D(0, -dim.y/2, dim.z/2)), myAxis.b.add(new Vec3D(0, -dim.y/2, dim.z/2)))) ;
}
// Y
if (dim.y > dim.z && dim.y > dim.x) {
Vec3D a = loc.add(new Vec3D(0, dim.y/2, 0));
Vec3D b = loc.sub(new Vec3D(0, dim.y/2, 0));
myAxis = new Line3D(a, b);
topCap = new AABB (a, new Vec3D(dim.x/2, capWidth, dim.z/2));
bottomCap = new AABB (b, new Vec3D(dim.x/2, capWidth, dim.z/2));
edgeList = new ArrayList<Line3D>();
edgeList.add(new Line3D(myAxis.a.add(new Vec3D(dim.x/2, 0, dim.z/2)), myAxis.b.add(new Vec3D(dim.x/2, 0, dim.z/2)))) ;
edgeList.add(new Line3D(myAxis.a.add(new Vec3D(-dim.x/2, 0, dim.z/2)), myAxis.b.add(new Vec3D(-dim.x/2, 0, dim.z/2)))) ;
}
// Z
if (dim.z > dim.y && dim.z > dim.x) {
Vec3D a = loc.add(new Vec3D(0, 0, dim.z/2));
Vec3D b = loc.sub(new Vec3D(0, 0, dim.z/2));
myAxis = new Line3D(a, b);
topCap = new AABB (a, new Vec3D(dim.x/2, dim.y/2, capWidth));
bottomCap = new AABB (b, new Vec3D(dim.x/2, dim.y/2, capWidth));
edgeList = new ArrayList<Line3D>();
// no top edges as it is vertical !!
}
// SQUARE BASE, axis is on different side
if (myAxis == null) {
if (dim.x == dim.y) {
Vec3D a = loc.add(new Vec3D(0, 0, dim.z/2));
Vec3D b = loc.sub(new Vec3D(0, 0, dim.z/2));
myAxis = new Line3D(a, b);
edgeList = new ArrayList<Line3D>();
}
if (dim.x == dim.z) {
Vec3D a = loc.add(new Vec3D(0, dim.y/2, 0));
Vec3D b = loc.sub(new Vec3D(0, dim.y/2, 0));
myAxis = new Line3D(a, b);
edgeList = new ArrayList<Line3D>();
}
if (dim.z == dim.y) {
Vec3D a = loc.add(new Vec3D(dim.x/2, 0, 0));
Vec3D b = loc.sub(new Vec3D(dim.x/2, 0, 0));
myAxis = new Line3D(a, b);
edgeList = new ArrayList<Line3D>();
}
}
}
}
class BlockShape extends Part {
Vec3D a, b, c, d;
int thickness;
boolean extrudeInBothDirections = false;
Vec3D aa, bb, cc, dd, ee, ff, gg, hh;
BlockShape () {
}
boolean isAboveGround() {
return aa.z > 0 && bb.z > 0 && cc.z > 0 && dd.z > 0 && ee.z > 0 && ff.z > 0 && gg.z > 0 && hh.z > 0;
}
boolean isWithinWorld() {
return checkVectorWithinWorld(aa) && checkVectorWithinWorld(bb) && checkVectorWithinWorld(cc) && checkVectorWithinWorld(dd)
&& checkVectorWithinWorld(ee) && checkVectorWithinWorld(ff) && checkVectorWithinWorld(gg) && checkVectorWithinWorld(hh);
}
boolean intersectsBlockShapeList(ArrayList<BlockShape> thisBlockList, int thisStep) {
for (BlockShape thisBlock : thisBlockList) {
if (intersectsBlockShape(thisBlock, thisStep)) {
return true;
}
}
return false;
}
boolean intersectsBlockShape(BlockShape thisBlock, int thisStep) { /// really harsh approximation to collision, only works when planes are coincident!
ArrayList<Vec3D> pointsAB = getPointsFromLine(new Line3D(aa, bb), thisStep, true);
ArrayList<Vec3D> pointsDC = getPointsFromLine(new Line3D(dd, cc), thisStep, true);
// ArrayList<Vec3D> pointsEF = getPointsFromLine(new Line3D(ee, ff), thisStep, true);
// ArrayList<Vec3D> pointsHG = getPointsFromLine(new Line3D(hh, gg), thisStep, true);
ArrayList<Vec3D> thisPointsAD = getPointsFromLine(new Line3D(thisBlock.aa, thisBlock.dd), thisStep, true);
ArrayList<Vec3D> thisPointsBC = getPointsFromLine(new Line3D(thisBlock.bb, thisBlock.cc), thisStep, true);
// ArrayList<Vec3D> thisPointsEF = getPointsFromLine(new Line3D(thisBlock.ee, thisBlock.ff), thisStep, true);
// ArrayList<Vec3D> thisPointsHG = getPointsFromLine(new Line3D(thisBlock.hh, thisBlock.gg), thisStep, true);
for (int i=0;i<pointsAB.size();i++) {
Line3D tempLine = new Line3D(pointsAB.get(i), pointsDC.get(i));
for (int j=0;j<thisPointsAD.size();j++) {
Line3D thisTempLine = new Line3D(thisPointsAD.get(j), thisPointsBC.get(j));
if (checkLinesCollision(tempLine, thisTempLine)) {
return true;
}
}
}
return false;
}
boolean intersectsBlockList(ArrayList<SuperBlock> thisBlockList, int thisStep) {
for (SuperBlock thisBlock : thisBlockList) {
if (intersectsBlock(thisBlock, thisStep)) {
return true;
}
}
return false;
}
boolean intersectsBlock(SuperBlock thisBlock, int thisStep) {
// println(aa+" "+bb+" "+cc+" "+dd+" "+ee+" "+ff+" "+gg+" "+hh);
ArrayList<Vec3D> pointsAB = getPointsFromLine(new Line3D(aa, bb), thisStep, true);
ArrayList<Vec3D> pointsDC = getPointsFromLine(new Line3D(dd, cc), thisStep, true);
// println(pointsAB.size()+" "+pointsDC.size());
for (int i=0;i<pointsAB.size();i++) {
Line3D tempLine = new Line3D(pointsAB.get(i), pointsDC.get(i));
if (checkLineBlockCollision(tempLine, thisBlock)) {
return true;
}
}
ArrayList<Vec3D> pointsEF = getPointsFromLine(new Line3D(ee, ff), thisStep, true);
ArrayList<Vec3D> pointsHG = getPointsFromLine(new Line3D(hh, gg), thisStep, true);
// println(pointsEF.size()+" "+pointsHG.size());
for (int i=0;i<pointsEF.size();i++) {
Line3D tempLine = new Line3D(pointsEF.get(i), pointsHG.get(i));
if (checkLineBlockCollision(tempLine, thisBlock)) {
return true;
}
}
return false;
}
void calculate() {
Triangle3D thisTriangle = new Triangle3D(a, b, c);
Vec3D nor = thisTriangle.computeNormal().normalize();
Vec3D offsetA;
Vec3D offsetB;
if (extrudeInBothDirections) {
offsetA = nor.copy().scaleSelf(thickness/2);
offsetB = nor.getInverted();
}
else {
offsetA = new Vec3D(0, 0, 0);
offsetB = nor.copy().scaleSelf(thickness);
}
aa = a.copy().addSelf(offsetA);
bb = b.copy().addSelf(offsetA);
cc = c.copy().addSelf(offsetA);
dd = d.copy().addSelf(offsetA);
ee = a.copy().addSelf(offsetB);
ff = b.copy().addSelf(offsetB);
gg = c.copy().addSelf(offsetB);
hh = d.copy().addSelf(offsetB);
}
void display() {
if (structure) {
stroke(color(strokeColor, 0, 0), strokeAlpha);
strokeWeight(1);
// noFill();
if (filled) {
fill(fillColor, fillAlpha);
}
else {
noFill();
}
beginShape(QUAD_STRIP);
vertex(aa.x, aa.y, aa.z);
vertex(bb.x, bb.y, bb.z);
vertex(dd.x, dd.y, dd.z);
vertex(cc.x, cc.y, cc.z);
endShape();
beginShape(QUAD_STRIP);
vertex(ee.x, ee.y, ee.z);
vertex(ff.x, ff.y, ff.z);
vertex(hh.x, hh.y, hh.z);
vertex(gg.x, gg.y, gg.z);
endShape();
// maybe add other side ?
}
// stroke(0, 255, 0);
// ArrayList<Vec3D> pointsAB = getPointsFromLine(new Line3D(aa, bb), 5, true);
// ArrayList<Vec3D> pointsDC = getPointsFromLine(new Line3D(dd, cc), 5, true);
// for (int i=0;i<pointsAB.size();i++) {
// Line3D tempLine = new Line3D(pointsAB.get(i), pointsDC.get(i));
// fx.line(tempLine);
// }
}
void build() {
addMesh(createBlockMeshFromPlane(a, b, c, d, thickness, false), material);
}
void setCorners(Vec3D a, Vec3D b, Vec3D c, Vec3D d) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
class Block extends Part {
Vec3D dim;
Block () {
id = millis();
}
void display() {
pushMatrix();
if (dots) {
stroke(redColor);
strokeWeight(markerStroke);
drawMarker(loc);
}
if (structure) {
stroke(color(strokeColor, 0, 0), strokeAlpha);
if (filled) {
fill(fillColor, fillAlpha);
}
else {
noFill();
}
translate(loc.x, loc.y, loc.z);
rotateX(rot.x);
rotateY(rot.y);
rotateZ(rot.z);
box(dim.x, dim.y, dim.z);
}
popMatrix();
}
void build() {
// 1 first option with axis aligned box and rotated mesh
AABB block=new AABB(new Vec3D(0, 0, 0), dim.scale(.5));
TriangleMesh blockMesh=(TriangleMesh)block.toMesh();
blockMesh.rotateZ(rot.z);
blockMesh.rotateY(rot.y);
blockMesh.rotateX(rot.x);
blockMesh.translate(loc);
addMesh(blockMesh, material);
//2 second option creating mesh from plane
// Vec3D aa = locateVector(Vec3D.ZERO, rot, new Vec3D(-dim.x/2, -dim.y/2, 0));
// Vec3D bb = locateVector(Vec3D.ZERO, rot, new Vec3D(dim.x/2, -dim.y/2, 0));
// Vec3D cc = locateVector(Vec3D.ZERO, rot, new Vec3D(dim.x/2, dim.y/2, 0));
// Vec3D dd = locateVector(Vec3D.ZERO, rot, new Vec3D(-dim.x/2, dim.y/2, 0));
// TriangleMesh blockShapeMesh = createBlockMeshFromPlane(aa, bb, cc, dd, (int)dim.z);
// blockShapeMesh.translate(loc);
// meshList[material].addMesh(blockShapeMesh);
}
void setDimension(Vec3D dim) {
this.dim = dim;
}
void setRotation(Vec3D rot) {
this.rot = rot;
}
}
class Cylinder extends Part {
float r, h;
Cylinder () {
id = millis();
}
void display() {
pushMatrix();
translate(loc.x, loc.y, loc.z);
rotateX(rot.x);
rotateY(rot.y);
rotateZ(rot.z);
if (dots) {
strokeWeight(markerStroke);
stroke(color(strokeColor, 0, 0), strokeAlpha);
drawMarker(Vec3D.ZERO.copy());
drawMarker(new Vec3D(0, 0, -h/2));
drawMarker(new Vec3D(0, 0, h/2));
}
if (structure) {
// fill(redColor, solidAlpha);
noFill();
stroke(redColor, strokeAlpha);
strokeWeight(thickStroke);
translate(0, 0, -h/2);
ellipse(0, 0, 2*r, 2*r);
translate(0, 0, h);
ellipse(0, 0, 2*r, 2*r);
}
if (details) {
// stroke(redColor, strokeAlpha);
// fx.line(new Vec3D(0, 0, -h/2), new Vec3D(0, 0, +h/2));
}
popMatrix();
}
void build() {
AxisAlignedCylinder cyl=new ZAxisCylinder(Vec3D.ZERO, r, h);
TriangleMesh cylinderMesh = (TriangleMesh)cyl.toMesh(cylinderRes, 0);
// option A
// this is how to find point A and point B outside the matrix -> same as locateVector(loc, rot, new Vec3D(0,0,h/2));
// Vec3D startPoint = new Vec3D (0, 0, h/2);
// startPoint.rotateZ(-rot.z);
// startPoint.rotateY(-rot.y);
// startPoint.rotateX(-rot.x);
// startPoint.addSelf(loc);
// Vec3D endPoint = new Vec3D (0, 0, -h/2);
// endPoint.rotateZ(-rot.z);
// endPoint.rotateY(-rot.y);
// endPoint.rotateX(-rot.x);
// endPoint.addSelf(loc);
// Line3D thisLine = new Line3D(startPoint, endPoint);
Vec3D thisDirection = new Vec3D(0, 0, 1);
thisDirection.rotateZ(-rot.z);
thisDirection.rotateY(-rot.y);
thisDirection.rotateX(-rot.x);
thisDirection.normalize();
cylinderMesh.pointTowards(thisDirection); // same as following line
//cylinderMesh.transform(Quaternion.getAlignmentQuat(thisDirection, Vec3D.Z_AXIS).getMatrix(), true);
cylinderMesh.translate(loc);
addMesh(cylinderMesh, material);
}
void setHeight(float h ) {
this.h = h;
}
void setRadius(float r) {
this.r = r;
}
}
class Fabric extends Part {
Vec3D a, b, c, d;
int thickness;
Fabric () {
id = millis();
}
void display() {
if (structure) {
stroke(color(strokeColor, 0, 0), strokeAlpha);
strokeWeight(1);
if (filled) {
fill(color(fillColor, 0, 0), fillAlpha);
}
else {
noFill();
}
beginShape(QUAD_STRIP);
vertex(a.x, a.y, a.z);
vertex(b.x, b.y, b.z);
vertex(d.x, d.y, d.z);
vertex(c.x, c.y, c.z);
endShape();
}
}
void build() {
addMesh(createBlockMeshFromPlane(a, b, c, d, bFabricThickness/2, true), material);
addMesh(createCylinderMeshFromLine(new Line3D(b, c), (int)bTubeR+2), material);
addMesh(createCylinderMeshFromLine(new Line3D(a, d), (int)bTubeR+2), material);
}
}
class Pipe extends Part {
// boolean closed = true;
LineStrip3D myCurve;
ParallelTransportFrame ptf;
ParallelTube tube = null;
ArrayList<Vec3D> pointList = new ArrayList<Vec3D>();
// ArrayList tangentList = new ArrayList();
int tubeR;
int cornerR;
Pipe() {
id = millis();
}
void addPoint(Vec3D loc) {
pointList.add(loc);
}
void update() {
myCurve = new LineStrip3D();
Vec3D midPoint = null;
Vec3D firstPoint = (Vec3D)pointList.get(0);
Vec3D secondPoint = (Vec3D)pointList.get(1);
Vec3D lastPoint = (Vec3D)pointList.get(pointList.size()-1);
ArrayList<Vec3D> tempPointList = new ArrayList<Vec3D>();
for (Vec3D thisPoint : pointList) {
tempPointList.add(thisPoint);
}
if (firstPoint.equals(lastPoint)) {
midPoint = new Line3D(firstPoint, secondPoint).getMidPoint();
myCurve.add(midPoint);
tempPointList.add(midPoint);
}
else {
myCurve.add((Vec3D)pointList.get(0));
}
for (int i=1;i<tempPointList.size()-1;i++) {
Vec3D previousPoint = (Vec3D)tempPointList.get(i-1);
Vec3D cornerPoint = (Vec3D)tempPointList.get(i);
Vec3D nextPoint = (Vec3D)tempPointList.get(i+1);
Vec3D vec1 = previousPoint.sub(cornerPoint).normalize();
Vec3D vec2 = nextPoint.sub(cornerPoint).normalize();
float cornerAngle = vec2.angleBetween(vec1);
Triangle3D cornerTriangle = new Triangle3D(previousPoint, cornerPoint, nextPoint);
Vec3D cornerNormal = cornerTriangle.computeNormal();
Vec3D cornerBisector = vec1.copy();
cornerBisector.rotateAroundAxis(cornerNormal, cornerAngle/2);
Vec3D centerPoint = cornerPoint.add(cornerBisector.scale(cornerR/cos(HALF_PI-cornerAngle/2))); //// equili qua !!!
// Vec3D tangentPoint2 = cornerPoint.sub(nextPoint);
// tangentPoint2.normalize();
// tangentPoint2.scaleSelf(cornerR);
// tangentPoint2.addSelf(centerPoint);
// tangentList.add(tangentPoint2);
//
// Vec3D tangentPoint1 = cornerPoint.sub(previousPoint);
// tangentPoint1.normalize();
// tangentPoint1.scaleSelf(cornerR);
// tangentPoint1.addSelf(centerPoint);
// tangentList.add(tangentPoint1);
float arcAngle = cornerAngle-PI;
float offsetAngle = arcAngle/(float)curveRes;
for (float j = 0; j<= curveRes; j ++) {
Vec3D thisPoint = cornerBisector.copy();
thisPoint.rotateAroundAxis(cornerNormal, (j*offsetAngle)-PI-arcAngle/2); // important
thisPoint.scaleSelf(cornerR);
thisPoint.addSelf(centerPoint);
myCurve.add(thisPoint);
}
}
myCurve.add((Vec3D)tempPointList.get(tempPointList.size()-1));
myCurve.add((Vec3D)tempPointList.get(tempPointList.size()-1));
}
void build() {
if (myCurve != null) {
ptf = new ParallelTransportFrame(myCurve.getVertices());
tube = new ParallelTube(ptf, tubeR, segmentRes);
tube.computeVertexNormals();
addMesh(tube, material);
}
}
void display() {
if (dots) {
stroke(redColor);
strokeWeight(markerStroke);
for (int i = 0; i < pointList.size() ; i++) {
Vec3D thisPoint = (Vec3D) pointList.get(i);
drawMarker(thisPoint);
}
// for (int i = 0; i < tangentList.size() ; i++) {
// Vec3D thisPoint = (Vec3D) tangentList.get(i);
// fx.point(thisPoint);
// }
}
if (structure) {
stroke(redColor, strokeAlpha);
strokeWeight(thickStroke);
if (myCurve != null) {
fx.lineStrip3D(myCurve.getVertices());
}
}
if (dots) {
if (myCurve != null) {
List <Vec3D> curvePointList = myCurve.getVertices();
for (int i = 0; i < curvePointList.size(); i++) {
Vec3D thisPoint = curvePointList.get(i);
stroke(blueColor);
strokeWeight(markerStroke);
drawMarker(thisPoint);
}
}
}
}
void setRadius(int thisRadius) {
tubeR = thisRadius;
}
void setCornerRadius(int thisRadius) {
cornerR = thisRadius;
}
}
class BezierTube extends Part {
LineStrip3D myCurve;
ParallelTransportFrame ptf;
ParallelTube tube = null;
ArrayList pointList;
ArrayList controlPointListA;
ArrayList controlPointListB;
ArrayList bezierPointList;
float r;
BezierTube() {
id = millis();
pointList = new ArrayList();
controlPointListA = new ArrayList();
controlPointListB = new ArrayList();
bezierPointList = new ArrayList();
}
void addPoint(Vec3D loc, Vec3D controlPointA, Vec3D controlPointB) {
pointList.add(loc);
controlPointListA.add(controlPointA.addSelf(loc));
controlPointListB.add(controlPointB.addSelf(loc));
}
void update() {
bezierPointList = new ArrayList();
for (int i = 0; i < pointList.size()-1 ; i++) {
for (int j=0;j<=curveRes;j++) { // curece detail
float t = j / float(curveRes);
Vec3D firstPoint = (Vec3D)pointList.get(i);
Vec3D secondPoint = (Vec3D)pointList.get(i+1);
Vec3D firstControlPoint = (Vec3D)controlPointListB.get(i);
Vec3D secondControlPoint = (Vec3D)controlPointListA.get(i+1);
float thisX = bezierPoint(firstPoint.x, firstControlPoint.x, secondControlPoint.x, secondPoint.x, t);
float thisY = bezierPoint(firstPoint.y, firstControlPoint.y, secondControlPoint.y, secondPoint.y, t);
float thisZ = bezierPoint(firstPoint.z, firstControlPoint.z, secondControlPoint.z, secondPoint.z, t);
bezierPointList.add(new Vec3D(thisX, thisY, thisZ));
}
bezierPointList.add((Vec3D)bezierPointList.get(bezierPointList.size()-1)); // needed by PTF constructor
myCurve = new LineStrip3D(bezierPointList); // compute bezier
}
}
void build() {
ptf = new ParallelTransportFrame(myCurve.getVertices());
tube = new ParallelTube(ptf, (int)r, segmentRes); // tube detail
tube.computeVertexNormals();
addMesh(tube, material);
}
void display() {
if (structure) {
// stroke(redColor, strokeColor);
// strokeWeight(thickStroke);
// noFill();
// for (int i = 0; i < pointList.size()-1 ; i=i+2) {
// Vec3D firstPoint = (Vec3D)pointList.get(i);
// Vec3D secondPoint = (Vec3D)pointList.get(i+1);
// Vec3D firstControlPoint = (Vec3D)controlPointListB.get(i);
// Vec3D secondControlPoint = (Vec3D)controlPointListA.get(i+1);
// bezier(firstPoint.x, firstPoint.y, firstPoint.z,
// firstControlPoint.x, firstControlPoint.y, firstControlPoint.z,
// secondControlPoint.x, secondControlPoint.y, secondControlPoint.z,
// secondPoint.x, secondPoint.y, secondPoint.z);
// }
// fater alternative follow as spline does not need to be computed again
stroke(redColor, strokeAlpha);
strokeWeight(thickStroke);
fx.lineStrip3D(myCurve.getVertices());
}
if (details) {
strokeWeight(markerStroke);
for (int i = 0; i < pointList.size() ; i++) {
stroke(blueColor);
drawMarker((Vec3D)pointList.get(i));
drawControlPoint((Vec3D)controlPointListA.get(i));
drawControlPoint((Vec3D)controlPointListB.get(i));
strokeWeight(thinStroke);
fx.line((Vec3D) pointList.get(i), (Vec3D)controlPointListA.get(i));
fx.line((Vec3D) pointList.get(i), (Vec3D)controlPointListB.get(i));
}
List <Vec3D> curvePointList = myCurve.getVertices();
for (int i = 0; i < curvePointList.size(); i++) {
stroke(redColor);
strokeWeight(markerStroke);
drawMarker((Vec3D)curvePointList.get(i));
}
}
}
void setRadius(float r) {
this.r = r;
}
}
class SplineTube extends Part {
LineStrip3D myCurve;
ParallelTransportFrame ptf;
ParallelTube tube = null;
ArrayList pointList;
int r;
SplineTube() {
id = millis();
pointList = new ArrayList();
}
void addPoint(Vec3D loc) {
pointList.add(loc);
}
void update() {
Spline3D spline = new Spline3D(pointList);
spline.setTightness(tightness); // tight
myCurve = spline.toLineStrip3D(curveRes); // res
}
void build() {
ptf = new ParallelTransportFrame(myCurve.getVertices());
tube = new ParallelTube(ptf, r, segmentRes);
tube.computeVertexNormals();
addMesh(tube, material);
}
void display() {
if (dots) {
stroke(redColor);
strokeWeight(markerStroke);
for (int i = 0; i < pointList.size() ; i++) {
Vec3D thisPoint = (Vec3D) pointList.get(i);
drawMarker(thisPoint);
}
}
if (structure) {
stroke(redColor, strokeAlpha);
strokeWeight(thickStroke);
fx.lineStrip3D(myCurve.getVertices());
}
if (details) {
List <Vec3D> curvePointList = myCurve.getVertices();
for (int i = 0; i < curvePointList.size(); i++) {
Vec3D thisPoint = curvePointList.get(i);
stroke(blueColor);
strokeWeight(markerStroke);
drawMarker(thisPoint);
}
}
}
void setRadius(int r) {
this.r = r;
}
}
class Torus extends Part {
LineStrip3D myCurve;
ParallelTransportFrame ptf;
ParallelTube tube = null;
float r;
float tubeRadius;
Torus () {
id = millis();
}
void update() {
myCurve = new LineStrip3D();
float offsetAngle = 2*PI/(float)(cylinderRes);
for (int i = 0; i < (cylinderRes)+2; i++) {
myCurve.add(loc.x+r*cos(offsetAngle*i), loc.y+r*sin(offsetAngle*i), loc.z);
}
}
void build() {
ptf = new ParallelTransportFrame(myCurve.getVertices());
tube = new ParallelTube(ptf, (int)tubeRadius, segmentRes);
tube.computeVertexNormals();
addMesh(tube, material);
}
void display() {
if (structure) {
stroke(redColor, strokeAlpha);
strokeWeight(thickStroke);
fx.lineStrip3D(myCurve.getVertices());
}
if (dots) {
stroke(redColor);
strokeWeight(markerStroke);
drawMarker(loc);
}
}
void setRadius(float r) {
this.r = r;
}
void setTubeRadius(float tubeRadius) {
this.tubeRadius = tubeRadius;
}
}
// tube following a knot curve
class Knot extends Part {
LineStrip3D myCurve;
ParallelTransportFrame ptf;
ParallelTube tube = null;
float r;
float p = 3;
float q = 2;
float curveLength;
float knotRadius;
Knot () {
id = millis();
}
void update() {
myCurve = new LineStrip3D();
curveLength = curveRes*4;
float theta = 0.1f;
float dt = ((TWO_PI) / curveLength);
for (int i=0; i<curveLength+2; i++) {
float a = cos(q * theta) + 2;
float x = a * cos(p * theta) * knotRadius;
float y = a * sin(p * theta) * knotRadius;
float z = -sin(q*theta) * knotRadius;
theta += dt;
myCurve.add(new Vec3D(x, y, z).add(loc));
}
}
void build() {
ptf = new ParallelTransportFrame(myCurve.getVertices());
tube = new ParallelTube(ptf, (int)r, segmentRes);
tube.computeVertexNormals();
addMesh(tube, material);
}
void display() {
stroke(strokeColor, strokeAlpha);
strokeWeight(thickStroke);
fx.lineStrip3D(myCurve.getVertices());
}
void setRadius(float r) {
this.r = r;
}
void setKnotRadius(float knotRadius) {
this.knotRadius = knotRadius;
}
void setP(float p) {
this.p = p;
}
void setQ(float q) {
this.q = q;
}
}
class HeMesh extends Part {
HE_Mesh myMesh;
//AABB boundingBox;
//Line3D axis;
//boolean offcenter = false;
//float sca = 1;
HeMesh () {
id = millis();
}
// void setScale (float sca) {
// this.sca = sca;
// }
void setMesh (HE_Mesh thisMesh) {
myMesh= thisMesh;
}
// void setAxis(Line3D axis) {
// this.axis = axis;
// }
void display() {
if (structure) {
stroke(strokeColor, strokeAlpha);
render.drawEdges(myMesh);
}
if (filled) {
fill(fillColor, fillAlpha);
render.drawFaces(myMesh);
}
if (dots) {
stroke(redColor, strokeAlpha);
// render.drawVertices(myMesh);
}
}
void update() {
}
void build() {
if (myMesh != null) {
addMesh(myMesh, material);
}
}
}