diff --git a/csg/src/brush.rs b/csg/src/brush.rs index ee96325..e68e0f2 100644 --- a/csg/src/brush.rs +++ b/csg/src/brush.rs @@ -56,9 +56,21 @@ impl Brush { panic!("Invalid number of planes. Minimum 4 planes required."); } + self.calculate_raw_vertices(); + self.merge_vertices(); + } + + pub fn get_vertices(&self) -> Vec { + self.vertices.clone() + } + + pub fn get_intersections(&self) -> Vec { + self.raw_vertices.clone() + } + + fn calculate_raw_vertices(&mut self) { // Test all permutations of brush planes for intersections let mut intersections = vec![]; - let mut vs: Vec = vec![]; let len = self.planes.len(); for i in 0..(len - 2) { for j in (i + 1)..(len - 1) { @@ -75,7 +87,10 @@ impl Brush { } // Validate intersections against other brush planes + // Intersections only produce vertices if they're in the half-space of every + // plane in the brush. // TODO: No need to check against planes that are part of the intersection + let mut raw_vertices = vec![]; for intersection in &intersections { let mut valid = true; for bplane in &self.planes { @@ -85,31 +100,30 @@ impl Brush { } } - // Merge vertices if valid { - let point = intersection.get_intersection_point(); - let mut duplicate = false; - for v in &vs { - if v.abs_diff_eq(point, math::EPSILON) { - duplicate = true; - break; - } - } - if !duplicate { - vs.push(point); + raw_vertices.push(*intersection); + } + } + + self.raw_vertices = raw_vertices; + } + + fn merge_vertices(&mut self) { + let mut vs: Vec = vec![]; + for intersection in &self.raw_vertices { + let point = intersection.get_intersection_point(); + let mut duplicate = false; + for v in &vs { + if v.abs_diff_eq(point, math::EPSILON) { + duplicate = true; + break; } } + if !duplicate { + vs.push(point); + } } self.vertices = vs; - self.raw_vertices = intersections; - } - - pub fn get_vertices(&self) -> Vec { - self.vertices.clone() - } - - pub fn get_intersections(&self) -> Vec { - self.raw_vertices.clone() } }