From b39c7e89b10ffb3e122cb3583f07029960978d5f Mon Sep 17 00:00:00 2001 From: Matthew Gordon Date: Sat, 4 Jul 2020 23:16:22 -0400 Subject: [PATCH] Some cleanup of BVH and BinaryTree --- src/raycasting/bounding_volume_hierarchy.rs | 73 ++------------------- src/util/binary_tree.rs | 20 +++++- 2 files changed, 22 insertions(+), 71 deletions(-) diff --git a/src/raycasting/bounding_volume_hierarchy.rs b/src/raycasting/bounding_volume_hierarchy.rs index 72a12ce..6252235 100644 --- a/src/raycasting/bounding_volume_hierarchy.rs +++ b/src/raycasting/bounding_volume_hierarchy.rs @@ -19,8 +19,7 @@ use std::mem::swap; /// Each node knows the overall bounds of all it's children, which means that a ray that /// doesn't intersect the [BoundingBox](BoundingBox) of the node doesn't intersect any of /// the primitives stored in it's children. -pub type BoundingVolumeHierarchy = - BinaryTree, (BoundingBox, Box>)>; +pub type BoundingVolumeHierarchy = BinaryTree, Box>>; fn centre(bounds: &BoundingBox) -> Point3 { let two = convert(2.0); @@ -75,9 +74,7 @@ impl BoundingVolumeHierarchy { let mut primitive = None; swap(primitive_src, &mut primitive); let primitive = primitive.unwrap(); - BoundingVolumeHierarchy::Leaf { - value: (bounds, primitive), - } + BoundingVolumeHierarchy::Leaf { value: primitive } } else { BoundingVolumeHierarchy::None } @@ -90,22 +87,10 @@ impl BoundingVolumeHierarchy { left: _, right: _, } => *value, - BoundingVolumeHierarchy::Leaf { value: (bounds, _) } => *bounds, + BoundingVolumeHierarchy::Leaf { value } => value.bounding_box(), BoundingVolumeHierarchy::None => BoundingBox::empty(), } } - - pub fn count_leaves(&self) -> usize { - match self { - Self::Branch { - value: _, - left, - right, - } => right.count_leaves() + left.count_leaves(), - Self::Leaf { value: _ } => 1, - Self::None => 0, - } - } } fn closest_intersection( @@ -140,9 +125,7 @@ impl Intersect for BoundingVolumeHierarchy { None } } - Self::Leaf { - value: (_, primitive), - } => primitive.intersect(ray), + Self::Leaf { value: primitive } => primitive.intersect(ray), Self::None => None, } } @@ -156,54 +139,6 @@ impl HasBoundingBox for BoundingVolumeHierarchy { impl Aggregate for BoundingVolumeHierarchy {} -pub struct FilterIterator<'a, T: Real> { - unsearched_subtrees: Vec<&'a BoundingVolumeHierarchy>, - predicate: Box) -> bool>, -} - -impl<'a, T: Real> FilterIterator<'a, T> { - pub fn new( - root: &'a BoundingVolumeHierarchy, - predicate: Box) -> bool>, - ) -> Self { - FilterIterator { - unsearched_subtrees: vec![root], - predicate, - } - } -} - -impl<'a, T: Real> Iterator for FilterIterator<'a, T> { - type Item = &'a dyn Primitive; - - fn next(&mut self) -> Option { - //let mut result = Option::None; - while let Some(next_subtree) = self.unsearched_subtrees.pop() { - match next_subtree { - BoundingVolumeHierarchy::Branch { - value: bounds, - left, - right, - } => { - if (self.predicate)(bounds) { - self.unsearched_subtrees.push(right); - self.unsearched_subtrees.push(left); - } - } - BoundingVolumeHierarchy::Leaf { - value: (bounds, ref primitive), - } => { - if (self.predicate)(bounds) { - return Some(&**primitive); - } - } - BoundingVolumeHierarchy::None => {} - } - } - Option::None - } -} - #[cfg(test)] mod test { diff --git a/src/util/binary_tree.rs b/src/util/binary_tree.rs index 0e4a920..c073980 100644 --- a/src/util/binary_tree.rs +++ b/src/util/binary_tree.rs @@ -1,6 +1,7 @@ -pub enum BinaryTree { +pub enum BinaryTree +{ Branch { - value: BranchValue, + value: Value, left: Box, right: Box, }, @@ -9,3 +10,18 @@ pub enum BinaryTree { }, None, } + +impl BinaryTree +{ + pub fn count_leaves(&self) -> usize { + match self { + Self::Branch { + value: _, + left, + right, + } => right.count_leaves() + left.count_leaves(), + Self::Leaf { value: _ } => 1, + Self::None => 0, + } + } +}