Debian Codegraph @

debian/missing-sources/glam-0.17.3/src/euler.rs

raw symbols SCIP
1 /*
2 Conversion from quaternions to Euler rotation sequences.
3
4 From: http://bediyap.com/programming/convert-quaternion-to-euler-rotations/
5 */
6
7 use super::quat::{DQuat, Quat};
8 use crate::core::traits::scalar::*;
9
10 /// Euler rotation sequences.
11 ///
12 /// The angles are applied starting from the right.
13 /// E.g. XYZ will first apply the z-axis rotation.
14 ///
15 /// YXZ can be used for yaw (y-axis), pitch (x-axis), roll (z-axis).
16 ///
17 /// The two-axis rotations (e.g. ZYZ) are not fully tested and have to be treated with caution.
18 #[derive(Debug, Clone, Copy)]
19 pub enum EulerRot {
20 /// Intrinsic three-axis rotation ZYX
21 ZYX,
22 /// Intrinsic three-axis rotation ZXY
23 ZXY,
24 /// Intrinsic three-axis rotation YXZ
25 YXZ,
26 /// Intrinsic three-axis rotation YZX
27 YZX,
28 /// Intrinsic three-axis rotation XYZ
29 XYZ,
30 /// Intrinsic three-axis rotation XZY
31 XZY,
32
33 /// Intrinsic two-axis rotation ZYZ
34 #[deprecated(note = "Untested! Use at own risk!")]
35 ZYZ,
36 /// Intrinsic two-axis rotation ZXZ
37 #[deprecated(note = "Untested! Use at own risk!")]
38 ZXZ,
39 /// Intrinsic two-axis rotation YXY
40 #[deprecated(note = "Untested! Use at own risk!")]
41 YXY,
42 /// Intrinsic two-axis rotation YZY
43 #[deprecated(note = "Untested! Use at own risk!")]
44 YZY,
45 /// Intrinsic two-axis rotation XYX
46 #[deprecated(note = "Untested! Use at own risk!")]
47 XYX,
48 /// Intrinsic two-axis rotation XZX
49 #[deprecated(note = "Untested! Use at own risk!")]
50 XZX,
51 }
52
53 impl Default for EulerRot {
54 /// Default YXZi as yaw (y-axis), pitch (x-axis), roll (z-axis).
55 fn default() -> Self {
56 Self::YXZ
57 }
58 }
59
60 /// Conversion from quaternion to euler angles.
61 pub trait EulerFromQuaternion<Q: Copy>: Sized + Copy {
62 type Output: FloatEx;
63 /// Compute the angle of the first axis (X-x-x)
64 fn first(self, q: Q) -> Self::Output;
65 /// Compute then angle of the second axis (x-X-x)
66 fn second(self, q: Q) -> Self::Output;
67 /// Compute then angle of the third axis (x-x-X)
68 fn third(self, q: Q) -> Self::Output;
69
70 /// Compute all angles of a rotation in the notation order
71 fn convert_quat(self, q: Q) -> (Self::Output, Self::Output, Self::Output) {
72 (self.first(q), self.second(q), self.third(q))
73 }
74 }
75
76 /// Conversion from euler angles to quaternion.
77 pub trait EulerToQuaternion<T>: Copy {
78 type Output;
79 /// Create the rotation quaternion for the three angles of this euler rotation sequence.
80 fn new_quat(self, u: T, v: T, w: T) -> Self::Output;
81 }
82
83 /// Helper, until std::f32::clamp is stable.
84 fn clamp<T: PartialOrd>(value: T, min: T, max: T) -> T {
85 assert!(min <= max);
86 if value < min {
87 min
88 } else if value > max {
89 max
90 } else {
91 value
92 }
93 }
94
95 /// Adds a atan2 that handles the negative zero case.
96 /// Basically forces positive zero in the x-argument for atan2.
97 pub trait Atan2Fixed<T = Self> {
98 fn atan2_fixed(self, other: T) -> T;
99 }
100 impl Atan2Fixed for f32 {
101 fn atan2_fixed(self, other: f32) -> f32 {
102 self.atan2(if other == 0.0f32 { 0.0f32 } else { other })
103 }
104 }
105 impl Atan2Fixed for f64 {
106 fn atan2_fixed(self, other: f64) -> f64 {
107 self.atan2(if other == 0.0f64 { 0.0f64 } else { other })
108 }
109 }
110
111 macro_rules! impl_from_quat {
112 ($t:ty, $quat:ident) => {
113 impl EulerFromQuaternion<$quat> for EulerRot {
114 type Output = $t;
115 fn first(self, q: $quat) -> $t {
116 use EulerRot::*;
117 match self {
118 ZYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
119 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
120 ZXY => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
121 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
122 YXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
123 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
124 YZX => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
125 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
126 XYZ => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
127 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
128 XZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
129 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
130 #[allow(deprecated)]
131 ZYZ => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
132 .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
133 #[allow(deprecated)]
134 ZXZ => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
135 .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
136 #[allow(deprecated)]
137 YXY => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
138 .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
139 #[allow(deprecated)]
140 YZY => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
141 .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
142 #[allow(deprecated)]
143 XYX => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
144 .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
145 #[allow(deprecated)]
146 XZX => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
147 .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
148 }
149 }
150
151 fn second(self, q: $quat) -> $t {
152 use EulerRot::*;
153
154 /// Clamp number to range [-1,1](-1,1) for asin() and acos(), else NaN is possible.
155 #[inline(always)]
156 fn arc_clamp<T: FloatEx>(val: T) -> T {
157 clamp(val, T::NEG_ONE, T::ONE)
158 }
159 match self {
160 ZYX => arc_clamp(-Self::Output::TWO * (q.x * q.z - q.w * q.y)).asin(),
161 ZXY => arc_clamp(Self::Output::TWO * (q.y * q.z + q.w * q.x)).asin(),
162 YXZ => arc_clamp(-Self::Output::TWO * (q.y * q.z - q.w * q.x)).asin(),
163 YZX => arc_clamp(Self::Output::TWO * (q.x * q.y + q.w * q.z)).asin(),
164 XYZ => arc_clamp(Self::Output::TWO * (q.x * q.z + q.w * q.y)).asin(),
165 XZY => arc_clamp(-Self::Output::TWO * (q.x * q.y - q.w * q.z)).asin(),
166 #[allow(deprecated)]
167 ZYZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
168 #[allow(deprecated)]
169 ZXZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
170 #[allow(deprecated)]
171 YXY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
172 #[allow(deprecated)]
173 YZY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
174 #[allow(deprecated)]
175 XYX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
176 #[allow(deprecated)]
177 XZX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
178 }
179 }
180
181 fn third(self, q: $quat) -> $t {
182 use EulerRot::*;
183 #[allow(deprecated)]
184 match self {
185 ZYX => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
186 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
187 ZXY => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
188 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
189 YXZ => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
190 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
191 YZX => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
192 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
193 XYZ => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
194 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
195 XZY => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
196 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
197 #[allow(deprecated)]
198 ZYZ => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
199 .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
200 #[allow(deprecated)]
201 ZXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
202 .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
203 #[allow(deprecated)]
204 YXY => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
205 .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
206 #[allow(deprecated)]
207 YZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
208 .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
209 #[allow(deprecated)]
210 XYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
211 .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
212 #[allow(deprecated)]
213 XZX => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
214 .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
215 }
216 }
217 }
218 // End - impl EulerFromQuaternion
219 };
220 }
221
222 macro_rules! impl_to_quat {
223 ($t:ty, $quat:ident) => {
224 impl EulerToQuaternion<$t> for EulerRot {
225 type Output = $quat;
226 #[inline(always)]
227 fn new_quat(self, u: $t, v: $t, w: $t) -> $quat {
228 use EulerRot::*;
229 #[inline(always)]
230 fn rot_x(a: $t) -> $quat {
231 $quat::from_rotation_x(a)
232 }
233 #[inline(always)]
234 fn rot_y(a: $t) -> $quat {
235 $quat::from_rotation_y(a)
236 }
237 #[inline(always)]
238 fn rot_z(a: $t) -> $quat {
239 $quat::from_rotation_z(a)
240 }
241 match self {
242 ZYX => rot_z(u) * rot_y(v) * rot_x(w),
243 ZXY => rot_z(u) * rot_x(v) * rot_y(w),
244 YXZ => rot_y(u) * rot_x(v) * rot_z(w),
245 YZX => rot_y(u) * rot_z(v) * rot_x(w),
246 XYZ => rot_x(u) * rot_y(v) * rot_z(w),
247 XZY => rot_x(u) * rot_z(v) * rot_y(w),
248 #[allow(deprecated)]
249 ZYZ => rot_z(u) * rot_y(v) * rot_z(w),
250 #[allow(deprecated)]
251 ZXZ => rot_z(u) * rot_x(v) * rot_z(w),
252 #[allow(deprecated)]
253 YXY => rot_y(u) * rot_x(v) * rot_y(w),
254 #[allow(deprecated)]
255 YZY => rot_y(u) * rot_z(v) * rot_y(w),
256 #[allow(deprecated)]
257 XYX => rot_x(u) * rot_y(v) * rot_x(w),
258 #[allow(deprecated)]
259 XZX => rot_x(u) * rot_z(v) * rot_x(w),
260 }
261 .normalize()
262 }
263 }
264 // End - impl EulerToQuaternion
265 };
266 }
267
268 impl_from_quat!(f32, Quat);
269 impl_from_quat!(f64, DQuat);
270 impl_to_quat!(f32, Quat);
271 impl_to_quat!(f64, DQuat);