use crate::Point3;
#[derive(Clone, Debug)]
pub struct AABB {
pub min: Point3,
pub max: Point3,
}
impl Default for AABB {
fn default() -> Self {
Self::empty()
}
}
impl AABB {
pub fn new(min: Point3, max: Point3) -> Self {
Self { min, max }
}
pub fn empty() -> Self {
Self::new(
Point3::new(f32::INFINITY, f32::INFINITY, f32::INFINITY),
Point3::new(f32::NEG_INFINITY, f32::NEG_INFINITY, f32::NEG_INFINITY),
)
}
pub fn volume(&self) -> f32 {
(self.max.x - self.min.x) * (self.max.y - self.min.y) * (self.max.z - self.min.z)
}
pub fn surface(&self) -> f32 {
let dx = self.max.x - self.min.x;
let dy = self.max.y - self.min.y;
let dz = self.max.z - self.min.z;
2.0 * (dx * dy + dx * dz + dy * dz)
}
pub fn merge(&mut self, other: &Self) {
self.min = Point3::new(
self.min.x.min(other.min.x),
self.min.y.min(other.min.y),
self.min.z.min(other.min.z),
);
self.max = Point3::new(
self.max.x.max(other.max.x),
self.max.y.max(other.max.y),
self.max.z.max(other.max.z),
);
}
}
pub trait Bounded {
fn bound(&self) -> AABB;
}