Debian Codegraph @

debian/missing-sources/glam-0.19.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 superrust-analyzer cargo glam 0.19.0 crate/::quatrust-analyzer cargo glam 0.19.0 quat/::{DQuatrust-analyzer cargo glam 0.19.0 quat/DQuat#, Quatrust-analyzer cargo glam 0.19.0 quat/Quat#};
8 use craterust-analyzer cargo glam 0.19.0 crate/::corerust-analyzer cargo glam 0.19.0 core/::traitsrust-analyzer cargo glam 0.19.0 core/traits/::scalarrust-analyzer cargo glam 0.19.0 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 #[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:)]
19 pub enum EulerRotrust-analyzer cargo glam 0.19.0 euler/EulerRot# {
20 /// Intrinsic three-axis rotation ZYX
21 ZYXrust-analyzer cargo glam 0.19.0 euler/EulerRot#ZYX#,
22 /// Intrinsic three-axis rotation ZXY
23 ZXYrust-analyzer cargo glam 0.19.0 euler/EulerRot#ZXY#,
24 /// Intrinsic three-axis rotation YXZ
25 YXZrust-analyzer cargo glam 0.19.0 euler/EulerRot#YXZ#,
26 /// Intrinsic three-axis rotation YZX
27 YZXrust-analyzer cargo glam 0.19.0 euler/EulerRot#YZX#,
28 /// Intrinsic three-axis rotation XYZ
29 XYZrust-analyzer cargo glam 0.19.0 euler/EulerRot#XYZ#,
30 /// Intrinsic three-axis rotation XZY
31 XZYrust-analyzer cargo glam 0.19.0 euler/EulerRot#XZY#,
32
33 /// Intrinsic two-axis rotation ZYZ
34 #[deprecated(note = "Untested! Use at own risk!")]
35 ZYZrust-analyzer cargo glam 0.19.0 euler/EulerRot#ZYZ#,
36 /// Intrinsic two-axis rotation ZXZ
37 #[deprecated(note = "Untested! Use at own risk!")]
38 ZXZrust-analyzer cargo glam 0.19.0 euler/EulerRot#ZXZ#,
39 /// Intrinsic two-axis rotation YXY
40 #[deprecated(note = "Untested! Use at own risk!")]
41 YXYrust-analyzer cargo glam 0.19.0 euler/EulerRot#YXY#,
42 /// Intrinsic two-axis rotation YZY
43 #[deprecated(note = "Untested! Use at own risk!")]
44 YZYrust-analyzer cargo glam 0.19.0 euler/EulerRot#YZY#,
45 /// Intrinsic two-axis rotation XYX
46 #[deprecated(note = "Untested! Use at own risk!")]
47 XYXrust-analyzer cargo glam 0.19.0 euler/EulerRot#XYX#,
48 /// Intrinsic two-axis rotation XZX
49 #[deprecated(note = "Untested! Use at own risk!")]
50 XZXrust-analyzer cargo glam 0.19.0 euler/EulerRot#XZX#,
51 }
52
53 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EulerRotrust-analyzer cargo glam 0.19.0 euler/EulerRot# {
54 /// Default YXZi as yaw (y-axis), pitch (x-axis), roll (z-axis).
55 fn defaultrust-analyzer cargo glam 0.19.0 euler/impl#[EulerRot][Default]default().() -> Selfrust-analyzer cargo glam 0.19.0 euler/impl#[EulerRot][Default] {
56 Selfrust-analyzer cargo glam 0.19.0 euler/impl#[EulerRot][Default]::YXZrust-analyzer cargo glam 0.19.0 euler/EulerRot#YXZ#
57 }
58 }
59
60 /// Conversion from quaternion to euler angles.
61 pub trait EulerFromQuaternionrust-analyzer cargo glam 0.19.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# {
62 type Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#: FloatExrust-analyzer cargo glam 0.19.0 core/traits/scalar/FloatEx#;
63 /// Compute the angle of the first axis (X-x-x)
64 fn firstrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#first().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#;
65 /// Compute then angle of the second axis (x-X-x)
66 fn secondrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#second().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#;
67 /// Compute then angle of the third axis (x-x-X)
68 fn thirdrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#third().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#;
69
70 /// Compute all angles of a rotation in the notation order
71 fn convert_quatrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#convert_quat().(self, q: Q) -> (Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#Output#) {
72 (self.firstrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#first().(q), self.secondrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#second().(q), self.thirdrust-analyzer cargo glam 0.19.0 euler/EulerFromQuaternion#third().(q))
73 }
74 }
75
76 /// Conversion from euler angles to quaternion.
77 pub trait EulerToQuaternionrust-analyzer cargo glam 0.19.0 euler/EulerToQuaternion#<T>: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# {
78 type Outputrust-analyzer cargo glam 0.19.0 euler/EulerToQuaternion#Output#;
79 /// Create the rotation quaternion for the three angles of this euler rotation sequence.
80 fn new_quatrust-analyzer cargo glam 0.19.0 euler/EulerToQuaternion#new_quat().(self, u: T, v: T, w: T) -> Self::Outputrust-analyzer cargo glam 0.19.0 euler/EulerToQuaternion#Output#;
81 }
82
83 /// Helper, until std::f32::clamp is stable.
84 fn clamprust-analyzer cargo glam 0.19.0 euler/clamp().<T: PartialOrdrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#>(value: T, min: T, max: T) -> T {
85 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(min <= max);
86 if value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().<rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#lt().min {
87 min
88 } else if value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialOrd#gt().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 Atan2Fixedrust-analyzer cargo glam 0.19.0 euler/Atan2Fixed#<T = Self> {
98 fn atan2_fixedrust-analyzer cargo glam 0.19.0 euler/Atan2Fixed#atan2_fixed().(self, other: T) -> T;
99 }
100 impl Atan2Fixedrust-analyzer cargo glam 0.19.0 euler/Atan2Fixed# for f32 {
101 fn atan2_fixedrust-analyzer cargo glam 0.19.0 euler/impl#[f32][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f32) -> f32 {
102 self.atan2rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f32/impl#[f32]atan2().(if other rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().0.0f32 { 0.0f32 } else { other })
103 }
104 }
105 impl Atan2Fixedrust-analyzer cargo glam 0.19.0 euler/Atan2Fixed# for f64 {
106 fn atan2_fixedrust-analyzer cargo glam 0.19.0 euler/impl#[f64][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f64) -> f64 {
107 self.atan2rust-analyzer cargo std https://github.com/rust-lang/rust/library/std f64/impl#[f64]atan2().(if other rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f64][`PartialEq<Self>`]eq().0.0f64 { 0.0f64 } else { other })
108 }
109 }
110
111 macro_rules! impl_from_quatrust-analyzer cargo glam 0.19.0 euler/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_quatrust-analyzer cargo glam 0.19.0 euler/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_quatrust-analyzer cargo glam 0.19.0 euler/impl_from_quat!!(f32, Quatrust-analyzer cargo glam 0.19.0 quat/Quat#);
269 impl_from_quatrust-analyzer cargo glam 0.19.0 euler/impl_from_quat!!(f64, DQuatrust-analyzer cargo glam 0.19.0 quat/DQuat#);
270 impl_to_quatrust-analyzer cargo glam 0.19.0 euler/impl_to_quat!!(f32, Quatrust-analyzer cargo glam 0.19.0 quat/Quat#);
271 impl_to_quatrust-analyzer cargo glam 0.19.0 euler/impl_to_quat!!(f64, DQuatrust-analyzer cargo glam 0.19.0 quat/DQuat#);