Skip to main content

DynamicBvh

Struct DynamicBvh 

Source
pub struct DynamicBvh { /* private fields */ }
Expand description

Dynamic AABB Tree (DBVT) for broadphase collision detection.

Supports incremental insert, remove, and update operations. Uses the surface-area heuristic (SAH) to choose the best sibling when inserting a new leaf.

Implementations§

Source§

impl DynamicBvh

Source

pub fn new() -> Self

Create a new, empty dynamic BVH.

Source

pub fn insert(&mut self, aabb: BvhAabb, data: u32) -> usize

Insert a leaf AABB with associated data. Returns the leaf node index.

The leaf is stored with a fat AABB (expanded by FAT_MARGIN) to reduce the frequency of re-insertions when the object moves slightly.

Source

pub fn remove(&mut self, leaf: usize)

Remove a leaf node by its index.

§Panics

Panics if leaf is not a leaf node.

Source

pub fn update(&mut self, leaf: usize, new_aabb: BvhAabb) -> bool

Update an existing leaf’s AABB.

If new_aabb still fits within the leaf’s current fat AABB, the tree is unchanged and false is returned. Otherwise the leaf is removed and reinserted with a fresh fat AABB, and true is returned.

Source

pub fn query_overlapping_pairs(&self) -> Vec<(u32, u32)>

Return all leaf-data pairs (a, b) where a < b and the leaves overlap. Uses a stack-based traversal.

Source

pub fn query_aabb(&self, aabb: &BvhAabb) -> Vec<u32>

Return the data of all leaves whose fat AABB overlaps aabb.

Source

pub fn query_point(&self, point: Vec3) -> Vec<u32>

Return the data of all leaves whose fat AABB contains point.

Source

pub fn n_leaves(&self) -> usize

Number of leaf nodes currently in the tree.

Source

pub fn n_nodes(&self) -> usize

Total number of nodes (leaves + internal) currently in the tree.

Source

pub fn height(&self) -> i32

Height of the tree (0 for empty, 0 for a single leaf).

Source

pub fn validate(&self) -> bool

Validate tree invariants. Returns true if the tree is consistent.

Checks:

  • root has no parent
  • all parent/child pointers are consistent
  • internal node AABBs contain both children’s AABBs
Source§

impl DynamicBvh

Source

pub fn stats(&self) -> DbvtStats

Collect tree statistics.

Source

pub fn refit_all(&mut self)

Refit all internal-node AABBs from leaves upward (full refit pass).

Useful after bulk updates.

Source

pub fn ray_cast( &self, origin: Vec3, dir: Vec3, t_min: f64, t_max: f64, ) -> Vec<(u32, f64)>

Ray cast against all leaves; returns data of all leaves hit.

origin + dir * t for t ∈ [t_min, t_max].

Source

pub fn query_aabb_filtered( &self, aabb: &BvhAabb, filter: impl Fn(u32) -> bool, ) -> Vec<u32>

Overlap query: return data of all leaves that overlap query_aabb and whose data value satisfies filter.

This is an enhanced version of query_aabb that supports a predicate.

Source

pub fn insert_bulk(&mut self, items: &[(BvhAabb, u32)]) -> Vec<usize>

Bulk insert many (aabb, data) pairs and return their leaf indices.

Source

pub fn clear(&mut self)

Remove all leaves and reset the tree to empty.

Source

pub fn all_leaf_data(&self) -> Vec<u32>

Collect all leaf data values in the tree (arbitrary order).

Source

pub fn query_pairs_with_stats(&self) -> (Vec<(u32, u32)>, DbvtStats)

Count overlapping pairs and return both the pairs and stats.

Source§

impl DynamicBvh

Source

pub fn quality_metrics(&self) -> Option<BvhQuality>

Compute BVH quality metrics via a single DFS traversal.

Returns None if the tree is empty.

Source

pub fn quality_degraded(&self, threshold: f64) -> bool

Return true if the SAH cost is above threshold (tree quality degraded).

Useful as a trigger to rebuild the tree from scratch.

Source§

impl DynamicBvh

Source

pub fn frustum_query(&self, frustum: &BvhFrustum) -> Vec<u32>

Return the data of all leaves potentially visible inside frustum.

Uses a stack-based traversal that prunes subtrees whose AABB is fully outside any frustum plane.

Source§

impl DynamicBvh

Source

pub fn k_nearest(&self, point: Vec3, k: usize) -> Vec<NearestLeaf>

Return the k closest leaves (by AABB-center distance) to point.

Result is sorted nearest-first.

Source§

impl DynamicBvh

Source

pub fn serialize(&self) -> Vec<f64>

