-
Notifications
You must be signed in to change notification settings - Fork 2
/
geom.cpp
1056 lines (988 loc) · 32.8 KB
/
geom.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 "hull.h"
#include "iso-surface.h"
#include "geom.h"
#include <cstdio>
#include <math.h>
#include <vector>
#include <string>
#include <sstream>
#include <map>
#include <set>
int g_kind(Geom* g) { return g->k; }
extern Mesh quick_hull(Mesh mesh);
Geom* g_args_fab(void) { return new ArgsGeom(); }
Geom* g_args_add(Geom* g, Geom* e) {
((ArgsGeom*)g)->val.push_back(e);
return g;
}
int g_args_len(Geom* g) { return g_args_val(g).size(); }
std::vector<Geom*> g_args_val(Geom* g) {
ensure(g->k == args_kind, "NOT ARGS");
return ((ArgsGeom*)g)->val;
}
inline float int_as_float(int xi) {
float x = *(float*)ξ
return x;
}
inline int float_as_int(float xf) {
int x = *(int*)&xf;
return x;
}
Geom* g_num_fab(int a) {
return g_num((T)int_as_float(a));
}
double g_num_val(Geom* g) {
ensure(g->k == num_kind, "NOT NUM");
return ((NumGeom*)g)->val;
}
int g_num_value(Geom* g) {
return float_as_int(g_num_val(g));
}
std::string g_string_val(Geom* g) {
ensure(g->k == string_kind, "NOT STRING");
return ((StringGeom*)g)->val;
}
Geom* g_string_fab(char* str) {
std::string s(str);
return g_string(s);
}
int g_string_len(Geom* g) {
return g_string_val(g).size();
}
char* g_string_c_str(Geom* g) {
return (char*)g_string_val(g).c_str();
}
Geom* g_v2d_fab(int x, int y) {
auto res = g_v2d(vec((T)int_as_float(x), (T)int_as_float(y)));
return res;
}
TV2 g_v2d_val(Geom* g) {
ensure(g->k == v2d_kind, "NOT V2D");
return ((V2dGeom*)g)->val;
}
int g_v2d_elt(Geom* g, int idx) { return float_as_int(g_v2d_val(g)[idx]); }
int g_v2d_x(Geom* g) { return float_as_int(g_v2d_val(g).x); }
int g_v2d_y(Geom* g) { return float_as_int(g_v2d_val(g).y); }
Geom* g_v3d_fab(int x, int y, int z) {
return g_v3d(vec((T)int_as_float(x), (T)int_as_float(y), (T)int_as_float(z)));
}
TV3 g_v3d_val(Geom* g) {
ensure(g->k == v3d_kind, "NOT V3D");
return ((V3dGeom*)g)->val;
}
int g_v3d_elt(Geom* g, int idx) { return float_as_int(g_v3d_val(g)[idx]); }
int g_v3d_x(Geom* g) { return float_as_int(g_v3d_val(g).x); }
int g_v3d_y(Geom* g) { return float_as_int(g_v3d_val(g).y); }
int g_v3d_z(Geom* g) { return float_as_int(g_v3d_val(g).z); }
Geom* g_v3i_fab(int x, int y, int z) { return g_v3i(vec(x, y, z)); }
IV3 g_v3i_val(Geom* g) {
ensure(g->k == v3i_kind || g->k == v3d_kind, "NOT V3I");
if (g->k == v3d_kind) {
auto v = g_v3d_val(g);
return vec((int)(v.x), (int)(v.y), (int)(v.z));
} else
return ((V3iGeom*)g)->val;
}
int g_v3i_elt(Geom* g, int idx) { return g_v3i_val(g)[idx]; }
int g_v3i_x(Geom* g) { return g_v3i_val(g).x; }
int g_v3i_y(Geom* g) { return g_v3i_val(g).y; }
int g_v3i_z(Geom* g) { return g_v3i_val(g).z; }
Geom* g_mat_fab(int i00, int i01, int i02, int i03, int i10, int i11, int i12, int i13,
int i20, int i21, int i22, int i23, int i30, int i31, int i32, int i33) {
Matrix<T,4> m(int_as_float(i00), int_as_float(i01), int_as_float(i02), int_as_float(i03),
int_as_float(i10), int_as_float(i11), int_as_float(i12), int_as_float(i13),
int_as_float(i20), int_as_float(i21), int_as_float(i22), int_as_float(i23),
int_as_float(i30), int_as_float(i31), int_as_float(i32), int_as_float(i33));
return g_mat(m);
}
Matrix<T,4> g_mat_val(Geom* g) {
ensure(g->k == mat_kind, "NOT MAT");
return ((MatGeom*)g)->val;
}
int g_mat_elt(Geom* g, int i, int j) {
return float_as_int(g_mat_val(g).x[i][j]);
}
Array<TV2> g_array_v2d_val(Geom* g) {
ensure(g->k == array_v2d_kind, "NOT ARRAY_V2D");
return ((ArrayV2dGeom*)g)->val;
}
Geom* g_array_v2d_fab(Geom* args) {
Array<TV2> v;
for (auto arg : g_args_val(args))
v.append(g_v2d_val(arg));
return g_array_v2d(v);
}
Geom* g_array_v2d_elt(Geom* g, int idx) { return g_v2d(g_array_v2d_val(g)[idx]); }
int g_array_v2d_len(Geom* g) { return g_array_v2d_val(g).size(); }
Array<TV3> g_array_v3d_val(Geom* g) {
ensure(g->k == array_v3d_kind, "NOT ARRAY_V3D");
return ((ArrayV3dGeom*)g)->val;
}
Geom* g_array_v3d_fab(Geom* args) {
Array<TV3> v;
for (auto arg : g_args_val(args))
v.append(g_v3d_val(arg));
return g_array_v3d(v);
}
Geom* g_array_v3d_elt(Geom* g, int idx) { return g_v3d(g_array_v3d_val(g)[idx]); }
int g_array_v3d_len(Geom* g) { return g_array_v3d_val(g).size(); }
Array<IV3> g_array_v3i_val(Geom* g) {
ensure(g->k == array_v3i_kind || g->k == array_v3d_kind, "NOT ARRAY_V3I");
if (g->k == array_v3d_kind) {
Array<IV3> array_v3i;
for (auto v : g_array_v3d_val(g))
array_v3i.append(vec((int)(v.x), (int)(v.y), (int)(v.z)));
return array_v3i;
}
return ((ArrayV3iGeom*)g)->val;
}
Geom* g_array_v3i_fab(Geom* args) {
Array<IV3> array_v3i;
for (auto arg : g_args_val(args))
array_v3i.append(g_v3i_val(arg));
return g_array_v3i(array_v3i);
}
Geom* g_array_v3i_elt(Geom* g, int idx) {
return g_v3i(g_array_v3i_val(g)[idx]);
}
int g_array_v3i_len(Geom* g) { return g_array_v3i_val(g).size(); }
bool is_nested_v2d(Geom* g) {
return g->k == nested_v2d_kind || g->k == array_v2d_kind;
}
Nested<TV2> g_nested_v2d_val(Geom* g) {
ensure(is_nested_v2d(g), "NOT NESTED_V3D");
if (g->k == array_v2d_kind)
return array_to_nested(g_array_v2d_val(g));
else
return ((NestedV2dGeom*)g)->val;
}
Geom* g_nested_v2d_fab(Geom* args) {
Nested<TV2, false> v;
for (auto arg : g_args_val(args))
v.append(g_array_v2d_val(arg));
v.freeze();
return g_nested_v2d(v);
}
Geom* g_nested_v2d_elt(Geom* g, int idx) { return g_array_v2d(g_nested_v2d_val(g)[idx]); }
int g_nested_v2d_len(Geom* g) { return g_nested_v2d_val(g).size(); }
bool is_poly(Geom* g) {
return g->k == poly_kind || g->k == array_v2d_kind;
}
Nested<TV2> g_poly_val(Geom* g) {
ensure(is_poly(g), "NOT POLY");
if (g->k == array_v2d_kind)
return array_to_nested(g_array_v2d_val(g));
else
return ((PolyGeom*)g)->val;
}
Geom* g_poly_fab(Geom* args) {
Nested<TV2, false> v;
for (auto arg : g_args_val(args))
v.append(g_array_v2d_val(arg));
v.freeze();
return g_poly(v);
}
Geom* g_poly_elt(Geom* g, int idx) { return g_array_v2d(g_poly_val(g)[idx]); }
int g_poly_len(Geom* g) { return g_poly_val(g).size(); }
bool is_nested_v3d(Geom* g) {
return g->k == nested_v3d_kind || g->k == array_v3d_kind;
}
Nested<TV3> g_nested_v3d_val(Geom* g) {
ensure(is_nested_v3d(g), "NOT NESTED_V3D");
if (g->k == array_v3d_kind)
return line_to_polyline(g_array_v3d_val(g));
else
return ((NestedV3dGeom*)g)->val;
}
Geom* g_nested_v3d_fab(Geom* args) {
Nested<TV3, false> v;
for (auto arg : g_args_val(args))
v.append(g_array_v3d_val(arg));
v.freeze();
return g_nested_v3d(v);
}
Geom* g_nested_v3d_elt(Geom* g, int idx) { return g_array_v3d(g_nested_v3d_val(g)[idx]); }
int g_nested_v3d_len(Geom* g) { return g_nested_v3d_val(g).size(); }
Mesh g_mesh_val(Geom* g) {
ensure(g->k == mesh_kind, "NOT MESH");
return ((MeshGeom*)g)->val;
}
Geom* g_mesh_fab(Geom* vertices, Geom* faces) {
return g_mesh(fab_mesh(g_array_v3i_val(faces), g_array_v3d_val(vertices)));
}
Geom* g_mesh_faces(Geom* g) {
auto faces = g_mesh_val(g).soup->elements;
Array<IV3> a;
for (auto e : faces)
a.append(vec(e.x, e.y, e.z));
return g_array_v3i(a);
}
Geom* g_mesh_points(Geom* g) {
return g_array_v3d(g_mesh_val(g).points);
}
Box<TV3> bbox(Array<TV3> points) {
return bounding_box(points);
}
Box<TV2> bbox(Array<TV2> points) {
return bounding_box(points);
}
Box<TV3> bbox(Nested<TV3> points) {
Box<TV3> box = Box<TV3>::empty_box();
for (auto line : points)
box.enlarge(bounding_box(line));
return box;
}
Box<TV2> bbox(Nested<TV2> points) {
Box<TV2> box = Box<TV2>::empty_box();
for (auto line : points)
box.enlarge(bounding_box(line));
return box;
}
Geom* g_bbox2(Box<TV2> box) {
Array<TV2> bb; bb.append(box.min); bb.append(box.max);
return g_array_v2d(bb);
}
Geom* g_bbox3(Box<TV3> box) {
Array<TV3> bb; bb.append(box.min); bb.append(box.max);
return g_array_v3d(bb);
}
Geom* g_bbox2_min(Geom* g) { return g_array_v2d_elt(g, 0); }
Geom* g_bbox2_max(Geom* g) { return g_array_v2d_elt(g, 1); }
Geom* g_bbox3_min(Geom* g) { return g_array_v3d_elt(g, 0); }
Geom* g_bbox3_max(Geom* g) { return g_array_v3d_elt(g, 1); }
Geom* g_bbox(Geom* g) {
if (g->k == mesh_kind)
return g_bbox3(bbox(g_mesh_val(g).points));
else if (g->k == poly_kind)
return g_bbox2(bbox(g_poly_val(g)));
else if (g->k == array_v2d_kind)
return g_bbox2(bbox(g_array_v2d_val(g)));
else if (g->k == array_v3d_kind)
return g_bbox3(bbox(g_array_v3d_val(g)));
else if (g->k == nested_v2d_kind)
return g_bbox2(bbox(g_nested_v2d_val(g)));
else if (g->k == nested_v3d_kind)
return g_bbox3(bbox(g_nested_v3d_val(g)));
else {
error("BBOX UNDEFINED"); return NULL;
}
}
Geom* g_dims(Geom* g) {
auto bb = g_bbox(g);
return g_sub(g_elt(bb, g_num(1.0)), g_elt(bb, g_num(0.0)));
}
Geom* g_center(Geom* g) {
auto bb = g_bbox(g);
return g_mul(g_num(0.5), g_add(g_elt(bb, g_num(0.0)), g_elt(bb, g_num(1.0))));
}
Geom* g_centering(Geom* g) {
auto c = g_center(g);
return g_mov(g_mul(g_num(-1), c), g);
}
Geom* g_load(Geom* s) {
// TODO: LOAD SVG
return new MeshGeom(read_soup(g_string_val(s)));
}
Geom* g_save(Geom* s, Geom* g) {
// TODO: LOAD SVG
if (g->k == mesh_kind) {
auto mesh = g_mesh_val(g);
write_mesh(g_string_val(s), mesh.soup, mesh.points);
} else if (g->k == poly_kind) {
save_polygon(g_string_val(s), g_poly_val(g));
} else if (g->k == nested_v2d_kind) {
save_polyline(g_string_val(s), g_nested_v2d_val(g));
}
return g;
}
std::string g_to_str_val(Geom* g) {
if (g->k == num_kind)
return num_to_str(g_num_val(g));
else if (g->k == v2d_kind)
return v2d_to_str(g_v2d_val(g));
else if (g->k == v3d_kind)
return v3d_to_str(g_v3d_val(g));
else if (g->k == v3i_kind)
return v3i_to_str(g_v3i_val(g));
else if (g->k == mat_kind)
return matrix_to_str(g_mat_val(g));
else if (g->k == array_v2d_kind)
return array_v2d_to_str(g_array_v2d_val(g));
else if (g->k == array_v3d_kind)
return array_v3d_to_str(g_array_v3d_val(g));
else if (g->k == nested_v2d_kind)
return nested_v2d_to_str(g_nested_v2d_val(g));
else if (g->k == nested_v3d_kind)
return nested_v3d_to_str(g_nested_v3d_val(g));
else if (g->k == poly_kind)
return poly_to_str(g_poly_val(g));
else if (g->k == mesh_kind)
return mesh_to_str(g_mesh_val(g));
else
return "-1";
}
Geom* g_to_str(Geom* g) {
return g_string(g_to_str_val(g));
}
Geom* g_print(Geom* g) {
printf("%s\n", g_to_str_val(g).c_str());
return g;
}
Geom* g_check(Geom* g) {
if (g->k == mesh_kind)
return g_mesh(check_mesh(g_mesh_val(g)));
else {
error("BAD CHECK"); return NULL;
}
}
Geom* g_pretty_print(Geom* g) {
if (g->k == num_kind)
pretty_print_num(g_num_val(g));
else if (g->k == v2d_kind)
pretty_print_v2d(g_v2d_val(g));
else if (g->k == v3d_kind)
pretty_print_v3d(g_v3d_val(g));
else if (g->k == v3i_kind)
pretty_print_v3i(g_v3i_val(g));
else if (g->k == poly_kind)
pretty_print_poly(g_poly_val(g));
else if (g->k == mat_kind)
pretty_print_matrix(g_mat_val(g));
else if (g->k == array_v2d_kind)
pretty_print_array_v2d(g_array_v2d_val(g));
else if (g->k == array_v3d_kind)
pretty_print_array_v3d(g_array_v3d_val(g));
else if (g->k == nested_v2d_kind)
pretty_print_nested_v2d(g_nested_v2d_val(g));
else if (g->k == nested_v3d_kind)
pretty_print_nested_v3d(g_nested_v3d_val(g));
else if (g->k == mesh_kind)
pretty_print_mesh(g_mesh_val(g));
else if (g->k == octree_kind)
pretty_print_tree(g_octree_val(g));
else if (g->k == expr_kind)
pretty_print_expr(g_expr_val(g));
return g;
}
Geom* g_num(T a) { return new NumGeom(a); }
Geom* g_string(std::string s) { return new StringGeom(s); }
Geom* g_v2d(TV2 v) { return new V2dGeom(v); }
Geom* g_v3d(TV3 v) { return new V3dGeom(v); }
Geom* g_v3i(IV3 v) { return new V3iGeom(v); }
Geom* g_mat(Matrix<T,4> mat) { return new MatGeom(mat); }
Geom* g_array_v2d(Array<TV2> line) { return new ArrayV2dGeom(line); }
Geom* g_array_v2d(RawArray<TV2> line) { return g_array_v2d(line.copy()); }
Geom* g_array_v3d(Array<TV3> line) { return new ArrayV3dGeom(line); }
Geom* g_array_v3d(RawArray<TV3> line) { return g_array_v3d(line.copy()); }
Geom* g_array_v3i(Array<IV3> line) { return new ArrayV3iGeom(line); }
Geom* g_array_v3i(RawArray<IV3> line) { return g_array_v3i(line.copy()); }
Geom* g_nested_v2d(Nested<TV2> polyline) { return new NestedV2dGeom(polyline); }
Geom* g_nested_v3d(Nested<TV3> polyline) { return new NestedV3dGeom(polyline); }
Geom* g_poly(Nested<TV2> poly) { return new PolyGeom(poly); }
Geom* g_mesh(Mesh mesh) { return new MeshGeom(mesh); }
bool all_args_kind (std::vector<Geom*> args, int kind) {
bool res = true;
for (auto arg : args)
res = res && arg->k == kind;
return res;
}
Geom* g_array_v3d (std::vector<Geom*> args) {
Array< TV3 > points;
for (auto arg : args)
points.append(g_v3d_val(arg));
return new ArrayV3dGeom(points);
}
Geom* g_array_v3i (std::vector<Geom*> args) {
Array< IV3 > points;
for (auto arg : args)
points.append(g_v3i_val(arg));
return new ArrayV3iGeom(points);
}
Geom* g_array_v2d (std::vector<Geom*> args) {
Array< TV2 > points;
for (auto arg : args) {
points.append(g_v2d_val(arg));
}
return new ArrayV2dGeom(points);
}
Geom* g_nested_v3d (std::vector<Geom*> args) {
Nested< TV3,false > lines;
for (auto arg : args)
lines.append(g_array_v3d_val(arg));
lines.freeze();
return new NestedV3dGeom(lines);
}
Geom* g_nested_v2d (std::vector<Geom*> args) {
Nested< TV2,false > lines;
for (auto arg : args)
lines.append(g_array_v2d_val(arg));
lines.freeze();
return new NestedV2dGeom(lines);
}
Geom* g_poly (std::vector<Geom*> args) {
Nested< TV2,false > lines;
if (all_args_kind(args, array_v2d_kind)) {
for (auto arg : args)
lines.append(g_array_v2d_val(arg));
} else if (all_args_kind(args, v2d_kind)) {
Array< TV2 > points;
for (auto arg : args)
points.append(g_v2d_val(arg));
lines.append(points);
} else {
error("Bad Poly Args"); return NULL;
}
lines.freeze();
return new PolyGeom(lines);
}
Geom* g_pi(void) { return new NumGeom(M_PI); }
Geom* g_none2(void) { return new PolyGeom(none_poly()); }
Geom* g_all2(void) { return new PolyGeom(all_poly()); }
Geom* g_none3(void) { return new MeshGeom(none_mesh()); }
Geom* g_all3(void) { return new MeshGeom(all_mesh()); }
Geom* g_circle(Geom* a) {
if (a->k == expr_kind)
return expr_circle(g_expr_val(a));
else
return new PolyGeom(circle_poly(g_num_val(a), 16));
}
Geom* g_square(Geom* a) {
if (a->k == expr_kind)
return expr_square(g_expr_val(a));
else
return new PolyGeom(square_poly(g_num_val(a)));
}
Geom* g_square_lo_hi(Geom* lo, Geom* hi) {
if (lo->k == expr_kind && hi->k == expr_kind)
return expr_rect2(g_expr_val(lo), g_expr_val(hi));
else
return new PolyGeom(square_poly(g_v2d_val(lo), g_v2d_val(hi)));
}
Geom* g_letter(Geom* a) {
char c = g_string_val(a)[0];
auto ol = stroke_char(c);
return new NestedV2dGeom(ol);
}
Geom* g_text(Geom* a) {
auto txt = g_string_val(a);
auto res = stroke_text(txt);
return new NestedV2dGeom(res);
}
Geom* g_elt(Geom* g, Geom* i) {
if (g->k == poly_kind)
return new ArrayV2dGeom(nested_elt(g_poly_val(g), (int)g_num_val(i)));
else if (g->k == array_v2d_kind)
return g_v2d(g_array_v2d_val(g)[(int)g_num_val(i)]);
else if (g->k == array_v3d_kind)
return g_v3d(g_array_v3d_val(g)[(int)g_num_val(i)]);
else if (g->k == v3d_kind)
return g_num(g_v3d_val(g)[(int)g_num_val(i)]);
else if (g->k == v2d_kind)
return g_num(g_v2d_val(g)[(int)g_num_val(i)]);
else {
error("Bad arg for elt"); return NULL;
}
}
Geom* g_add(Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(g_num_val(a) + g_num_val(b));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_add(g_expr_val(a), g_expr_val(b));
else if (a->k == v3d_kind && b->k == v3d_kind)
return g_v3d(g_v3d_val(a) + g_v3d_val(b));
else if (a->k == v2d_kind && b->k == v2d_kind)
return g_v2d(g_v2d_val(a) + g_v2d_val(b));
else {
error("Bad args for add"); return NULL;
}
}
Geom* g_sub(Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(g_num_val(a) - g_num_val(b));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_sub(g_expr_val(a), g_expr_val(b));
else if (a->k == v3d_kind && b->k == v3d_kind)
return g_v3d(g_v3d_val(a) - g_v3d_val(b));
else if (a->k == v2d_kind && b->k == v2d_kind)
return g_v2d(g_v2d_val(a) - g_v2d_val(b));
else {
error("Bad args for sub"); return NULL;
}
}
Geom* g_dither(Geom* g) {
if (g->k == mesh_kind)
return new MeshGeom(dither_mesh(g_mesh_val(g), 1e-16));
else {
error("Bad args for dither"); return NULL;
}
}
Geom* do_g_mul(Matrix<T,4> m, Geom* g, bool is_invert = false) {
if (g->k == mesh_kind)
return new MeshGeom(mul(m, g_mesh_val(g), is_invert));
else if (g->k == poly_kind)
return g_poly(mul_poly(m, g_poly_val(g), is_invert));
else if (g->k == array_v2d_kind)
return g_array_v2d(mul(m, g_array_v2d_val(g)));
else if (g->k == array_v3d_kind)
return g_array_v3d(mul(m, g_array_v3d_val(g)));
else if (g->k == nested_v2d_kind)
return g_nested_v2d(mul(m, g_nested_v2d_val(g)));
else if (g->k == nested_v3d_kind)
return g_nested_v3d(mul(m, g_nested_v3d_val(g)));
else {
error("Bad mul kind"); return NULL;
}
}
Geom* g_mul(Geom* a, Geom* b) {
if (a->k == mat_kind)
return do_g_mul(g_mat_val(a), b);
else if (a->k == expr_kind && b->k == expr_kind)
return expr_mul(g_expr_val(a), g_expr_val(b));
else if (a->k == num_kind && b->k == num_kind)
return g_num(g_num_val(a) * g_num_val(b));
else if (a->k == num_kind && b->k == v3d_kind)
return g_v3d(g_num_val(a) * g_v3d_val(b));
else if (a->k == num_kind && b->k == v2d_kind)
return g_v2d(g_num_val(a) * g_v2d_val(b));
else {
error("Bad args for mul"); return NULL;
}
}
Geom* g_normalize(Geom* a) {
if (a->k == v3d_kind)
return g_v3d(g_v3d_val(a).normalized());
else if (a->k == v2d_kind)
return g_v2d(g_v2d_val(a).normalized());
else {
error("Bad args for normalize"); return NULL;
}
}
Geom* g_magnitude(Geom* a) {
if (a->k == v3d_kind)
return g_num(g_v3d_val(a).magnitude());
else if (a->k == v2d_kind)
return g_num(g_v2d_val(a).magnitude());
else {
error("Bad args for magnitude"); return NULL;
}
}
Geom* g_cross(Geom* a, Geom* b) {
if (a->k == v3d_kind && b->k == v3d_kind) {
return g_v3d(cross(g_v3d_val(a), g_v3d_val(b)));
} else {
error("Bad args for cross"); return NULL;
}
}
Geom* g_dot(Geom* a, Geom* b) {
if (a->k == v3d_kind && b->k == v3d_kind)
return g_num(dot(g_v3d_val(a), g_v3d_val(b)));
else if (a->k == num_kind && b->k == v2d_kind)
return g_num(dot(g_v2d_val(a), g_v2d_val(b)));
else {
error("Bad args for dot"); return NULL;
}
}
Geom* g_div(Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(g_num_val(a) * g_num_val(b));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_div(g_expr_val(a), g_expr_val(b));
else if (a->k == v3d_kind && b->k == num_kind)
return g_v3d(g_v3d_val(a) / g_num_val(b));
else if (a->k == v2d_kind && b->k == num_kind)
return g_v2d(g_v2d_val(a) * g_num_val(b));
else {
error("Bad args for div"); return NULL;
}
}
Geom* g_mag(Geom* v, Geom* g) {
return do_g_mul(scale_matrix(g_v3d_val(v)), g);
}
Geom* g_mag1(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_mag1(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(scale_matrix(vec(g_num_val(a), g_num_val(a), g_num_val(a))), g);
}
Geom* g_xmag(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_xmag(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(scale_matrix(vec(g_num_val(a), 1.0, 1.0)), g);
}
Geom* g_ymag(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_ymag(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(scale_matrix(vec(1.0, g_num_val(a), 1.0)), g);
}
Geom* g_zmag(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_zmag(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(scale_matrix(vec(1.0, 1.0, g_num_val(a))), g);
}
Geom* g_mov(Geom* v, Geom* g) {
return do_g_mul(translation_matrix(g_v3d_val(v)), g);
}
Geom* g_xmov(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_xmov(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(translation_matrix(vec(g_num_val(a), 0.0, 0.0)), g);
}
Geom* g_ymov(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_ymov(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(translation_matrix(vec(0.0, g_num_val(a), 0.0)), g);
}
Geom* g_zmov(Geom* a, Geom* g) {
if (a->k == expr_kind && g->k == expr_kind)
return expr_zmov(g_expr_val(a), g_expr_val(g));
else
return do_g_mul(translation_matrix(vec(0.0, 0.0, g_num_val(a))), g);
}
Geom* g_rot(Geom* v, Geom* g) {
TV3 rv = g_v3d_val(v);
TV3 av = vec(degrees_to_radians(rv.x), degrees_to_radians(rv.y), degrees_to_radians(rv.z));
return do_g_mul(rotation_matrix(av), g);
}
Geom* g_rot_from_to(Geom* from, Geom* to, Geom* g) {
return do_g_mul(rotation_matrix(g_v3d_val(from), g_v3d_val(to)), g);
}
Geom* g_xrot(Geom* d, Geom* g) {
if (d->k == expr_kind && g->k == expr_kind)
return expr_xrot(g_expr_val(d), g_expr_val(g));
else {
T a = degrees_to_radians(g_num_val(d));
T c=cos(a),s=sin(a);
return do_g_mul(Matrix<T,4>(1,0,0,0,0,c,s,0,0,-s,c,0,0,0,0,1), g);
}
}
Geom* g_yrot(Geom* d, Geom* g) {
if (d->k == expr_kind && g->k == expr_kind)
return expr_yrot(g_expr_val(d), g_expr_val(g));
else {
T a = degrees_to_radians(g_num_val(d));
T c=cos(a),s=sin(a);
return do_g_mul(Matrix<T,4>(c,0,-s,0,0,1,0,0,s,0,c,0,0,0,0,1), g);
}
}
Geom* g_zrot(Geom* d, Geom* g) {
if (d->k == expr_kind && g->k == expr_kind)
return expr_zrot(g_expr_val(d), g_expr_val(g));
else {
T a = degrees_to_radians(g_num_val(d));
T c=cos(a),s=sin(a);
return do_g_mul(Matrix<T,4>(c,s,0,0,-s,c,0,0,0,0,1,0,0,0,0,1), g);
}
}
Geom* g_reflect_x(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_x(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(-1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1), g, true);
}
Geom* g_reflect_y(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_y(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,0,0,1), g, true);
}
Geom* g_reflect_xy(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_xy(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(-1,0,0,0, 0,-1,0,0, 0,0,1,0, 0,0,0,1), g, false); // TODO: NORMALS BROKEN FOR POLYS
}
Geom* g_reflect_z(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_z(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(1,0,0,0, 0,1,0,0, 0,0,-1,0, 0,0,0,1), g, true);
}
Geom* g_reflect_yz(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_yz(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(1,0,0,0, 0,-1,0,0, 0,0,-1,0,0,0,0,1), g, false);
}
Geom* g_reflect_xz(Geom* g) {
if (g->k == expr_kind)
return expr_reflect_xz(g_expr_val(g));
else
return do_g_mul(Matrix<T,4>(-1,0,0,0, 0,1,0,0, 0,0,-1,0, 0,0,0,1), g, false);
}
Geom* g_union(Geom* a, Geom* b) {
if (a->k == mesh_kind && b->k == mesh_kind)
return new MeshGeom(union_add(g_mesh_val(a), g_mesh_val(b)));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_or(g_expr_val(a), g_expr_val(b));
else if (is_poly(a) && is_poly(b))
return new PolyGeom(union_add(g_poly_val(a), g_poly_val(b)));
else {
error("Bad args for union"); return NULL;
}
}
Geom* g_intersection(Geom* a, Geom* b) {
if (a->k == mesh_kind && b->k == mesh_kind)
return new MeshGeom(intersection(g_mesh_val(a), g_mesh_val(b)));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_and(g_expr_val(a), g_expr_val(b));
else if (is_poly(a) && is_poly(b))
return new PolyGeom(intersection(g_poly_val(a), g_poly_val(b)));
else {
error("Bad args for intersection"); return NULL;
}
}
Geom* g_difference(Geom* a, Geom* b) {
if (a->k == mesh_kind && b->k == mesh_kind)
return new MeshGeom(difference(g_mesh_val(a), g_mesh_val(b)));
else if (a->k == expr_kind && b->k == expr_kind)
return expr_rem(g_expr_val(a), g_expr_val(b));
else if (is_poly(a) && is_poly(b))
return g_poly(difference(g_poly_val(a), g_poly_val(b)));
else {
error("Bad args for difference"); return NULL;
}
}
Geom* g_not(Geom* a) {
if (a->k == mesh_kind)
return new MeshGeom(invert_mesh(g_mesh_val(a)));
else if (a->k == expr_kind)
return expr_not(g_expr_val(a));
else if (is_poly(a))
return g_poly(invert_poly(g_poly_val(a)));
else {
error("Bad args for not"); return NULL;
}
}
Geom* g_offset(Geom* a, Geom* g) {
if (g->k == mesh_kind) {
// return new MeshGeom(offset_mesh(1, g_num_val(a), g_mesh_val(g)));
return new MeshGeom(offset_mesh(g_num_val(a), g_mesh_val(g)));
} else if (a->k == expr_kind && g->k == expr_kind) {
return expr_sub(g_expr_val(g), g_expr_val(a));
} else if (g->k == nested_v2d_kind) {
return g_poly(offset_polyline(16, g_num_val(a), g_nested_v2d_val(g)));
} else if (is_poly(g)) {
return g_poly(offset_poly(16, g_num_val(a), g_poly_val(g)));
} else {
error("Bad args for offset"); return NULL;
}
}
Geom* g_hollow(Geom* a, Geom* m) { return g_difference(m, g_offset(g_num(-g_num_val(a)), m)); }
Geom* g_simplify(Geom* g) { return new MeshGeom(simplify_mesh(g_mesh_val(g))); }
Geom* g_cleanup(Geom* g) { return new MeshGeom(cleanup_mesh(g_mesh_val(g))); }
extern flo_t get_radius(void), get_threshold(void);
Geom* g_slice(Geom* a, Geom* g) {
if (a->k == num_kind && g->k == octree_kind) {
return new PolyGeom(tree_slice(g_octree_val(g), g_num_val(a)));
} else if (a->k == num_kind && g->k == expr_kind) {
return new PolyGeom(tree_slice(expr_to_tree(g_expr_val(g), get_radius(), get_threshold()), g_num_val(a)));
} else
return new PolyGeom(slice(g_num_val(a), g_mesh_val(g)));
}
Geom* g_extrude(Geom* a, Geom* p) {
if (a->k == expr_kind && p->k == expr_kind)
return expr_extrude(g_expr_val(a), g_expr_val(p));
else
return new MeshGeom(extrude(g_num_val(a), g_poly_val(p)));
}
Geom* g_thicken(Geom* a, Geom* l) {
if (is_nested_v2d(l))
return new PolyGeom(thicken(1, g_num_val(a), g_nested_v2d_val(l)));
else // (is_nested_v3d(l))
return new MeshGeom(thicken(1, g_num_val(a), g_nested_v3d_val(l)));
}
Geom* g_sphere(Geom* a) {
if (a->k == expr_kind)
return expr_sphere(g_expr_val(a));
else
return new MeshGeom(sphere_mesh(1, vec(0.0, 0.0, 0.0), 0.5 * g_num_val(a)));
}
Geom* g_cube(Geom* a) {
if (a->k == expr_kind)
return expr_cube(g_expr_val(a));
else {
auto d = g_num_val(a); auto r = 0.5 * d; return new MeshGeom(cube_mesh(vec(-r, -r, -r), vec(r, r, r)));
}
}
Geom* g_cube_lo_hi(Geom* lo, Geom* hi) {
return new MeshGeom(cube_mesh(g_v3d_val(lo), g_v3d_val(hi)));
}
Geom* g_cone(Geom* a, Geom* p) {
if (a->k == expr_kind && p->k == expr_kind)
return expr_cone(g_expr_val(a), g_expr_val(p));
else
return new MeshGeom(cone_mesh(g_num_val(a), g_poly_val(p)));
}
Geom* g_revolve(Geom* p) {
if (p->k == expr_kind)
return expr_xrevolve(g_expr_val(p));
else
return new MeshGeom(revolve(16, g_poly_val(p)));
}
Geom* g_hull(Geom* g) {
if (g->k == mesh_kind)
return new MeshGeom(quick_hull_mesh(g_mesh_val(g)));
else if (g->k == poly_kind)
return new PolyGeom(quick_hull_poly(g_poly_val(g)));
else {
error("Bad HULL type"); return NULL;
}
}
// Geom* g_shear_x_z(Geom* z0, Geom* z1, Geom* dx0, Geom* dx1, Geom* m) {
// return new MeshGeom(shear_x_z(g_num_val(z0), g_num_val(z1), g_num_val(dx0), g_num_val(dx1), g_mesh(m))); }
Geom* g_expr(Geom* g) {
if (g->k == num_kind) {
return expr_lit(g_num_val(g));
} else if (g->k == poly_kind) {
auto polygon = g_poly_val(g);
return polygonize(polygon);
} else if (g->k == mesh_kind) {
auto mesh = g_mesh_val(g);
return expr_mesh(mesh);
} else {
error("INCOMPATIBLE EXPR ARG");
return NULL;
}
}
Geom* g_lit(flo_t a) { return expr_lit(a); }
Geom* g_x(void) { return expr_x(); }
Geom* g_y(void) { return expr_y(); }
Geom* g_z(void) { return expr_z(); }
Geom* g_neg(Geom* g) {
if (g->k == num_kind)
return g_num(-g_num_val(g));
else
return expr_neg(g_expr_val(g));
}
Geom* g_min (Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(min(g_num_val(a), g_num_val(b)));
else
return expr_min(g_expr_val(a), g_expr_val(b));
}
Geom* g_max (Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(max(g_num_val(a), g_num_val(b)));
else
return expr_max(g_expr_val(a), g_expr_val(b));
}
Geom* g_pow(Geom* a, Geom* b) {
if (a->k == num_kind && b->k == num_kind)
return g_num(pow(g_num_val(a), g_num_val(b)));
else
return expr_pow(g_expr_val(a), g_expr_val(b));
}
Geom* g_sqr(Geom* a) {
if (a->k == num_kind)
return g_num(g_num_val(a) * g_num_val(a));
else
return expr_sqr(g_expr_val(a));
}
Geom* g_abs(Geom* a) {
if (a->k == num_kind)
return g_num(abs(g_num_val(a)));
else
return expr_abs(g_expr_val(a));
}
Geom* g_sqrt(Geom* a) {
if (a->k == num_kind)
return g_num(sqrt(g_num_val(a)));
else
return expr_sqrt(g_expr_val(a));
}
Geom* g_sin(Geom* g) {
if (g->k == num_kind)
return g_num(sin(g_num_val(g)));
else
return expr_sin(g_expr_val(g));
}
Geom* g_cos(Geom* g) {
if (g->k == num_kind)
return g_num(cos(g_num_val(g)));
else
return expr_cos(g_expr_val(g));
}
Geom* g_tan(Geom* a) {
if (a->k == num_kind)
return g_num(tan(g_num_val(a)));
else
return expr_tan(g_expr_val(a));
}
Geom* g_asin(Geom* g) {
if (g->k == num_kind)
return g_num(asin(g_num_val(g)));
else
return expr_asin(g_expr_val(g));
}
Geom* g_acos(Geom* g) {
if (g->k == num_kind)
return g_num(acos(g_num_val(g)));
else
return expr_acos(g_expr_val(g));
}
Geom* g_atan(Geom* a) {
if (a->k == num_kind)
return g_num(atan(g_num_val(a)));
else
return expr_atan(g_expr_val(a));
}
Geom* g_xform(Geom* nx, Geom* ny, Geom* nz, Geom* g) {
return expr_xform(g_expr_val(nx), g_expr_val(ny), g_expr_val(nz), g_expr_val(g)); }
Geom* g_half(Geom* nx, Geom* ny, Geom* nz, Geom* d) {
return expr_half(g_expr_val(nx), g_expr_val(ny), g_expr_val(nz), g_expr_val(d)); }