ncollide3d/query/closest_points/
closest_points_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::query::ClosestPoints;
5use crate::shape::SupportMap;
6use na::{RealField, Unit};
7
8/// Closest points between support-mapped shapes (`Cuboid`, `ConvexHull`, etc.)
9pub fn closest_points_support_map_support_map<N, G1: ?Sized, G2: ?Sized>(
10    m1: &Isometry<N>,
11    g1: &G1,
12    m2: &Isometry<N>,
13    g2: &G2,
14    prediction: N,
15) -> ClosestPoints<N>
16where
17    N: RealField + Copy,
18    G1: SupportMap<N>,
19    G2: SupportMap<N>,
20{
21    match closest_points_support_map_support_map_with_params(
22        m1,
23        g1,
24        m2,
25        g2,
26        prediction,
27        &mut VoronoiSimplex::new(),
28        None,
29    ) {
30        GJKResult::ClosestPoints(pt1, pt2, _) => ClosestPoints::WithinMargin(pt1, pt2),
31        GJKResult::NoIntersection(_) => ClosestPoints::Disjoint,
32        GJKResult::Intersection => ClosestPoints::Intersecting,
33        GJKResult::Proximity(_) => unreachable!(),
34    }
35}
36
37/// Closest points between support-mapped shapes (`Cuboid`, `ConvexHull`, etc.)
38///
39/// This allows a more fine grained control other the underlying GJK algorigtm.
40pub fn closest_points_support_map_support_map_with_params<N, G1: ?Sized, G2: ?Sized>(
41    m1: &Isometry<N>,
42    g1: &G1,
43    m2: &Isometry<N>,
44    g2: &G2,
45    prediction: N,
46    simplex: &mut VoronoiSimplex<N>,
47    init_dir: Option<Vector<N>>,
48) -> GJKResult<N>
49where
50    N: RealField + Copy,
51    G1: SupportMap<N>,
52    G2: SupportMap<N>,
53{
54    let dir = match init_dir {
55        // FIXME: or m2.translation - m1.translation ?
56        None => m1.translation.vector - m2.translation.vector,
57        Some(dir) => dir,
58    };
59
60    if let Some(dir) = Unit::try_new(dir, N::default_epsilon()) {
61        simplex.reset(CSOPoint::from_shapes(m1, g1, m2, g2, &dir));
62    } else {
63        simplex.reset(CSOPoint::from_shapes(m1, g1, m2, g2, &Vector::x_axis()));
64    }
65
66    gjk::closest_points(m1, g1, m2, g2, prediction, true, simplex)
67}