Skip to content

Commit

Permalink
FlatBVH and BoundingHierarchy trait
Browse files Browse the repository at this point in the history
  • Loading branch information
finnbear committed Jan 7, 2025
1 parent 6dc32bb commit f97fe5f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 16 deletions.
13 changes: 6 additions & 7 deletions src/bounding_hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ use nalgebra::{
};
use num::{Float, FromPrimitive, Signed};

use crate::aabb::Bounded;
use crate::aabb::{AabbIntersection, Bounded};
#[cfg(feature = "rayon")]
use crate::bvh::rayon_executor;
use crate::bvh::BvhNodeBuildArgs;
use crate::ray::Ray;

/// Encapsulates the required traits for the value type used in the Bvh.
pub trait BHValue:
Expand Down Expand Up @@ -240,9 +239,9 @@ pub trait BoundingHierarchy<T: BHValue, const D: usize> {
/// [`BoundingHierarchy`]: trait.BoundingHierarchy.html
/// [`Aabb`]: ../aabb/struct.Aabb.html
///
fn traverse<'a, Shape: BHShape<T, D>>(
fn traverse<'a, Query: AabbIntersection<T, D>, Shape: BHShape<T, D>>(
&'a self,
ray: &Ray<T, D>,
query: &Query,
shapes: &'a [Shape],
) -> Vec<&'a Shape>;

Expand All @@ -258,12 +257,12 @@ impl<T: BHValue, const D: usize, H: BoundingHierarchy<T, D>> BoundingHierarchy<T
Box::new(H::build(shapes))
}

fn traverse<'a, Shape: BHShape<T, D>>(
fn traverse<'a, Query: AabbIntersection<T, D>, Shape: BHShape<T, D>>(
&'a self,
ray: &Ray<T, D>,
query: &Query,
shapes: &'a [Shape],
) -> Vec<&'a Shape> {
H::traverse(self, ray, shapes)
H::traverse(self, query, shapes)
}

fn build_with_executor<
Expand Down
6 changes: 3 additions & 3 deletions src/bvh/bvh_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -410,12 +410,12 @@ impl<T: BHValue + std::fmt::Display, const D: usize> BoundingHierarchy<T, D> for
Bvh::build(shapes)
}

fn traverse<'a, Shape: Bounded<T, D>>(
fn traverse<'a, Query: AabbIntersection<T, D>, Shape: Bounded<T, D>>(
&'a self,
ray: &Ray<T, D>,
query: &Query,
shapes: &'a [Shape],
) -> Vec<&'a Shape> {
self.traverse(ray, shapes)
self.traverse(query, shapes)
}

fn pretty_print(&self) {
Expand Down
13 changes: 8 additions & 5 deletions src/flat_bvh.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
//! This module exports methods to flatten the [`Bvh`] into a [`FlatBvh`] and traverse it iteratively.
use crate::aabb::{Aabb, Bounded};
use crate::aabb::{Aabb, AabbIntersection, Bounded};
use crate::bounding_hierarchy::{BHShape, BHValue, BoundingHierarchy};
use crate::bvh::{Bvh, BvhNode};
use crate::ray::Ray;

use num::Float;

Expand Down Expand Up @@ -382,7 +381,11 @@ impl<T: BHValue + std::fmt::Display, const D: usize> BoundingHierarchy<T, D> for
/// let flat_bvh = FlatBvh::build(&mut shapes);
/// let hit_shapes = flat_bvh.traverse(&ray, &shapes);
/// ```
fn traverse<'a, B: Bounded<T, D>>(&'a self, ray: &Ray<T, D>, shapes: &'a [B]) -> Vec<&'a B> {
fn traverse<'a, Q: AabbIntersection<T, D>, B: Bounded<T, D>>(
&'a self,
query: &Q,
shapes: &'a [B],
) -> Vec<&'a B> {
let mut hit_shapes = Vec::new();
let mut index = 0;

Expand All @@ -396,13 +399,13 @@ impl<T: BHValue + std::fmt::Display, const D: usize> BoundingHierarchy<T, D> for
if node.entry_index == u32::MAX {
// If the entry_index is MAX_UINT32, then it's a leaf node.
let shape = &shapes[node.shape_index as usize];
if ray.intersects_aabb(&shape.aabb()) {
if query.intersects_aabb(&shape.aabb()) {
hit_shapes.push(shape);
}

// Exit the current node.
index = node.exit_index as usize;
} else if ray.intersects_aabb(&node.aabb) {
} else if query.intersects_aabb(&node.aabb) {
// If entry_index is not MAX_UINT32 and the Aabb test passes, then
// proceed to the node in entry_index (which goes down the bvh branch).
index = node.entry_index as usize;
Expand Down
2 changes: 1 addition & 1 deletion src/ray/ray_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ mod tests {

impl<T: BHValue, const D: usize> AabbIntersection<T, D> for Ray<T, D> {
fn intersects_aabb(&self, aabb: &Aabb<T, D>) -> bool {
self.intersects_aabb(&aabb)
self.intersects_aabb(aabb)
}
}

Expand Down

0 comments on commit f97fe5f

Please sign in to comment.