Serialize the entire tree into a flat Vecf64`.

Format per node (14 values): \[min_x, min_y, min_z, max_x, max_y, max_z, parent_or_neg1, left_or_neg1, right_or_neg1, data_or_neg1, height, is_leaf, is_on_free_list, PADDING]

The first element of the buffer is the total number of nodes (as f64).

Source

pub fn root(&self) -> Option<usize>

Return the root node index, or None if the tree is empty.

Source§

impl DynamicBvh

Source

pub fn move_proxy( &mut self, leaf: usize, new_aabb: BvhAabb, velocity: Vec3, dt: f64, ) -> bool

Update leaf leaf using a new tight AABB expanded in the direction of velocity * dt (speculative / predicted motion).

The resulting fat AABB covers both the current and predicted positions. Returns true if the tree was modified (re-insertion occurred).

Source§

impl DynamicBvh

Source

pub fn try_rotate(&mut self, idx: usize) -> bool

Attempt one SAH-improving rotation at node idx.

Tries swapping each grandchild with its uncle; keeps the swap only if it strictly reduces the total surface area of the two affected internal nodes.

Returns true if a rotation was performed.

Source

pub fn optimize_rotations(&mut self)

Perform one bottom-up pass of SAH-improving rotations across the tree.

Visits every non-leaf node from leaves upward and attempts try_rotate.

Source§

impl DynamicBvh

Source

pub fn refit_leaf_ancestors(&mut self, leaf: usize)

Refit only the ancestors of a specific leaf node.

More efficient than refit_all when only one leaf changed.

Source

pub fn update_leaf_inplace(&mut self, leaf: usize, new_aabb: BvhAabb)

Update a leaf’s AABB in place without re-inserting it, then refit its ancestor chain.

Only use this when new_aabb is contained within the current fat AABB; otherwise the tree’s AABBs may become incorrect. For unconstrained moves use DynamicBvh::update or DynamicBvh::move_proxy.

Source

pub fn sah_cost(&self) -> f64

Compute the total SAH cost of the tree as a single scalar.

Defined as sum(SA(internal_node)) / SA(root). Lower is better; returns 0.0 for trees with 0 or 1 nodes.

Source§

impl DynamicBvh

Source

pub fn compute_sah_cost(&self) -> f64

Compute the Surface Area Heuristic (SAH) cost of the entire tree.

This is an alias for DynamicBvh::sah_cost provided under the name requested by the algorithm expansion spec so that callers can use either name without changing existing code.

Returns 0.0 for an empty or degenerate tree.

Source

pub fn balance_rotation(&mut self) -> usize

Apply one round of balance-improving rotations to the entire tree.

Walks every internal node in bottom-up order and attempts a single SAH-improving swap (see try_rotate). Nodes are visited from shallowest to deepest so that parent improvements propagate before children are processed.

Returns the number of rotations that were accepted.

Source

pub fn traverse_frustum(&self, frustum: &BvhFrustum) -> Vec<u32>

Frustum-culling traversal: return data of all leaves whose fat AABB is potentially inside frustum.

This is a named alias for frustum_query following the algorithm expansion spec. Uses a stack-based traversal that prunes fully-outside subtrees for O(k + log n) complexity where k is the number of hits.

Source§

impl DynamicBvh

Source

pub fn node_depth(&self, idx: usize) -> Option<usize>

Return the depth of a given node in the tree (root = 0).

Returns None if idx is on the free list.

Source

pub fn find_leaf(&self, target: u32) -> Option<usize>

Return the leaf node index whose data matches target, or None.

Source

pub fn leaf_data_preorder(&self) -> Vec<u32>

Collect all leaf data in DFS pre-order (left-first).

Source

pub fn root_aabb(&self) -> Option<BvhAabb>

Return the AABB of the root node, or None if the tree is empty.

Source

pub fn max_depth(&self) -> usize

Compute the maximum tree depth by traversal.

Source

pub fn n_internal(&self) -> usize

Return the number of internal nodes (non-leaf, non-free).

Source§

impl DynamicBvh

Source

pub fn query_sphere(&self, center: Vec3, radius: f64) -> Vec<u32>

Return data of all leaves whose fat AABB overlaps a sphere defined by center and radius.

Uses the AABB-vs-sphere test: the sphere overlaps an AABB iff the squared distance from center to the nearest point on the AABB is ≤ radius².

Source

pub fn any_in_sphere(&self, center: Vec3, radius: f64) -> bool

Return true if any leaf’s fat AABB overlaps the given sphere.

Source§

impl DynamicBvh

Source

pub fn query_capsule(&self, capsule: BvhCapsule) -> Vec<u32>

Return data of all leaves whose fat AABB is hit by a capsule query.

Implemented as AABB-vs-segment-distance test: an AABB intersects the capsule iff the minimum distance from the AABB to the line segment is ≤ capsule.radius.

Source§

impl DynamicBvh

Source

pub fn rebuild(&mut self)

Rebuild the tree from scratch using the current fat AABBs of all leaves.

Removes all nodes, collects leaf data and AABBs, then re-inserts them. This is a full O(n log n) rebuild; use only when the tree is severely degraded (e.g. after many updates without rotations).

Source

pub fn leaf_snapshot(&self) -> Vec<(u32, BvhAabb)>

Return a snapshot of all leaf (data, fat_aabb) pairs without modifying the tree.

Source

pub fn leaf_aabb(&self, idx: usize) -> Option<BvhAabb>

Return the fat AABB for a given leaf idx, or None if it is not a valid leaf.

Source

pub fn remove_where(&mut self, pred: impl Fn(u32) -> bool) -> usize

Remove all leaves whose data value satisfies pred.

Returns the number of leaves removed.

Source

pub fn relabel_leaf(&mut self, leaf: usize, new_data: u32) -> bool

Replace the data value of a leaf without changing its AABB.

Returns true if the leaf was found and updated.

Source§

impl DynamicBvh

Source

pub fn subtree_leaf_count(&self, idx: usize) -> usize

Return the number of leaves in the subtree rooted at idx.

Source

pub fn validate_heights(&self) -> bool

Return true if the heights stored in nodes are consistent with the actual tree structure.

Trait Implementations§

Source§

impl Default for DynamicBvh

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> Conv for T

Source§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
Source§

impl<T> FmtForward for T

Source§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
Source§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
Source§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
Source§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
Source§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
Source§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
Source§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
Source§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
Source§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pipe for T
where T: ?Sized,

Source§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
Source§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
Source§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
Source§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
Source§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
Source§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
Source§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
Source§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T> Tap for T

Source§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
Source§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
Source§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
Source§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
Source§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
Source§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
Source§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
Source§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
Source§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
Source§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
Source§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
Source§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
Source§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
Source§

impl<T> TryConv for T

Source§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.