librarylibs/Vector3/src/Vector3.xtl
Vector3: batched 3D vectors and rigid transforms. Import with "v:" u_se< "Vector3"; Matrix is a dependency. Points are N by 3 Float rows, right-handed xyz, y up, radians. Transforms are 4 by 4, acting on homogeneous column vectors.
ʰf̲inite : Float -> Float
Refuse nonfinite input or arithmetic overflow, including NaN.
ʰf̲inite ← { a → n̲ot '∧ r̲/ r̲avel (a − a) = 0.0 ? @ p̲anic< "Vector3 requires finite numbers and finite results" a }
ʰb̲atch : Num a => a -> Float
Validate one N by 3 batch, including a zero-row batch.
ʰb̲atch ← { p → n̲ot ((t̲ally p) c̲at 3) m̲atch s̲hape p ? @ p̲anic< "Vector3 requires an N by 3 batch" ʰf̲inite f̲loat p }
ʰv̲ector : Num a => a -> Float
Validate a single xyz vector; there is no implicit batch broadcasting.
ʰv̲ector ← { p → n̲ot (1 r̲eshape 3) m̲atch s̲hape p ? @ p̲anic< "Vector3 requires an xyz vector of length 3" ʰf̲inite f̲loat p }
ˡd̲ot : (Num a, Num b) => a -> b -> Float
a d_ot b: one dot product per matching row, no broadcasting.
ˡd̲ot ← { a b → x ← ʰb̲atch a y ← ʰb̲atch b n̲ot (s̲hape x) m̲atch s̲hape y ? @ p̲anic< "Vector3 batches must have matching shapes" ʰf̲inite '+ r̲/₂ x × y }
ˡc̲ross : (Num a, Num b) => a -> b -> Float
a c_ross b: right-handed cross products of matching rows.
ˡc̲ross ← { a b → x ← ʰb̲atch a y ← ʰb̲atch b n̲ot (s̲hape x) m̲atch s̲hape y ? @ p̲anic< "Vector3 batches must have matching shapes" ʰf̲inite ((1 o̲-₂ x) × 2 o̲-₂ y) − (2 o̲-₂ x) × 1 o̲-₂ y }
ˡl̲ength : Num a => a -> Float
l_ength p: Euclidean length per row, scaled before squaring.
ˡl̲ength ← { p → x ← ʰb̲atch p m ← 'm̲ax r̲/₂ a̲bs x d ← m + f̲loat m = 0.0 q ← x ÷ d '× t̲able 1.0 1.0 1.0 ʰf̲inite m × s̲qrt '+ r̲/₂ q × q }
ˡn̲ormalize : Num a => a -> Float
n_ormalize p: unit rows; a zero row stays zero, without dividing by 0.
ˡn̲ormalize ← { p → x ← ʰb̲atch p m ← 'm̲ax r̲/₂ a̲bs x q ← x ÷ (m + f̲loat m = 0.0) '× t̲able 1.0 1.0 1.0 d ← s̲qrt '+ r̲/₂ q × q q ÷ (d + f̲loat d = 0.0) '× t̲able 1.0 1.0 1.0 }
ˡt̲ranslation : Num a => a -> Float
t_ranslation offset: rigid matrix translating by xyz offset.
ˡt̲ranslation ← { offset → d ← ʰv̲ector offset (f̲loat ᵐˣi̲dentity 4) + (d c̲at 0.0) '× t̲able 0.0 0.0 0.0 1.0 }
ˡr̲otationY : Num a => a -> Float
r_otationY angle: right-handed rotation matrix about y.
ˡr̲otationY ← { angle → n̲ot 0 = t̲ally s̲hape angle ? @ p̲anic< "Vector3 requires a scalar angle" a ← ʰf̲inite f̲loat angle c ← c̲os a s ← s̲in a 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 }
ˡt̲ransform : (Num a, Num b) => a -> b -> Float
matrix t_ransform p: apply a rigid homogeneous matrix to point rows. Refuse non-rigid matrices to 1e-10 (orthogonality and determinant +1).
ˡt̲ransform ← { matrix p → n̲ot 4 4 m̲atch s̲hape matrix ? @ p̲anic< "Vector3 requires a 4 by 4 rigid matrix" m ← ʰf̲inite f̲loat matrix r ← 3 t̲ake₂ 3 t̲ake m 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" 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" 1e-10 < a̲bs (ᵐˣd̲et r) − 1.0 ? @ p̲anic< "Vector3 requires a proper rotation" x ← ʰb̲atch p ʰf̲inite 3 t̲ake₂ (x c̲at₂ ((t̲ally x) c̲at 1) r̲eshape 1.0) ᵐˣm̲ul o̲\ m }
ˡt̲ranslate : (Num a, Num b) => a -> b -> Float
offset t_ranslate p: move every point by one explicit xyz offset.
ˡt̲ranslate ← { offset p → (ˡt̲ranslation offset) ˡt̲ransform p }
ˡr̲otateY : (Num a, Num b, Num c) => a -> b -> c -> Float
((pivot r_otateY angle)_ p): rotate point rows about an xyz pivot.
ˡr̲otateY ← { pivot angle p → d ← ʰv̲ector pivot m ← (ˡt̲ranslation d) ᵐˣm̲ul (ˡr̲otationY angle) ᵐˣm̲ul ˡt̲ranslation n̲eg d m ˡt̲ransform p }