kika 0.7.1

Robust 2D kernel for Rust: exact predicates, Delaunay triangulation, constrained Delaunay triangulation, and simple-polygon triangulation, with adaptive/exact fallback arithmetic. No CMake, no Boost, no GMP/MPFR, WASM-ready.
Documentation
use super::Point3;

/// An axis-aligned bounding box in 3D. See [`super::Aabb2`]'s doc
/// comment; same design, one more axis.
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Aabb3 {
    min: Point3,
    max: Point3,
}

impl Aabb3 {
    /// Builds the AABB containing exactly `p` and `q`, in either order.
    pub fn from_points(p: Point3, q: Point3) -> Self {
        Aabb3 {
            min: Point3::new_unchecked(p.x().min(q.x()), p.y().min(q.y()), p.z().min(q.z())),
            max: Point3::new_unchecked(p.x().max(q.x()), p.y().max(q.y()), p.z().max(q.z())),
        }
    }

    /// The box's minimum corner (component-wise minimum).
    #[inline]
    pub fn min(&self) -> Point3 {
        self.min
    }

    /// The box's maximum corner (component-wise maximum).
    #[inline]
    pub fn max(&self) -> Point3 {
        self.max
    }

    /// `true` iff the two boxes share at least one point, including
    /// touching at a face, edge, or corner.
    pub fn overlaps(&self, other: &Aabb3) -> bool {
        self.min.x() <= other.max.x()
            && other.min.x() <= self.max.x()
            && self.min.y() <= other.max.y()
            && other.min.y() <= self.max.y()
            && self.min.z() <= other.max.z()
            && other.min.z() <= self.max.z()
    }

    /// `true` iff `p` lies inside the box, boundary inclusive.
    pub fn contains_point(&self, p: Point3) -> bool {
        self.min.x() <= p.x()
            && p.x() <= self.max.x()
            && self.min.y() <= p.y()
            && p.y() <= self.max.y()
            && self.min.z() <= p.z()
            && p.z() <= self.max.z()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn p(x: f64, y: f64, z: f64) -> Point3 {
        Point3::new(x, y, z).unwrap()
    }

    #[test]
    fn from_points_normalizes_order() {
        let a = Aabb3::from_points(p(3.0, 4.0, -1.0), p(1.0, -2.0, 5.0));
        assert_eq!(a.min(), p(1.0, -2.0, -1.0));
        assert_eq!(a.max(), p(3.0, 4.0, 5.0));
    }

    #[test]
    fn overlap_cases() {
        let a = Aabb3::from_points(p(0.0, 0.0, 0.0), p(2.0, 2.0, 2.0));
        let touching = Aabb3::from_points(p(2.0, 0.0, 0.0), p(4.0, 2.0, 2.0));
        let disjoint = Aabb3::from_points(p(3.0, 3.0, 3.0), p(4.0, 4.0, 4.0));
        assert!(a.overlaps(&touching));
        assert!(!a.overlaps(&disjoint));
    }

    #[test]
    fn contains_point_boundary_inclusive() {
        let a = Aabb3::from_points(p(0.0, 0.0, 0.0), p(2.0, 2.0, 2.0));
        assert!(a.contains_point(p(0.0, 0.0, 0.0)));
        assert!(a.contains_point(p(2.0, 2.0, 2.0)));
        assert!(!a.contains_point(p(2.1, 1.0, 1.0)));
    }
}