1use 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#[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#[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 const ZERO: Self = Self::from_array([0.0; 4]);
61
62 pub const IDENTITY: Self = Self::from_xyzw(0.0, 0.0, 0.0, 1.0);
64
65 pub const NAN: Self = Self::from_array([f32::NAN; 4]);
67
68 #[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 #[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 #[inline]
104 #[must_use]
105 pub const fn from_vec4(v: Vec4) -> Self {
106 Self(v.0)
107 }
108
109 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
207 #[must_use]
208 pub(crate) 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 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 let dif10 = m11 - m00;
217 let omm22 = 1.0 - m22;
218 if dif10 <= 0.0 {
219 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 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 let sum10 = m11 + m00;
242 let opm22 = 1.0 + m22;
243 if sum10 <= 0.0 {
244 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 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 #[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 #[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 #[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 #[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 Self::IDENTITY
337 } else if dot < -ONE_MINUS_EPS {
338 use core::f32::consts::PI; 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 #[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 #[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 Self::IDENTITY
393 } else if dot < -ONE_MINUS_EPSILON {
394 const COS_FRAC_PI_2: f32 = 0.0;
396 const SIN_FRAC_PI_2: f32 = 1.0;
397 Self::from_xyzw(0.0, 0.0, SIN_FRAC_PI_2, COS_FRAC_PI_2)
399 } else {
400 let z = from.x * to.y - to.x * from.y;
402 let w = 1.0 + dot;
403 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 #[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 #[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 #[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 #[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 #[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 #[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 #[inline]
499 #[must_use]
500 pub fn to_euler(self, order: EulerRot) -> (f32, f32, f32) {
501 self.to_euler_angles(order)
502 }
503
504 #[inline]
506 #[must_use]
507 pub fn to_array(&self) -> [f32; 4] {
508 [self.x, self.y, self.z, self.w]
509 }
510
511 #[inline]
513 #[must_use]
514 pub fn xyz(self) -> Vec3 {
515 Vec3::new(self.x, self.y, self.z)
516 }
517
518 #[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 #[inline]
537 #[must_use]
538 pub fn inverse(self) -> Self {
539 glam_assert!(self.is_normalized());
540 self.conjugate()
541 }
542
543 #[inline]
546 #[must_use]
547 pub fn dot(self, rhs: Self) -> f32 {
548 Vec4::from(self).dot(Vec4::from(rhs))
549 }
550
551 #[doc(alias = "magnitude")]
553 #[inline]
554 #[must_use]
555 pub fn length(self) -> f32 {
556 Vec4::from(self).length()
557 }
558
559 #[doc(alias = "magnitude2")]
564 #[inline]
565 #[must_use]
566 pub fn length_squared(self) -> f32 {
567 Vec4::from(self).length_squared()
568 }
569
570 #[inline]
574 #[must_use]
575 pub fn length_recip(self) -> f32 {
576 Vec4::from(self).length_recip()
577 }
578
579 #[inline]
587 #[must_use]
588 pub fn normalize(self) -> Self {
589 Self::from_vec4(Vec4::from(self).normalize())
590 }
591
592 #[inline]
595 #[must_use]
596 pub fn is_finite(self) -> bool {
597 Vec4::from(self).is_finite()
598 }
599
600 #[inline]
602 #[must_use]
603 pub fn is_nan(self) -> bool {
604 Vec4::from(self).is_nan()
605 }
606
607 #[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 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 #[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 #[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 #[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 #[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 let bias = _mm_and_ps(dot, NEG_ZERO);
722 self.lerp_impl(Self(_mm_xor_ps(end.0, bias)), s)
723 }
724 }
725
726 #[inline]
736 #[must_use]
737 pub fn slerp(self, mut end: Self, s: f32) -> Self {
738 glam_assert!(self.is_normalized());
740 glam_assert!(end.is_normalized());
741
742 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 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 #[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 #[inline]
803 #[must_use]
804 pub fn mul_quat(self, rhs: Self) -> Self {
805 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 #[inline]
852 #[must_use]
853 pub fn from_affine3(a: &crate::Affine3A) -> Self {
854 #[allow(clippy::useless_conversion)]
855 Self::from_rotation_axes(
856 a.matrix3.x_axis.into(),
857 a.matrix3.y_axis.into(),
858 a.matrix3.z_axis.into(),
859 )
860 }
861
862 #[inline]
864 #[must_use]
865 pub fn mul_vec3a(self, rhs: Vec3A) -> Vec3A {
866 unsafe {
867 const TWO: __m128 = m128_from_f32x4([2.0; 4]);
868 let w = _mm_shuffle_ps(self.0, self.0, 0b11_11_11_11);
869 let b = self.0;
870 let b2 = dot3_into_m128(b, b);
871 Vec3A(_mm_add_ps(
872 _mm_add_ps(
873 _mm_mul_ps(rhs.0, _mm_sub_ps(_mm_mul_ps(w, w), b2)),
874 _mm_mul_ps(b, _mm_mul_ps(dot3_into_m128(rhs.0, b), TWO)),
875 ),
876 _mm_mul_ps(Vec3A(b).cross(rhs).into(), _mm_mul_ps(w, TWO)),
877 ))
878 }
879 }
880
881 #[inline]
882 #[must_use]
883 pub fn as_dquat(self) -> DQuat {
884 DQuat::from_xyzw(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
885 }
886}
887
888impl fmt::Debug for Quat {
889 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
890 fmt.debug_tuple(stringify!(Quat))
891 .field(&self.x)
892 .field(&self.y)
893 .field(&self.z)
894 .field(&self.w)
895 .finish()
896 }
897}
898
899impl fmt::Display for Quat {
900 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
901 if let Some(p) = f.precision() {
902 write!(
903 f,
904 "[{:.*}, {:.*}, {:.*}, {:.*}]",
905 p, self.x, p, self.y, p, self.z, p, self.w
906 )
907 } else {
908 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
909 }
910 }
911}
912
913impl Add for Quat {
914 type Output = Self;
915 #[inline]
922 fn add(self, rhs: Self) -> Self {
923 Self::from_vec4(Vec4::from(self) + Vec4::from(rhs))
924 }
925}
926
927impl Add<&Self> for Quat {
928 type Output = Self;
929 #[inline]
930 fn add(self, rhs: &Self) -> Self {
931 self.add(*rhs)
932 }
933}
934
935impl Add<&Quat> for &Quat {
936 type Output = Quat;
937 #[inline]
938 fn add(self, rhs: &Quat) -> Quat {
939 (*self).add(*rhs)
940 }
941}
942
943impl Add<Quat> for &Quat {
944 type Output = Quat;
945 #[inline]
946 fn add(self, rhs: Quat) -> Quat {
947 (*self).add(rhs)
948 }
949}
950
951impl AddAssign for Quat {
952 #[inline]
953 fn add_assign(&mut self, rhs: Self) {
954 *self = self.add(rhs);
955 }
956}
957
958impl AddAssign<&Self> for Quat {
959 #[inline]
960 fn add_assign(&mut self, rhs: &Self) {
961 self.add_assign(*rhs);
962 }
963}
964
965impl Sub for Quat {
966 type Output = Self;
967 #[inline]
971 fn sub(self, rhs: Self) -> Self {
972 Self::from_vec4(Vec4::from(self) - Vec4::from(rhs))
973 }
974}
975
976impl Sub<&Self> for Quat {
977 type Output = Self;
978 #[inline]
979 fn sub(self, rhs: &Self) -> Self {
980 self.sub(*rhs)
981 }
982}
983
984impl Sub<&Quat> for &Quat {
985 type Output = Quat;
986 #[inline]
987 fn sub(self, rhs: &Quat) -> Quat {
988 (*self).sub(*rhs)
989 }
990}
991
992impl Sub<Quat> for &Quat {
993 type Output = Quat;
994 #[inline]
995 fn sub(self, rhs: Quat) -> Quat {
996 (*self).sub(rhs)
997 }
998}
999
1000impl SubAssign for Quat {
1001 #[inline]
1002 fn sub_assign(&mut self, rhs: Self) {
1003 *self = self.sub(rhs);
1004 }
1005}
1006
1007impl SubAssign<&Self> for Quat {
1008 #[inline]
1009 fn sub_assign(&mut self, rhs: &Self) {
1010 self.sub_assign(*rhs);
1011 }
1012}
1013
1014impl Mul<f32> for Quat {
1015 type Output = Self;
1016 #[inline]
1020 fn mul(self, rhs: f32) -> Self {
1021 Self::from_vec4(Vec4::from(self) * rhs)
1022 }
1023}
1024
1025impl Mul<&f32> for Quat {
1026 type Output = Self;
1027 #[inline]
1028 fn mul(self, rhs: &f32) -> Self {
1029 self.mul(*rhs)
1030 }
1031}
1032
1033impl Mul<&f32> for &Quat {
1034 type Output = Quat;
1035 #[inline]
1036 fn mul(self, rhs: &f32) -> Quat {
1037 (*self).mul(*rhs)
1038 }
1039}
1040
1041impl Mul<f32> for &Quat {
1042 type Output = Quat;
1043 #[inline]
1044 fn mul(self, rhs: f32) -> Quat {
1045 (*self).mul(rhs)
1046 }
1047}
1048
1049impl MulAssign<f32> for Quat {
1050 #[inline]
1051 fn mul_assign(&mut self, rhs: f32) {
1052 *self = self.mul(rhs);
1053 }
1054}
1055
1056impl MulAssign<&f32> for Quat {
1057 #[inline]
1058 fn mul_assign(&mut self, rhs: &f32) {
1059 self.mul_assign(*rhs);
1060 }
1061}
1062
1063impl Div<f32> for Quat {
1064 type Output = Self;
1065 #[inline]
1068 fn div(self, rhs: f32) -> Self {
1069 Self::from_vec4(Vec4::from(self) / rhs)
1070 }
1071}
1072
1073impl Div<&f32> for Quat {
1074 type Output = Self;
1075 #[inline]
1076 fn div(self, rhs: &f32) -> Self {
1077 self.div(*rhs)
1078 }
1079}
1080
1081impl Div<&f32> for &Quat {
1082 type Output = Quat;
1083 #[inline]
1084 fn div(self, rhs: &f32) -> Quat {
1085 (*self).div(*rhs)
1086 }
1087}
1088
1089impl Div<f32> for &Quat {
1090 type Output = Quat;
1091 #[inline]
1092 fn div(self, rhs: f32) -> Quat {
1093 (*self).div(rhs)
1094 }
1095}
1096
1097impl DivAssign<f32> for Quat {
1098 #[inline]
1099 fn div_assign(&mut self, rhs: f32) {
1100 *self = self.div(rhs);
1101 }
1102}
1103
1104impl DivAssign<&f32> for Quat {
1105 #[inline]
1106 fn div_assign(&mut self, rhs: &f32) {
1107 self.div_assign(*rhs);
1108 }
1109}
1110
1111impl Mul for Quat {
1112 type Output = Self;
1113 #[inline]
1123 fn mul(self, rhs: Self) -> Self {
1124 self.mul_quat(rhs)
1125 }
1126}
1127
1128impl Mul<&Self> for Quat {
1129 type Output = Self;
1130 #[inline]
1131 fn mul(self, rhs: &Self) -> Self {
1132 self.mul(*rhs)
1133 }
1134}
1135
1136impl Mul<&Quat> for &Quat {
1137 type Output = Quat;
1138 #[inline]
1139 fn mul(self, rhs: &Quat) -> Quat {
1140 (*self).mul(*rhs)
1141 }
1142}
1143
1144impl Mul<Quat> for &Quat {
1145 type Output = Quat;
1146 #[inline]
1147 fn mul(self, rhs: Quat) -> Quat {
1148 (*self).mul(rhs)
1149 }
1150}
1151
1152impl MulAssign for Quat {
1153 #[inline]
1154 fn mul_assign(&mut self, rhs: Self) {
1155 *self = self.mul(rhs);
1156 }
1157}
1158
1159impl MulAssign<&Self> for Quat {
1160 #[inline]
1161 fn mul_assign(&mut self, rhs: &Self) {
1162 self.mul_assign(*rhs);
1163 }
1164}
1165
1166impl Mul<Vec3> for Quat {
1167 type Output = Vec3;
1168 #[inline]
1174 fn mul(self, rhs: Vec3) -> Self::Output {
1175 self.mul_vec3(rhs)
1176 }
1177}
1178
1179impl Mul<&Vec3> for Quat {
1180 type Output = Vec3;
1181 #[inline]
1182 fn mul(self, rhs: &Vec3) -> Vec3 {
1183 self.mul(*rhs)
1184 }
1185}
1186
1187impl Mul<&Vec3> for &Quat {
1188 type Output = Vec3;
1189 #[inline]
1190 fn mul(self, rhs: &Vec3) -> Vec3 {
1191 (*self).mul(*rhs)
1192 }
1193}
1194
1195impl Mul<Vec3> for &Quat {
1196 type Output = Vec3;
1197 #[inline]
1198 fn mul(self, rhs: Vec3) -> Vec3 {
1199 (*self).mul(rhs)
1200 }
1201}
1202
1203impl Mul<Vec3A> for Quat {
1204 type Output = Vec3A;
1205 #[inline]
1206 fn mul(self, rhs: Vec3A) -> Self::Output {
1207 self.mul_vec3a(rhs)
1208 }
1209}
1210
1211impl Mul<&Vec3A> for Quat {
1212 type Output = Vec3A;
1213 #[inline]
1214 fn mul(self, rhs: &Vec3A) -> Vec3A {
1215 self.mul(*rhs)
1216 }
1217}
1218
1219impl Mul<&Vec3A> for &Quat {
1220 type Output = Vec3A;
1221 #[inline]
1222 fn mul(self, rhs: &Vec3A) -> Vec3A {
1223 (*self).mul(*rhs)
1224 }
1225}
1226
1227impl Mul<Vec3A> for &Quat {
1228 type Output = Vec3A;
1229 #[inline]
1230 fn mul(self, rhs: Vec3A) -> Vec3A {
1231 (*self).mul(rhs)
1232 }
1233}
1234
1235impl Neg for Quat {
1236 type Output = Self;
1237 #[inline]
1238 fn neg(self) -> Self {
1239 self * -1.0
1240 }
1241}
1242
1243impl Neg for &Quat {
1244 type Output = Quat;
1245 #[inline]
1246 fn neg(self) -> Quat {
1247 (*self).neg()
1248 }
1249}
1250
1251impl Default for Quat {
1252 #[inline]
1253 fn default() -> Self {
1254 Self::IDENTITY
1255 }
1256}
1257
1258impl PartialEq for Quat {
1259 #[inline]
1260 fn eq(&self, rhs: &Self) -> bool {
1261 Vec4::from(*self).eq(&Vec4::from(*rhs))
1262 }
1263}
1264
1265impl AsRef<[f32; 4]> for Quat {
1266 #[inline]
1267 fn as_ref(&self) -> &[f32; 4] {
1268 unsafe { &*(self as *const Self as *const [f32; 4]) }
1269 }
1270}
1271
1272impl Sum<Self> for Quat {
1273 fn sum<I>(iter: I) -> Self
1274 where
1275 I: Iterator<Item = Self>,
1276 {
1277 iter.fold(Self::ZERO, Self::add)
1278 }
1279}
1280
1281impl<'a> Sum<&'a Self> for Quat {
1282 fn sum<I>(iter: I) -> Self
1283 where
1284 I: Iterator<Item = &'a Self>,
1285 {
1286 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1287 }
1288}
1289
1290impl Product for Quat {
1291 fn product<I>(iter: I) -> Self
1292 where
1293 I: Iterator<Item = Self>,
1294 {
1295 iter.fold(Self::IDENTITY, Self::mul)
1296 }
1297}
1298
1299impl<'a> Product<&'a Self> for Quat {
1300 fn product<I>(iter: I) -> Self
1301 where
1302 I: Iterator<Item = &'a Self>,
1303 {
1304 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
1305 }
1306}
1307
1308impl From<Quat> for Vec4 {
1309 #[inline]
1310 fn from(q: Quat) -> Self {
1311 Self(q.0)
1312 }
1313}
1314
1315impl From<Quat> for (f32, f32, f32, f32) {
1316 #[inline]
1317 fn from(q: Quat) -> Self {
1318 Vec4::from(q).into()
1319 }
1320}
1321
1322impl From<Quat> for [f32; 4] {
1323 #[inline]
1324 fn from(q: Quat) -> Self {
1325 Vec4::from(q).into()
1326 }
1327}
1328
1329impl From<Quat> for __m128 {
1330 #[inline]
1331 fn from(q: Quat) -> Self {
1332 q.0
1333 }
1334}
1335
1336impl Deref for Quat {
1337 type Target = crate::deref::Vec4<f32>;
1338 #[inline]
1339 fn deref(&self) -> &Self::Target {
1340 unsafe { &*(self as *const Self).cast() }
1341 }
1342}
1343
1344impl DerefMut for Quat {
1345 #[inline]
1346 fn deref_mut(&mut self) -> &mut Self::Target {
1347 unsafe { &mut *(self as *mut Self).cast() }
1348 }
1349}