1use crate::{f32::math, sse2::*, BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[cfg(target_arch = "x86")]
10use core::arch::x86::*;
11#[cfg(target_arch = "x86_64")]
12use core::arch::x86_64::*;
13
14#[cfg(feature = "zerocopy")]
15use zerocopy_derive::*;
16
17#[repr(C)]
18union UnionCast {
19 a: [f32; 4],
20 v: Vec3A,
21}
22
23#[inline(always)]
25#[must_use]
26pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
27 Vec3A::new(x, y, z)
28}
29
30#[derive(Clone, Copy)]
40#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
41#[cfg_attr(
42 feature = "zerocopy",
43 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
44)]
45#[repr(transparent)]
46pub struct Vec3A(pub(crate) __m128);
47
48impl Vec3A {
49 pub const ZERO: Self = Self::splat(0.0);
51
52 pub const ONE: Self = Self::splat(1.0);
54
55 pub const NEG_ONE: Self = Self::splat(-1.0);
57
58 pub const MIN: Self = Self::splat(f32::MIN);
60
61 pub const MAX: Self = Self::splat(f32::MAX);
63
64 pub const NAN: Self = Self::splat(f32::NAN);
66
67 pub const INFINITY: Self = Self::splat(f32::INFINITY);
69
70 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
72
73 pub const X: Self = Self::new(1.0, 0.0, 0.0);
75
76 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
78
79 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
81
82 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
84
85 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
87
88 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
90
91 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
93
94 pub const USES_CORE_SIMD: bool = false;
96 pub const USES_NEON: bool = false;
98 pub const USES_SCALAR_MATH: bool = false;
100 pub const USES_SSE2: bool = true;
102 pub const USES_WASM_SIMD: bool = false;
104 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
105 pub const USES_WASM32_SIMD: bool = false;
106
107 #[inline(always)]
109 #[must_use]
110 pub const fn new(x: f32, y: f32, z: f32) -> Self {
111 unsafe { UnionCast { a: [x, y, z, z] }.v }
112 }
113
114 #[inline]
116 #[must_use]
117 pub const fn splat(v: f32) -> Self {
118 unsafe { UnionCast { a: [v; 4] }.v }
119 }
120
121 #[inline]
123 #[must_use]
124 pub fn map<F>(self, mut f: F) -> Self
125 where
126 F: FnMut(f32) -> f32,
127 {
128 Self::new(f(self.x), f(self.y), f(self.z))
129 }
130
131 #[inline]
137 #[must_use]
138 pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
139 Self(unsafe {
140 _mm_or_ps(
141 _mm_andnot_ps(mask.0, if_false.0),
142 _mm_and_ps(if_true.0, mask.0),
143 )
144 })
145 }
146
147 #[inline]
149 #[must_use]
150 pub const fn from_array(a: [f32; 3]) -> Self {
151 Self::new(a[0], a[1], a[2])
152 }
153
154 #[inline]
156 #[must_use]
157 pub const fn to_array(&self) -> [f32; 3] {
158 unsafe { *(self as *const Self as *const [f32; 3]) }
159 }
160
161 #[inline]
167 #[must_use]
168 pub const fn from_slice(slice: &[f32]) -> Self {
169 assert!(slice.len() >= 3);
170 Self::new(slice[0], slice[1], slice[2])
171 }
172
173 #[inline]
179 pub fn write_to_slice(self, slice: &mut [f32]) {
180 slice[..3].copy_from_slice(&self.to_array());
181 }
182
183 #[inline]
187 #[must_use]
188 pub fn from_vec4(v: Vec4) -> Self {
189 Self(v.0)
190 }
191
192 #[inline]
194 #[must_use]
195 pub fn extend(self, w: f32) -> Vec4 {
196 Vec4::new(self.x, self.y, self.z, w)
197 }
198
199 #[inline]
203 #[must_use]
204 pub fn truncate(self) -> Vec2 {
205 use crate::swizzles::Vec3Swizzles;
206 self.xy()
207 }
208
209 #[inline]
215 #[must_use]
216 pub fn from_homogeneous(v: Vec4) -> Self {
217 glam_assert!(v.w != 0.0);
218 Self::from_vec4(v) / v.w
219 }
220
221 #[inline]
223 #[must_use]
224 pub fn to_homogeneous(self) -> Vec4 {
225 self.extend(1.0)
226 }
227
228 #[inline]
230 #[must_use]
231 pub fn to_vec3(self) -> Vec3 {
232 Vec3::from(self)
233 }
234
235 #[inline]
237 #[must_use]
238 pub fn with_x(mut self, x: f32) -> Self {
239 self.x = x;
240 self
241 }
242
243 #[inline]
245 #[must_use]
246 pub fn with_y(mut self, y: f32) -> Self {
247 self.y = y;
248 self
249 }
250
251 #[inline]
253 #[must_use]
254 pub fn with_z(mut self, z: f32) -> Self {
255 self.z = z;
256 self
257 }
258
259 #[inline]
261 #[must_use]
262 pub fn dot(self, rhs: Self) -> f32 {
263 unsafe { dot3(self.0, rhs.0) }
264 }
265
266 #[inline]
268 #[must_use]
269 pub fn dot_into_vec(self, rhs: Self) -> Self {
270 Self(unsafe { dot3_into_m128(self.0, rhs.0) })
271 }
272
273 #[inline]
275 #[must_use]
276 pub fn cross(self, rhs: Self) -> Self {
277 unsafe {
278 let lhszxy = _mm_shuffle_ps(self.0, self.0, 0b01_01_00_10);
284 let rhszxy = _mm_shuffle_ps(rhs.0, rhs.0, 0b01_01_00_10);
285 let lhszxy_rhs = _mm_mul_ps(lhszxy, rhs.0);
286 let rhszxy_lhs = _mm_mul_ps(rhszxy, self.0);
287 let sub = _mm_sub_ps(lhszxy_rhs, rhszxy_lhs);
288 Self(_mm_shuffle_ps(sub, sub, 0b01_01_00_10))
289 }
290 }
291
292 #[inline]
299 #[must_use]
300 pub fn min(self, rhs: Self) -> Self {
301 Self(unsafe { _mm_min_ps(self.0, rhs.0) })
302 }
303
304 #[inline]
311 #[must_use]
312 pub fn max(self, rhs: Self) -> Self {
313 Self(unsafe { _mm_max_ps(self.0, rhs.0) })
314 }
315
316 #[inline]
327 #[must_use]
328 pub fn clamp(self, min: Self, max: Self) -> Self {
329 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
330 self.max(min).min(max)
331 }
332
333 #[inline]
340 #[must_use]
341 pub fn min_element(self) -> f32 {
342 unsafe {
343 let v = self.0;
344 let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b01_01_10_10));
345 let v = _mm_min_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01));
346 _mm_cvtss_f32(v)
347 }
348 }
349
350 #[inline]
357 #[must_use]
358 pub fn max_element(self) -> f32 {
359 unsafe {
360 let v = self.0;
361 let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_10_10));
362 let v = _mm_max_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_01));
363 _mm_cvtss_f32(v)
364 }
365 }
366
367 #[doc(alias = "argmin")]
369 #[inline]
370 #[must_use]
371 pub fn min_position(self) -> usize {
372 let mut min = self.x;
373 let mut index = 0;
374 if self.y < min {
375 min = self.y;
376 index = 1;
377 }
378 if self.z < min {
379 index = 2;
380 }
381 index
382 }
383
384 #[doc(alias = "argmax")]
386 #[inline]
387 #[must_use]
388 pub fn max_position(self) -> usize {
389 let mut max = self.x;
390 let mut index = 0;
391 if self.y > max {
392 max = self.y;
393 index = 1;
394 }
395 if self.z > max {
396 index = 2;
397 }
398 index
399 }
400
401 #[inline]
405 #[must_use]
406 pub fn element_sum(self) -> f32 {
407 unsafe {
408 let v = self.0;
409 let v = _mm_add_ps(v, _mm_shuffle_ps(v, Self::ZERO.0, 0b00_11_00_01));
410 let v = _mm_add_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10));
411 _mm_cvtss_f32(v)
412 }
413 }
414
415 #[inline]
419 #[must_use]
420 pub fn element_product(self) -> f32 {
421 unsafe {
422 let v = self.0;
423 let v = _mm_mul_ps(v, _mm_shuffle_ps(v, Self::ONE.0, 0b00_11_00_01));
424 let v = _mm_mul_ps(v, _mm_shuffle_ps(v, v, 0b00_00_00_10));
425 _mm_cvtss_f32(v)
426 }
427 }
428
429 #[inline]
435 #[must_use]
436 pub fn cmpeq(self, rhs: Self) -> BVec3A {
437 BVec3A(unsafe { _mm_cmpeq_ps(self.0, rhs.0) })
438 }
439
440 #[inline]
446 #[must_use]
447 pub fn cmpne(self, rhs: Self) -> BVec3A {
448 BVec3A(unsafe { _mm_cmpneq_ps(self.0, rhs.0) })
449 }
450
451 #[inline]
457 #[must_use]
458 pub fn cmpge(self, rhs: Self) -> BVec3A {
459 BVec3A(unsafe { _mm_cmpge_ps(self.0, rhs.0) })
460 }
461
462 #[inline]
468 #[must_use]
469 pub fn cmpgt(self, rhs: Self) -> BVec3A {
470 BVec3A(unsafe { _mm_cmpgt_ps(self.0, rhs.0) })
471 }
472
473 #[inline]
479 #[must_use]
480 pub fn cmple(self, rhs: Self) -> BVec3A {
481 BVec3A(unsafe { _mm_cmple_ps(self.0, rhs.0) })
482 }
483
484 #[inline]
490 #[must_use]
491 pub fn cmplt(self, rhs: Self) -> BVec3A {
492 BVec3A(unsafe { _mm_cmplt_ps(self.0, rhs.0) })
493 }
494
495 #[inline]
497 #[must_use]
498 pub fn abs(self) -> Self {
499 Self(unsafe { crate::sse2::m128_abs(self.0) })
500 }
501
502 #[inline]
508 #[must_use]
509 pub fn signum(self) -> Self {
510 let result = Self(unsafe { _mm_or_ps(_mm_and_ps(self.0, Self::NEG_ONE.0), Self::ONE.0) });
511 let mask = self.is_nan_mask();
512 Self::select(mask, self, result)
513 }
514
515 #[inline]
517 #[must_use]
518 pub fn copysign(self, rhs: Self) -> Self {
519 let mask = Self::splat(-0.0);
520 Self(unsafe { _mm_or_ps(_mm_and_ps(rhs.0, mask.0), _mm_andnot_ps(mask.0, self.0)) })
521 }
522
523 #[inline]
531 #[must_use]
532 pub fn is_negative_bitmask(self) -> u32 {
533 unsafe { (_mm_movemask_ps(self.0) as u32) & 0x7 }
534 }
535
536 #[inline]
541 #[must_use]
542 pub fn is_negative_mask(self) -> BVec3A {
543 BVec3A(unsafe {
544 _mm_castsi128_ps(_mm_cmplt_epi32(
545 _mm_castps_si128(self.0),
546 _mm_setzero_si128(),
547 ))
548 })
549 }
550
551 #[inline]
554 #[must_use]
555 pub fn is_finite(self) -> bool {
556 self.is_finite_mask().all()
557 }
558
559 #[inline]
563 #[must_use]
564 pub fn is_finite_mask(self) -> BVec3A {
565 BVec3A(unsafe { _mm_cmplt_ps(crate::sse2::m128_abs(self.0), Self::INFINITY.0) })
566 }
567
568 #[inline]
570 #[must_use]
571 pub fn is_nan(self) -> bool {
572 self.is_nan_mask().any()
573 }
574
575 #[inline]
579 #[must_use]
580 pub fn is_nan_mask(self) -> BVec3A {
581 BVec3A(unsafe { _mm_cmpunord_ps(self.0, self.0) })
582 }
583
584 #[doc(alias = "magnitude")]
586 #[inline]
587 #[must_use]
588 pub fn length(self) -> f32 {
589 unsafe {
590 let dot = dot3_in_x(self.0, self.0);
591 _mm_cvtss_f32(_mm_sqrt_ps(dot))
592 }
593 }
594
595 #[doc(alias = "magnitude2")]
599 #[inline]
600 #[must_use]
601 pub fn length_squared(self) -> f32 {
602 self.dot(self)
603 }
604
605 #[inline]
609 #[must_use]
610 pub fn length_recip(self) -> f32 {
611 unsafe {
612 let dot = dot3_in_x(self.0, self.0);
613 _mm_cvtss_f32(_mm_div_ps(Self::ONE.0, _mm_sqrt_ps(dot)))
614 }
615 }
616
617 #[inline]
619 #[must_use]
620 pub fn distance(self, rhs: Self) -> f32 {
621 (self - rhs).length()
622 }
623
624 #[inline]
626 #[must_use]
627 pub fn distance_squared(self, rhs: Self) -> f32 {
628 (self - rhs).length_squared()
629 }
630
631 #[inline]
633 #[must_use]
634 pub fn div_euclid(self, rhs: Self) -> Self {
635 Self::new(
636 math::div_euclid(self.x, rhs.x),
637 math::div_euclid(self.y, rhs.y),
638 math::div_euclid(self.z, rhs.z),
639 )
640 }
641
642 #[inline]
646 #[must_use]
647 pub fn rem_euclid(self, rhs: Self) -> Self {
648 Self::new(
649 math::rem_euclid(self.x, rhs.x),
650 math::rem_euclid(self.y, rhs.y),
651 math::rem_euclid(self.z, rhs.z),
652 )
653 }
654
655 #[inline]
665 #[must_use]
666 pub fn normalize(self) -> Self {
667 unsafe {
668 let length = _mm_sqrt_ps(dot3_into_m128(self.0, self.0));
669 #[allow(clippy::let_and_return)]
670 let normalized = Self(_mm_div_ps(self.0, length));
671 glam_assert!(normalized.is_finite());
672 normalized
673 }
674 }
675
676 #[inline]
683 #[must_use]
684 pub fn try_normalize(self) -> Option<Self> {
685 let rcp = self.length_recip();
686 if rcp.is_finite() && rcp > 0.0 {
687 Some(self * rcp)
688 } else {
689 None
690 }
691 }
692
693 #[inline]
701 #[must_use]
702 pub fn normalize_or(self, fallback: Self) -> Self {
703 let rcp = self.length_recip();
704 if rcp.is_finite() && rcp > 0.0 {
705 self * rcp
706 } else {
707 fallback
708 }
709 }
710
711 #[inline]
718 #[must_use]
719 pub fn normalize_or_zero(self) -> Self {
720 self.normalize_or(Self::ZERO)
721 }
722
723 #[inline]
727 #[must_use]
728 pub fn normalize_and_length(self) -> (Self, f32) {
729 let length = self.length();
730 let rcp = 1.0 / length;
731 if rcp.is_finite() && rcp > 0.0 {
732 (self * rcp, length)
733 } else {
734 (Self::X, 0.0)
735 }
736 }
737
738 #[inline]
742 #[must_use]
743 pub fn is_normalized(self) -> bool {
744 math::abs(self.length_squared() - 1.0) <= 2e-4
745 }
746
747 #[inline]
755 #[must_use]
756 pub fn project_onto(self, rhs: Self) -> Self {
757 let other_len_sq_rcp = rhs.dot(rhs).recip();
758 glam_assert!(other_len_sq_rcp.is_finite());
759 rhs * self.dot(rhs) * other_len_sq_rcp
760 }
761
762 #[doc(alias("plane"))]
773 #[inline]
774 #[must_use]
775 pub fn reject_from(self, rhs: Self) -> Self {
776 self - self.project_onto(rhs)
777 }
778
779 #[inline]
787 #[must_use]
788 pub fn project_onto_normalized(self, rhs: Self) -> Self {
789 glam_assert!(rhs.is_normalized());
790 rhs * self.dot(rhs)
791 }
792
793 #[doc(alias("plane"))]
804 #[inline]
805 #[must_use]
806 pub fn reject_from_normalized(self, rhs: Self) -> Self {
807 self - self.project_onto_normalized(rhs)
808 }
809
810 #[inline]
813 #[must_use]
814 pub fn round(self) -> Self {
815 Self(unsafe { m128_round(self.0) })
816 }
817
818 #[inline]
821 #[must_use]
822 pub fn floor(self) -> Self {
823 Self(unsafe { m128_floor(self.0) })
824 }
825
826 #[inline]
829 #[must_use]
830 pub fn ceil(self) -> Self {
831 Self(unsafe { m128_ceil(self.0) })
832 }
833
834 #[inline]
837 #[must_use]
838 pub fn trunc(self) -> Self {
839 Self(unsafe { m128_trunc(self.0) })
840 }
841
842 #[inline]
846 #[must_use]
847 pub fn step(self, rhs: Self) -> Self {
848 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
849 }
850
851 #[inline]
853 #[must_use]
854 pub fn saturate(self) -> Self {
855 self.clamp(Self::ZERO, Self::ONE)
856 }
857
858 #[inline]
865 #[must_use]
866 pub fn fract(self) -> Self {
867 self - self.trunc()
868 }
869
870 #[inline]
877 #[must_use]
878 pub fn fract_gl(self) -> Self {
879 self - self.floor()
880 }
881
882 #[inline]
885 #[must_use]
886 pub fn exp(self) -> Self {
887 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
888 }
889
890 #[inline]
892 #[must_use]
893 pub fn exp2(self) -> Self {
894 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
895 }
896
897 #[inline]
900 #[must_use]
901 pub fn ln(self) -> Self {
902 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
903 }
904
905 #[inline]
908 #[must_use]
909 pub fn log2(self) -> Self {
910 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
911 }
912
913 #[inline]
915 #[must_use]
916 pub fn powf(self, n: f32) -> Self {
917 Self::new(
918 math::powf(self.x, n),
919 math::powf(self.y, n),
920 math::powf(self.z, n),
921 )
922 }
923
924 #[inline]
927 #[must_use]
928 pub fn sqrt(self) -> Self {
929 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
930 }
931
932 #[inline]
934 #[must_use]
935 pub fn cos(self) -> Self {
936 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
937 }
938
939 #[inline]
941 #[must_use]
942 pub fn sin(self) -> Self {
943 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
944 }
945
946 #[inline]
948 #[must_use]
949 pub fn sin_cos(self) -> (Self, Self) {
950 let (sin_x, cos_x) = math::sin_cos(self.x);
951 let (sin_y, cos_y) = math::sin_cos(self.y);
952 let (sin_z, cos_z) = math::sin_cos(self.z);
953
954 (
955 Self::new(sin_x, sin_y, sin_z),
956 Self::new(cos_x, cos_y, cos_z),
957 )
958 }
959
960 #[inline]
962 #[must_use]
963 pub fn recip(self) -> Self {
964 Self(unsafe { _mm_div_ps(Self::ONE.0, self.0) })
965 }
966
967 #[doc(alias = "mix")]
973 #[inline]
974 #[must_use]
975 pub fn lerp(self, rhs: Self, s: f32) -> Self {
976 self * (1.0 - s) + rhs * s
977 }
978
979 #[inline]
984 #[must_use]
985 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
986 let a = rhs - self;
987 let len = a.length();
988 if len <= d || len <= 1e-4 {
989 return rhs;
990 }
991 self + a / len * d
992 }
993
994 #[inline]
1000 pub fn midpoint(self, rhs: Self) -> Self {
1001 (self + rhs) * 0.5
1002 }
1003
1004 #[inline]
1014 #[must_use]
1015 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1016 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1017 }
1018
1019 #[inline]
1025 #[must_use]
1026 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1027 glam_assert!(0.0 <= min);
1028 glam_assert!(min <= max);
1029 let length_sq = self.length_squared();
1030 if length_sq < min * min {
1031 min * (self / math::sqrt(length_sq))
1032 } else if length_sq > max * max {
1033 max * (self / math::sqrt(length_sq))
1034 } else {
1035 self
1036 }
1037 }
1038
1039 #[inline]
1045 #[must_use]
1046 pub fn clamp_length_max(self, max: f32) -> Self {
1047 glam_assert!(0.0 <= max);
1048 let length_sq = self.length_squared();
1049 if length_sq > max * max {
1050 max * (self / math::sqrt(length_sq))
1051 } else {
1052 self
1053 }
1054 }
1055
1056 #[inline]
1062 #[must_use]
1063 pub fn clamp_length_min(self, min: f32) -> Self {
1064 glam_assert!(0.0 <= min);
1065 let length_sq = self.length_squared();
1066 if length_sq < min * min {
1067 min * (self / math::sqrt(length_sq))
1068 } else {
1069 self
1070 }
1071 }
1072
1073 #[inline]
1081 #[must_use]
1082 pub fn mul_add(self, a: Self, b: Self) -> Self {
1083 #[cfg(target_feature = "fma")]
1084 unsafe {
1085 Self(_mm_fmadd_ps(self.0, a.0, b.0))
1086 }
1087 #[cfg(not(target_feature = "fma"))]
1088 Self::new(
1089 math::mul_add(self.x, a.x, b.x),
1090 math::mul_add(self.y, a.y, b.y),
1091 math::mul_add(self.z, a.z, b.z),
1092 )
1093 }
1094
1095 #[inline]
1104 #[must_use]
1105 pub fn reflect(self, normal: Self) -> Self {
1106 glam_assert!(normal.is_normalized());
1107 self - 2.0 * self.dot(normal) * normal
1108 }
1109
1110 #[inline]
1120 #[must_use]
1121 pub fn refract(self, normal: Self, eta: f32) -> Self {
1122 glam_assert!(self.is_normalized());
1123 glam_assert!(normal.is_normalized());
1124 let n_dot_i = normal.dot(self);
1125 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1126 if k >= 0.0 {
1127 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1128 } else {
1129 Self::ZERO
1130 }
1131 }
1132
1133 #[inline]
1137 #[must_use]
1138 pub fn angle_between(self, rhs: Self) -> f32 {
1139 math::acos_approx(
1140 self.dot(rhs)
1141 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1142 )
1143 }
1144
1145 #[inline]
1147 #[must_use]
1148 pub fn rotate_x(self, angle: f32) -> Self {
1149 let (sina, cosa) = math::sin_cos(angle);
1150 Self::new(
1151 self.x,
1152 self.y * cosa - self.z * sina,
1153 self.y * sina + self.z * cosa,
1154 )
1155 }
1156
1157 #[inline]
1159 #[must_use]
1160 pub fn rotate_y(self, angle: f32) -> Self {
1161 let (sina, cosa) = math::sin_cos(angle);
1162 Self::new(
1163 self.x * cosa + self.z * sina,
1164 self.y,
1165 self.x * -sina + self.z * cosa,
1166 )
1167 }
1168
1169 #[inline]
1171 #[must_use]
1172 pub fn rotate_z(self, angle: f32) -> Self {
1173 let (sina, cosa) = math::sin_cos(angle);
1174 Self::new(
1175 self.x * cosa - self.y * sina,
1176 self.x * sina + self.y * cosa,
1177 self.z,
1178 )
1179 }
1180
1181 #[inline]
1189 #[must_use]
1190 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1191 Quat::from_axis_angle(axis.into(), angle) * self
1192 }
1193
1194 #[inline]
1200 #[must_use]
1201 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1202 let angle_between = self.angle_between(rhs);
1203 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1205 let axis = self
1206 .cross(rhs)
1207 .try_normalize()
1208 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1209 Quat::from_axis_angle(axis.into(), angle) * self
1210 }
1211
1212 #[inline]
1219 #[must_use]
1220 pub fn any_orthogonal_vector(self) -> Self {
1221 if math::abs(self.x) > math::abs(self.y) {
1223 Self::new(-self.z, 0.0, self.x) } else {
1225 Self::new(0.0, self.z, -self.y) }
1227 }
1228
1229 #[inline]
1237 #[must_use]
1238 pub fn any_orthonormal_vector(self) -> Self {
1239 glam_assert!(self.is_normalized());
1240 let sign = math::signum(self.z);
1242 let a = -1.0 / (sign + self.z);
1243 let b = self.x * self.y * a;
1244 Self::new(b, sign + self.y * self.y * a, -self.y)
1245 }
1246
1247 #[inline]
1254 #[must_use]
1255 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1256 glam_assert!(self.is_normalized());
1257 let sign = math::signum(self.z);
1259 let a = -1.0 / (sign + self.z);
1260 let b = self.x * self.y * a;
1261 (
1262 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1263 Self::new(b, sign + self.y * self.y * a, -self.y),
1264 )
1265 }
1266
1267 #[inline]
1273 #[must_use]
1274 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1275 let self_length = self.length();
1276 let rhs_length = rhs.length();
1277 let dot = self.dot(rhs) / (self_length * rhs_length);
1279 if math::abs(dot) < 1.0 - 3e-7 {
1281 let theta = math::acos_approx(dot);
1283 let sin_theta = math::sin(theta);
1285 let t1 = math::sin(theta * (1. - s));
1286 let t2 = math::sin(theta * s);
1287
1288 let result_length = self_length.lerp(rhs_length, s);
1290 return (self * (result_length / self_length) * t1
1292 + rhs * (result_length / rhs_length) * t2)
1293 * sin_theta.recip();
1294 }
1295 if dot < 0.0 {
1296 let axis = self.any_orthogonal_vector().normalize().into();
1300 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1301 let result_length = self_length.lerp(rhs_length, s);
1303 rotation * self * (result_length / self_length)
1304 } else {
1305 self.lerp(rhs, s)
1307 }
1308 }
1309
1310 #[cfg(feature = "f64")]
1312 #[inline]
1313 #[must_use]
1314 pub fn as_dvec3(self) -> crate::DVec3 {
1315 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1316 }
1317
1318 #[cfg(feature = "i8")]
1320 #[inline]
1321 #[must_use]
1322 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1323 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1324 }
1325
1326 #[cfg(feature = "u8")]
1328 #[inline]
1329 #[must_use]
1330 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1331 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1332 }
1333
1334 #[cfg(feature = "i16")]
1336 #[inline]
1337 #[must_use]
1338 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1339 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1340 }
1341
1342 #[cfg(feature = "u16")]
1344 #[inline]
1345 #[must_use]
1346 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1347 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1348 }
1349
1350 #[cfg(feature = "i32")]
1352 #[inline]
1353 #[must_use]
1354 pub fn as_ivec3(self) -> crate::IVec3 {
1355 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1356 }
1357
1358 #[cfg(feature = "u32")]
1360 #[inline]
1361 #[must_use]
1362 pub fn as_uvec3(self) -> crate::UVec3 {
1363 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1364 }
1365
1366 #[cfg(feature = "i64")]
1368 #[inline]
1369 #[must_use]
1370 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1371 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1372 }
1373
1374 #[cfg(feature = "u64")]
1376 #[inline]
1377 #[must_use]
1378 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1379 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1380 }
1381
1382 #[cfg(feature = "isize")]
1384 #[inline]
1385 #[must_use]
1386 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1387 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1388 }
1389
1390 #[cfg(feature = "usize")]
1392 #[inline]
1393 #[must_use]
1394 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1395 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1396 }
1397}
1398
1399impl Default for Vec3A {
1400 #[inline(always)]
1401 fn default() -> Self {
1402 Self::ZERO
1403 }
1404}
1405
1406impl PartialEq for Vec3A {
1407 #[inline]
1408 fn eq(&self, rhs: &Self) -> bool {
1409 self.cmpeq(*rhs).all()
1410 }
1411}
1412
1413impl Div for Vec3A {
1414 type Output = Self;
1415 #[inline]
1416 fn div(self, rhs: Self) -> Self {
1417 Self(unsafe { _mm_div_ps(self.0, rhs.0) })
1418 }
1419}
1420
1421impl Div<&Self> for Vec3A {
1422 type Output = Self;
1423 #[inline]
1424 fn div(self, rhs: &Self) -> Self {
1425 self.div(*rhs)
1426 }
1427}
1428
1429impl Div<&Vec3A> for &Vec3A {
1430 type Output = Vec3A;
1431 #[inline]
1432 fn div(self, rhs: &Vec3A) -> Vec3A {
1433 (*self).div(*rhs)
1434 }
1435}
1436
1437impl Div<Vec3A> for &Vec3A {
1438 type Output = Vec3A;
1439 #[inline]
1440 fn div(self, rhs: Vec3A) -> Vec3A {
1441 (*self).div(rhs)
1442 }
1443}
1444
1445impl DivAssign for Vec3A {
1446 #[inline]
1447 fn div_assign(&mut self, rhs: Self) {
1448 self.0 = unsafe { _mm_div_ps(self.0, rhs.0) };
1449 }
1450}
1451
1452impl DivAssign<&Self> for Vec3A {
1453 #[inline]
1454 fn div_assign(&mut self, rhs: &Self) {
1455 self.div_assign(*rhs);
1456 }
1457}
1458
1459impl Div<f32> for Vec3A {
1460 type Output = Self;
1461 #[inline]
1462 fn div(self, rhs: f32) -> Self {
1463 Self(unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) })
1464 }
1465}
1466
1467impl Div<&f32> for Vec3A {
1468 type Output = Self;
1469 #[inline]
1470 fn div(self, rhs: &f32) -> Self {
1471 self.div(*rhs)
1472 }
1473}
1474
1475impl Div<&f32> for &Vec3A {
1476 type Output = Vec3A;
1477 #[inline]
1478 fn div(self, rhs: &f32) -> Vec3A {
1479 (*self).div(*rhs)
1480 }
1481}
1482
1483impl Div<f32> for &Vec3A {
1484 type Output = Vec3A;
1485 #[inline]
1486 fn div(self, rhs: f32) -> Vec3A {
1487 (*self).div(rhs)
1488 }
1489}
1490
1491impl DivAssign<f32> for Vec3A {
1492 #[inline]
1493 fn div_assign(&mut self, rhs: f32) {
1494 self.0 = unsafe { _mm_div_ps(self.0, _mm_set1_ps(rhs)) };
1495 }
1496}
1497
1498impl DivAssign<&f32> for Vec3A {
1499 #[inline]
1500 fn div_assign(&mut self, rhs: &f32) {
1501 self.div_assign(*rhs);
1502 }
1503}
1504
1505impl Div<Vec3A> for f32 {
1506 type Output = Vec3A;
1507 #[inline]
1508 fn div(self, rhs: Vec3A) -> Vec3A {
1509 Vec3A(unsafe { _mm_div_ps(_mm_set1_ps(self), rhs.0) })
1510 }
1511}
1512
1513impl Div<&Vec3A> for f32 {
1514 type Output = Vec3A;
1515 #[inline]
1516 fn div(self, rhs: &Vec3A) -> Vec3A {
1517 self.div(*rhs)
1518 }
1519}
1520
1521impl Div<&Vec3A> for &f32 {
1522 type Output = Vec3A;
1523 #[inline]
1524 fn div(self, rhs: &Vec3A) -> Vec3A {
1525 (*self).div(*rhs)
1526 }
1527}
1528
1529impl Div<Vec3A> for &f32 {
1530 type Output = Vec3A;
1531 #[inline]
1532 fn div(self, rhs: Vec3A) -> Vec3A {
1533 (*self).div(rhs)
1534 }
1535}
1536
1537impl Mul for Vec3A {
1538 type Output = Self;
1539 #[inline]
1540 fn mul(self, rhs: Self) -> Self {
1541 Self(unsafe { _mm_mul_ps(self.0, rhs.0) })
1542 }
1543}
1544
1545impl Mul<&Self> for Vec3A {
1546 type Output = Self;
1547 #[inline]
1548 fn mul(self, rhs: &Self) -> Self {
1549 self.mul(*rhs)
1550 }
1551}
1552
1553impl Mul<&Vec3A> for &Vec3A {
1554 type Output = Vec3A;
1555 #[inline]
1556 fn mul(self, rhs: &Vec3A) -> Vec3A {
1557 (*self).mul(*rhs)
1558 }
1559}
1560
1561impl Mul<Vec3A> for &Vec3A {
1562 type Output = Vec3A;
1563 #[inline]
1564 fn mul(self, rhs: Vec3A) -> Vec3A {
1565 (*self).mul(rhs)
1566 }
1567}
1568
1569impl MulAssign for Vec3A {
1570 #[inline]
1571 fn mul_assign(&mut self, rhs: Self) {
1572 self.0 = unsafe { _mm_mul_ps(self.0, rhs.0) };
1573 }
1574}
1575
1576impl MulAssign<&Self> for Vec3A {
1577 #[inline]
1578 fn mul_assign(&mut self, rhs: &Self) {
1579 self.mul_assign(*rhs);
1580 }
1581}
1582
1583impl Mul<f32> for Vec3A {
1584 type Output = Self;
1585 #[inline]
1586 fn mul(self, rhs: f32) -> Self {
1587 Self(unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) })
1588 }
1589}
1590
1591impl Mul<&f32> for Vec3A {
1592 type Output = Self;
1593 #[inline]
1594 fn mul(self, rhs: &f32) -> Self {
1595 self.mul(*rhs)
1596 }
1597}
1598
1599impl Mul<&f32> for &Vec3A {
1600 type Output = Vec3A;
1601 #[inline]
1602 fn mul(self, rhs: &f32) -> Vec3A {
1603 (*self).mul(*rhs)
1604 }
1605}
1606
1607impl Mul<f32> for &Vec3A {
1608 type Output = Vec3A;
1609 #[inline]
1610 fn mul(self, rhs: f32) -> Vec3A {
1611 (*self).mul(rhs)
1612 }
1613}
1614
1615impl MulAssign<f32> for Vec3A {
1616 #[inline]
1617 fn mul_assign(&mut self, rhs: f32) {
1618 self.0 = unsafe { _mm_mul_ps(self.0, _mm_set1_ps(rhs)) };
1619 }
1620}
1621
1622impl MulAssign<&f32> for Vec3A {
1623 #[inline]
1624 fn mul_assign(&mut self, rhs: &f32) {
1625 self.mul_assign(*rhs);
1626 }
1627}
1628
1629impl Mul<Vec3A> for f32 {
1630 type Output = Vec3A;
1631 #[inline]
1632 fn mul(self, rhs: Vec3A) -> Vec3A {
1633 Vec3A(unsafe { _mm_mul_ps(_mm_set1_ps(self), rhs.0) })
1634 }
1635}
1636
1637impl Mul<&Vec3A> for f32 {
1638 type Output = Vec3A;
1639 #[inline]
1640 fn mul(self, rhs: &Vec3A) -> Vec3A {
1641 self.mul(*rhs)
1642 }
1643}
1644
1645impl Mul<&Vec3A> for &f32 {
1646 type Output = Vec3A;
1647 #[inline]
1648 fn mul(self, rhs: &Vec3A) -> Vec3A {
1649 (*self).mul(*rhs)
1650 }
1651}
1652
1653impl Mul<Vec3A> for &f32 {
1654 type Output = Vec3A;
1655 #[inline]
1656 fn mul(self, rhs: Vec3A) -> Vec3A {
1657 (*self).mul(rhs)
1658 }
1659}
1660
1661impl Add for Vec3A {
1662 type Output = Self;
1663 #[inline]
1664 fn add(self, rhs: Self) -> Self {
1665 Self(unsafe { _mm_add_ps(self.0, rhs.0) })
1666 }
1667}
1668
1669impl Add<&Self> for Vec3A {
1670 type Output = Self;
1671 #[inline]
1672 fn add(self, rhs: &Self) -> Self {
1673 self.add(*rhs)
1674 }
1675}
1676
1677impl Add<&Vec3A> for &Vec3A {
1678 type Output = Vec3A;
1679 #[inline]
1680 fn add(self, rhs: &Vec3A) -> Vec3A {
1681 (*self).add(*rhs)
1682 }
1683}
1684
1685impl Add<Vec3A> for &Vec3A {
1686 type Output = Vec3A;
1687 #[inline]
1688 fn add(self, rhs: Vec3A) -> Vec3A {
1689 (*self).add(rhs)
1690 }
1691}
1692
1693impl AddAssign for Vec3A {
1694 #[inline]
1695 fn add_assign(&mut self, rhs: Self) {
1696 self.0 = unsafe { _mm_add_ps(self.0, rhs.0) };
1697 }
1698}
1699
1700impl AddAssign<&Self> for Vec3A {
1701 #[inline]
1702 fn add_assign(&mut self, rhs: &Self) {
1703 self.add_assign(*rhs);
1704 }
1705}
1706
1707impl Add<f32> for Vec3A {
1708 type Output = Self;
1709 #[inline]
1710 fn add(self, rhs: f32) -> Self {
1711 Self(unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) })
1712 }
1713}
1714
1715impl Add<&f32> for Vec3A {
1716 type Output = Self;
1717 #[inline]
1718 fn add(self, rhs: &f32) -> Self {
1719 self.add(*rhs)
1720 }
1721}
1722
1723impl Add<&f32> for &Vec3A {
1724 type Output = Vec3A;
1725 #[inline]
1726 fn add(self, rhs: &f32) -> Vec3A {
1727 (*self).add(*rhs)
1728 }
1729}
1730
1731impl Add<f32> for &Vec3A {
1732 type Output = Vec3A;
1733 #[inline]
1734 fn add(self, rhs: f32) -> Vec3A {
1735 (*self).add(rhs)
1736 }
1737}
1738
1739impl AddAssign<f32> for Vec3A {
1740 #[inline]
1741 fn add_assign(&mut self, rhs: f32) {
1742 self.0 = unsafe { _mm_add_ps(self.0, _mm_set1_ps(rhs)) };
1743 }
1744}
1745
1746impl AddAssign<&f32> for Vec3A {
1747 #[inline]
1748 fn add_assign(&mut self, rhs: &f32) {
1749 self.add_assign(*rhs);
1750 }
1751}
1752
1753impl Add<Vec3A> for f32 {
1754 type Output = Vec3A;
1755 #[inline]
1756 fn add(self, rhs: Vec3A) -> Vec3A {
1757 Vec3A(unsafe { _mm_add_ps(_mm_set1_ps(self), rhs.0) })
1758 }
1759}
1760
1761impl Add<&Vec3A> for f32 {
1762 type Output = Vec3A;
1763 #[inline]
1764 fn add(self, rhs: &Vec3A) -> Vec3A {
1765 self.add(*rhs)
1766 }
1767}
1768
1769impl Add<&Vec3A> for &f32 {
1770 type Output = Vec3A;
1771 #[inline]
1772 fn add(self, rhs: &Vec3A) -> Vec3A {
1773 (*self).add(*rhs)
1774 }
1775}
1776
1777impl Add<Vec3A> for &f32 {
1778 type Output = Vec3A;
1779 #[inline]
1780 fn add(self, rhs: Vec3A) -> Vec3A {
1781 (*self).add(rhs)
1782 }
1783}
1784
1785impl Sub for Vec3A {
1786 type Output = Self;
1787 #[inline]
1788 fn sub(self, rhs: Self) -> Self {
1789 Self(unsafe { _mm_sub_ps(self.0, rhs.0) })
1790 }
1791}
1792
1793impl Sub<&Self> for Vec3A {
1794 type Output = Self;
1795 #[inline]
1796 fn sub(self, rhs: &Self) -> Self {
1797 self.sub(*rhs)
1798 }
1799}
1800
1801impl Sub<&Vec3A> for &Vec3A {
1802 type Output = Vec3A;
1803 #[inline]
1804 fn sub(self, rhs: &Vec3A) -> Vec3A {
1805 (*self).sub(*rhs)
1806 }
1807}
1808
1809impl Sub<Vec3A> for &Vec3A {
1810 type Output = Vec3A;
1811 #[inline]
1812 fn sub(self, rhs: Vec3A) -> Vec3A {
1813 (*self).sub(rhs)
1814 }
1815}
1816
1817impl SubAssign for Vec3A {
1818 #[inline]
1819 fn sub_assign(&mut self, rhs: Self) {
1820 self.0 = unsafe { _mm_sub_ps(self.0, rhs.0) };
1821 }
1822}
1823
1824impl SubAssign<&Self> for Vec3A {
1825 #[inline]
1826 fn sub_assign(&mut self, rhs: &Self) {
1827 self.sub_assign(*rhs);
1828 }
1829}
1830
1831impl Sub<f32> for Vec3A {
1832 type Output = Self;
1833 #[inline]
1834 fn sub(self, rhs: f32) -> Self {
1835 Self(unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) })
1836 }
1837}
1838
1839impl Sub<&f32> for Vec3A {
1840 type Output = Self;
1841 #[inline]
1842 fn sub(self, rhs: &f32) -> Self {
1843 self.sub(*rhs)
1844 }
1845}
1846
1847impl Sub<&f32> for &Vec3A {
1848 type Output = Vec3A;
1849 #[inline]
1850 fn sub(self, rhs: &f32) -> Vec3A {
1851 (*self).sub(*rhs)
1852 }
1853}
1854
1855impl Sub<f32> for &Vec3A {
1856 type Output = Vec3A;
1857 #[inline]
1858 fn sub(self, rhs: f32) -> Vec3A {
1859 (*self).sub(rhs)
1860 }
1861}
1862
1863impl SubAssign<f32> for Vec3A {
1864 #[inline]
1865 fn sub_assign(&mut self, rhs: f32) {
1866 self.0 = unsafe { _mm_sub_ps(self.0, _mm_set1_ps(rhs)) };
1867 }
1868}
1869
1870impl SubAssign<&f32> for Vec3A {
1871 #[inline]
1872 fn sub_assign(&mut self, rhs: &f32) {
1873 self.sub_assign(*rhs);
1874 }
1875}
1876
1877impl Sub<Vec3A> for f32 {
1878 type Output = Vec3A;
1879 #[inline]
1880 fn sub(self, rhs: Vec3A) -> Vec3A {
1881 Vec3A(unsafe { _mm_sub_ps(_mm_set1_ps(self), rhs.0) })
1882 }
1883}
1884
1885impl Sub<&Vec3A> for f32 {
1886 type Output = Vec3A;
1887 #[inline]
1888 fn sub(self, rhs: &Vec3A) -> Vec3A {
1889 self.sub(*rhs)
1890 }
1891}
1892
1893impl Sub<&Vec3A> for &f32 {
1894 type Output = Vec3A;
1895 #[inline]
1896 fn sub(self, rhs: &Vec3A) -> Vec3A {
1897 (*self).sub(*rhs)
1898 }
1899}
1900
1901impl Sub<Vec3A> for &f32 {
1902 type Output = Vec3A;
1903 #[inline]
1904 fn sub(self, rhs: Vec3A) -> Vec3A {
1905 (*self).sub(rhs)
1906 }
1907}
1908
1909impl Rem for Vec3A {
1910 type Output = Self;
1911 #[inline]
1912 fn rem(self, rhs: Self) -> Self {
1913 unsafe {
1914 let n = m128_floor(_mm_div_ps(self.0, rhs.0));
1915 Self(_mm_sub_ps(self.0, _mm_mul_ps(n, rhs.0)))
1916 }
1917 }
1918}
1919
1920impl Rem<&Self> for Vec3A {
1921 type Output = Self;
1922 #[inline]
1923 fn rem(self, rhs: &Self) -> Self {
1924 self.rem(*rhs)
1925 }
1926}
1927
1928impl Rem<&Vec3A> for &Vec3A {
1929 type Output = Vec3A;
1930 #[inline]
1931 fn rem(self, rhs: &Vec3A) -> Vec3A {
1932 (*self).rem(*rhs)
1933 }
1934}
1935
1936impl Rem<Vec3A> for &Vec3A {
1937 type Output = Vec3A;
1938 #[inline]
1939 fn rem(self, rhs: Vec3A) -> Vec3A {
1940 (*self).rem(rhs)
1941 }
1942}
1943
1944impl RemAssign for Vec3A {
1945 #[inline]
1946 fn rem_assign(&mut self, rhs: Self) {
1947 *self = self.rem(rhs);
1948 }
1949}
1950
1951impl RemAssign<&Self> for Vec3A {
1952 #[inline]
1953 fn rem_assign(&mut self, rhs: &Self) {
1954 self.rem_assign(*rhs);
1955 }
1956}
1957
1958impl Rem<f32> for Vec3A {
1959 type Output = Self;
1960 #[inline]
1961 fn rem(self, rhs: f32) -> Self {
1962 self.rem(Self::splat(rhs))
1963 }
1964}
1965
1966impl Rem<&f32> for Vec3A {
1967 type Output = Self;
1968 #[inline]
1969 fn rem(self, rhs: &f32) -> Self {
1970 self.rem(*rhs)
1971 }
1972}
1973
1974impl Rem<&f32> for &Vec3A {
1975 type Output = Vec3A;
1976 #[inline]
1977 fn rem(self, rhs: &f32) -> Vec3A {
1978 (*self).rem(*rhs)
1979 }
1980}
1981
1982impl Rem<f32> for &Vec3A {
1983 type Output = Vec3A;
1984 #[inline]
1985 fn rem(self, rhs: f32) -> Vec3A {
1986 (*self).rem(rhs)
1987 }
1988}
1989
1990impl RemAssign<f32> for Vec3A {
1991 #[inline]
1992 fn rem_assign(&mut self, rhs: f32) {
1993 *self = self.rem(Self::splat(rhs));
1994 }
1995}
1996
1997impl RemAssign<&f32> for Vec3A {
1998 #[inline]
1999 fn rem_assign(&mut self, rhs: &f32) {
2000 self.rem_assign(*rhs);
2001 }
2002}
2003
2004impl Rem<Vec3A> for f32 {
2005 type Output = Vec3A;
2006 #[inline]
2007 fn rem(self, rhs: Vec3A) -> Vec3A {
2008 Vec3A::splat(self).rem(rhs)
2009 }
2010}
2011
2012impl Rem<&Vec3A> for f32 {
2013 type Output = Vec3A;
2014 #[inline]
2015 fn rem(self, rhs: &Vec3A) -> Vec3A {
2016 self.rem(*rhs)
2017 }
2018}
2019
2020impl Rem<&Vec3A> for &f32 {
2021 type Output = Vec3A;
2022 #[inline]
2023 fn rem(self, rhs: &Vec3A) -> Vec3A {
2024 (*self).rem(*rhs)
2025 }
2026}
2027
2028impl Rem<Vec3A> for &f32 {
2029 type Output = Vec3A;
2030 #[inline]
2031 fn rem(self, rhs: Vec3A) -> Vec3A {
2032 (*self).rem(rhs)
2033 }
2034}
2035
2036impl AsRef<[f32; 3]> for Vec3A {
2037 #[inline]
2038 fn as_ref(&self) -> &[f32; 3] {
2039 unsafe { &*(self as *const Self as *const [f32; 3]) }
2040 }
2041}
2042
2043impl AsMut<[f32; 3]> for Vec3A {
2044 #[inline]
2045 fn as_mut(&mut self) -> &mut [f32; 3] {
2046 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2047 }
2048}
2049
2050impl Sum for Vec3A {
2051 #[inline]
2052 fn sum<I>(iter: I) -> Self
2053 where
2054 I: Iterator<Item = Self>,
2055 {
2056 iter.fold(Self::ZERO, Self::add)
2057 }
2058}
2059
2060impl<'a> Sum<&'a Self> for Vec3A {
2061 #[inline]
2062 fn sum<I>(iter: I) -> Self
2063 where
2064 I: Iterator<Item = &'a Self>,
2065 {
2066 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2067 }
2068}
2069
2070impl Product for Vec3A {
2071 #[inline]
2072 fn product<I>(iter: I) -> Self
2073 where
2074 I: Iterator<Item = Self>,
2075 {
2076 iter.fold(Self::ONE, Self::mul)
2077 }
2078}
2079
2080impl<'a> Product<&'a Self> for Vec3A {
2081 #[inline]
2082 fn product<I>(iter: I) -> Self
2083 where
2084 I: Iterator<Item = &'a Self>,
2085 {
2086 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2087 }
2088}
2089
2090impl Neg for Vec3A {
2091 type Output = Self;
2092 #[inline]
2093 fn neg(self) -> Self {
2094 Self(unsafe { _mm_xor_ps(_mm_set1_ps(-0.0), self.0) })
2095 }
2096}
2097
2098impl Neg for &Vec3A {
2099 type Output = Vec3A;
2100 #[inline]
2101 fn neg(self) -> Vec3A {
2102 (*self).neg()
2103 }
2104}
2105
2106impl Index<usize> for Vec3A {
2107 type Output = f32;
2108 #[inline]
2109 fn index(&self, index: usize) -> &Self::Output {
2110 match index {
2111 0 => &self.x,
2112 1 => &self.y,
2113 2 => &self.z,
2114 _ => panic!("index out of bounds"),
2115 }
2116 }
2117}
2118
2119impl IndexMut<usize> for Vec3A {
2120 #[inline]
2121 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2122 match index {
2123 0 => &mut self.x,
2124 1 => &mut self.y,
2125 2 => &mut self.z,
2126 _ => panic!("index out of bounds"),
2127 }
2128 }
2129}
2130
2131impl fmt::Display for Vec3A {
2132 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2133 if let Some(p) = f.precision() {
2134 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2135 } else {
2136 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2137 }
2138 }
2139}
2140
2141impl fmt::Debug for Vec3A {
2142 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2143 fmt.debug_tuple(stringify!(Vec3A))
2144 .field(&self.x)
2145 .field(&self.y)
2146 .field(&self.z)
2147 .finish()
2148 }
2149}
2150
2151impl From<Vec3A> for __m128 {
2152 #[inline(always)]
2153 fn from(t: Vec3A) -> Self {
2154 t.0
2155 }
2156}
2157
2158impl From<__m128> for Vec3A {
2159 #[inline(always)]
2160 fn from(t: __m128) -> Self {
2161 Self(t)
2162 }
2163}
2164
2165impl From<[f32; 3]> for Vec3A {
2166 #[inline]
2167 fn from(a: [f32; 3]) -> Self {
2168 Self::new(a[0], a[1], a[2])
2169 }
2170}
2171
2172impl From<Vec3A> for [f32; 3] {
2173 #[inline]
2174 fn from(v: Vec3A) -> Self {
2175 use crate::Align16;
2176 use core::mem::MaybeUninit;
2177 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2178 unsafe {
2179 _mm_store_ps(out.as_mut_ptr().cast(), v.0);
2180 out.assume_init().0
2181 }
2182 }
2183}
2184
2185impl From<(f32, f32, f32)> for Vec3A {
2186 #[inline]
2187 fn from(t: (f32, f32, f32)) -> Self {
2188 Self::new(t.0, t.1, t.2)
2189 }
2190}
2191
2192impl From<Vec3A> for (f32, f32, f32) {
2193 #[inline]
2194 fn from(v: Vec3A) -> Self {
2195 (v.x, v.y, v.z)
2196 }
2197}
2198
2199impl From<Vec3> for Vec3A {
2200 #[inline]
2201 fn from(v: Vec3) -> Self {
2202 Self::new(v.x, v.y, v.z)
2203 }
2204}
2205
2206impl From<Vec3A> for Vec3 {
2207 #[inline]
2208 fn from(v: Vec3A) -> Self {
2209 use crate::Align16;
2210 use core::mem::MaybeUninit;
2211 let mut out: MaybeUninit<Align16<Self>> = MaybeUninit::uninit();
2212 unsafe {
2213 _mm_store_ps(out.as_mut_ptr().cast(), v.0);
2214 out.assume_init().0
2215 }
2216 }
2217}
2218
2219impl From<(Vec2, f32)> for Vec3A {
2220 #[inline]
2221 fn from((v, z): (Vec2, f32)) -> Self {
2222 Self::new(v.x, v.y, z)
2223 }
2224}
2225
2226impl Deref for Vec3A {
2227 type Target = crate::deref::Vec3<f32>;
2228 #[inline]
2229 fn deref(&self) -> &Self::Target {
2230 unsafe { &*(self as *const Self).cast() }
2231 }
2232}
2233
2234impl DerefMut for Vec3A {
2235 #[inline]
2236 fn deref_mut(&mut self) -> &mut Self::Target {
2237 unsafe { &mut *(self as *mut Self).cast() }
2238 }
2239}
2240
2241impl From<BVec3> for Vec3A {
2242 #[inline]
2243 fn from(v: BVec3) -> Self {
2244 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2245 }
2246}
2247
2248impl From<BVec3A> for Vec3A {
2249 #[inline]
2250 fn from(v: BVec3A) -> Self {
2251 let bool_array: [bool; 3] = v.into();
2252 Self::new(
2253 f32::from(bool_array[0]),
2254 f32::from(bool_array[1]),
2255 f32::from(bool_array[2]),
2256 )
2257 }
2258}