From 43aa079d89ebcf7d6bb3b2e8da5a662c9a973a6b Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 8 Feb 2024 22:58:52 -0800 Subject: [PATCH] bvh reduce badness --- src/bvh.rs | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/bvh.rs b/src/bvh.rs index c16d61c..5235790 100644 --- a/src/bvh.rs +++ b/src/bvh.rs @@ -42,9 +42,13 @@ impl BvhNode{ } } -pub fn generate_bvh_node(boxen:Vec<(T,Aabb)>)->BvhNode{ +pub fn generate_bvh(boxen:Vec<(T,Aabb)>)->BvhNode{ + generate_bvh_node(boxen,false) +} + +fn generate_bvh_node(boxen:Vec<(T,Aabb)>,force:bool)->BvhNode{ let n=boxen.len(); - if n<20{ + if force||n<20{ let mut aabb=Aabb::default(); let nodes=boxen.into_iter().map(|b|{ aabb.join(&b.1); @@ -107,14 +111,19 @@ pub fn generate_bvh_node(boxen:Vec<(T,Aabb)>)->BvhNod list_list[list_id].push((i,aabb)); } let mut aabb=Aabb::default(); - let children=list_list.into_iter().map(|b|{ - let node=generate_bvh_node(b); - aabb.join(&node.aabb); - node - }).collect(); - BvhNode{ - content:BvhNodeContent::Branch(children), - aabb, + if list_list.len()==1{ + generate_bvh_node(list_list.remove(0),true) + }else{ + BvhNode{ + content:BvhNodeContent::Branch( + list_list.into_iter().map(|b|{ + let node=generate_bvh_node(b,false); + aabb.join(&node.aabb); + node + }).collect() + ), + aabb, + } } } }