Debian Codegraph @

debian/missing-sources/glam-0.23.0/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::{DQuat, Quat};
8
9 #[cfg(feature = "libm")]
10 #[allow(unused_imports)]
11 use num_traits::Float;
12
13 /// Euler rotation sequences.
14 ///
15 /// The angles are applied starting from the right.
16 /// E.g. XYZ will first apply the z-axis rotation.
17 ///
18 /// YXZ can be used for yaw (y-axis), pitch (x-axis), roll (z-axis).
19 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
20 #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
21 pub enum EulerRot {
22 /// Intrinsic three-axis rotation ZYX
23 ZYX,
24 /// Intrinsic three-axis rotation ZXY
25 ZXY,
26 /// Intrinsic three-axis rotation YXZ
27 YXZ,
28 /// Intrinsic three-axis rotation YZX
29 YZX,
30 /// Intrinsic three-axis rotation XYZ
31 XYZ,
32 /// Intrinsic three-axis rotation XZY
33 XZY,
34 }
35
36 impl Default for EulerRot {
37 /// Default `YXZ` as yaw (y-axis), pitch (x-axis), roll (z-axis).
38 fn default() -> Self {
39 Self::YXZ
40 }
41 }
42
43 /// Conversion from quaternion to euler angles.
44 pub(crate) trait EulerFromQuaternion<Q: Copy>: Sized + Copy {
45 type Output;
46 /// Compute the angle of the first axis (X-x-x)
47 fn first(self, q: Q) -> Self::Output;
48 /// Compute then angle of the second axis (x-X-x)
49 fn second(self, q: Q) -> Self::Output;
50 /// Compute then angle of the third axis (x-x-X)
51 fn third(self, q: Q) -> Self::Output;
52
53 /// Compute all angles of a rotation in the notation order
54 fn convert_quat(self, q: Q) -> (Self::Output, Self::Output, Self::Output) {
55 (self.first(q), self.second(q), self.third(q))
56 }
57 }
58
59 /// Conversion from euler angles to quaternion.
60 pub(crate) trait EulerToQuaternion<T>: Copy {
61 type Output;
62 /// Create the rotation quaternion for the three angles of this euler rotation sequence.
63 fn new_quat(self, u: T, v: T, w: T) -> Self::Output;
64 }
65
66 /// Adds a atan2 that handles the negative zero case.
67 /// Basically forces positive zero in the x-argument for atan2.
68 pub(crate) trait Atan2Fixed<T = Self> {
69 fn atan2_fixed(self, other: T) -> T;
70 }
71
72 impl Atan2Fixed for f32 {
73 fn atan2_fixed(self, other: f32) -> f32 {
74 self.atan2(if other == 0.0f32 { 0.0f32 } else { other })
75 }
76 }
77 impl Atan2Fixed for f64 {
78 fn atan2_fixed(self, other: f64) -> f64 {
79 self.atan2(if other == 0.0f64 { 0.0f64 } else { other })
80 }
81 }
82
83 macro_rules! impl_from_quat {
84 ($t:ty, $quat:ident) => {
85 impl EulerFromQuaternion<$quat> for EulerRot {
86 type Output = $t;
87 fn first(self, q: $quat) -> $t {
88 use EulerRot::*;
89 match self {
90 ZYX => (2.0 * (q.x * q.y + q.w * q.z))
91 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
92 ZXY => (-2.0 * (q.x * q.y - q.w * q.z))
93 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
94 YXZ => (2.0 * (q.x * q.z + q.w * q.y))
95 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
96 YZX => (-2.0 * (q.x * q.z - q.w * q.y))
97 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
98 XYZ => (-2.0 * (q.y * q.z - q.w * q.x))
99 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
100 XZY => (2.0 * (q.y * q.z + q.w * q.x))
101 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
102 }
103 }
104
105 fn second(self, q: $quat) -> $t {
106 use EulerRot::*;
107
108 /// Clamp number to range [-1,1](-1,1) for asin() and acos(), else NaN is possible.
109 #[inline(always)]
110 fn arc_clamp(val: $t) -> $t {
111 <$t>::min(<$t>::max(val, -1.0), 1.0)
112 }
113
114 match self {
115 ZYX => arc_clamp(-2.0 * (q.x * q.z - q.w * q.y)).asin(),
116 ZXY => arc_clamp(2.0 * (q.y * q.z + q.w * q.x)).asin(),
117 YXZ => arc_clamp(-2.0 * (q.y * q.z - q.w * q.x)).asin(),
118 YZX => arc_clamp(2.0 * (q.x * q.y + q.w * q.z)).asin(),
119 XYZ => arc_clamp(2.0 * (q.x * q.z + q.w * q.y)).asin(),
120 XZY => arc_clamp(-2.0 * (q.x * q.y - q.w * q.z)).asin(),
121 }
122 }
123
124 fn third(self, q: $quat) -> $t {
125 use EulerRot::*;
126 match self {
127 ZYX => (2.0 * (q.y * q.z + q.w * q.x))
128 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
129 ZXY => (-2.0 * (q.x * q.z - q.w * q.y))
130 .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z),
131 YXZ => (2.0 * (q.x * q.y + q.w * q.z))
132 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
133 YZX => (-2.0 * (q.y * q.z - q.w * q.x))
134 .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z),
135 XYZ => (-2.0 * (q.x * q.y - q.w * q.z))
136 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
137 XZY => (2.0 * (q.x * q.z + q.w * q.y))
138 .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z),
139 }
140 }
141 }
142 // End - impl EulerFromQuaternion
143 };
144 }
145
146 macro_rules! impl_to_quat {
147 ($t:ty, $quat:ident) => {
148 impl EulerToQuaternion<$t> for EulerRot {
149 type Output = $quat;
150 #[inline(always)]
151 fn new_quat(self, u: $t, v: $t, w: $t) -> $quat {
152 use EulerRot::*;
153 #[inline(always)]
154 fn rot_x(a: $t) -> $quat {
155 $quat::from_rotation_x(a)
156 }
157 #[inline(always)]
158 fn rot_y(a: $t) -> $quat {
159 $quat::from_rotation_y(a)
160 }
161 #[inline(always)]
162 fn rot_z(a: $t) -> $quat {
163 $quat::from_rotation_z(a)
164 }
165 match self {
166 ZYX => rot_z(u) * rot_y(v) * rot_x(w),
167 ZXY => rot_z(u) * rot_x(v) * rot_y(w),
168 YXZ => rot_y(u) * rot_x(v) * rot_z(w),
169 YZX => rot_y(u) * rot_z(v) * rot_x(w),
170 XYZ => rot_x(u) * rot_y(v) * rot_z(w),
171 XZY => rot_x(u) * rot_z(v) * rot_y(w),
172 }
173 .normalize()
174 }
175 }
176 // End - impl EulerToQuaternion
177 };
178 }
179
180 impl_from_quat!(f32, Quat);
181 impl_from_quat!(f64, DQuat);
182 impl_to_quat!(f32, Quat);
183 impl_to_quat!(f64, DQuat);