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
137
138
139
140
141
#[cfg(feature = "dim3")]
use crate::geometry::contact_generator::PfmPfmContactManifoldGeneratorWorkspace;
use crate::geometry::contact_generator::{
    ContactGenerator, ContactGeneratorWorkspace, ContactPhase,
    HeightFieldShapeContactGeneratorWorkspace, PrimitiveContactGenerator,
    TrimeshShapeContactGeneratorWorkspace,
};
use crate::geometry::ShapeType;

/// Trait implemented by structures responsible for selecting a collision-detection algorithm
/// for a given pair of shapes.
pub trait ContactDispatcher {
    /// Select the collision-detection algorithm for the given pair of primitive shapes.
    fn dispatch_primitives(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (PrimitiveContactGenerator, Option<ContactGeneratorWorkspace>);
    /// Select the collision-detection algorithm for the given pair of non-primitive shapes.
    fn dispatch(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (ContactPhase, Option<ContactGeneratorWorkspace>);
}

/// The default contact dispatcher used by Rapier.
pub struct DefaultContactDispatcher;

impl ContactDispatcher for DefaultContactDispatcher {
    fn dispatch_primitives(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (PrimitiveContactGenerator, Option<ContactGeneratorWorkspace>) {
        match (shape1, shape2) {
            (ShapeType::Ball, ShapeType::Ball) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_ball_ball,
                    #[cfg(feature = "simd-is-enabled")]
                    generate_contacts_simd: super::generate_contacts_ball_ball_simd,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (ShapeType::Cuboid, ShapeType::Cuboid) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_cuboid_cuboid,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            // (ShapeType::Polygon, ShapeType::Polygon) => (
            //     PrimitiveContactGenerator {
            //         generate_contacts: super::generate_contacts_polygon_polygon,
            //         ..PrimitiveContactGenerator::default()
            //     },
            //     None,
            // ),
            (ShapeType::Capsule, ShapeType::Capsule) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_capsule_capsule,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (_, ShapeType::Ball) | (ShapeType::Ball, _) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_ball_convex,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (ShapeType::Capsule, ShapeType::Cuboid) | (ShapeType::Cuboid, ShapeType::Capsule) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_cuboid_capsule,
                    ..PrimitiveContactGenerator::default()
                },
                None,
            ),
            (ShapeType::Triangle, ShapeType::Cuboid) | (ShapeType::Cuboid, ShapeType::Triangle) => {
                (
                    PrimitiveContactGenerator {
                        generate_contacts: super::generate_contacts_cuboid_triangle,
                        ..PrimitiveContactGenerator::default()
                    },
                    None,
                )
            }
            #[cfg(feature = "dim3")]
            (ShapeType::Cylinder, _)
            | (_, ShapeType::Cylinder)
            | (ShapeType::Cone, _)
            | (_, ShapeType::Cone)
            | (ShapeType::RoundCylinder, _)
            | (_, ShapeType::RoundCylinder)
            | (ShapeType::Capsule, _)
            | (_, ShapeType::Capsule) => (
                PrimitiveContactGenerator {
                    generate_contacts: super::generate_contacts_pfm_pfm,
                    ..PrimitiveContactGenerator::default()
                },
                Some(ContactGeneratorWorkspace::from(
                    PfmPfmContactManifoldGeneratorWorkspace::default(),
                )),
            ),
            _ => (PrimitiveContactGenerator::default(), None),
        }
    }

    fn dispatch(
        &self,
        shape1: ShapeType,
        shape2: ShapeType,
    ) -> (ContactPhase, Option<ContactGeneratorWorkspace>) {
        match (shape1, shape2) {
            (ShapeType::Trimesh, _) | (_, ShapeType::Trimesh) => (
                ContactPhase::NearPhase(ContactGenerator {
                    generate_contacts: super::generate_contacts_trimesh_shape,
                    ..ContactGenerator::default()
                }),
                Some(ContactGeneratorWorkspace::from(
                    TrimeshShapeContactGeneratorWorkspace::new(),
                )),
            ),
            (ShapeType::HeightField, _) | (_, ShapeType::HeightField) => (
                ContactPhase::NearPhase(ContactGenerator {
                    generate_contacts: super::generate_contacts_heightfield_shape,
                    ..ContactGenerator::default()
                }),
                Some(ContactGeneratorWorkspace::from(
                    HeightFieldShapeContactGeneratorWorkspace::new(),
                )),
            ),
            _ => {
                let (gen, workspace) = self.dispatch_primitives(shape1, shape2);
                (ContactPhase::ExactPhase(gen), workspace)
            }
        }
    }
}