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
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::geometry::proximity_detector::{
    PrimitiveProximityDetector, ProximityDetector, ProximityPhase,
    TrimeshShapeProximityDetectorWorkspace,
};
use crate::geometry::ShapeType;
use std::any::Any;

/// Trait implemented by structures responsible for selecting a collision-detection algorithm
/// for a given pair of shapes.
pub trait ProximityDispatcher {
    /// Select the proximity detection algorithm for the given pair of primitive shapes.
    fn dispatch_primitives(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (
        PrimitiveProximityDetector,
        Option<Box<dyn Any + Send + Sync>>,
    );
    /// Select the proximity detection algorithm for the given pair of non-primitive shapes.
    fn dispatch(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (ProximityPhase, Option<Box<dyn Any + Send + Sync>>);
}

/// The default proximity dispatcher used by Rapier.
pub struct DefaultProximityDispatcher;

impl ProximityDispatcher for DefaultProximityDispatcher {
    fn dispatch_primitives(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (
        PrimitiveProximityDetector,
        Option<Box<dyn Any + Send + Sync>>,
    ) {
        match (shape1, shape2) {
            (ShapeType::Ball, ShapeType::Ball) => (
                PrimitiveProximityDetector {
                    #[cfg(feature = "simd-is-enabled")]
                    detect_proximity_simd: super::detect_proximity_ball_ball_simd,
                    detect_proximity: super::detect_proximity_ball_ball,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Cuboid, ShapeType::Cuboid) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_cuboid_cuboid,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Polygon, ShapeType::Polygon) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_polygon_polygon,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Triangle, ShapeType::Ball) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_ball_convex,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Ball, ShapeType::Triangle) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_ball_convex,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Cuboid, ShapeType::Ball) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_ball_convex,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Ball, ShapeType::Cuboid) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_ball_convex,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Triangle, ShapeType::Cuboid) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_cuboid_triangle,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            (ShapeType::Cuboid, ShapeType::Triangle) => (
                PrimitiveProximityDetector {
                    detect_proximity: super::detect_proximity_cuboid_triangle,
                    ..PrimitiveProximityDetector::default()
                },
                None,
            ),
            _ => (PrimitiveProximityDetector::default(), None),
        }
    }

    fn dispatch(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (ProximityPhase, Option<Box<dyn Any + Send + Sync>>) {
        match (shape1, shape2) {
            (ShapeType::Trimesh, _) => (
                ProximityPhase::NearPhase(ProximityDetector {
                    detect_proximity: super::detect_proximity_trimesh_shape,
                    ..ProximityDetector::default()
                }),
                Some(Box::new(TrimeshShapeProximityDetectorWorkspace::new())),
            ),
            (_, ShapeType::Trimesh) => (
                ProximityPhase::NearPhase(ProximityDetector {
                    detect_proximity: super::detect_proximity_trimesh_shape,
                    ..ProximityDetector::default()
                }),
                Some(Box::new(TrimeshShapeProximityDetectorWorkspace::new())),
            ),
            _ => {
                let (gen, workspace) = self.dispatch_primitives(shape1, shape2);
                (ProximityPhase::ExactPhase(gen), workspace)
            }
        }
    }
}