Debian Codegraph @

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