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
144
145
146
147
148
149
150
151
use crate::geometry::{Rotation, UnitComplex, UnitQuaternion};
use crate::{Const, OVector, Point, SVector, Scalar, SimdRealField, Unit};

use simba::scalar::ClosedMul;

/// Trait implemented by rotations that can be used inside of an `Isometry` or `Similarity`.
pub trait AbstractRotation<T: Scalar, const D: usize>: PartialEq + ClosedMul + Clone {
    /// The rotation identity.
    fn identity() -> Self;
    /// The rotation inverse.
    fn inverse(&self) -> Self;
    /// Change `self` to its inverse.
    fn inverse_mut(&mut self);
    /// Apply the rotation to the given vector.
    fn transform_vector(&self, v: &SVector<T, D>) -> SVector<T, D>;
    /// Apply the rotation to the given point.
    fn transform_point(&self, p: &Point<T, D>) -> Point<T, D>;
    /// Apply the inverse rotation to the given vector.
    fn inverse_transform_vector(&self, v: &OVector<T, Const<D>>) -> OVector<T, Const<D>>;
    /// Apply the inverse rotation to the given unit vector.
    fn inverse_transform_unit_vector(&self, v: &Unit<SVector<T, D>>) -> Unit<SVector<T, D>> {
        Unit::new_unchecked(self.inverse_transform_vector(&**v))
    }
    /// Apply the inverse rotation to the given point.
    fn inverse_transform_point(&self, p: &Point<T, D>) -> Point<T, D>;
}

impl<T: SimdRealField, const D: usize> AbstractRotation<T, D> for Rotation<T, D>
where
    T::Element: SimdRealField,
{
    #[inline]
    fn identity() -> Self {
        Self::identity()
    }

    #[inline]
    fn inverse(&self) -> Self {
        self.inverse()
    }

    #[inline]
    fn inverse_mut(&mut self) {
        self.inverse_mut()
    }

    #[inline]
    fn transform_vector(&self, v: &SVector<T, D>) -> SVector<T, D> {
        self * v
    }

    #[inline]
    fn transform_point(&self, p: &Point<T, D>) -> Point<T, D> {
        self * p
    }

    #[inline]
    fn inverse_transform_vector(&self, v: &SVector<T, D>) -> SVector<T, D> {
        self.inverse_transform_vector(v)
    }

    #[inline]
    fn inverse_transform_unit_vector(&self, v: &Unit<SVector<T, D>>) -> Unit<SVector<T, D>> {
        self.inverse_transform_unit_vector(v)
    }

    #[inline]
    fn inverse_transform_point(&self, p: &Point<T, D>) -> Point<T, D> {
        self.inverse_transform_point(p)
    }
}

impl<T: SimdRealField> AbstractRotation<T, 3> for UnitQuaternion<T>
where
    T::Element: SimdRealField,
{
    #[inline]
    fn identity() -> Self {
        Self::identity()
    }

    #[inline]
    fn inverse(&self) -> Self {
        self.inverse()
    }

    #[inline]
    fn inverse_mut(&mut self) {
        self.inverse_mut()
    }

    #[inline]
    fn transform_vector(&self, v: &SVector<T, 3>) -> SVector<T, 3> {
        self * v
    }

    #[inline]
    fn transform_point(&self, p: &Point<T, 3>) -> Point<T, 3> {
        self * p
    }

    #[inline]
    fn inverse_transform_vector(&self, v: &SVector<T, 3>) -> SVector<T, 3> {
        self.inverse_transform_vector(v)
    }

    #[inline]
    fn inverse_transform_point(&self, p: &Point<T, 3>) -> Point<T, 3> {
        self.inverse_transform_point(p)
    }
}

impl<T: SimdRealField> AbstractRotation<T, 2> for UnitComplex<T>
where
    T::Element: SimdRealField,
{
    #[inline]
    fn identity() -> Self {
        Self::identity()
    }

    #[inline]
    fn inverse(&self) -> Self {
        self.inverse()
    }

    #[inline]
    fn inverse_mut(&mut self) {
        self.inverse_mut()
    }

    #[inline]
    fn transform_vector(&self, v: &SVector<T, 2>) -> SVector<T, 2> {
        self * v
    }

    #[inline]
    fn transform_point(&self, p: &Point<T, 2>) -> Point<T, 2> {
        self * p
    }

    #[inline]
    fn inverse_transform_vector(&self, v: &SVector<T, 2>) -> SVector<T, 2> {
        self.inverse_transform_vector(v)
    }

    #[inline]
    fn inverse_transform_point(&self, p: &Point<T, 2>) -> Point<T, 2> {
        self.inverse_transform_point(p)
    }
}