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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
Conversion from quaternions to Euler rotation sequences.

From: http://bediyap.com/programming/convert-quaternion-to-euler-rotations/
*/

use super::quat::{DQuat, Quat};
use crate::core::traits::scalar::*;

/// Euler rotation sequences.
///
/// The angles are applied starting from the right.
/// E.g. XYZ will first apply the z-axis rotation.
///
/// YXZ can be used for yaw (y-axis), pitch (x-axis), roll (z-axis).
///
/// The two-axis rotations (e.g. ZYZ) are not fully tested and have to be treated with caution.
#[derive(Debug, Clone, Copy)]
pub enum EulerRot {
    /// Intrinsic three-axis rotation ZYX
    ZYX,
    /// Intrinsic three-axis rotation ZXY
    ZXY,
    /// Intrinsic three-axis rotation YXZ
    YXZ,
    /// Intrinsic three-axis rotation YZX
    YZX,
    /// Intrinsic three-axis rotation XYZ
    XYZ,
    /// Intrinsic three-axis rotation XZY
    XZY,

    /// Intrinsic two-axis rotation ZYZ
    #[deprecated(note = "Untested! Use at own risk!")]
    ZYZ,
    /// Intrinsic two-axis rotation ZXZ
    #[deprecated(note = "Untested! Use at own risk!")]
    ZXZ,
    /// Intrinsic two-axis rotation YXY
    #[deprecated(note = "Untested! Use at own risk!")]
    YXY,
    /// Intrinsic two-axis rotation YZY
    #[deprecated(note = "Untested! Use at own risk!")]
    YZY,
    /// Intrinsic two-axis rotation XYX
    #[deprecated(note = "Untested! Use at own risk!")]
    XYX,
    /// Intrinsic two-axis rotation XZX
    #[deprecated(note = "Untested! Use at own risk!")]
    XZX,
}

impl Default for EulerRot {
    /// Default YXZi as yaw (y-axis), pitch (x-axis), roll (z-axis).
    fn default() -> Self {
        Self::YXZ
    }
}

/// Conversion from quaternion to euler angles.
pub trait EulerFromQuaternion<Q: Copy>: Sized + Copy {
    type Output: FloatEx;
    /// Compute the angle of the first axis (X-x-x)
    fn first(self, q: Q) -> Self::Output;
    /// Compute then angle of the second axis (x-X-x)
    fn second(self, q: Q) -> Self::Output;
    /// Compute then angle of the third axis (x-x-X)
    fn third(self, q: Q) -> Self::Output;

    /// Compute all angles of a rotation in the notation order
    fn convert_quat(self, q: Q) -> (Self::Output, Self::Output, Self::Output) {
        (self.first(q), self.second(q), self.third(q))
    }
}

/// Conversion from euler angles to quaternion.
pub trait EulerToQuaternion<T>: Copy {
    type Output;
    /// Create the rotation quaternion for the three angles of this euler rotation sequence.
    fn new_quat(self, u: T, v: T, w: T) -> Self::Output;
}

/// Helper, until std::f32::clamp is stable.
fn clamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
    assert!(min <= max);
    if value < min {
        min
    } else if value > max {
        max
    } else {
        value
    }
}

/// Adds a atan2 that handles the negative zero case.
/// Basically forces positive zero in the x-argument for atan2.
pub trait Atan2Fixed<T = Self> {
    fn atan2_fixed(self, other: T) -> T;
}
impl Atan2Fixed for f32 {
    fn atan2_fixed(self, other: f32) -> f32 {
        self.atan2(if other == 0.0f32 { 0.0f32 } else { other })
    }
}
impl Atan2Fixed for f64 {
    fn atan2_fixed(self, other: f64) -> f64 {
        self.atan2(if other == 0.0f64 { 0.0f64 } else { other })
    }
}

