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 craterust-analyzer cargo glam 0.28.0 crate/::{DQuatrust-analyzer cargo glam 0.28.0 f64/dquat/DQuat#, Quatrust-analyzer cargo glam 0.28.0 f32/sse2/quat/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 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, Hashrust-analyzer cargo core https://github.com/rust-lang/rust/library/core hash/macros/Hash:)]
16 pub enum EulerRotrust-analyzer cargo glam 0.28.0 euler/EulerRot# {
17 /// Intrinsic three-axis rotation ZYX
18 ZYXrust-analyzer cargo glam 0.28.0 euler/EulerRot#ZYX#,
19 /// Intrinsic three-axis rotation ZXY
20 ZXYrust-analyzer cargo glam 0.28.0 euler/EulerRot#ZXY#,
21 /// Intrinsic three-axis rotation YXZ
22 YXZrust-analyzer cargo glam 0.28.0 euler/EulerRot#YXZ#,
23 /// Intrinsic three-axis rotation YZX
24 YZXrust-analyzer cargo glam 0.28.0 euler/EulerRot#YZX#,
25 /// Intrinsic three-axis rotation XYZ
26 XYZrust-analyzer cargo glam 0.28.0 euler/EulerRot#XYZ#,
27 /// Intrinsic three-axis rotation XZY
28 XZYrust-analyzer cargo glam 0.28.0 euler/EulerRot#XZY#,
29 }
30
31 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EulerRotrust-analyzer cargo glam 0.28.0 euler/EulerRot# {
32 /// Default `YXZ` as yaw (y-axis), pitch (x-axis), roll (z-axis).
33 fn defaultrust-analyzer cargo glam 0.28.0 euler/impl#[EulerRot][Default]default().() -> Selfrust-analyzer cargo glam 0.28.0 euler/impl#[EulerRot][Default] {
34 Selfrust-analyzer cargo glam 0.28.0 euler/impl#[EulerRot][Default]::YXZrust-analyzer cargo glam 0.28.0 euler/EulerRot#YXZ#
35 }
36 }
37
38 /// Conversion from quaternion to euler angles.
39 pub(craterust-analyzer cargo glam 0.28.0 crate/) trait EulerFromQuaternionrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#<Q: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy#>: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
40 type Outputrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#Output#;
41
42 /// Compute all angles of a rotation in the notation order
43 fn convert_quatrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#convert_quat().(self, q: Q) -> (Self::Outputrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#Output#);
44
45 #[doc(hidden)]
46 fn sine_thetarust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#sine_theta().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.28.0 euler/EulerFromQuaternion#Output#;
47 }
48
49 /// Conversion from euler angles to quaternion.
50 pub(craterust-analyzer cargo glam 0.28.0 crate/) trait EulerToQuaternionrust-analyzer cargo glam 0.28.0 euler/EulerToQuaternion#<T>: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
51 type Outputrust-analyzer cargo glam 0.28.0 euler/EulerToQuaternion#Output#;
52 /// Create the rotation quaternion for the three angles of this euler rotation sequence.
53 fn new_quatrust-analyzer cargo glam 0.28.0 euler/EulerToQuaternion#new_quat().(self, u: T, v: T, w: T) -> Self::Outputrust-analyzer cargo glam 0.28.0 euler/EulerToQuaternion#Output#;
54 }
55
56 macro_rules! impl_from_quatrust-analyzer cargo glam 0.28.0 euler/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_quatrust-analyzer cargo glam 0.28.0 euler/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_quatrust-analyzer cargo glam 0.28.0 euler/impl_from_quat!!(f32, Quatrust-analyzer cargo glam 0.28.0 f32/sse2/quat/Quat#);
190 impl_from_quatrust-analyzer cargo glam 0.28.0 euler/impl_from_quat!!(f64, DQuatrust-analyzer cargo glam 0.28.0 f64/dquat/DQuat#);
191 impl_to_quatrust-analyzer cargo glam 0.28.0 euler/impl_to_quat!!(f32, Quatrust-analyzer cargo glam 0.28.0 f32/sse2/quat/Quat#);
192 impl_to_quatrust-analyzer cargo glam 0.28.0 euler/impl_to_quat!!(f64, DQuatrust-analyzer cargo glam 0.28.0 f64/dquat/DQuat#);