diff --git a/beevee/src/aabb/bounded.rs b/beevee/src/aabb/bounded.rs new file mode 100644 index 0000000..f61112c --- /dev/null +++ b/beevee/src/aabb/bounded.rs @@ -0,0 +1,53 @@ +use super::AABB; +use crate::Point; + +/// A trait for objects that can be bounded using an [`AABB`] +/// +/// [`AABB`]: struct.AABB.html +pub trait Bounded { + /// Return the [`AABB`] surrounding self. + /// + /// [`AABB`]: struct.AABB.html + fn aabb(&self) -> AABB; +} + +/// Implementation of [`Bounded`] for [`AABB`] +/// +/// [`Bounded`]: trait.Bounded.html +/// [`AABB`]: struct.Point.html +/// +/// # Examples +/// ``` +/// use beevee::Point; +/// use beevee::aabb::{AABB, Bounded}; +/// +/// let low = Point::new(0., 0., 0.); +/// let high = Point::new(1., 2., 3.); +/// let aabb = AABB::with_bounds(low, high); +/// assert_eq!(aabb, aabb.aabb()); +/// ``` +impl Bounded for AABB { + fn aabb(&self) -> AABB { + *self + } +} + +/// Implementation of [`Bounded`] for [`Point`] +/// +/// [`Bounded`]: trait.Bounded.html +/// [`Point`]: ../type.Point.html +/// +/// # Examples +/// ``` +/// use beevee::Point; +/// use beevee::aabb::Bounded; +/// +/// let point = Point::new(1., 2., 3.); +/// let aabb = point.aabb(); +/// assert!(aabb.contains(&point)); +/// ``` +impl Bounded for Point { + fn aabb(&self) -> AABB { + AABB::with_bounds(*self, *self) + } +} diff --git a/beevee/src/aabb/mod.rs b/beevee/src/aabb/mod.rs index d469ebd..e3b0474 100644 --- a/beevee/src/aabb/mod.rs +++ b/beevee/src/aabb/mod.rs @@ -1,4 +1,7 @@ //! The module relating to Axis-Aligned Bounding Boxes. +mod bounded; +pub use bounded::*; + mod bounding_box; pub use bounding_box::*;