beevee: aabb: add Bounded trait implementation

This commit is contained in:
Bruno BELANYI 2020-03-24 14:25:14 +01:00
parent 58ee42d21d
commit 0ce49bb3a3
2 changed files with 56 additions and 0 deletions

View file

@ -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)
}
}

View file

@ -1,4 +1,7 @@
//! The module relating to Axis-Aligned Bounding Boxes. //! The module relating to Axis-Aligned Bounding Boxes.
mod bounded;
pub use bounded::*;
mod bounding_box; mod bounding_box;
pub use bounding_box::*; pub use bounding_box::*;