ifc_lite_geometry/math/
aabb.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//! Axis-aligned boxes over `[f64; 3]`.
6
7use super::v3::V3;
8
9/// A closed axis-aligned box. [`Aabb3::EMPTY`] (`+INF`/`-INF`) is the identity
10/// of [`Aabb3::extend`] and [`Aabb3::union`]; it is not well-formed, so nothing
11/// publishes it by accident (see [`Aabb3::is_well_formed`]).
12#[derive(Clone, Copy, Debug, PartialEq)]
13pub struct Aabb3 {
14    pub min: V3,
15    pub max: V3,
16}
17
18impl Aabb3 {
19    /// No point yet: `min = +INF`, `max = -INF` on every axis.
20    pub const EMPTY: Aabb3 = Aabb3 {
21        min: [f64::INFINITY; 3],
22        max: [f64::NEG_INFINITY; 3],
23    };
24
25    /// The box of `points` ([`Aabb3::EMPTY`] for none).
26    #[inline]
27    pub fn from_points(points: impl IntoIterator<Item = V3>) -> Aabb3 {
28        let mut b = Aabb3::EMPTY;
29        for p in points {
30            b.extend(p);
31        }
32        b
33    }
34
35    /// The box of a flat `x, y, z, ...` f32 position buffer, widened exactly to
36    /// f64. Only complete triplets count; a 1-2 float tail is ignored.
37    #[inline]
38    pub fn from_f32_positions(positions: &[f32]) -> Aabb3 {
39        Aabb3::from_points(
40            positions
41                .chunks_exact(3)
42                .map(|p| [p[0] as f64, p[1] as f64, p[2] as f64]),
43        )
44    }
45
46    /// Grow to include `p`.
47    ///
48    /// Uses `f64::min`/`f64::max`, which return the non-NaN operand: a NaN
49    /// coordinate is skipped rather than poisoning the box. That is the
50    /// behaviour of every fold this replaces. It also means axes can diverge (one
51    /// axis left at its sentinel while its neighbours hold real bounds), which is
52    /// why publishing a box goes through [`Aabb3::is_well_formed`].
53    #[inline]
54    pub fn extend(&mut self, p: V3) {
55        for k in 0..3 {
56            self.min[k] = self.min[k].min(p[k]);
57            self.max[k] = self.max[k].max(p[k]);
58        }
59    }
60
61    /// The smallest box holding both (NaN skipped, as in [`Aabb3::extend`]).
62    #[inline]
63    pub fn union(&self, other: &Aabb3) -> Aabb3 {
64        let mut u = *self;
65        for k in 0..3 {
66            u.min[k] = u.min[k].min(other.min[k]);
67            u.max[k] = u.max[k].max(other.max[k]);
68        }
69        u
70    }
71
72    /// Closed overlap: boxes that merely touch overlap. An [`Aabb3::EMPTY`] box
73    /// overlaps nothing.
74    #[inline]
75    pub fn overlaps(&self, other: &Aabb3) -> bool {
76        (0..3).all(|k| self.min[k] <= other.max[k] && other.min[k] <= self.max[k])
77    }
78
79    /// Every axis finite and `min <= max`.
80    ///
81    /// All three axes are tested, not just one: because [`Aabb3::extend`] drops
82    /// NaN, a position buffer carrying NaN on one axis and finite values on the
83    /// others leaves that axis at its `INFINITY..NEG_INFINITY` sentinel while its
84    /// neighbours hold real bounds. Testing only axis 0 would then publish
85    /// `[x0, inf, z0, x1, -inf, z1]` as a measured box, which downstream
86    /// differences turn into NaN. This also covers "no point was ever seen".
87    #[inline]
88    pub fn is_well_formed(&self) -> bool {
89        (0..3).all(|k| {
90            self.min[k].is_finite() && self.max[k].is_finite() && self.min[k] <= self.max[k]
91        })
92    }
93
94    /// `[minx, miny, minz, maxx, maxy, maxz]`.
95    #[inline]
96    pub fn to_array6(&self) -> [f64; 6] {
97        [
98            self.min[0], self.min[1], self.min[2], self.max[0], self.max[1], self.max[2],
99        ]
100    }
101
102    /// Inverse of [`Aabb3::to_array6`]; performs no validation.
103    #[inline]
104    pub fn from_array6(a: [f64; 6]) -> Aabb3 {
105        Aabb3 { min: [a[0], a[1], a[2]], max: [a[3], a[4], a[5]] }
106    }
107}
108
109/// Internal f32 counterpart for folds whose rounding must remain f32 (#7781).
110/// Like `Aabb3`, starts at infinities, ignores NaN coordinates through native
111/// `min`/`max`, and visits points in order. Signed-zero ties use those same
112/// native operations; no widening, total ordering or zero normalization occurs.
113#[derive(Clone, Copy)]
114pub(crate) struct Aabb3f32 {
115    pub(crate) min: [f32; 3],
116    pub(crate) max: [f32; 3],
117}
118
119impl Aabb3f32 {
120    #[inline]
121    pub(crate) fn extend(&mut self, p: [f32; 3]) {
122        for k in 0..3 {
123            self.min[k] = self.min[k].min(p[k]);
124            self.max[k] = self.max[k].max(p[k]);
125        }
126    }
127
128    /// Complete triplets only; an empty buffer keeps the infinity sentinels.
129    #[inline]
130    pub(crate) fn from_positions(positions: &[f32]) -> Self {
131        let mut bounds = Self {
132            min: [f32::INFINITY; 3],
133            max: [f32::NEG_INFINITY; 3],
134        };
135        for p in positions.chunks_exact(3) {
136            bounds.extend([p[0], p[1], p[2]]);
137        }
138        bounds
139    }
140}