-
Notifications
You must be signed in to change notification settings - Fork 115
/
distributors.scad
2054 lines (1946 loc) · 89.4 KB
/
distributors.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: distributors.scad
// Functions and modules to distribute children or copies of children onto
// a line, a grid, or an arbitrary path. The $idx mechanism means that
// the "copies" of children can vary. Also includes shortcuts for mirroring.
// Includes:
// include <BOSL2/std.scad>
// FileGroup: Basic Modeling
// FileSummary: Copy or distribute objects onto a line, grid, or path. Mirror shortcuts.
// FileFootnotes: STD=Included in std.scad
//////////////////////////////////////////////////////////////////////
// Section: Adaptive Children Using `$` Variables
// The distributor methods create multiple copies of their children and place them in various ways. While many models
// require multiple identical copies of an object, this framework is more powerful than
// might be immediately obvious because of `$` variables. The distributors set `$` variables that the children can use to change their
// behavior from one child to the next within a single distributor invocation. This means the copies need not be identical.
// The {{xcopies()}} module sets `$idx` to the index number of the copy, and in the examples below we use `$idx`, but the various
// distributors offer a variety of `$` variables that you can use in your children. Check the "Side Effects" section for each module
// to learn what variables that module provides.
// .
// Two gotchas may lead to models that don't behave as expected. While `if` statements work to control modules, you cannot
// use them to make variable assignments in your child object. If you write a statement like
// ```
// if (condition) { c="red";}
// else {c="green";}
// ```
// then the `c` variable is set only in the scope of the `if` and `else` clauses and is not available later on when you actually
// try to use it. Instead you must use the ternary operator and write:
// ```
// c = condition ? "red" : "green";
// ```
// The second complication is
// that in OpenSCAD version 2021.01 and earlier, assignments in children were executed before their parent. This means
// that `$` variables like `$idx` are not available in assignments because the parent hasn't run to set them, so if you use them
// you will get a warning about an unknown variable.
// Two workarounds exist, neither of which are needed in newer versions of OpenSCAD. The workarounds solve the problem because
// **modules** execute after their parent, so the `$` variables **are** available in modules. You can put your assignments
// in a `let()` module, or you can wrap your child in a `union()`. Both methods appear below.
// Figure(2D,NoScales): This example shows how we can use `$idx` to produce **different** geometry at each index.
// xcopies(n=10, spacing=10)
// text(str($idx));
// Continues:
// ```
// xcopies(n=10, spacing=10)
// text(str($idx));
// ```
// Figure(2D,NoScales): Here the children are sometimes squares and sometimes circles as determined by the conditional `if` module. This use of `if` is OK because no variables are assigned.
// xcopies(n=4, spacing=10)
// if($idx%2==0) circle(r=3,$fn=16);
// else rect(6);
// Continues:
// ```
// xcopies(n=4, spacing=10)
// if($idx%2==0) circle(r=3,$fn=16);
// else rect(6);
// ```
// Figure(2D,NoScales): Suppose we would like to color odd and even index copies differently. In this example we compute the color for a given child from `$idx` using the ternary operator. The `let()` module is a module that sets variables and makes them available to its children. Note that multiple assignments in `let()` are separated by commas, not semicolons.
// xcopies(n=6, spacing=10){
// let(c = $idx % 2 == 0 ? "red" : "green")
// color(c) rect(6);
// }
// Continues:
// ```
// xcopies(n=6, spacing=10){
// let(c = $idx % 2 == 0 ? "red" : "green")
// color(c) rect(6);
// }
// ```
// Figure(2D,NoScales): This example shows how you can change the position of children adaptively. If you want to avoid repeating your code for each case, this requires storing a transformation matrix in a variable and then applying it using `multmatrix()`. We wrap our code in `union()` to ensure that it works in OpenSCAD 2021.01.
// xcopies(n=5,spacing=10)
// union()
// {
// shiftback = $idx%2==0 ? back(10) : IDENT;
// spin = zrot(180*$idx/4);
// multmatrix(shiftback*spin) stroke([[-4,0],[4,0]],endcap2="arrow2",width=3/4, color="red");
// }
// Continues:
// ```
// xcopies(n=5,spacing=10)
// union()
// {
// shiftback = $idx%2==0 ? back(10) : IDENT;
// spin = zrot(180*$idx/4);
// multmatrix(shiftback*spin) stroke([[-4,0],[4,0]],endcap2="arrow2",width=3/4,color="red");
// }
// ```
//////////////////////////////////////////////////////////////////////
// Section: Translating copies of all the children
//////////////////////////////////////////////////////////////////////
// Function&Module: move_copies()
// Synopsis: Translates copies of all children.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: xcopies(), ycopies(), zcopies(), line_copies(), grid_copies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage:
// move_copies(a) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = move_copies(a, p=);
// Usage: Get Translation Matrices
// mats = move_copies(a);
// Description:
// When called as a module, translates copies of all children to each given translation offset.
// When called as a function, with no `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// a = Array of XYZ offset vectors. Default `[[0,0,0]]`
// ---
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index number of each child being copied.
//
//
// Example:
// #sphere(r=10);
// move_copies([[-25,-25,0], [25,-25,0], [0,0,50], [0,25,0]]) sphere(r=10);
module move_copies(a=[[0,0,0]])
{
req_children($children);
assert(is_list(a));
for ($idx = idx(a)) {
$pos = a[$idx];
assert(is_vector($pos),"move_copies offsets should be a 2d or 3d vector.");
translate($pos) children();
}
}
function move_copies(a=[[0,0,0]],p=_NO_ARG) =
assert(is_list(a))
let(
mats = [
for (pos = a)
assert(is_vector(pos),"move_copies offsets should be a 2d or 3d vector.")
translate(pos)
]
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
// Function&Module: xcopies()
// Synopsis: Places copies of children along the X axis.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: move_copies(), ycopies(), zcopies(), line_copies(), grid_copies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage:
// xcopies(spacing, [n], [sp=]) CHILDREN;
// xcopies(l=, [n=], [sp=]) CHILDREN;
// xcopies(LIST) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = xcopies(spacing, [n], [sp=], p=);
// copies = xcopies(l=, [n=], [sp=], p=);
// copies = xcopies(LIST, p=);
// Usage: Get Translation Matrices
// mats = xcopies(spacing, [n], [sp=]);
// mats = xcopies(l=, [n=], [sp=]);
// mats = xcopies(LIST);
// Description:
// When called as a module, places `n` copies of the children along a line on the X axis.
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// spacing = Given a scalar, specifies a uniform spacing between copies. Given a list of scalars, each one gives a specific position along the line. (Default: 1.0)
// n = Number of copies to place. (Default: 2)
// ---
// l = If given, the length to place copies over.
// sp = If given as a point, copies will be placed on a line to the right of starting position `sp`. If given as a scalar, copies will be placed on a line segment to the right of starting position `[sp,0,0]`. If not given, copies will be placed along a line segment that is centered at [0,0,0].
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index number of each child being copied.
//
// Examples:
// xcopies(20) sphere(3);
// xcopies(20, n=3) sphere(3);
// xcopies(spacing=15, l=50) sphere(3);
// xcopies(n=4, l=30, sp=[0,10,0]) sphere(3);
// Example:
// xcopies(10, n=3) {
// cube(size=[1,3,1],center=true);
// cube(size=[3,1,1],center=true);
// }
// Example:
// xcopies([1,2,3,5,7]) sphere(d=1);
module xcopies(spacing, n, l, sp)
{
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
req_children($children);
dir = RIGHT;
sp = is_finite(sp)? (sp*dir) : sp;
if (is_vector(spacing)) {
translate(default(sp,[0,0,0])) {
for (i = idx(spacing)) {
$idx = i;
$pos = spacing[i]*dir;
translate($pos) children();
}
}
} else {
line_copies(
l=u_mul(l,dir),
spacing=u_mul(spacing,dir),
n=n, p1=sp
) children();
}
}
function xcopies(spacing, n, l, sp, p=_NO_ARG) =
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
let(
dir = RIGHT,
sp = is_finite(sp)? (sp*dir) : sp,
mats = is_vector(spacing)
? let(sp = default(sp,[0,0,0])) [for (n = spacing) translate(sp + n*dir)]
: line_copies(l=u_mul(l,dir), spacing=u_mul(spacing,dir), n=n, p1=sp)
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
// Function&Module: ycopies()
// Synopsis: Places copies of children along the Y axis.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: move_copies(), xcopies(), zcopies(), line_copies(), grid_copies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage:
// ycopies(spacing, [n], [sp=]) CHILDREN;
// ycopies(l=, [n=], [sp=]) CHILDREN;
// ycopies(LIST) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = ycopies(spacing, [n], [sp=], p=);
// copies = ycopies(l=, [n=], [sp=], p=);
// copies = ycopies(LIST, p=);
// Usage: Get Translation Matrices
// mats = ycopies(spacing, [n], [sp=]);
// mats = ycopies(l=, [n=], [sp=]);
// mats = ycopies(LIST);
// Description:
// When called as a module, places `n` copies of the children along a line on the Y axis.
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// spacing = Given a scalar, specifies a uniform spacing between copies. Given a list of scalars, each one gives a specific position along the line. (Default: 1.0)
// n = Number of copies to place on the line. (Default: 2)
// ---
// l = If given, the length to place copies over.
// sp = If given as a point, copies will be place on a line back from starting position `sp`. If given as a scalar, copies will be placed on a line back from starting position `[0,sp,0]`. If not given, copies will be placed along a line that is centered at [0,0,0].
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index number of each child being copied.
//
// Examples:
// ycopies(20) sphere(3);
// ycopies(20, n=3) sphere(3);
// ycopies(spacing=15, l=50) sphere(3);
// ycopies(n=4, l=30, sp=[10,0,0]) sphere(3);
// Example:
// ycopies(10, n=3) {
// cube(size=[1,3,1],center=true);
// cube(size=[3,1,1],center=true);
// }
// Example:
// ycopies([1,2,3,5,7]) sphere(d=1);
module ycopies(spacing, n, l, sp)
{
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
req_children($children);
dir = BACK;
sp = is_finite(sp)? (sp*dir) : sp;
if (is_vector(spacing)) {
translate(default(sp,[0,0,0])) {
for (i = idx(spacing)) {
$idx = i;
$pos = spacing[i]*dir;
translate($pos) children();
}
}
} else {
line_copies(
l=u_mul(l,dir),
spacing=u_mul(spacing,dir),
n=n, p1=sp
) children();
}
}
function ycopies(spacing, n, l, sp, p=_NO_ARG) =
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
let(
dir = BACK,
sp = is_finite(sp)? (sp*dir) : sp,
mats = is_vector(spacing)
? let(sp = default(sp,[0,0,0])) [for (n = spacing) translate(sp + n*dir)]
: line_copies(l=u_mul(l,dir), spacing=u_mul(spacing,dir), n=n, p1=sp)
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
// Function&Module: zcopies()
// Synopsis: Places copies of children along the Z axis.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: move_copies(), xcopies(), ycopies(), line_copies(), grid_copies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage:
// zcopies(spacing, [n], [sp=]) CHILDREN;
// zcopies(l=, [n=], [sp=]) CHILDREN;
// zcopies(LIST) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = zcopies(spacing, [n], [sp=], p=);
// copies = zcopies(l=, [n=], [sp=], p=);
// copies = zcopies(LIST, p=);
// Usage: Get Translation Matrices
// mats = zcopies(spacing, [n], [sp=]);
// mats = zcopies(l=, [n=], [sp=]);
// mats = zcopies(LIST);
// Description:
// When called as a module, places `n` copies of the children along a line on the Z axis.
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// spacing = Given a scalar, specifies a uniform spacing between copies. Given a list of scalars, each one gives a specific position along the line. (Default: 1.0)
// n = Number of copies to place. (Default: 2)
// ---
// l = If given, the length to place copies over.
// sp = If given as a point, copies will be placed on a line up from starting position `sp`. If given as a scalar, copies will be placed on a line up from starting position `[0,0,sp]`. If not given, copies will be placed on a line that is centered at [0,0,0].
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index number of each child being copied.
//
// Examples:
// zcopies(20) sphere(3);
// zcopies(20, n=3) sphere(3);
// zcopies(spacing=15, l=50) sphere(3);
// zcopies(n=4, l=30, sp=[10,0,0]) sphere(3);
// Example:
// zcopies(10, n=3) {
// cube(size=[1,3,1],center=true);
// cube(size=[3,1,1],center=true);
// }
// Example: Cubic sphere packing
// s = 20;
// s2 = s * sin(45);
// zcopies(s2,n=8)
// grid_copies([s2,s2],n=8,stagger=($idx%2)? true : "alt")
// sphere(d=s);
// Example: Hexagonal sphere packing
// s = 20;
// xyr = adj_ang_to_hyp(s/2,30);
// h = hyp_adj_to_opp(s,xyr);
// zcopies(h,n=8)
// back(($idx%2)*xyr*cos(60))
// grid_copies(s,n=[12,7],stagger=($idx%2)? "alt" : true)
// sphere(d=s);
// Example:
// zcopies([1,2,3,5,7]) sphere(d=1);
module zcopies(spacing, n, l, sp)
{
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
req_children($children);
dir = UP;
sp = is_finite(sp)? (sp*dir) : sp;
if (is_vector(spacing)) {
translate(default(sp,[0,0,0])) {
for (i = idx(spacing)) {
$idx = i;
$pos = spacing[i]*dir;
translate($pos) children();
}
}
} else {
line_copies(
l=u_mul(l,dir),
spacing=u_mul(spacing,dir),
n=n, p1=sp
) children();
}
}
function zcopies(spacing, n, l, sp, p=_NO_ARG) =
assert(is_undef(n) || num_defined([l,spacing])==1, "When n is given must give exactly one of spacing or l")
assert(is_def(n) || num_defined([l,spacing])>=1, "When n is not given must give at least one of spacing or l")
let(
dir = UP,
sp = is_finite(sp)? (sp*dir) : sp,
mats = is_vector(spacing)
? let(sp = default(sp,[0,0,0])) [for (n = spacing) translate(sp + n*dir)]
: line_copies(l=u_mul(l,dir), spacing=u_mul(spacing,dir), n=n, p1=sp)
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
// Function&Module: line_copies()
// Synopsis: Places copies of children along an arbitrary line.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: move_copies(), xcopies(), ycopies(), zcopies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage: Place `n` copies at a given spacing along the line
// line_copies(spacing, [n], [p1=]) CHILDREN;
// Usage: Place as many copies as will fit at a given spacing
// line_copies(spacing, [l=], [p1=]) CHILDREN;
// Usage: Place `n` copies along the length of the line
// line_copies([n=], [l=], [p1=]) CHILDREN;
// Usage: Place `n` copies along the line from `p1` to `p2`
// line_copies([n=], [p1=], [p2=]) CHILDREN;
// Usage: Place copies at the given spacing, centered along the line from `p1` to `p2`
// line_copies([spacing], [p1=], [p2=]) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = line_copies([spacing], [n], [p1=], p=);
// copies = line_copies([spacing], [l=], [p1=], p=);
// copies = line_copies([n=], [l=], [p1=], p=);
// copies = line_copies([n=], [p1=], [p2=], p=);
// copies = line_copies([spacing], [p1=], [p2=], p=);
// Usage: Get Translation Matrices
// mats = line_copies([spacing], [n], [p1=]);
// mats = line_copies([spacing], [l=], [p1=]);
// mats = line_copies([n=], [l=], [p1=]);
// mats = line_copies([n=], [p1=], [p2=]);
// mats = line_copies([spacing], [p1=], [p2=]);
// Description:
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
// When called as a module, copies `children()` at one or more evenly spaced positions along a line.
// By default, the line will be centered at the origin, unless the starting point `p1` is given.
// The line will be pointed towards `RIGHT` (X+) unless otherwise given as a vector in `l`,
// `spacing`, or `p1`/`p2`. The psotion of the copies is specified in one of several ways:
// .
// If You Know... | Then Use Something Like...
// -------------------------------- | --------------------------------
// Spacing distance, Count | `line_copies(spacing=10, n=5) ...` or `line_copies(10, n=5) ...`
// Spacing vector, Count | `line_copies(spacing=[10,5], n=5) ...` or `line_copies([10,5], n=5) ...`
// Spacing distance, Line length | `line_copies(spacing=10, l=50) ...` or `line_copies(10, l=50) ...`
// Spacing distance, Line vector | `line_copies(spacing=10, l=[50,30]) ...` or `line_copies(10, l=[50,30]) ...`
// Spacing vector, Line length | `line_copies(spacing=[10,5], l=50) ...` or `line_copies([10,5], l=50) ...`
// Line length, Count | `line_copies(l=50, n=5) ...`
// Line vector, Count | `line_copies(l=[50,40], n=5) ...`
// Line endpoints, Count | `line_copies(p1=[10,10], p2=[60,-10], n=5) ...`
// Line endpoints, Spacing distance | `line_copies(p1=[10,10], p2=[60,-10], spacing=10) ...`
//
// Arguments:
// spacing = Either the scalar spacing distance along the X+ direction, or the vector giving both the direction and spacing distance between each set of copies.
// n = Number of copies to distribute along the line. (Default: 2)
// ---
// l = Either the scalar length of the line, or a vector giving both the direction and length of the line.
// p1 = If given, specifies the starting point of the line.
// p2 = If given with `p1`, specifies the ending point of line, and indirectly calculates the line length.
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index number of each child being copied.
//
// Examples:
// line_copies(10) sphere(d=1.5);
// line_copies(10, n=5) sphere(d=3);
// line_copies([10,5], n=5) sphere(d=3);
// line_copies(spacing=10, n=6) sphere(d=3);
// line_copies(spacing=[10,5], n=6) sphere(d=3);
// line_copies(spacing=10, l=50) sphere(d=3);
// line_copies(spacing=10, l=[50,30]) sphere(d=3);
// line_copies(spacing=[10,5], l=50) sphere(d=3);
// line_copies(l=50, n=4) sphere(d=3);
// line_copies(l=[50,-30], n=4) sphere(d=3);
// Example(FlatSpin,VPD=133):
// line_copies(p1=[0,0,0], p2=[5,5,20], n=6) cuboid([3,2,1]);
// Example(FlatSpin,VPD=133):
// line_copies(p1=[0,0,0], p2=[5,5,20], spacing=6) cuboid([3,2,1]);
// Example: All children are copied to each position
// line_copies(l=20, n=3) {
// cube(size=[1,3,1],center=true);
// cube(size=[3,1,1],center=true);
// }
// Example(2D): The functional form of line_copies() returns a list of transform matrices.
// mats = line_copies([10,5],n=5);
// for (m = mats) multmatrix(m) circle(d=3);
// Example(2D): The functional form of line_copies() returns a list of points if given a point.
// pts = line_copies([10,5],n=5,p=[0,0,0]);
// move_copies(pts) circle(d=3);
module line_of(spacing, n, l, p1, p2) {
deprecate("line_copies");
line_copies(spacing, n, l, p1, p2) children();
}
module line_copies(spacing, n, l, p1, p2)
{
req_children($children);
pts = line_copies(spacing=spacing, n=n, l=l, p1=p1, p2=p2, p=[0,0,0]);
for (i=idx(pts)) {
$idx = i;
$pos = pts[i];
translate($pos) children();
}
}
function line_copies(spacing, n, l, p1, p2, p=_NO_ARG) =
assert(is_undef(spacing) || is_finite(spacing) || is_vector(spacing))
assert(is_undef(n) || is_finite(n))
assert(is_undef(l) || is_finite(l) || is_vector(l))
assert(is_undef(p1) || is_vector(p1))
assert(is_undef(p2) || is_vector(p2))
assert(is_undef(p2) || is_def(p1), "If p2 is given must also give p1")
assert(is_undef(p2) || is_undef(l), "Cannot give both p2 and l")
assert(is_undef(n) || num_defined([l,spacing,p2])==1,"If n is given then must give exactly one of 'l', 'spacing', or the 'p1'/'p2' pair")
assert(is_def(n) || num_defined([l,spacing,p2])>=1,"If n is given then must give at least one of 'l', 'spacing', or the 'p1'/'p2' pair")
let(
ll = is_def(l)? scalar_vec3(l, 0)
: is_def(spacing) && is_def(n)? (n-1) * scalar_vec3(spacing, 0)
: is_def(p1) && is_def(p2)? point3d(p2-p1)
: undef,
cnt = is_def(n)? n
: is_def(spacing) && is_def(ll) ? floor(norm(ll) / norm(scalar_vec3(spacing, 0)) + 1.000001)
: 2,
spc = cnt<=1? [0,0,0]
: is_undef(spacing) && is_def(ll)? ll/(cnt-1)
: is_num(spacing) && is_def(ll)? (ll/(cnt-1))
: scalar_vec3(spacing, 0)
)
assert(!is_undef(cnt), "Need two of `spacing`, 'l', 'n', or `p1`/`p2` arguments in `line_copies()`.")
let( spos = !is_undef(p1)? point3d(p1) : -(cnt-1)/2 * spc )
[for (i=[0:1:cnt-1]) translate(i * spc + spos, p=p)];
// Function&Module: grid_copies()
// Synopsis: Places copies of children in an [X,Y] grid.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Translation, Copiers
// See Also: move_copies(), xcopies(), ycopies(), zcopies(), line_copies(), rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies()
//
// Usage:
// grid_copies(spacing, size=, [stagger=], [scale=], [inside=]) CHILDREN;
// grid_copies(n=, size=, [stagger=], [scale=], [inside=]) CHILDREN;
// grid_copies(spacing, [n], [stagger=], [scale=], [inside=]) CHILDREN;
// grid_copies(n=, inside=, [stagger], [scale]) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = grid_copies(spacing, size=, [stagger=], [scale=], [inside=], p=);
// copies = grid_copies(n=, size=, [stagger=], [scale=], [inside=], p=);
// copies = grid_copies(spacing, [n], [stagger=], [scale=], [inside=], p=);
// copies = grid_copies(n=, inside=, [stagger], [scale], p=);
// Usage: Get Translation Matrices
// mats = grid_copies(spacing, size=, [stagger=], [scale=], [inside=]);
// mats = grid_copies(n=, size=, [stagger=], [scale=], [inside=]);
// mats = grid_copies(spacing, [n], [stagger=], [scale=], [inside=]);
// mats = grid_copies(n=, inside=, [stagger], [scale]);
// Description:
// When called as a module, makes a square or hexagonal grid of copies of children, with an optional masking polygon or region.
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// spacing = Distance between copies in [X,Y] or scalar distance.
// n = How many columns and rows of copies to make. Can be given as `[COLS,ROWS]`, or just as a scalar that specifies both. If staggered, count both staggered and unstaggered columns and rows. Default: 2 (3 if staggered)
// size = The [X,Y] size to spread the copies over.
// ---
// stagger = If true, make a staggered (hexagonal) grid. If false, make square grid. If `"alt"`, makes alternate staggered pattern. Default: false
// inside = If given a list of polygon points, or a region, only creates copies whose center would be inside the polygon or region. Polygon can be concave and/or self crossing.
// nonzero = If inside is set to a polygon with self-crossings then use the nonzero method for deciding if points are in the polygon. Default: false
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$pos` is set to the relative centerpoint of each child copy, and can be used to modify each child individually.
// `$col` is set to the integer column number for each child.
// `$row` is set to the integer row number for each child.
// `$idx` is set to a unique index for each child, progressing across rows first, from the bottom
//
// Examples:
// grid_copies(size=50, spacing=10) cylinder(d=10, h=1);
// grid_copies(size=50, spacing=[10,15]) cylinder(d=10, h=1);
// grid_copies(spacing=10, n=[13,7], stagger=true) cylinder(d=6, h=5);
// grid_copies(spacing=10, n=[13,7], stagger="alt") cylinder(d=6, h=5);
// grid_copies(size=50, n=11, stagger=true) cylinder(d=5, h=1);
//
// Example:
// poly = [[-25,-25], [25,25], [-25,25], [25,-25]];
// grid_copies(spacing=5, stagger=true, inside=poly)
// zrot(180/6) cylinder(d=5, h=1, $fn=6);
// %polygon(poly);
//
// Example: Using `$row` and `$col`
// grid_copies(spacing=8, n=8)
// color(($row+$col)%2?"black":"red")
// cube([8,8,0.01], center=false);
//
// Example: Makes a grid of hexagon pillars whose tops are all angled to reflect light at [0,0,50], if they were shiny.
// hexregion = circle(r=50.01,$fn=6);
// grid_copies(spacing=10, stagger=true, inside=hexregion)
// union() { // Needed for OpenSCAD 2021.01 as noted above
// ref_v = (unit([0,0,50]-point3d($pos)) + UP)/2;
// half_of(v=-ref_v, cp=[0,0,5])
// zrot(180/6)
// cylinder(h=20, d=10/cos(180/6)+0.01, $fn=6);
// }
module grid2d(spacing, n, size, stagger=false, inside=undef, nonzero)
{
deprecate("grid_copies");
grid_copies(spacing, n, size, stagger, inside, nonzero) children();
}
module grid_copies(spacing, n, size, stagger=false, inside=undef, nonzero)
{
req_children($children);
dummy = assert(in_list(stagger, [false, true, "alt"]));
bounds = is_undef(inside)? undef :
is_path(inside)? pointlist_bounds(inside) :
assert(is_region(inside))
pointlist_bounds(flatten(inside));
nonzero = is_path(inside) ? default(nonzero,false)
: assert(is_undef(nonzero), "nonzero only allowed if inside is a polygon")
false;
size = is_num(size)? [size, size] :
is_vector(size)? assert(len(size)==2) size :
bounds!=undef? [
for (i=[0:1]) 2*max(abs(bounds[0][i]),bounds[1][i])
] : undef;
spacing = is_num(spacing)? (
stagger!=false? polar_to_xy(spacing,60) :
[spacing,spacing]
) :
is_vector(spacing)? assert(len(spacing)==2) spacing :
size!=undef? (
is_num(n)? v_div(size,(n-1)*[1,1]) :
is_vector(n)? assert(len(n)==2) v_div(size,n-[1,1]) :
v_div(size,(stagger==false? [1,1] : [2,2]))
) :
undef;
n = is_num(n)? [n,n] :
is_vector(n)? assert(len(n)==2) n :
size!=undef && spacing!=undef? v_floor(v_div(size,spacing))+[1,1] :
[2,2];
dummy2 = assert(is_int(n[0]) && is_int(n[1]), "The number of rows/columns must be an integer");
offset = v_mul(spacing, n-[1,1])/2;
poslist =
stagger==false ?
[for (row = [0:1:n.y-1], col = [0:1:n.x-1])
let(
pos = v_mul([col,row],spacing) - offset
)
if (
is_undef(inside) ||
(is_path(inside) && point_in_polygon(pos, inside, nonzero=nonzero)>=0) ||
(is_region(inside) && point_in_region(pos, inside)>=0)
)
[pos,row,col]
]
:
let( // stagger == true or stagger == "alt"
staggermod = (stagger == "alt")? 1 : 0,
cols1 = ceil(n.x/2),
cols2 = n.x - cols1
)
[for (row = [0:1:n.y-1])
let(
rowcols = ((row%2) == staggermod)? cols1 : cols2
)
if (rowcols > 0)
for (col = [0:1:rowcols-1])
let(
rowdx = (row%2 != staggermod)? spacing.x : 0,
pos = v_mul([2*col,row],spacing) + [rowdx,0] - offset
)
if (
is_undef(inside) ||
(is_path(inside) && point_in_polygon(pos, inside, nonzero=nonzero)>=0) ||
(is_region(inside) && point_in_region(pos, inside)>=0)
)
[pos, row, col * 2 + ((row%2!=staggermod)? 1 : 0)]
];
for(i=idx(poslist)){
$idx=i;
$pos=poslist[i][0];
$row=poslist[i][1];
$col=poslist[i][2];
translate(poslist[i][0])children();
}
}
function grid_copies(spacing, n, size, stagger=false, inside=undef, nonzero, p=_NO_ARG) =
let(
dummy = assert(in_list(stagger, [false, true, "alt"])),
bounds = is_undef(inside)? undef :
is_path(inside)? pointlist_bounds(inside) :
assert(is_region(inside))
pointlist_bounds(flatten(inside)),
nonzero = is_path(inside) ? default(nonzero,false)
: assert(is_undef(nonzero), "nonzero only allowed if inside is a polygon")
false,
size = is_num(size)? [size, size] :
is_vector(size)? assert(len(size)==2) size :
bounds!=undef? [
for (i=[0:1]) 2*max(abs(bounds[0][i]),bounds[1][i])
] : undef,
spacing = is_num(spacing)? (
stagger!=false? polar_to_xy(spacing,60) :
[spacing,spacing]
) :
is_vector(spacing)? assert(len(spacing)==2) spacing :
size!=undef? (
is_num(n)? v_div(size,(n-1)*[1,1]) :
is_vector(n)? assert(len(n)==2) v_div(size,n-[1,1]) :
v_div(size,(stagger==false? [1,1] : [2,2]))
) :
undef,
n = is_num(n)? [n,n] :
is_vector(n)? assert(len(n)==2) n :
size!=undef && spacing!=undef? v_floor(v_div(size,spacing))+[1,1] :
[2,2],
offset = v_mul(spacing, n-[1,1])/2,
mats = stagger == false
? [
for (row = [0:1:n.y-1], col = [0:1:n.x-1])
let( pos = v_mul([col,row],spacing) - offset )
if (
is_undef(inside) ||
(is_path(inside) && point_in_polygon(pos, inside, nonzero=nonzero)>=0) ||
(is_region(inside) && point_in_region(pos, inside)>=0)
)
translate(pos)
]
: // stagger == true or stagger == "alt"
let(
staggermod = (stagger == "alt")? 1 : 0,
cols1 = ceil(n.x/2),
cols2 = n.x - cols1
)
[
for (row = [0:1:n.y-1])
let( rowcols = ((row%2) == staggermod)? cols1 : cols2 )
if (rowcols > 0)
for (col = [0:1:rowcols-1])
let(
rowdx = (row%2 != staggermod)? spacing.x : 0,
pos = v_mul([2*col,row],spacing) + [rowdx,0] - offset
)
if (
is_undef(inside) ||
(is_path(inside) && point_in_polygon(pos, inside, nonzero=nonzero)>=0) ||
(is_region(inside) && point_in_region(pos, inside)>=0)
)
translate(pos)
]
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
//////////////////////////////////////////////////////////////////////
// Section: Rotating copies of all children
//////////////////////////////////////////////////////////////////////
// Function&Module: rot_copies()
// Synopsis: Rotates copies of children.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Rotation, Copiers
// See Also: rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies(), move_copies(), xcopies(), ycopies(), zcopies(), line_copies(), grid_copies()
//
// Usage:
// rot_copies(rots, [cp=], [sa=], [delta=], [subrot=]) CHILDREN;
// rot_copies(rots, v, [cp=], [sa=], [delta=], [subrot=]) CHILDREN;
// rot_copies(n=, [v=], [cp=], [sa=], [delta=], [subrot=]) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = rot_copies(rots, [cp=], [sa=], [delta=], [subrot=], p=);
// copies = rot_copies(rots, v, [cp=], [sa=], [delta=], [subrot=], p=);
// copies = rot_copies(n=, [v=], [cp=], [sa=], [delta=], [subrot=], p=);
// Usage: Get Translation Matrices
// mats = rot_copies(rots, [cp=], [sa=], [delta=], [subrot=]);
// mats = rot_copies(rots, v, [cp=], [sa=], [delta=], [subrot=]);
// mats = rot_copies(n=, [v=], [cp=], [sa=], [delta=], [subrot=]);
// Description:
// When called as a module:
// - Given a list of [X,Y,Z] rotation angles in `rots`, rotates copies of the children to each of those angles, regardless of axis of rotation.
// - Given a list of scalar angles in `rots`, rotates copies of the children to each of those angles around the axis of rotation.
// - If given a vector `v`, that becomes the axis of rotation. Default axis of rotation is UP.
// - If given a count `n`, makes that many copies, rotated evenly around the axis.
// - If given an offset `delta`, translates each child by that amount before rotating them into place. This makes rings.
// - If given a centerpoint `cp`, centers the ring around that centerpoint.
// - If `subrot` is true, each child will be rotated in place to keep the same size towards the center when making rings.
// - The first (unrotated) copy will be placed at the relative starting angle `sa`.
// .
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// rots = A list of [X,Y,Z] rotation angles in degrees. If `v` is given, this will be a list of scalar angles in degrees to rotate around `v`.
// v = If given, this is the vector of the axis to rotate around.
// cp = Centerpoint to rotate around. Default: `[0,0,0]`
// ---
// n = Optional number of evenly distributed copies, rotated around the axis.
// sa = Starting angle, in degrees. For use with `n`. Angle is in degrees counter-clockwise. Default: 0
// delta = [X,Y,Z] amount to move away from cp before rotating. Makes rings of copies. Default: `[0,0,0]`
// subrot = If false, don't sub-rotate children as they are copied around the ring. Instead maintain their native orientation. The false setting is only allowed when `delta` is given. Default: `true`
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$ang` is set to the rotation angle (or XYZ rotation triplet) of each child copy, and can be used to modify each child individually.
// `$idx` is set to the index value of each child copy.
// `$axis` is set to the axis to rotate around, if `rots` was given as a list of angles instead of a list of [X,Y,Z] rotation angles.
//
//
// Example:
// #cylinder(h=20, r1=5, r2=0);
// rot_copies([[45,0,0],[0,45,90],[90,-45,270]]) cylinder(h=20, r1=5, r2=0);
//
// Example:
// rot_copies([45, 90, 135], v=DOWN+BACK)
// yrot(90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) yrot(90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// rot_copies(n=6, v=DOWN+BACK)
// yrot(90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) yrot(90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// rot_copies(n=6, v=DOWN+BACK, delta=[10,0,0])
// yrot(90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) yrot(90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// rot_copies(n=6, v=UP+FWD, delta=[10,0,0], sa=45)
// yrot(90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) yrot(90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// rot_copies(n=6, v=DOWN+BACK, delta=[20,0,0], subrot=false)
// yrot(90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) yrot(90) cylinder(h=20, r1=5, r2=0);
module rot_copies(rots=[], v, cp=[0,0,0], n, sa=0, offset=0, delta=[0,0,0], subrot=true)
{
assert(subrot || norm(delta)>0, "subrot can only be false if delta is not zero");
req_children($children);
sang = sa + offset;
angs = !is_undef(n)?
(n<=0? [] : [for (i=[0:1:n-1]) i/n*360+sang]) :
rots==[]? [] :
assert(!is_string(rots), "Argument rots must be an angle, a list of angles, or a range of angles.")
assert(!is_undef(rots[0]), "Argument rots must be an angle, a list of angles, or a range of angles.")
[for (a=rots) a];
for ($idx = idx(angs)) {
$ang = angs[$idx];
$axis = v;
translate(cp) {
rotate(a=$ang, v=v) {
translate(delta) {
rot(a=subrot? 0 : $ang, v=v, reverse=true) {
translate(-cp) {
children();
}
}
}
}
}
}
}
function rot_copies(rots=[], v, cp=[0,0,0], n, sa=0, offset=0, delta=[0,0,0], subrot=true, p=_NO_ARG) =
assert(subrot || norm(delta)>0, "subrot can only be false if delta is not zero")
let(
sang = sa + offset,
angs = !is_undef(n)?
(n<=0? [] : [for (i=[0:1:n-1]) i/n*360+sang]) :
rots==[]? [] :
assert(!is_string(rots), "Argument rots must be an angle, a list of angles, or a range of angles.")
assert(!is_undef(rots[0]), "Argument rots must be an angle, a list of angles, or a range of angles.")
[for (a=rots) a],
mats = [
for (ang = angs)
translate(cp) *
rot(a=ang, v=v) *
translate(delta) *
rot(a=subrot? 0 : ang, v=v, reverse=true) *
translate(-cp)
]
)
p==_NO_ARG? mats : [for (m = mats) apply(m, p)];
// Function&Module: xrot_copies()
// Synopsis: Rotates copies of children around the X axis.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Rotation, Copiers
// See Also: rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies(), move_copies(), xcopies(), ycopies(), zcopies(), line_copies(), grid_copies()
//
// Usage:
// xrot_copies(rots, [cp], [r=|d=], [sa=], [subrot=]) CHILDREN;
// xrot_copies(n=, [cp=], [r=|d=], [sa=], [subrot=]) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// copies = xrot_copies(rots, [cp], [r=|d=], [sa=], [subrot=], p=);
// copies = xrot_copies(n=, [cp=], [r=|d=], [sa=], [subrot=], p=);
// Usage: Get Translation Matrices
// mats = xrot_copies(rots, [cp], [r=|d=], [sa=], [subrot=]);
// mats = xrot_copies(n=, [cp=], [r=|d=], [sa=], [subrot=]);
// Description:
// When called as a module:
// - Given an array of angles, rotates copies of the children to each of those angles around the X axis.
// - If given a count `n`, makes that many copies, rotated evenly around the X axis.
// - If given a radius `r` (or diameter `d`), distributes children around a ring of that size around the X axis.
// - If given a centerpoint `cp`, centers the rotation around that centerpoint.
// - If `subrot` is true, each child will be rotated in place to keep the same size towards the center when making rings.
// - The first (unrotated) copy will be placed at the relative starting angle `sa`.
// .
// When called as a function, *without* a `p=` argument, returns a list of transformation matrices, one for each copy.
// When called as a function, *with* a `p=` argument, returns a list of transformed copies of `p=`.
//
// Arguments:
// rots = Optional array of rotation angles, in degrees, to make copies at.
// cp = Centerpoint to rotate around.
// ---
// n = Optional number of evenly distributed copies to be rotated around the ring.
// sa = Starting angle, in degrees. For use with `n`. Angle is in degrees counter-clockwise from Y+, when facing the origin from X+. First unrotated copy is placed at that angle.
// r = If given, makes a ring of child copies around the X axis, at the given radius. Default: 0
// d = If given, makes a ring of child copies around the X axis, at the given diameter.
// subrot = If false, don't sub-rotate children as they are copied around the ring. Instead maintain their native orientation. The false setting is only allowed when `d` or `r` is given. Default: `true`
// subrot = If false, don't sub-rotate children as they are copied around the ring.
// p = Either a point, pointlist, VNF or Bezier patch to be translated when used as a function.
//
// Side Effects:
// `$idx` is set to the index value of each child copy.
// `$ang` is set to the rotation angle of each child copy, and can be used to modify each child individually.
// `$axis` is set to the axis vector rotated around.
//
//
// Example:
// xrot_copies([180, 270, 315])
// cylinder(h=20, r1=5, r2=0);
// color("red",0.333) cylinder(h=20, r1=5, r2=0);
//
// Example:
// xrot_copies(n=6)
// cylinder(h=20, r1=5, r2=0);
// color("red",0.333) cylinder(h=20, r1=5, r2=0);
//
// Example:
// xrot_copies(n=6, r=10)
// xrot(-90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) xrot(-90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// xrot_copies(n=6, r=10, sa=45)
// xrot(-90) cylinder(h=20, r1=5, r2=0);
// color("red",0.333) xrot(-90) cylinder(h=20, r1=5, r2=0);
//
// Example:
// xrot_copies(n=6, r=20, subrot=false)
// xrot(-90) cylinder(h=20, r1=5, r2=0, center=true);
// color("red",0.333) xrot(-90) cylinder(h=20, r1=5, r2=0, center=true);
module xrot_copies(rots=[], cp=[0,0,0], n, sa=0, r, d, subrot=true)
{
req_children($children);
r = get_radius(r=r, d=d, dflt=0);
assert(all_nonnegative([r]), "d/r must be nonnegative");
assert(subrot || r>0, "subrot can only be false if d or r is given");
rot_copies(rots=rots, v=RIGHT, cp=cp, n=n, sa=sa, delta=[0, r, 0], subrot=subrot) children();
}
function xrot_copies(rots=[], cp=[0,0,0], n, sa=0, r, d, subrot=true, p=_NO_ARG) =
let( r = get_radius(r=r, d=d, dflt=0) )
assert(all_nonnegative([r]), "d/r must be nonnegative")
assert(subrot || r>0, "subrot can only be false if d or r is given")
rot_copies(rots=rots, v=RIGHT, cp=cp, n=n, sa=sa, delta=[0, r, 0], subrot=subrot, p=p);
// Function&Module: yrot_copies()
// Synopsis: Rotates copies of children around the Y axis.
// SynTags: MatList, Trans
// Topics: Transformations, Distributors, Rotation, Copiers
// See Also: rot_copies(), xrot_copies(), yrot_copies(), zrot_copies(), arc_copies(), sphere_copies(), move_copies(), xcopies(), ycopies(), zcopies(), line_copies(), grid_copies()
//
// Usage:
// yrot_copies(rots, [cp], [r=|d=], [sa=], [subrot=]) CHILDREN;
// yrot_copies(n=, [cp=], [r=|d=], [sa=], [subrot=]) CHILDREN;