ifc_lite_geometry/math/
frame.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//! The IFC Z-up to WebGL Y-up frame, defined once.
6//!
7//! `S` maps `(x, y, z)` to `(x, z, -y)`; a proper rotation (det +1), so
8//! `S⁻¹ = Sᵀ` maps `(x, y, z)` to `(x, -z, y)`.
9//!
10//! - **Points and vectors** swap components. The swap never folds the sign of
11//!   zero: `-y` of `+0.0` is `-0.0`, in every copy on main.
12//! - **Boxes** reverse min/max on the negated axis ([`aabb_zup_to_yup`]).
13//! - **Matrices** are conjugated, `M' = S·M·Sᵀ`. Conjugating by a signed
14//!   permutation moves each entry to `M'[r][c] = s(r)·s(c)·M[p(r)][p(c)]`, so it
15//!   is done as exactly that, with `+ 0.0` folding the sign of zero. For every
16//!   finite input this is bit-equal to the `S·M·Sᵀ` matmul (each matmul output
17//!   is `0.0 + .. + (±m)`, which is `±m` with every zero positive). It is
18//!   storage-agnostic: conjugation commutes with transposition, so it applies to
19//!   row- and column-major arrays alike. A non-finite entry stays in its own
20//!   slot instead of spreading `NaN` across a row.
21
22use super::mat4::{M3, M4};
23use std::ops::Neg;
24
25/// `S` as a row-major 4x4 (linear part only).
26pub const ZUP_TO_YUP: M4 = [
27    1.0, 0.0, 0.0, 0.0, //
28    0.0, 0.0, 1.0, 0.0, //
29    0.0, -1.0, 0.0, 0.0, //
30    0.0, 0.0, 0.0, 1.0,
31];
32/// `Sᵀ`.
33pub const YUP_TO_ZUP: M4 = [
34    1.0, 0.0, 0.0, 0.0, //
35    0.0, 0.0, -1.0, 0.0, //
36    0.0, 1.0, 0.0, 0.0, //
37    0.0, 0.0, 0.0, 1.0,
38];
39
40/// `(x, y, z)` to `(x, z, -y)`. Generic so f32 vertex buffers and f64 origins
41/// share it; it monomorphises in the caller and always inlines.
42#[inline]
43pub fn zup_to_yup<T: Copy + Neg<Output = T>>(v: [T; 3]) -> [T; 3] {
44    [v[0], v[2], -v[1]]
45}
46
47/// `(x, y, z)` to `(x, -z, y)`, the inverse of [`zup_to_yup`].
48#[inline]
49pub fn yup_to_zup<T: Copy + Neg<Output = T>>(v: [T; 3]) -> [T; 3] {
50    [v[0], -v[2], v[1]]
51}
52
53/// [`zup_to_yup`] over a flat `x, y, z, ...` buffer. Only complete triplets are
54/// rewritten; a 1-2 float tail is left alone.
55#[inline]
56pub fn zup_to_yup_in_place(buf: &mut [f32]) {
57    for c in buf.chunks_exact_mut(3) {
58        let (y, z) = (c[1], c[2]);
59        c[1] = z;
60        c[2] = -y;
61    }
62}
63
64/// Swap an AABB `[minx, miny, minz, maxx, maxy, maxz]` from Z-up to Y-up.
65/// Swapping `min`/`max` per component is WRONG: negating Y flips which corner
66/// is the min along the new Z, so the new Z range is `[-maxY, -minY]`.
67///
68/// Generic so the f32 local bounds and the f64 world AABBs share one reversal.
69#[inline]
70pub fn aabb_zup_to_yup<T: Copy + Neg<Output = T>>(b: [T; 6]) -> [T; 6] {
71    let [min_x, min_y, min_z, max_x, max_y, max_z] = b;
72    [min_x, min_z, -max_y, max_x, max_z, -min_y]
73}
74
75/// `S·M·Sᵀ` for a Z-up matrix, as a signed permutation (see the module docs).
76#[inline]
77pub fn conjugate_zup_to_yup(m: &M4) -> M4 {
78    conjugate4(m, [0, 2, 1, 3], [1.0, 1.0, -1.0, 1.0])
79}
80
81/// `Sᵀ·M·S`, the inverse of [`conjugate_zup_to_yup`].
82#[inline]
83pub fn conjugate_yup_to_zup(m: &M4) -> M4 {
84    conjugate4(m, [0, 2, 1, 3], [1.0, -1.0, 1.0, 1.0])
85}
86
87/// [`conjugate_zup_to_yup`] for a 3x3 rotation.
88#[inline]
89pub fn conjugate3_zup_to_yup(m: &M3) -> M3 {
90    let (p, s) = ([0usize, 2, 1], [1.0, 1.0, -1.0]);
91    std::array::from_fn(|i| {
92        let (r, c) = (i / 3, i % 3);
93        s[r] * s[c] * m[p[r] * 3 + p[c]] + 0.0
94    })
95}
96
97#[inline]
98fn conjugate4(m: &M4, p: [usize; 4], s: [f64; 4]) -> M4 {
99    std::array::from_fn(|i| {
100        let (r, c) = (i / 4, i % 4);
101        s[r] * s[c] * m[p[r] * 4 + p[c]] + 0.0
102    })
103}