glam/f32/sse2/
quat.rs

1// Generated from quat.rs.tera template. Edit the template, not the generated file.
2
3use crate::{
4    euler::{EulerRot, FromEuler, ToEuler},
5    f32::math,
6    sse2::*,
7    DQuat, Mat3, Mat3A, Mat4, Vec2, Vec3, Vec3A, Vec4,
8};
9
10#[cfg(target_arch = "x86")]
11use core::arch::x86::*;
12#[cfg(target_arch = "x86_64")]
13use core::arch::x86_64::*;
14
15use core::fmt;
16use core::iter::{Product, Sum};
17use core::ops::{
18    Add, AddAssign, Deref, DerefMut, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign,
19};
20
21#[cfg(feature = "zerocopy")]
22use zerocopy_derive::*;
23
24#[repr(C)]
25union UnionCast {
26    a: [f32; 4],
27    v: Quat,
28}
29
30/// Creates a quaternion from `x`, `y`, `z` and `w` values.
31///
32/// This should generally not be called manually unless you know what you are doing. Use
33/// one of the other constructors instead such as `identity` or `from_axis_angle`.
34#[inline]
35#[must_use]
36pub const fn quat(x: f32, y: f32, z: f32, w: f32) -> Quat {
37    Quat::from_xyzw(x, y, z, w)
38}
39
40/// A quaternion representing an orientation.
41///
42/// This quaternion is intended to be of unit length but may denormalize due to
43/// floating point "error creep" which can occur when successive quaternion
44/// operations are applied.
45///
46/// SIMD vector types are used for storage on supported platforms.
47///
48/// This type is 16 byte aligned.
49#[derive(Clone, Copy)]
50#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
51#[cfg_attr(
52    feature = "zerocopy",
53    derive(FromBytes, Immutable, IntoBytes, KnownLayout)
54)]
55#[repr(transparent)]
56pub struct Quat(pub(crate) __m128);
57
58impl Quat {
59    /// All zeros.
60    const ZERO: Self = Self::from_array([0.0; 4]);
61
62    /// The identity quaternion. Corresponds to no rotation.
63    pub const IDENTITY: Self = Self::from_xyzw(0.0, 0.0, 0.0, 1.0);
64
65    /// All NANs.
66    pub const NAN: Self = Self::from_array([f32::NAN; 4]);
67
68    /// Creates a new rotation quaternion.
69    ///
70    /// This should generally not be called manually unless you know what you are doing.
71    /// Use one of the other constructors instead such as `identity` or `from_axis_angle`.
72    ///
73    /// `from_xyzw` is mostly used by unit tests and `serde` deserialization.
74    ///
75    /// # Preconditions
76    ///
77    /// This function does not check if the input is normalized, it is up to the user to
78    /// provide normalized input or to normalized the resulting quaternion.
79    #[inline(always)]
80    #[must_use]
81    pub const fn from_xyzw(x: f32, y: f32, z: f32, w: f32) -> Self {
82        unsafe { UnionCast { a: [x, y, z, w] }.v }
83    }
84
85    /// Creates a rotation quaternion from an array.
86    ///
87    /// # Preconditions
88    ///
89    /// This function does not check if the input is normalized, it is up to the user to
90    /// provide normalized input or to normalized the resulting quaternion.
91    #[inline]
92    #[must_use]
93    pub const fn from_array(a: [f32; 4]) -> Self {
94        Self::from_xyzw(a[0], a[1], a[2], a[3])
95    }
96
97    /// Creates a new rotation quaternion from a 4D vector.
98    ///
99    /// # Preconditions
100    ///
101    /// This function does not check if the input is normalized, it is up to the user to
102    /// provide normalized input or to normalized the resulting quaternion.
103    #[inline]
104    #[must_use]
105    pub const fn from_vec4(v: Vec4) -> Self {
106        Self(v.0)
107    }
108
109    /// Creates a rotation quaternion from a slice.
110    ///
111    /// # Preconditions
112    ///
113    /// This function does not check if the input is normalized, it is up to the user to
114    /// provide normalized input or to normalized the resulting quaternion.
115    ///
116    /// # Panics
117    ///
118    /// Panics if `slice` length is less than 4.
119    #[inline]
120    #[must_use]
121    pub fn from_slice(slice: &[f32]) -> Self {
122        assert!(slice.len() >= 4);
123        Self(unsafe { _mm_loadu_ps(slice.as_ptr()) })
124    }
125
126    /// Writes the quaternion to an unaligned slice.
127    ///
128    /// # Panics
129    ///
130    /// Panics if `slice` length is less than 4.
131    #[inline]
132    pub fn write_to_slice(self, slice: &mut [f32]) {
133        assert!(slice.len() >= 4);
134        unsafe { _mm_storeu_ps(slice.as_mut_ptr(), self.0) }
135    }
136
137    /// Create a quaternion for a normalized rotation `axis` and `angle` (in radians).
138    ///
139    /// The axis must be a unit vector.
140    ///
141    /// # Panics
142    ///
143    /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
144    #[inline]
145    #[must_use]
146    pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
147        glam_assert!(axis.is_normalized());
148        let (s, c) = math::sin_cos(angle * 0.5);
149        let v = axis * s;
150        Self::from_xyzw(v.x, v.y, v.z, c)
151    }
152
153    /// Create a quaternion that rotates `v.length()` radians around `v.normalize()`.
154    ///
155    /// `from_scaled_axis(Vec3::ZERO)` results in the identity quaternion.
156    #[inline]
157    #[must_use]
158    pub fn from_scaled_axis(v: Vec3) -> Self {
159        let length = v.length();
160        if length == 0.0 {
161            Self::IDENTITY
162        } else {
163            Self::from_axis_angle(v / length, length)
164        }
165    }
166
167    /// Creates a quaternion from the `angle` (in radians) around the x axis.
168    #[inline]
169    #[must_use]
170    pub fn from_rotation_x(angle: f32) -> Self {
171        let (s, c) = math::sin_cos(angle * 0.5);
172        Self::from_xyzw(s, 0.0, 0.0, c)
173    }
174
175    /// Creates a quaternion from the `angle` (in radians) around the y axis.
176    #[inline]
177    #[must_use]
178    pub fn from_rotation_y(angle: f32) -> Self {
179        let (s, c) = math::sin_cos(angle * 0.5);
180        Self::from_xyzw(0.0, s, 0.0, c)
181    }
182
183    /// Creates a quaternion from the `angle` (in radians) around the z axis.
184    #[inline]
185    #[must_use]
186    pub fn from_rotation_z(angle: f32) -> Self {
187        let (s, c) = math::sin_cos(angle * 0.5);
188        Self::from_xyzw(0.0, 0.0, s, c)
189    }
190
191    /// Creates a quaternion from the given Euler rotation sequence and the angles (in radians).
192    #[inline]
193    #[must_use]
194    pub fn from_euler(euler: EulerRot, a: f32, b: f32, c: f32) -> Self {
195        Self::from_euler_angles(euler, a, b, c)
196    }
197
198    /// From the columns of a 3x3 rotation matrix.
199    ///
200    /// Note if the input axes contain scales, shears, or other non-rotation transformations then
201    /// the output of this function is ill-defined.
202    ///
203    /// # Panics
204    ///
205    /// Will panic if any axis is not normalized when `glam_assert` is enabled.
206    #[inline]
207    #[must_use]
208    pub fn from_rotation_axes(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
209        glam_assert!(x_axis.is_normalized() && y_axis.is_normalized() && z_axis.is_normalized());
210        // Based on https://github.com/microsoft/DirectXMath `XMQuaternionRotationMatrix`
211        let (m00, m01, m02) = x_axis.into();
212        let (m10, m11, m12) = y_axis.into();
213        let (m20, m21, m22) = z_axis.into();
214        if m22 <= 0.0 {
215            // x^2 + y^2 >= z^2 + w^2
216            let dif10 = m11 - m00;
217            let omm22 = 1.0 - m22;
218            if dif10 <= 0.0 {
219                // x^2 >= y^2
220                let four_xsq = omm22 - dif10;
221                let inv4x = 0.5 / math::sqrt(four_xsq);
222                Self::from_xyzw(
223                    four_xsq * inv4x,
224                    (m01 + m10) * inv4x,
225                    (m02 + m20) * inv4x,
226                    (m12 - m21) * inv4x,
227                )
228            } else {
229                // y^2 >= x^2
230                let four_ysq = omm22 + dif10;
231                let inv4y = 0.5 / math::sqrt(four_ysq);
232                Self::from_xyzw(
233                    (m01 + m10) * inv4y,
234                    four_ysq * inv4y,
235                    (m12 + m21) * inv4y,
236                    (m20 - m02) * inv4y,
237                )
238            }
239        } else {
240            // z^2 + w^2 >= x^2 + y^2
241            let sum10 = m11 + m00;
242            let opm22 = 1.0 + m22;
243            if sum10 <= 0.0 {
244                // z^2 >= w^2
245                let four_zsq = opm22 - sum10;
246                let inv4z = 0.5 / math::sqrt(four_zsq);
247                Self::from_xyzw(
248                    (m02 + m20) * inv4z,
249                    (m12 + m21) * inv4z,
250                    four_zsq * inv4z,
251                    (m01 - m10) * inv4z,
252                )
253            } else {
254                // w^2 >= z^2
255                let four_wsq = opm22 + sum10;
256                let inv4w = 0.5 / math::sqrt(four_wsq);
257                Self::from_xyzw(
258                    (m12 - m21) * inv4w,
259                    (m20 - m02) * inv4w,
260                    (m01 - m10) * inv4w,
261                    four_wsq * inv4w,
262                )
263            }
264        }
265    }
266
267    /// Creates a quaternion from a 3x3 rotation matrix.
268    ///
269    /// Note if the input matrix contain scales, shears, or other non-rotation transformations then
270    /// the resulting quaternion will be ill-defined.
271    ///
272    /// # Panics
273    ///
274    /// Will panic if any input matrix column is not normalized when `glam_assert` is enabled.
275    #[inline]
276    #[must_use]
277    pub fn from_mat3(mat: &Mat3) -> Self {
278        Self::from_rotation_axes(mat.x_axis, mat.y_axis, mat.z_axis)
279    }
280
281    /// Creates a quaternion from a 3x3 SIMD aligned rotation matrix.
282    ///
283    /// Note if the input matrix contain scales, shears, or other non-rotation transformations then
284    /// the resulting quaternion will be ill-defined.
285    ///
286    /// # Panics
287    ///
288    /// Will panic if any input matrix column is not normalized when `glam_assert` is enabled.
289    #[inline]
290    #[must_use]
291    pub fn from_mat3a(mat: &Mat3A) -> Self {
292        Self::from_rotation_axes(mat.x_axis.into(), mat.y_axis.into(), mat.z_axis.into())
293    }
294
295    /// Creates a quaternion from the upper 3x3 rotation matrix inside a homogeneous 4x4 matrix.
296    ///
297    /// Note if the upper 3x3 matrix contain scales, shears, or other non-rotation transformations
298    /// then the resulting quaternion will be ill-defined.
299    ///
300    /// # Panics
301    ///
302    /// Will panic if any column of the upper 3x3 rotation matrix is not normalized when
303    /// `glam_assert` is enabled.
304    #[inline]
305    #[must_use]
306    pub fn from_mat4(mat: &Mat4) -> Self {
307        Self::from_rotation_axes(
308            mat.x_axis.truncate(),
309            mat.y_axis.truncate(),
310            mat.z_axis.truncate(),
311        )
312    }
313
314    /// Gets the minimal rotation for transforming `from` to `to`.  The rotation is in the
315    /// plane spanned by the two vectors.  Will rotate at most 180 degrees.
316    ///
317    /// The inputs must be unit vectors.
318    ///
319    /// `from_rotation_arc(from, to) * from ≈ to`.
320    ///
321    /// For near-singular cases (from≈to and from≈-to) the current implementation
322    /// is only accurate to about 0.001 (for `f32`).
323    ///
324    /// # Panics
325    ///
326    /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
327    #[must_use]
328    pub fn from_rotation_arc(from: Vec3, to: Vec3) -> Self {
329        glam_assert!(from.is_normalized());
330        glam_assert!(to.is_normalized());
331
332        const ONE_MINUS_EPS: f32 = 1.0 - 2.0 * f32::EPSILON;
333        let dot = from.dot(to);
334        if dot > ONE_MINUS_EPS {
335            // 0° singularity: from ≈ to
336            Self::IDENTITY
337        } else if dot < -ONE_MINUS_EPS {
338            // 180° singularity: from ≈ -to
339            use core::f32::consts::PI; // half a turn = 𝛕/2 = 180°
340            Self::from_axis_angle(from.any_orthonormal_vector(), PI)
341        } else {
342            let c = from.cross(to);
343            Self::from_xyzw(c.x, c.y, c.z, 1.0 + dot).normalize()
344        }
345    }
346
347    /// Gets the minimal rotation for transforming `from` to either `to` or `-to`.  This means
348    /// that the resulting quaternion will rotate `from` so that it is colinear with `to`.
349    ///
350    /// The rotation is in the plane spanned by the two vectors.  Will rotate at most 90
351    /// degrees.
352    ///
353    /// The inputs must be unit vectors.
354    ///
355    /// `to.dot(from_rotation_arc_colinear(from, to) * from).abs() ≈ 1`.
356    ///
357    /// # Panics
358    ///
359    /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
360    #[inline]
361    #[must_use]
362    pub fn from_rotation_arc_colinear(from: Vec3, to: Vec3) -> Self {
363        if from.dot(to) < 0.0 {
364            Self::from_rotation_arc(from, -to)
365        } else {
366            Self::from_rotation_arc(from, to)
367        }
368    }
369
370    /// Gets the minimal rotation for transforming `from` to `to`.  The resulting rotation is
371    /// around the z axis. Will rotate at most 180 degrees.
372    ///
373    /// The inputs must be unit vectors.
374    ///
375    /// `from_rotation_arc_2d(from, to) * from ≈ to`.
376    ///
377    /// For near-singular cases (from≈to and from≈-to) the current implementation
378    /// is only accurate to about 0.001 (for `f32`).
379    ///
380    /// # Panics
381    ///
382    /// Will panic if `from` or `to` are not normalized when `glam_assert` is enabled.
383    #[must_use]
384    pub fn from_rotation_arc_2d(from: Vec2, to: Vec2) -> Self {
385        glam_assert!(from.is_normalized());
386        glam_assert!(to.is_normalized());
387
388        const ONE_MINUS_EPSILON: f32 = 1.0 - 2.0 * f32::EPSILON;
389        let dot = from.dot(to);
390        if dot > ONE_MINUS_EPSILON {
391            // 0° singularity: from ≈ to
392            Self::IDENTITY
393        } else if dot < -ONE_MINUS_EPSILON {
394            // 180° singularity: from ≈ -to
395            const COS_FRAC_PI_2: f32 = 0.0;
396            const SIN_FRAC_PI_2: f32 = 1.0;
397            // rotation around z by PI radians
398            Self::from_xyzw(0.0, 0.0, SIN_FRAC_PI_2, COS_FRAC_PI_2)
399        } else {
400            // vector3 cross where z=0
401            let z = from.x * to.y - to.x * from.y;
402            let w = 1.0 + dot;
403            // calculate length with x=0 and y=0 to normalize
404            let len_rcp = 1.0 / math::sqrt(z * z + w * w);
405            Self::from_xyzw(0.0, 0.0, z * len_rcp, w * len_rcp)
406        }
407    }
408
409    /// Creates a quaterion rotation from a facing direction and an up direction.
410    ///
411    /// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
412    ///
413    /// # Panics
414    ///
415    /// Will panic if `up` is not normalized when `glam_assert` is enabled.
416    #[inline]
417    #[must_use]
418    pub fn look_to_lh(dir: Vec3, up: Vec3) -> Self {
419        Self::look_to_rh(-dir, up)
420    }
421
422    /// Creates a quaterion rotation from facing direction and an up direction.
423    ///
424    /// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
425    ///
426    /// # Panics
427    ///
428    /// Will panic if `dir` and `up` are not normalized when `glam_assert` is enabled.
429    #[inline]
430    #[must_use]
431    pub fn look_to_rh(dir: Vec3, up: Vec3) -> Self {
432        glam_assert!(dir.is_normalized());
433        glam_assert!(up.is_normalized());
434        let f = dir;
435        let s = f.cross(up).normalize();
436        let u = s.cross(f);
437
438        Self::from_rotation_axes(
439            Vec3::new(s.x, u.x, -f.x),
440            Vec3::new(s.y, u.y, -f.y),
441            Vec3::new(s.z, u.z, -f.z),
442        )
443    }
444
445    /// Creates a left-handed view matrix using a camera position, a focal point, and an up
446    /// direction.
447    ///
448    /// For a left-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=forward`.
449    ///
450    /// # Panics
451    ///
452    /// Will panic if `up` is not normalized when `glam_assert` is enabled.
453    #[inline]
454    #[must_use]
455    pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
456        Self::look_to_lh(center.sub(eye).normalize(), up)
457    }
458
459    /// Creates a right-handed view matrix using a camera position, an up direction, and a focal
460    /// point.
461    ///
462    /// For a right-handed view coordinate system with `+X=right`, `+Y=up` and `+Z=back`.
463    ///
464    /// # Panics
465    ///
466    /// Will panic if `up` is not normalized when `glam_assert` is enabled.
467    #[inline]
468    #[must_use]
469    pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
470        Self::look_to_rh(center.sub(eye).normalize(), up)
471    }
472
473    /// Returns the rotation axis (normalized) and angle (in radians) of `self`.
474    #[inline]
475    #[must_use]
476    pub fn to_axis_angle(self) -> (Vec3, f32) {
477        const EPSILON: f32 = 1.0e-8;
478        let v = Vec3::new(self.x, self.y, self.z);
479        let length = v.length();
480        if length >= EPSILON {
481            let angle = 2.0 * math::atan2(length, self.w);
482            let axis = v / length;
483            (axis, angle)
484        } else {
485            (Vec3::X, 0.0)
486        }
487    }
488
489    /// Returns the rotation axis scaled by the rotation in radians.
490    #[inline]
491    #[must_use]
492    pub fn to_scaled_axis(self) -> Vec3 {
493        let (axis, angle) = self.to_axis_angle();
494        axis * angle
495    }
496
497    /// Returns the rotation angles for the given euler rotation sequence.
498    #[inline]
499    #[must_use]
500    pub fn to_euler(self, order: EulerRot) -> (f32, f32, f32) {
501        self.to_euler_angles(order)
502    }
503
504    /// `[x, y, z, w]`
505    #[inline]
506    #[must_use]
507    pub fn to_array(self) -> [f32; 4] {
508        [self.x, self.y, self.z, self.w]
509    }
510
511    /// Returns the vector part of the quaternion.
512    #[inline]
513    #[must_use]
514    pub fn xyz(self) -> Vec3 {
515        Vec3::new(self.x, self.y, self.z)
516    }
517
518    /// Returns the quaternion conjugate of `self`. For a unit quaternion the
519    /// conjugate is also the inverse.
520    #[inline]
521    #[must_use]
522    pub fn conjugate(self) -> Self {
523        const SIGN: __m128 = m128_from_f32x4([-0.0, -0.0, -0.0, 0.0]);
524        Self(unsafe { _mm_xor_ps(self.0, SIGN) })
525    }
526
527    /// Returns the inverse of a normalized quaternion.
528    ///
529    /// Typically quaternion inverse returns the conjugate of a normalized quaternion.
530    /// Because `self` is assumed to already be unit length this method *does not* normalize
531    /// before returning the conjugate.
532    ///
533    /// # Panics
534    ///
535    /// Will panic if `self` is not normalized when `glam_assert` is enabled.
536    #[inline]
537    #[must_use]
538    pub fn inverse(self) -> Self {
539        glam_assert!(self.is_normalized());
540        self.conjugate()
541    }
542
543    /// Computes the dot product of `self` and `rhs`. The dot product is
544    /// equal to the cosine of the angle between two quaternion rotations.
545    #[inline]
546    #[must_use]
547    pub fn dot(self, rhs: Self) -> f32 {
548        Vec4::from(self).dot(Vec4::from(rhs))
549    }
550
551    /// Computes the length of `self`.
552    #[doc(alias = "magnitude")]
553    #[inline]
554    #[must_use]
555    pub fn length(self) -> f32 {
556        Vec4::from(self).length()
557    }
558
559    /// Computes the squared length of `self`.
560    ///
561    /// This is generally faster than `length()` as it avoids a square
562    /// root operation.
563    #[doc(alias = "magnitude2")]
564    #[inline]
565    #[must_use]
566    pub fn length_squared(self) -> f32 {
567        Vec4::from(self).length_squared()
568    }
569
570    /// Computes `1.0 / length()`.
571    ///
572    /// For valid results, `self` must _not_ be of length zero.
573    #[inline]
574    #[must_use]
575    pub fn length_recip(self) -> f32 {
576        Vec4::from(self).length_recip()
577    }
578
579    /// Returns `self` normalized to length 1.0.
580    ///
581    /// For valid results, `self` must _not_ be of length zero.
582    ///
583    /// Panics
584    ///
585    /// Will panic if `self` is zero length when `glam_assert` is enabled.
586    #[inline]
587    #[must_use]
588    pub fn normalize(self) -> Self {
589        Self::from_vec4(Vec4::from(self).normalize())
590    }
591
592    /// Returns `true` if, and only if, all elements are finite.
593    /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
594    #[inline]
595    #[must_use]
596    pub fn is_finite(self) -> bool {
597        Vec4::from(self).is_finite()
598    }
599
600    /// Returns `true` if any elements are `NAN`.
601    #[inline]
602    #[must_use]
603    pub fn is_nan(self) -> bool {
604        Vec4::from(self).is_nan()
605    }
606
607    /// Returns whether `self` of length `1.0` or not.
608    ///
609    /// Uses a precision threshold of `1e-6`.
610    #[inline]
611    #[must_use]
612    pub fn is_normalized(self) -> bool {
613        Vec4::from(self).is_normalized()
614    }
615
616    #[inline]
617    #[must_use]
618    pub fn is_near_identity(self) -> bool {
619        // Based on https://github.com/nfrechette/rtm `rtm::quat_near_identity`
620        // Because of floating point precision, we cannot represent very small rotations.
621        // The closest f32 to 1.0 that is not 1.0 itself yields:
622        // 0.99999994.acos() * 2.0  = 0.000690533954 rad
623        //
624        // An error threshold of 1.e-6 is used by default.
625        // (1.0 - 1.e-6).acos() * 2.0 = 0.00284714461 rad
626        // (1.0 - 1.e-7).acos() * 2.0 = 0.00097656250 rad
627        //
628        // We don't really care about the angle value itself, only if it's close to 0.
629        // This will happen whenever quat.w is close to 1.0.
630        // If the quat.w is close to -1.0, the angle will be near 2*PI which is close to
631        // a negative 0 rotation. By forcing quat.w to be positive, we'll end up with
632        // the shortest path.
633        //
634        // For f64 we're using a threshhold of
635        // (1.0 - 1e-14).acos() * 2.0
636        const THRESHOLD_ANGLE: f32 = 0.002_847_144_6;
637        let positive_w_angle = math::acos_approx(math::abs(self.w)) * 2.0;
638        positive_w_angle < THRESHOLD_ANGLE
639    }
640
641    /// Returns the angle (in radians) for the minimal rotation
642    /// for transforming this quaternion into another.
643    ///
644    /// Both quaternions must be normalized.
645    ///
646    /// # Panics
647    ///
648    /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
649    #[inline]
650    #[must_use]
651    pub fn angle_between(self, rhs: Self) -> f32 {
652        glam_assert!(self.is_normalized() && rhs.is_normalized());
653        math::acos_approx(math::abs(self.dot(rhs))) * 2.0
654    }
655
656    /// Rotates towards `rhs` up to `max_angle` (in radians).
657    ///
658    /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
659    /// `self.angle_between(rhs)`, the result will be equal to `rhs`. If `max_angle` is negative,
660    /// rotates towards the exact opposite of `rhs`. Will not go past the target.
661    ///
662    /// Both quaternions must be normalized.
663    ///
664    /// # Panics
665    ///
666    /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
667    #[inline]
668    #[must_use]
669    pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
670        glam_assert!(self.is_normalized() && rhs.is_normalized());
671        let angle = self.angle_between(rhs);
672        if angle <= 1e-4 {
673            return rhs;
674        }
675        let s = (max_angle / angle).clamp(-1.0, 1.0);
676        self.slerp(rhs, s)
677    }
678
679    /// Returns true if the absolute difference of all elements between `self` and `rhs`
680    /// is less than or equal to `max_abs_diff`.
681    ///
682    /// This can be used to compare if two quaternions contain similar elements. It works
683    /// best when comparing with a known value. The `max_abs_diff` that should be used used
684    /// depends on the values being compared against.
685    ///
686    /// For more see
687    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
688    #[inline]
689    #[must_use]
690    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
691        Vec4::from(self).abs_diff_eq(Vec4::from(rhs), max_abs_diff)
692    }
693
694    #[inline(always)]
695    #[must_use]
696    fn lerp_impl(self, end: Self, s: f32) -> Self {
697        (self * (1.0 - s) + end * s).normalize()
698    }
699
700    /// Performs a linear interpolation between `self` and `rhs` based on
701    /// the value `s`.
702    ///
703    /// When `s` is `0.0`, the result will be equal to `self`.  When `s`
704    /// is `1.0`, the result will be equal to `rhs`.
705    ///
706    /// # Panics
707    ///
708    /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
709    #[doc(alias = "mix")]
710    #[inline]
711    #[must_use]
712    pub fn lerp(self, end: Self, s: f32) -> Self {
713        glam_assert!(self.is_normalized());
714        glam_assert!(end.is_normalized());
715
716        const NEG_ZERO: __m128 = m128_from_f32x4([-0.0; 4]);
717        unsafe {
718            let dot = dot4_into_m128(self.0, end.0);
719            // Calculate the bias, if the dot product is positive or zero, there is no bias
720            // but if it is negative, we want to flip the 'end' rotation XYZW components
721            let bias = _mm_and_ps(dot, NEG_ZERO);
722            self.lerp_impl(Self(_mm_xor_ps(end.0, bias)), s)
723        }
724    }
725
726    /// Performs a spherical linear interpolation between `self` and `end`
727    /// based on the value `s`.
728    ///
729    /// When `s` is `0.0`, the result will be equal to `self`.  When `s`
730    /// is `1.0`, the result will be equal to `end`.
731    ///
732    /// # Panics
733    ///
734    /// Will panic if `self` or `end` are not normalized when `glam_assert` is enabled.
735    #[inline]
736    #[must_use]
737    pub fn slerp(self, mut end: Self, s: f32) -> Self {
738        // http://number-none.com/product/Understanding%20Slerp,%20Then%20Not%20Using%20It/
739        glam_assert!(self.is_normalized());
740        glam_assert!(end.is_normalized());
741
742        // Note that a rotation can be represented by two quaternions: `q` and
743        // `-q`. The slerp path between `q` and `end` will be different from the
744        // path between `-q` and `end`. One path will take the long way around and
745        // one will take the short way. In order to correct for this, the `dot`
746        // product between `self` and `end` should be positive. If the `dot`
747        // product is negative, slerp between `self` and `-end`.
748        let mut dot = self.dot(end);
749        if dot < 0.0 {
750            end = -end;
751            dot = -dot;
752        }
753
754        const DOT_THRESHOLD: f32 = 1.0 - f32::EPSILON;
755        if dot > DOT_THRESHOLD {
756            // if above threshold perform linear interpolation to avoid divide by zero
757            self.lerp_impl(end, s)
758        } else {
759            let theta = math::acos_approx(dot);
760
761            let x = 1.0 - s;
762            let y = s;
763            let z = 1.0;
764
765            unsafe {
766                let tmp = _mm_mul_ps(_mm_set_ps1(theta), _mm_set_ps(0.0, z, y, x));
767                let tmp = m128_sin(tmp);
768
769                let scale1 = _mm_shuffle_ps(tmp, tmp, 0b00_00_00_00);
770                let scale2 = _mm_shuffle_ps(tmp, tmp, 0b01_01_01_01);
771                let theta_sin = _mm_shuffle_ps(tmp, tmp, 0b10_10_10_10);
772
773                Self(_mm_div_ps(
774                    _mm_add_ps(_mm_mul_ps(self.0, scale1), _mm_mul_ps(end.0, scale2)),
775                    theta_sin,
776                ))
777            }
778        }
779    }
780
781    /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
782    ///
783    /// # Panics
784    ///
785    /// Will panic if `self` is not normalized when `glam_assert` is enabled.
786    #[inline]
787    #[must_use]
788    pub fn mul_vec3(self, rhs: Vec3) -> Vec3 {
789        glam_assert!(self.is_normalized());
790
791        self.mul_vec3a(rhs.into()).into()
792    }
793
794    /// Multiplies two quaternions. If they each represent a rotation, the result will
795    /// represent the combined rotation.
796    ///
797    /// Note that due to floating point rounding the result may not be perfectly normalized.
798    ///
799    /// # Panics
800    ///
801    /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
802    #[inline]
803    #[must_use]
804    pub fn mul_quat(self, rhs: Self) -> Self {
805        // Based on https://github.com/nfrechette/rtm `rtm::quat_mul`
806        const CONTROL_WZYX: __m128 = m128_from_f32x4([1.0, -1.0, 1.0, -1.0]);
807        const CONTROL_ZWXY: __m128 = m128_from_f32x4([1.0, 1.0, -1.0, -1.0]);
808        const CONTROL_YXWZ: __m128 = m128_from_f32x4([-1.0, 1.0, 1.0, -1.0]);
809
810        let lhs = self.0;
811        let rhs = rhs.0;
812
813        unsafe {
814            let r_xxxx = _mm_shuffle_ps(lhs, lhs, 0b00_00_00_00);
815            let r_yyyy = _mm_shuffle_ps(lhs, lhs, 0b01_01_01_01);
816            let r_zzzz = _mm_shuffle_ps(lhs, lhs, 0b10_10_10_10);
817            let r_wwww = _mm_shuffle_ps(lhs, lhs, 0b11_11_11_11);
818
819            let lxrw_lyrw_lzrw_lwrw = _mm_mul_ps(r_wwww, rhs);
820            let l_wzyx = _mm_shuffle_ps(rhs, rhs, 0b00_01_10_11);
821
822            let lwrx_lzrx_lyrx_lxrx = _mm_mul_ps(r_xxxx, l_wzyx);
823            let l_zwxy = _mm_shuffle_ps(l_wzyx, l_wzyx, 0b10_11_00_01);
824
825            let lwrx_nlzrx_lyrx_nlxrx = _mm_mul_ps(lwrx_lzrx_lyrx_lxrx, CONTROL_WZYX);
826
827            let lzry_lwry_lxry_lyry = _mm_mul_ps(r_yyyy, l_zwxy);
828            let l_yxwz = _mm_shuffle_ps(l_zwxy, l_zwxy, 0b00_01_10_11);
829
830            let lzry_lwry_nlxry_nlyry = _mm_mul_ps(lzry_lwry_lxry_lyry, CONTROL_ZWXY);
831
832            let lyrz_lxrz_lwrz_lzrz = _mm_mul_ps(r_zzzz, l_yxwz);
833            let result0 = _mm_add_ps(lxrw_lyrw_lzrw_lwrw, lwrx_nlzrx_lyrx_nlxrx);
834
835            let nlyrz_lxrz_lwrz_wlzrz = _mm_mul_ps(lyrz_lxrz_lwrz_lzrz, CONTROL_YXWZ);
836            let result1 = _mm_add_ps(lzry_lwry_nlxry_nlyry, nlyrz_lxrz_lwrz_wlzrz);
837
838            Self(_mm_add_ps(result0, result1))
839        }
840    }
841
842    /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
843    ///
844    /// Note if the input affine matrix contain scales, shears, or other non-rotation
845    /// transformations then the resulting quaternion will be ill-defined.
846    ///
847    /// # Panics
848    ///
849    /// Will panic if any input affine matrix column is not normalized when `glam_assert` is
850    /// enabled.
851    #[inline]
852    #[must_use]
853    pub fn from_affine3(a: &crate::Affine3) -> Self {
854        Self::from_rotation_axes(a.matrix3.x_axis, a.matrix3.y_axis, a.matrix3.z_axis)
855    }
856
857    /// Creates a quaternion from a 3x3 rotation matrix inside a 3D affine transform.
858    ///
859    /// Note if the input affine matrix contain scales, shears, or other non-rotation
860    /// transformations then the resulting quaternion will be ill-defined.
861    ///
862    /// # Panics
863    ///
864    /// Will panic if any input affine matrix column is not normalized when `glam_assert` is
865    /// enabled.
866    #[inline]
867    #[must_use]
868    pub fn from_affine3a(a: &crate::Affine3A) -> Self {
869        Self::from_rotation_axes(
870            a.matrix3.x_axis.into(),
871            a.matrix3.y_axis.into(),
872            a.matrix3.z_axis.into(),
873        )
874    }
875
876    /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
877    #[inline]
878    #[must_use]
879    pub fn mul_vec3a(self, rhs: Vec3A) -> Vec3A {
880        unsafe {
881            const TWO: __m128 = m128_from_f32x4([2.0; 4]);
882            let w = _mm_shuffle_ps(self.0, self.0, 0b11_11_11_11);
883            let b = self.0;
884            let b2 = dot3_into_m128(b, b);
885            Vec3A(_mm_add_ps(
886                _mm_add_ps(
887                    _mm_mul_ps(rhs.0, _mm_sub_ps(_mm_mul_ps(w, w), b2)),
888                    _mm_mul_ps(b, _mm_mul_ps(dot3_into_m128(rhs.0, b), TWO)),
889                ),
890                _mm_mul_ps(Vec3A(b).cross(rhs).into(), _mm_mul_ps(w, TWO)),
891            ))
892        }
893    }
894
895    #[inline]
896    #[must_use]
897    pub fn as_dquat(self) -> DQuat {
898        DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
899    }
900}
901
902impl fmt::Debug for Quat {
903    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
904        fmt.debug_tuple(stringify!(Quat))
905            .field(&self.x)
906            .field(&self.y)
907            .field(&self.z)
908            .field(&self.w)
909            .finish()
910    }
911}
912
913impl fmt::Display for Quat {
914    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
915        if let Some(p) = f.precision() {
916            write!(
917                f,
918                "[{:.*}, {:.*}, {:.*}, {:.*}]",
919                p, self.x, p, self.y, p, self.z, p, self.w
920            )
921        } else {
922            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
923        }
924    }
925}
926
927impl Add for Quat {
928    type Output = Self;
929    /// Adds two quaternions.
930    ///
931    /// The sum is not guaranteed to be normalized.
932    ///
933    /// Note that addition is not the same as combining the rotations represented by the
934    /// two quaternions! That corresponds to multiplication.
935    #[inline]
936    fn add(self, rhs: Self) -> Self {
937        Self::from_vec4(Vec4::from(self) + Vec4::from(rhs))
938    }
939}
940
941impl Add<&Self> for Quat {
942    type Output = Self;
943    #[inline]
944    fn add(self, rhs: &Self) -> Self {
945        self.add(*rhs)
946    }
947}
948
949impl Add<&Quat> for &Quat {
950    type Output = Quat;
951    #[inline]
952    fn add(self, rhs: &Quat) -> Quat {
953        (*self).add(*rhs)
954    }
955}
956
957impl Add<Quat> for &Quat {
958    type Output = Quat;
959    #[inline]
960    fn add(self, rhs: Quat) -> Quat {
961        (*self).add(rhs)
962    }
963}
964
965impl AddAssign for Quat {
966    #[inline]
967    fn add_assign(&mut self, rhs: Self) {
968        *self = self.add(rhs);
969    }
970}
971
972impl AddAssign<&Self> for Quat {
973    #[inline]
974    fn add_assign(&mut self, rhs: &Self) {
975        self.add_assign(*rhs);
976    }
977}
978
979impl Sub for Quat {
980    type Output = Self;
981    /// Subtracts the `rhs` quaternion from `self`.
982    ///
983    /// The difference is not guaranteed to be normalized.
984    #[inline]
985    fn sub(self, rhs: Self) -> Self {
986        Self::from_vec4(Vec4::from(self) - Vec4::from(rhs))
987    }
988}
989
990impl Sub<&Self> for Quat {
991    type Output = Self;
992    #[inline]
993    fn sub(self, rhs: &Self) -> Self {
994        self.sub(*rhs)
995    }
996}
997
998impl Sub<&Quat> for &Quat {
999    type Output = Quat;
1000    #[inline]
1001    fn sub(self, rhs: &Quat) -> Quat {
1002        (*self).sub(*rhs)
1003    }
1004}
1005
1006impl Sub<Quat> for &Quat {
1007    type Output = Quat;
1008    #[inline]
1009    fn sub(self, rhs: Quat) -> Quat {
1010        (*self).sub(rhs)
1011    }
1012}
1013
1014impl SubAssign for Quat {
1015    #[inline]
1016    fn sub_assign(&mut self, rhs: Self) {
1017        *self = self.sub(rhs);
1018    }
1019}
1020
1021impl SubAssign<&Self> for Quat {
1022    #[inline]
1023    fn sub_assign(&mut self, rhs: &Self) {
1024        self.sub_assign(*rhs);
1025    }
1026}
1027
1028impl Mul<f32> for Quat {
1029    type Output = Self;
1030    /// Multiplies a quaternion by a scalar value.
1031    ///
1032    /// The product is not guaranteed to be normalized.
1033    #[inline]
1034    fn mul(self, rhs: f32) -> Self {
1035        Self::from_vec4(Vec4::from(self) * rhs)
1036    }
1037}
1038
1039impl Mul<&f32> for Quat {
1040    type Output = Self;
1041    #[inline]
1042    fn mul(self, rhs: &f32) -> Self {
1043        self.mul(*rhs)
1044    }
1045}
1046
1047impl Mul<&f32> for &Quat {
1048    type Output = Quat;
1049    #[inline]
1050    fn mul(self, rhs: &f32) -> Quat {
1051        (*self).mul(*rhs)
1052    }
1053}
1054
1055impl Mul<f32> for &Quat {
1056    type Output = Quat;
1057    #[inline]
1058    fn mul(self, rhs: f32) -> Quat {
1059        (*self).mul(rhs)
1060    }
1061}
1062
1063impl MulAssign<f32> for Quat {
1064    #[inline]
1065    fn mul_assign(&mut self, rhs: f32) {
1066        *self = self.mul(rhs);
1067    }
1068}
1069
1070impl MulAssign<&f32> for Quat {
1071    #[inline]
1072    fn mul_assign(&mut self, rhs: &f32) {
1073        self.mul_assign(*rhs);
1074    }
1075}
1076
1077impl Div<f32> for Quat {
1078    type Output = Self;
1079    /// Divides a quaternion by a scalar value.
1080    /// The quotient is not guaranteed to be normalized.
1081    #[inline]
1082    fn div(self, rhs: f32) -> Self {
1083        Self::from_vec4(Vec4::from(self) / rhs)
1084    }
1085}
1086
1087impl Div<&f32> for Quat {
1088    type Output = Self;
1089    #[inline]
1090    fn div(self, rhs: &f32) -> Self {
1091        self.div(*rhs)
1092    }
1093}
1094
1095impl Div<&f32> for &Quat {
1096    type Output = Quat;
1097    #[inline]
1098    fn div(self, rhs: &f32) -> Quat {
1099        (*self).div(*rhs)
1100    }
1101}
1102
1103impl Div<f32> for &Quat {
1104    type Output = Quat;
1105    #[inline]
1106    fn div(self, rhs: f32) -> Quat {
1107        (*self).div(rhs)
1108    }
1109}
1110
1111impl DivAssign<f32> for Quat {
1112    #[inline]
1113    fn div_assign(&mut self, rhs: f32) {
1114        *self = self.div(rhs);
1115    }
1116}
1117
1118impl DivAssign<&f32> for Quat {
1119    #[inline]
1120    fn div_assign(&mut self, rhs: &f32) {
1121        self.div_assign(*rhs);
1122    }
1123}
1124
1125impl Mul for Quat {
1126    type Output = Self;
1127    /// Multiplies two quaternions. If they each represent a rotation, the result will
1128    /// represent the combined rotation.
1129    ///
1130    /// Note that due to floating point rounding the result may not be perfectly
1131    /// normalized.
1132    ///
1133    /// # Panics
1134    ///
1135    /// Will panic if `self` or `rhs` are not normalized when `glam_assert` is enabled.
1136    #[inline]
1137    fn mul(self, rhs: Self) -> Self {
1138        self.mul_quat(rhs)
1139    }
1140}
1141
1142impl Mul<&Self> for Quat {
1143    type Output = Self;
1144    #[inline]
1145    fn mul(self, rhs: &Self) -> Self {
1146        self.mul(*rhs)
1147    }
1148}
1149
1150impl Mul<&Quat> for &Quat {
1151    type Output = Quat;
1152    #[inline]
1153    fn mul(self, rhs: &Quat) -> Quat {
1154        (*self).mul(*rhs)
1155    }
1156}
1157
1158impl Mul<Quat> for &Quat {
1159    type Output = Quat;
1160    #[inline]
1161    fn mul(self, rhs: Quat) -> Quat {
1162        (*self).mul(rhs)
1163    }
1164}
1165
1166impl MulAssign for Quat {
1167    #[inline]
1168    fn mul_assign(&mut self, rhs: Self) {
1169        *self = self.mul(rhs);
1170    }
1171}
1172
1173impl MulAssign<&Self> for Quat {
1174    #[inline]
1175    fn mul_assign(&mut self, rhs: &Self) {
1176        self.mul_assign(*rhs);
1177    }
1178}
1179
1180impl Mul<Vec3> for Quat {
1181    type Output = Vec3;
1182    /// Multiplies a quaternion and a 3D vector, returning the rotated vector.
1183    ///
1184    /// # Panics
1185    ///
1186    /// Will panic if `self` is not normalized when `glam_assert` is enabled.
1187    #[inline]
1188    fn mul(self, rhs: Vec3) -> Self::Output {
1189        self.mul_vec3(rhs)
1190    }
1191}
1192
1193impl Mul<&Vec3> for Quat {
1194    type Output = Vec3;
1195    #[inline]
1196    fn mul(self, rhs: &Vec3) -> Vec3 {
1197        self.mul(*rhs)
1198    }
1199}
1200
1201impl Mul<&Vec3> for &Quat {
1202    type Output = Vec3;
1203    #[inline]
1204    fn mul(self, rhs: &Vec3) -> Vec3 {
1205        (*self).mul(*rhs)
1206    }
1207}
1208
1209impl Mul<Vec3> for &Quat {
1210    type Output = Vec3;
1211    #[inline]
1212    fn mul(self, rhs: Vec3) -> Vec3 {
1213        (*self).mul(rhs)
1214    }
1215}
1216
1217impl Mul<Vec3A> for Quat {
1218    type Output = Vec3A;
1219    #[inline]
1220    fn mul(self, rhs: Vec3A) -> Self::Output {
1221        self.mul_vec3a(rhs)
1222    }
1223}
1224
1225impl Mul<&Vec3A> for Quat {
1226    type Output = Vec3A;
1227    #[inline]
1228    fn mul(self, rhs: &Vec3A) -> Vec3A {
1229        self.mul(*rhs)
1230    }
1231}
1232
1233impl Mul<&Vec3A> for &Quat {
1234    type Output = Vec3A;
1235    #[inline]
1236    fn mul(self, rhs: &Vec3A) -> Vec3A {
1237        (*self).mul(*rhs)
1238    }
1239}
1240
1241impl Mul<Vec3A> for &Quat {
1242    type Output = Vec3A;
1243    #[inline]
1244    fn mul(self, rhs: Vec3A) -> Vec3A {
1245        (*self).mul(rhs)
1246    }
1247}
1248
1249impl Neg for Quat {
1250    type Output = Self;
1251    #[inline]
1252    fn neg(self) -> Self {
1253        self * -1.0
1254    }
1255}
1256
1257impl Neg for &Quat {
1258    type Output = Quat;
1259    #[inline]
1260    fn neg(self) -> Quat {
1261        (*self).neg()
1262    }
1263}
1264
1265impl Default for Quat {
1266    #[inline]
1267    fn default() -> Self {
1268        Self::IDENTITY
1269    }
1270}
1271
1272impl PartialEq for Quat {
1273    #[inline]
1274    fn eq(&self, rhs: &Self) -> bool {
1275        Vec4::from(*self).eq(&Vec4::from(*rhs))
1276    }
1277}
1278
1279impl AsRef<[f32; 4]> for Quat {
1280    #[inline]
1281    fn as_ref(&self) -> &[f32; 4] {
1282        unsafe { &*(self as *const Self as *const [f32; 4]) }
1283    }
1284}
1285
1286impl Sum<Self> for Quat {
1287    fn sum<I>(iter: I) -> Self
1288    where
1289        I: Iterator<Item = Self>,
1290    {
1291        iter.fold(Self::ZERO, Self::add)
1292    }
1293}
1294
1295impl<'a> Sum<&'a Self> for Quat {
1296    fn sum<I>(iter: I) -> Self
1297    where
1298        I: Iterator<Item = &'a Self>,
1299    {
1300        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1301    }
1302}
1303
1304impl Product for Quat {
1305    fn product<I>(iter: I) -> Self
1306    where
1307        I: Iterator<Item = Self>,
1308    {
1309        iter.fold(Self::IDENTITY, Self::mul)
1310    }
1311}
1312
1313impl<'a> Product<&'a Self> for Quat {
1314    fn product<I>(iter: I) -> Self
1315    where
1316        I: Iterator<Item = &'a Self>,
1317    {
1318        iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1319    }
1320}
1321
1322impl From<Quat> for Vec4 {
1323    #[inline]
1324    fn from(q: Quat) -> Self {
1325        Self(q.0)
1326    }
1327}
1328
1329impl From<Quat> for (f32, f32, f32, f32) {
1330    #[inline]
1331    fn from(q: Quat) -> Self {
1332        Vec4::from(q).into()
1333    }
1334}
1335
1336impl From<Quat> for [f32; 4] {
1337    #[inline]
1338    fn from(q: Quat) -> Self {
1339        Vec4::from(q).into()
1340    }
1341}
1342
1343impl From<Quat> for __m128 {
1344    #[inline]
1345    fn from(q: Quat) -> Self {
1346        q.0
1347    }
1348}
1349
1350impl Deref for Quat {
1351    type Target = crate::deref::Vec4<f32>;
1352    #[inline]
1353    fn deref(&self) -> &Self::Target {
1354        unsafe { &*(self as *const Self).cast() }
1355    }
1356}
1357
1358impl DerefMut for Quat {
1359    #[inline]
1360    fn deref_mut(&mut self) -> &mut Self::Target {
1361        unsafe { &mut *(self as *mut Self).cast() }
1362    }
1363}