parry3d 0.26.0

3 dimensional collision detection library in Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use super::BvhNode;
use crate::math::Real;
use crate::partitioning::Bvh;
use smallvec::SmallVec;

const TRAVERSAL_STACK_SIZE: usize = 32;

pub struct Leaves<'a, Check: Fn(&BvhNode) -> bool> {
    tree: &'a Bvh,
    next: Option<&'a BvhNode>,
    stack: SmallVec<[&'a BvhNode; TRAVERSAL_STACK_SIZE]>,
    check: Check,
}

impl<'a, Check: Fn(&BvhNode) -> bool> Leaves<'a, Check> {
    pub fn new(tree: &'a Bvh, check: Check) -> Leaves<'a, Check> {
        let mut stack = SmallVec::default();
        let mut next = None;

        if let Some(root) = tree.nodes.first() {
            if check(&root.left) {
                next = Some(&root.left);
            }

            if root.right.leaf_count() > 0 && check(&root.right) {
                stack.push(&root.right);
            }
        }

        Leaves {
            tree,
            next,
            stack,
            check,
        }
    }
}

impl<'a, Check: Fn(&BvhNode) -> bool> Iterator for Leaves<'a, Check> {
    type Item = u32;
    fn next(&mut self) -> Option<Self::Item> {
        loop {
            if self.next.is_none() {
                self.next = self.stack.pop();
            }

            let node = self.next.take()?;

            if node.is_leaf() {
                return Some(node.children);
            }

            let children = &self.tree.nodes[node.children as usize];
            let left = &children.left;
            let right = &children.right;

            if (self.check)(left) {
                self.next = Some(left);
            }

            if (self.check)(right) {
                if self.next.is_none() {
                    self.next = Some(right);
                } else {
                    self.stack.push(right);
                }
            }
        }
    }
}

/// Cost associated to a BVH leaf during best-first traversal.
pub trait BvhLeafCost {
    /// The cost value associated to the leaf.
    ///
    /// Best-first searches for the leaf with the lowest cost.
    fn cost(&self) -> Real;
}

impl BvhLeafCost for Real {
    #[inline(always)]
    fn cost(&self) -> Real {
        *self
    }
}

impl<T> BvhLeafCost for (Real, T) {
    #[inline(always)]
    fn cost(&self) -> Real {
        self.0
    }
}

impl Bvh {
    /// Iterates through the leaves, in depth-first order.
    ///
    /// The `check_node` closure is called on every traversed node. If it returns `false` then the
    /// node and all its descendants won’t be iterated on. This is useful for pruning whole
    /// sub-trees based on a geometric predicate on the node’s AABB.
    ///
    /// See also the [`Bvh::traverse`] function which is slightly less convenient since it doesn’t
    /// rely on the iterator system, but takes a closure that implements [`FnMut`] instead of [`Fn`].
    pub fn leaves<F: Fn(&BvhNode) -> bool>(&self, check_node: F) -> Leaves<'_, F> {
        Leaves::new(self, check_node)
    }
}

/// Controls the execution flow of [`Bvh::traverse`].
pub enum TraversalAction {
    /// The traversal will continue on the children of the tested node.
    Continue,
    /// The traversal will skip all descendants of the tested node.
    Prune,
    /// The traversal will exit immediately.
    EarlyExit,
}

impl Bvh {
    #[inline(always)]
    pub(crate) fn traversal_stack() -> SmallVec<[u32; 32]> {
        Default::default()
    }

