| 1 | use crate::core::traits::{ |
| 2 | quaternion::Quaternion, |
| 3 | vector::{FloatVector4, MaskVector4, Vector, Vector4, Vector4Const}, |
| 4 | }; |
| 5 | use crate::euler::{EulerFromQuaternion, EulerRot, EulerToQuaternion}; |
| 6 | use crate::{DMat3, DMat4, DVec3, DVec4}; |
| 7 | use crate::{Mat3, Mat4, Vec3, Vec3A, Vec4}; |
| 8 | |
| 9 | #[cfg(not(feature = "std"))] |
| 10 | use num_traits::Float; |
| 11 | |
| 12 | #[cfg(all( |
| 13 | target_arch = "x86", |
| 14 | target_feature = "sse2", |
| 15 | not(feature = "scalar-math") |
| 16 | ))] |
| 17 | use core::arch::x86::*; |
| 18 | #[cfg(all( |
| 19 | target_arch = "x86_64", |
| 20 | target_feature = "sse2", |
| 21 | not(feature = "scalar-math") |
| 22 | ))] |
| 23 | use core::arch::x86_64::*; |
| 24 | |
| 25 | #[cfg(not(target_arch = "spirv"))] |
| 26 | use core::fmt; |
| 27 | use core::ops::{Add, Deref, Div, Mul, MulAssign, Neg, Sub}; |
| 28 | |
| 29 | #[cfg(feature = "std")] |
| 30 | use std::iter::{Product, Sum}; |
| 31 | |
| 32 | macro_rules! impl_quat_methods { |
| 33 | ($t:ident, $quat:ident, $vec3:ident, $vec4:ident, $mat3:ident, $mat4:ident, $inner:ident) => { |
| 34 | /// The identity quaternion. Corresponds to no rotation. |
| 35 | pub const IDENTITY: Self = Self($inner::W); |
| 36 | |
| 37 | /// Creates a new rotation quaternion. |
| 38 | /// |
| 39 | /// This should generally not be called manually unless you know what you are doing. |
| 40 | /// Use one of the other constructors instead such as `identity` or `from_axis_angle`. |
| 41 | /// |
| 42 | /// `from_xyzw` is mostly used by unit tests and `serde` deserialization. |
| 43 | /// |
| 44 | /// # Preconditions |
| 45 | /// |
| 46 | /// This function does not check if the input is normalized, it is up to the user to |
| 47 | /// provide normalized input or to normalized the resulting quaternion. |
| 48 | #[inline(always)] |
| 49 | pub fn from_xyzw(x: $t, y: $t, z: $t, w: $t) -> Self { |
| 50 | Self(Vector4::new(x, y, z, w)) |
| 51 | } |
| 52 | |
| 53 | /// Creates a rotation quaternion from an array. |
| 54 | /// |
| 55 | /// # Preconditions |
| 56 | /// |
| 57 | /// This function does not check if the input is normalized, it is up to the user to |
| 58 | /// provide normalized input or to normalized the resulting quaternion. |
| 59 | #[inline(always)] |
| 60 | pub fn from_array(a: [$t; 4]) -> Self { |
| 61 | let q = Vector4::from_array(a); |
| 62 | Self(q) |
| 63 | } |
| 64 | |
| 65 | /// Creates a new rotation quaternion from a 4D vector. |
| 66 | /// |
| 67 | /// # Preconditions |
| 68 | /// |
| 69 | /// This function does not check if the input is normalized, it is up to the user to |
| 70 | /// provide normalized input or to normalized the resulting quaternion. |
| 71 | #[inline(always)] |
| 72 | pub fn from_vec4(v: $vec4) -> Self { |
| 73 | Self(v.0) |
| 74 | } |
| 75 | |
| 76 | #[deprecated(since = "0.15.2", note = "Please use `from_slice()` instead")] |
| 77 | #[inline(always)] |
| 78 | pub fn from_slice_unaligned(slice: &[$t]) -> Self { |
| 79 | Self::from_slice(slice) |
| 80 | } |
| 81 | |
| 82 | /// Creates a rotation quaternion from a slice. |
| 83 | /// |
| 84 | /// # Preconditions |
| 85 | /// |
| 86 | /// This function does not check if the input is normalized, it is up to the user to |
| 87 | /// provide normalized input or to normalized the resulting quaternion. |
| 88 | /// |
| 89 | /// # Panics |
| 90 | /// |
| 91 | /// Panics if `slice` length is less than 4. |
| 92 | #[inline(always)] |
| 93 | pub fn from_slice(slice: &[$t]) -> Self { |
| 94 | #[allow(clippy::let_and_return)] |
| 95 | let q = Vector4::from_slice_unaligned(slice); |
| 96 | Self(q) |
| 97 | } |
| 98 | |
| 99 | #[deprecated(since = "0.15.2", note = "Please use `write_to_slice()` instead")] |
| 100 | #[inline(always)] |
| 101 | pub fn write_to_slice_unaligned(self, slice: &mut [$t]) { |
| 102 | self.write_to_slice(slice) |
| 103 | } |
| 104 | |
| 105 | /// Writes the quaternion to an unaligned slice. |
| 106 | /// |
| 107 | /// # Panics |
| 108 | /// |
| 109 | /// Panics if `slice` length is less than 4. |
| 110 | #[inline(always)] |
| 111 | pub fn write_to_slice(self, slice: &mut [$t]) { |
| 112 | Vector4::write_to_slice_unaligned(self.0, slice) |
| 113 | } |
| 114 | |
| 115 | /// Create a quaternion for a normalized rotation `axis` and `angle` (in radians). |
| 116 | /// The axis must be normalized (unit-length). |
| 117 | #[inline(always)] |
| 118 | pub fn from_axis_angle(axis: $vec3, angle: $t) -> Self { |
| 119 | Self($inner::from_axis_angle(axis.0, angle)) |
| 120 | } |
| 121 | |
| 122 | /// Create a quaternion that rotates `v.length()` radians around `v.normalize()`. |
| 123 | /// |
| 124 | /// `from_scaled_axis(Vec3::ZERO)` results in the identity quaternion. |
| 125 | #[inline(always)] |
| 126 | pub fn from_scaled_axis(v: $vec3) -> Self { |
| 127 | // Self($inner::from_scaled_axis(v.0)) |
| 128 | let length = v.length(); |
| 129 | if length == 0.0 { |
| 130 | Self::IDENTITY |
| 131 | } else { |
| 132 | Self::from_axis_angle(v / length, length) |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /// Creates a quaternion from the `angle` (in radians) around the x axis. |
| 137 | #[inline(always)] |
| 138 | pub fn from_rotation_x(angle: $t) -> Self { |
| 139 | Self($inner::from_rotation_x(angle)) |
| 140 | } |
| 141 | |
| 142 | /// Creates a quaternion from the `angle` (in radians) around the y axis. |
| 143 | #[inline(always)] |
| 144 | pub fn from_rotation_y(angle: $t) -> Self { |
| 145 | Self($inner::from_rotation_y(angle)) |
| 146 | } |
| 147 | |
| 148 | /// Creates a quaternion from the `angle` (in radians) around the z axis. |
| 149 | #[inline(always)] |
| 150 | pub fn from_rotation_z(angle: $t) -> Self { |
| 151 | Self($inner::from_rotation_z(angle)) |
| 152 | } |
| 153 | |
| 154 | #[deprecated( |
| 155 | since = "0.15.0", |
| 156 | note = "Please use `from_euler(EulerRot::YXZ, yaw, pitch, roll)` instead" |
| 157 | )] |
| 158 | #[inline(always)] |
| 159 | pub fn from_rotation_ypr(yaw: $t, pitch: $t, roll: $t) -> Self { |
| 160 | Self::from_euler(EulerRot::YXZ, yaw, pitch, roll) |
| 161 | } |
| 162 | |
| 163 | #[inline(always)] |
| 164 | /// Creates a quaternion from the given euler rotation sequence and the angles (in radians). |
| 165 | pub fn from_euler(euler: EulerRot, a: $t, b: $t, c: $t) -> Self { |
| 166 | euler.new_quat(a, b, c) |
| 167 | } |
| 168 | |
| 169 | #[deprecated(since = "0.15.0", note = "Please use `from_mat3` instead")] |
| 170 | pub fn from_rotation_mat3(mat: &$mat3) -> Self { |
| 171 | Self::from_mat3(mat) |
| 172 | } |
| 173 | |
| 174 | /// Creates a quaternion from a 3x3 rotation matrix. |
| 175 | #[inline] |
| 176 | pub fn from_mat3(mat: &$mat3) -> Self { |
| 177 | Self(Quaternion::from_rotation_axes( |
| 178 | mat.x_axis.0, |
| 179 | mat.y_axis.0, |
| 180 | mat.z_axis.0, |
| 181 | )) |
| 182 | } |
| 183 | |
| 184 | #[deprecated(since = "0.15.0", note = "Please use `from_mat4` instead")] |
| 185 | pub fn from_rotation_mat4(mat: &$mat4) -> Self { |
| 186 | Self::from_mat4(mat) |
| 187 | } |
| 188 | |
| 189 | /// Creates a quaternion from a 3x3 rotation matrix inside a homogeneous 4x4 matrix. |
| 190 | #[inline] |
| 191 | pub fn from_mat4(mat: &$mat4) -> Self { |
| 192 | Self(Quaternion::from_rotation_axes( |
| 193 | mat.x_axis.0.into(), |
| 194 | mat.y_axis.0.into(), |
| 195 | mat.z_axis.0.into(), |
| 196 | )) |
| 197 | } |
| 198 | |
| 199 | /// Gets the minimal rotation for transforming `from` to `to`. |
| 200 | /// The rotation is in the plane spanned by the two vectors. |
| 201 | /// Will rotate at most 180 degrees. |
| 202 | /// |
| 203 | /// The input vectors must be normalized (unit-length). |
| 204 | /// |
| 205 | /// `from_rotation_arc(from, to) * from ≈ to`. |
| 206 | /// |
| 207 | /// For near-singular cases (from≈to and from≈-to) the current implementation |
| 208 | /// is only accurate to about 0.001 (for `f32`). |
| 209 | #[cfg(feature = "std")] |
| 210 | pub fn from_rotation_arc(from: $vec3, to: $vec3) -> Self { |
| 211 | glam_assert!(from.is_normalized()); |
| 212 | glam_assert!(to.is_normalized()); |
| 213 | |
| 214 | let one_minus_eps = 1.0 - 2.0 * core::$t::EPSILON; |
| 215 | let dot = from.dot(to); |
| 216 | if dot > one_minus_eps { |
| 217 | // 0° singulary: from ≈ to |
| 218 | Self::IDENTITY |
| 219 | } else if dot < -one_minus_eps { |
| 220 | // 180° singulary: from ≈ -to |
| 221 | let pi = core::$t::consts::PI; // half a turn = 𝛕/2 = 180° |
| 222 | Self::from_axis_angle(from.any_orthonormal_vector(), pi) |
| 223 | } else { |
| 224 | let c = from.cross(to); |
| 225 | Self::from_xyzw(c.x, c.y, c.z, 1.0 + dot).normalize() |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | /// Gets the minimal rotation for transforming `from` to either `to` or `-to`. |
| 230 | /// This means that the resulting quaternion will rotate `from` so that it is colinear with `to`. |
| 231 | /// |
| 232 | /// The rotation is in the plane spanned by the two vectors. |
| 233 | /// Will rotate at most 90 degrees. |
| 234 | /// |
| 235 | /// The input vectors must be normalized (unit-length). |
| 236 | /// |
| 237 | /// `to.dot(from_rotation_arc_colinear(from, to) * from).abs() ≈ 1`. |
| 238 | #[cfg(feature = "std")] |
| 239 | pub fn from_rotation_arc_colinear(from: $vec3, to: $vec3) -> Self { |
| 240 | if from.dot(to) < 0.0 { |
| 241 | Self::from_rotation_arc(from, -to) |
| 242 | } else { |
| 243 | Self::from_rotation_arc(from, to) |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | /// Returns the rotation axis and angle (in radians) of `self`. |
| 248 | #[inline(always)] |
| 249 | pub fn to_axis_angle(self) -> ($vec3, $t) { |
| 250 | let (axis, angle) = self.0.to_axis_angle(); |
| 251 | ($vec3(axis), angle) |
| 252 | } |
| 253 | |
| 254 | /// Returns the rotation axis scaled by the rotation in radians. |
| 255 | #[inline(always)] |
| 256 | pub fn to_scaled_axis(self) -> $vec3 { |
| 257 | let (axis, angle) = self.0.to_axis_angle(); |
| 258 | $vec3(axis) * angle |
| 259 | } |
| 260 | |
| 261 | /// Returns the rotation angles for the given euler rotation sequence. |
| 262 | #[inline(always)] |
| 263 | pub fn to_euler(self, euler: EulerRot) -> ($t, $t, $t) { |
| 264 | euler.convert_quat(self) |
| 265 | } |
| 266 | |
| 267 | /// Returns the quaternion conjugate of `self`. For a unit quaternion the |
| 268 | /// conjugate is also the inverse. |
| 269 | #[must_use] |
| 270 | #[inline(always)] |
| 271 | pub fn conjugate(self) -> Self { |
| 272 | Self(self.0.conjugate()) |
| 273 | } |
| 274 | |
| 275 | /// Returns the inverse of a normalized quaternion. |
| 276 | /// |
| 277 | /// Typically quaternion inverse returns the conjugate of a normalized quaternion. |
| 278 | /// Because `self` is assumed to already be unit length this method *does not* normalize |
| 279 | /// before returning the conjugate. |
| 280 | #[must_use] |
| 281 | #[inline(always)] |
| 282 | pub fn inverse(self) -> Self { |
| 283 | glam_assert!(self.is_normalized()); |
| 284 | self.conjugate() |
| 285 | } |
| 286 | |
| 287 | /// Computes the dot product of `self` and `other`. The dot product is |
| 288 | /// equal to the the cosine of the angle between two quaternion rotations. |
| 289 | #[inline(always)] |
| 290 | pub fn dot(self, other: Self) -> $t { |
| 291 | Vector4::dot(self.0, other.0) |
| 292 | } |
| 293 | |
| 294 | /// Computes the length of `self`. |
| 295 | #[cfg_attr(docsrs, doc(alias = "magnitude"))] |
| 296 | #[inline(always)] |
| 297 | pub fn length(self) -> $t { |
| 298 | FloatVector4::length(self.0) |
| 299 | } |
| 300 | |
| 301 | /// Computes the squared length of `self`. |
| 302 | /// |
| 303 | /// This is generally faster than `length()` as it avoids a square |
| 304 | /// root operation. |
| 305 | #[cfg_attr(docsrs, doc(alias = "magnitude2"))] |
| 306 | #[inline(always)] |
| 307 | pub fn length_squared(self) -> $t { |
| 308 | FloatVector4::length_squared(self.0) |
| 309 | } |
| 310 | |
| 311 | /// Computes `1.0 / length()`. |
| 312 | /// |
| 313 | /// For valid results, `self` must _not_ be of length zero. |
| 314 | #[inline(always)] |
| 315 | pub fn length_recip(self) -> $t { |
| 316 | FloatVector4::length_recip(self.0) |
| 317 | } |
| 318 | |
| 319 | /// Returns `self` normalized to length 1.0. |
| 320 | /// |
| 321 | /// For valid results, `self` must _not_ be of length zero. |
| 322 | #[must_use] |
| 323 | #[inline(always)] |
| 324 | pub fn normalize(self) -> Self { |
| 325 | Self(FloatVector4::normalize(self.0)) |
| 326 | } |
| 327 | |
| 328 | /// Returns `true` if, and only if, all elements are finite. |
| 329 | /// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 330 | #[inline(always)] |
| 331 | pub fn is_finite(self) -> bool { |
| 332 | FloatVector4::is_finite(self.0) |
| 333 | } |
| 334 | |
| 335 | #[inline(always)] |
| 336 | pub fn is_nan(self) -> bool { |
| 337 | FloatVector4::is_nan(self.0) |
| 338 | } |
| 339 | |
| 340 | /// Returns whether `self` of length `1.0` or not. |
| 341 | /// |
| 342 | /// Uses a precision threshold of `1e-6`. |
| 343 | #[inline(always)] |
| 344 | pub fn is_normalized(self) -> bool { |
| 345 | FloatVector4::is_normalized(self.0) |
| 346 | } |
| 347 | |
| 348 | #[inline(always)] |
| 349 | pub fn is_near_identity(self) -> bool { |
| 350 | self.0.is_near_identity() |
| 351 | } |
| 352 | |
| 353 | /// Returns the angle (in radians) for the minimal rotation |
| 354 | /// for transforming this quaternion into another. |
| 355 | /// |
| 356 | /// Both quaternions must be normalized. |
| 357 | pub fn angle_between(self, other: Self) -> $t { |
| 358 | glam_assert!(self.is_normalized() && other.is_normalized()); |
| 359 | use crate::core::traits::scalar::FloatEx; |
| 360 | self.dot(other).abs().acos_approx() * 2.0 |
| 361 | } |
| 362 | |
| 363 | /// Returns true if the absolute difference of all elements between `self` and `other` |
| 364 | /// is less than or equal to `max_abs_diff`. |
| 365 | /// |
| 366 | /// This can be used to compare if two quaternions contain similar elements. It works |
| 367 | /// best when comparing with a known value. The `max_abs_diff` that should be used used |
| 368 | /// depends on the values being compared against. |
| 369 | /// |
| 370 | /// For more see |
| 371 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 372 | #[inline(always)] |
| 373 | pub fn abs_diff_eq(self, other: Self, max_abs_diff: $t) -> bool { |
| 374 | FloatVector4::abs_diff_eq(self.0, other.0, max_abs_diff) |
| 375 | } |
| 376 | |
| 377 | /// Performs a linear interpolation between `self` and `other` based on |
| 378 | /// the value `s`. |
| 379 | /// |
| 380 | /// When `s` is `0.0`, the result will be equal to `self`. When `s` |
| 381 | /// is `1.0`, the result will be equal to `other`. |
| 382 | #[inline(always)] |
| 383 | #[cfg_attr(docsrs, doc(alias = "mix"))] |
| 384 | pub fn lerp(self, end: Self, s: $t) -> Self { |
| 385 | Self(self.0.lerp(end.0, s)) |
| 386 | } |
| 387 | |
| 388 | /// Performs a spherical linear interpolation between `self` and `end` |
| 389 | /// based on the value `s`. |
| 390 | /// |
| 391 | /// When `s` is `0.0`, the result will be equal to `self`. When `s` |
| 392 | /// is `1.0`, the result will be equal to `end`. |
| 393 | /// |
| 394 | /// Note that a rotation can be represented by two quaternions: `q` and |
| 395 | /// `-q`. The slerp path between `q` and `end` will be different from the |
| 396 | /// path between `-q` and `end`. One path will take the long way around and |
| 397 | /// one will take the short way. In order to correct for this, the `dot` |
| 398 | /// product between `self` and `end` should be positive. If the `dot` |
| 399 | /// product is negative, slerp between `-self` and `end`. |
| 400 | #[inline(always)] |
| 401 | pub fn slerp(self, end: Self, s: $t) -> Self { |
| 402 | Self(self.0.slerp(end.0, s)) |
| 403 | } |
| 404 | |
| 405 | /// Multiplies a quaternion and a 3D vector, returning the rotated vector. |
| 406 | #[inline(always)] |
| 407 | pub fn mul_vec3(self, other: $vec3) -> $vec3 { |
| 408 | $vec3(self.0.mul_vector3(other.0)) |
| 409 | } |
| 410 | |
| 411 | /// Multiplies two quaternions. |
| 412 | /// If they each represent a rotation, the result will represent the combined rotation. |
| 413 | /// Note that due to floating point rounding the result may not be perfectly normalized. |
| 414 | #[inline(always)] |
| 415 | pub fn mul_quat(self, other: Self) -> Self { |
| 416 | Self(self.0.mul_quaternion(other.0)) |
| 417 | } |
| 418 | }; |
| 419 | } |
| 420 | |
| 421 | macro_rules! impl_quat_traits { |
| 422 | ($t:ty, $new:ident, $quat:ident, $vec3:ident, $vec4:ident, $inner:ident) => { |
| 423 | /// Creates a quaternion from `x`, `y`, `z` and `w` values. |
| 424 | /// |
| 425 | /// This should generally not be called manually unless you know what you are doing. Use |
| 426 | /// one of the other constructors instead such as `identity` or `from_axis_angle`. |
| 427 | #[inline] |
| 428 | pub fn $new(x: $t, y: $t, z: $t, w: $t) -> $quat { |
| 429 | $quat::from_xyzw(x, y, z, w) |
| 430 | } |
| 431 | |
| 432 | #[cfg(not(target_arch = "spirv"))] |
| 433 | impl fmt::Debug for $quat { |
| 434 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 435 | fmt.debug_tuple(stringify!($quat)) |
| 436 | .field(&self.x) |
| 437 | .field(&self.y) |
| 438 | .field(&self.z) |
| 439 | .field(&self.w) |
| 440 | .finish() |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | #[cfg(not(target_arch = "spirv"))] |
| 445 | impl fmt::Display for $quat { |
| 446 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 447 | write!(fmt, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w) |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | impl Add<$quat> for $quat { |
| 452 | type Output = Self; |
| 453 | /// Adds two quaternions. |
| 454 | /// The sum is not guaranteed to be normalized. |
| 455 | /// |
| 456 | /// NB: Addition is not the same as combining the rotations represented by the two quaternions! |
| 457 | /// That corresponds to multiplication. |
| 458 | #[inline] |
| 459 | fn add(self, other: Self) -> Self { |
| 460 | Self(self.0.add(other.0)) |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | impl Sub<$quat> for $quat { |
| 465 | type Output = Self; |
| 466 | /// Subtracts the other quaternion from self. |
| 467 | /// The difference is not guaranteed to be normalized. |
| 468 | #[inline] |
| 469 | fn sub(self, other: Self) -> Self { |
| 470 | Self(self.0.sub(other.0)) |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | impl Mul<$t> for $quat { |
| 475 | type Output = Self; |
| 476 | /// Multiplies a quaternion by a scalar value. |
| 477 | /// The product is not guaranteed to be normalized. |
| 478 | #[inline] |
| 479 | fn mul(self, other: $t) -> Self { |
| 480 | Self(self.0.scale(other)) |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | impl Div<$t> for $quat { |
| 485 | type Output = Self; |
| 486 | /// Divides a quaternion by a scalar value. |
| 487 | /// The quotient is not guaranteed to be normalized. |
| 488 | #[inline] |
| 489 | fn div(self, other: $t) -> Self { |
| 490 | Self(self.0.scale(other.recip())) |
| 491 | } |
| 492 | } |
| 493 | |
| 494 | impl Mul<$quat> for $quat { |
| 495 | type Output = Self; |
| 496 | #[inline] |
| 497 | fn mul(self, other: Self) -> Self { |
| 498 | Self(self.0.mul_quaternion(other.0)) |
| 499 | } |
| 500 | } |
| 501 | |
| 502 | impl MulAssign<$quat> for $quat { |
| 503 | #[inline] |
| 504 | fn mul_assign(&mut self, other: Self) { |
| 505 | self.0 = self.0.mul_quaternion(other.0); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | impl Mul<$vec3> for $quat { |
| 510 | type Output = $vec3; |
| 511 | #[inline] |
| 512 | fn mul(self, other: $vec3) -> Self::Output { |
| 513 | $vec3(self.0.mul_vector3(other.0)) |
| 514 | } |
| 515 | } |
| 516 | |
| 517 | impl Neg for $quat { |
| 518 | type Output = Self; |
| 519 | #[inline] |
| 520 | fn neg(self) -> Self { |
| 521 | Self(self.0.scale(-1.0)) |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | impl Default for $quat { |
| 526 | #[inline] |
| 527 | fn default() -> Self { |
| 528 | Self::IDENTITY |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | impl PartialEq for $quat { |
| 533 | #[inline] |
| 534 | fn eq(&self, other: &Self) -> bool { |
| 535 | MaskVector4::all(self.0.cmpeq(other.0)) |
| 536 | } |
| 537 | } |
| 538 | |
| 539 | impl AsRef<[$t; 4]> for $quat { |
| 540 | #[inline(always)] |
| 541 | fn as_ref(&self) -> &[$t; 4] { |
| 542 | unsafe { &*(self as *const Self as *const [$t; 4]) } |
| 543 | } |
| 544 | } |
| 545 | |
| 546 | impl AsMut<[$t; 4]> for $quat { |
| 547 | #[inline(always)] |
| 548 | fn as_mut(&mut self) -> &mut [$t; 4] { |
| 549 | unsafe { &mut *(self as *mut Self as *mut [$t; 4]) } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | impl From<$quat> for $vec4 { |
| 554 | #[inline(always)] |
| 555 | fn from(q: $quat) -> Self { |
| 556 | $vec4(q.0) |
| 557 | } |
| 558 | } |
| 559 | |
| 560 | impl From<$quat> for ($t, $t, $t, $t) { |
| 561 | #[inline(always)] |
| 562 | fn from(q: $quat) -> Self { |
| 563 | Vector4::into_tuple(q.0) |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | impl From<$quat> for [$t; 4] { |
| 568 | #[inline(always)] |
| 569 | fn from(q: $quat) -> Self { |
| 570 | Vector4::into_array(q.0) |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | impl From<$quat> for $inner { |
| 575 | // TODO: write test |
| 576 | #[inline(always)] |
| 577 | fn from(q: $quat) -> Self { |
| 578 | q.0 |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | impl Deref for $quat { |
| 583 | type Target = crate::XYZW<$t>; |
| 584 | #[inline(always)] |
| 585 | fn deref(&self) -> &Self::Target { |
| 586 | self.0.as_ref_xyzw() |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | #[cfg(feature = "std")] |
| 591 | impl<'a> Sum<&'a Self> for $quat { |
| 592 | fn sum<I>(iter: I) -> Self |
| 593 | where |
| 594 | I: Iterator<Item = &'a Self>, |
| 595 | { |
| 596 | use crate::core::traits::vector::VectorConst; |
| 597 | iter.fold(Self($inner::ZERO), |a, &b| Self::add(a, b)) |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | #[cfg(feature = "std")] |
| 602 | impl<'a> Product<&'a Self> for $quat { |
| 603 | fn product<I>(iter: I) -> Self |
| 604 | where |
| 605 | I: Iterator<Item = &'a Self>, |
| 606 | { |
| 607 | iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b)) |
| 608 | } |
| 609 | } |
| 610 | }; |
| 611 | } |
| 612 | |
| 613 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 614 | type InnerF32 = __m128; |
| 615 | |
| 616 | #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))] |
| 617 | type InnerF32 = crate::XYZW<f32>; |
| 618 | |
| 619 | /// A quaternion representing an orientation. |
| 620 | /// |
| 621 | /// This quaternion is intended to be of unit length but may denormalize due to |
| 622 | /// floating point "error creep" which can occur when successive quaternion |
| 623 | /// operations are applied. |
| 624 | /// |
| 625 | /// This type is 16 byte aligned. |
| 626 | #[derive(Clone, Copy)] |
| 627 | #[cfg_attr( |
| 628 | not(any(feature = "scalar-math", target_arch = "spriv")), |
| 629 | repr(align(16)) |
| 630 | )] |
| 631 | #[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))] |
| 632 | pub struct Quat(pub(crate) InnerF32); |
| 633 | |
| 634 | impl Quat { |
| 635 | impl_quat_methods!(f32, Quat, Vec3, Vec4, Mat3, Mat4, InnerF32); |
| 636 | |
| 637 | /// Multiplies a quaternion and a 3D vector, returning the rotated vector. |
| 638 | #[inline(always)] |
| 639 | pub fn mul_vec3a(self, other: Vec3A) -> Vec3A { |
| 640 | #[allow(clippy::useless_conversion)] |
| 641 | Vec3A(self.0.mul_float4_as_vector3(other.0.into()).into()) |
| 642 | } |
| 643 | |
| 644 | #[inline(always)] |
| 645 | pub fn as_f64(self) -> DQuat { |
| 646 | DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64) |
| 647 | } |
| 648 | |
| 649 | /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform. |
| 650 | #[inline] |
| 651 | pub fn from_affine3(mat: &crate::Affine3A) -> Self { |
| 652 | Self(Quaternion::from_rotation_axes( |
| 653 | mat.x_axis.0.into(), |
| 654 | mat.y_axis.0.into(), |
| 655 | mat.z_axis.0.into(), |
| 656 | )) |
| 657 | } |
| 658 | } |
| 659 | impl_quat_traits!(f32, quat, Quat, Vec3, Vec4, InnerF32); |
| 660 | |
| 661 | impl Mul<Vec3A> for Quat { |
| 662 | type Output = Vec3A; |
| 663 | #[inline(always)] |
| 664 | fn mul(self, other: Vec3A) -> Self::Output { |
| 665 | self.mul_vec3a(other) |
| 666 | } |
| 667 | } |
| 668 | |
| 669 | type InnerF64 = crate::XYZW<f64>; |
| 670 | |
| 671 | /// A quaternion representing an orientation. |
| 672 | /// |
| 673 | /// This quaternion is intended to be of unit length but may denormalize due to |
| 674 | /// floating point "error creep" which can occur when successive quaternion |
| 675 | /// operations are applied. |
| 676 | #[derive(Clone, Copy)] |
| 677 | #[repr(transparent)] |
| 678 | pub struct DQuat(pub(crate) InnerF64); |
| 679 | |
| 680 | impl DQuat { |
| 681 | impl_quat_methods!(f64, DQuat, DVec3, DVec4, DMat3, DMat4, InnerF64); |
| 682 | |
| 683 | #[inline(always)] |
| 684 | pub fn as_f32(self) -> Quat { |
| 685 | Quat::from_xyzw(self.x as f32, self.y as f32, self.z as f32, self.w as f32) |
| 686 | } |
| 687 | |
| 688 | /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform. |
| 689 | #[inline] |
| 690 | pub fn from_affine3(mat: &crate::DAffine3) -> Self { |
| 691 | Self(Quaternion::from_rotation_axes( |
| 692 | mat.x_axis.0, |
| 693 | mat.y_axis.0, |
| 694 | mat.z_axis.0, |
| 695 | )) |
| 696 | } |
| 697 | } |
| 698 | impl_quat_traits!(f64, dquat, DQuat, DVec3, DVec4, InnerF64); |
| 699 | |
| 700 | #[cfg(any(feature = "scalar-math", target_arch = "spriv"))] |
| 701 | mod const_test_quat { |
| 702 | const_assert_eq!(4, core::mem::align_of::<super::Quat>()); |
| 703 | const_assert_eq!(16, core::mem::size_of::<super::Quat>()); |
| 704 | } |
| 705 | |
| 706 | #[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))] |
| 707 | mod const_test_quat { |
| 708 | const_assert_eq!(16, core::mem::align_of::<super::Quat>()); |
| 709 | const_assert_eq!(16, core::mem::size_of::<super::Quat>()); |
| 710 | } |
| 711 | |
| 712 | mod const_test_dquat { |
| 713 | const_assert_eq!(8, core::mem::align_of::<super::DQuat>()); |
| 714 | const_assert_eq!(32, core::mem::size_of::<super::DQuat>()); |
| 715 | } |