Skip to content

Commit

Permalink
Use fixed size arrayvec in kdtree traversal
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Oom committed Jul 7, 2024
1 parent d2055f8 commit fc2f236
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 16 deletions.
8 changes: 1 addition & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion kdtree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ version = "1.0.0"
edition = "2021"

[dependencies]
arrayvec = "0.7.4"
geometry = { version = "1.0.0", path = "../geometry" }
glam = "0.28.0"
rayon = { version = "1.10.0", default-features = false }
smallvec = "1.13.2"
textwrap = "0.16.1"
7 changes: 7 additions & 0 deletions kdtree/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ pub fn build_kdtree<B>(builder: B, max_depth: u32) -> KdTree
where
B: KdTreeBuilder,
{
if max_depth as usize > super::MAX_DEPTH {
panic!(
"Max depth ({}) must be smaller than hard coded value ({}).",
max_depth,
super::MAX_DEPTH
);
}
let root = build_helper(&builder, max_depth, 0, builder.starting_box());
builder.make_tree(root)
}
18 changes: 10 additions & 8 deletions kdtree/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
use std::{fmt::Display, ops::RangeInclusive};

use arrayvec::ArrayVec;
use geometry::{aap::Aap, geometric::Geometric, intersection::RayIntersection, ray::Ray, Geometry};
use smallvec::SmallVec;

pub mod build;
pub mod build_sah;
pub mod format;
mod split;

pub const MAX_DEPTH: usize = 20;

fn intersect_closest(
geometries: &[Geometric],
indices: &[u32],
Expand Down Expand Up @@ -105,20 +107,19 @@ impl KdNode {
let mut node = self;
let mut t1 = *t_range.start();
let mut t2 = *t_range.end();
let mut stack: SmallVec<[(&KdNode, f32, f32); 5]> = SmallVec::new();
let mut stack: ArrayVec<(&KdNode, f32, f32), MAX_DEPTH> = ArrayVec::new();
loop {
match node {
KdNode::Leaf(indices) => {
match intersect_closest(geometries, indices, ray, t1..=t2) {
Some(result) => return Some(result),
_ if t2 == *t_range.end() => return None,
_ => {
if let Some(s) = stack.pop() {
_ => match stack.pop() {
Some(s) => {
(node, t1, t2) = s;
} else {
return None;
}
}
None => return None,
},
}
}
KdNode::Node { plane, left, right } => {
Expand All @@ -134,7 +135,8 @@ impl KdNode {
} else if t < t1 {
node = far;
} else {
stack.push((far, t, t2));
let result = stack.try_push((far, t, t2));
debug_assert!(result.is_ok());
node = near;
t2 = t;
}
Expand Down

0 comments on commit fc2f236

Please sign in to comment.