Debian Codegraph @

debian/missing-sources/glam-0.20.5/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 `YXZ` 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 /// Adds a atan2 that handles the negative zero case.
84 /// Basically forces positive zero in the x-argument for atan2.
85 pub trait Atan2Fixed<T = Self> {
86 fn atan2_fixed(self, other: T) -> T;
87 }
88
89 impl Atan2Fixed for f32 {
90 fn atan2_fixed(self, other: f32) -> f32 {
91 self.atan2(if other == 0.0f32 { 0.0f32 } else { other })
92 }
93 }
94 impl Atan2Fixed for f64 {
95 fn atan2_fixed(self, other: f64) -> f64 {
96 self.atan2(if other == 0.0f64 { 0.0f64 } else { other })
97 }
98 }
99
100 macro_rules! impl_from_quat {
101 ($t:ty, $quat:ident) => {
102 impl EulerFromQuaternion<$quat> for EulerRot {
103 type Output = $t;
104 fn first(self, q: $quat) -> $t {
105 use EulerRot::*;
106 match self {
107 ZYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
108 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
109 ZXY => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
110 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
111 YXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
112 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
113 YZX => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
114 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
115 XYZ => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
116 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
117 XZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
118 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
119 #[allow(deprecated)]
120 ZYZ => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
121 .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
122 #[allow(deprecated)]
123 ZXZ => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
124 .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
125 #[allow(deprecated)]
126 YXY => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
127 .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
128 #[allow(deprecated)]
129 YZY => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
130 .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
131 #[allow(deprecated)]
132 XYX => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
133 .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
134 #[allow(deprecated)]
135 XZX => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
136 .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
137 }
138 }
139
140 fn second(self, q: $quat) -> $t {
141 use EulerRot::*;
142
143 /// Clamp number to range [-1,1](-1,1) for asin() and acos(), else NaN is possible.
144 #[inline(always)]
145 fn arc_clamp<T: FloatEx>(val: T) -> T {
146 NumEx::min(NumEx::max(val, T::NEG_ONE), T::ONE)
147 }
148
149 match self {
150 ZYX => arc_clamp(-Self::Output::TWO * (q.x * q.z - q.w * q.y)).asin(),
151 ZXY => arc_clamp(Self::Output::TWO * (q.y * q.z + q.w * q.x)).asin(),
152 YXZ => arc_clamp(-Self::Output::TWO * (q.y * q.z - q.w * q.x)).asin(),
153 YZX => arc_clamp(Self::Output::TWO * (q.x * q.y + q.w * q.z)).asin(),
154 XYZ => arc_clamp(Self::Output::TWO * (q.x * q.z + q.w * q.y)).asin(),
155 XZY => arc_clamp(-Self::Output::TWO * (q.x * q.y - q.w * q.z)).asin(),
156 #[allow(deprecated)]
157 ZYZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
158 #[allow(deprecated)]
159 ZXZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(),
160 #[allow(deprecated)]
161 YXY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
162 #[allow(deprecated)]
163 YZY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(),
164 #[allow(deprecated)]
165 XYX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
166 #[allow(deprecated)]
167 XZX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(),
168 }
169 }
170
171 fn third(self, q: $quat) -> $t {
172 use EulerRot::*;
173 #[allow(deprecated)]
174 match self {
175 ZYX => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
176 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
177 ZXY => (-Self::Output::TWO * (q.x * q.z - q.w * q.y))
178 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
179 YXZ => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
180 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
181 YZX => (-Self::Output::TWO * (q.y * q.z - q.w * q.x))
182 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
183 XYZ => (-Self::Output::TWO * (q.x * q.y - q.w * q.z))
184 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
185 XZY => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
186 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
187 #[allow(deprecated)]
188 ZYZ => (Self::Output::TWO * (q.y * q.z - q.w * q.x))
189 .atan2_fixed(Self::Output::TWO * (q.x * q.z + q.w * q.y)),
190 #[allow(deprecated)]
191 ZXZ => (Self::Output::TWO * (q.x * q.z + q.w * q.y))
192 .atan2_fixed(-Self::Output::TWO * (q.y * q.z - q.w * q.x)),
193 #[allow(deprecated)]
194 YXY => (Self::Output::TWO * (q.x * q.y - q.w * q.z))
195 .atan2_fixed(Self::Output::TWO * (q.y * q.z + q.w * q.x)),
196 #[allow(deprecated)]
197 YZY => (Self::Output::TWO * (q.y * q.z + q.w * q.x))
198 .atan2_fixed(-Self::Output::TWO * (q.x * q.y - q.w * q.z)),
199 #[allow(deprecated)]
200 XYX => (Self::Output::TWO * (q.x * q.y + q.w * q.z))
201 .atan2_fixed(-Self::Output::TWO * (q.x * q.z - q.w * q.y)),
202 #[allow(deprecated)]
203 XZX => (Self::Output::TWO * (q.x * q.z - q.w * q.y))
204 .atan2_fixed(Self::Output::TWO * (q.x * q.y + q.w * q.z)),
205 }
206 }
207 }
208 // End - impl EulerFromQuaternion
209 };
210 }
211
212 macro_rules! impl_to_quat {
213 ($t:ty, $quat:ident) => {
214 impl EulerToQuaternion<$t> for EulerRot {
215 type Output = $quat;
216 #[inline(always)]
217 fn new_quat(self, u: $t, v: $t, w: $t) -> $quat {
218 use EulerRot::*;
219 #[inline(always)]
220 fn rot_x(a: $t) -> $quat {
221 $quat::from_rotation_x(a)
222 }
223 #[inline(always)]
224 fn rot_y(a: $t) -> $quat {
225 $quat::from_rotation_y(a)
226 }
227 #[inline(always)]
228 fn rot_z(a: $t) -> $quat {
229 $quat::from_rotation_z(a)
230 }
231 match self {
232 ZYX => rot_z(u) * rot_y(v) * rot_x(w),
233 ZXY => rot_z(u) * rot_x(v) * rot_y(w),
234 YXZ => rot_y(u) * rot_x(v) * rot_z(w),
235 YZX => rot_y(u) * rot_z(v) * rot_x(w),
236 XYZ => rot_x(u) * rot_y(v) * rot_z(w),
237 XZY => rot_x(u) * rot_z(v) * rot_y(w),
238 #[allow(deprecated)]
239 ZYZ => rot_z(u) * rot_y(v) * rot_z(w),
240 #[allow(deprecated)]
241 ZXZ => rot_z(u) * rot_x(v) * rot_z(w),
242 #[allow(deprecated)]
243 YXY => rot_y(u) * rot_x(v) * rot_y(w),
244 #[allow(deprecated)]
245 YZY => rot_y(u) * rot_z(v) * rot_y(w),
246 #[allow(deprecated)]
247 XYX => rot_x(u) * rot_y(v) * rot_x(w),
248 #[allow(deprecated)]
249 XZX => rot_x(u) * rot_z(v) * rot_x(w),
250 }
251 .normalize()
252 }
253 }
254 // End - impl EulerToQuaternion
255 };
256 }
257
258 impl_from_quat!(f32, Quat);
259 impl_from_quat!(f64, DQuat);
260 impl_to_quat!(f32, Quat);
261 impl_to_quat!(f64, DQuat);