Add brush AABB generation

This commit is contained in:
Jarrod Doyle 2023-09-22 12:02:33 +01:00
parent 7cb3e59329
commit bcf73c0251
Signed by: Jayrude
GPG Key ID: 38B57B16E7C0ADF7
3 changed files with 53 additions and 0 deletions

44
csg/src/aabb.rs Normal file
View File

@ -0,0 +1,44 @@
use glam::Vec3;
#[derive(Default, Debug)]
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)
}
}

View File

@ -1,6 +1,7 @@
use glam::Vec3; use glam::Vec3;
use crate::{ use crate::{
aabb::Aabb,
math, math,
plane::{Plane, PlaneIntersection}, plane::{Plane, PlaneIntersection},
}; };
@ -40,6 +41,7 @@ pub struct Brush {
pub planes: Vec<BrushPlane>, pub planes: Vec<BrushPlane>,
raw_vertices: Vec<PlaneIntersection>, raw_vertices: Vec<PlaneIntersection>,
vertices: Vec<Vec3>, vertices: Vec<Vec3>,
aabb: Aabb,
} }
impl Brush { impl Brush {
@ -48,6 +50,7 @@ impl Brush {
planes: planes.to_vec(), planes: planes.to_vec(),
raw_vertices: vec![], raw_vertices: vec![],
vertices: vec![], vertices: vec![],
aabb: Aabb::new(),
} }
} }
@ -58,6 +61,7 @@ impl Brush {
self.calculate_raw_vertices(); self.calculate_raw_vertices();
self.merge_vertices(); self.merge_vertices();
self.aabb = Aabb::from_positions(&self.vertices);
} }
pub fn get_vertices(&self) -> Vec<Vec3> { pub fn get_vertices(&self) -> Vec<Vec3> {
@ -68,6 +72,10 @@ impl Brush {
self.raw_vertices.clone() self.raw_vertices.clone()
} }
pub fn get_aabb(&self) -> &Aabb {
&self.aabb
}
fn calculate_raw_vertices(&mut self) { fn calculate_raw_vertices(&mut self) {
// Test all permutations of brush planes for intersections // Test all permutations of brush planes for intersections
let mut intersections = vec![]; let mut intersections = vec![];

View File

@ -1,3 +1,4 @@
mod aabb;
pub mod brush; pub mod brush;
pub mod math; pub mod math;
pub mod plane; pub mod plane;