sourcelibs/Vector3/src/Vector3.xtl

1⍝│ Vector3: batched 3D vectors and rigid transforms. 2⍝│ Import with "v:" u_se< "Vector3"; Matrix is a dependency. 3⍝│ Points are N by 3 Float rows, right-handed xyz, y up, radians. 4⍝│ Transforms are 4 by 4, acting on homogeneous column vectors. 5 6"mx:" u̲se< "Matrix" 7 8⍝│ Refuse nonfinite input or arithmetic overflow, including NaN. 9ʰf̲inite ← { a → 10 n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Vector3 requires finite numbers and finite results"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires finite numbers and finite results"))
11 a 12} 13 14⍝│ Validate one N by 3 batch, including a zero-row batch. 15ʰb̲atch ← { p → 16 n̲ot ((t̲ally p) c̲at 3) m̲atch s̲hape p ? @ p̲anic< "Vector3 requires an N by 3 batch"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires an N by 3 batch"))
17 ʰf̲inite f̲loat p 18} 19 20⍝│ Validate a single xyz vector; there is no implicit batch broadcasting. 21ʰv̲ector ← { p → 22 n̲ot (1 r̲eshape 3) m̲atch s̲hape p ? @ p̲anic< "Vector3 requires an xyz vector of length 3"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires an xyz vector of length 3"))
23 ʰf̲inite f̲loat p 24} 25 26⍝│ a d_ot b: one dot product per matching row, no broadcasting. 27ˡd̲ot ← { a b → 28 x ← ʰb̲atch a 29 y ← ʰb̲atch b 30 n̲ot (s̲hape x) m̲atch s̲hape y ? @ p̲anic< "Vector3 batches must have matching shapes"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 batches must have matching shapes"))
31 ʰf̲inite '+ r̲/₂ x × y 32} 33 34⍝│ a c_ross b: right-handed cross products of matching rows. 35ˡc̲ross ← { a b → 36 x ← ʰb̲atch a 37 y ← ʰb̲atch b 38 n̲ot (s̲hape x) m̲atch s̲hape y ? @ p̲anic< "Vector3 batches must have matching shapes"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 batches must have matching shapes"))
39 ʰf̲inite ((1 o̲-₂ x) × 2 o̲-₂ y) − (2 o̲-₂ x) × 1 o̲-₂ y 40} 41 42⍝│ l_ength p: Euclidean length per row, scaled before squaring. 43ˡl̲ength ← { p → 44 x ← ʰb̲atch p 45 m ← 'm̲ax r̲/₂ a̲bs x 46 d ← m + f̲loat m = 0.0 47 q ← x ÷ d '× t̲able 1.0 1.0 1.0 48 ʰf̲inite m × s̲qrt '+ r̲/₂ q × q 49} 50 51⍝│ n_ormalize p: unit rows; a zero row stays zero, without dividing by 0. 52ˡn̲ormalize ← { p → 53 x ← ʰb̲atch p 54 m ← 'm̲ax r̲/₂ a̲bs x 55 q ← x ÷ (m + f̲loat m = 0.0) '× t̲able 1.0 1.0 1.0 56 d ← s̲qrt '+ r̲/₂ q × q 57 q ÷ (d + f̲loat d = 0.0) '× t̲able 1.0 1.0 1.0 58} 59 60⍝│ t_ranslation offset: rigid matrix translating by xyz offset. 61ˡt̲ranslation ← { offset → 62 d ← ʰv̲ector offset 63 (f̲loat ᵐˣi̲dentity 4) + (d c̲at 0.0) '× t̲able 0.0 0.0 0.0 1.0 64} 65 66⍝│ r_otationY angle: right-handed rotation matrix about y. 67ˡr̲otationY ← { angle → 68 n̲ot 0 = t̲ally s̲hape angle ? @ p̲anic< "Vector3 requires a scalar angle"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires a scalar angle"))
69 a ← ʰf̲inite f̲loat angle 70 c ← c̲os a 71 s ← s̲in a 72 4 4 r̲eshape c c̲at 0.0 c̲at s c̲at 0.0 c̲at 0.0 1.0 0.0 0.0 c̲at (n̲eg s) c̲at 0.0 c̲at c c̲at 0.0 0.0 0.0 0.0 1.0 73} 74 75⍝│ matrix t_ransform p: apply a rigid homogeneous matrix to point rows. 76⍝│ Refuse non-rigid matrices to 1e-10 (orthogonality and determinant +1). 77ˡt̲ransform ← { matrix p → 78 n̲ot 4 4 m̲atch s̲hape matrix ? @ p̲anic< "Vector3 requires a 4 by 4 rigid matrix"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires a 4 by 4 rigid matrix"))
79 m ← ʰf̲inite f̲loat matrix 80 r ← 3 t̲ake₂ 3 t̲ake m 81 n̲ot '∧ r̲/ (a̲bs (4 s̲elect m) − 0.0 0.0 0.0 1.0) ≤ 1e-10 ? @ p̲anic< "Vector3 requires a rigid matrix"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires a rigid matrix"))
82 n̲ot '∧ r̲/ r̲avel (a̲bs (r ᵐˣm̲ul o̲\ r) − f̲loat ᵐˣi̲dentity 3) ≤ 1e-10 ? @ p̲anic< "Vector3 requires a rigid matrix"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires a rigid matrix"))
83 1e-10 < a̲bs (ᵐˣd̲et r) − 1.0 ? @ p̲anic< "Vector3 requires a proper rotation"
p̲anic< expands to
(⎕P̲ANIC ("Vector3 requires a proper rotation"))
84 x ← ʰb̲atch p 85 ʰf̲inite 3 t̲ake₂ (x c̲at₂ ((t̲ally x) c̲at 1) r̲eshape 1.0) ᵐˣm̲ul o̲\ m 86} 87 88⍝│ offset t_ranslate p: move every point by one explicit xyz offset. 89ˡt̲ranslate ← { offset p → (ˡt̲ranslation offset) ˡt̲ransform p } 90 91⍝│ ((pivot r_otateY angle)_ p): rotate point rows about an xyz pivot. 92ˡr̲otateY ← { pivot angle p → 93 d ← ʰv̲ector pivot 94 m ← (ˡt̲ranslation d) ᵐˣm̲ul (ˡr̲otationY angle) ᵐˣm̲ul ˡt̲ranslation n̲eg d 95 m ˡt̲ransform p 96}