macro_rules! impl_from_quat {
    ($t:ty, $quat:ident) => {
        impl EulerFromQuaternion<$quat> for EulerRot {
            type Output = $t;
            fn first(self, q: $quat) -> $t {
                use EulerRot::*;
                match self {
                    ZYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
                        .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
                    ZXY => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
                        .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
                    YXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
                        .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
                    YZX => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
                        .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
                    XYZ => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
                        .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
                    XZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
                        .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
                    #[allow(deprecated)]
                    ZYZ => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
                        .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
                    #[allow(deprecated)]
                    ZXZ => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
                        .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
                    #[allow(deprecated)]
                    YXY => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
                        .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
                    #[allow(deprecated)]
                    YZY => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
                        .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
                    #[allow(deprecated)]
                    XYX => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
                        .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
                    #[allow(deprecated)]
                    XZX => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
                        .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
                }
            }

            fn second(self, q: $quat) -> $t {
                use EulerRot::*;

                /// Clamp number to range [-1,1](-1,1) for asin() and acos(), else NaN is possible.
                #[inline(always)]
                fn arc_clamp<T: FloatEx>(val: T) -> T {
                    clamp(val, T::NEG_ONE, T::ONE)
                }
                match self {
                    ZYX => arc_clamp(-Self::Output::TWO * (q.x * q.z - q.w * q.y)).asin(),
                    ZXY => arc_clamp(Self::Output::TWO * (q.y * q.z + q.w * q.x)).asin(),
                    YXZ => arc_clamp(-Self::Output::TWO * (q.y * q.z - q.w * q.x)).asin(),
                    YZX => arc_clamp(Self::Output::TWO * (q.x * q.y + q.w * q.z)).asin(),
                    XYZ => arc_clamp(Self::Output::TWO * (q.x * q.z + q.w * q.y)).asin(),
                    XZY => arc_clamp(-Self::Output::TWO * (q.x * q.y - q.w * q.z)).asin(),
                    #[allow(deprecated)]
                    ZYZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
                    #[allow(deprecated)]
                    ZXZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
                    #[allow(deprecated)]
                    YXY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
                    #[allow(deprecated)]
                    YZY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
                    #[allow(deprecated)]
                    XYX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
                    #[allow(deprecated)]
                    XZX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
                }
            }

            fn third(self, q: $quat) -> $t {
                use EulerRot::*;
                #[allow(deprecated)]
                match self {
                    ZYX => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
                        .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
                    ZXY => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
                        .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
                    YXZ => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
                        .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
                    YZX => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
                        .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
                    XYZ => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
                        .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
                    XZY => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
                        .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
                    #[allow(deprecated)]
                    ZYZ => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
                        .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
                    #[allow(deprecated)]
                    ZXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
                        .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
                    #[allow(deprecated)]
                    YXY => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
                        .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
                    #[allow(deprecated)]
                    YZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
                        .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
                    #[allow(deprecated)]
                    XYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
                        .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
                    #[allow(deprecated)]
                    XZX => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
                        .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
                }
            }
        }
        // End - impl EulerFromQuaternion
    };
}

macro_rules! impl_to_quat {
    ($t:ty, $quat:ident) => {
        impl EulerToQuaternion<$t> for EulerRot {
            type Output = $quat;
            #[inline(always)]
            fn new_quat(self, u: $t, v: $t, w: $t) -> $quat {
                use EulerRot::*;
                #[inline(always)]
                fn rot_x(a: $t) -> $quat {
                    $quat::from_rotation_x(a)
                }
                #[inline(always)]
                fn rot_y(a: $t) -> $quat {
                    $quat::from_rotation_y(a)
                }
                #[inline(always)]
                fn rot_z(a: $t) -> $quat {
                    $quat::from_rotation_z(a)
                }
                match self {
                    ZYX => rot_z(u) * rot_y(v) * rot_x(w),
                    ZXY => rot_z(u) * rot_x(v) * rot_y(w),
                    YXZ => rot_y(u) * rot_x(v) * rot_z(w),
                    YZX => rot_y(u) * rot_z(v) * rot_x(w),
                    XYZ => rot_x(u) * rot_y(v) * rot_z(w),
                    XZY => rot_x(u) * rot_z(v) * rot_y(w),
                    #[allow(deprecated)]
                    ZYZ => rot_z(u) * rot_y(v) * rot_z(w),
                    #[allow(deprecated)]
                    ZXZ => rot_z(u) * rot_x(v) * rot_z(w),
                    #[allow(deprecated)]
                    YXY => rot_y(u) * rot_x(v) * rot_y(w),
                    #[allow(deprecated)]
                    YZY => rot_y(u) * rot_z(v) * rot_y(w),
                    #[allow(deprecated)]
                    XYX => rot_x(u) * rot_y(v) * rot_x(w),
                    #[allow(deprecated)]
                    XZX => rot_x(u) * rot_z(v) * rot_x(w),
                }
                .normalize()
            }
        }
        // End - impl EulerToQuaternion
    };
}

impl_from_quat!(f32, Quat);
impl_from_quat!(f64, DQuat);
impl_to_quat!(f32, Quat);
impl_to_quat!(f64, DQuat);