Skip to main content

spatialrust_transform/
bounds.rs

1//! Bounding-volume types.
2
3use spatialrust_math::{Mat3, Vec3};
4
5/// Axis-aligned bounding box.
6#[derive(Clone, Copy, Debug, PartialEq)]
7pub struct Aabb {
8    /// Lower corner `(x, y, z)`.
9    pub min: Vec3<f32>,
10    /// Upper corner `(x, y, z)`.
11    pub max: Vec3<f32>,
12}
13
14impl Aabb {
15    /// Creates a box from its corners.
16    #[must_use]
17    pub const fn new(min: Vec3<f32>, max: Vec3<f32>) -> Self {
18        Self { min, max }
19    }
20
21    /// Center of the box.
22    #[must_use]
23    pub fn center(&self) -> Vec3<f32> {
24        Vec3::new(
25            0.5 * (self.min.x + self.max.x),
26            0.5 * (self.min.y + self.max.y),
27            0.5 * (self.min.z + self.max.z),
28        )
29    }
30
31    /// Side lengths `(dx, dy, dz)`.
32    #[must_use]
33    pub fn extent(&self) -> Vec3<f32> {
34        Vec3::new(self.max.x - self.min.x, self.max.y - self.min.y, self.max.z - self.min.z)
35    }
36}
37
38/// Oriented bounding box recovered from the principal axes of a cloud.
39#[derive(Clone, Copy, Debug, PartialEq)]
40pub struct Obb {
41    /// Box center.
42    pub center: Vec3<f32>,
43    /// Column vectors are the three orthonormal box axes.
44    pub axes: Mat3<f32>,
45    /// Half side lengths along each axis.
46    pub half_extents: Vec3<f32>,
47}