ncollide3d/query/distance/
distance_support_map_support_map.rs

1use crate::math::{Isometry, Vector};
2use crate::query::algorithms::VoronoiSimplex;
3use crate::query::algorithms::{gjk, gjk::GJKResult, CSOPoint};
4use crate::shape::SupportMap;
5use na::{self, RealField, Unit};
6
7/// Distance between support-mapped shapes.
8pub fn distance_support_map_support_map<N, G1: ?Sized, G2: ?Sized>(
9    m1: &Isometry<N>,
10    g1: &G1,
11    m2: &Isometry<N>,
12    g2: &G2,
13) -> N
14where
15    N: RealField + Copy,
16    G1: SupportMap<N>,
17    G2: SupportMap<N>,
18{
19    distance_support_map_support_map_with_params(m1, g1, m2, g2, &mut VoronoiSimplex::new(), None)
20}
21
22/// Distance between support-mapped shapes.
23///
24/// This allows a more fine grained control other the underlying GJK algorigtm.
25pub fn distance_support_map_support_map_with_params<N, G1: ?Sized, G2: ?Sized>(
26    m1: &Isometry<N>,
27    g1: &G1,
28    m2: &Isometry<N>,
29    g2: &G2,
30    simplex: &mut VoronoiSimplex<N>,
31    init_dir: Option<Vector<N>>,
32) -> N
33where
34    N: RealField + Copy,
35    G1: SupportMap<N>,
36    G2: SupportMap<N>,
37{
38    // FIXME: or m2.translation - m1.translation ?
39    let dir = init_dir.unwrap_or_else(|| m1.translation.vector - m2.translation.vector);
40
41    if let Some(dir) = Unit::try_new(dir, N::default_epsilon()) {
42        simplex.reset(CSOPoint::from_shapes(m1, g1, m2, g2, &dir));
43    } else {
44        simplex.reset(CSOPoint::from_shapes(m1, g1, m2, g2, &Vector::x_axis()));
45    }
46
47    match gjk::closest_points(m1, g1, m2, g2, N::max_value().unwrap(), true, simplex) {
48        GJKResult::Intersection => N::zero(),
49        GJKResult::ClosestPoints(p1, p2, _) => na::distance(&p1, &p2),
50        GJKResult::Proximity(_) => unreachable!(),
51        GJKResult::NoIntersection(_) => N::zero(), // FIXME: GJK did not converge.
52    }
53}