beevee: bvh: tree: fix build panic

This commit is contained in:
Bruno BELANYI 2020-03-27 17:24:23 +01:00
parent 0e65a75e2b
commit 642f4221cd

View file

@ -405,7 +405,7 @@ fn build_node<O: Accelerated>(objects: &mut [O], begin: usize, end: usize, max_c
}; };
} }
// Calculate the SAH heuristic for this slice // Calculate the SAH heuristic for this slice
let (split, axis, cost) = compute_sah(&mut objects[begin..end], aabb.surface(), max_cap); let (split, axis, cost) = compute_sah(objects, aabb.surface(), max_cap);
// Only split if the heuristic shows that it is worth it // Only split if the heuristic shows that it is worth it
if cost >= objects.len() as f32 { if cost >= objects.len() as f32 {
return Node { return Node {
@ -415,11 +415,11 @@ fn build_node<O: Accelerated>(objects: &mut [O], begin: usize, end: usize, max_c
kind: NodeEnum::Leaf, kind: NodeEnum::Leaf,
}; };
} }
// Avoid degenerate cases, and recenter the split inside [begin, end) // Avoid degenerate cases
let split = if split == 0 || split >= (end - begin - 1) { let split = if split <= 1 || split >= (objects.len() - 1) {
begin + (end - begin) / 2 (end - begin) / 2
} else { } else {
begin + split split
}; };
// Project along chosen axis // Project along chosen axis
pdqselect::select_by(objects, split, |lhs, rhs| { pdqselect::select_by(objects, split, |lhs, rhs| {
@ -428,8 +428,18 @@ fn build_node<O: Accelerated>(objects: &mut [O], begin: usize, end: usize, max_c
.expect("Can't use Nans in the SAH computation") .expect("Can't use Nans in the SAH computation")
}); });
// Construct children recurivsely on [begin, split) and [split, end) // Construct children recurivsely on [begin, split) and [split, end)
let left = Box::new(build_node(objects, begin, split, max_cap)); let left = Box::new(build_node(
let right = Box::new(build_node(objects, split, end, max_cap)); &mut objects[0..split],
begin,
begin + split,
max_cap,
));
let right = Box::new(build_node(
&mut objects[split..],
begin + split,
end,
max_cap,
));
// Build the node recursivelly // Build the node recursivelly
Node { Node {
bounds: aabb, bounds: aabb,
@ -485,7 +495,7 @@ fn compute_sah<O: Accelerated>(
let cost = 1. / max_cap as f32 let cost = 1. / max_cap as f32
+ (left_count as f32 * left_surfaces[left_count - 1] + (left_count as f32 * left_surfaces[left_count - 1]
+ right_count as f32 * right_surfaces[right_count]) + right_count as f32 * right_surfaces[right_count - 1])
/ surface; / surface;
if cost < min { if cost < min {