-
Notifications
You must be signed in to change notification settings - Fork 41
/
gen-nets.scad
2170 lines (1904 loc) · 65.8 KB
/
gen-nets.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
/*
A script to implement the Conway operations on Polyhedra.
By Kit Wallace [email protected]
with thanks to George Hart whose javascript version http://www.georgehart.com/virtual-polyhedra/conway_notation.html was the inspiration for this work.
Code licensed under the Creative Commons - Attribution - Share Alike license.
The project is being documented in my blog
http://kitwallace.tumblr.com/tagged/conway
Done :
poly object constructor
poly accessors and renderers (as 3d object, description, full print, face and vertex analyses)
primitives T(),C(),O(),D(),I(),Y(n),P(n),A(n)
all centered and normalized to a mid-scribed radius of 1
conway/hart operators
kis(obj,ratio, nsides)
ambo(obj)
meta(obj,ratio)
ortho(obj,ratio)
trunc(obj,ratio, nsides)
dual(obj)
snub(obj,height)
expand(obj,height), rexpand () to apply recursively
reflect(obj)
gyro(obj)
propellor(obj,ratio)
join(obj) == dual(ambo(obj)
bevel(obj) == trunc(ambo(obj))
chamfer(obj,ratio)
whirl(obj,ratio)
tt(obj) convert triangular faces into 4 triangles
additional operators
transform(obj,matrix) matrix transformation of vertices
insetkis(obj,ratio,height,fn)
modulate(obj) with global spherical function fmod()
shell(obj,outer_inset_ratio,inner_inset_ratio,
,outer_inset,inner_inset,height,min_edge_length)
place(obj) on largest face -use before shell
crop(obj,minz,maxz) - then render with wire frame
orientation, centering and resizing
p_inscribed_resize_points() - resize to a given average face centre
p_midscribed_resize_points() - resize to a given average edge centre
p_circumscribed_resize_points() - resize to a given average vertex
orient(obj) - ensure all faces have lhs order (only convex )
needed for some imported solids eg Georges solids and Johnson
and occasionally for David's
canonicalization
plane(obj,itr) - planarization using reciprocals of centres
canon(obj,itr) - canonicalization using edge tangents
to do
canon still fails if face is extreme - use plane first
last updated 22 March 2015 10:00
requires version of OpenSCAD with concat, list comprehension and let()
*/
// seed polyhedra
function T()=
p_resize(poly(name= "T",
vertices= [[1,1,1],[1,-1,-1],[-1,1,-1],[-1,-1,1]],
faces= [[2,1,0],[3,2,0],[1,3,0],[2,3,1]]
));
function C() =
p_resize(poly(name= "C",
vertices= [
[ 0.5, 0.5, 0.5],
[ 0.5, 0.5, -0.5],
[ 0.5, -0.5, 0.5],
[ 0.5, -0.5, -0.5],
[-0.5, 0.5, 0.5],
[-0.5, 0.5, -0.5],
[-0.5, -0.5, 0.5],
[-0.5, -0.5, -0.5]],
faces=
[
[ 4 , 5, 1, 0],
[ 2 , 6, 4, 0],
[ 1 , 3, 2, 0],
[ 6 , 2, 3, 7],
[ 5 , 4, 6, 7],
[ 3 , 1, 5, 7]]
));
function O() =
let (C0 = 0.7071067811865475244008443621048)
p_resize(poly(name="O",
vertices=[
[0.0, 0.0, C0],
[0.0, 0.0, -C0],
[ C0, 0.0, 0.0],
[-C0, 0.0, 0.0],
[0.0, C0, 0.0],
[0.0, -C0, 0.0]],
faces= [
[ 4 , 2, 0],
[ 3 , 4, 0],
[ 5 , 3, 0],
[ 2 , 5, 0],
[ 5 , 2, 1],
[ 3 , 5, 1],
[ 4 , 3, 1],
[ 2 , 4, 1]]
));
function D() =
let (C0 = 0.809016994374947424102293417183)
let (C1 =1.30901699437494742410229341718)
p_resize(poly(name="D",
vertices=[
[ 0.0, 0.5, C1],
[ 0.0, 0.5, -C1],
[ 0.0, -0.5, C1],
[ 0.0, -0.5, -C1],
[ C1, 0.0, 0.5],
[ C1, 0.0, -0.5],
[ -C1, 0.0, 0.5],
[ -C1, 0.0, -0.5],
[ 0.5, C1, 0.0],
[ 0.5, -C1, 0.0],
[-0.5, C1, 0.0],
[-0.5, -C1, 0.0],
[ C0, C0, C0],
[ C0, C0, -C0],
[ C0, -C0, C0],
[ C0, -C0, -C0],
[ -C0, C0, C0],
[ -C0, C0, -C0],
[ -C0, -C0, C0],
[ -C0, -C0, -C0]],
faces=[
[ 12 , 4, 14, 2, 0],
[ 16 , 10, 8, 12, 0],
[ 2 , 18, 6, 16, 0],
[ 17 , 10, 16, 6, 7],
[ 19 , 3, 1, 17, 7],
[ 6 , 18, 11, 19, 7],
[ 15 , 3, 19, 11, 9],
[ 14 , 4, 5, 15, 9],
[ 11 , 18, 2, 14, 9],
[ 8 , 10, 17, 1, 13],
[ 5 , 4, 12, 8, 13],
[ 1 , 3, 15, 5, 13]]
));
function I() =
let(C0 = 0.809016994374947424102293417183)
p_resize(poly(name= "I",
vertices= [
[ 0.5, 0.0, C0],
[ 0.5, 0.0, -C0],
[-0.5, 0.0, C0],
[-0.5, 0.0, -C0],
[ C0, 0.5, 0.0],
[ C0, -0.5, 0.0],
[ -C0, 0.5, 0.0],
[ -C0, -0.5, 0.0],
[ 0.0, C0, 0.5],
[ 0.0, C0, -0.5],
[ 0.0, -C0, 0.5],
[ 0.0, -C0, -0.5]],
faces=[
[ 10 , 2, 0],
[ 5 , 10, 0],
[ 4 , 5, 0],
[ 8 , 4, 0],
[ 2 , 8, 0],
[ 6 , 8, 2],
[ 7 , 6, 2],
[ 10 , 7, 2],
[ 11 , 7, 10],
[ 5 , 11, 10],
[ 1 , 11, 5],
[ 4 , 1, 5],
[ 9 , 1, 4],
[ 8 , 9, 4],
[ 6 , 9, 8],
[ 3 , 9, 6],
[ 7 , 3, 6],
[ 11 , 3, 7],
[ 1 , 3, 11],
[ 9 , 3, 1]]
));
function Y(n,h=1) =
// pyramids
p_resize(poly(name= str("Y",n) ,
vertices=
concat(
[for (i=[0:n-1])
[cos(i*360/n),sin(i*360/n),0]
],
[[0,0,h]]
),
faces=concat(
[for (i=[0:n-1])
[(i+1)%n,i,n]
],
[[for (i=[0:n-1]) i]]
)
));
function P(n,h=1) =
// prisms
p_resize(poly(name=str("P",n) ,
vertices=concat(
[for (i=[0:n-1])
[cos(i*360/n),sin(i*360/n),-h/2]
],
[for (i=[0:n-1])
[cos(i*360/n),sin(i*360/n),h/2]
]
),
faces=concat(
[for (i=[0:n-1])
[(i+1)%n,i,i+n,(i+1)%n + n]
],
[[for (i=[0:n-1]) i]],
[[for (i=[n-1:-1:0]) i+n]]
)
));
function A(n,h=1) =
// antiprisms
p_resize(poly(name=str("A",n) ,
vertices=concat(
[for (i=[0:n-1])
[cos(i*360/n),sin(i*360/n),-h/2]
],
[for (i=[0:n-1])
[cos((i+1/2)*360/n),sin((i+1/2)*360/n),h/2]
]
),
faces=concat(
[for (i=[0:n-1])
[(i+1)%n,i,i+n]
],
[for (i=[0:n-1])
[(i+1)%n,i+n,(i+1)%n + n]
],
[[for (i=[0:n-1]) i]],
[[for (i=[n-1:-1:0]) i+n]]
)
));
// basic list comprehension functions
function depth(a) =
len(a)== undef
? 0
: 1+depth(a[0]);
function flatten(l) = [ for (a = l) for (b = a) b ] ;
function dflatten(l,d=2) =
// hack to flattened mixed list and list of lists
flatten([for (a = l) depth(a) > d ? dflatten(a, d) : [a]]);
function reverse(l) =
[for (i=[1:len(l)]) l[len(l)-i]];
function shift_reverse(l,shift=0) =
[for (i=[0:len(l)-1]) l[(len(l)-1-i + shift)%len(l)]];
// functions for creating the matrices for transforming a single point
function m_translate(v) = [ [1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[v.x, v.y, v.z, 1 ] ];
function m_scale(v) = [ [v.x, 0, 0, 0],
[0, v.y, 0, 0],
[0, 0, v.z, 0],
[0, 0, 0, 1 ] ];
function m_rotate(v) = [ [1, 0, 0, 0],
[0, cos(v.x), sin(v.x), 0],
[0, -sin(v.x), cos(v.x), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.y), 0, -sin(v.y), 0],
[0, 1, 0, 0],
[ sin(v.y), 0, cos(v.y), 0],
[0, 0, 0, 1] ]
* [ [ cos(v.z), sin(v.z), 0, 0],
[-sin(v.z), cos(v.z), 0, 0],
[ 0, 0, 1, 0],
[ 0, 0, 0, 1] ];
function vec3(v) = [v.x, v.y, v.z];
function m_transform(v, m) = vec3([v.x, v.y, v.z, 1] * m);
function m_rotate_to(normal) =
m_rotate([0, atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0])
* m_rotate([0, 0, atan2(normal.y, normal.x)]);
function m_rotate_from(normal) =
m_rotate([0, 0, -atan2(normal.y, normal.x)])
* m_rotate([0, -atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0]);
function m_to(centre,normal) =
m_rotate([0, atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0])
* m_rotate([0, 0, atan2(normal.y, normal.x)])
* m_translate(centre);
function m_from(centre,normal) =
m_translate(-centre)
* m_rotate([0, 0, -atan2(normal.y, normal.x)])
* m_rotate([0, -atan2(sqrt(pow(normal.x, 2) + pow(normal.y, 2)), normal.z), 0]);
function m_rotate_about_line(a,v1,v2) =
m_from(v1,v2-v1)*m_rotate([0,0,a])*m_to(v1,v2-v1);
// modules to orient objects for rendering
module orient_to(centre, normal) {
translate(centre)
rotate([0, 0, atan2(normal.y, normal.x)]) //rotation
rotate([0, atan2(sqrt(pow(normal.x, 2)+pow(normal.y, 2)),normal.z), 0])
children();
}
// vector functions
function unitv(v)= v/ norm(v);
function signx (x) =
x==0 ? 1 : sign(x);
function angle_between(u, v, normal) =
// protection against inaccurate computation
let (x= unitv(u) * unitv(v))
let (y = x <= -1 ? -1 :x >= 1 ? 1 : x)
let (a = acos(y))
normal == undef
? a
: signx(normal * cross(u,v)) * a;
function vadd(points,v,i=0) =
i < len(points)
? concat([points[i] + v], vadd(points,v,i+1))
: [];
function vsum(points,i=0) =
i < len(points)
? (points[i] + vsum(points,i+1))
: [0,0,0];
function norm2(v) = v.x*v.x+ v.y*v.y + v.z*v.z;
function reciprocal(v) = v/norm2(v);
function ssum(list,i=0) =
i < len(list)
? (list[i] + ssum(list,i+1))
: 0;
function vcontains(val,list) =
search([val],list)[0] != [];
function index_of(key, list) =
search([key],list)[0] ;
function value_of(key, list) =
list[search([key],list)[0]][1] ;
// dictionary shorthand assuming present
function find(key,array) = array[search([key],array)[0]];
function count(val, list) = // number of occurances of val in list
ssum([for(v= list) v== val ? 1 :0]);
function distinct(list,dlist=[],i=0) = // return only distinct items of d
i==len(list)
? dlist
: search(list[i],dlist) != []
? distinct(list,dlist,i+1)
: distinct(list,concat(dlist,list[i]),i+1)
;
// points functions
function as_points(indexes,points) =
[for (i=[0:len(indexes)-1])
points[indexes[i]]
];
function centre(points) =
vsum(points) / len(points);
function vnorm(points) =
[for (p=points) norm(p)];
function average_norm(points) =
ssum(vnorm(points)) / len(points);
function transform_points(points, matrix) =
[for (p=points) m_transform(p, matrix) ] ;
// vertex functions
function vertex_faces(v,faces) = // return the faces containing v
[ for (f=faces) if(v!=[] && search(v,f)) f ];
function ordered_vertex_faces(v,vfaces,cface=[],k=0) =
k==0
? let (nface=vfaces[0])
concat([nface],ordered_vertex_faces(v,vfaces,nface,k+1))
: k < len(vfaces)
? let(i = index_of(v,cface))
let(j= (i-1+len(cface))%len(cface))
let(edge=[v,cface[j]])
let(nface=face_with_edge(edge,vfaces))
concat([nface],ordered_vertex_faces(v,vfaces,nface,k+1 ))
: []
;
function ordered_vertex_edges(v,vfaces,face,k=0) =
let(cface=(k==0)? vfaces[0] : face)
k < len(vfaces)
? let(i = index_of(v,cface))
let(j= (i-1+len(cface))%len(cface))
let(edge=[v,cface[j]])
let(nface=face_with_edge(edge,vfaces))
concat([edge],ordered_vertex_edges(v,vfaces,nface,k+1 ))
: []
;
// edge functions
function distinct_edge(e) =
e[0]< e[1]
? e
: reverse(e);
function ordered_face_edges(f) =
// edges are ordered anticlockwise
[for (j=[0:len(f)-1])
[f[j],f[(j+1)%len(f)]]
];
function all_edges(faces) =
[for (f = faces)
for (j=[0:len(f)-1])
let(p=f[j],q=f[(j+1)%len(f)])
[p,q]
];
function distinct_face_edges(f) =
[for (j=[0:len(f)-1])
let(p=f[j],q=f[(j+1)%len(f)])
distinct_edge([p,q])
];
function distinct_edges(faces) =
[for (f = faces)
for (j=[0:len(f)-1])
let(p=f[j],q=f[(j+1)%len(f)])
if(p<q) [p,q] // no duplicates
];
function check_euler(obj) =
// E = V + F -2
len(p_vertices(obj)) + len(p_faces(obj)) - 2
== len(distinct_edges(obj[2]));
function edge_length(edge,points) =
let (points = as_points(edge,points))
norm(points[0]-points[1]) ;
function edge_lengths(edges,points) =
[ for (edge = edges)
edge_length(edge,points)
];
function tangent(v1,v2) =
let (d=v2-v1)
v1 - v2 * (d*v1)/norm2(d);
function edge_distance(v1,v2) = sqrt(norm2(tangent(v1,v2)));
function face_with_edge(edge,faces) =
flatten(
[for (f = faces)
if (vcontains(edge,ordered_face_edges(f)))
f
]);
function dihedral_angle(edge,faces,points)=
let(f0 = face_with_edge(edge,faces),
f1 = face_with_edge(reverse(edge),faces)
)
let(angle =
angle_between(
normal(as_points(f0,points)),
normal(as_points(f1,points))))
180-angle;
function dihedral_angle_faces(f0,f1,faces,points)=
let(angle =
angle_between(
normal(as_points(faces[f0],points)),
normal(as_points(faces[f1],points))))
180-angle;
//face functions
function selected_face(face,fn) =
fn == [] || search(len(face),fn) != [] ;
function orthogonal(v0,v1,v2) = cross(v1-v0,v2-v1);
function normal(face) =
let (n=orthogonal(face[0],face[1],face[2]))
- n / norm(n);
function triangle(a,b) = norm(cross(a,b))/2;
function face_area(face) =
ssum([for (i=[0:len(face)-1])
triangle(face[i], face[(i+1)%len(face)]) ]);
function face_areas(obj) =
[for (f=p_faces(obj))
let(face_points = as_points(f,p_vertices(obj)))
let(centre=centre(face_points))
face_area(vadd(face_points,-centre))
];
function face_areas_index(obj) =
[for (face=p_faces(obj))
let(face_points = as_points(face,p_vertices(obj)))
let(centre=centre(face_points))
[face,face_area(vadd(face_points,-centre))]
];
function max_area(areas, max=[undef,0], i=0) =
i <len(areas)
? areas[i][1] > max[1]
? max_area(areas,areas[i],i+1)
: max_area(areas,max,i+1)
: max[0];
function average_face_normal(fp) =
let(fl=len(fp))
let(normals=
[for(i=[0:fl-1])
orthogonal(fp[i],fp[(i+1)%fl],fp[(i+2)%fl])
]
)
vsum(normals)/len(normals);
function average_normal(fp) =
let(fl=len(fp))
let(unitns=
[for(i=[0:fl-1])
let(n=orthogonal(fp[i],fp[(i+1)%fl],fp[(i+2)%fl]))
let(normn=norm(n))
normn==0 ? [] : n/normn
]
)
vsum(unitns)/len(unitns);
function average_edge_distance(fp) =
let(fl=len(fp))
ssum( [for (i=[0:fl-1])
edge_distance(fp[i],fp[(i+1)%fl])
])/ fl;
function face_sides(faces) =
[for (f=faces) len(f)];
function face_coplanarity(face) =
norm(cross(cross(face[1]-face[0],face[2]-face[1]),
cross(face[2]-face[1],face[3]-face[2])
));
function face_edges(face,points) =
[for (edge=ordered_face_edges(face))
edge_length(edge,points)
];
function min_edge_length(face,points) =
min(face_edges(face,points));
function face_irregularity(face,points) =
let (lengths=face_edges(face,points))
max(lengths)/ min(lengths);
function face_analysis(faces) =
let (edge_counts=face_sides(faces))
[for (sides=distinct(edge_counts))
[sides,count(sides,edge_counts)]
];
function vertex_face_list(vertices,faces) =
[for (i=[0:len(vertices)-1])
let (vf= vertex_faces(i,faces))
len(vf)];
function vertex_analysis(vertices,faces) =
let (face_counts=vertex_face_list(vertices,faces))
[for (vo = distinct(face_counts))
[vo,count(vo,face_counts)]
];
// ensure that all faces have a lhs orientation
function cosine_between(u, v) =(u * v) / (norm(u) * norm(v));
function lhs_faces(faces,vertices) =
[for (face = faces)
let(points = as_points(face,vertices))
cosine_between(normal(points), centre(points)) < 0
? reverse(face) : face
];
// poly functions
// constructor
function poly(name,vertices,faces,debug=[],partial=false) =
[name,vertices,faces,debug,partial];
// accessors
function p_name(obj) = obj[0];
function p_vertices(obj) = obj[1];
function p_faces(obj) = obj[2];
function p_debug(obj)=obj[3];
function p_partial(obj)=obj[4];
function p_edges(obj) =
p_partial(obj)
? all_edges(p_faces(obj))
: distinct_edges(p_faces(obj));
function p_description(obj) =
str(p_name(obj),
", ",str(len(p_vertices(obj)), " Vertices " ),
vertex_analysis(p_vertices(obj), p_faces(obj)),
", ",str(len(p_faces(obj))," Faces "),
face_analysis(p_faces(obj)),
" ",str(len(p_non_planar_faces(obj))," not planar"),
", ",str(len(p_edges(obj))," Edges ")
);
function p_faces_as_points(obj) =
[for (f = p_faces(obj))
as_points(f,p_vertices(obj))
];
function p_non_planar_faces(obj,tolerance=0.001) =
[for (face = p_faces(obj))
if (len(face) >3)
let (points = as_points(face,p_vertices(obj)))
let (error=face_coplanarity(points))
if (error>tolerance)
[tolerance,face]
];
function p_dihedral_angles(obj) =
[for (edge=p_edges(obj))
dihedral_angle(edge, p_faces(obj),p_vertices(obj))
];
function p_irregular_faces(obj,tolerance=0.01) =
[for (face = p_faces(obj))
let(ir=face_irregularity(face,p_vertices(obj)))
if(abs(ir-1)>tolerance)
[ir,face]
];
function p_vertices_to_faces(obj)=
[for (vi = [0:len(p_vertices(obj))-1]) // each old vertex creates a new face, with
let (vf=vertex_faces(vi,p_faces(obj))) // vertex faces in left-hand order
[for (of = ordered_vertex_faces(vi,vf))
index_of(of,p_faces(obj))
]
];
module show_points(points,r=0.1) {
for (point=points)
if (point != []) // ignore null points
translate(point) sphere(r);
};
module show_edge(edge, r) {
p0 = edge[0];
p1 = edge[1];
v = p1 -p0 ;
orient_to(p0,v)
cylinder(r1=r,r2=r, h=norm(v));
};
module show_edges(edges,points,r=0.1) {
for (edge = edges)
show_edge(as_points(edge, points), r);
};
module p_render(obj,show_vertices=false,show_edges=false,show_faces=true, rv=0.04, re=0.02) {
if(show_faces)
polyhedron(p_vertices(obj),p_faces(obj),convexity=10);
if(show_vertices)
show_points(p_vertices(obj),rv);
if(show_edges)
show_edges(p_edges(obj),p_vertices(obj),re);
};
module p_describe(obj){
echo(p_description(obj));
}
module p_print(obj) {
p_describe(obj);
echo(" Vertices " ,p_vertices(obj));
echo(" Faces ", p_faces(obj));
edges=p_edges(obj);
echo(str(len(edges)," Edges ",edges));
non_planar=p_non_planar_faces(obj);
echo(str(len(non_planar)," faces are not planar", non_planar));
debug=p_debug(obj);
if(debug!=[]) echo("Debug",debug);
};
// centering and resizing
function centre_points(points) =
vadd(points, - centre(points));
function p_inscribed_resize(obj,radius=1) =
let(pv=centre_points(p_vertices(obj)))
let (centres= [for (f=p_faces(obj))
norm(centre(as_points(f,pv)))
])
let (average = ssum(centres) / len(centres))
poly(name=p_name(obj),
vertices = pv * radius /average,
faces=p_faces(obj),
debug=centres
);
function p_midscribed_resize(obj,radius=1) =
let(pv=centre_points(p_vertices(obj)))
let(centres= [for (e=p_edges(obj))
let (ep = as_points(e,pv))
norm((ep[0]+ep[1])/2)
])
let (average = ssum(centres) / len(centres))
poly(name=p_name(obj),
vertices = pv * radius /average,
faces=p_faces(obj),
debug=centres
);
function p_circumscribed_resize(obj,radius=1) =
let(pv=centre_points(p_vertices(obj)))
let(average=average_norm(pv))
poly(name=p_name(obj),
vertices=pv * radius /average,
faces=p_faces(obj),
debug=average
);
function p_resize(obj,radius=1) =
p_circumscribed_resize(obj,radius);
// canonicalization
function rdual(obj) =
let(np=p_vertices(obj))
poly(name=p_name(obj),
vertices =
[ for (f=p_faces(obj))
let (c=centre(as_points(f,np)))
reciprocal(c)
]
,
faces= p_vertices_to_faces(obj)
);
function plane(obj,n=5) =
n > 0
? plane(rdual(rdual(obj)),n-1)
: p_resize(poly(name=str("P",p_name(obj)),
vertices=p_vertices(obj),
faces=p_faces(obj)
));
function ndual(obj) =
let(np=p_vertices(obj))
poly(name=p_name(obj),
vertices =
[ for (f=p_faces(obj))
let (fp=as_points(f,np),
c=centre(fp),
n=average_normal(fp),
cdotn = c*n,
ed=average_edge_distance(fp))
reciprocal(n*cdotn) * (1+ed)/2
]
,
faces= p_vertices_to_faces(obj)
);
function canon(obj,n=5) =
n > 0
? canon(ndual(ndual(obj)),n-1)
: p_resize(poly(name=str("K",p_name(obj)),
vertices=p_vertices(obj),
faces=p_faces(obj)
));
function dual(obj) =
poly(name=str("d",p_name(obj)),
vertices =
[for (f = p_faces(obj))
let(fp=as_points(f,p_vertices(obj)))
centre(fp)
],
faces= p_vertices_to_faces(obj)
)
; // end dual
// Conway operators
/* where necessary, new vertices are first created and stored in an associative array, keyed by whatever is appropriate to identify the new vertex - this could be an old vertex id, a face, an edge or something more complicated. This array is then used to create an associative array of key and vertex ids for use in face construction, and to generate the new vertices themselves.
*/
function vertex_ids(entries,offset=0,i=0) =
// to get position of new vertices
len(entries) > 0
?[for (i=[0:len(entries)-1])
[entries[i][0],i+offset]
]
:[]
;
function vertex_values(entries)=
[for (e = entries) e[1]];
function vertex(key,entries) = // key is an array
entries[search([key],entries)[0]][1];
// operators
function kis(obj,height=0.1, fn=[]) =
// kis each n-face is divided into n triangles which extend to the face centre
// existimg vertices retained
let(pf=p_faces(obj),
pv=p_vertices(obj))
let(newv= // new centroid vertices
[for (f=pf)
if (selected_face(f,fn))
let(fp=as_points(f,pv))
[f,centre(fp) + normal(fp) * height] // centroid + a bit of normal
])
let(newids=vertex_ids(newv,len(pv)))
let(newf=
flatten(
[for (face=pf)
selected_face(face,fn)
//replace face with triangles
? let(centre=vertex(face,newids))
[for (j=[0:len(face)-1])
let(a=face[j],
b=face[(j+1)%len(face)])
[a,b,centre]
]
: [face] // original face
])
)
poly(name=str("k",p_name(obj)),
vertices= concat(pv, vertex_values(newv)) ,
faces=newf
)
; // end kis
function gyro(obj, ratio=0.3333, height=0.2) =
// retain original vertices, add face centres and directed edge points
// each N-face becomes N pentagons
let(pf=p_faces(obj),
pv=p_vertices(obj),
pe=p_edges(obj))
let(newv=
concat(
[for (face=pf) // centres
let(fp=as_points(face,pv))
[face,centre(fp) + normal(fp) * height] // centroid + a bit of normal
] ,
flatten( // 2 points per edge
[for (edge = pe)
let (ep = as_points(edge,pv))
[ [ edge, ep[0]+ ratio*(ep[1]-ep[0])],
[ reverse(edge), ep[1]+ ratio*(ep[0]-ep[1])]
]
]
)
))
let(newids=vertex_ids(newv,len(pv)))
let(newf=
flatten( // new faces are pentagons
[for (face=pf)
[for (j=[0:len(face)-1])
let (a=face[j],
b=face[(j+1)%len(face)],
z=face[(j-1+len(face))%len(face)],
eab=vertex([a,b],newids),
eza=vertex([z,a],newids),
eaz=vertex([a,z],newids),
centre=vertex(face,newids))
[a,eab,centre,eza,eaz]
]
]
))
poly(name=str("g",p_name(obj)),
vertices= concat(pv, vertex_values(newv)),
faces= newf
)
; // end gyro
function meta(obj,height=0.1) =
// each face is replaced with 2n triangles based on edge midpoint and centre
let(pe=p_edges(obj),
pf=p_faces(obj),
pv=p_vertices(obj))
let (newv =concat(
[for (face = pf) // new centre vertices
let (fp=as_points(face,pv))
[face,centre(fp) + normal(fp)*height]
],
[for (edge=pe)
let (ep = as_points(edge,pv))
[edge,(ep[0]+ep[1])/2]
]))
let(newids=vertex_ids(newv,len(pv)))
let(newf =
flatten(
[for (face=pf)
let(centre=vertex(face,newids))
flatten(
[for (j=[0:len(face)-1]) // replace face with 2n triangle
let (a=face[j],
b=face[(j+1)%len(face)],
mid=vertex(distinct_edge([a,b]),newids))
[ [ mid, centre, a],
[b,centre, mid] ]
] )
])
)
poly(name=str("m",p_name(obj)),
vertices= concat(pv,vertex_values(newv)),
faces=newf
)
; //end meta
function pyra(obj,height=0.1) =
// very like meta but different triangles
let(pe=p_edges(obj),
pf=p_faces(obj),
pv=p_vertices(obj))
let(newv=concat(
[for (face = pf) // new centre vertices
let(fp=as_points(face,pv))
[face,centre(fp) + normal(fp)*height]
],
[for (edge=pe) // new midpoints
let (ep = as_points(edge,pv))
[edge,(ep[0]+ep[1])/2]
]))
let(newids=vertex_ids(newv,len(pv)))
let(newf=flatten(
[ for (face=pf)
let(centre=vertex(face,newids))
flatten( [for (j=[0:len(face)-1])
let(a=face[j],
b=face[(j+1)%len(face)],
z=face[(j-1+len(face))%len(face)],
midab = vertex(distinct_edge([a,b]),newids),
midza = vertex(distinct_edge([z,a]),newids))
[[midza,a,midab], [midza,midab,centre]]
])
] ))
poly(name=str("y",p_name(obj)),
vertices= concat(pv, vertex_values(newv)),
faces=newf
)
; // end pyra
function ortho(obj,height=0.2) =
// very like meta but divided into quadrilaterals
let (pe=p_edges(obj),