ncollide3d/pipeline/broad_phase/
broad_phase.rs

1use na::RealField;
2use std::any::Any;
3
4use crate::math::Point;
5use crate::query::{Ray, RayIntersection};
6
7#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct BroadPhaseProxyHandle(pub usize);
9
10impl BroadPhaseProxyHandle {
11    #[inline]
12    pub fn invalid() -> Self {
13        BroadPhaseProxyHandle(usize::max_value())
14    }
15
16    #[inline]
17    pub fn is_invalid(&self) -> bool {
18        self.0 == usize::max_value()
19    }
20
21    #[inline]
22    pub fn uid(&self) -> usize {
23        self.0
24    }
25}
26
27/// Proximity handling for BroadPhase updates.
28pub trait BroadPhaseInterferenceHandler<T> {
29    /// A pre-filter that may cheaply discard objects before checking for bounding volume
30    /// interference.
31    fn is_interference_allowed(&mut self, data1: &T, data2: &T) -> bool;
32
33    /// Handle a starting interference.
34    fn interference_started(&mut self, data1: &T, data2: &T);
35
36    /// Handle a stopping interference.
37    fn interference_stopped(&mut self, data1: &T, data2: &T);
38}
39
40/// Trait all broad phase must implement.
41pub trait BroadPhase<N: RealField + Copy, BV, T>: Any + Sync + Send {
42    /// Tells the broad phase to add a bounding-volume at the next update.
43    fn create_proxy(&mut self, bv: BV, data: T) -> BroadPhaseProxyHandle;
44
45    /// Retrieves the bounding volume and data associated to the given proxy.
46    fn proxy(&self, handle: BroadPhaseProxyHandle) -> Option<(&BV, &T)>;
47
48    /// Tells the broad phase to remove the given set of handles.
49    fn remove(
50        &mut self,
51        handles: &[BroadPhaseProxyHandle],
52        removal_handler: &mut dyn FnMut(&T, &T),
53    );
54
55    /// Sets the next bounding volume to be used during the update of this broad phase.
56    fn deferred_set_bounding_volume(&mut self, handle: BroadPhaseProxyHandle, bv: BV);
57
58    /// Forces the broad-phase to recompute and re-report all the proximities with the given object.
59    fn deferred_recompute_all_proximities_with(&mut self, handle: BroadPhaseProxyHandle);
60
61    /// Forces the broad-phase to recompute and re-report all the proximities.
62    fn deferred_recompute_all_proximities(&mut self);
63
64    /// Updates the object additions, removals, and interferences detection.
65    fn update(&mut self, handler: &mut dyn BroadPhaseInterferenceHandler<T>);
66
67    /*
68     * FIXME: the following are not flexible enough.
69     */
70    // XXX: return iterators when associated types work.
71    /// Collects every object which might intersect a given bounding volume.
72    fn interferences_with_bounding_volume<'a>(&'a self, bv: &BV, out: &mut Vec<&'a T>);
73
74    /// Collects every object which might intersect a given ray.
75    fn interferences_with_ray<'a>(&'a self, ray: &Ray<N>, max_toi: N, out: &mut Vec<&'a T>);
76
77    /// Collects every object which might contain a given point.
78    fn interferences_with_point<'a>(&'a self, point: &Point<N>, out: &mut Vec<&'a T>);
79
80    fn first_interference_with_ray<'a, 'b>(
81        &'a self,
82        ray: &'b Ray<N>,
83        max_toi: N,
84        cost_fn: &'a dyn Fn(T, &'b Ray<N>, N) -> Option<(T, RayIntersection<N>)>,
85    ) -> Option<(T, RayIntersection<N>)>;
86}