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
142
143
use na::RealField;

use crate::interpolation::RigidMotion;
use crate::math::{Isometry, Vector};
use crate::query::{self, Unsupported, TOI};
use crate::shape::Shape;

/// Dispatcher for time-of-impact queries
///
/// Custom implementations allow crates that support an abstract `TOIDispatcher` to handle custom
/// shapes. Methods take `root_dispatcher` to allow dispatchers to delegate to eachother. Callers
/// that will not themselves be used to implement a `TOIDispatcher` should pass `self`.
pub trait TOIDispatcher<N: RealField>: Send + Sync {
    /// Computes the smallest time of impact of two shapes under translational movement.
    fn nonlinear_time_of_impact(
        &self,
        root_dispatcher: &dyn TOIDispatcher<N>,
        motion1: &dyn RigidMotion<N>,
        g1: &dyn Shape<N>,
        motion2: &dyn RigidMotion<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Result<Option<TOI<N>>, Unsupported>;

    /// Computes the smallest time at with two shapes under translational movement are separated by a
    /// distance smaller or equal to `distance`.
    ///
    /// Returns `0.0` if the objects are touching or penetrating.
    fn time_of_impact(
        &self,
        root_dispatcher: &dyn TOIDispatcher<N>,
        m1: &Isometry<N>,
        vel1: &Vector<N>,
        g1: &dyn Shape<N>,
        m2: &Isometry<N>,
        vel2: &Vector<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Result<Option<TOI<N>>, Unsupported>;

    /// Construct a `TOIDispatcher` that falls back on `other` for cases not handled by `self`
    fn chain<U: TOIDispatcher<N>>(self, other: U) -> Chain<Self, U>
    where
        Self: Sized,
    {
        Chain(self, other)
    }
}

/// A dispatcher that exposes built-in queries
#[derive(Debug, Clone)]
pub struct DefaultTOIDispatcher;

impl<N: RealField> TOIDispatcher<N> for DefaultTOIDispatcher {
    fn nonlinear_time_of_impact(
        &self,
        root_dispatcher: &dyn TOIDispatcher<N>,
        motion1: &dyn RigidMotion<N>,
        g1: &dyn Shape<N>,
        motion2: &dyn RigidMotion<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Result<Option<TOI<N>>, Unsupported> {
        query::nonlinear_time_of_impact(
            root_dispatcher,
            motion1,
            g1,
            motion2,
            g2,
            max_toi,
            target_distance,
        )
    }

    fn time_of_impact(
        &self,
        root_dispatcher: &dyn TOIDispatcher<N>,
        m1: &Isometry<N>,
        vel1: &Vector<N>,
        g1: &dyn Shape<N>,
        m2: &Isometry<N>,
        vel2: &Vector<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Result<Option<TOI<N>>, Unsupported> {
        query::time_of_impact(
            root_dispatcher,
            m1,
            vel1,
            g1,
            m2,
            vel2,
            g2,
            max_toi,
            target_distance,
        )
    }
}

/// The composition of two dispatchers
pub struct Chain<T, U>(T, U);

macro_rules! chain_method {
    ($name:ident ( $( $arg:ident : $ty:ty,)*) -> $result:ty) => {
        fn $name(&self, root_dispatcher: &dyn TOIDispatcher<N>,
                 $($arg : $ty,)*
        ) -> Result<$result, Unsupported> {
            (self.0).$name(root_dispatcher, $($arg,)*)
                .or_else(|Unsupported| (self.1).$name(root_dispatcher, $($arg,)*))
        }
    }
}

impl<N, T, U> TOIDispatcher<N> for Chain<T, U>
where
    N: RealField,
    T: TOIDispatcher<N>,
    U: TOIDispatcher<N>,
{
    chain_method!(nonlinear_time_of_impact(
        motion1: &dyn RigidMotion<N>,
        g1: &dyn Shape<N>,
        motion2: &dyn RigidMotion<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Option<TOI<N>>);

    chain_method!(time_of_impact(
        m1: &Isometry<N>,
        vel1: &Vector<N>,
        g1: &dyn Shape<N>,
        m2: &Isometry<N>,
        vel2: &Vector<N>,
        g2: &dyn Shape<N>,
        max_toi: N,
        target_distance: N,
    ) -> Option<TOI<N>>);
}