ifc_lite_geometry/math/v3.rs
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
5//! 3-vectors over `[f64; 3]`. Every function is FMA-free with the op order
6//! written out; see the [module conventions](super).
7
8/// A 3-vector.
9pub type V3 = [f64; 3];
10
11#[inline]
12pub fn add(a: V3, b: V3) -> V3 {
13 [a[0] + b[0], a[1] + b[1], a[2] + b[2]]
14}
15
16#[inline]
17pub fn sub(a: V3, b: V3) -> V3 {
18 [a[0] - b[0], a[1] - b[1], a[2] - b[2]]
19}
20
21#[inline]
22pub fn scale(a: V3, s: f64) -> V3 {
23 [a[0] * s, a[1] * s, a[2] * s]
24}
25
26/// `a0*b0 + a1*b1 + a2*b2`, left to right, never fused.
27#[inline]
28pub fn dot(a: V3, b: V3) -> f64 {
29 a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
30}
31
32/// Right-handed cross product, each component `x*y - z*w` unfused.
33#[inline]
34pub fn cross(a: V3, b: V3) -> V3 {
35 [
36 a[1] * b[2] - a[2] * b[1],
37 a[2] * b[0] - a[0] * b[2],
38 a[0] * b[1] - a[1] * b[0],
39 ]
40}
41
42/// Euclidean length, `dot(a, a).sqrt()`. (Not `hypot`: the two differ in the
43/// last bit, and every site on main uses this spelling.)
44#[inline]
45pub fn norm(a: V3) -> f64 {
46 dot(a, a).sqrt()
47}
48
49/// Distance, `norm(sub(a, b))`. Bit-equal to the `powi(2)` spelling: `powi(x, 2)`
50/// lowers to `x * x`.
51#[inline]
52pub fn dist(a: V3, b: V3) -> f64 {
53 norm(sub(a, b))
54}
55
56/// `v / |v|`, or `None` when the length is not finite or is `<= min_len`.
57///
58/// The threshold is the caller's: pass `0.0` to refuse only a zero (or
59/// non-finite) vector, or a scale-appropriate epsilon. A NaN component gives a
60/// NaN length and so `None`. Each caller's degenerate fallback (zero vector,
61/// `+Z`, the input itself) is an explicit `.unwrap_or(..)` at the call site.
62/// Divides each component (not a reciprocal multiply), as every site does.
63#[inline]
64pub fn normalize(v: V3, min_len: f64) -> Option<V3> {
65 let len = norm(v);
66 if !len.is_finite() || len <= min_len {
67 return None;
68 }
69 Some([v[0] / len, v[1] / len, v[2] / len])
70}
71
72/// `v` scaled into normalisable range: `v` itself, bit for bit, when
73/// `dot(v, v)` is finite (every ordinary direction); otherwise `v` divided by
74/// its largest-magnitude component. Finite ratios whose squared length
75/// overflows (components from about 1e154 up) would give `|v| = inf`, and
76/// `v / |v|` the zero vector, which passes every one-ended length check; the
77/// rescaled vector has the same direction and a length in `[1, √3]`. A
78/// non-finite component is returned as is, for the caller's own refusal.
79#[inline]
80pub(crate) fn unoverflowed(v: V3) -> V3 {
81 if dot(v, v).is_finite() || !v.iter().all(|c| c.is_finite()) {
82 return v;
83 }
84 let largest = v[0].abs().max(v[1].abs()).max(v[2].abs());
85 [v[0] / largest, v[1] / largest, v[2] / largest]
86}
87
88/// The unnormalised triangle normal `cross(b - a, c - a)`; its length is twice
89/// the triangle's area.
90#[inline]
91pub fn tri_normal(a: V3, b: V3, c: V3) -> V3 {
92 cross(sub(b, a), sub(c, a))
93}
94
95/// Flip `n` so its largest-magnitude component is positive, so a face and its
96/// antipodal opposite collapse onto one direction. Ties break x, y, z. A NaN
97/// component never wins a comparison, and `-0.0 < 0.0` is false, so neither
98/// flips the vector.
99///
100/// The same rule as clash's `canonical` (`rust/clash/src/canonical.rs`), which
101/// is TS-parity-locked and so cannot be shared; `canonical_parity_tests` pins
102/// the two together bit for bit.
103#[inline]
104pub fn canonical(n: V3) -> V3 {
105 let (ax, ay, az) = (n[0].abs(), n[1].abs(), n[2].abs());
106 let mut idx = 0usize;
107 if ay > ax && ay >= az {
108 idx = 1;
109 } else if az > ax && az > ay {
110 idx = 2;
111 }
112 if n[idx] < 0.0 {
113 [-n[0], -n[1], -n[2]]
114 } else {
115 n
116 }
117}