| 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.21.3 crate/::{DQuatrust-analyzer cargo glam 0.21.3 f64/dquat/DQuat#, Quatrust-analyzer cargo glam 0.21.3 f32/sse2/quat/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 | /// |
| 20 | /// The two-axis rotations (e.g. ZYZ) are not fully tested and have to be treated with caution. |
| 21 | #[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:)] |
| 22 | pub enum EulerRotrust-analyzer cargo glam 0.21.3 euler/EulerRot# { |
| 23 | /// Intrinsic three-axis rotation ZYX |
| 24 | ZYXrust-analyzer cargo glam 0.21.3 euler/EulerRot#ZYX#, |
| 25 | /// Intrinsic three-axis rotation ZXY |
| 26 | ZXYrust-analyzer cargo glam 0.21.3 euler/EulerRot#ZXY#, |
| 27 | /// Intrinsic three-axis rotation YXZ |
| 28 | YXZrust-analyzer cargo glam 0.21.3 euler/EulerRot#YXZ#, |
| 29 | /// Intrinsic three-axis rotation YZX |
| 30 | YZXrust-analyzer cargo glam 0.21.3 euler/EulerRot#YZX#, |
| 31 | /// Intrinsic three-axis rotation XYZ |
| 32 | XYZrust-analyzer cargo glam 0.21.3 euler/EulerRot#XYZ#, |
| 33 | /// Intrinsic three-axis rotation XZY |
| 34 | XZYrust-analyzer cargo glam 0.21.3 euler/EulerRot#XZY#, |
| 35 | |
| 36 | /// Intrinsic two-axis rotation ZYZ |
| 37 | #[deprecated(note = "Untested! Use at own risk!")] |
| 38 | ZYZrust-analyzer cargo glam 0.21.3 euler/EulerRot#ZYZ#, |
| 39 | /// Intrinsic two-axis rotation ZXZ |
| 40 | #[deprecated(note = "Untested! Use at own risk!")] |
| 41 | ZXZrust-analyzer cargo glam 0.21.3 euler/EulerRot#ZXZ#, |
| 42 | /// Intrinsic two-axis rotation YXY |
| 43 | #[deprecated(note = "Untested! Use at own risk!")] |
| 44 | YXYrust-analyzer cargo glam 0.21.3 euler/EulerRot#YXY#, |
| 45 | /// Intrinsic two-axis rotation YZY |
| 46 | #[deprecated(note = "Untested! Use at own risk!")] |
| 47 | YZYrust-analyzer cargo glam 0.21.3 euler/EulerRot#YZY#, |
| 48 | /// Intrinsic two-axis rotation XYX |
| 49 | #[deprecated(note = "Untested! Use at own risk!")] |
| 50 | XYXrust-analyzer cargo glam 0.21.3 euler/EulerRot#XYX#, |
| 51 | /// Intrinsic two-axis rotation XZX |
| 52 | #[deprecated(note = "Untested! Use at own risk!")] |
| 53 | XZXrust-analyzer cargo glam 0.21.3 euler/EulerRot#XZX#, |
| 54 | } |
| 55 | |
| 56 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EulerRotrust-analyzer cargo glam 0.21.3 euler/EulerRot# { |
| 57 | /// Default `YXZ` as yaw (y-axis), pitch (x-axis), roll (z-axis). |
| 58 | fn defaultrust-analyzer cargo glam 0.21.3 euler/impl#[EulerRot][Default]default().() -> Selfrust-analyzer cargo glam 0.21.3 euler/impl#[EulerRot][Default] { |
| 59 | Selfrust-analyzer cargo glam 0.21.3 euler/impl#[EulerRot][Default]::YXZrust-analyzer cargo glam 0.21.3 euler/EulerRot#YXZ# |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Conversion from quaternion to euler angles. |
| 64 | pub(craterust-analyzer cargo glam 0.21.3 crate/) trait EulerFromQuaternionrust-analyzer cargo glam 0.21.3 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# { |
| 65 | type Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#; |
| 66 | /// Compute the angle of the first axis (X-x-x) |
| 67 | fn firstrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#first().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#; |
| 68 | /// Compute then angle of the second axis (x-X-x) |
| 69 | fn secondrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#second().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#; |
| 70 | /// Compute then angle of the third axis (x-x-X) |
| 71 | fn thirdrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#third().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#; |
| 72 | |
| 73 | /// Compute all angles of a rotation in the notation order |
| 74 | fn convert_quatrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#convert_quat().(self, q: Q) -> (Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#Output#) { |
| 75 | (self.firstrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#first().(q), self.secondrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#second().(q), self.thirdrust-analyzer cargo glam 0.21.3 euler/EulerFromQuaternion#third().(q)) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Conversion from euler angles to quaternion. |
| 80 | pub(craterust-analyzer cargo glam 0.21.3 crate/) trait EulerToQuaternionrust-analyzer cargo glam 0.21.3 euler/EulerToQuaternion#<T>: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# { |
| 81 | type Outputrust-analyzer cargo glam 0.21.3 euler/EulerToQuaternion#Output#; |
| 82 | /// Create the rotation quaternion for the three angles of this euler rotation sequence. |
| 83 | fn new_quatrust-analyzer cargo glam 0.21.3 euler/EulerToQuaternion#new_quat().(self, u: T, v: T, w: T) -> Self::Outputrust-analyzer cargo glam 0.21.3 euler/EulerToQuaternion#Output#; |
| 84 | } |
| 85 | |
| 86 | /// Adds a atan2 that handles the negative zero case. |
| 87 | /// Basically forces positive zero in the x-argument for atan2. |
| 88 | pub(craterust-analyzer cargo glam 0.21.3 crate/) trait Atan2Fixedrust-analyzer cargo glam 0.21.3 euler/Atan2Fixed#<T = Self> { |
| 89 | fn atan2_fixedrust-analyzer cargo glam 0.21.3 euler/Atan2Fixed#atan2_fixed().(self, other: T) -> T; |
| 90 | } |
| 91 | |
| 92 | impl Atan2Fixedrust-analyzer cargo glam 0.21.3 euler/Atan2Fixed# for f32 { |
| 93 | fn atan2_fixedrust-analyzer cargo glam 0.21.3 euler/impl#[f32][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f32) -> f32 { |
| 94 | 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 }) |
| 95 | } |
| 96 | } |
| 97 | impl Atan2Fixedrust-analyzer cargo glam 0.21.3 euler/Atan2Fixed# for f64 { |
| 98 | fn atan2_fixedrust-analyzer cargo glam 0.21.3 euler/impl#[f64][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f64) -> f64 { |
| 99 | 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 }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | macro_rules! impl_from_quatrust-analyzer cargo glam 0.21.3 euler/impl_from_quat! { |
| 104 | ($t:ty, $quat:ident) => { |
| 105 | impl EulerFromQuaternion<$quat> for EulerRot { |
| 106 | type Output = $t; |
| 107 | fn first(self, q: $quat) -> $t { |
| 108 | use EulerRot::*; |
| 109 | match self { |
| 110 | ZYX => (2.0 * (q.x * q.y + q.w * q.z)) |
| 111 | .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z), |
| 112 | ZXY => (-2.0 * (q.x * q.y - q.w * q.z)) |
| 113 | .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z), |
| 114 | YXZ => (2.0 * (q.x * q.z + q.w * q.y)) |
| 115 | .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z), |
| 116 | YZX => (-2.0 * (q.x * q.z - q.w * q.y)) |
| 117 | .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z), |
| 118 | XYZ => (-2.0 * (q.y * q.z - q.w * q.x)) |
| 119 | .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z), |
| 120 | XZY => (2.0 * (q.y * q.z + q.w * q.x)) |
| 121 | .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z), |
| 122 | #[allow(deprecated)] |
| 123 | ZYZ => { |
| 124 | (2.0 * (q.y * q.z + q.w * q.x)).atan2_fixed(-2.0 * (q.x * q.z - q.w * q.y)) |
| 125 | } |
| 126 | #[allow(deprecated)] |
| 127 | ZXZ => { |
| 128 | (2.0 * (q.x * q.z - q.w * q.y)).atan2_fixed(2.0 * (q.y * q.z + q.w * q.x)) |
| 129 | } |
| 130 | #[allow(deprecated)] |
| 131 | YXY => { |
| 132 | (2.0 * (q.x * q.y + q.w * q.z)).atan2_fixed(-2.0 * (q.y * q.z - q.w * q.x)) |
| 133 | } |
| 134 | #[allow(deprecated)] |
| 135 | YZY => { |
| 136 | (2.0 * (q.y * q.z - q.w * q.x)).atan2_fixed(2.0 * (q.x * q.y + q.w * q.z)) |
| 137 | } |
| 138 | #[allow(deprecated)] |
| 139 | XYX => { |
| 140 | (2.0 * (q.x * q.y - q.w * q.z)).atan2_fixed(2.0 * (q.x * q.z + q.w * q.y)) |
| 141 | } |
| 142 | #[allow(deprecated)] |
| 143 | XZX => { |
| 144 | (2.0 * (q.x * q.z + q.w * q.y)).atan2_fixed(-2.0 * (q.x * q.y - q.w * q.z)) |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | fn second(self, q: $quat) -> $t { |
| 150 | use EulerRot::*; |
| 151 | |
| 152 | /// Clamp number to range [-1,1](-1,1) for asin() and acos(), else NaN is possible. |
| 153 | #[inline(always)] |
| 154 | fn arc_clamp(val: $t) -> $t { |
| 155 | <$t>::min(<$t>::max(val, -1.0), 1.0) |
| 156 | } |
| 157 | |
| 158 | match self { |
| 159 | ZYX => arc_clamp(-2.0 * (q.x * q.z - q.w * q.y)).asin(), |
| 160 | ZXY => arc_clamp(2.0 * (q.y * q.z + q.w * q.x)).asin(), |
| 161 | YXZ => arc_clamp(-2.0 * (q.y * q.z - q.w * q.x)).asin(), |
| 162 | YZX => arc_clamp(2.0 * (q.x * q.y + q.w * q.z)).asin(), |
| 163 | XYZ => arc_clamp(2.0 * (q.x * q.z + q.w * q.y)).asin(), |
| 164 | XZY => arc_clamp(-2.0 * (q.x * q.y - q.w * q.z)).asin(), |
| 165 | #[allow(deprecated)] |
| 166 | ZYZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(), |
| 167 | #[allow(deprecated)] |
| 168 | ZXZ => arc_clamp(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z).acos(), |
| 169 | #[allow(deprecated)] |
| 170 | YXY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(), |
| 171 | #[allow(deprecated)] |
| 172 | YZY => arc_clamp(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z).acos(), |
| 173 | #[allow(deprecated)] |
| 174 | XYX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(), |
| 175 | #[allow(deprecated)] |
| 176 | XZX => arc_clamp(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z).acos(), |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | fn third(self, q: $quat) -> $t { |
| 181 | use EulerRot::*; |
| 182 | #[allow(deprecated)] |
| 183 | match self { |
| 184 | ZYX => (2.0 * (q.y * q.z + q.w * q.x)) |
| 185 | .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z), |
| 186 | ZXY => (-2.0 * (q.x * q.z - q.w * q.y)) |
| 187 | .atan2(q.w * q.w - q.x * q.x - q.y * q.y + q.z * q.z), |
| 188 | YXZ => (2.0 * (q.x * q.y + q.w * q.z)) |
| 189 | .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z), |
| 190 | YZX => (-2.0 * (q.y * q.z - q.w * q.x)) |
| 191 | .atan2(q.w * q.w - q.x * q.x + q.y * q.y - q.z * q.z), |
| 192 | XYZ => (-2.0 * (q.x * q.y - q.w * q.z)) |
| 193 | .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z), |
| 194 | XZY => (2.0 * (q.x * q.z + q.w * q.y)) |
| 195 | .atan2(q.w * q.w + q.x * q.x - q.y * q.y - q.z * q.z), |
| 196 | #[allow(deprecated)] |
| 197 | ZYZ => { |
| 198 | (2.0 * (q.y * q.z - q.w * q.x)).atan2_fixed(2.0 * (q.x * q.z + q.w * q.y)) |
| 199 | } |
| 200 | #[allow(deprecated)] |
| 201 | ZXZ => { |
| 202 | (2.0 * (q.x * q.z + q.w * q.y)).atan2_fixed(-2.0 * (q.y * q.z - q.w * q.x)) |
| 203 | } |
| 204 | #[allow(deprecated)] |
| 205 | YXY => { |
| 206 | (2.0 * (q.x * q.y - q.w * q.z)).atan2_fixed(2.0 * (q.y * q.z + q.w * q.x)) |
| 207 | } |
| 208 | #[allow(deprecated)] |
| 209 | YZY => { |
| 210 | (2.0 * (q.y * q.z + q.w * q.x)).atan2_fixed(-2.0 * (q.x * q.y - q.w * q.z)) |
| 211 | } |
| 212 | #[allow(deprecated)] |
| 213 | XYX => { |
| 214 | (2.0 * (q.x * q.y + q.w * q.z)).atan2_fixed(-2.0 * (q.x * q.z - q.w * q.y)) |
| 215 | } |
| 216 | #[allow(deprecated)] |
| 217 | XZX => { |
| 218 | (2.0 * (q.x * q.z - q.w * q.y)).atan2_fixed(2.0 * (q.x * q.y + q.w * q.z)) |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | // End - impl EulerFromQuaternion |
| 224 | }; |
| 225 | } |
| 226 | |
| 227 | macro_rules! impl_to_quatrust-analyzer cargo glam 0.21.3 euler/impl_to_quat! { |
| 228 | ($t:ty, $quat:ident) => { |
| 229 | impl EulerToQuaternion<$t> for EulerRot { |
| 230 | type Output = $quat; |
| 231 | #[inline(always)] |
| 232 | fn new_quat(self, u: $t, v: $t, w: $t) -> $quat { |
| 233 | use EulerRot::*; |
| 234 | #[inline(always)] |
| 235 | fn rot_x(a: $t) -> $quat { |
| 236 | $quat::from_rotation_x(a) |
| 237 | } |
| 238 | #[inline(always)] |
| 239 | fn rot_y(a: $t) -> $quat { |
| 240 | $quat::from_rotation_y(a) |
| 241 | } |
| 242 | #[inline(always)] |
| 243 | fn rot_z(a: $t) -> $quat { |
| 244 | $quat::from_rotation_z(a) |
| 245 | } |
| 246 | match self { |
| 247 | ZYX => rot_z(u) * rot_y(v) * rot_x(w), |
| 248 | ZXY => rot_z(u) * rot_x(v) * rot_y(w), |
| 249 | YXZ => rot_y(u) * rot_x(v) * rot_z(w), |
| 250 | YZX => rot_y(u) * rot_z(v) * rot_x(w), |
| 251 | XYZ => rot_x(u) * rot_y(v) * rot_z(w), |
| 252 | XZY => rot_x(u) * rot_z(v) * rot_y(w), |
| 253 | #[allow(deprecated)] |
| 254 | ZYZ => rot_z(u) * rot_y(v) * rot_z(w), |
| 255 | #[allow(deprecated)] |
| 256 | ZXZ => rot_z(u) * rot_x(v) * rot_z(w), |
| 257 | #[allow(deprecated)] |
| 258 | YXY => rot_y(u) * rot_x(v) * rot_y(w), |
| 259 | #[allow(deprecated)] |
| 260 | YZY => rot_y(u) * rot_z(v) * rot_y(w), |
| 261 | #[allow(deprecated)] |
| 262 | XYX => rot_x(u) * rot_y(v) * rot_x(w), |
| 263 | #[allow(deprecated)] |
| 264 | XZX => rot_x(u) * rot_z(v) * rot_x(w), |
| 265 | } |
| 266 | .normalize() |
| 267 | } |
| 268 | } |
| 269 | // End - impl EulerToQuaternion |
| 270 | }; |
| 271 | } |
| 272 | |
| 273 | impl_from_quatrust-analyzer cargo glam 0.21.3 euler/impl_from_quat!!(f32, Quatrust-analyzer cargo glam 0.21.3 f32/sse2/quat/Quat#); |
| 274 | impl_from_quatrust-analyzer cargo glam 0.21.3 euler/impl_from_quat!!(f64, DQuatrust-analyzer cargo glam 0.21.3 f64/dquat/DQuat#); |
| 275 | impl_to_quatrust-analyzer cargo glam 0.21.3 euler/impl_to_quat!!(f32, Quatrust-analyzer cargo glam 0.21.3 f32/sse2/quat/Quat#); |
| 276 | impl_to_quatrust-analyzer cargo glam 0.21.3 euler/impl_to_quat!!(f64, DQuatrust-analyzer cargo glam 0.21.3 f64/dquat/DQuat#); |