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