1use crate::{f32::math, BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3A, Vec4};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[cfg(feature = "zerocopy")]
10use zerocopy_derive::*;
11
12#[inline(always)]
14#[must_use]
15pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
16 Vec3::new(x, y, z)
17}
18
19#[derive(Clone, Copy, PartialEq)]
21#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
22#[cfg_attr(
23 feature = "zerocopy",
24 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
25)]
26#[repr(C)]
27#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
28pub struct Vec3 {
29 pub x: f32,
30 pub y: f32,
31 pub z: f32,
32}
33
34impl Vec3 {
35 pub const ZERO: Self = Self::splat(0.0);
37
38 pub const ONE: Self = Self::splat(1.0);
40
41 pub const NEG_ONE: Self = Self::splat(-1.0);
43
44 pub const MIN: Self = Self::splat(f32::MIN);
46
47 pub const MAX: Self = Self::splat(f32::MAX);
49
50 pub const NAN: Self = Self::splat(f32::NAN);
52
53 pub const INFINITY: Self = Self::splat(f32::INFINITY);
55
56 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
58
59 pub const X: Self = Self::new(1.0, 0.0, 0.0);
61
62 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
64
65 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
67
68 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
70
71 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
73
74 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
76
77 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
79
80 pub const USES_CORE_SIMD: bool = false;
82 pub const USES_NEON: bool = false;
84 pub const USES_SCALAR_MATH: bool = true;
86 pub const USES_SSE2: bool = false;
88 pub const USES_WASM_SIMD: bool = false;
90 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
91 pub const USES_WASM32_SIMD: bool = false;
92
93 #[inline(always)]
95 #[must_use]
96 pub const fn new(x: f32, y: f32, z: f32) -> Self {
97 Self { x, y, z }
98 }
99
100 #[inline]
102 #[must_use]
103 pub const fn splat(v: f32) -> Self {
104 Self { x: v, y: v, z: v }
105 }
106
107 #[inline]
109 #[must_use]
110 pub fn map<F>(self, mut f: F) -> Self
111 where
112 F: FnMut(f32) -> f32,
113 {
114 Self::new(f(self.x), f(self.y), f(self.z))
115 }
116
117 #[inline]
123 #[must_use]
124 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
125 Self {
126 x: if mask.test(0) { if_true.x } else { if_false.x },
127 y: if mask.test(1) { if_true.y } else { if_false.y },
128 z: if mask.test(2) { if_true.z } else { if_false.z },
129 }
130 }
131
132 #[inline]
134 #[must_use]
135 pub const fn from_array(a: [f32; 3]) -> Self {
136 Self::new(a[0], a[1], a[2])
137 }
138
139 #[inline]
141 #[must_use]
142 pub const fn to_array(&self) -> [f32; 3] {
143 [self.x, self.y, self.z]
144 }
145
146 #[inline]
152 #[must_use]
153 pub const fn from_slice(slice: &[f32]) -> Self {
154 assert!(slice.len() >= 3);
155 Self::new(slice[0], slice[1], slice[2])
156 }
157
158 #[inline]
164 pub fn write_to_slice(self, slice: &mut [f32]) {
165 slice[..3].copy_from_slice(&self.to_array());
166 }
167
168 #[allow(dead_code)]
170 #[inline]
171 #[must_use]
172 pub(crate) fn from_vec4(v: Vec4) -> Self {
173 Self {
174 x: v.x,
175 y: v.y,
176 z: v.z,
177 }
178 }
179
180 #[inline]
182 #[must_use]
183 pub fn extend(self, w: f32) -> Vec4 {
184 Vec4::new(self.x, self.y, self.z, w)
185 }
186
187 #[inline]
191 #[must_use]
192 pub fn truncate(self) -> Vec2 {
193 use crate::swizzles::Vec3Swizzles;
194 self.xy()
195 }
196
197 #[inline]
203 #[must_use]
204 pub fn from_homogeneous(v: Vec4) -> Self {
205 glam_assert!(v.w != 0.0);
206 Self::from_vec4(v) / v.w
207 }
208
209 #[inline]
211 #[must_use]
212 pub fn to_homogeneous(self) -> Vec4 {
213 self.extend(1.0)
214 }
215
216 #[inline]
218 #[must_use]
219 pub fn to_vec3a(self) -> Vec3A {
220 Vec3A::from(self)
221 }
222
223 #[inline]
225 #[must_use]
226 pub fn with_x(mut self, x: f32) -> Self {
227 self.x = x;
228 self
229 }
230
231 #[inline]
233 #[must_use]
234 pub fn with_y(mut self, y: f32) -> Self {
235 self.y = y;
236 self
237 }
238
239 #[inline]
241 #[must_use]
242 pub fn with_z(mut self, z: f32) -> Self {
243 self.z = z;
244 self
245 }
246
247 #[inline]
249 #[must_use]
250 pub fn dot(self, rhs: Self) -> f32 {
251 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
252 }
253
254 #[inline]
256 #[must_use]
257 pub fn dot_into_vec(self, rhs: Self) -> Self {
258 Self::splat(self.dot(rhs))
259 }
260
261 #[inline]
263 #[must_use]
264 pub fn cross(self, rhs: Self) -> Self {
265 Self {
266 x: self.y * rhs.z - rhs.y * self.z,
267 y: self.z * rhs.x - rhs.z * self.x,
268 z: self.x * rhs.y - rhs.x * self.y,
269 }
270 }
271
272 #[inline]
279 #[must_use]
280 pub fn min(self, rhs: Self) -> Self {
281 Self {
282 x: if self.x < rhs.x { self.x } else { rhs.x },
283 y: if self.y < rhs.y { self.y } else { rhs.y },
284 z: if self.z < rhs.z { self.z } else { rhs.z },
285 }
286 }
287
288 #[inline]
295 #[must_use]
296 pub fn max(self, rhs: Self) -> Self {
297 Self {
298 x: if self.x > rhs.x { self.x } else { rhs.x },
299 y: if self.y > rhs.y { self.y } else { rhs.y },
300 z: if self.z > rhs.z { self.z } else { rhs.z },
301 }
302 }
303
304 #[inline]
315 #[must_use]
316 pub fn clamp(self, min: Self, max: Self) -> Self {
317 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
318 self.max(min).min(max)
319 }
320
321 #[inline]
328 #[must_use]
329 pub fn min_element(self) -> f32 {
330 let min = |a, b| if a < b { a } else { b };
331 min(self.x, min(self.y, self.z))
332 }
333
334 #[inline]
341 #[must_use]
342 pub fn max_element(self) -> f32 {
343 let max = |a, b| if a > b { a } else { b };
344 max(self.x, max(self.y, self.z))
345 }
346
347 #[doc(alias = "argmin")]
349 #[inline]
350 #[must_use]
351 pub fn min_position(self) -> usize {
352 let mut min = self.x;
353 let mut index = 0;
354 if self.y < min {
355 min = self.y;
356 index = 1;
357 }
358 if self.z < min {
359 index = 2;
360 }
361 index
362 }
363
364 #[doc(alias = "argmax")]
366 #[inline]
367 #[must_use]
368 pub fn max_position(self) -> usize {
369 let mut max = self.x;
370 let mut index = 0;
371 if self.y > max {
372 max = self.y;
373 index = 1;
374 }
375 if self.z > max {
376 index = 2;
377 }
378 index
379 }
380
381 #[inline]
385 #[must_use]
386 pub fn element_sum(self) -> f32 {
387 self.x + self.y + self.z
388 }
389
390 #[inline]
394 #[must_use]
395 pub fn element_product(self) -> f32 {
396 self.x * self.y * self.z
397 }
398
399 #[inline]
405 #[must_use]
406 pub fn cmpeq(self, rhs: Self) -> BVec3 {
407 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
408 }
409
410 #[inline]
416 #[must_use]
417 pub fn cmpne(self, rhs: Self) -> BVec3 {
418 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
419 }
420
421 #[inline]
427 #[must_use]
428 pub fn cmpge(self, rhs: Self) -> BVec3 {
429 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
430 }
431
432 #[inline]
438 #[must_use]
439 pub fn cmpgt(self, rhs: Self) -> BVec3 {
440 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
441 }
442
443 #[inline]
449 #[must_use]
450 pub fn cmple(self, rhs: Self) -> BVec3 {
451 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
452 }
453
454 #[inline]
460 #[must_use]
461 pub fn cmplt(self, rhs: Self) -> BVec3 {
462 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
463 }
464
465 #[inline]
467 #[must_use]
468 pub fn abs(self) -> Self {
469 Self {
470 x: math::abs(self.x),
471 y: math::abs(self.y),
472 z: math::abs(self.z),
473 }
474 }
475
476 #[inline]
482 #[must_use]
483 pub fn signum(self) -> Self {
484 Self {
485 x: math::signum(self.x),
486 y: math::signum(self.y),
487 z: math::signum(self.z),
488 }
489 }
490
491 #[inline]
493 #[must_use]
494 pub fn copysign(self, rhs: Self) -> Self {
495 Self {
496 x: math::copysign(self.x, rhs.x),
497 y: math::copysign(self.y, rhs.y),
498 z: math::copysign(self.z, rhs.z),
499 }
500 }
501
502 #[inline]
510 #[must_use]
511 pub fn is_negative_bitmask(self) -> u32 {
512 (self.x.is_sign_negative() as u32)
513 | ((self.y.is_sign_negative() as u32) << 1)
514 | ((self.z.is_sign_negative() as u32) << 2)
515 }
516
517 #[inline]
522 #[must_use]
523 pub fn is_negative_mask(self) -> BVec3 {
524 BVec3::new(
525 self.x.is_sign_negative(),
526 self.y.is_sign_negative(),
527 self.z.is_sign_negative(),
528 )
529 }
530
531 #[inline]
534 #[must_use]
535 pub fn is_finite(self) -> bool {
536 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
537 }
538
539 #[inline]
543 #[must_use]
544 pub fn is_finite_mask(self) -> BVec3 {
545 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
546 }
547
548 #[inline]
550 #[must_use]
551 pub fn is_nan(self) -> bool {
552 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
553 }
554
555 #[inline]
559 #[must_use]
560 pub fn is_nan_mask(self) -> BVec3 {
561 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
562 }
563
564 #[doc(alias = "magnitude")]
566 #[inline]
567 #[must_use]
568 pub fn length(self) -> f32 {
569 math::sqrt(self.dot(self))
570 }
571
572 #[doc(alias = "magnitude2")]
576 #[inline]
577 #[must_use]
578 pub fn length_squared(self) -> f32 {
579 self.dot(self)
580 }
581
582 #[inline]
586 #[must_use]
587 pub fn length_recip(self) -> f32 {
588 self.length().recip()
589 }
590
591 #[inline]
593 #[must_use]
594 pub fn distance(self, rhs: Self) -> f32 {
595 (self - rhs).length()
596 }
597
598 #[inline]
600 #[must_use]
601 pub fn distance_squared(self, rhs: Self) -> f32 {
602 (self - rhs).length_squared()
603 }
604
605 #[inline]
607 #[must_use]
608 pub fn div_euclid(self, rhs: Self) -> Self {
609 Self::new(
610 math::div_euclid(self.x, rhs.x),
611 math::div_euclid(self.y, rhs.y),
612 math::div_euclid(self.z, rhs.z),
613 )
614 }
615
616 #[inline]
620 #[must_use]
621 pub fn rem_euclid(self, rhs: Self) -> Self {
622 Self::new(
623 math::rem_euclid(self.x, rhs.x),
624 math::rem_euclid(self.y, rhs.y),
625 math::rem_euclid(self.z, rhs.z),
626 )
627 }
628
629 #[inline]
639 #[must_use]
640 pub fn normalize(self) -> Self {
641 #[allow(clippy::let_and_return)]
642 let normalized = self.mul(self.length_recip());
643 glam_assert!(normalized.is_finite());
644 normalized
645 }
646
647 #[inline]
654 #[must_use]
655 pub fn try_normalize(self) -> Option<Self> {
656 let rcp = self.length_recip();
657 if rcp.is_finite() && rcp > 0.0 {
658 Some(self * rcp)
659 } else {
660 None
661 }
662 }
663
664 #[inline]
672 #[must_use]
673 pub fn normalize_or(self, fallback: Self) -> Self {
674 let rcp = self.length_recip();
675 if rcp.is_finite() && rcp > 0.0 {
676 self * rcp
677 } else {
678 fallback
679 }
680 }
681
682 #[inline]
689 #[must_use]
690 pub fn normalize_or_zero(self) -> Self {
691 self.normalize_or(Self::ZERO)
692 }
693
694 #[inline]
698 #[must_use]
699 pub fn normalize_and_length(self) -> (Self, f32) {
700 let length = self.length();
701 let rcp = 1.0 / length;
702 if rcp.is_finite() && rcp > 0.0 {
703 (self * rcp, length)
704 } else {
705 (Self::X, 0.0)
706 }
707 }
708
709 #[inline]
713 #[must_use]
714 pub fn is_normalized(self) -> bool {
715 math::abs(self.length_squared() - 1.0) <= 2e-4
716 }
717
718 #[inline]
726 #[must_use]
727 pub fn project_onto(self, rhs: Self) -> Self {
728 let other_len_sq_rcp = rhs.dot(rhs).recip();
729 glam_assert!(other_len_sq_rcp.is_finite());
730 rhs * self.dot(rhs) * other_len_sq_rcp
731 }
732
733 #[doc(alias("plane"))]
744 #[inline]
745 #[must_use]
746 pub fn reject_from(self, rhs: Self) -> Self {
747 self - self.project_onto(rhs)
748 }
749
750 #[inline]
758 #[must_use]
759 pub fn project_onto_normalized(self, rhs: Self) -> Self {
760 glam_assert!(rhs.is_normalized());
761 rhs * self.dot(rhs)
762 }
763
764 #[doc(alias("plane"))]
775 #[inline]
776 #[must_use]
777 pub fn reject_from_normalized(self, rhs: Self) -> Self {
778 self - self.project_onto_normalized(rhs)
779 }
780
781 #[inline]
784 #[must_use]
785 pub fn round(self) -> Self {
786 Self {
787 x: math::round(self.x),
788 y: math::round(self.y),
789 z: math::round(self.z),
790 }
791 }
792
793 #[inline]
796 #[must_use]
797 pub fn floor(self) -> Self {
798 Self {
799 x: math::floor(self.x),
800 y: math::floor(self.y),
801 z: math::floor(self.z),
802 }
803 }
804
805 #[inline]
808 #[must_use]
809 pub fn ceil(self) -> Self {
810 Self {
811 x: math::ceil(self.x),
812 y: math::ceil(self.y),
813 z: math::ceil(self.z),
814 }
815 }
816
817 #[inline]
820 #[must_use]
821 pub fn trunc(self) -> Self {
822 Self {
823 x: math::trunc(self.x),
824 y: math::trunc(self.y),
825 z: math::trunc(self.z),
826 }
827 }
828
829 #[inline]
833 #[must_use]
834 pub fn step(self, rhs: Self) -> Self {
835 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
836 }
837
838 #[inline]
840 #[must_use]
841 pub fn saturate(self) -> Self {
842 self.clamp(Self::ZERO, Self::ONE)
843 }
844
845 #[inline]
852 #[must_use]
853 pub fn fract(self) -> Self {
854 self - self.trunc()
855 }
856
857 #[inline]
864 #[must_use]
865 pub fn fract_gl(self) -> Self {
866 self - self.floor()
867 }
868
869 #[inline]
872 #[must_use]
873 pub fn exp(self) -> Self {
874 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
875 }
876
877 #[inline]
879 #[must_use]
880 pub fn exp2(self) -> Self {
881 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
882 }
883
884 #[inline]
887 #[must_use]
888 pub fn ln(self) -> Self {
889 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
890 }
891
892 #[inline]
895 #[must_use]
896 pub fn log2(self) -> Self {
897 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
898 }
899
900 #[inline]
902 #[must_use]
903 pub fn powf(self, n: f32) -> Self {
904 Self::new(
905 math::powf(self.x, n),
906 math::powf(self.y, n),
907 math::powf(self.z, n),
908 )
909 }
910
911 #[inline]
914 #[must_use]
915 pub fn sqrt(self) -> Self {
916 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
917 }
918
919 #[inline]
921 #[must_use]
922 pub fn cos(self) -> Self {
923 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
924 }
925
926 #[inline]
928 #[must_use]
929 pub fn sin(self) -> Self {
930 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
931 }
932
933 #[inline]
935 #[must_use]
936 pub fn sin_cos(self) -> (Self, Self) {
937 let (sin_x, cos_x) = math::sin_cos(self.x);
938 let (sin_y, cos_y) = math::sin_cos(self.y);
939 let (sin_z, cos_z) = math::sin_cos(self.z);
940
941 (
942 Self::new(sin_x, sin_y, sin_z),
943 Self::new(cos_x, cos_y, cos_z),
944 )
945 }
946
947 #[inline]
949 #[must_use]
950 pub fn recip(self) -> Self {
951 Self {
952 x: 1.0 / self.x,
953 y: 1.0 / self.y,
954 z: 1.0 / self.z,
955 }
956 }
957
958 #[doc(alias = "mix")]
964 #[inline]
965 #[must_use]
966 pub fn lerp(self, rhs: Self, s: f32) -> Self {
967 self * (1.0 - s) + rhs * s
968 }
969
970 #[inline]
975 #[must_use]
976 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
977 let a = rhs - self;
978 let len = a.length();
979 if len <= d || len <= 1e-4 {
980 return rhs;
981 }
982 self + a / len * d
983 }
984
985 #[inline]
991 pub fn midpoint(self, rhs: Self) -> Self {
992 (self + rhs) * 0.5
993 }
994
995 #[inline]
1005 #[must_use]
1006 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1007 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1008 }
1009
1010 #[inline]
1016 #[must_use]
1017 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1018 glam_assert!(0.0 <= min);
1019 glam_assert!(min <= max);
1020 let length_sq = self.length_squared();
1021 if length_sq < min * min {
1022 min * (self / math::sqrt(length_sq))
1023 } else if length_sq > max * max {
1024 max * (self / math::sqrt(length_sq))
1025 } else {
1026 self
1027 }
1028 }
1029
1030 #[inline]
1036 #[must_use]
1037 pub fn clamp_length_max(self, max: f32) -> Self {
1038 glam_assert!(0.0 <= max);
1039 let length_sq = self.length_squared();
1040 if length_sq > max * max {
1041 max * (self / math::sqrt(length_sq))
1042 } else {
1043 self
1044 }
1045 }
1046
1047 #[inline]
1053 #[must_use]
1054 pub fn clamp_length_min(self, min: f32) -> Self {
1055 glam_assert!(0.0 <= min);
1056 let length_sq = self.length_squared();
1057 if length_sq < min * min {
1058 min * (self / math::sqrt(length_sq))
1059 } else {
1060 self
1061 }
1062 }
1063
1064 #[inline]
1072 #[must_use]
1073 pub fn mul_add(self, a: Self, b: Self) -> Self {
1074 Self::new(
1075 math::mul_add(self.x, a.x, b.x),
1076 math::mul_add(self.y, a.y, b.y),
1077 math::mul_add(self.z, a.z, b.z),
1078 )
1079 }
1080
1081 #[inline]
1090 #[must_use]
1091 pub fn reflect(self, normal: Self) -> Self {
1092 glam_assert!(normal.is_normalized());
1093 self - 2.0 * self.dot(normal) * normal
1094 }
1095
1096 #[inline]
1106 #[must_use]
1107 pub fn refract(self, normal: Self, eta: f32) -> Self {
1108 glam_assert!(self.is_normalized());
1109 glam_assert!(normal.is_normalized());
1110 let n_dot_i = normal.dot(self);
1111 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1112 if k >= 0.0 {
1113 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1114 } else {
1115 Self::ZERO
1116 }
1117 }
1118
1119 #[inline]
1123 #[must_use]
1124 pub fn angle_between(self, rhs: Self) -> f32 {
1125 math::acos_approx(
1126 self.dot(rhs)
1127 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1128 )
1129 }
1130
1131 #[inline]
1133 #[must_use]
1134 pub fn rotate_x(self, angle: f32) -> Self {
1135 let (sina, cosa) = math::sin_cos(angle);
1136 Self::new(
1137 self.x,
1138 self.y * cosa - self.z * sina,
1139 self.y * sina + self.z * cosa,
1140 )
1141 }
1142
1143 #[inline]
1145 #[must_use]
1146 pub fn rotate_y(self, angle: f32) -> Self {
1147 let (sina, cosa) = math::sin_cos(angle);
1148 Self::new(
1149 self.x * cosa + self.z * sina,
1150 self.y,
1151 self.x * -sina + self.z * cosa,
1152 )
1153 }
1154
1155 #[inline]
1157 #[must_use]
1158 pub fn rotate_z(self, angle: f32) -> Self {
1159 let (sina, cosa) = math::sin_cos(angle);
1160 Self::new(
1161 self.x * cosa - self.y * sina,
1162 self.x * sina + self.y * cosa,
1163 self.z,
1164 )
1165 }
1166
1167 #[inline]
1175 #[must_use]
1176 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1177 Quat::from_axis_angle(axis, angle) * self
1178 }
1179
1180 #[inline]
1186 #[must_use]
1187 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1188 let angle_between = self.angle_between(rhs);
1189 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1191 let axis = self
1192 .cross(rhs)
1193 .try_normalize()
1194 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1195 Quat::from_axis_angle(axis, angle) * self
1196 }
1197
1198 #[inline]
1205 #[must_use]
1206 pub fn any_orthogonal_vector(self) -> Self {
1207 if math::abs(self.x) > math::abs(self.y) {
1209 Self::new(-self.z, 0.0, self.x) } else {
1211 Self::new(0.0, self.z, -self.y) }
1213 }
1214
1215 #[inline]
1223 #[must_use]
1224 pub fn any_orthonormal_vector(self) -> Self {
1225 glam_assert!(self.is_normalized());
1226 let sign = math::signum(self.z);
1228 let a = -1.0 / (sign + self.z);
1229 let b = self.x * self.y * a;
1230 Self::new(b, sign + self.y * self.y * a, -self.y)
1231 }
1232
1233 #[inline]
1240 #[must_use]
1241 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1242 glam_assert!(self.is_normalized());
1243 let sign = math::signum(self.z);
1245 let a = -1.0 / (sign + self.z);
1246 let b = self.x * self.y * a;
1247 (
1248 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1249 Self::new(b, sign + self.y * self.y * a, -self.y),
1250 )
1251 }
1252
1253 #[inline]
1259 #[must_use]
1260 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1261 let self_length = self.length();
1262 let rhs_length = rhs.length();
1263 let dot = self.dot(rhs) / (self_length * rhs_length);
1265 if math::abs(dot) < 1.0 - 3e-7 {
1267 let theta = math::acos_approx(dot);
1269 let sin_theta = math::sin(theta);
1271 let t1 = math::sin(theta * (1. - s));
1272 let t2 = math::sin(theta * s);
1273
1274 let result_length = self_length.lerp(rhs_length, s);
1276 return (self * (result_length / self_length) * t1
1278 + rhs * (result_length / rhs_length) * t2)
1279 * sin_theta.recip();
1280 }
1281 if dot < 0.0 {
1282 let axis = self.any_orthogonal_vector().normalize();
1286 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1287 let result_length = self_length.lerp(rhs_length, s);
1289 rotation * self * (result_length / self_length)
1290 } else {
1291 self.lerp(rhs, s)
1293 }
1294 }
1295
1296 #[cfg(feature = "f64")]
1298 #[inline]
1299 #[must_use]
1300 pub fn as_dvec3(self) -> crate::DVec3 {
1301 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1302 }
1303
1304 #[cfg(feature = "i8")]
1306 #[inline]
1307 #[must_use]
1308 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1309 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1310 }
1311
1312 #[cfg(feature = "u8")]
1314 #[inline]
1315 #[must_use]
1316 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1317 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1318 }
1319
1320 #[cfg(feature = "i16")]
1322 #[inline]
1323 #[must_use]
1324 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1325 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1326 }
1327
1328 #[cfg(feature = "u16")]
1330 #[inline]
1331 #[must_use]
1332 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1333 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1334 }
1335
1336 #[cfg(feature = "i32")]
1338 #[inline]
1339 #[must_use]
1340 pub fn as_ivec3(self) -> crate::IVec3 {
1341 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1342 }
1343
1344 #[cfg(feature = "u32")]
1346 #[inline]
1347 #[must_use]
1348 pub fn as_uvec3(self) -> crate::UVec3 {
1349 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1350 }
1351
1352 #[cfg(feature = "i64")]
1354 #[inline]
1355 #[must_use]
1356 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1357 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1358 }
1359
1360 #[cfg(feature = "u64")]
1362 #[inline]
1363 #[must_use]
1364 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1365 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1366 }
1367
1368 #[cfg(feature = "isize")]
1370 #[inline]
1371 #[must_use]
1372 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1373 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1374 }
1375
1376 #[cfg(feature = "usize")]
1378 #[inline]
1379 #[must_use]
1380 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1381 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1382 }
1383}
1384
1385impl Default for Vec3 {
1386 #[inline(always)]
1387 fn default() -> Self {
1388 Self::ZERO
1389 }
1390}
1391
1392impl Div for Vec3 {
1393 type Output = Self;
1394 #[inline]
1395 fn div(self, rhs: Self) -> Self {
1396 Self {
1397 x: self.x.div(rhs.x),
1398 y: self.y.div(rhs.y),
1399 z: self.z.div(rhs.z),
1400 }
1401 }
1402}
1403
1404impl Div<&Self> for Vec3 {
1405 type Output = Self;
1406 #[inline]
1407 fn div(self, rhs: &Self) -> Self {
1408 self.div(*rhs)
1409 }
1410}
1411
1412impl Div<&Vec3> for &Vec3 {
1413 type Output = Vec3;
1414 #[inline]
1415 fn div(self, rhs: &Vec3) -> Vec3 {
1416 (*self).div(*rhs)
1417 }
1418}
1419
1420impl Div<Vec3> for &Vec3 {
1421 type Output = Vec3;
1422 #[inline]
1423 fn div(self, rhs: Vec3) -> Vec3 {
1424 (*self).div(rhs)
1425 }
1426}
1427
1428impl DivAssign for Vec3 {
1429 #[inline]
1430 fn div_assign(&mut self, rhs: Self) {
1431 self.x.div_assign(rhs.x);
1432 self.y.div_assign(rhs.y);
1433 self.z.div_assign(rhs.z);
1434 }
1435}
1436
1437impl DivAssign<&Self> for Vec3 {
1438 #[inline]
1439 fn div_assign(&mut self, rhs: &Self) {
1440 self.div_assign(*rhs);
1441 }
1442}
1443
1444impl Div<f32> for Vec3 {
1445 type Output = Self;
1446 #[inline]
1447 fn div(self, rhs: f32) -> Self {
1448 Self {
1449 x: self.x.div(rhs),
1450 y: self.y.div(rhs),
1451 z: self.z.div(rhs),
1452 }
1453 }
1454}
1455
1456impl Div<&f32> for Vec3 {
1457 type Output = Self;
1458 #[inline]
1459 fn div(self, rhs: &f32) -> Self {
1460 self.div(*rhs)
1461 }
1462}
1463
1464impl Div<&f32> for &Vec3 {
1465 type Output = Vec3;
1466 #[inline]
1467 fn div(self, rhs: &f32) -> Vec3 {
1468 (*self).div(*rhs)
1469 }
1470}
1471
1472impl Div<f32> for &Vec3 {
1473 type Output = Vec3;
1474 #[inline]
1475 fn div(self, rhs: f32) -> Vec3 {
1476 (*self).div(rhs)
1477 }
1478}
1479
1480impl DivAssign<f32> for Vec3 {
1481 #[inline]
1482 fn div_assign(&mut self, rhs: f32) {
1483 self.x.div_assign(rhs);
1484 self.y.div_assign(rhs);
1485 self.z.div_assign(rhs);
1486 }
1487}
1488
1489impl DivAssign<&f32> for Vec3 {
1490 #[inline]
1491 fn div_assign(&mut self, rhs: &f32) {
1492 self.div_assign(*rhs);
1493 }
1494}
1495
1496impl Div<Vec3> for f32 {
1497 type Output = Vec3;
1498 #[inline]
1499 fn div(self, rhs: Vec3) -> Vec3 {
1500 Vec3 {
1501 x: self.div(rhs.x),
1502 y: self.div(rhs.y),
1503 z: self.div(rhs.z),
1504 }
1505 }
1506}
1507
1508impl Div<&Vec3> for f32 {
1509 type Output = Vec3;
1510 #[inline]
1511 fn div(self, rhs: &Vec3) -> Vec3 {
1512 self.div(*rhs)
1513 }
1514}
1515
1516impl Div<&Vec3> for &f32 {
1517 type Output = Vec3;
1518 #[inline]
1519 fn div(self, rhs: &Vec3) -> Vec3 {
1520 (*self).div(*rhs)
1521 }
1522}
1523
1524impl Div<Vec3> for &f32 {
1525 type Output = Vec3;
1526 #[inline]
1527 fn div(self, rhs: Vec3) -> Vec3 {
1528 (*self).div(rhs)
1529 }
1530}
1531
1532impl Mul for Vec3 {
1533 type Output = Self;
1534 #[inline]
1535 fn mul(self, rhs: Self) -> Self {
1536 Self {
1537 x: self.x.mul(rhs.x),
1538 y: self.y.mul(rhs.y),
1539 z: self.z.mul(rhs.z),
1540 }
1541 }
1542}
1543
1544impl Mul<&Self> for Vec3 {
1545 type Output = Self;
1546 #[inline]
1547 fn mul(self, rhs: &Self) -> Self {
1548 self.mul(*rhs)
1549 }
1550}
1551
1552impl Mul<&Vec3> for &Vec3 {
1553 type Output = Vec3;
1554 #[inline]
1555 fn mul(self, rhs: &Vec3) -> Vec3 {
1556 (*self).mul(*rhs)
1557 }
1558}
1559
1560impl Mul<Vec3> for &Vec3 {
1561 type Output = Vec3;
1562 #[inline]
1563 fn mul(self, rhs: Vec3) -> Vec3 {
1564 (*self).mul(rhs)
1565 }
1566}
1567
1568impl MulAssign for Vec3 {
1569 #[inline]
1570 fn mul_assign(&mut self, rhs: Self) {
1571 self.x.mul_assign(rhs.x);
1572 self.y.mul_assign(rhs.y);
1573 self.z.mul_assign(rhs.z);
1574 }
1575}
1576
1577impl MulAssign<&Self> for Vec3 {
1578 #[inline]
1579 fn mul_assign(&mut self, rhs: &Self) {
1580 self.mul_assign(*rhs);
1581 }
1582}
1583
1584impl Mul<f32> for Vec3 {
1585 type Output = Self;
1586 #[inline]
1587 fn mul(self, rhs: f32) -> Self {
1588 Self {
1589 x: self.x.mul(rhs),
1590 y: self.y.mul(rhs),
1591 z: self.z.mul(rhs),
1592 }
1593 }
1594}
1595
1596impl Mul<&f32> for Vec3 {
1597 type Output = Self;
1598 #[inline]
1599 fn mul(self, rhs: &f32) -> Self {
1600 self.mul(*rhs)
1601 }
1602}
1603
1604impl Mul<&f32> for &Vec3 {
1605 type Output = Vec3;
1606 #[inline]
1607 fn mul(self, rhs: &f32) -> Vec3 {
1608 (*self).mul(*rhs)
1609 }
1610}
1611
1612impl Mul<f32> for &Vec3 {
1613 type Output = Vec3;
1614 #[inline]
1615 fn mul(self, rhs: f32) -> Vec3 {
1616 (*self).mul(rhs)
1617 }
1618}
1619
1620impl MulAssign<f32> for Vec3 {
1621 #[inline]
1622 fn mul_assign(&mut self, rhs: f32) {
1623 self.x.mul_assign(rhs);
1624 self.y.mul_assign(rhs);
1625 self.z.mul_assign(rhs);
1626 }
1627}
1628
1629impl MulAssign<&f32> for Vec3 {
1630 #[inline]
1631 fn mul_assign(&mut self, rhs: &f32) {
1632 self.mul_assign(*rhs);
1633 }
1634}
1635
1636impl Mul<Vec3> for f32 {
1637 type Output = Vec3;
1638 #[inline]
1639 fn mul(self, rhs: Vec3) -> Vec3 {
1640 Vec3 {
1641 x: self.mul(rhs.x),
1642 y: self.mul(rhs.y),
1643 z: self.mul(rhs.z),
1644 }
1645 }
1646}
1647
1648impl Mul<&Vec3> for f32 {
1649 type Output = Vec3;
1650 #[inline]
1651 fn mul(self, rhs: &Vec3) -> Vec3 {
1652 self.mul(*rhs)
1653 }
1654}
1655
1656impl Mul<&Vec3> for &f32 {
1657 type Output = Vec3;
1658 #[inline]
1659 fn mul(self, rhs: &Vec3) -> Vec3 {
1660 (*self).mul(*rhs)
1661 }
1662}
1663
1664impl Mul<Vec3> for &f32 {
1665 type Output = Vec3;
1666 #[inline]
1667 fn mul(self, rhs: Vec3) -> Vec3 {
1668 (*self).mul(rhs)
1669 }
1670}
1671
1672impl Add for Vec3 {
1673 type Output = Self;
1674 #[inline]
1675 fn add(self, rhs: Self) -> Self {
1676 Self {
1677 x: self.x.add(rhs.x),
1678 y: self.y.add(rhs.y),
1679 z: self.z.add(rhs.z),
1680 }
1681 }
1682}
1683
1684impl Add<&Self> for Vec3 {
1685 type Output = Self;
1686 #[inline]
1687 fn add(self, rhs: &Self) -> Self {
1688 self.add(*rhs)
1689 }
1690}
1691
1692impl Add<&Vec3> for &Vec3 {
1693 type Output = Vec3;
1694 #[inline]
1695 fn add(self, rhs: &Vec3) -> Vec3 {
1696 (*self).add(*rhs)
1697 }
1698}
1699
1700impl Add<Vec3> for &Vec3 {
1701 type Output = Vec3;
1702 #[inline]
1703 fn add(self, rhs: Vec3) -> Vec3 {
1704 (*self).add(rhs)
1705 }
1706}
1707
1708impl AddAssign for Vec3 {
1709 #[inline]
1710 fn add_assign(&mut self, rhs: Self) {
1711 self.x.add_assign(rhs.x);
1712 self.y.add_assign(rhs.y);
1713 self.z.add_assign(rhs.z);
1714 }
1715}
1716
1717impl AddAssign<&Self> for Vec3 {
1718 #[inline]
1719 fn add_assign(&mut self, rhs: &Self) {
1720 self.add_assign(*rhs);
1721 }
1722}
1723
1724impl Add<f32> for Vec3 {
1725 type Output = Self;
1726 #[inline]
1727 fn add(self, rhs: f32) -> Self {
1728 Self {
1729 x: self.x.add(rhs),
1730 y: self.y.add(rhs),
1731 z: self.z.add(rhs),
1732 }
1733 }
1734}
1735
1736impl Add<&f32> for Vec3 {
1737 type Output = Self;
1738 #[inline]
1739 fn add(self, rhs: &f32) -> Self {
1740 self.add(*rhs)
1741 }
1742}
1743
1744impl Add<&f32> for &Vec3 {
1745 type Output = Vec3;
1746 #[inline]
1747 fn add(self, rhs: &f32) -> Vec3 {
1748 (*self).add(*rhs)
1749 }
1750}
1751
1752impl Add<f32> for &Vec3 {
1753 type Output = Vec3;
1754 #[inline]
1755 fn add(self, rhs: f32) -> Vec3 {
1756 (*self).add(rhs)
1757 }
1758}
1759
1760impl AddAssign<f32> for Vec3 {
1761 #[inline]
1762 fn add_assign(&mut self, rhs: f32) {
1763 self.x.add_assign(rhs);
1764 self.y.add_assign(rhs);
1765 self.z.add_assign(rhs);
1766 }
1767}
1768
1769impl AddAssign<&f32> for Vec3 {
1770 #[inline]
1771 fn add_assign(&mut self, rhs: &f32) {
1772 self.add_assign(*rhs);
1773 }
1774}
1775
1776impl Add<Vec3> for f32 {
1777 type Output = Vec3;
1778 #[inline]
1779 fn add(self, rhs: Vec3) -> Vec3 {
1780 Vec3 {
1781 x: self.add(rhs.x),
1782 y: self.add(rhs.y),
1783 z: self.add(rhs.z),
1784 }
1785 }
1786}
1787
1788impl Add<&Vec3> for f32 {
1789 type Output = Vec3;
1790 #[inline]
1791 fn add(self, rhs: &Vec3) -> Vec3 {
1792 self.add(*rhs)
1793 }
1794}
1795
1796impl Add<&Vec3> for &f32 {
1797 type Output = Vec3;
1798 #[inline]
1799 fn add(self, rhs: &Vec3) -> Vec3 {
1800 (*self).add(*rhs)
1801 }
1802}
1803
1804impl Add<Vec3> for &f32 {
1805 type Output = Vec3;
1806 #[inline]
1807 fn add(self, rhs: Vec3) -> Vec3 {
1808 (*self).add(rhs)
1809 }
1810}
1811
1812impl Sub for Vec3 {
1813 type Output = Self;
1814 #[inline]
1815 fn sub(self, rhs: Self) -> Self {
1816 Self {
1817 x: self.x.sub(rhs.x),
1818 y: self.y.sub(rhs.y),
1819 z: self.z.sub(rhs.z),
1820 }
1821 }
1822}
1823
1824impl Sub<&Self> for Vec3 {
1825 type Output = Self;
1826 #[inline]
1827 fn sub(self, rhs: &Self) -> Self {
1828 self.sub(*rhs)
1829 }
1830}
1831
1832impl Sub<&Vec3> for &Vec3 {
1833 type Output = Vec3;
1834 #[inline]
1835 fn sub(self, rhs: &Vec3) -> Vec3 {
1836 (*self).sub(*rhs)
1837 }
1838}
1839
1840impl Sub<Vec3> for &Vec3 {
1841 type Output = Vec3;
1842 #[inline]
1843 fn sub(self, rhs: Vec3) -> Vec3 {
1844 (*self).sub(rhs)
1845 }
1846}
1847
1848impl SubAssign for Vec3 {
1849 #[inline]
1850 fn sub_assign(&mut self, rhs: Self) {
1851 self.x.sub_assign(rhs.x);
1852 self.y.sub_assign(rhs.y);
1853 self.z.sub_assign(rhs.z);
1854 }
1855}
1856
1857impl SubAssign<&Self> for Vec3 {
1858 #[inline]
1859 fn sub_assign(&mut self, rhs: &Self) {
1860 self.sub_assign(*rhs);
1861 }
1862}
1863
1864impl Sub<f32> for Vec3 {
1865 type Output = Self;
1866 #[inline]
1867 fn sub(self, rhs: f32) -> Self {
1868 Self {
1869 x: self.x.sub(rhs),
1870 y: self.y.sub(rhs),
1871 z: self.z.sub(rhs),
1872 }
1873 }
1874}
1875
1876impl Sub<&f32> for Vec3 {
1877 type Output = Self;
1878 #[inline]
1879 fn sub(self, rhs: &f32) -> Self {
1880 self.sub(*rhs)
1881 }
1882}
1883
1884impl Sub<&f32> for &Vec3 {
1885 type Output = Vec3;
1886 #[inline]
1887 fn sub(self, rhs: &f32) -> Vec3 {
1888 (*self).sub(*rhs)
1889 }
1890}
1891
1892impl Sub<f32> for &Vec3 {
1893 type Output = Vec3;
1894 #[inline]
1895 fn sub(self, rhs: f32) -> Vec3 {
1896 (*self).sub(rhs)
1897 }
1898}
1899
1900impl SubAssign<f32> for Vec3 {
1901 #[inline]
1902 fn sub_assign(&mut self, rhs: f32) {
1903 self.x.sub_assign(rhs);
1904 self.y.sub_assign(rhs);
1905 self.z.sub_assign(rhs);
1906 }
1907}
1908
1909impl SubAssign<&f32> for Vec3 {
1910 #[inline]
1911 fn sub_assign(&mut self, rhs: &f32) {
1912 self.sub_assign(*rhs);
1913 }
1914}
1915
1916impl Sub<Vec3> for f32 {
1917 type Output = Vec3;
1918 #[inline]
1919 fn sub(self, rhs: Vec3) -> Vec3 {
1920 Vec3 {
1921 x: self.sub(rhs.x),
1922 y: self.sub(rhs.y),
1923 z: self.sub(rhs.z),
1924 }
1925 }
1926}
1927
1928impl Sub<&Vec3> for f32 {
1929 type Output = Vec3;
1930 #[inline]
1931 fn sub(self, rhs: &Vec3) -> Vec3 {
1932 self.sub(*rhs)
1933 }
1934}
1935
1936impl Sub<&Vec3> for &f32 {
1937 type Output = Vec3;
1938 #[inline]
1939 fn sub(self, rhs: &Vec3) -> Vec3 {
1940 (*self).sub(*rhs)
1941 }
1942}
1943
1944impl Sub<Vec3> for &f32 {
1945 type Output = Vec3;
1946 #[inline]
1947 fn sub(self, rhs: Vec3) -> Vec3 {
1948 (*self).sub(rhs)
1949 }
1950}
1951
1952impl Rem for Vec3 {
1953 type Output = Self;
1954 #[inline]
1955 fn rem(self, rhs: Self) -> Self {
1956 Self {
1957 x: self.x.rem(rhs.x),
1958 y: self.y.rem(rhs.y),
1959 z: self.z.rem(rhs.z),
1960 }
1961 }
1962}
1963
1964impl Rem<&Self> for Vec3 {
1965 type Output = Self;
1966 #[inline]
1967 fn rem(self, rhs: &Self) -> Self {
1968 self.rem(*rhs)
1969 }
1970}
1971
1972impl Rem<&Vec3> for &Vec3 {
1973 type Output = Vec3;
1974 #[inline]
1975 fn rem(self, rhs: &Vec3) -> Vec3 {
1976 (*self).rem(*rhs)
1977 }
1978}
1979
1980impl Rem<Vec3> for &Vec3 {
1981 type Output = Vec3;
1982 #[inline]
1983 fn rem(self, rhs: Vec3) -> Vec3 {
1984 (*self).rem(rhs)
1985 }
1986}
1987
1988impl RemAssign for Vec3 {
1989 #[inline]
1990 fn rem_assign(&mut self, rhs: Self) {
1991 self.x.rem_assign(rhs.x);
1992 self.y.rem_assign(rhs.y);
1993 self.z.rem_assign(rhs.z);
1994 }
1995}
1996
1997impl RemAssign<&Self> for Vec3 {
1998 #[inline]
1999 fn rem_assign(&mut self, rhs: &Self) {
2000 self.rem_assign(*rhs);
2001 }
2002}
2003
2004impl Rem<f32> for Vec3 {
2005 type Output = Self;
2006 #[inline]
2007 fn rem(self, rhs: f32) -> Self {
2008 Self {
2009 x: self.x.rem(rhs),
2010 y: self.y.rem(rhs),
2011 z: self.z.rem(rhs),
2012 }
2013 }
2014}
2015
2016impl Rem<&f32> for Vec3 {
2017 type Output = Self;
2018 #[inline]
2019 fn rem(self, rhs: &f32) -> Self {
2020 self.rem(*rhs)
2021 }
2022}
2023
2024impl Rem<&f32> for &Vec3 {
2025 type Output = Vec3;
2026 #[inline]
2027 fn rem(self, rhs: &f32) -> Vec3 {
2028 (*self).rem(*rhs)
2029 }
2030}
2031
2032impl Rem<f32> for &Vec3 {
2033 type Output = Vec3;
2034 #[inline]
2035 fn rem(self, rhs: f32) -> Vec3 {
2036 (*self).rem(rhs)
2037 }
2038}
2039
2040impl RemAssign<f32> for Vec3 {
2041 #[inline]
2042 fn rem_assign(&mut self, rhs: f32) {
2043 self.x.rem_assign(rhs);
2044 self.y.rem_assign(rhs);
2045 self.z.rem_assign(rhs);
2046 }
2047}
2048
2049impl RemAssign<&f32> for Vec3 {
2050 #[inline]
2051 fn rem_assign(&mut self, rhs: &f32) {
2052 self.rem_assign(*rhs);
2053 }
2054}
2055
2056impl Rem<Vec3> for f32 {
2057 type Output = Vec3;
2058 #[inline]
2059 fn rem(self, rhs: Vec3) -> Vec3 {
2060 Vec3 {
2061 x: self.rem(rhs.x),
2062 y: self.rem(rhs.y),
2063 z: self.rem(rhs.z),
2064 }
2065 }
2066}
2067
2068impl Rem<&Vec3> for f32 {
2069 type Output = Vec3;
2070 #[inline]
2071 fn rem(self, rhs: &Vec3) -> Vec3 {
2072 self.rem(*rhs)
2073 }
2074}
2075
2076impl Rem<&Vec3> for &f32 {
2077 type Output = Vec3;
2078 #[inline]
2079 fn rem(self, rhs: &Vec3) -> Vec3 {
2080 (*self).rem(*rhs)
2081 }
2082}
2083
2084impl Rem<Vec3> for &f32 {
2085 type Output = Vec3;
2086 #[inline]
2087 fn rem(self, rhs: Vec3) -> Vec3 {
2088 (*self).rem(rhs)
2089 }
2090}
2091
2092impl AsRef<[f32; 3]> for Vec3 {
2093 #[inline]
2094 fn as_ref(&self) -> &[f32; 3] {
2095 unsafe { &*(self as *const Self as *const [f32; 3]) }
2096 }
2097}
2098
2099impl AsMut<[f32; 3]> for Vec3 {
2100 #[inline]
2101 fn as_mut(&mut self) -> &mut [f32; 3] {
2102 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2103 }
2104}
2105
2106impl Sum for Vec3 {
2107 #[inline]
2108 fn sum<I>(iter: I) -> Self
2109 where
2110 I: Iterator<Item = Self>,
2111 {
2112 iter.fold(Self::ZERO, Self::add)
2113 }
2114}
2115
2116impl<'a> Sum<&'a Self> for Vec3 {
2117 #[inline]
2118 fn sum<I>(iter: I) -> Self
2119 where
2120 I: Iterator<Item = &'a Self>,
2121 {
2122 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2123 }
2124}
2125
2126impl Product for Vec3 {
2127 #[inline]
2128 fn product<I>(iter: I) -> Self
2129 where
2130 I: Iterator<Item = Self>,
2131 {
2132 iter.fold(Self::ONE, Self::mul)
2133 }
2134}
2135
2136impl<'a> Product<&'a Self> for Vec3 {
2137 #[inline]
2138 fn product<I>(iter: I) -> Self
2139 where
2140 I: Iterator<Item = &'a Self>,
2141 {
2142 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2143 }
2144}
2145
2146impl Neg for Vec3 {
2147 type Output = Self;
2148 #[inline]
2149 fn neg(self) -> Self {
2150 Self {
2151 x: self.x.neg(),
2152 y: self.y.neg(),
2153 z: self.z.neg(),
2154 }
2155 }
2156}
2157
2158impl Neg for &Vec3 {
2159 type Output = Vec3;
2160 #[inline]
2161 fn neg(self) -> Vec3 {
2162 (*self).neg()
2163 }
2164}
2165
2166impl Index<usize> for Vec3 {
2167 type Output = f32;
2168 #[inline]
2169 fn index(&self, index: usize) -> &Self::Output {
2170 match index {
2171 0 => &self.x,
2172 1 => &self.y,
2173 2 => &self.z,
2174 _ => panic!("index out of bounds"),
2175 }
2176 }
2177}
2178
2179impl IndexMut<usize> for Vec3 {
2180 #[inline]
2181 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2182 match index {
2183 0 => &mut self.x,
2184 1 => &mut self.y,
2185 2 => &mut self.z,
2186 _ => panic!("index out of bounds"),
2187 }
2188 }
2189}
2190
2191impl fmt::Display for Vec3 {
2192 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2193 if let Some(p) = f.precision() {
2194 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2195 } else {
2196 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2197 }
2198 }
2199}
2200
2201impl fmt::Debug for Vec3 {
2202 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2203 fmt.debug_tuple(stringify!(Vec3))
2204 .field(&self.x)
2205 .field(&self.y)
2206 .field(&self.z)
2207 .finish()
2208 }
2209}
2210
2211impl From<[f32; 3]> for Vec3 {
2212 #[inline]
2213 fn from(a: [f32; 3]) -> Self {
2214 Self::new(a[0], a[1], a[2])
2215 }
2216}
2217
2218impl From<Vec3> for [f32; 3] {
2219 #[inline]
2220 fn from(v: Vec3) -> Self {
2221 [v.x, v.y, v.z]
2222 }
2223}
2224
2225impl From<(f32, f32, f32)> for Vec3 {
2226 #[inline]
2227 fn from(t: (f32, f32, f32)) -> Self {
2228 Self::new(t.0, t.1, t.2)
2229 }
2230}
2231
2232impl From<Vec3> for (f32, f32, f32) {
2233 #[inline]
2234 fn from(v: Vec3) -> Self {
2235 (v.x, v.y, v.z)
2236 }
2237}
2238
2239impl From<(Vec2, f32)> for Vec3 {
2240 #[inline]
2241 fn from((v, z): (Vec2, f32)) -> Self {
2242 Self::new(v.x, v.y, z)
2243 }
2244}
2245
2246impl From<BVec3> for Vec3 {
2247 #[inline]
2248 fn from(v: BVec3) -> Self {
2249 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2250 }
2251}
2252
2253impl From<BVec3A> for Vec3 {
2254 #[inline]
2255 fn from(v: BVec3A) -> Self {
2256 let bool_array: [bool; 3] = v.into();
2257 Self::new(
2258 f32::from(bool_array[0]),
2259 f32::from(bool_array[1]),
2260 f32::from(bool_array[2]),
2261 )
2262 }
2263}