-
-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0f70ecb
commit dbe1f72
Showing
8 changed files
with
199 additions
and
13 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
mod closest; | ||
mod range; | ||
mod ray; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
use std::{cmp::Reverse, collections::BinaryHeap, fmt::Debug}; | ||
|
||
use geometry::{aabb::Aabb, ray::Ray}; | ||
use ordered_float::NotNan; | ||
|
||
use crate::{Bvh, Node, utils::NodeOrd}; | ||
|
||
impl<T: Debug> Bvh<T> { | ||
/// Returns the closest element hit by the ray and the intersection distance (t) along the ray. | ||
/// | ||
/// If no element is hit, returns `None`. | ||
#[allow(clippy::excessive_nesting)] | ||
pub fn get_closest_ray( | ||
&self, | ||
ray: Ray, | ||
get_aabb: impl Fn(&T) -> Aabb, | ||
) -> Option<(&T, NotNan<f32>)> { | ||
let mut closest_t = NotNan::new(f32::INFINITY).unwrap(); | ||
let mut closest_elem = None; | ||
|
||
let root = self.root(); | ||
|
||
match root { | ||
Node::Leaf(elems) => { | ||
// Only a leaf: check all elements directly. | ||
for elem in elems { | ||
if let Some(t) = get_aabb(elem).intersect_ray(&ray) { | ||
if t < closest_t && t.into_inner() >= 0.0 { | ||
closest_t = t; | ||
closest_elem = Some(elem); | ||
} | ||
} | ||
} | ||
} | ||
Node::Internal(internal) => { | ||
let mut heap: BinaryHeap<_> = BinaryHeap::new(); | ||
|
||
// Check if the ray hits the root node's AABB | ||
if let Some(t) = internal.aabb.intersect_ray(&ray) { | ||
if t.into_inner() >= 0.0 { | ||
heap.push(Reverse(NodeOrd { | ||
node: internal, | ||
by: t, | ||
})); | ||
} | ||
} | ||
|
||
while let Some(Reverse(current)) = heap.pop() { | ||
let node = current.node; | ||
let node_t = current.by; | ||
|
||
// If the node AABB is farther than any known intersection, prune | ||
if node_t > closest_t { | ||
continue; | ||
} | ||
|
||
for child in node.children(self) { | ||
match child { | ||
Node::Internal(child_node) => { | ||
if let Some(t) = child_node.aabb.intersect_ray(&ray) { | ||
if t < closest_t && t.into_inner() >= 0.0 { | ||
heap.push(Reverse(NodeOrd { | ||
node: child_node, | ||
by: t, | ||
})); | ||
} | ||
} | ||
} | ||
Node::Leaf(elems) => { | ||
for elem in elems { | ||
if let Some(t) = get_aabb(elem).intersect_ray(&ray) { | ||
if t < closest_t && t.into_inner() >= 0.0 { | ||
closest_t = t; | ||
closest_elem = Some(elem); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
closest_elem.map(|elem| (elem, closest_t)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Seeds for failure cases proptest has generated in the past. It is | ||
# automatically read and these particular cases re-run before any | ||
# novel cases are generated. | ||
# | ||
# It is recommended to check this file in to source control so that | ||
# everyone who runs the test benefits from these saved cases. | ||
cc 6eda8ce9f67e5e83265d2d9b12b641fd177829ff6893611a8fba5fb06af97015 # shrinks to elements = [[-126.04, 0.00, -677.53] -> [0.00, 726.19, 953.13], [-694.23, 0.00, 0.00] -> [0.00, 627.63, 922.19], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, -53.49] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, -947.84] -> [0.00, 0.00, 0.00], [0.00, 0.00, -290.94] -> [0.00, 0.00, 0.00], [0.00, 0.00, -486.91] -> [0.00, 0.00, 0.00], [0.00, 0.00, -418.65] -> [0.00, 0.00, 0.00], [0.00, 0.00, -866.01] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 0.00, 0.00], [0.00, 0.00, 0.00] -> [0.00, 929.05, 0.00]], origin = (-4.9637938, 474.28943, 904.7494), direction = (0.0, 866.90247, 0.0) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters