-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathisosurface.scad
1413 lines (1353 loc) · 64 KB
/
isosurface.scad
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
// An isosurface is a three-dimensional surface representing points of a constant
// value (e.g. density pressure, temperature, electric field strength, density) in a
// 3D volume. It is essentially a 3D cross-section of a 4-dimensional function.
// An isosurface may be represented generally by any function of three variables,
// that is, the function returns a single value based on [x,y,z] inputs. The
// isosurface is defined by all return values equal to a constant isovalue.
// .
// A [gryoid](https://en.wikipedia.org/wiki/Gyroid) (often used as a volume infill pattern in [FDM 3D printing](https://en.wikipedia.org/wiki/Fused_filament_fabrication))
// is an exmaple of an isosurface that is unbounded and periodic in all three dimensions.
// Other typical examples in 3D graphics are [metaballs](https://en.wikipedia.org/wiki/Metaballs) (also known as "blobby objects"),
// which are bounded and closed organic-looking surfaces that meld together when in close proximity.
//
// Includes:
// include <BOSL2/std.scad>
// include <BOSL2/isosurface.scad>
// FileGroup: Advanced Modeling
// FileSummary: Isosurfaces and metaballs.
//////////////////////////////////////////////////////////////////////
/*
Lookup Tables for Transvoxel's Modified Marching Cubes
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, 10000110 (8-bit binary for decimal index 134) has corners 2, 3, and 7 greater than the threshold. After determining the cube's index value, 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
-----------------------------------------------------------
Addition by Alex Matulich:
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)
*/
/// 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
];
/// return an array of face indices in _MCFaceVertexIndices if the voxel at coordinate v0 corresponds to the bounding box.
function _bbox_faces(v0, voxsize, bbox) = let(
a = v0-bbox[0],
bb1 = bbox[1] - [voxsize,voxsize,voxsize],
b = v0-bb1
) [
if(a[0]==0) 1,
if(a[1]==0) 2,
if(a[2]==0) 3,
if(b[0]>=0) 4,
if(b[1]>=0) 5,
if(b[2]>=0) 6
];
/// End of bounding-box faace-clipping stuff. Back to the marching cubes triangulation....
/// 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 255 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).
/// More efficient just to have a static table than to generate it each time by calling reverse() hundreds of times (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],
[]
];
// Function&Module: isosurface()
// Synopsis: Creates a 3D isosurface.
// SynTags: Geom,VNF
// Topics: Isosurfaces, VNF Generators
// Usage: As a module
// isosurface(voxel_size, bounding_box, isovalue, field_function, [additional=], [reverse=], [close_clip=], [show_stats=]);
// Usage: As a function
// vnf = isosurface(voxel_size, bounding_box, isovalue, field_function, [additional=], [close_clip=], [show_stats=]);
// Description:
// When called as a function, returns a [VNF structure](vnf.scad) (list of triangles and faces) representing a 3D isosurface within the specified bounding box at a single isovalue or range of isovalues.
// When called as a module, displays the isosurface within the specified bounding box at a single isovalue or range of isovalues. This module just passes the parameters to the function, and then calls {{vnf_polyhedron()}} to display the isosurface.
// .
// A [marching cubes](https://en.wikipedia.org/wiki/Marching_cubes) algorithm is used
// to identify an envelope containing the isosurface within the bounding box. The surface
// intersecttion with a voxel cube is then triangulated to form a surface fragment, which is
// combined with all other surface fragments. Ambiguities in triangulating the surfaces
// in certain voxel cube configurations are resolved so that all triangular facets are
// properly oriented with no holes in the surface. If a side of the bounding box clips
// the isosurface, this clipped area is filled in so that the surface remains manifold.
// .
// Be mindful of how you set `voxel_size` and `bounding_box`. For example a voxel size
// of 1 unit with a bounding box volume of 200×200×200 may be noticeably slow,
// requiring calculation and storage of 8,000,000 field values, and more processing
// and memory to generate the triangulated mesh. On the other hand, a voxel size of 5
// in a 100×100×100 bounding box requires only 8,000 field values and the mesh
// generates fairly quickly, just a handful of seconds. A good rule is to keep the
// number of field values below 10,000 for preview, and adjust the voxel size
// smaller for final rendering. If the isosurface fits completely within the bounding
// box, you can call {{pointlist_bounds()}} on `vnf[0]` returned from the
// `isosurface()` function to get an idea of a more optimal smaller bounding box to use,
// possibly allowing increasing resolution by decresing the voxel size. You can also set
// the parameter `show_stats=true` to get the bounds of the voxels containing the surface.
// .
// The point list in the VNF structure contains many duplicated points. This is not a
// problem for rendering the shape, but if you want to eliminate these, you can pass
// the structure to {{vnf_merge_points()}}. 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 expensive for execution time
// and are not normally necessary.
// Arguments:
// voxel_size = The size (scalar) of the voxel cube that determines the resolution of the surface.
// bounding_box = A pair of 3D points `[[xmin,ymin,zmin], [xmax,ymax,zmax]]`, specifying the minimum and maximum corner coordinates of the bounding box. You don't have ensure that the voxels fit perfectly inside the bounding box. While the voxel at the minimum bounding box corner is aligned on that corner, the last voxel at the maximum box corner may extend a bit beyond it.
// isovalue = As a scalar, specifies the output value of `field_function` corresponding to the isosurface. As a vector `[min_isovalue, max_isovalue]`, specifies the range of isovalues around which to generate a surface. For closed surfaces, a single value results in a closed volume, and a range results in a shell (with an inside and outside surface) enclosing a volume. A range must be specified for infinite-extent surfaces (such as gyroids) to create a manifold shape within the bounding box.
// field_function = A [function literal](https://en.wikibooks.org/wiki/OpenSCAD_User_Manual/User-Defined_Functions_and_Modules#Function_literals) taking as input an `[x,y,z]` coordinate and optional additional parameters, and returns a single value.
// ---
// additional = A single value, or an array of optional additional parameters that may be required by the field function. It is your responsibility to create a function literal compatible with these inputs. If `additional` is not set, only the `[x,y,z]` parameter is passed to the function; no additional parameters are passed. Default: undef
// reverse = When true, reverses the orientation of the facets in the mesh. Default: false
// close_clip = When true, maintains a manifold surface where the bounding box clips it (there is a negligible speed penalty in doing this). When false, the bounding box clips the surface, exposing the back sides of facets. Setting this to false can be useful with OpenSCAD's "View > Thrown Together" menu option to distinguish inside from outside. Default: true
// show_stats = If true, display statistics about the isosurface in the console window. Besides the number of voxels found to contain the surface, and the number of triangles making up the surface, this is useful for getting information about a smaller bounding box possible for the isosurface, to improve speed for subsequent renders. Enabling this parameter has a speed penalty. Default: false
// Example(3D,ThrownTogether,NoAxes): A gyroid is an isosurface defined by all the zero values of a 3D periodic function. To illustrate what the surface looks like, `close_clip=false` has been set to expose both sides of the surface. The surface is periodic and tileable along all three axis directions. This a non-manifold surface as displayed, not useful for 3D modeling. This example also demonstrates the use of the `additional` parameter, which in this case controls the wavelength of the gyroid.
// gyroid = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) sin(px)*cos(py) + sin(py)*cos(pz) + sin(pz)*cos(px);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=5, bounding_box=bbox, isovalue=0,
// field_function=gyroid, additional=200, close_clip=false);
// Example(3D,NoAxes): If we remove the `close_clip` parameter or set it to true, the isosurface algorithm encloses the entire half-space bounded by the "inner" gyroid surface, leaving only the "outer" surface exposed. This is a manifold shape but not what we want if trying to model a gyroid.
// gyroid = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) sin(px)*cos(py) + sin(py)*cos(pz) + sin(pz)*cos(px);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=5, bounding_box=bbox, isovalue=0,
// field_function=gyroid, additional=200);
// Example(3D,ThrownTogether,NoAxes): To make the gyroid a double-sided surface, we need to specify a small range around zero for `isovalue`. Now we have a double-sided surface although with `close_clip=false` the edges are not closed where the surface is clipped by the bounding box.
// gyroid = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) sin(px)*cos(py) + sin(py)*cos(pz) + sin(pz)*cos(px);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=5, bounding_box=bbox, isovalue=[-0.3, 0.3],
// field_function=gyroid, additional=200, close_clip=false);
// Example(3D,ThrownTogether,NoAxes): To make the gyroid a valid manifold 3D object, we remove the `close_clip` parameter (same as setting `close_clip=true`), which closes the edges where the surface is clipped by the bounding box. The resulting object can be tiled, the VNF returned by the functional version can be wrapped around an axis using {{vnf_bend()}}, and other operations.
// gyroid = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) sin(px)*cos(py) + sin(py)*cos(pz) + sin(pz)*cos(px);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=5, bounding_box=bbox, isovalue=[-0.3, 0.3],
// field_function=gyroid, additional=200);
// Example(3D,NoAxes): An approximation of the triply-periodic minimal surface known as [Schwartz P](https://en.wikipedia.org/wiki/Schwarz_minimal_surface).
// schwartz_p = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) cos(px) + cos(py) + cos(pz);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=4, bounding_box=bbox, isovalue=[-0.2,0.2],
// field_function=schwartz_p, additional=100);
// Example(3D,NoAxes): Another approximation of the triply-periodic minimal surface known as [Neovius](https://en.wikipedia.org/wiki/Neovius_surface).
// neovius = function (xyz, wavelength) let(
// p = 360/wavelength,
// px = p*xyz[0],
// py = p*xyz[1],
// pz = p*xyz[2]
// ) 3*(cos(px) + cos(py) + cos(pz)) + 4*cos(px)*cos(py)*cos(pz);
//
// bbox = [[-100,-100,-100], [100,100,100]];
// isosurface(voxel_size=4, bounding_box=bbox, isovalue=[-0.3,0.3],
// field_function=neovius, additional=200);
module isosurface(voxel_size, bounding_box, isovalue, field_function, additional, reverse=false, close_clip=true, show_stats=false) {
vnf = isosurface(voxel_size, bounding_box, isovalue, field_function, additional, reverse, close_clip, show_stats);
vnf_polyhedron(vnf);
}
function isosurface(voxel_size, bounding_box, isovalue, field_function, additional, reverse=false, close_clip=true, show_stats=false) =
assert(all_defined([voxel_size, bounding_box, isovalue, field_function]), "The parameters voxel_size, bounding_box, isovalue, and field_function must all be defined.")
let(
isovalmin = is_list(isovalue) ? isovalue[0] : isovalue,
isovalmax = is_list(isovalue) ? isovalue[1] : INF,
newbbox = let( // new bounding box quantized for voxel_size
hv = 0.5*voxel_size,
bbn = (bounding_box[1]-bounding_box[0]+[hv,hv,hv]) / voxel_size,
bbsize = [round(bbn[0]), round(bbn[1]), round(bbn[2])] * voxel_size
) [bounding_box[0], bounding_box[0]+bbsize],
cubes = _isosurface_cubes(voxel_size, bbox=newbbox, fieldfunc=field_function, additional=additional, isovalmin=isovalmin, isovalmax=isovalmax, close_clip=close_clip),
tritablemin = reverse ? _MCTriangleTable_reverse : _MCTriangleTable,
tritablemax = reverse ? _MCTriangleTable : _MCTriangleTable_reverse,
trianglepoints = _isosurface_triangles(cubes, voxel_size, isovalmin, isovalmax, tritablemin, tritablemax),
faces = [ for(i=[0:3:len(trianglepoints)-1]) [i,i+1,i+2] ],
dummy = show_stats ? _showstats(voxel_size, newbbox, isovalmin, cubes, faces) : 0
) [trianglepoints, faces];
// Function&Module: isosurface_array()
// Synopsis: Creates a 3D isosurface from a 3D array of densities.
// SynTags: Geom,VNF
// Topics: Isosurfaces, VNF Generators
// Usage: As a module
// isosurface_array(voxel_size, isovalue, fields, [origin=], [reverse=], [close_clip=], [show_stats=]);
// Usage: As a function
// vnf = isosurface_array(voxel_size, isovalue, fields, [origin=], [reverse=], [close_clip=], [show_stats=]);
// Description:
// When called as a function, returns a [VNF structure](vnf.scad) (list of triangles and
// faces) representing a 3D isosurface within the passed array at a single isovalue or
// range of isovalues.
// When called as a module, displays the isosurface within the passed array at a single
// isovalue or range of isovalues. This module just passes the parameters to the function,
// and then calls {{vnf_polyhedron()}} to display the isosurface.
// .
// Use this when you already have a 3D array of intensity or density data, for example like
// what you may get from a [CT scan](https://en.wikipedia.org/wiki/CT_scan).
// .
// By default, the returned VNF structure occupies a volume with its origin at [0,0,0]
// extending in the positive x, y, and z directions by multiples of `voxel_size`.
// This origin can be overridden by the `origin` parameter.
// .
// The point list in the VNF structure contains many duplicated points. This is not a
// problem for rendering the shape, but if you want to eliminate these, you can pass
// the structure to {{vnf_merge_points()}}. Additionally, flat surfaces at the outer limits
// of the `fields` array 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 expensive for execution time
// and are not normally necessary.
// Arguments:
// voxel_size = The size (scalar) of the voxel cube that determines the resolution of the surface.
// isovalue = As a scalar, specifies the output value of `field_function` corresponding to the isosurface. As a vector `[min_isovalue, max_isovalue]`, specifies the range of isovalues around which to generate a surface. For closed surfaces, a single value results in a closed volume, and a range results in a shell (with an inside and outside surface) enclosing a volume. A range must be specified for surfaces (such as gyroids) that have both sides exposed within the bounding box.
// fields = 3D array of field intesities. This array should be organized so that the indices are in order of x, y, and z when the array is referenced; that is, `fields[x_index][y_index][z_index]` has `z_index` changing most rapidly as the array is traversed. If you organize the array differently, you may have to perform a `rotate()` or `mirror()` operation on the final result to orient it properly.
// ---
// origin = Origin in 3D space corresponding to `fields[0][0][0]`. The bounding box of the isosurface extends from this origin by multiples of `voxel_size` according to the size of the `fields` array. Default: [0,0,0]
// reverse = When true, reverses the orientation of the facets in the mesh. Default: false
// close_clip = When true, maintains a manifold surface where the bounding box clips it (there is a negligible speed penalty in doing this). When false, the bounding box clips the surface, exposes the back sides of facets. Setting this to false can be useful with OpenSCAD's "View > Thrown together" menu option to distinguish inside from outside. Default: true
// show_stats = If true, display statistics about the isosurface in the console window. Besides the number of voxels found to contain the surface, and the number of triangles making up the surface, this is useful for getting information about a smaller bounding box possible for the isosurface, to improve speed for subsequent renders. Enabling this parameter has a speed penalty. Default: false
// Example(3D):
// fields = [
// repeat(0,[6,6]),
// [ [0,1,2,2,1,0],
// [1,2,3,3,2,1],
// [2,3,4,4,3,2],
// [2,3,4,4,3,2],
// [1,2,3,3,2,1],
// [0,1,2,2,1,0]
// ],
// [ [0,0,0,0,0,0],
// [0,0,1,1,0,0],
// [0,2,3,3,2,0],
// [0,2,3,3,2,0],
// [0,0,1,1,0,0],
// [0,0,0,0,0,0]
// ],
// [ [0,0,0,0,0,0],
// [0,0,0,0,0,0],
// [0,1,2,2,1,0],
// [0,1,2,2,1,0],
// [0,0,0,0,0,0],
// [0,0,0,0,0,0]
// ],
// repeat(0,[6,6])
// ];
// rotate([0,-90,180])
// isosurface_array(voxel_size=10,
// isovalue=0.5, fields=fields);
module isosurface_array(voxel_size, isovalue, fields, origin=[0,0,0], reverse=false, close_clip=true, show_stats=false) {
vnf = isosurface_array(voxel_size, isovalue, fields, origin, reverse, close_clip, show_stats);
vnf_polyhedron(vnf);
}
function isosurface_array(voxel_size, isovalue, fields, origin=[0,0,0], reverse=false, close_clip=true, show_stats=false) =
assert(all_defined([voxel_size, fields, isovalue]), "The parameters voxel_size, fields, and isovalue must all be defined.")
let(
isovalmin = is_list(isovalue) ? isovalue[0] : isovalue,
isovalmax = is_list(isovalue) ? isovalue[1] : INF,
bbox = let(
nx = len(fields)-1,
ny = len(fields[0])-1,
nz = len(fields[0][0])-1
) [origin, origin+[nx*voxel_size, ny*voxel_size, nz*voxel_size]],
cubes = _isosurface_cubes(voxel_size, bbox, fieldarray=fields, isovalmin=isovalmin, isovalmax=isovalmax, close_clip=close_clip),
tritablemin = reverse ? _MCTriangleTable_reverse : _MCTriangleTable,
tritablemax = reverse ? _MCTriangleTable : _MCTriangleTable_reverse,
trianglepoints = _isosurface_triangles(cubes, voxel_size, isovalmin, isovalmax, tritablemin, tritablemax),
faces = [ for(i=[0:3:len(trianglepoints)-1]) [i,i+1,i+2] ],
dummy = show_stats ? _showstats(voxel_size, bbox, isovalmin, cubes, faces) : 0
) [trianglepoints, faces];
/// isosurface_cubes() - private function, called by isosurface()
/// This implements a marching cube 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, field, 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 is vector containing the 6 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.
function _isosurface_cubes(voxsize, bbox, fieldarray, fieldfunc, additional, isovalmin, isovalmax, close_clip=true) = let(
// get field intensities
fields = is_def(fieldarray)
? fieldarray
: let(v = bbox[0], hv = 0.5*voxsize, b1 = bbox[1]+[hv,hv,hv]) [
for(x=[v[0]:voxsize:b1[0]]) [
for(y=[v[1]:voxsize:b1[1]]) [
for(z=[v[2]:voxsize:b1[2]])
additional==undef
? fieldfunc([x,y,z])
: fieldfunc([x,y,z], additional)
]
]
],
nx = len(fields)-2,
ny = len(fields[0])-2,
nz = len(fields[0][0])-2,
v0 = bbox[0]
) [
for(i=[0:nx]) let(x=v0[0]+voxsize*i)
for(j=[0:ny]) let(y=v0[1]+voxsize*j)
for(k=[0:nz]) let(z=v0[2]+voxsize*k)
let(i1=i+1, j1=j+1, k1=k+1,
cf = [
fields[i][j][k],
fields[i][j][k1],
fields[i][j1][k],
fields[i][j1][k1],
fields[i1][j][k],
fields[i1][j][k1],
fields[i1][j1][k],
fields[i1][j1][k1]
],
mincf = min(cf),
maxcf = max(cf),
cubecoord = [x,y,z],
bfaces = close_clip ? _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 = sum([for(b=bf) isovalmin<=cf[b] && cf[b]<=isovalmax ? 1 : 0])
) sumcond == len(bf),
cubeindex_isomin = cubefound_isomin ? _cubeindex(cf, isovalmin) : 0,
cubeindex_isomax = cubefound_isomax ? _cubeindex(cf, isovalmax) : 0
) if(cubefound_isomin || cubefound_isomax || cubefound_outer) [
cubecoord,
cubeindex_isomin, cubeindex_isomax,
cf, bfaces
]
];
/// _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);
/// _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, cubesize, isovalmin, isovalmax, tritablemin, tritablemax) = [
for(cl=cubelist) let(
v = cl[0],
cbidxmin = cl[1],
cbidxmax = cl[2],
f = cl[3],
bbfaces = cl[4],
vcube = [
v, v+[0,0,cubesize], v+[0,cubesize,0], v+[0,cubesize,cubesize],
v+[cubesize,0,0], v+[cubesize,0,cubesize],
v+[cubesize,cubesize,0], v+[cubesize,cubesize,cubesize]
],
epathmin = tritablemin[cbidxmin],
epathmax = tritablemax[cbidxmax],
lenmin = len(epathmin),
lenmax = len(epathmax),
outfacevertices = flatten([
for(bf = bbfaces)
_bbfacevertices(vcube, f, bf, isovalmax, isovalmin)
]),
n_outer = len(outfacevertices)
)
// bunch of repeated code here in an attempt to gain some speed to avoid function calls and calls to flatten().
// Where the face of the bounding box clips a voxel, those are done in separate if() blocks and require require a concat(), but the majority of voxels can have triangles generated directly. If there is no clipping, the list of trianges is generated all at once.
if(lenmin>0 && lenmax>0) let(
// both min and max surfaces intersect a voxel clipped by bounding box
list = concat(
// min surface
[ for(ei=epathmin) let(
edge = _MCEdgeVertexIndices[ei],
vi0 = edge[0],
vi1 = edge[1],
denom = f[vi1] - f[vi0],
u = abs(denom)<0.0001 ? 0.5 : (isovalmin-f[vi0]) / denom
) vcube[vi0] + u*(vcube[vi1]-vcube[vi0]) ],
// max surface
[ for(ei=epathmax) let(
edge = _MCEdgeVertexIndices[ei],
vi0 = edge[0],
vi1 = edge[1],
denom = f[vi1] - f[vi0],
u = abs(denom)<0.0001 ? 0.5 : (isovalmax-f[vi0]) / denom
) vcube[vi0] + u*(vcube[vi1]-vcube[vi0]) ], outfacevertices)
) for(ls = list) ls
else if(n_outer>0 && lenmin>0) let(
// only min surface intersects a voxel clipped by bounding box
list = concat(
[ for(ei=epathmin) let(
edge = _MCEdgeVertexIndices[ei],
vi0 = edge[0],
vi1 = edge[1],