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
use crate::math::Isometry;
use crate::partitioning::{VisitStatus, Visitor};
use crate::query::{Ray, RayCast};
use na::RealField;

/// Bounding Volume Tree visitor collecting interferences with a given ray.
pub struct RayInterferencesCollector<'a, N: 'a + RealField, T: 'a> {
    /// Ray to be tested.
    pub ray: &'a Ray<N>,
    /// The maximum allowed time of impact.
    pub max_toi: N,
    /// The data contained by the nodes which bounding volume intersects `self.ray`.
    pub collector: &'a mut Vec<T>,
}

impl<'a, N: RealField, T> RayInterferencesCollector<'a, N, T> {
    /// Creates a new `RayInterferencesCollector`.
    #[inline]
    pub fn new(
        ray: &'a Ray<N>,
        max_toi: N,
        buffer: &'a mut Vec<T>,
    ) -> RayInterferencesCollector<'a, N, T> {
        RayInterferencesCollector {
            ray,
            max_toi,
            collector: buffer,
        }
    }
}

impl<'a, N, T, BV> Visitor<T, BV> for RayInterferencesCollector<'a, N, T>
where
    N: RealField,
    T: Clone,
    BV: RayCast<N>,
{
    #[inline]
    fn visit(&mut self, bv: &BV, t: Option<&T>) -> VisitStatus {
        if bv.intersects_ray(&Isometry::identity(), self.ray, self.max_toi) {
            if let Some(t) = t {
                self.collector.push(t.clone())
            }

            VisitStatus::Continue
        } else {
            VisitStatus::Stop
        }
    }
}