CSG-Forge/csg/src/aabb.rs

45 lines
1.1 KiB
Rust

use glam::Vec3;
#[derive(Default, Debug, Copy, Clone)]
pub struct Aabb {
pub min: Vec3,
pub max: Vec3,
}
impl Aabb {
pub fn new() -> Self {
Self {
min: Vec3::ZERO,
max: Vec3::ZERO,
}
}
pub fn from_min_max(min: Vec3, max: Vec3) -> Self {
Self { min, max }
}
pub fn from_positions(positions: &[Vec3]) -> Self {
let mut min = positions[0];
let mut max = min;
for pos in positions {
min = min.min(*pos);
max = max.max(*pos);
}
Self { min, max }
}
pub fn intersects(&self, other: &Aabb) -> bool {
(self.min.x <= other.max.x && self.max.x >= other.min.x)
&& (self.min.y <= other.max.y && self.max.y >= other.min.y)
&& (self.min.z <= other.max.z && self.max.z >= other.min.z)
}
pub fn contains(&self, other: &Aabb) -> bool {
(self.min.x <= other.min.x && self.max.x >= other.max.x)
&& (self.min.y <= other.min.y && self.max.y >= other.max.y)
&& (self.min.z <= other.min.z && self.max.z >= other.max.z)
}
}