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
use na::{self, RealField, Unit};

use crate::math::{Isometry, Vector};
use crate::query::algorithms::VoronoiSimplex;
use crate::query::algorithms::{gjk, gjk::GJKResult, CSOPoint};
use crate::query::Proximity;
use crate::shape::SupportMap;

/// Proximity between support-mapped shapes (`Cuboid`, `ConvexHull`, etc.)
pub fn proximity_support_map_support_map<N, G1: ?Sized, G2: ?Sized>(
    m1: &Isometry<N>,
    g1: &G1,
    m2: &Isometry<N>,
    g2: &G2,
    margin: N,
) -> Proximity
where
    N: RealField,
    G1: SupportMap<N>,
    G2: SupportMap<N>,
{
    proximity_support_map_support_map_with_params(
        m1,
        g1,
        m2,
        g2,
        margin,
        &mut VoronoiSimplex::new(),
        None,
    )
    .0
}

/// Proximity between support-mapped shapes (`Cuboid`, `ConvexHull`, etc.)
///
/// This allows a more fine grained control other the underlying GJK algorigtm.
pub fn proximity_support_map_support_map_with_params<N, G1: ?Sized, G2: ?Sized>(
    m1: &Isometry<N>,
    g1: &G1,
    m2: &Isometry<N>,
    g2: &G2,
    margin: N,
    simplex: &mut VoronoiSimplex<N>,
    init_dir: Option<Unit<Vector<N>>>,
) -> (Proximity, Unit<Vector<N>>)
where
    N: RealField,
    G1: SupportMap<N>,
    G2: SupportMap<N>,
{
    assert!(
        margin >= na::zero(),
        "The proximity margin must be positive or zero."
    );

    let dir = if let Some(init_dir) = init_dir {
        init_dir
    } else if let Some(init_dir) = Unit::try_new(
        m2.translation.vector - m1.translation.vector,
        N::default_epsilon(),
    ) {
        init_dir
    } else {
        Vector::x_axis()
    };

    simplex.reset(CSOPoint::from_shapes(m1, g1, m2, g2, &dir));

    match gjk::closest_points(m1, g1, m2, g2, margin, false, simplex) {
        GJKResult::Intersection => (Proximity::Intersecting, dir),
        GJKResult::Proximity(dir) => (Proximity::WithinMargin, dir),
        GJKResult::NoIntersection(dir) => (Proximity::Disjoint, dir),
        GJKResult::ClosestPoints(..) => unreachable!(),
    }
}