    /// Traverses the BVH in depth-first order with full control over traversal.
    ///
    /// This method provides low-level control over tree traversal. For each node visited,
    /// it calls your closure which can decide whether to continue traversing, prune that
    /// subtree, or exit early.
    ///
    /// # Arguments
    ///
    /// * `check_node` - A mutable closure called for each node. It receives a [`BvhNode`]
    ///   and returns a [`TraversalAction`] to control the traversal:
    ///   - `Continue`: Visit this node's children (if not a leaf)
    ///   - `Prune`: Skip this node's subtree entirely
    ///   - `EarlyExit`: Stop traversal immediately
    ///
    /// # Traversal Order
    ///
    /// The tree is traversed in **depth-first** order:
    /// 1. Check current node with `check_node`
    /// 2. If `Continue`, descend into left child first
    /// 3. Then descend into right child
    /// 4. Backtrack when both subtrees are processed
    ///
    /// This order is cache-friendly and matches the tree's memory layout.
    ///
    /// # When to Use
    ///
    /// Use `traverse` when you need:
    /// - **Mutable state** during traversal
    /// - **Early exit** conditions
    /// - **Full control** over pruning decisions
    /// - **Custom query logic** that doesn't fit standard patterns
    ///
    /// For simpler cases, consider:
    /// - [`leaves`](Self::leaves) - Iterator over leaves matching a predicate
    /// - [`intersect_aabb`](Self::intersect_aabb) - Find leaves intersecting an AABB
    /// - [`cast_ray`](Self::cast_ray) - Ray casting queries
    ///
    /// # Performance
    ///
    /// - **Best case**: O(log n) when pruning effectively
    /// - **Worst case**: O(n) when visiting all nodes
    /// - Cache-friendly due to depth-first order
    /// - No allocations beyond a small stack (~32 entries)
    ///
    /// # Examples
    ///
    /// ## Count leaves in a region
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::partitioning::{Bvh, BvhBuildStrategy, TraversalAction};
    /// use parry3d::bounding_volume::{Aabb, BoundingVolume};
    /// use parry3d::math::Vector;
    ///
    /// let aabbs = vec![
    ///     Aabb::new(Vector::ZERO, Vector::new(1.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(5.0, 0.0, 0.0), Vector::new(6.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(10.0, 0.0, 0.0), Vector::new(11.0, 1.0, 1.0)),
    /// ];
    ///
    /// let bvh = Bvh::from_leaves(BvhBuildStrategy::default(), &aabbs);
    ///
    /// let query_region = Aabb::new(
    ///     Vector::new(-1.0, -1.0, -1.0),
    ///     Vector::new(7.0, 2.0, 2.0)
    /// );
    ///
    /// let mut count = 0;
    /// bvh.traverse(|node| {
    ///     if !node.aabb().intersects(&query_region) {
    ///         // Prune: this entire subtree is outside the region
    ///         return TraversalAction::Prune;
    ///     }
    ///
    ///     if node.is_leaf() {
    ///         count += 1;
    ///     }
    ///
    ///     TraversalAction::Continue
    /// });
    ///
    /// assert_eq!(count, 2); // First two objects intersect the region
    /// # }
    /// ```
    ///
    /// ## Find first leaf matching a condition
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::partitioning::{Bvh, BvhBuildStrategy, TraversalAction};
    /// use parry3d::bounding_volume::Aabb;
    /// use parry3d::math::Vector;
    ///
    /// let aabbs = vec![
    ///     Aabb::new(Vector::ZERO, Vector::new(1.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(5.0, 0.0, 0.0), Vector::new(6.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(10.0, 0.0, 0.0), Vector::new(11.0, 1.0, 1.0)),
    /// ];
    ///
    /// let bvh = Bvh::from_leaves(BvhBuildStrategy::default(), &aabbs);
    ///
    /// let target_point = Vector::new(5.5, 0.5, 0.5);
    /// let mut found_leaf = None;
    ///
    /// bvh.traverse(|node| {
    ///     if !node.aabb().contains_local_point(target_point) {
    ///         return TraversalAction::Prune;
    ///     }
    ///
    ///     if let Some(leaf_id) = node.leaf_data() {
    ///         found_leaf = Some(leaf_id);
    ///         return TraversalAction::EarlyExit; // Found it, stop searching
    ///     }
    ///
    ///     TraversalAction::Continue
    /// });
    ///
    /// assert_eq!(found_leaf, Some(1));
    /// # }
    /// ```
    ///
    /// ## Collect statistics with mutable state
    ///
    /// ```
    /// # #[cfg(all(feature = "dim3", feature = "f32"))] {
    /// use parry3d::partitioning::{Bvh, BvhBuildStrategy, TraversalAction};
    /// use parry3d::bounding_volume::Aabb;
    /// use parry3d::math::Vector;
    ///
    /// let aabbs = vec![
    ///     Aabb::new(Vector::ZERO, Vector::new(1.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(2.0, 0.0, 0.0), Vector::new(3.0, 1.0, 1.0)),
    ///     Aabb::new(Vector::new(4.0, 0.0, 0.0), Vector::new(5.0, 1.0, 1.0)),
    /// ];
    ///
    /// let bvh = Bvh::from_leaves(BvhBuildStrategy::default(), &aabbs);
    ///
    /// let mut stats = (0, 0); // (num_internal_nodes, num_leaves)
    ///
    /// bvh.traverse(|node| {
    ///     if node.is_leaf() {
    ///         stats.1 += 1;
    ///     } else {
    ///         stats.0 += 1;
    ///     }
    ///     TraversalAction::Continue
    /// });
    ///
    /// assert_eq!(stats.1, 3); // 3 leaves
    /// # }
    /// ```
    ///
    /// # Notes
    ///
    /// - The closure is called for **every** visited node (internal and leaf)
    /// - Pruning a node skips all its descendants
    /// - Use `node.is_leaf()` to distinguish leaves from internal nodes
    /// - Use `node.leaf_data()` to get the leaf's index (returns `None` for internal nodes)
    /// - The traversal uses a small fixed-size stack internally
    ///
    /// # See Also
    ///
    /// - [`leaves`](Self::leaves) - Simpler iterator interface for leaf traversal
    /// - [`intersect_aabb`](Self::intersect_aabb) - Specialized AABB intersection query
    /// - [`cast_ray`](Self::cast_ray) - Ray casting with best-first traversal
    /// - [`TraversalAction`] - Controls traversal flow
    pub fn traverse(&self, mut check_node: impl FnMut(&BvhNode) -> TraversalAction) {
        let mut stack = Self::traversal_stack();
        let mut curr_id = 0;

        if self.nodes.is_empty() {
            return;
        } else if self.nodes[0].right.leaf_count() == 0 {
            // Special case for partial root.
            let _ = check_node(&self.nodes[0].left);
            return;
        }

        loop {
            let node = &self.nodes[curr_id as usize];
            let left = &node.left;
            let right = &node.right;
            let go_left = match check_node(left) {
                TraversalAction::Continue => !left.is_leaf(),
                TraversalAction::Prune => false,
                TraversalAction::EarlyExit => return,
            };
            let go_right = match check_node(right) {
                TraversalAction::Continue => !right.is_leaf(),
                TraversalAction::Prune => false,
                TraversalAction::EarlyExit => return,
            };

            match (go_left, go_right) {
                (true, true) => {
                    curr_id = left.children;
                    stack.push(right.children);
                }
                (true, false) => curr_id = left.children,
                (false, true) => curr_id = right.children,
                (false, false) => {
                    let Some(next) = stack.pop() else {
                        return;
                    };
                    curr_id = next;
                }
            }
        }
    }

