Skip to content

Commit

Permalink
Merge pull request svenstaro#139 from dashedman/fixed_ordered
Browse files Browse the repository at this point in the history
Fix distance traversing bug with heap
  • Loading branch information
finnbear authored Jan 14, 2025
2 parents ef1580b + 3de2ee1 commit 9e6b312
Show file tree
Hide file tree
Showing 4 changed files with 524 additions and 155 deletions.
55 changes: 50 additions & 5 deletions src/bvh/bvh_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ use crate::utils::joint_aabb_of_shapes;

use std::mem::MaybeUninit;

use super::{BvhNode, BvhNodeBuildArgs, DistanceTraverseIterator, ShapeIndex, Shapes};
use super::{
BvhNode, BvhNodeBuildArgs, ChildDistanceTraverseIterator, DistanceTraverseIterator, ShapeIndex,
Shapes,
};

/// The [`Bvh`] data structure. Contains the list of [`BvhNode`]s.
///
Expand Down Expand Up @@ -133,8 +136,7 @@ impl<T: BHValue, const D: usize> Bvh<T, D> {
/// Returns a subset of [`shape`], in which the [`Aabb`]s of the elements were hit by [`Ray`].
/// Return in order from nearest to farthest for ray.
///
/// This is a best-effort function that orders interior parent nodes before ordering child
/// nodes, so the output is not necessarily perfectly sorted.
/// Time complexity: for first `O(log(n))`, for all `O(n*log(n))`
///
/// [`Bvh`]: struct.Bvh.html
/// [`Aabb`]: ../aabb/struct.AABB.html
Expand All @@ -151,8 +153,7 @@ impl<T: BHValue, const D: usize> Bvh<T, D> {
/// Returns a subset of [`Shape`], in which the [`Aabb`]s of the elements were hit by [`Ray`].
/// Return in order from farthest to nearest for ray.
///
/// This is a best-effort function that orders interior parent nodes before ordering child
/// nodes, so the output is not necessarily perfectly sorted.
/// Time complexity: for first `O(log(n))`, for all `O(n*log(n))`.
///
/// [`Bvh`]: struct.Bvh.html
/// [`Aabb`]: ../aabb/struct.AABB.html
Expand All @@ -165,6 +166,50 @@ impl<T: BHValue, const D: usize> Bvh<T, D> {
DistanceTraverseIterator::new(self, ray, shapes)
}

/// Creates a [`ChildDistanceTraverseIterator`] to traverse the [`Bvh`].
/// Returns a subset of [`shape`], in which the [`Aabb`]s of the elements were hit by [`Ray`].
/// Return in order from nearest to farthest for ray.
///
/// This is a best-effort function that orders interior parent nodes before ordering child
/// nodes, so the output is not necessarily perfectly sorted.
///
/// So the output is not necessarily perfectly sorted if the children of any node overlap.
///
/// Time complexity: for first `O(log(n))`, for all `O(n)`.
///
/// [`Bvh`]: struct.Bvh.html
/// [`Aabb`]: ../aabb/struct.AABB.html
///
pub fn nearest_child_traverse_iterator<'bvh, 'shape, Shape: Bounded<T, D>>(
&'bvh self,
ray: &'bvh Ray<T, D>,
shapes: &'shape [Shape],
) -> ChildDistanceTraverseIterator<'bvh, 'shape, T, D, Shape, true> {
ChildDistanceTraverseIterator::new(self, ray, shapes)
}

/// Creates a [`ChildDistanceTraverseIterator`] to traverse the [`Bvh`].
/// Returns a subset of [`Shape`], in which the [`Aabb`]s of the elements were hit by [`Ray`].
/// Return in order from farthest to nearest for ray.
///
/// This is a best-effort function that orders interior parent nodes before ordering child
/// nodes, so the output is not necessarily perfectly sorted.
///
/// So the output is not necessarily perfectly sorted if the children of any node overlap.
///
/// Time complexity: for first `O(log(n))`, for all `O(n)`.
///
/// [`Bvh`]: struct.Bvh.html
/// [`Aabb`]: ../aabb/struct.AABB.html
///
pub fn farthest_child_traverse_iterator<'bvh, 'shape, Shape: Bounded<T, D>>(
&'bvh self,
ray: &'bvh Ray<T, D>,
shapes: &'shape [Shape],
) -> ChildDistanceTraverseIterator<'bvh, 'shape, T, D, Shape, false> {
ChildDistanceTraverseIterator::new(self, ray, shapes)
}

/// Prints the [`Bvh`] in a tree-like visualization.
///
/// [`Bvh`]: struct.Bvh.html
Expand Down
Loading

0 comments on commit 9e6b312

Please sign in to comment.