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
use crate::bounding_volume::AABB;
use crate::math::{Isometry, Point};
use na::{self, Real};
use crate::partitioning::{BestFirstBVVisitStatus, BestFirstDataVisitStatus, BestFirstVisitor};
use crate::query::{PointProjection, PointQuery};
use crate::shape::CompositeShape;

/// Best-fisrt traversal visitor for computin the point closest to a composite shape.
pub struct CompositeClosestPointVisitor<'a, N: 'a + Real, S: 'a + CompositeShape<N>> {
    shape: &'a S,
    point: &'a Point<N>,
    solid: bool,
}

impl<'a, N: Real, S: CompositeShape<N>> CompositeClosestPointVisitor<'a, N, S> {
    /// Initializes a visitor that allows the computation of the point closest to `point` on `shape`.
    pub fn new(shape: &'a S, point: &'a Point<N>, solid: bool) -> Self {
        CompositeClosestPointVisitor {
            shape,
            point,
            solid,
        }
    }
}

impl<'a, N: Real, S: CompositeShape<N> + PointQuery<N>> BestFirstVisitor<N, usize, AABB<N>>
    for CompositeClosestPointVisitor<'a, N, S>
{
    type Result = PointProjection<N>;

    fn visit_bv(&mut self, aabb: &AABB<N>) -> BestFirstBVVisitStatus<N> {
        BestFirstBVVisitStatus::ContinueWithCost(aabb.distance_to_point(
            &Isometry::identity(),
            self.point,
            true,
        ))
    }

    fn visit_data(&mut self, b: &usize) -> BestFirstDataVisitStatus<N, PointProjection<N>> {
        let mut res = BestFirstDataVisitStatus::Continue;

        self.shape.map_part_at(*b, &Isometry::identity(), &mut |objm, obj| {
            let proj = obj.project_point(objm, self.point, self.solid);
            res = BestFirstDataVisitStatus::ContinueWithResult(
                na::distance(self.point, &proj.point),
                proj,
            );
        });

        res
    }
}