-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathisosurface.scad
More file actions
3827 lines (3649 loc) · 217 KB
/
isosurface.scad
File metadata and controls
3827 lines (3649 loc) · 217 KB
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
/////////////////////////////////////////////////////////////////////
// LibFile: isosurface.scad
// [Metaballs](https://en.wikipedia.org/wiki/Metaballs) (also known as "blobby objects"),
// are bounded and closed organic surfaces that smoothly blend together.
// Metaballs are a specific kind of [isosurface](https://en.wikipedia.org/wiki/Isosurface).
// .
// An isosurface, or implicit surface, is a three-dimensional surface representing all points of a
// constant value (e.g. pressure, temperature, electric potential, density) in a
// 3D volume. It's the 3D version of a 2D contour; in fact, any 2D cross-section of an
// isosurface **is** a 2D contour.
// .
// For computer-aided design, isosurfaces of abstract functions can generate complex curved surfaces
// and organic shapes. For example, spherical metaballs can be formulated using a set of point
// centers that define the metaball locations. For each metaball, a function is defined to compute
// the contribution of the metaball to any point in a 3D volume. The
// combined contributions from all the metaballs results in a function that varies in a complicated
// way throughout the volume. When two metaballs are far apart, they appear simply as spheres, but when
// they are close together they enlarge, reach toward each other, and meld together in a smooth
// fashion. The resulting metaball model appears as smoothly blended blobby shapes. The
// implementation below provides metaballs of a variety of types including spheres, cuboids, and
// cylinders (cones), with optional parameters to adjust the influence of one metaball on others,
// and the cutoff distance where the metaball's influence stops.
// .
// In general, an isosurface can be defined using any function of three variables $x, y, z$.
// The isosurface of a function $f(x,y,z)$ is the set of points where $f(x,y,z)=c$ for some constant
// value $c$. Such a function is also known as an "implicit surface" because the function *implies* a
// surface of constant value within a volume of space. The constant $c$ is referred to as the "isovalue".
// Changing the isovalue changes the position of the isosurface, depending on how the function is
// defined. Because metaballs are isosurfaces, they also have an isovalue. The isovalue is also known
// as the "threshold".
// .
// Some isosurface functions are unbounded, extending infinitely in all directions. A familiar example may
// be a [gryoid](https://en.wikipedia.org/wiki/Gyroid), which is often used as a volume infill pattern in
// [fused filament fabrication](https://en.wikipedia.org/wiki/Fused_filament_fabrication). The gyroid
// isosurface is unbounded and periodic in all three dimensions.
// .
// This file provides modules and functions to create a [VNF](vnf.scad) using metaballs, or from
// general isosurfaces. This file also provides modules and functions to create 2d metaballs and
// contours, where the output is a list of [paths](paths.scad), which can be open or closed paths.
// .
// For isosurfaces and 3D metaballs, the point list in the generated VNF structure contains many duplicated
// points. This is normally not a problem for rendering the shape, but machine roundoff differences may
// result in Manifold issuing warnings when doing the final render, causing rendering to abort if you have
// enabled the "stop on first warning" setting. You can prevent this by passing the VNF through {{vnf_quantize()}}
// using a quantization of 1e-7, or you can pass the VNF structure into {{vnf_merge_points()}}, which also
// removes the duplicates. Additionally, flat surfaces (often resulting from clipping by the bounding
// box) are triangulated at the voxel size resolution, and these can be unified into a single face by
// passing the vnf structure to {{vnf_unify_faces()}}. These steps can be computationally expensive
// and are not normally necessary.
// Includes:
// include <BOSL2/std.scad>
// include <BOSL2/isosurface.scad>
// FileGroup: Advanced Modeling
// FileSummary: Isosurfaces and metaballs.
//////////////////////////////////////////////////////////////////////
_BOSL2_ISOSURFACE = is_undef(_BOSL2_STD) && (is_undef(BOSL2_NO_STD_WARNING) || !BOSL2_NO_STD_WARNING) ?
echo("Warning: isosurface.scad included without std.scad; dependencies may be missing\nSet BOSL2_NO_STD_WARNING = true to mute this warning.") true : true;
//////////////////// 3D initializations and support functions ////////////////////
/*
Lookup Tables for Transvoxel's Modified Marching Cubes
Adapted for OpenSCAD from https://gist.github.com/dwilliamson/72c60fcd287a94867b4334b42a7888ad
Unlike the original paper (Marching Cubes: A High Resolution 3D Surface Construction Algorithm), these tables guarantee a closed mesh in which connected components are continuous and free of holes.
Rotations are prioritized over inversions so that 3 of the 6 cases containing ambiguous faces are never added. 3 extra cases are added as a post-process, overriding inversions through custom-built rotations to eliminate the remaining ambiguities.
The cube index determines the sequence of edges to split. The index ranges from 0 to 255, representing all possible combinations of the 8 corners of the cube being greater or less than the isosurface threshold.
For example, a cube with corners 2, 3, and 7 greater than the threshold isovalue would have the index 10000110, an 8-bit binary number with bits 2, 3, and 7 set to 1, corresponding to decimal index 134. After determining the cube's index value this way, the triangulation order is looked up in a table.
Axes are
z
(top)
| y (back)
| /
|/
+----- x (right)
Vertex and edge layout (heavier = and # indicate closer to viewer):
3 +----------+ 7 +----10----+
/: /| /: /|
/ : / | 1 2 5 6
1 +==========+5 | +=====9====+ |
# 2+ - - - # -+ 6 # +- - 11-# -+
# / # / 0 3 4 7
#/ #/ #/ #/
0 +==========+ 4 +=====8=====+
z changes fastest, then y, then x.
*/
/// Pair of vertex indices for each edge on the voxel
_MCEdgeVertexIndices = [
[0, 1],
[1, 3],
[3, 2],
[2, 0],
[4, 5],
[5, 7],
[7, 6],
[6, 4],
[0, 4],
[1, 5],
[3, 7],
[2, 6]
];
/// For each of the 256 configurations of a marching cube, define a list of triangles, specified as triples of edge indices.
_MCTriangleTable = [
[],
[3,8,0],
[1,0,9],
[9,1,8,8,1,3],
[3,2,11],
[2,11,0,0,11,8],
[1,0,9,3,2,11],
[11,1,2,11,9,1,11,8,9],
[10,2,1],
[2,1,10,0,3,8],
[0,9,2,2,9,10],
[8,2,3,8,10,2,8,9,10],
[1,10,3,3,10,11],
[10,0,1,10,8,0,10,11,8],
[9,3,0,9,11,3,9,10,11],
[9,10,8,8,10,11],
[7,4,8],
[0,3,4,4,3,7],
[0,9,1,4,8,7],
[1,4,9,1,7,4,1,3,7],
[11,3,2,8,7,4],
[4,11,7,4,2,11,4,0,2],
[3,2,11,0,9,1,4,8,7],
[9,1,4,4,1,7,7,1,2,7,2,11],
[7,4,8,1,10,2],
[7,4,3,3,4,0,10,2,1],
[10,2,9,9,2,0,7,4,8],
[7,4,9,7,9,2,9,10,2,3,7,2],
[1,10,3,3,10,11,4,8,7],
[4,0,7,0,1,10,7,0,10,7,10,11],
[7,4,8,9,3,0,9,11,3,9,10,11],
[7,4,11,4,9,11,9,10,11],
[5,9,4],
[8,0,3,9,4,5],
[1,0,5,5,0,4],
[5,8,4,5,3,8,5,1,3],
[3,2,11,5,9,4],
[2,11,0,0,11,8,5,9,4],
[4,5,0,0,5,1,11,3,2],
[11,8,2,8,4,5,2,8,5,2,5,1],
[5,9,4,1,10,2],
[0,3,8,1,10,2,5,9,4],
[2,5,10,2,4,5,2,0,4],
[4,5,8,8,5,3,3,5,10,3,10,2],
[11,3,10,10,3,1,4,5,9],
[4,5,9,10,0,1,10,8,0,10,11,8],
[4,5,10,4,10,3,10,11,3,0,4,3],
[4,5,8,5,10,8,10,11,8],
[5,9,7,7,9,8],
[3,9,0,3,5,9,3,7,5],
[7,0,8,7,1,0,7,5,1],
[3,7,1,1,7,5],
[5,9,7,7,9,8,2,11,3],
[5,9,0,5,0,11,0,2,11,7,5,11],
[2,11,3,7,0,8,7,1,0,7,5,1],
[2,11,1,11,7,1,7,5,1],
[8,7,9,9,7,5,2,1,10],
[10,2,1,3,9,0,3,5,9,3,7,5],
[2,0,10,0,8,7,10,0,7,10,7,5],
[10,2,5,2,3,5,3,7,5],
[5,9,8,5,8,7,1,10,3,10,11,3],
[1,10,0,0,10,11,0,11,7,0,7,5,0,5,9],
[8,7,0,0,7,5,0,5,10,0,10,11,0,11,3],
[5,11,7,10,11,5],
[11,6,7],
[3,8,0,7,11,6],
[1,0,9,7,11,6],
[9,1,8,8,1,3,6,7,11],
[6,7,2,2,7,3],
[0,7,8,0,6,7,0,2,6],
[6,7,2,2,7,3,9,1,0],
[9,1,2,9,2,7,2,6,7,8,9,7],
[10,2,1,11,6,7],
[2,1,10,3,8,0,7,11,6],
[0,9,2,2,9,10,7,11,6],
[6,7,11,8,2,3,8,10,2,8,9,10],
[7,10,6,7,1,10,7,3,1],
[1,10,0,0,10,8,8,10,6,8,6,7],
[9,10,0,10,6,7,0,10,7,0,7,3],
[6,7,10,7,8,10,8,9,10],
[4,8,6,6,8,11],
[6,3,11,6,0,3,6,4,0],
[11,6,8,8,6,4,1,0,9],
[6,4,11,4,9,1,11,4,1,11,1,3],
[2,8,3,2,4,8,2,6,4],
[0,2,4,4,2,6],
[9,1,0,2,8,3,2,4,8,2,6,4],
[9,1,4,1,2,4,2,6,4],
[4,8,6,6,8,11,1,10,2],
[1,10,2,6,3,11,6,0,3,6,4,0],
[0,9,10,0,10,2,4,8,6,8,11,6],
[11,6,3,3,6,4,3,4,9,3,9,10,3,10,2],
[1,10,6,1,6,8,6,4,8,3,1,8],
[1,10,0,10,6,0,6,4,0],
[0,9,3,3,9,10,3,10,6,3,6,4,3,4,8],
[4,10,6,9,10,4],
[4,5,9,6,7,11],
[7,11,6,8,0,3,9,4,5],
[1,0,5,5,0,4,11,6,7],
[11,6,7,5,8,4,5,3,8,5,1,3],
[3,2,7,7,2,6,9,4,5],
[5,9,4,0,7,8,0,6,7,0,2,6],
[1,0,4,1,4,5,3,2,7,2,6,7],
[4,5,8,8,5,1,8,1,2,8,2,6,8,6,7],
[6,7,11,5,9,4,1,10,2],
[5,9,4,7,11,6,0,3,8,2,1,10],
[7,11,6,2,5,10,2,4,5,2,0,4],
[6,7,11,3,8,4,3,4,5,3,5,2,2,5,10],
[9,4,5,7,10,6,7,1,10,7,3,1],
[5,9,4,8,0,1,8,1,10,8,10,7,7,10,6],
[6,7,10,10,7,3,10,3,0,10,0,4,10,4,5],
[4,5,8,8,5,10,8,10,6,8,6,7],
[9,6,5,9,11,6,9,8,11],
[0,3,9,9,3,5,5,3,11,5,11,6],
[1,0,8,1,8,6,8,11,6,5,1,6],
[11,6,3,6,5,3,5,1,3],
[2,6,3,6,5,9,3,6,9,3,9,8],
[5,9,6,9,0,6,0,2,6],
[3,2,8,8,2,6,8,6,5,8,5,1,8,1,0],
[1,6,5,2,6,1],
[2,1,10,9,6,5,9,11,6,9,8,11],
[2,1,10,5,9,0,5,0,3,5,3,6,6,3,11],
[10,2,5,5,2,0,5,0,8,5,8,11,5,11,6],
[10,2,5,5,2,3,5,3,11,5,11,6],
[5,9,6,6,9,8,6,8,3,6,3,1,6,1,10],
[5,9,6,6,9,0,6,0,1,6,1,10],
[8,3,0,5,10,6],
[6,5,10],
[6,10,5],
[3,8,0,5,6,10],
[9,1,0,10,5,6],
[3,8,1,1,8,9,6,10,5],
[6,10,5,2,11,3],
[8,0,11,11,0,2,5,6,10],
[10,5,6,1,0,9,3,2,11],
[5,6,10,11,1,2,11,9,1,11,8,9],
[2,1,6,6,1,5],
[5,6,1,1,6,2,8,0,3],
[6,9,5,6,0,9,6,2,0],
[8,9,3,9,5,6,3,9,6,3,6,2],
[3,6,11,3,5,6,3,1,5],
[5,6,11,5,11,0,11,8,0,1,5,0],
[0,9,3,3,9,11,11,9,5,11,5,6],
[5,6,9,6,11,9,11,8,9],
[7,4,8,5,6,10],
[0,3,4,4,3,7,10,5,6],
[4,8,7,9,1,0,10,5,6],
[6,10,5,1,4,9,1,7,4,1,3,7],
[11,3,2,7,4,8,5,6,10],
[10,5,6,4,11,7,4,2,11,4,0,2],
[7,4,8,3,2,11,9,1,0,10,5,6],
[10,5,6,7,4,9,7,9,1,7,1,11,11,1,2],
[2,1,6,6,1,5,8,7,4],
[7,4,0,7,0,3,5,6,1,6,2,1],
[8,7,4,6,9,5,6,0,9,6,2,0],
[5,6,9,9,6,2,9,2,3,9,3,7,9,7,4],
[4,8,7,3,6,11,3,5,6,3,1,5],
[7,4,11,11,4,0,11,0,1,11,1,5,11,5,6],
[4,8,7,11,3,0,11,0,9,11,9,6,6,9,5],
[5,6,9,9,6,11,9,11,7,9,7,4],
[9,4,10,10,4,6],
[6,10,4,4,10,9,3,8,0],
[0,10,1,0,6,10,0,4,6],
[3,8,4,3,4,10,4,6,10,1,3,10],
[9,4,10,10,4,6,3,2,11],
[8,0,2,8,2,11,9,4,10,4,6,10],
[11,3,2,0,10,1,0,6,10,0,4,6],
[2,11,1,1,11,8,1,8,4,1,4,6,1,6,10],
[4,1,9,4,2,1,4,6,2],
[3,8,0,4,1,9,4,2,1,4,6,2],
[4,6,0,0,6,2],
[3,8,2,8,4,2,4,6,2],
[3,1,11,1,9,4,11,1,4,11,4,6],
[9,4,1,1,4,6,1,6,11,1,11,8,1,8,0],
[11,3,6,3,0,6,0,4,6],
[8,6,11,4,6,8],
[10,7,6,10,8,7,10,9,8],
[10,9,6,9,0,3,6,9,3,6,3,7],
[8,7,0,0,7,1,1,7,6,1,6,10],
[6,10,7,10,1,7,1,3,7],
[3,2,11,10,7,6,10,8,7,10,9,8],
[6,10,7,7,10,9,7,9,0,7,0,2,7,2,11],
[11,3,2,1,0,8,1,8,7,1,7,10,10,7,6],
[6,10,7,7,10,1,7,1,2,7,2,11],
[8,7,6,8,6,1,6,2,1,9,8,1],
[0,3,9,9,3,7,9,7,6,9,6,2,9,2,1],
[8,7,0,7,6,0,6,2,0],
[7,2,3,6,2,7],
[11,3,6,6,3,1,6,1,9,6,9,8,6,8,7],
[11,7,6,1,9,0],
[11,3,6,6,3,0,6,0,8,6,8,7],
[11,7,6],
[10,5,11,11,5,7],
[10,5,11,11,5,7,0,3,8],
[7,11,5,5,11,10,0,9,1],
[3,8,9,3,9,1,7,11,5,11,10,5],
[5,2,10,5,3,2,5,7,3],
[0,2,8,2,10,5,8,2,5,8,5,7],
[0,9,1,5,2,10,5,3,2,5,7,3],
[10,5,2,2,5,7,2,7,8,2,8,9,2,9,1],
[1,11,2,1,7,11,1,5,7],
[8,0,3,1,11,2,1,7,11,1,5,7],
[0,9,5,0,5,11,5,7,11,2,0,11],
[3,8,2,2,8,9,2,9,5,2,5,7,2,7,11],
[5,7,1,1,7,3],
[8,0,7,0,1,7,1,5,7],
[0,9,3,9,5,3,5,7,3],
[9,7,8,5,7,9],
[8,5,4,8,10,5,8,11,10],
[10,5,4,10,4,3,4,0,3,11,10,3],
[1,0,9,8,5,4,8,10,5,8,11,10],
[9,1,4,4,1,3,4,3,11,4,11,10,4,10,5],
[10,5,2,2,5,3,3,5,4,3,4,8],
[10,5,2,5,4,2,4,0,2],
[9,1,0,3,2,10,3,10,5,3,5,8,8,5,4],
[10,5,2,2,5,4,2,4,9,2,9,1],
[1,5,2,5,4,8,2,5,8,2,8,11],
[2,1,11,11,1,5,11,5,4,11,4,0,11,0,3],
[4,8,5,5,8,11,5,11,2,5,2,0,5,0,9],
[5,4,9,2,3,11],
[4,8,5,8,3,5,3,1,5],
[0,5,4,1,5,0],
[0,9,3,3,9,5,3,5,4,3,4,8],
[5,4,9],
[11,4,7,11,9,4,11,10,9],
[0,3,8,11,4,7,11,9,4,11,10,9],
[0,4,1,4,7,11,1,4,11,1,11,10],
[7,11,4,4,11,10,4,10,1,4,1,3,4,3,8],
[9,4,7,9,7,2,7,3,2,10,9,2],
[8,0,7,7,0,2,7,2,10,7,10,9,7,9,4],
[1,0,10,10,0,4,10,4,7,10,7,3,10,3,2],
[7,8,4,10,1,2],
[9,4,1,1,4,2,2,4,7,2,7,11],
[8,0,3,2,1,9,2,9,4,2,4,11,11,4,7],
[7,11,4,11,2,4,2,0,4],
[3,8,2,2,8,4,2,4,7,2,7,11],
[9,4,1,4,7,1,7,3,1],
[9,4,1,1,4,7,1,7,8,1,8,0],
[3,4,7,0,4,3],
[7,8,4],
[8,11,9,9,11,10],
[0,3,9,3,11,9,11,10,9],
[1,0,10,0,8,10,8,11,10],
[10,3,11,1,3,10],
[3,2,8,2,10,8,10,9,8],
[9,2,10,0,2,9],
[1,0,10,10,0,8,10,8,3,10,3,2],
[2,10,1],
[2,1,11,1,9,11,9,8,11],
[2,1,11,11,1,9,11,9,0,11,0,3],
[11,0,8,2,0,11],
[3,11,2],
[1,8,3,9,8,1],
[1,9,0],
[8,3,0],
[]
];
/// Same list as above, but with each row in reverse order. Needed for generating shells (two isosurfaces at slightly different iso values).
/// It is more efficient to have this static table than to call reverse() repeatedly while triangulating (although this static table was generated that way).
_MCTriangleTable_reverse = [
[],
[0,8,3],
[9,0,1],
[3,1,8,8,1,9],
[11,2,3],
[8,11,0,0,11,2],
[11,2,3,9,0,1],
[9,8,11,1,9,11,2,1,11],
[1,2,10],
[8,3,0,10,1,2],
[10,9,2,2,9,0],
[10,9,8,2,10,8,3,2,8],
[11,10,3,3,10,1],
[8,11,10,0,8,10,1,0,10],
[11,10,9,3,11,9,0,3,9],
[11,10,8,8,10,9],
[8,4,7],
[7,3,4,4,3,0],
[7,8,4,1,9,0],
[7,3,1,4,7,1,9,4,1],
[4,7,8,2,3,11],
[2,0,4,11,2,4,7,11,4],
[7,8,4,1,9,0,11,2,3],
[11,2,7,2,1,7,7,1,4,4,1,9],
[2,10,1,8,4,7],
[1,2,10,0,4,3,3,4,7],
[8,4,7,0,2,9,9,2,10],
[2,7,3,2,10,9,2,9,7,9,4,7],
[7,8,4,11,10,3,3,10,1],
[11,10,7,10,0,7,10,1,0,7,0,4],
[11,10,9,3,11,9,0,3,9,8,4,7],
[11,10,9,11,9,4,11,4,7],
[4,9,5],
[5,4,9,3,0,8],
[4,0,5,5,0,1],
[3,1,5,8,3,5,4,8,5],
[4,9,5,11,2,3],
[4,9,5,8,11,0,0,11,2],
[2,3,11,1,5,0,0,5,4],
[1,5,2,5,8,2,5,4,8,2,8,11],
[2,10,1,4,9,5],
[4,9,5,2,10,1,8,3,0],
[4,0,2,5,4,2,10,5,2],
[2,10,3,10,5,3,3,5,8,8,5,4],
[9,5,4,1,3,10,10,3,11],
[8,11,10,0,8,10,1,0,10,9,5,4],
[3,4,0,3,11,10,3,10,4,10,5,4],
[8,11,10,8,10,5,8,5,4],
[8,9,7,7,9,5],
[5,7,3,9,5,3,0,9,3],
[1,5,7,0,1,7,8,0,7],
[5,7,1,1,7,3],
[3,11,2,8,9,7,7,9,5],
[11,5,7,11,2,0,11,0,5,0,9,5],
[1,5,7,0,1,7,8,0,7,3,11,2],
[1,5,7,1,7,11,1,11,2],
[10,1,2,5,7,9,9,7,8],
[5,7,3,9,5,3,0,9,3,1,2,10],
[5,7,10,7,0,10,7,8,0,10,0,2],
[5,7,3,5,3,2,5,2,10],
[3,11,10,3,10,1,7,8,5,8,9,5],
[9,5,0,5,7,0,7,11,0,11,10,0,0,10,1],
[3,11,0,11,10,0,10,5,0,5,7,0,0,7,8],
[5,11,10,7,11,5],
[7,6,11],
[6,11,7,0,8,3],
[6,11,7,9,0,1],
[11,7,6,3,1,8,8,1,9],
[3,7,2,2,7,6],
[6,2,0,7,6,0,8,7,0],
[0,1,9,3,7,2,2,7,6],
[7,9,8,7,6,2,7,2,9,2,1,9],
[7,6,11,1,2,10],
[6,11,7,0,8,3,10,1,2],
[6,11,7,10,9,2,2,9,0],
[10,9,8,2,10,8,3,2,8,11,7,6],
[1,3,7,10,1,7,6,10,7],
[7,6,8,6,10,8,8,10,0,0,10,1],
[3,7,0,7,10,0,7,6,10,0,10,9],
[10,9,8,10,8,7,10,7,6],
[11,8,6,6,8,4],
[0,4,6,3,0,6,11,3,6],
[9,0,1,4,6,8,8,6,11],
[3,1,11,1,4,11,1,9,4,11,4,6],
[4,6,2,8,4,2,3,8,2],
[6,2,4,4,2,0],
[4,6,2,8,4,2,3,8,2,0,1,9],
[4,6,2,4,2,1,4,1,9],
[2,10,1,11,8,6,6,8,4],
[0,4,6,3,0,6,11,3,6,2,10,1],
[6,11,8,6,8,4,2,10,0,10,9,0],
[2,10,3,10,9,3,9,4,3,4,6,3,3,6,11],
[8,1,3,8,4,6,8,6,1,6,10,1],
[0,4,6,0,6,10,0,10,1],
[8,4,3,4,6,3,6,10,3,10,9,3,3,9,0],
[4,10,9,6,10,4],
[11,7,6,9,5,4],
[5,4,9,3,0,8,6,11,7],
[7,6,11,4,0,5,5,0,1],
[3,1,5,8,3,5,4,8,5,7,6,11],
[5,4,9,6,2,7,7,2,3],
[6,2,0,7,6,0,8,7,0,4,9,5],
[7,6,2,7,2,3,5,4,1,4,0,1],
[7,6,8,6,2,8,2,1,8,1,5,8,8,5,4],
[2,10,1,4,9,5,11,7,6],
[10,1,2,8,3,0,6,11,7,4,9,5],
[4,0,2,5,4,2,10,5,2,6,11,7],
[10,5,2,2,5,3,5,4,3,4,8,3,11,7,6],
[1,3,7,10,1,7,6,10,7,5,4,9],
[6,10,7,7,10,8,10,1,8,1,0,8,4,9,5],
[5,4,10,4,0,10,0,3,10,3,7,10,10,7,6],
[7,6,8,6,10,8,10,5,8,8,5,4],
[11,8,9,6,11,9,5,6,9],
[6,11,5,11,3,5,5,3,9,9,3,0],
[6,1,5,6,11,8,6,8,1,8,0,1],
[3,1,5,3,5,6,3,6,11],
[8,9,3,9,6,3,9,5,6,3,6,2],
[6,2,0,6,0,9,6,9,5],
[0,1,8,1,5,8,5,6,8,6,2,8,8,2,3],
[1,6,2,5,6,1],
[11,8,9,6,11,9,5,6,9,10,1,2],
[11,3,6,6,3,5,3,0,5,0,9,5,10,1,2],
[6,11,5,11,8,5,8,0,5,0,2,5,5,2,10],
[6,11,5,11,3,5,3,2,5,5,2,10],
[10,1,6,1,3,6,3,8,6,8,9,6,6,9,5],
[10,1,6,1,0,6,0,9,6,6,9,5],
[6,10,5,0,3,8],
[10,5,6],
[5,10,6],
[10,6,5,0,8,3],
[6,5,10,0,1,9],
[5,10,6,9,8,1,1,8,3],
[3,11,2,5,10,6],
[10,6,5,2,0,11,11,0,8],
[11,2,3,9,0,1,6,5,10],
[9,8,11,1,9,11,2,1,11,10,6,5],
[5,1,6,6,1,2],
[3,0,8,2,6,1,1,6,5],
[0,2,6,9,0,6,5,9,6],
[2,6,3,6,9,3,6,5,9,3,9,8],
[5,1,3,6,5,3,11,6,3],
[0,5,1,0,8,11,0,11,5,11,6,5],
[6,5,11,5,9,11,11,9,3,3,9,0],
[9,8,11,9,11,6,9,6,5],
[10,6,5,8,4,7],
[6,5,10,7,3,4,4,3,0],
[6,5,10,0,1,9,7,8,4],
[7,3,1,4,7,1,9,4,1,5,10,6],
[10,6,5,8,4,7,2,3,11],
[2,0,4,11,2,4,7,11,4,6,5,10],
[6,5,10,0,1,9,11,2,3,8,4,7],
[2,1,11,11,1,7,1,9,7,9,4,7,6,5,10],
[4,7,8,5,1,6,6,1,2],
[1,2,6,1,6,5,3,0,7,0,4,7],
[0,2,6,9,0,6,5,9,6,4,7,8],
[4,7,9,7,3,9,3,2,9,2,6,9,9,6,5],
[5,1,3,6,5,3,11,6,3,7,8,4],
[6,5,11,5,1,11,1,0,11,0,4,11,11,4,7],
[5,9,6,6,9,11,9,0,11,0,3,11,7,8,4],
[4,7,9,7,11,9,11,6,9,9,6,5],
[6,4,10,10,4,9],
[0,8,3,9,10,4,4,10,6],
[6,4,0,10,6,0,1,10,0],
[10,3,1,10,6,4,10,4,3,4,8,3],
[11,2,3,6,4,10,10,4,9],
[10,6,4,10,4,9,11,2,8,2,0,8],
[6,4,0,10,6,0,1,10,0,2,3,11],
[10,6,1,6,4,1,4,8,1,8,11,1,1,11,2],
[2,6,4,1,2,4,9,1,4],
[2,6,4,1,2,4,9,1,4,0,8,3],
[2,6,0,0,6,4],
[2,6,4,2,4,8,2,8,3],
[6,4,11,4,1,11,4,9,1,11,1,3],
[0,8,1,8,11,1,11,6,1,6,4,1,1,4,9],
[6,4,0,6,0,3,6,3,11],
[8,6,4,11,6,8],
[8,9,10,7,8,10,6,7,10],
[7,3,6,3,9,6,3,0,9,6,9,10],
[10,6,1,6,7,1,1,7,0,0,7,8],
[7,3,1,7,1,10,7,10,6],
[8,9,10,7,8,10,6,7,10,11,2,3],
[11,2,7,2,0,7,0,9,7,9,10,7,7,10,6],
[6,7,10,10,7,1,7,8,1,8,0,1,2,3,11],
[11,2,7,2,1,7,1,10,7,7,10,6],
[1,8,9,1,2,6,1,6,8,6,7,8],
[1,2,9,2,6,9,6,7,9,7,3,9,9,3,0],
[0,2,6,0,6,7,0,7,8],
[7,2,6,3,2,7],
[7,8,6,8,9,6,9,1,6,1,3,6,6,3,11],
[0,9,1,6,7,11],
[7,8,6,8,0,6,0,3,6,6,3,11],
[6,7,11],
[7,5,11,11,5,10],
[8,3,0,7,5,11,11,5,10],
[1,9,0,10,11,5,5,11,7],
[5,10,11,5,11,7,1,9,3,9,8,3],
[3,7,5,2,3,5,10,2,5],
[7,5,8,5,2,8,5,10,2,8,2,0],
[3,7,5,2,3,5,10,2,5,1,9,0],
[1,9,2,9,8,2,8,7,2,7,5,2,2,5,10],
[7,5,1,11,7,1,2,11,1],
[7,5,1,11,7,1,2,11,1,3,0,8],
[11,0,2,11,7,5,11,5,0,5,9,0],
[11,7,2,7,5,2,5,9,2,9,8,2,2,8,3],
[3,7,1,1,7,5],
[7,5,1,7,1,0,7,0,8],
[3,7,5,3,5,9,3,9,0],
[9,7,5,8,7,9],
[10,11,8,5,10,8,4,5,8],
[3,10,11,3,0,4,3,4,10,4,5,10],
[10,11,8,5,10,8,4,5,8,9,0,1],
[5,10,4,10,11,4,11,3,4,3,1,4,4,1,9],
[8,4,3,4,5,3,3,5,2,2,5,10],
[2,0,4,2,4,5,2,5,10],
[4,5,8,8,5,3,5,10,3,10,2,3,0,1,9],
[1,9,2,9,4,2,4,5,2,2,5,10],
[11,8,2,8,5,2,8,4,5,2,5,1],
[3,0,11,0,4,11,4,5,11,5,1,11,11,1,2],
[9,0,5,0,2,5,2,11,5,11,8,5,5,8,4],
[11,3,2,9,4,5],
[5,1,3,5,3,8,5,8,4],
[0,5,1,4,5,0],
[8,4,3,4,5,3,5,9,3,3,9,0],
[9,4,5],
[9,10,11,4,9,11,7,4,11],
[9,10,11,4,9,11,7,4,11,8,3,0],
[10,11,1,11,4,1,11,7,4,1,4,0],
[8,3,4,3,1,4,1,10,4,10,11,4,4,11,7],
[2,9,10,2,3,7,2,7,9,7,4,9],
[4,9,7,9,10,7,10,2,7,2,0,7,7,0,8],
[2,3,10,3,7,10,7,4,10,4,0,10,10,0,1],
[2,1,10,4,8,7],
[11,7,2,7,4,2,2,4,1,1,4,9],
[7,4,11,11,4,2,4,9,2,9,1,2,3,0,8],
[4,0,2,4,2,11,4,11,7],
[11,7,2,7,4,2,4,8,2,2,8,3],
[1,3,7,1,7,4,1,4,9],
[0,8,1,8,7,1,7,4,1,1,4,9],
[3,4,0,7,4,3],
[4,8,7],
[10,11,9,9,11,8],
[9,10,11,9,11,3,9,3,0],
[10,11,8,10,8,0,10,0,1],
[10,3,1,11,3,10],
[8,9,10,8,10,2,8,2,3],
[9,2,0,10,2,9],
[2,3,10,3,8,10,8,0,10,10,0,1],
[1,10,2],
[11,8,9,11,9,1,11,1,2],
[3,0,11,0,9,11,9,1,11,11,1,2],
[11,0,2,8,0,11],
[2,11,3],
[1,8,9,3,8,1],
[0,9,1],
[0,3,8],
[]
];
/// _cubindex() - private function, called by _isosurface_cubes()
/// Return the index ID of a voxel depending on the field strength at each corner exceeding isoval.
function _cubeindex(f, isoval) =
(f[0] >= isoval ? 1 : 0) +
(f[1] >= isoval ? 2 : 0) +
(f[2] >= isoval ? 4 : 0) +
(f[3] >= isoval ? 8 : 0) +
(f[4] >= isoval ? 16 : 0) +
(f[5] >= isoval ? 32 : 0) +
(f[6] >= isoval ? 64 : 0) +
(f[7] >= isoval ? 128 : 0);
/*
-----------------------------------------------------------
Bounding box clipping support:
Vertex and face layout for triangulating one voxel face that corrsesponds to a side of the box bounding all voxels.
4(back)
3 +----------+ 7
/: 5(top) /|
/ : / |
1 +==========+5 | <-- 3(side)
0(side) --> # 2+ - - - # -+ 6
# / # /
#/ 2(bot) #/
0 +----------+ 4
1(front)
The clip face uses different indexing. After vertex coordinates and function values are assigned to each corner from the original voxel based on _MCFaceVertexIndices below, this is the clip face diagram:
(1) (2)
+----1----+
| |
0 2
| |
+----3----+
(0) (3)
*/
/// four indices for each face of the cube, counterclockwise looking from inside out
_MCFaceVertexIndices = [
[],
[0,2,3,1], // left, x=0 plane
[0,1,5,4], // front, y=0 plane
[0,4,6,2], // bottom, z=0 plane
[4,5,7,6], // right, x=voxsize plane
[2,6,7,3], // back, y=voxsize plane
[1,3,7,5], // top, z=voxsize plane
];
/// Pair of vertex indices for each edge on the clip face (using clip face indexing)
_MCClipEdgeVertexIndices = [
[0,1], [1,2], [2,3], [3,0]
];
/// In keeping with the convention for triangulating an isosurface through a voxel, analogous to the case in which two surfaces separate two diagonally opposite high-value corners of one face, in 2D contour terms it is assumed there is a valley separating two high corners, not a ridge connecting them. The 8 ambiguous triangulation cases for opposing corners are set up accordingly. These are the rotational groups of indices {10,30}, {11,19,33,57}, {20,60} in the array below.
/// For each of the 81 possible configurations of a clip face intersected by a minimum and/or maximum isovalue, define a list of triangles, specified as pairs of corner ID and edge ID arrays, with a total of 3 points in each pair. Each pair has the form [corner],[edge1,edge2] or [corner1,corner2],[edge], or [corner1,corner2,corner3],[] or [],[edge1,edge2,edge3].
_MCClipTriangleTable = [
// Explanation of inline comments:
// "base-3 index = decimal index", followed by
// "(xRotations)" for number of rotation versions, or
// "(Rotation n from decimal index)" indicating which decimal index this was rotated from, where n=the number of 90° clockwise rotations from the original.
[], // 0000 = 0 (×1)
[[0],[0,3]], // 0001 = 1 (×4)
[[],[7,4,3,3,4,0]], // 0002 = 2 (×4)
[[1],[1,0]], // 0010 = 3 (r1 from 1)
[[0,1],[1],[0],[1,3]], // 0011 = 4 (×4)
[[1],[1,4],[],[4,3,7],[],[4,1,3]], // 0012 = 5 (×4)
[[],[4,5,0,0,5,1]], // 0020 = 6 (r1 from 2)
[[0],[4,3],[],[4,5,1],[],[4,1,3]], // 0021 = 7 (×4)
[[],[7,5,1,1,3,7]], // 0022 = 8 (×4)
[[2],[2,1]], // 0100 = 9 (r2 from 1)
[[0],[0,3],[2],[2,1]], // 0101 = 10 (×2)
[[],[7,4,3,3,4,0],[2],[2,1]], // 0102 = 11 (×4)
[[1,2],[2],[1],[2,0]], // 0110 = 12 (r1 from 4)
[[0,1],[3],[1],[2,3],[1,2],[2]], // 0111 = 13 (×4)
[[1,2],[4],[2],[2,4],[],[2,3,7],[],[2,7,4]], // 0112 = 14 (×4)
[[2],[2,5],[],[5,0,4],[],[5,2,0]], // 0120 = 15 (r1 from 5)
[[0],[4,3],[2],[2,5],[],[4,5,2],[],[2,3,4]], // 0121 = 16 (×4)
[[2],[2,5],[],[2,3,7],[],[5,2,7]], // 0122 = 17 (×4)
[[],[5,6,1,1,6,2]], // 0200 = 18 (r2 from 2)
[[],[5,6,1,1,6,2],[0],[0,3]], // 0201 = 19 (r2 from 11)
[[],[7,4,0],[],[0,3,7],[],[1,5,6],[],[6,2,1]], // 0202 = 20 (×2)
[[1],[5,0],[],[5,6,2],[],[5,2,0]], // 0210 = 21 (r1 from 7)
[[0,1],[3],[1],[5,3],[],[3,5,2],[],[5,6,2]], // 0211 = 22 (×4)
[[1],[5,4],[],[5,6,7],[],[6,2,3],[],[6,3,7],[],[7,4,5]], // 0212 = 23 (×4)
[[],[4,6,2,2,0,4]], // 0220 = 24 (r1 from 8)
[[0],[4,3],[],[3,4,6],[],[6,2,3]], // 0221 = 25 (×4)
[[],[2,3,7,2,7,6]], // 0222 = 26 (×4)
[[3],[3,2]], // 1000 = 27 (r3 from 1)
[[3,0],[0],[3],[0,2]], // 1001 = 28 (r3 from 4)
[[3],[7,2],[],[7,4,0],[],[7,0,2]], // 1002 = 29 (r3 from 7)
[[1],[1,0],[3],[3,2]], // 1010 = 30 (r1 from 10)
[[3,0],[2],[0],[1,2],[0,1],[1]], // 1011 = 31 (r3 from 13)
[[3],[7,2],[1],[1,4],[],[7,4,1],[],[1,2,7]], // 1012 = 32 (r3 from 16)
[[],[4,5,0,0,5,1],[3],[3,2]], // 1020 = 33 (r1 from 11)
[[3,0],[2],[0],[4,2],[],[2,4,1],[],[4,5,1]], // 1021 = 34 (r3 from 22)
[[3],[7,2],[],[2,7,5],[],[5,1,2]], // 1022 = 35 (r3 from 25)
[[2,3],[3],[2],[3,1]], // 1100 = 36 (r2 from 4)
[[2,3],[1],[3],[0,1],[3,0],[0]], // 1101 = 37 (r2 from 13)
[[2,3],[1],[3],[7,1],[],[1,7,0],[],[7,4,0]], // 1102 = 38 (r2 from 22)
[[1,2],[0],[2],[3,0],[2,3],[3]], // 1110 = 39 (r1 from 13)
[[0,1,2],[],[0,2,3],[]], // 1111 = 40 (×1)
[[1,2],[4],[2],[7,4],[2,3],[7]], // 1112 = 41 (×4)
[[2,3],[5],[3],[3,5],[],[3,0,4],[],[3,4,5]], // 1120 = 42 (r1 from 14)
[[2,3],[5],[3],[4,5],[3,0],[4]], // 1121 = 43 (r1 from 41)
[[2],[7,5],[2,3],[7]], // 1122 = 44 (×4)
[[3],[3,6],[],[6,1,5],[],[6,3,1]], // 1200 = 45 (r2 from 5)
[[3,0],[6],[0],[0,6],[],[0,1,5],[],[0,5,6]], // 1201 = 46 (r2 from 14)
[[3],[7,6],[],[7,4,5],[],[4,0,1],[],[4,1,5],[],[5,6,7]], // 1202 = 47 (r2 from 23)
[[1],[5,0],[3],[3,6],[],[5,6,3],[],[3,0,5]], // 1210 = 48 (r1 from 16)
[[3,0],[6],[0],[5,6],[0,1],[5]], // 1211 = 49 (r2 from 41)
[[1],[5,4],[3],[7,6],[],[4,5,6],[],[4,6,7]], // 1212 = 50 (×2)
[[3],[3,6],[],[3,0,4],[],[6,3,4]], // 1220 = 51 (r1 from 17)
[[3],[4,6],[3,0],[4]], // 1221 = 52 (r1 from 44)
[[3],[7,6]], // 1222 = 53 (×4)
[[],[6,7,2,2,7,3]], // 2000 = 54 (r3 from 2)
[[0],[0,7],[],[7,2,6],[],[7,0,2]], // 2001 = 55 (r3 from 5)
[[],[6,4,0,0,2,6]], // 2002 = 56 (r3 from 8)
[[],[6,7,2,2,7,3],[1],[1,0]], // 2010 = 57 (r3 from 11)
[[0,1],[7],[1],[1,7],[],[1,2,6],[],[1,6,7]], // 2011 = 58 (r3 from 14)
[[1],[1,4],[],[1,2,6],[],[4,1,6]], // 2012 = 59 (r3 from 17)
[[],[4,5,1],[],[1,0,4],[],[2,6,7],[],[7,3,2]], // 2020 = 60 (r1 from 20)
[[0],[4,7],[],[4,5,6],[],[5,1,2],[],[5,2,6],[],[6,7,4]], // 2021 = 61 (r3 from 23)
[[],[1,2,6,1,6,5]], // 2022 = 62 (r3 from 26)
[[2],[6,1],[],[6,7,3],[],[6,3,1]], // 2100 = 63 (r2 from 7)
[[2],[6,1],[0],[0,7],[],[6,7,0],[],[0,1,6]], // 2101 = 64 (r2 from 16)
[[2],[6,1],[],[1,6,4],[],[4,0,1]], // 2102 = 65 (r2 from 25)
[[1,2],[0],[2],[6,0],[],[0,6,3],[],[6,7,3]], // 2110 = 66 (r1 from 22)
[[0,1],[7],[1],[6,7],[1,2],[6]], // 2111 = 67 (r3 from 41)
[[1],[6,4],[1,2],[6]], // 2112 = 68 (r3 from 44)
[[2],[6,5],[],[6,7,4],[],[7,3,0],[],[7,0,4],[],[4,5,6]], // 2120 = 69 (r1 from 23)
[[2],[6,5],[0],[4,7],[],[5,6,7],[],[5,7,4]], // 2121 = 70 (r1 from 50)
[[2],[6,5]], // 2122 = 71 (r3 from 53)
[[],[5,7,3,3,1,5]], // 2200 = 72 (r2 from 8)
[[0],[0,7],[],[0,1,5],[],[7,0,5]], // 2201 = 73 (r2 from 17)
[[],[0,1,5,0,5,4]], // 2202 = 74 (r2 from 26)
[[1],[5,0],[],[0,5,7],[],[7,3,0]], // 2210 = 75 (r1 from 25)
[[0],[5,7],[0,1],[5]], // 2211 = 76 (r2 from 44)
[[1],[5,4]], // 2212 = 77 (r2 from 53)
[[],[3,0,4,3,4,7]], // 2220 = 78 (r1 from 26)
[[0],[4,7]], // 2221 = 79 (r1 from 53)
[] // 2222 = 80 (×1)
];
/// _clipfacindex() - private function, called by _clipfacevertices()
/// Return the index ID of a voxel face depending on the field strength at each corner in relation to isovalmin and isovalmax.
// Returns a decimal version of a 4-digit base-3 index.
function _clipfacindex(f, isovalmin, isovalmax) =
(f[0] >= isovalmax ? 2 : f[0] >= isovalmin ? 1 : 0) +
(f[1] >= isovalmax ? 6 : f[1] >= isovalmin ? 3 : 0) +
(f[2] >= isovalmax ? 18 : f[2] >= isovalmin ? 9 : 0) +
(f[3] >= isovalmax ? 54 : f[3] >= isovalmin ? 27 : 0);
/// return an array of face indices in _MCFaceVertexIndices if the voxel at coordinate v0 corresponds to the bounding box. voxsize is a 3-vector.
function _bbox_faces(v0, voxsize, bbox) = let(
a = v_abs(v0-bbox[0]),
bb1 = bbox[1] - voxsize,
b = v0-bb1
) [
if(a[0]<_EPSILON) 1,
if(a[1]<_EPSILON) 2,
if(a[2]<_EPSILON) 3,
if(b[0]>=-_EPSILON) 4,
if(b[1]>=-_EPSILON) 5,
if(b[2]>=-_EPSILON) 6
];
/// End of bounding-box face-clipping stuff
/// -----------------------------------------------------------
/// isosurface_cubes() - private function, called by isosurface()
/// This implements a marching cubes algorithm, sacrificing some memory in favor of speed.
/// Return a list of voxel cube structures that have one or both surfaces isovalmin or isovalmax intersecting them, and cubes inside the isosurface volume that are at the bounds of the bounding box.
/// The cube structure is:
/// [cubecoord, cubeindex_isomin, cubeindex_isomax, cf, bfaces]
/// where
/// cubecoord is the [x,y,z] coordinate of the front left bottom corner of the voxel.
/// cubeindex_isomin and cubeindex_isomax are the index IDs of the voxel corresponding to the min and max iso surface intersections.
/// cf (corner function) is vector containing the 8 field strength values at each corner of the voxel cube.
/// bfaces is an array of faces corresponding to the sides of the bounding box - this is empty most of the time; it has data only where the isosurface is clipped by the bounding box.
/// The bounding box 'bbox' is expected to be quantized for the voxel size already, and `voxsize` is a 3-vector.
function _isosurface_cubes(voxsize, bbox, fieldarray, fieldfunc, isovalmin, isovalmax, closed=true) = let(
// get field intensities
field = is_def(fieldarray)
? fieldarray
: let(v = bbox[0], hv = 0.5*voxsize, b1 = bbox[1]+hv) [
for(x=[v.x:voxsize.x:b1.x]) [
for(y=[v.y:voxsize.y:b1.y]) [
for(z=[v.z:voxsize.z:b1.z])
fieldfunc(x,y,z)
]
]
],
nx = len(field)-2,
ny = len(field[0])-2,
nz = len(field[0][0])-2,
v0 = bbox[0]
) [
for(i=[0:nx]) let(x=v0[0]+i*voxsize.x)
for(j=[0:ny]) let(y=v0[1]+j*voxsize.y)
for(k=[0:nz]) let(z=v0[2]+k*voxsize.z)
let(i1=i+1, j1=j+1, k1=k+1,
cf = [ // cube corner field values clamped to ±1e9
min(1e9,max(-1e9,field[i][j][k])),
min(1e9,max(-1e9,field[i][j][k1])),
min(1e9,max(-1e9,field[i][j1][k])),
min(1e9,max(-1e9,field[i][j1][k1])),
min(1e9,max(-1e9,field[i1][j][k])),
min(1e9,max(-1e9,field[i1][j][k1])),
min(1e9,max(-1e9,field[i1][j1][k])),
min(1e9,max(-1e9,field[i1][j1][k1]))
],
mincf = min(cf),
maxcf = max(cf),
cubecoord = [x,y,z],
bfaces = closed ? _bbox_faces(cubecoord, voxsize, bbox) : [],
cubefound_isomin = (mincf<=isovalmin && isovalmin<=maxcf),
cubefound_isomax = (mincf<=isovalmax && isovalmax<=maxcf),
cubefound_outer = len(bfaces)==0 ? false
: let(
bf = flatten([for(i=bfaces) _MCFaceVertexIndices[i]]),
sumcond = len([for(b=bf) if(isovalmin<=cf[b] && cf[b]<=isovalmax) 1 ])
) sumcond == len(bf), // true if full faces are inside
cubeindex_isomin = cubefound_isomin ? _cubeindex(cf, isovalmin) : 0,
cubeindex_isomax = cubefound_isomax ? _cubeindex(cf, isovalmax) : 0
) if(cubefound_isomin || cubefound_isomax || cubefound_outer)
[ // return data structure:
cubecoord, // voxel lower coordinate
cubeindex_isomin, // cube ID for isomin
cubeindex_isomax, // cube ID for isomax
cf, // clamped voxel corner values
bfaces // list of bounding box faces, if any
]
];
/// _isosurface_trangles() - called by isosurface()
/// Given a list of voxel cubes structures, triangulate the isosurface(s) that intersect each cube and return a list of triangle vertices.
function _isosurface_triangles(cubelist, voxsize, isovalmin, isovalmax, tritablemin, tritablemax) = [
for(cl=cubelist)
let(
v = cl[0], // voxel coord
cbidxmin = cl[1], // cube ID for isomvalmin
cbidxmax = cl[2], // cube ID for isovalmax
f = cl[3], // function values for each cube corner
bbfaces = cl[4], // faces (if any) on the bounding box
vcube = [ // list of cube corner vertex coordinates
v, v+[0,0,voxsize.z], v+[0,voxsize.y,0], v+[0,voxsize.y,voxsize.z],
v+[voxsize.x,0,0], v+[voxsize.x,0,voxsize.z],
v+[voxsize.x,voxsize.y,0], v+voxsize
]
)
each [
if(len(tritablemin[cbidxmin])>0) for(ei=tritablemin[cbidxmin]) // min surface
let(
edge = _MCEdgeVertexIndices[ei],
vi0 = edge[0],
vi1 = edge[1],
denom = f[vi1] - f[vi0],
u = abs(denom)<0.00001 ? 0.5 : (isovalmin-f[vi0]) / denom
)
vcube[vi0] + u*(vcube[vi1]-vcube[vi0]),
if(len(tritablemax[cbidxmax])>0) for(ei=tritablemax[cbidxmax]) // max surface
let(
edge = _MCEdgeVertexIndices[ei],
vi0 = edge[0],
vi1 = edge[1],
denom = f[vi1] - f[vi0],
u = abs(denom)<0.00001 ? 0.5 : (isovalmax-f[vi0]) / denom
)
vcube[vi0] + u*(vcube[vi1]-vcube[vi0]),
if(len(bbfaces)>0) for(bf = bbfaces)
each _clipfacevertices(vcube, f, bf, isovalmin, isovalmax)
]
];
/// Generate triangles for the special case of voxel faces clipped by the bounding box
function _clipfacevertices(vcube, fld, bbface, isovalmin, isovalmax) =
let(
vi = _MCFaceVertexIndices[bbface], // four voxel face vertex indices
vface = [ for(i=vi) vcube[i] ], // four voxel face vertex coordinates
f = [ for(i=vi) fld[i] ], // four corner field values
idx = _clipfacindex(f, isovalmin, isovalmax)
) [
if(idx>0 && idx<80)
let(tri = _MCClipTriangleTable[idx])
for(i=[0:2:len(tri)-1]) let(
cpath = tri[i],
epath = tri[i+1]
) each [
for(corner=cpath) vface[corner],
for(edge=epath) let(
iso = edge>3 ? isovalmax : isovalmin,
e = edge>3 ? edge-4 : edge,
v0 = e,
v1 = (e+1)%4,
denom = f[v1]-f[v0],
u = abs(denom)<0.00001 ? 0.5 : (iso-f[v0]) / denom
) vface[v0] + u*(vface[v1]-vface[v0])
]
];
//////////////////// 2D initializations and support functions ////////////////////
/*
"Marching triangles" algorithm
A square pixel has 5 vertices, four on each corner and one in the center. Vertices and edges are numbered as follows:
(1) (3)
+-------1-------+
| \ / |
| 5 6 |
| \ / |
0 (4) 2
| / \ |
| 4 7 |
| / \ |
+-------3-------+
(0) (2)
The vertices are assigned a value 1 if greater than or equal to the isovalue, or 0 if less than the isovalue.
These ones and zeros, when arranged as a binary number with vertex (0) being the least significant bit and vertex (4) the most significant, forms an address ranging from 0 to 31.
This address is used as an index in _MTriSegmentTable to get the order of edges that are crossed.
*/
// vertices that make each edge
_MTEdgeVertexIndices = [
[0, 1],
[1, 3],
[3, 2],
[2, 0],
[0, 4],
[1, 4],
[3, 4],
[2, 4]
];
// edge order for drawing a contour (or two contours) through a pixel, for all 32 possibilities of vertices being higher or lower than isovalue
_MTriSegmentTable = [ // marching triangle segment table
[[], []], // 0 - 00000
[[0,4,3], []], // 1 - 00001
[[1,5,0], []], // 2 - 00010
[[1,5,4,3], []], // 3 - 00011
[[3,7,2], []], // 4 - 00100
[[0,4,7,2], []], // 5 - 00101
[[1,5,0], [3,7,2]], // 6 - 00110 - 2 corners
[[1,5,4,7,2], []], // 7 - 00111
[[2,6,1], []], // 8 - 01000
[[0,4,3], [2,6,1]], // 9 - 01001 - 2 corners
[[2,6,5,0], []], //10 - 01010
[[2,6,5,4,3], []], //11 - 01011
[[3,7,6,1], []], //12 - 01100
[[0,4,7,6,1], []], //13 - 01101
[[3,7,6,5,0], []], //14 - 01110
[[7,6,5,4,7], []], //15 - 01111 low center - pixel encloses contour
[[4,5,6,7,4], []], //16 - 10000 high center - pixel encloses contour
[[0,5,6,7,3], []], //17 - 10001
[[1,6,7,4,0], []], //18 - 10010