forked from BelfrySCAD/BOSL2
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtransforms.scad
More file actions
1617 lines (1506 loc) · 65.9 KB
/
transforms.scad
File metadata and controls
1617 lines (1506 loc) · 65.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//////////////////////////////////////////////////////////////////////
// LibFile: transforms.scad
// Functions and modules that provide shortcuts for translation,
// rotation and mirror operations. Also provided are skew and frame_map,
// which remaps the coordinate axes. The shortcuts can act on
// geometry, like the usual OpenSCAD rotate() and translate(). They
// also work as functions that operate on lists of points in various
// forms: paths, VNFS and bezier patches. Lastly, the function form
// of the shortcuts can return a matrix representing the operation
// the shortcut performs. The rotation and scaling shortcuts accept
// an optional centerpoint for the rotation or scaling operation.
// .
// Almost all of the transformation functions take a point, a point
// list, bezier patch, or VNF as a second positional argument to
// operate on. The exceptions are rot(), frame_map() and skew().
// Includes:
// include <BOSL2/std.scad>
// FileGroup: Basic Modeling
// FileSummary: Shortcuts for translation, rotation, etc. Can act on geometry, paths, or can return a matrix.
// FileFootnotes: STD=Included in std.scad
//////////////////////////////////////////////////////////////////////
_BOSL2_TRANSFORMS = is_undef(_BOSL2_STD) && (is_undef(BOSL2_NO_STD_WARNING) || !BOSL2_NO_STD_WARNING) ?
echo("Warning: transforms.scad included without std.scad; dependencies may be missing\nSet BOSL2_NO_STD_WARNING = true to mute this warning.") true : true;
// Section: Affine Transformations
// OpenSCAD provides various built-in modules to transform geometry by
// translation, scaling, rotation, and mirroring. All of these operations
// are affine transformations. A three-dimensional affine transformation
// can be represented by a 4x4 matrix. The transformation shortcuts in this
// file generally have three modes of operation. They can operate
// directly on geometry like their OpenSCAD built-in equivalents. For example,
// `left(10) cube()`. They can operate on a list of points (or various other
// types of geometric data). For example, operating on a list of points: `points = left(10, [[1,2,3],[4,5,6]])`.
// The third option is that the shortcut can return the transformation matrix
// corresponding to its action. For example, `M=left(10)`.
// .
// This capability allows you to store and manipulate transformations, and can
// be useful in more advanced modeling. You can multiply these matrices
// together, analogously to applying a sequence of operations with the
// built-in transformations. So you can write `zrot(37)left(5)cube()`
// to perform two operations on a cube. You can also store
// that same transformation by multiplying the matrices together: `M = zrot(37) * left(5)`.
// Note that the order is exactly the same as the order used to apply the transformation.
// .
// Suppose you have constructed `M` as above. What now? You can use
// the OpensCAD built-in `multmatrix` to apply it to some geometry: `multmatrix(M) cube()`.
// Alternative you can use the BOSL2 function `apply` to apply `M` to a point, a list
// of points, a bezier patch, or a VNF. For example, `points = apply(M, [[3,4,5],[5,6,7]])`.
// Note that the `apply` function can work on both 2D and 3D data, but if you want to
// operate on 2D data, you must choose transformations that don't modify z
// .
// You can use matrices as described above without understanding the details, just
// treating a matrix as a box that stores a transformation. The OpenSCAD manual section for multmatrix
// gives some details of how this works. We'll elaborate a bit more below. An affine transformation
// matrix for three dimensional data is a 4x4 matrix. The top left 3x3 portion gives the linear
// transformation to apply to the data. For example, it could be a rotation or scaling, or combination of both.
// The 3x1 column at the top right gives the translation to apply. The bottom row should be `[0,0,0,1]`. That
// bottom row is only present to enable
// the matrices to be multiplied together. OpenSCAD ignores it and in fact `multmatrix`
// accepts a 3x4 matrix, where that row is missing. In order for a matrix to act on a point you have to
// augment the point with an extra 1, making it a length 4 vector. In OpenSCAD you can then compute the
// the affine transformed point as `tran_point = M * point`. However, this syntax hides a complication that
// arises if you have a list of points. A list of points like `[[1,2,3,1],[4,5,6,1],[7,8,9,1]]` has the augmented points
// as row vectors on the list. In order to transform such a list, it needs to be muliplied on the right
// side, not the left side.
_NO_ARG = [true,[123232345],false];
//////////////////////////////////////////////////////////////////////
// Section: Translations
//////////////////////////////////////////////////////////////////////
// Function&Module: move()
// Aliases: translate()
//
// Synopsis: Translates children in an arbitrary direction.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: left(), right(), fwd(), back(), down(), up(), spherical_to_xyz(), altaz_to_xyz(), cylindrical_to_xyz(), polar_to_xy()
//
// Usage: As Module
// move(v) CHILDREN;
// Usage: As a function to translate points, VNF, or Bezier patches
// pts = move(v, p);
// pts = move(STRING, p);
// Usage: Get Translation Matrix
// mat = move(v);
// Description:
// Translates geometry position or point position by the given vector.
// * If called as a module, moves/translates all children.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
// .
// The functional form of `move()` can also be invoked as `translate()`.
// Arguments:
// v = An [X,Y,Z] vector to translate by. For function form with `p` a point list or VNF, can be "centroid", "mean" or "box".
// p = (function only) A point, list of points or VNF to be translated.
//
// Example:
// #sphere(d=10);
// move([0,20,30]) sphere(d=10);
//
// Example: You can move a 3D object with a 2D vector. The Z component is treated as zero.
// #sphere(d=10);
// move([-10,-5]) sphere(d=10);
//
// Example(2D): Move to centroid
// polygon(move("centroid", right_triangle([10,4])));
//
// Example(FlatSpin): Using Altitude-Azimuth Coordinates
// #sphere(d=10);
// move(altaz_to_xyz(30,90,20)) sphere(d=10);
//
// Example(FlatSpin): Using Spherical Coordinates
// #sphere(d=10);
// move(spherical_to_xyz(20,45,30)) sphere(d=10);
//
// Example(2D):
// path = square([50,30], center=true);
// #stroke(path, closed=true);
// stroke(move([10,20],p=path), closed=true);
//
// Example(NORENDER):
// pt1 = move([0,20,30], [15,23,42]); // Returns: [15, 43, 72]
// pt2 = move([0,3,1], [[1,2,3],[4,5,6]]); // Returns: [[1,5,4], [4,8,7]]
// mat2d = move([2,3]); // Returns: [[1,0,2],[0,1,3],[0,0,1]]
// mat3d = move([2,3,4]); // Returns: [[1,0,0,2],[0,1,0,3],[0,0,1,4],[0,0,0,1]]
module move(v=[0,0,0], p) {
req_children($children);
assert(!is_string(v),"Module form of `move()` does not accept string `v` arguments");
assert(is_undef(p), "Module form `move()` does not accept p= argument.");
assert(is_vector(v) && (len(v)==3 || len(v)==2), "Invalid value for `v`")
translate(point3d(v)) children();
}
function move(v=[0,0,0], p=_NO_ARG) =
is_string(v) ? (
assert(is_vnf(p) || is_path(p),"String movements only work with point lists and VNFs")
let(
center = v=="centroid" ? centroid(p)
: v=="mean" ? mean(p)
: v=="box" ? mean(pointlist_bounds(p))
: assert(false,str("Unknown string movement ",v))
)
move(-center,p=p)
)
:
assert(is_vector(v) && (len(v)==3 || len(v)==2), "Invalid value for `v`")
let(
m = affine3d_translate(point3d(v))
)
p==_NO_ARG ? m : apply(m, p);
function translate(v=[0,0,0], p=_NO_ARG) = move(v=v, p=p);
// Function&Module: left()
//
// Synopsis: Translates children leftward (X-).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), right(), fwd(), back(), down(), up()
//
// Usage: As Module
// left(x) CHILDREN;
// Usage: Translate Points
// pts = left(x, p);
// Usage: Get Translation Matrix
// mat = left(x);
//
// Description:
// Moves geometry or data to the left (in the X- direction) by the specified amount. (If `x` is negative motion is to the right.)
// * If called as a module, moves all children left.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// x = Scalar amount to move left.
// p = (function only) A point, list of points or VNF to be translated.
//
// Example:
// #sphere(d=10);
// left(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = left(20, [23,42]); // Returns: [3,42]
// pt2 = left(20, [15,23,42]); // Returns: [-5,23,42]
// pt3 = left(3, [[1,2,3],[4,5,6]]); // Returns: [[-2,2,3], [1,5,6]]
// mat3d = left(4); // Returns: [[1,0,0,-4],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
module left(x=0, p) {
req_children($children);
assert(is_undef(p), "Module form `left()` does not accept p= argument.");
assert(is_finite(x), "Invalid number")
translate([-x,0,0]) children();
}
function left(x=0, p=_NO_ARG) =
assert(is_finite(x), "Invalid number")
move([-x,0,0],p=p);
// Function&Module: right()
// Aliases: xmove()
//
// Synopsis: Translates children rightward (X+).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), left(), fwd(), back(), down(), up()
//
// Usage: As Module
// right(x) CHILDREN;
// Usage: Translate Points
// pts = right(x, p);
// Usage: Get Translation Matrix
// mat = right(x);
//
// Description:
// Moves geometry or data to the right (in the X+ direction) by the specified amount. (If `x` is negative motion is to the left.)
// * If called as a module, moves all children right.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 tranformation matrix.
// .
// You can also call this as `xmove()`.
// Arguments:
// x = Scalar amount to move right.
// p = (function only) A point, list of points or VNF to be translated.
//
// Example:
// #sphere(d=10);
// right(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = right(20, [23,42]); // Returns: [43,42]
// pt2 = right(20, [15,23,42]); // Returns: [35,23,42]
// pt3 = right(3, [[1,2,3],[4,5,6]]); // Returns: [[4,2,3], [7,5,6]]
// pt4 = xmove(-2, [1,2,3]); // Returns: [-1,2,3]
// mat3d = right(4); // Returns: [[1,0,0,4],[0,1,0,0],[0,0,1,0],[0,0,0,1]]
module right(x=0, p) {
req_children($children);
assert(is_undef(p), "Module form `right()` does not accept p= argument.");
assert(is_finite(x), "Invalid number")
translate([x,0,0]) children();
}
function right(x=0, p=_NO_ARG) =
assert(is_finite(x), "Invalid number")
move([x,0,0],p=p);
module xmove(x=0, p) {
req_children($children);
assert(is_undef(p), "Module form `xmove()` does not accept p= argument.");
assert(is_finite(x), "Invalid number")
translate([x,0,0]) children();
}
function xmove(x=0, p=_NO_ARG) =
assert(is_finite(x), "Invalid number")
move([x,0,0],p=p);
// Function&Module: fwd()
//
// Synopsis: Translates children forward (Y-).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), left(), right(), back(), down(), up()
//
// Usage: As Module
// fwd(y) CHILDREN;
// Usage: Translate Points
// pts = fwd(y, p);
// Usage: Get Translation Matrix
// mat = fwd(y);
//
// Description:
// Moves geometry or data forward (in the Y- direction) by the specified amount. (If `y` is negative motion is to the back.)
// * If called as a module, moves all children forward.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 transformation matrix.
// Arguments:
// y = Scalar amount to move forward.
// p = (function only) A point, list of points or VNF to be translated.
//
// Example:
// #sphere(d=10);
// fwd(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = fwd(20, [23,42]); // Returns: [23,22]
// pt2 = fwd(20, [15,23,42]); // Returns: [15,3,42]
// pt3 = fwd(3, [[1,2,3],[4,5,6]]); // Returns: [[1,-1,3], [4,2,6]]
// mat3d = fwd(4); // Returns: [[1,0,0,0],[0,1,0,-4],[0,0,1,0],[0,0,0,1]]
module fwd(y=0, p) {
req_children($children);
assert(is_undef(p), "Module form `fwd()` does not accept p= argument.");
assert(is_finite(y), "Invalid number")
translate([0,-y,0]) children();
}
function fwd(y=0, p=_NO_ARG) =
assert(is_finite(y), "Invalid number")
move([0,-y,0],p=p);
// Function&Module: back()
// Aliases: ymove()
//
// Synopsis: Translates children backward (Y+).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), left(), right(), fwd(), down(), up()
//
// Usage: As Module
// back(y) CHILDREN;
// Usage: Translate Points
// pts = back(y, p);
// Usage: Get Translation Matrix
// mat = back(y);
//
// Description:
// Moves geometry or data forward (in the Y+ direction) by the specified amount. (If `y` is negative motion is forward.)
// * If called as a module, moves all children forward.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 transformation matrix.
// .
// You can also call this as `ymove()`.
// Arguments:
// y = Scalar amount to move back.
// p = (function only) A point, list of points or VNF to be translated.
//
// Example:
// #sphere(d=10);
// back(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = back(20, [23,42]); // Returns: [23,62]
// pt2 = back(20, [15,23,42]); // Returns: [15,43,42]
// pt3 = back(3, [[1,2,3],[4,5,6]]); // Returns: [[1,5,3], [4,8,6]]
// pt4 = ymove(4, [2,2,2]); // Returns: [2,6,2]
// mat3d = back(4); // Returns: [[1,0,0,0],[0,1,0,4],[0,0,1,0],[0,0,0,1]]
module back(y=0, p) {
req_children($children);
assert(is_undef(p), "Module form `back()` does not accept p= argument.");
assert(is_finite(y), "Invalid number")
translate([0,y,0]) children();
}
function back(y=0,p=_NO_ARG) =
assert(is_finite(y), "Invalid number")
move([0,y,0],p=p);
module ymove(y=0, p) {
req_children($children);
assert(is_undef(p), "Module form `ymove()` does not accept p= argument.");
assert(is_finite(y), "Invalid number")
translate([0,y,0]) children();
}
function ymove(y=0,p=_NO_ARG) =
assert(is_finite(y), "Invalid number")
move([0,y,0],p=p);
// Function&Module: down()
//
// Synopsis: Translates children downward (Z-).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), left(), right(), fwd(), back(), up()
//
// Usage: As Module
// down(z) CHILDREN;
// Usage: Translate Points
// pts = down(z, p);
// Usage: Get Translation Matrix
// mat = down(z);
//
// Description:
// Moves geometry or data dwon (in the Z- direction) by the specified amount. (If `z` is negative motion is upward.)
// * If called as a module, moves all children down.
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// z = Scalar amount to move down
// p = Either a point, or a list of points to be translated when used as a function.
//
// Example:
// #sphere(d=10);
// down(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = down(20, [15,23,42]); // Returns: [15,23,22]
// pt2 = down(3, [[1,2,3],[4,5,6]]); // Returns: [[1,2,0], [4,5,3]]
// mat3d = down(4); // Returns: [[1,0,0,0],[0,1,0,0],[0,0,1,-4],[0,0,0,1]]
module down(z=0, p) {
req_children($children);
assert(is_undef(p), "Module form `down()` does not accept p= argument.");
translate([0,0,-z]) children();
}
function down(z=0, p=_NO_ARG) =
assert(is_finite(z), "Invalid number")
move([0,0,-z],p=p);
// Function&Module: up()
// Aliases: zmove()
//
// Synopsis: Translates children upward (Z+).
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Translation
// See Also: move(), left(), right(), fwd(), back(), down()
//
// Usage: As Module
// up(z) CHILDREN;
// Usage: Translate Points
// pts = up(z, p);
// Usage: Get Translation Matrix
// mat = up(z);
//
// Description:
// Moves geometry or data up (in the Z+ direction) by the specified amount. (If `z` is negative motion is downward.)
// * If called as a module, moves all children up
// * If called as a function with the `p` argument, returns the translated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without the `p` argument, returns a 4x4 transformation matrix.
// .
// You can also call this as `zmove()`.
// Arguments:
// z = Scalar amount to move up.
// p = Either a point, or a list of points to be translated when used as a function.
//
// Example:
// #sphere(d=10);
// up(20) sphere(d=10);
//
// Example(NORENDER):
// pt1 = up(20, [15,23,42]); // Returns: [15,23,62]
// pt2 = up(3, [[1,2,3],[4,5,6]]); // Returns: [[1,2,6], [4,5,9]]
// p53 = zmove(-2, [3,3,3]); // Returns: [3,3,1]
// mat3d = up(4); // Returns: [[1,0,0,0],[0,1,0,0],[0,0,1,4],[0,0,0,1]]
module up(z=0, p) {
req_children($children);
assert(is_undef(p), "Module form `up()` does not accept p= argument.");
assert(is_finite(z), "Invalid number");
translate([0,0,z]) children();
}
function up(z=0, p=_NO_ARG) =
assert(is_finite(z), "Invalid number")
move([0,0,z],p=p);
module zmove(z=0, p) {
req_children($children);
assert(is_undef(p), "Module form `zmove()` does not accept p= argument.");
assert(is_finite(z), "Invalid number");
translate([0,0,z]) children();
}
function zmove(z=0, p=_NO_ARG) =
assert(is_finite(z), "Invalid number")
move([0,0,z],p=p);
//////////////////////////////////////////////////////////////////////
// Section: Rotations
//////////////////////////////////////////////////////////////////////
// Function&Module: rot()
//
// Synopsis: Rotates children in various ways.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Rotation
// See Also: xrot(), yrot(), zrot(), tilt()
//
// Usage: As a Module
// rot(a, [cp=], [reverse=]) CHILDREN;
// rot([X,Y,Z], [cp=], [reverse=]) CHILDREN;
// rot(a, v, [cp=], [reverse=]) CHILDREN;
// rot(from=, to=, [a=], [reverse=]) CHILDREN;
// Usage: As a Function to transform data in `p`
// pts = rot(a, p=, [cp=], [reverse=]);
// pts = rot([X,Y,Z], p=, [cp=], [reverse=]);
// pts = rot(a, v, p=, [cp=], [reverse=]);
// pts = rot([a], from=, to=, p=, [reverse=]);
// Usage: As a Function to return a transform matrix
// M = rot(a, [cp=], [reverse=]);
// M = rot([X,Y,Z], [cp=], [reverse=]);
// M = rot(a, v, [cp=], [reverse=]);
// M = rot(from=, to=, [a=], [reverse=]);
//
// Description:
// This is an expanded version of the built-in `rotate()` that behaves identically to the built-in with the same arguments,
// but offers additional capabilities.
// You can specify the rotation to perform in one of several ways:
// * `rot(30)` or `rot(a=30)` rotates 30 degrees around the Z axis.
// * `rot([20,30,40])` or `rot(a=[20,30,40])` rotates 20 degrees around the X axis, then 30 degrees around the Y axis, then 40 degrees around the Z axis.
// * `rot(30, [1,1,0])` or `rot(a=30, v=[1,1,0])` rotates 30 degrees around the axis vector `[1,1,0]` (in the right-hand direction).
// * `rot(from=[0,0,1], to=[1,0,0])` rotates the `from` vector to line up with the `to` vector, in this case the top to the right and hence equivalent to `rot(a=90,v=[0,1,0]`. The axis of rotation is perpendicular to the two given vectors.
// * `rot(from=[0,1,1], to=[1,1,0], a=45)` rotates 45 degrees around the `from` vector ([0,1,1]) and then rotates the `from` vector to align with the `to` vector. Equivalent to `rot(from=[0,1,1],to=[1,1,0]) rot(a=45,v=[0,1,1])`. You can also regard `a` as as post-rotation around the `to` vector. For this form, `a` must be a scalar.
// * If the `cp` centerpoint argument is given, then rotations are performed around that centerpoint. So `rot(args...,cp=[1,2,3])` is equivalent to `move(-[1,2,3])rot(args...)move([1,2,3])`.
// * If the `reverse` argument is true, then the rotations performed is exactly reversed.
// .
// The behavior and return value varies depending on how `rot()` is called:
// * If called as a module, rotates all children.
// * If called as a function with the `p=` argument, returns the rotated version of that `p=` argument. The `p=` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns the 4x4 rotation matrix.
// .
// This function (and sometimes {{frame_map()}} are the only two transformations that require
// that the `p=` argument be given as a named argument rather than a positional argument.
//
// Arguments:
// a = Scalar angle or vector of XYZ rotation angles to rotate by, in degrees. If you use the `from` and `to` arguments then `a` must be a scalar. Default: `0`
// v = vector for the axis of rotation. Default: [0,0,1] or UP
// ---
// from = Starting vector for vector-based rotations.
// to = Target vector for vector-based rotations.
// cp = centerpoint to rotate around. Default: [0,0,0]
// reverse = If true, exactly reverses the rotation, including axis rotation ordering. Default: false
// p = (function only) A point, list of points, Bezier patch or VNF to be rotated.
//
// Example:
// #cube([2,4,9]);
// rot([30,60,0], cp=[0,0,9]) cube([2,4,9]);
//
// Example:
// #cube([2,4,9]);
// rot(30, v=[1,1,0], cp=[0,0,9]) cube([2,4,9]);
//
// Example:
// #cube([2,4,9]);
// rot(from=UP, to=LEFT+BACK) cube([2,4,9]);
//
// Example(2D):
// path = square([50,30], center=true);
// #stroke(path, closed=true);
// stroke(rot(30,p=path), closed=true);
module rot(a=0, v, cp, from, to, reverse=false)
{
req_children($children);
m = rot(a=a, v=v, cp=cp, from=from, to=to, reverse=reverse);
multmatrix(m) children();
}
function rot(a=0, v, cp, from, to, reverse=false, p=_NO_ARG) =
assert(is_undef(from)==is_undef(to), "from and to must be specified together.")
assert(is_undef(from) || is_vector(from, zero=false), "'from' must be a non-zero vector.")
assert(is_undef(to) || is_vector(to, zero=false), "'to' must be a non-zero vector.")
assert(is_undef(v) || is_vector(v, zero=false), "'v' must be a non-zero vector.")
assert(is_undef(cp) || is_vector(cp), "'cp' must be a vector.")
assert(is_finite(a) || is_vector(a), "'a' must be a finite scalar or a vector.")
assert(is_bool(reverse))
let(
m = let(
from = is_undef(from)? undef : point3d(from),
to = is_undef(to)? undef : point3d(to),
cp = is_undef(cp)? undef : point3d(cp),
m1 = !is_undef(from) ?
assert(is_num(a))
affine3d_rot_from_to(from,to) * affine3d_rot_by_axis(from,a)
: !is_undef(v)?
assert(is_num(a))
affine3d_rot_by_axis(v,a)
: is_num(a) ? affine3d_zrot(a)
: affine3d_zrot(a.z) * affine3d_yrot(a.y) * affine3d_xrot(a.x),
m2 = is_undef(cp)? m1 : (move(cp) * m1 * move(-cp)),
m3 = reverse? rot_inverse(m2) : m2
) m3
)
p==_NO_ARG ? m : apply(m, p);
// Function&Module: xrot()
//
// Synopsis: Rotates children around the X axis using the right-hand rule.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Rotation
// See Also: rot(), yrot(), zrot(), tilt()
//
// Usage: As Module
// xrot(a, [cp=]) CHILDREN;
// Usage: As a function to rotate points
// rotated = xrot(a, p, [cp=]);
// Usage: As a function to return rotation matrix
// mat = xrot(a, [cp=]);
//
// Description:
// Rotates around the X+ axis by the given number of degrees. If `cp` is given, rotate around that center point.
// * If called as a module, rotates all children.
// * If called as a function with the `p` argument, returns the rotated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns the 4x4 rotation matrix.
//
// Arguments:
// a = Rotation angle in degrees.
// p = (function only) A point, list of points, Bezier patch or VNF to be rotated.
// ---
// cp = center point to rotate around. Default: [0,0,0]
//
// Example:
// #cylinder(h=50, r=10, center=true);
// xrot(90) cylinder(h=50, r=10, center=true);
module xrot(a=0, p, cp)
{
req_children($children);
assert(is_undef(p), "Module form `xrot()` does not accept p= argument.");
if (a==0) {
children(); // May be slightly faster?
} else if (!is_undef(cp)) {
translate(cp) rotate([a, 0, 0]) translate(-cp) children();
} else {
rotate([a, 0, 0]) children();
}
}
function xrot(a=0, p=_NO_ARG, cp) = rot([a,0,0], cp=cp, p=p);
// Function&Module: yrot()
//
// Synopsis: Rotates children around the Y axis using the right-hand rule.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Rotation
// See Also: rot(), xrot(), zrot(), tilt()
//
// Usage: As Module
// yrot(a, [cp=]) CHILDREN;
// Usage: Rotate Points
// rotated = yrot(a, p, [cp=]);
// Usage: Get Rotation Matrix
// mat = yrot(a, [cp=]);
//
// Description:
// Rotates around the Y+ axis by the given number of degrees. If `cp` is given, rotate around that center point.
// * If called as a module, rotates all children.
// * If called as a function with the `p` argument, returns the rotated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns the 4x4 rotation matrix.
//
// Arguments:
// a = Rotation angle in degrees.
// p = (function only) A point, list of points, Bezier patch or VNF to be rotated.
// ---
// cp = center point to rotate around. Default: [0,0,0]
//
// Example:
// #cylinder(h=50, r=10, center=true);
// yrot(90) cylinder(h=50, r=10, center=true);
module yrot(a=0, p, cp)
{
req_children($children);
assert(is_undef(p), "Module form `yrot()` does not accept p= argument.");
if (a==0) {
children(); // May be slightly faster?
} else if (!is_undef(cp)) {
translate(cp) rotate([0, a, 0]) translate(-cp) children();
} else {
rotate([0, a, 0]) children();
}
}
function yrot(a=0, p=_NO_ARG, cp) = rot([0,a,0], cp=cp, p=p);
// Function&Module: zrot()
//
// Synopsis: Rotates children around the Z axis using the right-hand rule.
// Topics: Affine, Matrices, Transforms, Rotation
// SynTags: Trans, Path, VNF, Mat
// See Also: rot(), xrot(), yrot(), tilt()
//
// Usage: As Module
// zrot(a, [cp=]) CHILDREN;
// Usage: As Function to rotate points
// rotated = zrot(a, p, [cp=]);
// Usage: As Function to return rotation matrix
// mat = zrot(a, [cp=]);
//
// Description:
// Rotates around the Z+ axis by the given number of degrees. If `cp` is given, rotate around that center point.
// * If called as a module, rotates all children.
// * If called as a function with the `p` argument, returns the rotated version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns the 4x4 rotation matrix.
//
// Arguments:
// a = Rotation angle in degrees.
// p = (function only) A point, list of points, Bezier patch or VNF to be rotated.
// ---
// cp = centerpoint to rotate around. Default: [0,0,0]
//
// Example:
// #cube(size=[60,20,40], center=true);
// zrot(90) cube(size=[60,20,40], center=true);
module zrot(a=0, p, cp)
{
req_children($children);
assert(is_undef(p), "Module form `zrot()` does not accept p= argument.");
if (a==0) {
children(); // May be slightly faster?
} else if (!is_undef(cp)) {
translate(cp) rotate(a) translate(-cp) children();
} else {
rotate(a) children();
}
}
function zrot(a=0, p=_NO_ARG, cp) = rot(a, cp=cp, p=p);
// Function&Module: tilt()
//
// Synopsis: Tilts children toward a direction
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Rotation
// See Also: rot(), xrot(), yrot(), zrot()
//
// Usage: As a Module
// tilt(to, [reverse=], [cp=]) CHILDREN;
// Usage: As a Function to transform data in `p`
// pts = tilt(to, p, [reverse=], [cp=]);
// Usage: As a Function to return a transform matrix
// M = tilt(to, [reverse=], [cp=]);
//
// Description:
// This is shorthand for `rot(from=UP,to=x)` and operates similarly. It tilts things that point UP until they point in the direction of the given `to` vector.
// * If the `cp` centerpoint argument is given, then the tilt/rotation is performed around that centerpoint. So `tilt(...,cp=[1,2,3])` is equivalent to `move([1,2,3]) tilt(...) move([-1,-2,-3])`.
// * If the `reverse` argument is true, then the tilt/rotation is reversed.
// .
// The behavior and return value varies depending on how `tilt()` is called:
// * Ifc called as a module, tilts all children.
// * If called as a function with the `p` argument, returns the tilted version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// to = Target vector for vector-based rotations.
// p = (function only) A point, list of points, Bezier patch or VNF to be rotated.
// ---
// cp = centerpoint to tilt/rotate around. Default: [0,0,0]
// reverse = If true, reverses the rotation. Default: false
//
// Example:
// #cube([2,4,9]);
// tilt(LEFT+BACK) cube([2,4,9]);
//
// Example(2D):
// path = square([50,30], center=true);
// #stroke(path, closed=true);
// stroke(tilt(RIGHT+FWD,path3d(path)), closed=true);
module tilt(to, p, cp, reverse=false)
{
assert(is_undef(p), "Module form `tilt()` does not accept p= argument.");
req_children($children);
m = rot(from=UP, to=to, cp=cp, reverse=reverse);
multmatrix(m) children();
}
function tilt(to, p=_NO_ARG, cp, reverse=false) =
assert(is_vector(to, zero=false), "'to' must be a non-zero vector.")
assert(is_undef(cp) || is_vector(cp), "'cp' must be a vector.")
assert(is_bool(reverse))
let( m = rot(from=UP, to=to, cp=cp, reverse=reverse) )
p==_NO_ARG ? m : apply(m, p);
//////////////////////////////////////////////////////////////////////
// Section: Scaling
//////////////////////////////////////////////////////////////////////
// Function&Module: scale()
//
// Synopsis: Scales children arbitrarily.
// SynTags: Trans, Path, VNF, Mat, Ext
// Topics: Affine, Matrices, Transforms, Scaling
// See Also: xscale(), yscale(), zscale()
//
// Usage: As Module (native OpenSCAD)
// scale(SCALAR) CHILDREN;
// scale([X,Y,Z]) CHILDREN;
// Usage: As module with center point (BOSL2 extension)
// scale(v, cp=) CHILDREN;
// Usage: As module with with scaling direction vector (BOSL2 extension)
// scale(v, dir=, [cp=]) CHILDREN;
// Usage: Scale Points
// pts = scale(v, p, [cp=], [dir=]);
// Usage: Get Scaling Matrix
// mat = scale(v, [cp=], [dir=]);
//
// Description:
// Scales by the [X,Y,Z] scaling factors given in `v`. If `v` is given as a scalar number, all axes are scaled uniformly by that amount.
// If `v` has fewer than three dimensions, it is padded with 1 values, so scaling by [4] is the same as [4,1,1]. If you give `cp` a
// vector value then the scaling is done relative to that specified center point. If you give the vector `dir` then the scale factor, `v`, must
// be a scalar and the scaling occurs along the direction of `dir`. (The sign and magnitude of `dir` don't matter.)
// * If called as the built-in module, scales all children.
// * If called as a function with the `p` argument, returns the scaled version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// v = Either a numeric uniform scaling factor, or a list of [X,Y,Z] scaling factors. Default: 1
// p = (function only) A point, list of points, Bezier patch or VNF to scale.
// ---
// cp = If given, centers the scaling on the point `cp`.
// dir = If given as a vector, scale along the direction of that vector.
//
// Example(NORENDER):
// pt1 = scale(3, [3,1,4]); // Returns: [9,3,12]
// pt2 = scale([2,3,4], [3,1,4]); // Returns: [6,3,16]
// pt3 = scale([2,3,4], [[1,2,3],[4,5,6]]); // Returns: [[2,6,12], [8,15,24]]
// mat2d = scale([2,3]); // Returns: [[2,0,0],[0,3,0],[0,0,1]]
// mat3d = scale([2,3,4]); // Returns: [[2,0,0,0],[0,3,0,0],[0,0,4,0],[0,0,0,1]]
//
// Example(2D):
// path = circle(d=50,$fn=12);
// #stroke(path,closed=true);
// stroke(scale([1.5,3],path),closed=true);
function scale(v=1, p=_NO_ARG, cp=[0,0,0],dir) =
assert(is_num(v) || is_vector(v),"Invalid scale")
assert(p==_NO_ARG || is_list(p),"Invalid point list")
assert(is_undef(dir) || is_vector(dir),"Invalid dir vector")
assert(is_vector(cp))
is_def(dir)? assert(is_num(v),"v must be a scalar when dir is given")
assert(len(dir)<=3 && norm(dir)>0, "dir must be a nonzero vector with 3 or fewer entries")
rot(from=dir,to=RIGHT,reverse=true)
* xscale(v,p=p,cp=rot(from=dir,to=RIGHT,p=cp))
* rot(from=dir,to=RIGHT)
: let(
v = is_num(v)? [v,v,v] : v,
m = cp==[0,0,0]
? affine3d_scale(v)
: affine3d_translate(point3d(cp))
* affine3d_scale(v)
* affine3d_translate(point3d(-cp))
)
p==_NO_ARG? m : apply(m, p) ;
// Function&Module: xscale()
//
// Synopsis: Scales children along the X axis.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Scaling
// See Also: scale(), yscale(), zscale()
//
// Usage: As Module
// xscale(x, [cp=]) CHILDREN;
// Usage: Scale Points
// scaled = xscale(x, p, [cp=]);
// Usage: Get Affine Matrix
// mat = xscale(x, [cp=]);
//
// Description:
// Scales in the X direction by the scale factor `x`. If `cp` is given either as a scalar X value or as an [X,Y,Z] value then X is used as the center for the scaling.
// * If called as the built-in module, scales all children.
// * If called as a function with the `p` argument, returns the scaled version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// x = Factor to scale by, along the X axis.
// p = (function only) A point, list of points, Bezier patch or VNF to be scaled.
// ---
// cp = If given as a point, centers the scaling on the point `cp`. If given as a scalar, centers scaling on the point `[cp,0,0]`
//
// Example: As Module
// xscale(3) sphere(r=10);
//
// Example(2D): Scaling Points
// path = circle(d=50,$fn=12);
// #stroke(path,closed=true);
// stroke(xscale(2,path),closed=true);
module xscale(x=1, p, cp=0) {
req_children($children);
assert(is_undef(p), "Module form `xscale()` does not accept p= argument.");
cp = is_num(cp)? [cp,0,0] : cp;
if (cp == [0,0,0]) {
scale([x,1,1]) children();
} else {
translate(cp) scale([x,1,1]) translate(-cp) children();
}
}
function xscale(x=1, p=_NO_ARG, cp=0) =
assert(is_finite(x))
assert(p==_NO_ARG || is_list(p))
assert(is_finite(cp) || is_vector(cp))
let( cp = is_num(cp)? [cp,0,0] : cp )
scale([x,1,1], cp=cp, p=p);
// Function&Module: yscale()
//
// Synopsis: Scales children along the Y axis.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Scaling
// See Also: scale(), xscale(), zscale()
//
// Usage: As Module
// yscale(y, [cp=]) CHILDREN;
// Usage: Scale Points
// scaled = yscale(y, p, [cp=]);
// Usage: Get Affine Matrix
// mat = yscale(y, [cp=]);
//
// Description:
// Scales in the Y direction by the scale factor `y`. If `cp` is given either as a scalar Y value or as an [X,Y,Z] value then Y is used
// as the center for the scaling.
// * If called as the built-in module, scales all children.
// * If called as a function with the `p` argument, returns the scaled version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// y = Factor to scale by, along the Y axis.
// p = (function only) A point, list of points, Bezier patch or VNF to be scaled.
// ---
// cp = If given as a point, centers the scaling on the point `cp`. If given as a scalar, centers scaling on the point `[0,cp,0]`
//
// Example: As Module
// yscale(3) sphere(r=10);
//
// Example(2D): Scaling Points
// path = circle(d=50,$fn=12);
// #stroke(path,closed=true);
// stroke(yscale(2,path),closed=true);
module yscale(y=1, p, cp=0) {
req_children($children);
assert(is_undef(p), "Module form `yscale()` does not accept p= argument.");
cp = is_num(cp)? [0,cp,0] : cp;
if (cp == [0,0,0]) {
scale([1,y,1]) children();
} else {
translate(cp) scale([1,y,1]) translate(-cp) children();
}
}
function yscale(y=1, p=_NO_ARG, cp=0) =
assert(is_finite(y))
assert(p==_NO_ARG || is_list(p))
assert(is_finite(cp) || is_vector(cp))
let( cp = is_num(cp)? [0,cp,0] : cp )
scale([1,y,1], cp=cp, p=p);
// Function&Module: zscale()
//
// Synopsis: Scales children along the Z axis.
// SynTags: Trans, Path, VNF, Mat
// Topics: Affine, Matrices, Transforms, Scaling
// See Also: scale(), xscale(), yscale()
//
// Usage: As Module
// zscale(z, [cp=]) CHILDREN;
// Usage: Scale Points
// scaled = zscale(z, p, [cp=]);
// Usage: Get Affine Matrix
// mat = zscale(z, [cp=]);
//
// Description:
// Scales along the Z axis by the scaling factor `z`. If `cp` is given either as a scalar Z value or as an [X,Y,Z] value then Z is used
// as the center for the scaling.
// * If called as the built-in module, scales all children.
// * If called as a function with the `p` argument, returns the scaled version of that `p` argument. The `p` argument can be a point, list of points, [bezier patch](beziers.scad) or [VNF structure](vnf.scad).
// * If called as a function without a `p` argument, returns a 4x4 transformation matrix.
//
// Arguments:
// z = Factor to scale by, along the Z axis.
// p = (function only) A point, list of points, Bezier patch or VNF to be scaled.
// ---
// cp = If given as a point, centers the scaling on the point `cp`. If given as a scalar, centers scaling on the point `[0,0,cp]`
//
// Example: As Module
// zscale(3) sphere(r=10);
//
// Example: Scaling Points
// path = xrot(90,path3d(circle(d=50,$fn=12)));
// #stroke(path,closed=true);
// stroke(zscale(2,path),closed=true);
module zscale(z=1, p, cp=0) {
req_children($children);
assert(is_undef(p), "Module form `zscale()` does not accept p= argument.");
cp = is_num(cp)? [0,0,cp] : cp;
if (cp == [0,0,0]) {
scale([1,1,z]) children();
} else {
translate(cp) scale([1,1,z]) translate(-cp) children();
}
}
function zscale(z=1, p=_NO_ARG, cp=0) =
assert(is_finite(z))
assert(is_undef(p) || is_list(p))
assert(is_finite(cp) || is_vector(cp))
let( cp = is_num(cp)? [0,0,cp] : cp )
scale([1,1,z], cp=cp, p=p);