    /// Find the leaf that minimizes their associated cost.
    pub fn find_best<L: BvhLeafCost>(
        &self,
        max_cost: Real,
        aabb_cost: impl Fn(&BvhNode, Real) -> Real,
        leaf_cost: impl Fn(u32, Real) -> Option<L>,
    ) -> Option<(u32, L)> {
        // A stack with 32 elements should be more than enough in most cases.
        let mut stack = Self::traversal_stack();
        let mut best_val = None;
        let mut best_cost = max_cost;
        let mut best_id = u32::MAX;
        let mut curr_id = 0;

        if self.nodes.is_empty() {
            return None;
        } else if self.nodes[0].right.leaf_count() == 0 {
            // Special case for partial root.
            let leaf = &self.nodes[0].left;
            if aabb_cost(leaf, max_cost) < max_cost {
                let cost = leaf_cost(leaf.children, best_cost)?;
                return (cost.cost() < max_cost).then_some((leaf.children, cost));
            } else {
                return None;
            }
        }

        loop {
            let node = &self.nodes[curr_id as usize];
            let mut left = &node.left;
            let mut right = &node.right;

            let mut left_score = aabb_cost(left, best_cost);
            let mut right_score = aabb_cost(right, best_cost);

            if left_score > right_score {
                core::mem::swap(&mut left_score, &mut right_score);
                core::mem::swap(&mut left, &mut right);
            }

            let mut found_next = false;
            if left_score < best_cost && left_score != Real::MAX {
                if left.is_leaf() {
                    if let Some(primitive_val) = leaf_cost(left.children, best_cost) {
                        let primitive_score = primitive_val.cost();
                        if primitive_score < best_cost {
                            best_val = Some(primitive_val);
                            best_cost = primitive_score;
                            best_id = left.children;
                        }
                    }
                } else {
                    curr_id = left.children;
                    found_next = true;
                }
            }

            if right_score < best_cost && right_score != Real::MAX {
                if right.is_leaf() {
                    if let Some(primitive_val) = leaf_cost(right.children, best_cost) {
                        let primitive_score = primitive_val.cost();
                        if primitive_score < best_cost {
                            best_val = Some(primitive_val);
                            best_cost = primitive_score;
                            best_id = right.children;
                        }
                    }
                } else if found_next {
                    stack.push(right.children);
                } else {
                    curr_id = right.children;
                    found_next = true;
                }
            }

            if !found_next {
                if let Some(next) = stack.pop() {
                    curr_id = next;
                } else {
                    return best_val.map(|val| (best_id, val));
                }
            }
        }
    }
}