| 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 super::{DQuat, 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 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 22 | pub enum EulerRot { |
| 23 | /// Intrinsic three-axis rotation ZYX |
| 24 | ZYX, |
| 25 | /// Intrinsic three-axis rotation ZXY |
| 26 | ZXY, |
| 27 | /// Intrinsic three-axis rotation YXZ |
| 28 | YXZ, |
| 29 | /// Intrinsic three-axis rotation YZX |
| 30 | YZX, |
| 31 | /// Intrinsic three-axis rotation XYZ |
| 32 | XYZ, |
| 33 | /// Intrinsic three-axis rotation XZY |
| 34 | XZY, |
| 35 | |
| 36 | /// Intrinsic two-axis rotation ZYZ |
| 37 | #[deprecated(note = "Untested! Use at own risk!")] |
| 38 | ZYZ, |
| 39 | /// Intrinsic two-axis rotation ZXZ |
| 40 | #[deprecated(note = "Untested! Use at own risk!")] |
| 41 | ZXZ, |
| 42 | /// Intrinsic two-axis rotation YXY |
| 43 | #[deprecated(note = "Untested! Use at own risk!")] |
| 44 | YXY, |
| 45 | /// Intrinsic two-axis rotation YZY |
| 46 | #[deprecated(note = "Untested! Use at own risk!")] |
| 47 | YZY, |
| 48 | /// Intrinsic two-axis rotation XYX |
| 49 | #[deprecated(note = "Untested! Use at own risk!")] |
| 50 | XYX, |
| 51 | /// Intrinsic two-axis rotation XZX |
| 52 | #[deprecated(note = "Untested! Use at own risk!")] |
| 53 | XZX, |
| 54 | } |
| 55 | |
| 56 | impl Default for EulerRot { |
| 57 | /// Default `YXZ` as yaw (y-axis), pitch (x-axis), roll (z-axis). |
| 58 | fn default() -> Self { |
| 59 | Self::YXZ |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | /// Conversion from quaternion to euler angles. |
| 64 | pub(crate) trait EulerFromQuaternion<Q: Copy>: Sized + Copy { |
| 65 | type Output; |
| 66 | /// Compute the angle of the first axis (X-x-x) |
| 67 | fn first(self, q: Q) -> Self::Output; |
| 68 | /// Compute then angle of the second axis (x-X-x) |
| 69 | fn second(self, q: Q) -> Self::Output; |
| 70 | /// Compute then angle of the third axis (x-x-X) |
| 71 | fn third(self, q: Q) -> Self::Output; |
| 72 | |
| 73 | /// Compute all angles of a rotation in the notation order |
| 74 | fn convert_quat(self, q: Q) -> (Self::Output, Self::Output, Self::Output) { |
| 75 | (self.first(q), self.second(q), self.third(q)) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /// Conversion from euler angles to quaternion. |
| 80 | pub(crate) trait EulerToQuaternion<T>: Copy { |
| 81 | type Output; |
| 82 | /// Create the rotation quaternion for the three angles of this euler rotation sequence. |
| 83 | fn new_quat(self, u: T, v: T, w: T) -> Self::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(crate) trait Atan2Fixed<T = Self> { |
| 89 | fn atan2_fixed(self, other: T) -> T; |
| 90 | } |
| 91 | |
| 92 | impl Atan2Fixed for f32 { |
| 93 | fn atan2_fixed(self, other: f32) -> f32 { |
| 94 | self.atan2(if other == 0.0f32 { 0.0f32 } else { other }) |
| 95 | } |
| 96 | } |
| 97 | impl Atan2Fixed for f64 { |
| 98 | fn atan2_fixed(self, other: f64) -> f64 { |
| 99 | self.atan2(if other == 0.0f64 { 0.0f64 } else { other }) |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | macro_rules! 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_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_quat!(f32, Quat); |
| 274 | impl_from_quat!(f64, DQuat); |
| 275 | impl_to_quat!(f32, Quat); |
| 276 | impl_to_quat!(f64, DQuat); |