-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcad.cpp
1598 lines (1435 loc) · 47.9 KB
/
cad.cpp
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
#include "cad.h"
#include <geode/mesh/improve_mesh.h>
// #include <geode/mesh/decimate.h>
#include <fstream>
double rndd () {
return (double)((double)rand() / (double)RAND_MAX);
}
double rndd (double mn, double mx) {
return rndd() * (mx-mn) + mn;
}
Vec rnd_vec_of (const Vec &lo, const Vec &hi) {
return vec(rndd(lo.x,hi.x), rndd(lo.y,hi.y), rndd(lo.z,hi.z));
}
Vec rnd_vec_of (const flo_t lo, const flo_t hi) {
return vec(rndd(lo,hi), rndd(lo,hi), rndd(lo,hi));
}
void error (std::string msg) {
fprintf(stderr, "MSG %s\n", msg.c_str());
exit(-1);
}
void error (std::string msg, std::string arg) {
fprintf(stderr, "MSG %s %s\n", msg.c_str(), arg.c_str());
exit(-1);
}
void ensure (bool val, std::string msg) {
if (!val) error(msg);
}
Mesh fab_mesh (Array<IV3> faces, Array<TV3> points) {
// return tuple(new_<const TriangleSoup>(faces),points);
return Mesh(new_<const TriangleSoup>(faces),points);
}
Mesh fab_mesh (Ref<const TriangleSoup> soup, Array<TV3> points) {
return Mesh(soup,points);
}
typedef real T;
typedef Vector<T,2> TV2;
typedef Vector<T,3> TV;
typedef Vector<int,3> IV3;
typedef Vector<int,2> IV2;
Matrix<T,4> to_matrix44(Matrix<T,3> M) {
return Matrix<T,4>(M.x[0][0],M.x[1][0],M.x[2][0],0,M.x[0][1],M.x[1][1],M.x[2][1],0,M.x[0][2],M.x[1][2],M.x[2][2],0,0,0,0,1);
}
Matrix<T,4> rotation_matrix(TV angles) {
auto m = Rotation<Vector<T,3> >::from_euler_angles(angles).matrix();
return to_matrix44(m);
}
Matrix<T,4> rotation_matrix(TV from, TV to) {
auto m = Rotation<Vector<T,3> >::from_rotated_vector(from, to).matrix();
return to_matrix44(m);
}
Matrix<T,4> rotation_matrix(TV2 from, TV2 to) {
auto a = atan2(to.y,to.x) - atan2(from.y,from.x);
T c=cos(a),s=sin(a);
return Matrix<T,4>(c,s,0,0,-s,c,0,0,0,0,1,0,0,0,0,1);
}
Matrix<T,4> scale_matrix(TV v) {
return Matrix<T,4>(v.x,0,0,0,0,v.y,0,0,0,0,v.z,0,0,0,0,1);
}
Matrix<T,4> translation_matrix(TV v) {
return Matrix<T,4>(1,0,0,0,0,1,0,0,0,0,1,0,v.x,v.y,v.z,1);
}
Matrix<T,4> translation_matrix(TV2 v) {
return Matrix<T,4>(1,0,0,0,0,1,0,0,0,0,1,0,v.x,v.y,0,1);
}
double is_clockwise (Array<TV2> contour) {
auto area = polygon_area(RawArray<TV2>(contour));
return area < 0;
}
// Combines multiple triangle soups into one
static Mesh concat_meshes(Mesh mesh0, Mesh mesh1) {
Array<IV3> triangles;
Array<TV3> vertices;
for(const auto& s : { mesh0, mesh1 }) {
// 0th vertex in each soup will be first vertex after all previous ones
// Save as a vector of ints to we can directly add it to each triangle
const auto index_offset = IV3::repeat(vertices.size());
vertices.extend(s.points);
for(const auto& t : s.soup->elements) {
triangles.append(t+index_offset); // Add new triangle with indices mapped to combined vertices
}
}
return fab_mesh(triangles,vertices);
}
Ref<const TriangleSoup> const_soup(Ref<TriangleSoup> val) {
return new_<const TriangleSoup>(val->elements);
}
Mesh const_mesh(Tuple<Ref<TriangleSoup>, Array<TV3>> val) {
return Mesh(const_soup(val.x), val.y);
}
void report_simplify_mesh(Mesh mesh) {
bool is_printed_headline = false;
bool is_found_problem = false;
int fi = 0;
for (auto face : mesh.soup->elements) {
Triangle<TV3> tri(mesh.points[face[0]],
mesh.points[face[1]],
mesh.points[face[2]]);
if (tri.area() == 0.0) {
is_found_problem = true;
if (!is_printed_headline) {
is_printed_headline = true;
printf("START REPORTING MESH ISSUES\n");
}
printf("ZERO AREA TRI %d: ", fi);
printf("[%f,%f,%f] [%f,%f,%f] [%f,%f,%f]: ",
tri.x0.x, tri.x0.y, tri.x0.z, tri.x1.x, tri.x1.y, tri.x1.z, tri.x2.x, tri.x2.y, tri.x2.z);
if (tri.x0 != tri.x1 && tri.x0 != tri.x2 && tri.x1 != tri.x2)
printf("CUP\n");
else
printf("SPLINTER\n");
}
fi += 1;
}
Hashtable<TV3,int> same_vertices;
for (int i = 0; i < mesh.points.size(); i++) {
auto p = mesh.points[i];
auto j = same_vertices.get_or_insert(p, i);
if (i != j) {
is_found_problem = true;
if (!is_printed_headline) {
is_printed_headline = true;
printf("START REPORTING MESH ISSUES\n");
}
printf("REDUNDANT POINT [%f,%f,%f] %d -> %d \n", p.x, p.y, p.z, i, j);
}
}
if (is_found_problem)
printf("END REPORTING MESH ISSUES\n");
}
Mesh dither_mesh(Mesh mesh, double delta) {
Array<TV3> points;
for (auto point : mesh.points)
points.append(point + vec(rndd(-delta, delta), rndd(-delta, delta), rndd(-delta, delta)));
return fab_mesh(mesh.soup, points);
}
Mesh unify_mesh_vertices (Mesh mesh) {
// printf("GCING\n");
printf("START COMPRESS VERTICES\n");
// pretty_print_mesh(mesh);
// printf("---\n");
Hashtable<TV3,int> same_vertices;
Hashtable<int,int> vertex_map;
for (int i = 0; i < mesh.points.size(); i++) {
auto p = mesh.points[i];
auto j = same_vertices.get_or_insert(p, i);
if (i != j)
printf("MAPPING [%f,%f,%f] %d -> %d [%f,%f,%f] \n", p.x, p.y, p.z, i, j, mesh.points[j].x, mesh.points[j].y, mesh.points[j].z);
vertex_map[i] = j;
}
Array<IV3> new_faces;
for (auto face : mesh.soup->elements)
new_faces.append(vec(vertex_map[face.x], vertex_map[face.y], vertex_map[face.z]));
printf("END COMPRESS VERTICES\n");
return fab_mesh(new_faces, mesh.points);
}
/*
Mesh quick_simplify_mesh(Mesh mesh) {
printf("SIMPLIFYING\n");
Array<IV3> faces;
for (auto face : mesh.soup->elements)
faces.append(face);
auto points = mesh.points.copy();
Array<int> mapping;
for (int i = 0; i < points.size(); i++)
mapping.append(i);
for (;;) {
bool is_changed = false;
for (auto face : faces) {
if (face.x != face.y && points[face.x] == points[face.y]) {
printf("MAPPING %d TO %d\n", face.x, face.y);
mapping[face.x] = face.y; is_changed = true; break;
}
if (face.y != face.z && points[face.y] == points[face.z]) {
printf("MAPPING %d TO %d\n", face.y, face.z);
mapping[face.y] = face.z; is_changed = true; break;
}
if (face.x != face.z && points[face.x] == points[face.z]) {
printf("MAPPING %d TO %d\n", face.x, face.z);
mapping[face.x] = face.z; is_changed = true; break;
}
}
if (!is_changed) break;
for (int i = 0; i < faces.size(); i++) {
auto face = faces[i];
if (face.x < 0 || face.x >= points.size())
printf("BAD FACE X %d\n", face.x);
if (face.y < 0 || face.y >= points.size())
printf("BAD FACE X %d\n", face.y);
if (face.z < 0 || face.z >= points.size())
printf("BAD FACE X %d\n", face.z);
auto new_face = vec(mapping[face.x], mapping[face.y], mapping[face.z]);
if (new_face.x != faces[i].x || new_face.y != faces[i].y || new_face.z != faces[i].z) {
printf("MAPPING [%d,%d,%d] => [%d,%d,%d]\n",
faces[i].x, faces[i].y, faces[i].z, new_face.x, new_face.y, new_face.z);
faces[i] = new_face;
}
}
}
Array<IV3> new_faces;
for (auto face : faces) {
if (face.x != face.y && face.x != face.z && face.y != face.z) {
new_faces.append(face);
} else
printf("REMOVED FACE %d,%d,%d\n", face.x, face.y, face.z);
}
printf("%d FACES NOW %d REMOVED %d FACES\n",
faces.size(), new_faces.size(), faces.size() - new_faces.size() );
return gc_mesh(fab_mesh(new_faces, points));
}
*/
int max_segment (Segment<TV3>& s0, Segment<TV3>& s1, Segment<TV3>& s2) {
// printf("S0 %f S1 %f S2 %f\n", s0.length(), s1.length(), s2.length());
auto ml = max(s0.length(), max(s1.length(), s2.length()));
if (ml == s0.length())
return 0;
else if (ml == s1.length())
return 1;
else if (ml == s2.length())
return 2;
else {
error("COULD NOT FIND MAX SEGMENT");
return -1;
}
}
VertexId common_vertex (VertexId e0v0, VertexId e0v1, VertexId e1v0, VertexId e1v1) {
// FIND VERTEX COMMON TO BOTH EDGES
// TODO: PROBABLY IMPLICIT IN REPRESENTATION
if (e0v0 == e1v0 || e0v0 == e1v1) return e0v0;
else if (e0v1 == e1v0 || e0v1 == e1v1) return e0v1;
else {
error("COULD NOT FIND COMMON VERTEX"); return (VertexId)0;
}
}
Mesh gc_mesh(Mesh mesh) {
// printf("GCING\n");
// pretty_print_mesh(mesh);
Array<bool> is_points;
for (int i = 0; i < mesh.points.size(); i++)
is_points.append(false);
Array<IV3> new_faces;
for (auto face : mesh.soup->elements)
is_points[face.x] = is_points[face.y] = is_points[face.z] = true;
int delta = 0;
Array<int> mapping;
Array<TV3> new_points;
for (int i = 0; i < mesh.points.size(); i++) {
mapping.append(i - delta);
if (is_points[i]) {
// printf("MAPPING %d TO %d\n", i, i - delta);
new_points.append(mesh.points[i]);
} else {
// printf("REMOVING %d DELTA %d\n", i, delta);
delta += 1;
}
}
for (auto face : mesh.soup->elements)
new_faces.append(vec(mapping[face.x], mapping[face.y], mapping[face.z]));
// printf("%d POINTS NOW %d REMOVED %d POINTS DELTA %d\n",
// mesh.points.size(), new_points.size(), mesh.points.size() - new_points.size(), delta);
return fab_mesh(new_faces, new_points);
}
Nested<TV3> nested2_to_nested3 (Nested<TV2> contours2) {
Nested<TV3, false> contours3;
for (auto c : contours2) {
Array<TV3> contour3;
for (auto p : c)
contour3.append(vec(p.x, p.y, 0.0));
contours3.append(contour3);
}
contours3.freeze();
return contours3;
}
Mesh topo_to_mesh (Ref<MutableTriangleTopology> topo, const Field<TV3,VertexId>& field) {
auto updates = topo->collect_garbage();
int i = 0, tot = 0;
for (auto update : updates.x) {
if (update >= 0) tot += 1;
// printf(" %d -> %d\n", i, update);
i += 1;
}
// printf("AFTER GC %d UPDATES %d - %d\n", field.size(), updates.x.size(), tot);
Array<TV3> new_points(tot);
i = 0;
for (auto update : updates.x) {
if (update >= 0) {
new_points[update] = field[VertexId(i)];
// printf("MAPPING %d -> %d\n", i, update);
}
i += 1;
}
auto new_soup = topo->face_soup().x;
// printf("SIMPLIFYING: BEFORE %d,%d AFTER %d,%d\n",
// pos.size(), mesh.x->elements.size(), new_points.size(), new_soup->elements.size());
// write_mesh("tst0.stl", mesh.soup, mesh.points);
auto new_mesh = Mesh(const_soup(new_soup), new_points);
auto gcd_mesh = gc_mesh(new_mesh);
// auto gcd_mesh = final_mesh;
report_simplify_mesh(gcd_mesh);
return gcd_mesh;
}
bool report_unsafe_collapse (Ref<MutableTriangleTopology> topo, HalfedgeId h) {
const auto o = topo->reverse(h);
const auto v0 = topo->src(h),
v1 = topo->dst(h);
// If v0 and v1 are on different boundaries, we can't do this
if (topo->is_boundary(v0) && topo->is_boundary(v1) &&
!topo->is_boundary(h) && !topo->is_boundary(o)) {
printf("If v0 and v1 are on different boundaries, we can't do this\n");
return false;
}
// Can't snip off an isolated vl or vr
if ((topo->is_boundary(topo->reverse(topo->next(h))) && topo->is_boundary(topo->reverse(topo->prev(h)))) ||
(topo->is_boundary(topo->reverse(topo->next(o))) && topo->is_boundary(topo->reverse(topo->prev(o))))) {
printf("Can't snip off an isolated vl or vr\n");
return false;
}
// Look up left and right vertices
const auto vl = topo->is_boundary(h) ? VertexId() : topo->dst(topo->next(h)),
vr = topo->is_boundary(o) ? VertexId() : topo->dst(topo->next(o));
// This only happens in temporarily invalid situations, such as if
// split_along_edge is called and not cleaned up. No good can come of it.
if (vl==vr) {
printf("INVALID EDGE\n");
return false;
}
// One-rings of v0 and v1 cannot intersect, otherwise we'll collapse a
// triangle-shaped tunnel
Hashtable<VertexId> covered;
for (auto oh: topo->outgoing(v0)) {
auto v = topo->dst(oh);
if (v != vl && v != vr)
covered.set(v);
}
for (auto oh: topo->outgoing(v1)) {
auto v = topo->dst(oh);
if (covered.contains(v)) {
printf("ONE-RINGS of V0 and V1 INTERSECT\n");
return false;
}
}
return true;
}
Mesh quick_cleanup_mesh(Mesh mesh) {
printf("--- START CLEANUP\n");
// pretty_print_mesh(mesh);
// write_mesh("tst1.stl", mesh.soup, mesh.points);
// printf("---\n");
// report_simplify_mesh(mesh);
// Array<TV3> pos(mesh.points.copy());
auto topo = new_<MutableTriangleTopology>(mesh.soup);
printf("ADDING FIELD\n");
FieldId<TV3,VertexId> pos_id = topo->add_field(Field<TV,VertexId>(mesh.points.copy()), vertex_position_id);
auto &field = topo->field(pos_id);
// topo->dump_internals();
// pretty_print_mesh(topo_to_mesh(topo, field));
// std::vector<FaceId> split_faces;
bool is_changed = false;
printf("CLEANUP ZERO GEOMETRY\n");
do {
do {
is_changed = false;
for (auto face : topo->faces()) {
auto tri = topo->triangle(field, face);
if (tri.area() == 0.0 && tri.x0 != tri.x1 && tri.x0 != tri.x2 && tri.x1 != tri.x2) {
auto e0 = topo->halfedge(face, 0);
auto e1 = topo->halfedge(face, 1);
auto e2 = topo->halfedge(face, 2);
RawField<TV3,VertexId> rawfield(field);
auto s0 = topo->segment(rawfield, e0);
auto s1 = topo->segment(rawfield, e1);
auto s2 = topo->segment(rawfield, e2);
// auto tri = topo->triangle(field, face);
// printf("ZERO AREA TRI %d [%f,%f,%f] [%f,%f,%f] [%f,%f,%f]\n",
// (int)face, tri.x0.x, tri.x0.y, tri.x0.z, tri.x1.x, tri.x1.y, tri.x1.z, tri.x2.x, tri.x2.y, tri.x2.z);
// printf(" E0 %d S0 [%f,%f,%f] [%f,%f,%f]\n", (int)e0, s0.x0.x, s0.x0.y, s0.x0.z, s0.x1.x, s0.x1.y, s0.x1.z);
// printf(" E1 %d S1 [%f,%f,%f] [%f,%f,%f]\n", (int)e1, s1.x0.x, s1.x0.y, s1.x0.z, s1.x1.x, s1.x1.y, s1.x1.z);
// printf(" E2 %d S2 [%f,%f,%f] [%f,%f,%f]\n", (int)e2, s2.x0.x, s2.x0.y, s2.x0.z, s2.x1.x, s2.x1.y, s2.x1.z);
int msi = max_segment(s0, s1, s2);
int o1i = (msi + 1)%3;
int o2i = (msi + 2)%3;
auto oe1 = topo->halfedge(face,o1i);
auto oe2 = topo->halfedge(face,o2i);
auto cvi = common_vertex(topo->src(oe1), topo->dst(oe1), topo->src(oe2), topo->dst(oe2));
// printf("BEFORE n-VERTS %d ALLOC VERTS %d\n", topo->n_vertices(), topo->allocated_vertices());
auto nvi = topo->split_edge(topo->halfedge(face,msi));
auto ov = field[cvi];
field[nvi] = ov;
auto nv = field[nvi];
// field.append(ov);
printf("SPLITTING EDGE %d (%d,%d,%d) ON FACE %d VERTEX %d -> %d [%f,%f,%f]->[%f,%f,%f] (%d) ALLOC %d\n",
(int)topo->halfedge(face,msi), int(e0), int(e1), int(e2),
(int)face, (int)cvi, (int)nvi,
ov.x, ov.y, ov.z, nv.x, nv.y, nv.z, (int)field.size(), topo->allocated_vertices());
topo->collect_garbage();
// pretty_print_mesh(topo_to_mesh(topo, field));
// topo->dump_internals();
// printf("AFTER n-VERTS %d ALLOC VERTS %d\n", topo->n_vertices(), topo->allocated_vertices());
break;
}
}
} while (is_changed == true);
// printf("COLLAPSE ZERO LENGTH EDGES\n");
// do {
is_changed = false;
for (auto e : topo->halfedges()) {
auto src = topo->src(e);
auto dst = topo->dst(e);
if (field[src] == field[dst]) {
if (topo->is_collapse_safe(e)) {
// printf("BEFORE n-VERTS %d ALLOC VERTS %d\n", topo->n_vertices(), topo->allocated_vertices());
printf("COLLAPSING E%d V%d->V%d\n", (int)e, (int)src, (int)dst);
topo->collapse(e);
topo->collect_garbage();
// pretty_print_mesh(topo_to_mesh(topo, field));
// topo->dump_internals();
// printf("AFTER n-VERTS %d ALLOC VERTS %d\n", topo->n_vertices(), topo->allocated_vertices());
is_changed = true;
break;
} else {
printf("UNABLE TO COLLAPSE %d %d->%d: ", (int)e, (int)src, (int)dst);
report_unsafe_collapse(topo, e);
}
}
}
} while (is_changed == true);
// printf("ERASING BOUNDARY EDGES\n");
// do {
// is_changed = false;
// for (auto e : topo->boundary_edges()) {
// printf("ERASING BOUNDARY EDGE %d\n", (int)e);
// topo->erase(e, true);
// is_changed = true;
// break;
// }
// } while (is_changed == true);
printf("COLLECTING GARBAGE\n");
// topo->dump_internals();
auto new_mesh = topo_to_mesh(topo, field);
// printf("DECIMATING MESH\n");
// RawField<TV3,VertexId> rawfield(field);
// do_decimate_inplace(topo, rawfield, 0.01, 0.01, 0, 0);
// auto dec_mesh = decimate_mesh(new_mesh);
// write_mesh("tst1.stl", new_mesh.soup, new_mesh.points);
// auto dec_mesh = simplify_mesh(new_mesh);
auto dec_mesh = new_mesh;
auto final_mesh = dec_mesh;
/*
auto gc_new_mesh = gc_mesh(dec_mesh);
write_mesh("tst2.stl", gc_new_mesh.soup, gc_new_mesh.points);
auto unified_mesh = unify_mesh_vertices(new_mesh);
write_mesh("tst3.stl", unified_mesh.soup, unified_mesh.points);
pretty_print_mesh(unified_mesh);
auto final_mesh = unified_mesh;
*/
report_simplify_mesh(final_mesh);
printf("--- GC CLEANUP\n");
auto gcd_mesh = gc_mesh(final_mesh);
report_simplify_mesh(gcd_mesh);
printf("--- END CLEANUP\n");
return gcd_mesh;
// return unified_mesh;
// auto new_soup = topo->face_soup().x;
// return gc_mesh(Mesh(const_soup(new_soup), pos));
}
Mesh simplify_mesh(Mesh mesh) {
// printf("STARTING SIMPLIFICATION\n");
// report_simplify_mesh(mesh);
Array<TV3> pos(mesh.points);
Field<TV3,VertexId> field(pos.copy());
auto topo = new_<MutableTriangleTopology>();
topo->add_vertices(mesh.points.size());
for (auto face : mesh.soup->elements)
topo->add_face(vec((VertexId)face.x, (VertexId)face.y, (VertexId)face.z));
// ImproveOptions options(1.1,0.1,0.1);
// ImproveOptions options(1.1,0.01,0.01);
// ImproveOptions options(1.0000001,0.0000001,0.0000001);
ImproveOptions options(1 + 1e-6,1e-6,1e-6);
improve_mesh_inplace(topo, field, options);
auto final_mesh = topo_to_mesh(topo, field);
// printf("BEFORE GC %d\n", field.size());
return final_mesh;
}
Mesh cleanup_mesh (Mesh mesh) {
if (true) {
return simplify_mesh(mesh);
} else {
return quick_cleanup_mesh(mesh);
}
}
// invert triangle soup so normals point inwards
Mesh invert_mesh(Mesh mesh) {
Array<IV3> triangles;
for(const auto& t : mesh.soup->elements) {
triangles.append(vec(t[0], t[2], t[1]));
}
return fab_mesh(triangles, mesh.points);
}
Array<TV2> invert_contour(Array<TV2> contour) {
Array<TV2> res;
for (int i = contour.size()-1; i >= 0; i--)
res.append(contour[i]);
return res;
}
Nested<TV2> invert_poly(Nested<TV2> poly) {
Nested<TV2,false> pres;
for (auto contour : poly) {
Array<TV2> cres;
for (int i = contour.size()-1; i >= 0; i--)
cres.append(contour[i]);
pres.append(cres);
}
pres.freeze();
return pres;
}
Nested<TV2> mul_poly(Matrix<T,4> m, Nested<TV2> poly, bool is_invert) {
auto res = mul(m, poly);
return is_invert ? invert_poly(res) : res;
}
Array<TV2> mul_contour(Matrix<T,4> m, Array<TV2> contour, bool is_invert) {
auto res = mul(m, contour);
return is_invert ? invert_contour(res) : res;
}
Array<TV2> cleanup_contour(RawArray<TV2> contour) {
// TODO: LIMITS
return polygon_simplify(contour, 179.9, 0.0001);
}
Nested<TV2> cleanup_poly(Nested<TV2> poly) {
Nested<TV2,false> res;
for (auto contour : poly)
res.append(cleanup_contour(contour));
res.freeze();
return res;
}
Nested<TV2> maybe_cleanup_poly(Nested<TV2> poly) {
if (true)
return cleanup_poly(poly);
else
return poly;
}
Nested<TV2> union_add(Nested<TV2> c0, Nested<TV2> c1) {
auto res = polygon_union(c0, c1);
// printf("UNION POLY\n");
return maybe_cleanup_poly(res);
}
Nested<TV2> union_all(Nested<TV2> c0) {
auto res = polygon_union(c0);
// printf("UNION POLY\n");
return maybe_cleanup_poly(res);
}
Nested<TV2> intersection(Nested<TV2> c0, Nested<TV2> c1) {
return maybe_cleanup_poly(polygon_intersection(c0, c1));
}
Nested<TV2> difference(Nested<TV2> c0, Nested<TV2> c1) {
return maybe_cleanup_poly(polygon_union(c0, invert_poly(c1)));
}
void save_svg_header(std::fstream &fs) {
fs << "<?xml version=\"1.0\" standalone=\"no\"?>\n";
fs << "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\" \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\n";
fs << "<svg width=\"10cm\" height=\"10cm\" viewBox=\"0 0 100000 100000\" xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\">\n";
}
void save_poly_star(std::string filename, std::string kind, Nested<TV2> poly) {
std::fstream fs;
fs.open (filename, std::fstream::out);
save_svg_header(fs);
for (auto c : poly) {
fs << "<" << kind << " fill=\"none\" stroke=\"black\" stroke-width=\"1\" points=\"";
for (auto p : c) {
fs << " " << ((1000.0 * p.x) + 50000) << "," << ((-1000.0 * p.y) + 50000);
}
fs << "\" />\n";
}
fs << "</svg>\n";
fs.close();
}
void save_polygon(std::string filename, Nested<TV2> polygon) {
save_poly_star(filename, "polygon", polygon);
}
void save_polyline(std::string filename, Nested<TV2> polyline) {
save_poly_star(filename, "polyline", polyline);
}
Mesh maybe_cleanup_mesh (Mesh mesh, bool is_cleanup) {
// report_cleanup_mesh(mesh);
if (is_cleanup)
return cleanup_mesh(mesh);
else
return mesh;
}
Mesh split_mesh (Mesh mesh, int depth, bool is_cleanup) {
auto split = split_soup(mesh.soup, mesh.points, depth);
return maybe_cleanup_mesh(Mesh(split.x, split.y), is_cleanup);
}
Nested<TV2> offset(T a, Nested<TV2> c) {
// auto merge = concat_polygons(c0, invert_polygon(c1));
// return split_polygons(merge.x, merge.y, 0);
return c;
}
Mesh union_add(Mesh mesh0, Mesh mesh1, bool is_cleanup) {
auto merge = concat_meshes(mesh0, mesh1);
// printf("--- START UNIONING ---\n");
auto res = split_mesh(merge, 0, is_cleanup);
// printf("--- DONE UNIONING ---\n");
return res;
}
void pretty_print_num(T n) {
printf("%g\n", n);
}
void pretty_print_v2d(TV2 pt) {
printf("%g,%g\n", pt.x, pt.y);
}
void pretty_print_v3d(TV3 pt) {
printf("%g,%g,%g\n", pt.x, pt.y, pt.z);
}
void pretty_print_v3i(IV3 pt) {
printf("%d,%d,%d\n", pt.x, pt.y, pt.z);
}
void pretty_print_mesh(Mesh mesh) {
int i = 0;
printf("AREA %f VOLUME %f\n", mesh.soup->surface_area(RawArray<const TV3>(mesh.points)), mesh.soup->volume(RawArray<const TV3>(mesh.points)));
for (auto pt : mesh.points) {
printf("PT[%2d] %f,%f,%f\n", i, pt.x, pt.y, pt.z);
i += 1;
}
i = 0;
for (auto tri : mesh.soup->elements) {
// T a = Triangle::area(mesh.points[tri.x], mesh.points[tri.y], mesh.points[tri.z]);
auto p0 = mesh.points[tri.x];
auto p1 = mesh.points[tri.y];
auto p2 = mesh.points[tri.z];
T a = 0.5 * cross(p1 - p0, p2 - p0).magnitude();
printf("TRI[%d] %2d,%2d,%2d [%f,%f,%f] [%f,%f,%f] [%f,%f,%f] AREA %f\n",
i, tri.x, tri.y, tri.z, p0.x, p0.y, p0.z, p1.x, p1.y, p1.z, p2.x, p2.y, p2.z, a);
i += 1;
}
}
void pretty_print_matrix(Matrix<T,4> M) {
printf("%g, %g, %g, %g\n", M.x[0][0],M.x[1][0],M.x[2][0],M.x[3][0]);
printf("%g, %g, %g, %g\n", M.x[0][1],M.x[1][1],M.x[2][1],M.x[3][1]);
printf("%g, %g, %g, %g\n", M.x[0][2],M.x[1][2],M.x[2][2],M.x[3][2]);
printf("%g, %g, %g, %g\n", M.x[0][3],M.x[1][3],M.x[2][3],M.x[3][3]);
}
void pretty_print_array_v2d(Array<TV2> line) {
int i = 0;
for (auto pt : line) {
printf("PT[%2d] %g,%g\n", i, pt.x, pt.y);
i += 1;
}
}
void pretty_print_array_v3d(Array<TV3> line) {
int i = 0;
for (auto pt : line) {
printf("PT[%2d] %g,%g,%g\n", i, pt.x, pt.y, pt.z);
i += 1;
}
}
void pretty_print_array_v3i(Array<IV3> line) {
int i = 0;
for (auto pt : line) {
printf("PT[%2d] %d,%d,%d\n", i, pt.x, pt.y, pt.z);
i += 1;
}
}
void pretty_print_poly(Nested<TV2> poly) {
int i = 0;
for (auto elt : poly) {
Array<TV2> contour; for (auto e : elt) contour.append(e);
printf("CONTOUR %d\n", i);
pretty_print_array_v2d(contour);
i += 1;
}
}
void pretty_print_nested_v3d(Nested<TV3> polyline) {
int i = 0;
for (auto elt : polyline) {
Array<TV3> line; for (auto e : elt) line.append(e);
printf("LINE %d\n", i);
pretty_print_array_v3d(line);
i += 1;
}
}
void pretty_print_nested_v2d(Nested<TV2> polyline) {
int i = 0;
for (auto elt : polyline) {
Array<TV2> line; for (auto e : elt) line.append(e);
printf("LINE %d\n", i);
pretty_print_array_v2d(line);
i += 1;
}
}
std::string num_to_str (T num) {
std::stringstream ss;
ss << num;
return ss.str();
}
std::string v2d_to_str (TV2 pt) {
std::stringstream ss;
ss << "[" << pt.x << "," << pt.y << "]";
return ss.str();
}
std::string v3d_to_str (TV3 pt) {
std::stringstream ss;
ss << "[" << pt.x << "," << pt.y << "," << pt.z << "]";
return ss.str();
}
std::string v3i_to_str (IV3 pt) {
std::stringstream ss;
ss << "[" << pt.x << "," << pt.y << "," << pt.z << "]";
return ss.str();
}
std::string array_v2d_to_str(Array<TV2> line) {
std::stringstream ss;
int i = 0;
ss << "[";
for (auto pt : line) {
if (i > 0) ss << ", ";
ss << v2d_to_str(pt);
i += 1;
}
ss << "]";
return ss.str();
}
std::string array_v3d_to_str(Array<TV3> line) {
std::stringstream ss;
int i = 0;
ss << "[";
for (auto pt : line) {
if (i > 0) ss << ", ";
ss << v3d_to_str(pt);
i += 1;
}
ss << "]";
return ss.str();
}
std::string array_v3i_to_str(Array<const IV3> line) {
std::stringstream ss;
int i = 0;
ss << "[";
for (auto pt : line) {
if (i > 0) ss << ", ";
ss << v3i_to_str(pt);
i += 1;
}
ss << "]";
return ss.str();
}
std::string mesh_to_str(Mesh mesh) {
std::stringstream ss;
ss << "mesh(";
ss << array_v3d_to_str(mesh.points);
ss << ", ";
ss << array_v3i_to_str(mesh.soup->elements);
ss << ")";
return ss.str();
}
std::string poly_to_str(Nested<TV2> poly) {
std::stringstream ss;
int i = 0;
ss << "poly(";
for (auto elt : poly) {
Array<TV2> contour; for (auto e : elt) contour.append(e);
Array<TV2> line; for (auto e : elt) line.append(e);
if (i > 0) ss << ", ";
ss << array_v2d_to_str(line);
i += 1;
}
ss << ")";
return ss.str();
}
std::string nested_v3d_to_str(Nested<TV3> polyline) {
std::stringstream ss;
int i = 0;
ss << "[";
for (auto elt : polyline) {
Array<TV3> line; for (auto e : elt) line.append(e);
if (i > 0) ss << ", ";
ss << array_v3d_to_str(line);
i += 1;
}
ss << "]";
return ss.str();
}
std::string nested_v2d_to_str(Nested<TV2> polyline) {
std::stringstream ss;
int i = 0;
ss << "[";
for (auto elt : polyline) {
Array<TV2> line; for (auto e : elt) line.append(e);
if (i > 0) ss << ", ";
ss << array_v2d_to_str(line);
i += 1;
}
ss << "]";
return ss.str();
}
std::string matrix_to_str(Matrix<T,4> M) {
std::stringstream ss;
ss << "mat(";
ss << M.x[0][0] << "," << M.x[1][0] << "," << M.x[2][0] << "," << M.x[3][0] << ",";
ss << M.x[0][1] << "," << M.x[1][1] << "," << M.x[2][1] << "," << M.x[3][1] << ",";
ss << M.x[0][2] << "," << M.x[1][2] << "," << M.x[2][2] << "," << M.x[3][2] << ",";
ss << M.x[0][3] << "," << M.x[1][3] << "," << M.x[2][3] << "," << M.x[3][3] << ")";
ss << ")";
return ss.str();
}
Mesh intersection(Mesh mesh0, Mesh mesh1, bool is_cleanup) {
auto merge = concat_meshes(mesh0, mesh1);
return split_mesh(merge, 1, is_cleanup);
}
Mesh mesh_from(int start, Mesh mesh) {
Array<TV3> pts;
for (int i = start; i < mesh.points.size(); i++) {
pts.append(mesh.points[i]);
}
/*
for (int i = 0; i < pts.size(); i++) {
for (int j = i + 1; j < pts.size(); j++) {
if (pts[i] == pts[j]) {
printf("%d (%f,%f,%f) === %d (%f,%f,%f)\n", i, pts[i].x, pts[i].y, pts[i].z, j, pts[j].x, pts[j].y, pts[j].z);
}
}
}
*/
Array<IV3> faces;
for (auto tri : mesh.soup->elements) {
bool is_all = tri.x >= start && tri.y >= start && tri.z >= start;
if (is_all)
faces.append(vec(tri.x - start, tri.y - start, tri.z - start));
}
return fab_mesh(faces, pts);
}
Nested<TV2> slice(T z, Mesh mesh) {
T t = 1e8; // TODO: USE INF
auto smesh = const_mesh(cube_mesh(vec(-t, -t, -t), vec( t, t, z)));
// printf("-- SLICE %f MESH --\n", z);
// pretty_print_mesh(smesh);
// printf("-- ABOUT TO SLICE %f --\n", z);
auto res = intersection(smesh, mesh, false);
// printf("-- SLICE %f INTERSECTION MESH --\n", z);
// pretty_print_mesh(res);
int start = smesh.points.size() + mesh.points.size();
// printf("---->>>>\n");
// print_mesh(res);
// printf("========\n");
// printf("%d OLD %d NEW VERTICES\n", start, res.points.size() - start);
// for (auto tri : res.soup->elements) {
// bool is_all = tri.x >= start && tri.y >= start && tri.z >= start;
// if (is_all)
// printf("SLICE %2d,%2d,%2d\n", tri.x, tri.y, tri.z);
// }
auto new_mesh = mesh_from(start, res);
auto boundary = new_mesh.soup->boundary_mesh();
auto polys = boundary->polygons();
// int i = 0;
// for (auto p : new_mesh.points) {
// printf("BP[%2d] %f,%f,%f\n", i, p.x, p.y, p.z);
// i += 1;
// }
// for (auto elt : boundary->elements) {
// printf("ELT %d,%d\n", elt.x, elt.y);
// }
// printf("POLYGONS\n");
// for (auto poly : polys.x) {
// for (auto p : poly)
// printf("%d ", p);
// printf("\n");
// }
Nested<TV2,false> pres;
for (auto poly : polys.x) {
Array<TV2> contour;
for (auto p : poly) {
auto pt = new_mesh.points[p];
contour.append(vec(pt.x, pt.y));
}
pres.append(contour);
}
pres.freeze();
return cleanup_poly(pres);
}
Mesh difference(Mesh mesh0, Mesh mesh1, bool is_cleanup) {
auto merge = concat_meshes(mesh0, invert_mesh(mesh1));
return split_mesh(merge, 0, is_cleanup);
}
template<class ET>
Nested<ET> array_to_nested(Array<ET> contour) {
Nested<ET,false> poly;
poly.append(contour);
poly.freeze();
return poly;
}
template<class ET>
Array<ET> nested_elt(Nested<ET> poly, int i) {
Array<ET> contour;
// printf("POLY TO CONTOUR[%d] %d\n", i, poly[i].size());
for (auto e : poly[i])
contour.append(e);
return contour;
}
Mesh all_mesh(void) {
T x = 1e8;
return const_mesh(cube_mesh(vec(-x, -x, -x), vec(x, x, x)));
}
Mesh none_mesh(void) {
Array<IV3> faces;
Array<TV3> points;
return fab_mesh(faces, points);
}
Nested<TV2> square_poly(TV2 min, TV2 max) {
Array<Vector<real, 2>> pts;