pub struct Bvh { /* private fields */ }Expand description
A flat-array AABB tree for spatial queries.
Implementations§
Source§impl Bvh
impl Bvh
Sourcepub fn build(aabbs: &[(usize, Aabb3)]) -> Self
pub fn build(aabbs: &[(usize, Aabb3)]) -> Self
Build a BVH from a set of (primitive_id, aabb) pairs.
Uses SAH-based construction for good query performance. If aabbs
is empty, returns an empty BVH.
Sourcepub fn query_overlap(&self, test: &Aabb3) -> Vec<usize>
pub fn query_overlap(&self, test: &Aabb3) -> Vec<usize>
Query all primitives whose AABB overlaps test.
Sourcepub fn query_overlap_into(&self, test: &Aabb3, results: &mut Vec<usize>)
pub fn query_overlap_into(&self, test: &Aabb3, results: &mut Vec<usize>)
Query primitives overlapping test, reusing caller-provided buffers.
results is cleared before use. This avoids per-call allocation when
called in a tight loop (e.g. compound boolean inner loops).
Sourcepub fn query_ray(&self, origin: Point3, dir: Vec3) -> Vec<usize>
pub fn query_ray(&self, origin: Point3, dir: Vec3) -> Vec<usize>
Query all primitives whose AABB is intersected by a ray.
origin is the ray start, dir is the ray direction (need not be
normalised). Only positive-t hits are returned.
Sourcepub fn query_ray_into(
&self,
origin: Point3,
dir: Vec3,
results: &mut Vec<usize>,
)
pub fn query_ray_into( &self, origin: Point3, dir: Vec3, results: &mut Vec<usize>, )
Query ray-intersecting primitives, reusing caller-provided buffers.
results is cleared before use. This avoids per-call allocation when
called in a tight loop.
Sourcepub fn query_closest(&self, point: Point3) -> Option<usize>
pub fn query_closest(&self, point: Point3) -> Option<usize>
Find the primitive whose AABB is closest to point.
Note: This returns the primitive with the closest AABB, which is
a lower bound on the actual distance. For exact closest-primitive
queries, use Bvh::query_closest_with_distance with a callback that
computes the true distance to each primitive.
Returns None if the BVH is empty.
Sourcepub fn query_closest_with_distance(
&self,
point: Point3,
distance_sq: &dyn Fn(usize) -> f64,
) -> Option<usize>
pub fn query_closest_with_distance( &self, point: Point3, distance_sq: &dyn Fn(usize) -> f64, ) -> Option<usize>
Find the closest primitive to point using an exact distance callback.
Unlike Bvh::query_closest, this uses the provided distance_sq callback
to compute the actual squared distance from point to each candidate
primitive, ensuring the true closest primitive is returned. The BVH
AABB distances are used only for pruning, so distant subtrees are
skipped efficiently.
Returns None if the BVH is empty.