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
use std::marker::PhantomData;
use na::Real;
use bounding_volume::BoundingVolume;

/// Visitor of Bounding Volume Trees.
pub trait BVTVisitor<B, BV> {
    /// Visits an internal node. Returns `true` if the internal node children have to be visited
    /// too.
    fn visit_internal(&mut self, &BV) -> bool;

    /// Visits a leaf.
    fn visit_leaf(&mut self, &B, &BV);
}

/// Bounding Volume Tree visitor collecting interferences with a given bounding volume.
pub struct BoundingVolumeInterferencesCollector<'a, N: 'a, B: 'a, BV: 'a> {
    bv: &'a BV,
    collector: &'a mut Vec<B>,
    _point: PhantomData<N>,
}

impl<'a, N, B, BV> BoundingVolumeInterferencesCollector<'a, N, B, BV>
where
    N: Real,
    BV: BoundingVolume<N>,
{
    /// Creates a new `BoundingVolumeInterferencesCollector`.
    #[inline]
    pub fn new(
        bv: &'a BV,
        buffer: &'a mut Vec<B>,
    ) -> BoundingVolumeInterferencesCollector<'a, N, B, BV> {
        BoundingVolumeInterferencesCollector {
            bv: bv,
            collector: buffer,
            _point: PhantomData,
        }
    }
}

impl<'a, N, B, BV> BVTVisitor<B, BV> for BoundingVolumeInterferencesCollector<'a, N, B, BV>
where
    N: Real,
    B: Clone,
    BV: BoundingVolume<N>,
{
    #[inline]
    fn visit_internal(&mut self, bv: &BV) -> bool {
        bv.intersects(self.bv)
    }

    #[inline]
    fn visit_leaf(&mut self, b: &B, bv: &BV) {
        if (self.bv as *const BV) != (bv as *const BV) && bv.intersects(self.bv) {
            self.collector.push(b.clone())
        }
    }
}