| 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.22.0 crate/::{DQuatrust-analyzer cargo glam 0.22.0 f64/dquat/DQuat#, Quatrust-analyzer cargo glam 0.22.0 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 | #[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:)] |
| 20 | #[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))] |
| 21 | pub enum EulerRotrust-analyzer cargo glam 0.22.0 euler/EulerRot# { |
| 22 | /// Intrinsic three-axis rotation ZYX |
| 23 | ZYXrust-analyzer cargo glam 0.22.0 euler/EulerRot#ZYX#, |
| 24 | /// Intrinsic three-axis rotation ZXY |
| 25 | ZXYrust-analyzer cargo glam 0.22.0 euler/EulerRot#ZXY#, |
| 26 | /// Intrinsic three-axis rotation YXZ |
| 27 | YXZrust-analyzer cargo glam 0.22.0 euler/EulerRot#YXZ#, |
| 28 | /// Intrinsic three-axis rotation YZX |
| 29 | YZXrust-analyzer cargo glam 0.22.0 euler/EulerRot#YZX#, |
| 30 | /// Intrinsic three-axis rotation XYZ |
| 31 | XYZrust-analyzer cargo glam 0.22.0 euler/EulerRot#XYZ#, |
| 32 | /// Intrinsic three-axis rotation XZY |
| 33 | XZYrust-analyzer cargo glam 0.22.0 euler/EulerRot#XZY#, |
| 34 | } |
| 35 | |
| 36 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for EulerRotrust-analyzer cargo glam 0.22.0 euler/EulerRot# { |
| 37 | /// Default `YXZ` as yaw (y-axis), pitch (x-axis), roll (z-axis). |
| 38 | fn defaultrust-analyzer cargo glam 0.22.0 euler/impl#[EulerRot][Default]default().() -> Selfrust-analyzer cargo glam 0.22.0 euler/impl#[EulerRot][Default] { |
| 39 | Selfrust-analyzer cargo glam 0.22.0 euler/impl#[EulerRot][Default]::YXZrust-analyzer cargo glam 0.22.0 euler/EulerRot#YXZ# |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Conversion from quaternion to euler angles. |
| 44 | pub(craterust-analyzer cargo glam 0.22.0 crate/) trait EulerFromQuaternionrust-analyzer cargo glam 0.22.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# { |
| 45 | type Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#; |
| 46 | /// Compute the angle of the first axis (X-x-x) |
| 47 | fn firstrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#first().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#; |
| 48 | /// Compute then angle of the second axis (x-X-x) |
| 49 | fn secondrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#second().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#; |
| 50 | /// Compute then angle of the third axis (x-x-X) |
| 51 | fn thirdrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#third().(self, q: Q) -> Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#; |
| 52 | |
| 53 | /// Compute all angles of a rotation in the notation order |
| 54 | fn convert_quatrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#convert_quat().(self, q: Q) -> (Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#, Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#Output#) { |
| 55 | (self.firstrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#first().(q), self.secondrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#second().(q), self.thirdrust-analyzer cargo glam 0.22.0 euler/EulerFromQuaternion#third().(q)) |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /// Conversion from euler angles to quaternion. |
| 60 | pub(craterust-analyzer cargo glam 0.22.0 crate/) trait EulerToQuaternionrust-analyzer cargo glam 0.22.0 euler/EulerToQuaternion#<T>: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# { |
| 61 | type Outputrust-analyzer cargo glam 0.22.0 euler/EulerToQuaternion#Output#; |
| 62 | /// Create the rotation quaternion for the three angles of this euler rotation sequence. |
| 63 | fn new_quatrust-analyzer cargo glam 0.22.0 euler/EulerToQuaternion#new_quat().(self, u: T, v: T, w: T) -> Self::Outputrust-analyzer cargo glam 0.22.0 euler/EulerToQuaternion#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(craterust-analyzer cargo glam 0.22.0 crate/) trait Atan2Fixedrust-analyzer cargo glam 0.22.0 euler/Atan2Fixed#<T = Self> { |
| 69 | fn atan2_fixedrust-analyzer cargo glam 0.22.0 euler/Atan2Fixed#atan2_fixed().(self, other: T) -> T; |
| 70 | } |
| 71 | |
| 72 | impl Atan2Fixedrust-analyzer cargo glam 0.22.0 euler/Atan2Fixed# for f32 { |
| 73 | fn atan2_fixedrust-analyzer cargo glam 0.22.0 euler/impl#[f32][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f32) -> f32 { |
| 74 | 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 }) |
| 75 | } |
| 76 | } |
| 77 | impl Atan2Fixedrust-analyzer cargo glam 0.22.0 euler/Atan2Fixed# for f64 { |
| 78 | fn atan2_fixedrust-analyzer cargo glam 0.22.0 euler/impl#[f64][`Atan2Fixed<Self>`]atan2_fixed().(self, other: f64) -> f64 { |
| 79 | 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 }) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | macro_rules! impl_from_quatrust-analyzer cargo glam 0.22.0 euler/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_quatrust-analyzer cargo glam 0.22.0 euler/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_quatrust-analyzer cargo glam 0.22.0 euler/impl_from_quat!!(f32, Quatrust-analyzer cargo glam 0.22.0 f32/sse2/quat/Quat#); |
| 181 | impl_from_quatrust-analyzer cargo glam 0.22.0 euler/impl_from_quat!!(f64, DQuatrust-analyzer cargo glam 0.22.0 f64/dquat/DQuat#); |
| 182 | impl_to_quatrust-analyzer cargo glam 0.22.0 euler/impl_to_quat!!(f32, Quatrust-analyzer cargo glam 0.22.0 f32/sse2/quat/Quat#); |
| 183 | impl_to_quatrust-analyzer cargo glam 0.22.0 euler/impl_to_quat!!(f64, DQuatrust-analyzer cargo glam 0.22.0 f64/dquat/DQuat#); |