From 7b826e11be342fa363f48a2afe82d8d33ee9bbad Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 25 Jul 2024 15:37:55 -0700 Subject: [PATCH] special method converts Bvh into Bvh with weights --- src/bvh.rs | 66 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/src/bvh.rs b/src/bvh.rs index e5427f9..ab73f65 100644 --- a/src/bvh.rs +++ b/src/bvh.rs @@ -10,26 +10,31 @@ use crate::aabb::Aabb; //sort the centerpoints on each axis (3 lists) //bv is put into octant based on whether it is upper or lower in each list -enum BvhNodeContent{ - Branch(Vec>), +pub enum RecursiveContent{ + Branch(Vec), Leaf(T), } -impl Default for BvhNodeContent{ +impl Default for RecursiveContent{ fn default()->Self{ Self::Branch(Vec::new()) } } #[derive(Default)] pub struct BvhNode{ - content:BvhNodeContent, + content:RecursiveContent,T>, + aabb:Aabb, +} +pub struct BvhWeightNode{ + content:RecursiveContent,T>, + weight:W, aabb:Aabb, } impl BvhNode{ pub fn the_tester(&self,aabb:&Aabb,f:&mut F){ match &self.content{ - BvhNodeContent::Leaf(model)=>f(model), - BvhNodeContent::Branch(children)=>for child in children{ + RecursiveContent::Leaf(model)=>f(model), + RecursiveContent::Branch(children)=>for child in children{ //this test could be moved outside the match statement //but that would test the root node aabb //you're probably not going to spend a lot of time outside the map, @@ -42,8 +47,47 @@ impl BvhNode{ } pub fn into_visitor(self,f:&mut F){ match self.content{ - BvhNodeContent::Leaf(model)=>f(model), - BvhNodeContent::Branch(children)=>for child in children{ + RecursiveContent::Leaf(model)=>f(model), + RecursiveContent::Branch(children)=>for child in children{ + child.into_visitor(f) + }, + } + } + pub fn weigh_contents,F:Fn(&T)->W>(self,f:&F)->BvhWeightNode{ + match self.content{ + RecursiveContent::Leaf(model)=>BvhWeightNode{ + weight:f(&model), + content:RecursiveContent::Leaf(model), + aabb:self.aabb, + }, + RecursiveContent::Branch(children)=>{ + let branch:Vec>=children.into_iter().map(|child| + child.weigh_contents(f) + ).collect(); + BvhWeightNode{ + weight:branch.iter().map(|node|node.weight).sum(), + content:RecursiveContent::Branch(branch), + aabb:self.aabb, + } + }, + } + } +} + +impl BvhWeightNode{ + pub const fn weight(&self)->&W{ + &self.weight + } + pub const fn aabb(&self)->&Aabb{ + &self.aabb + } + pub fn into_content(self)->RecursiveContent,T>{ + self.content + } + pub fn into_visitor(self,f:&mut F){ + match self.content{ + RecursiveContent::Leaf(model)=>f(model), + RecursiveContent::Branch(children)=>for child in children{ child.into_visitor(f) }, } @@ -61,12 +105,12 @@ fn generate_bvh_node(boxen:Vec<(T,Aabb)>,force:bool)->BvhNode{ let nodes=boxen.into_iter().map(|b|{ aabb.join(&b.1); BvhNode{ - content:BvhNodeContent::Leaf(b.0), + content:RecursiveContent::Leaf(b.0), aabb:b.1, } }).collect(); BvhNode{ - content:BvhNodeContent::Branch(nodes), + content:RecursiveContent::Branch(nodes), aabb, } }else{ @@ -123,7 +167,7 @@ fn generate_bvh_node(boxen:Vec<(T,Aabb)>,force:bool)->BvhNode{ generate_bvh_node(list_list.remove(0),true) }else{ BvhNode{ - content:BvhNodeContent::Branch( + content:RecursiveContent::Branch( list_list.into_iter().map(|b|{ let node=generate_bvh_node(b,false); aabb.join(&node.aabb);