1use crate::{f32::math, BVec3, BVec3A, Vec2, Vec4};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[inline(always)]
11#[must_use]
12pub const fn vec3(x: f32, y: f32, z: f32) -> Vec3 {
13 Vec3::new(x, y, z)
14}
15
16#[derive(Clone, Copy, PartialEq)]
18#[cfg_attr(not(target_arch = "spirv"), repr(C))]
19#[cfg_attr(target_arch = "spirv", repr(simd))]
20pub struct Vec3 {
21 pub x: f32,
22 pub y: f32,
23 pub z: f32,
24}
25
26impl Vec3 {
27 pub const ZERO: Self = Self::splat(0.0);
29
30 pub const ONE: Self = Self::splat(1.0);
32
33 pub const NEG_ONE: Self = Self::splat(-1.0);
35
36 pub const MIN: Self = Self::splat(f32::MIN);
38
39 pub const MAX: Self = Self::splat(f32::MAX);
41
42 pub const NAN: Self = Self::splat(f32::NAN);
44
45 pub const INFINITY: Self = Self::splat(f32::INFINITY);
47
48 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
50
51 pub const X: Self = Self::new(1.0, 0.0, 0.0);
53
54 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
56
57 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
59
60 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
62
63 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
65
66 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
68
69 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
71
72 #[inline(always)]
74 #[must_use]
75 pub const fn new(x: f32, y: f32, z: f32) -> Self {
76 Self { x, y, z }
77 }
78
79 #[inline]
81 #[must_use]
82 pub const fn splat(v: f32) -> Self {
83 Self { x: v, y: v, z: v }
84 }
85
86 #[inline]
88 #[must_use]
89 pub fn map<F>(self, f: F) -> Self
90 where
91 F: Fn(f32) -> f32,
92 {
93 Self::new(f(self.x), f(self.y), f(self.z))
94 }
95
96 #[inline]
102 #[must_use]
103 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
104 Self {
105 x: if mask.test(0) { if_true.x } else { if_false.x },
106 y: if mask.test(1) { if_true.y } else { if_false.y },
107 z: if mask.test(2) { if_true.z } else { if_false.z },
108 }
109 }
110
111 #[inline]
113 #[must_use]
114 pub const fn from_array(a: [f32; 3]) -> Self {
115 Self::new(a[0], a[1], a[2])
116 }
117
118 #[inline]
120 #[must_use]
121 pub const fn to_array(&self) -> [f32; 3] {
122 [self.x, self.y, self.z]
123 }
124
125 #[inline]
131 #[must_use]
132 pub const fn from_slice(slice: &[f32]) -> Self {
133 assert!(slice.len() >= 3);
134 Self::new(slice[0], slice[1], slice[2])
135 }
136
137 #[inline]
143 pub fn write_to_slice(self, slice: &mut [f32]) {
144 slice[..3].copy_from_slice(&self.to_array());
145 }
146
147 #[allow(dead_code)]
149 #[inline]
150 #[must_use]
151 pub(crate) fn from_vec4(v: Vec4) -> Self {
152 Self {
153 x: v.x,
154 y: v.y,
155 z: v.z,
156 }
157 }
158
159 #[inline]
161 #[must_use]
162 pub fn extend(self, w: f32) -> Vec4 {
163 Vec4::new(self.x, self.y, self.z, w)
164 }
165
166 #[inline]
170 #[must_use]
171 pub fn truncate(self) -> Vec2 {
172 use crate::swizzles::Vec3Swizzles;
173 self.xy()
174 }
175
176 #[inline]
178 #[must_use]
179 pub fn with_x(mut self, x: f32) -> Self {
180 self.x = x;
181 self
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn with_y(mut self, y: f32) -> Self {
188 self.y = y;
189 self
190 }
191
192 #[inline]
194 #[must_use]
195 pub fn with_z(mut self, z: f32) -> Self {
196 self.z = z;
197 self
198 }
199
200 #[inline]
202 #[must_use]
203 pub fn dot(self, rhs: Self) -> f32 {
204 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
205 }
206
207 #[inline]
209 #[must_use]
210 pub fn dot_into_vec(self, rhs: Self) -> Self {
211 Self::splat(self.dot(rhs))
212 }
213
214 #[inline]
216 #[must_use]
217 pub fn cross(self, rhs: Self) -> Self {
218 Self {
219 x: self.y * rhs.z - rhs.y * self.z,
220 y: self.z * rhs.x - rhs.z * self.x,
221 z: self.x * rhs.y - rhs.x * self.y,
222 }
223 }
224
225 #[inline]
229 #[must_use]
230 pub fn min(self, rhs: Self) -> Self {
231 Self {
232 x: self.x.min(rhs.x),
233 y: self.y.min(rhs.y),
234 z: self.z.min(rhs.z),
235 }
236 }
237
238 #[inline]
242 #[must_use]
243 pub fn max(self, rhs: Self) -> Self {
244 Self {
245 x: self.x.max(rhs.x),
246 y: self.y.max(rhs.y),
247 z: self.z.max(rhs.z),
248 }
249 }
250
251 #[inline]
259 #[must_use]
260 pub fn clamp(self, min: Self, max: Self) -> Self {
261 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
262 self.max(min).min(max)
263 }
264
265 #[inline]
269 #[must_use]
270 pub fn min_element(self) -> f32 {
271 self.x.min(self.y.min(self.z))
272 }
273
274 #[inline]
278 #[must_use]
279 pub fn max_element(self) -> f32 {
280 self.x.max(self.y.max(self.z))
281 }
282
283 #[inline]
287 #[must_use]
288 pub fn element_sum(self) -> f32 {
289 self.x + self.y + self.z
290 }
291
292 #[inline]
296 #[must_use]
297 pub fn element_product(self) -> f32 {
298 self.x * self.y * self.z
299 }
300
301 #[inline]
307 #[must_use]
308 pub fn cmpeq(self, rhs: Self) -> BVec3 {
309 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
310 }
311
312 #[inline]
318 #[must_use]
319 pub fn cmpne(self, rhs: Self) -> BVec3 {
320 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
321 }
322
323 #[inline]
329 #[must_use]
330 pub fn cmpge(self, rhs: Self) -> BVec3 {
331 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
332 }
333
334 #[inline]
340 #[must_use]
341 pub fn cmpgt(self, rhs: Self) -> BVec3 {
342 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
343 }
344
345 #[inline]
351 #[must_use]
352 pub fn cmple(self, rhs: Self) -> BVec3 {
353 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
354 }
355
356 #[inline]
362 #[must_use]
363 pub fn cmplt(self, rhs: Self) -> BVec3 {
364 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
365 }
366
367 #[inline]
369 #[must_use]
370 pub fn abs(self) -> Self {
371 Self {
372 x: math::abs(self.x),
373 y: math::abs(self.y),
374 z: math::abs(self.z),
375 }
376 }
377
378 #[inline]
384 #[must_use]
385 pub fn signum(self) -> Self {
386 Self {
387 x: math::signum(self.x),
388 y: math::signum(self.y),
389 z: math::signum(self.z),
390 }
391 }
392
393 #[inline]
395 #[must_use]
396 pub fn copysign(self, rhs: Self) -> Self {
397 Self {
398 x: math::copysign(self.x, rhs.x),
399 y: math::copysign(self.y, rhs.y),
400 z: math::copysign(self.z, rhs.z),
401 }
402 }
403
404 #[inline]
409 #[must_use]
410 pub fn is_negative_bitmask(self) -> u32 {
411 (self.x.is_sign_negative() as u32)
412 | (self.y.is_sign_negative() as u32) << 1
413 | (self.z.is_sign_negative() as u32) << 2
414 }
415
416 #[inline]
419 #[must_use]
420 pub fn is_finite(self) -> bool {
421 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
422 }
423
424 pub fn is_finite_mask(self) -> BVec3 {
428 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
429 }
430
431 #[inline]
433 #[must_use]
434 pub fn is_nan(self) -> bool {
435 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
436 }
437
438 #[inline]
442 #[must_use]
443 pub fn is_nan_mask(self) -> BVec3 {
444 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
445 }
446
447 #[doc(alias = "magnitude")]
449 #[inline]
450 #[must_use]
451 pub fn length(self) -> f32 {
452 math::sqrt(self.dot(self))
453 }
454
455 #[doc(alias = "magnitude2")]
459 #[inline]
460 #[must_use]
461 pub fn length_squared(self) -> f32 {
462 self.dot(self)
463 }
464
465 #[inline]
469 #[must_use]
470 pub fn length_recip(self) -> f32 {
471 self.length().recip()
472 }
473
474 #[inline]
476 #[must_use]
477 pub fn distance(self, rhs: Self) -> f32 {
478 (self - rhs).length()
479 }
480
481 #[inline]
483 #[must_use]
484 pub fn distance_squared(self, rhs: Self) -> f32 {
485 (self - rhs).length_squared()
486 }
487
488 #[inline]
490 #[must_use]
491 pub fn div_euclid(self, rhs: Self) -> Self {
492 Self::new(
493 math::div_euclid(self.x, rhs.x),
494 math::div_euclid(self.y, rhs.y),
495 math::div_euclid(self.z, rhs.z),
496 )
497 }
498
499 #[inline]
503 #[must_use]
504 pub fn rem_euclid(self, rhs: Self) -> Self {
505 Self::new(
506 math::rem_euclid(self.x, rhs.x),
507 math::rem_euclid(self.y, rhs.y),
508 math::rem_euclid(self.z, rhs.z),
509 )
510 }
511
512 #[inline]
522 #[must_use]
523 pub fn normalize(self) -> Self {
524 #[allow(clippy::let_and_return)]
525 let normalized = self.mul(self.length_recip());
526 glam_assert!(normalized.is_finite());
527 normalized
528 }
529
530 #[inline]
537 #[must_use]
538 pub fn try_normalize(self) -> Option<Self> {
539 let rcp = self.length_recip();
540 if rcp.is_finite() && rcp > 0.0 {
541 Some(self * rcp)
542 } else {
543 None
544 }
545 }
546
547 #[inline]
555 #[must_use]
556 pub fn normalize_or(self, fallback: Self) -> Self {
557 let rcp = self.length_recip();
558 if rcp.is_finite() && rcp > 0.0 {
559 self * rcp
560 } else {
561 fallback
562 }
563 }
564
565 #[inline]
572 #[must_use]
573 pub fn normalize_or_zero(self) -> Self {
574 self.normalize_or(Self::ZERO)
575 }
576
577 #[inline]
581 #[must_use]
582 pub fn is_normalized(self) -> bool {
583 math::abs(self.length_squared() - 1.0) <= 2e-4
584 }
585
586 #[inline]
594 #[must_use]
595 pub fn project_onto(self, rhs: Self) -> Self {
596 let other_len_sq_rcp = rhs.dot(rhs).recip();
597 glam_assert!(other_len_sq_rcp.is_finite());
598 rhs * self.dot(rhs) * other_len_sq_rcp
599 }
600
601 #[doc(alias("plane"))]
612 #[inline]
613 #[must_use]
614 pub fn reject_from(self, rhs: Self) -> Self {
615 self - self.project_onto(rhs)
616 }
617
618 #[inline]
626 #[must_use]
627 pub fn project_onto_normalized(self, rhs: Self) -> Self {
628 glam_assert!(rhs.is_normalized());
629 rhs * self.dot(rhs)
630 }
631
632 #[doc(alias("plane"))]
643 #[inline]
644 #[must_use]
645 pub fn reject_from_normalized(self, rhs: Self) -> Self {
646 self - self.project_onto_normalized(rhs)
647 }
648
649 #[inline]
652 #[must_use]
653 pub fn round(self) -> Self {
654 Self {
655 x: math::round(self.x),
656 y: math::round(self.y),
657 z: math::round(self.z),
658 }
659 }
660
661 #[inline]
664 #[must_use]
665 pub fn floor(self) -> Self {
666 Self {
667 x: math::floor(self.x),
668 y: math::floor(self.y),
669 z: math::floor(self.z),
670 }
671 }
672
673 #[inline]
676 #[must_use]
677 pub fn ceil(self) -> Self {
678 Self {
679 x: math::ceil(self.x),
680 y: math::ceil(self.y),
681 z: math::ceil(self.z),
682 }
683 }
684
685 #[inline]
688 #[must_use]
689 pub fn trunc(self) -> Self {
690 Self {
691 x: math::trunc(self.x),
692 y: math::trunc(self.y),
693 z: math::trunc(self.z),
694 }
695 }
696
697 #[inline]
704 #[must_use]
705 pub fn fract(self) -> Self {
706 self - self.trunc()
707 }
708
709 #[inline]
716 #[must_use]
717 pub fn fract_gl(self) -> Self {
718 self - self.floor()
719 }
720
721 #[inline]
724 #[must_use]
725 pub fn exp(self) -> Self {
726 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
727 }
728
729 #[inline]
731 #[must_use]
732 pub fn powf(self, n: f32) -> Self {
733 Self::new(
734 math::powf(self.x, n),
735 math::powf(self.y, n),
736 math::powf(self.z, n),
737 )
738 }
739
740 #[inline]
742 #[must_use]
743 pub fn recip(self) -> Self {
744 Self {
745 x: 1.0 / self.x,
746 y: 1.0 / self.y,
747 z: 1.0 / self.z,
748 }
749 }
750
751 #[doc(alias = "mix")]
757 #[inline]
758 #[must_use]
759 pub fn lerp(self, rhs: Self, s: f32) -> Self {
760 self * (1.0 - s) + rhs * s
761 }
762
763 #[inline]
768 #[must_use]
769 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
770 let a = rhs - *self;
771 let len = a.length();
772 if len <= d || len <= 1e-4 {
773 return rhs;
774 }
775 *self + a / len * d
776 }
777
778 #[inline]
784 pub fn midpoint(self, rhs: Self) -> Self {
785 (self + rhs) * 0.5
786 }
787
788 #[inline]
798 #[must_use]
799 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
800 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
801 }
802
803 #[inline]
809 #[must_use]
810 pub fn clamp_length(self, min: f32, max: f32) -> Self {
811 glam_assert!(0.0 <= min);
812 glam_assert!(min <= max);
813 let length_sq = self.length_squared();
814 if length_sq < min * min {
815 min * (self / math::sqrt(length_sq))
816 } else if length_sq > max * max {
817 max * (self / math::sqrt(length_sq))
818 } else {
819 self
820 }
821 }
822
823 #[inline]
829 #[must_use]
830 pub fn clamp_length_max(self, max: f32) -> Self {
831 glam_assert!(0.0 <= max);
832 let length_sq = self.length_squared();
833 if length_sq > max * max {
834 max * (self / math::sqrt(length_sq))
835 } else {
836 self
837 }
838 }
839
840 #[inline]
846 #[must_use]
847 pub fn clamp_length_min(self, min: f32) -> Self {
848 glam_assert!(0.0 <= min);
849 let length_sq = self.length_squared();
850 if length_sq < min * min {
851 min * (self / math::sqrt(length_sq))
852 } else {
853 self
854 }
855 }
856
857 #[inline]
865 #[must_use]
866 pub fn mul_add(self, a: Self, b: Self) -> Self {
867 Self::new(
868 math::mul_add(self.x, a.x, b.x),
869 math::mul_add(self.y, a.y, b.y),
870 math::mul_add(self.z, a.z, b.z),
871 )
872 }
873
874 #[inline]
883 #[must_use]
884 pub fn reflect(self, normal: Self) -> Self {
885 glam_assert!(normal.is_normalized());
886 self - 2.0 * self.dot(normal) * normal
887 }
888
889 #[inline]
899 #[must_use]
900 pub fn refract(self, normal: Self, eta: f32) -> Self {
901 glam_assert!(self.is_normalized());
902 glam_assert!(normal.is_normalized());
903 let n_dot_i = normal.dot(self);
904 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
905 if k >= 0.0 {
906 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
907 } else {
908 Self::ZERO
909 }
910 }
911
912 #[inline]
916 #[must_use]
917 pub fn angle_between(self, rhs: Self) -> f32 {
918 math::acos_approx(
919 self.dot(rhs)
920 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
921 )
922 }
923
924 #[inline]
931 #[must_use]
932 pub fn any_orthogonal_vector(&self) -> Self {
933 if math::abs(self.x) > math::abs(self.y) {
935 Self::new(-self.z, 0.0, self.x) } else {
937 Self::new(0.0, self.z, -self.y) }
939 }
940
941 #[inline]
949 #[must_use]
950 pub fn any_orthonormal_vector(&self) -> Self {
951 glam_assert!(self.is_normalized());
952 let sign = math::signum(self.z);
954 let a = -1.0 / (sign + self.z);
955 let b = self.x * self.y * a;
956 Self::new(b, sign + self.y * self.y * a, -self.y)
957 }
958
959 #[inline]
966 #[must_use]
967 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
968 glam_assert!(self.is_normalized());
969 let sign = math::signum(self.z);
971 let a = -1.0 / (sign + self.z);
972 let b = self.x * self.y * a;
973 (
974 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
975 Self::new(b, sign + self.y * self.y * a, -self.y),
976 )
977 }
978
979 #[inline]
981 #[must_use]
982 pub fn as_dvec3(&self) -> crate::DVec3 {
983 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
984 }
985
986 #[inline]
988 #[must_use]
989 pub fn as_i8vec3(&self) -> crate::I8Vec3 {
990 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
991 }
992
993 #[inline]
995 #[must_use]
996 pub fn as_u8vec3(&self) -> crate::U8Vec3 {
997 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
998 }
999
1000 #[inline]
1002 #[must_use]
1003 pub fn as_i16vec3(&self) -> crate::I16Vec3 {
1004 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1005 }
1006
1007 #[inline]
1009 #[must_use]
1010 pub fn as_u16vec3(&self) -> crate::U16Vec3 {
1011 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1012 }
1013
1014 #[inline]
1016 #[must_use]
1017 pub fn as_ivec3(&self) -> crate::IVec3 {
1018 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1019 }
1020
1021 #[inline]
1023 #[must_use]
1024 pub fn as_uvec3(&self) -> crate::UVec3 {
1025 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1026 }
1027
1028 #[inline]
1030 #[must_use]
1031 pub fn as_i64vec3(&self) -> crate::I64Vec3 {
1032 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1033 }
1034
1035 #[inline]
1037 #[must_use]
1038 pub fn as_u64vec3(&self) -> crate::U64Vec3 {
1039 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1040 }
1041}
1042
1043impl Default for Vec3 {
1044 #[inline(always)]
1045 fn default() -> Self {
1046 Self::ZERO
1047 }
1048}
1049
1050impl Div<Vec3> for Vec3 {
1051 type Output = Self;
1052 #[inline]
1053 fn div(self, rhs: Self) -> Self {
1054 Self {
1055 x: self.x.div(rhs.x),
1056 y: self.y.div(rhs.y),
1057 z: self.z.div(rhs.z),
1058 }
1059 }
1060}
1061
1062impl Div<&Vec3> for Vec3 {
1063 type Output = Vec3;
1064 #[inline]
1065 fn div(self, rhs: &Vec3) -> Vec3 {
1066 self.div(*rhs)
1067 }
1068}
1069
1070impl Div<&Vec3> for &Vec3 {
1071 type Output = Vec3;
1072 #[inline]
1073 fn div(self, rhs: &Vec3) -> Vec3 {
1074 (*self).div(*rhs)
1075 }
1076}
1077
1078impl Div<Vec3> for &Vec3 {
1079 type Output = Vec3;
1080 #[inline]
1081 fn div(self, rhs: Vec3) -> Vec3 {
1082 (*self).div(rhs)
1083 }
1084}
1085
1086impl DivAssign<Vec3> for Vec3 {
1087 #[inline]
1088 fn div_assign(&mut self, rhs: Self) {
1089 self.x.div_assign(rhs.x);
1090 self.y.div_assign(rhs.y);
1091 self.z.div_assign(rhs.z);
1092 }
1093}
1094
1095impl DivAssign<&Self> for Vec3 {
1096 #[inline]
1097 fn div_assign(&mut self, rhs: &Self) {
1098 self.div_assign(*rhs)
1099 }
1100}
1101
1102impl Div<f32> for Vec3 {
1103 type Output = Self;
1104 #[inline]
1105 fn div(self, rhs: f32) -> Self {
1106 Self {
1107 x: self.x.div(rhs),
1108 y: self.y.div(rhs),
1109 z: self.z.div(rhs),
1110 }
1111 }
1112}
1113
1114impl Div<&f32> for Vec3 {
1115 type Output = Vec3;
1116 #[inline]
1117 fn div(self, rhs: &f32) -> Vec3 {
1118 self.div(*rhs)
1119 }
1120}
1121
1122impl Div<&f32> for &Vec3 {
1123 type Output = Vec3;
1124 #[inline]
1125 fn div(self, rhs: &f32) -> Vec3 {
1126 (*self).div(*rhs)
1127 }
1128}
1129
1130impl Div<f32> for &Vec3 {
1131 type Output = Vec3;
1132 #[inline]
1133 fn div(self, rhs: f32) -> Vec3 {
1134 (*self).div(rhs)
1135 }
1136}
1137
1138impl DivAssign<f32> for Vec3 {
1139 #[inline]
1140 fn div_assign(&mut self, rhs: f32) {
1141 self.x.div_assign(rhs);
1142 self.y.div_assign(rhs);
1143 self.z.div_assign(rhs);
1144 }
1145}
1146
1147impl DivAssign<&f32> for Vec3 {
1148 #[inline]
1149 fn div_assign(&mut self, rhs: &f32) {
1150 self.div_assign(*rhs)
1151 }
1152}
1153
1154impl Div<Vec3> for f32 {
1155 type Output = Vec3;
1156 #[inline]
1157 fn div(self, rhs: Vec3) -> Vec3 {
1158 Vec3 {
1159 x: self.div(rhs.x),
1160 y: self.div(rhs.y),
1161 z: self.div(rhs.z),
1162 }
1163 }
1164}
1165
1166impl Div<&Vec3> for f32 {
1167 type Output = Vec3;
1168 #[inline]
1169 fn div(self, rhs: &Vec3) -> Vec3 {
1170 self.div(*rhs)
1171 }
1172}
1173
1174impl Div<&Vec3> for &f32 {
1175 type Output = Vec3;
1176 #[inline]
1177 fn div(self, rhs: &Vec3) -> Vec3 {
1178 (*self).div(*rhs)
1179 }
1180}
1181
1182impl Div<Vec3> for &f32 {
1183 type Output = Vec3;
1184 #[inline]
1185 fn div(self, rhs: Vec3) -> Vec3 {
1186 (*self).div(rhs)
1187 }
1188}
1189
1190impl Mul<Vec3> for Vec3 {
1191 type Output = Self;
1192 #[inline]
1193 fn mul(self, rhs: Self) -> Self {
1194 Self {
1195 x: self.x.mul(rhs.x),
1196 y: self.y.mul(rhs.y),
1197 z: self.z.mul(rhs.z),
1198 }
1199 }
1200}
1201
1202impl Mul<&Vec3> for Vec3 {
1203 type Output = Vec3;
1204 #[inline]
1205 fn mul(self, rhs: &Vec3) -> Vec3 {
1206 self.mul(*rhs)
1207 }
1208}
1209
1210impl Mul<&Vec3> for &Vec3 {
1211 type Output = Vec3;
1212 #[inline]
1213 fn mul(self, rhs: &Vec3) -> Vec3 {
1214 (*self).mul(*rhs)
1215 }
1216}
1217
1218impl Mul<Vec3> for &Vec3 {
1219 type Output = Vec3;
1220 #[inline]
1221 fn mul(self, rhs: Vec3) -> Vec3 {
1222 (*self).mul(rhs)
1223 }
1224}
1225
1226impl MulAssign<Vec3> for Vec3 {
1227 #[inline]
1228 fn mul_assign(&mut self, rhs: Self) {
1229 self.x.mul_assign(rhs.x);
1230 self.y.mul_assign(rhs.y);
1231 self.z.mul_assign(rhs.z);
1232 }
1233}
1234
1235impl MulAssign<&Self> for Vec3 {
1236 #[inline]
1237 fn mul_assign(&mut self, rhs: &Self) {
1238 self.mul_assign(*rhs)
1239 }
1240}
1241
1242impl Mul<f32> for Vec3 {
1243 type Output = Self;
1244 #[inline]
1245 fn mul(self, rhs: f32) -> Self {
1246 Self {
1247 x: self.x.mul(rhs),
1248 y: self.y.mul(rhs),
1249 z: self.z.mul(rhs),
1250 }
1251 }
1252}
1253
1254impl Mul<&f32> for Vec3 {
1255 type Output = Vec3;
1256 #[inline]
1257 fn mul(self, rhs: &f32) -> Vec3 {
1258 self.mul(*rhs)
1259 }
1260}
1261
1262impl Mul<&f32> for &Vec3 {
1263 type Output = Vec3;
1264 #[inline]
1265 fn mul(self, rhs: &f32) -> Vec3 {
1266 (*self).mul(*rhs)
1267 }
1268}
1269
1270impl Mul<f32> for &Vec3 {
1271 type Output = Vec3;
1272 #[inline]
1273 fn mul(self, rhs: f32) -> Vec3 {
1274 (*self).mul(rhs)
1275 }
1276}
1277
1278impl MulAssign<f32> for Vec3 {
1279 #[inline]
1280 fn mul_assign(&mut self, rhs: f32) {
1281 self.x.mul_assign(rhs);
1282 self.y.mul_assign(rhs);
1283 self.z.mul_assign(rhs);
1284 }
1285}
1286
1287impl MulAssign<&f32> for Vec3 {
1288 #[inline]
1289 fn mul_assign(&mut self, rhs: &f32) {
1290 self.mul_assign(*rhs)
1291 }
1292}
1293
1294impl Mul<Vec3> for f32 {
1295 type Output = Vec3;
1296 #[inline]
1297 fn mul(self, rhs: Vec3) -> Vec3 {
1298 Vec3 {
1299 x: self.mul(rhs.x),
1300 y: self.mul(rhs.y),
1301 z: self.mul(rhs.z),
1302 }
1303 }
1304}
1305
1306impl Mul<&Vec3> for f32 {
1307 type Output = Vec3;
1308 #[inline]
1309 fn mul(self, rhs: &Vec3) -> Vec3 {
1310 self.mul(*rhs)
1311 }
1312}
1313
1314impl Mul<&Vec3> for &f32 {
1315 type Output = Vec3;
1316 #[inline]
1317 fn mul(self, rhs: &Vec3) -> Vec3 {
1318 (*self).mul(*rhs)
1319 }
1320}
1321
1322impl Mul<Vec3> for &f32 {
1323 type Output = Vec3;
1324 #[inline]
1325 fn mul(self, rhs: Vec3) -> Vec3 {
1326 (*self).mul(rhs)
1327 }
1328}
1329
1330impl Add<Vec3> for Vec3 {
1331 type Output = Self;
1332 #[inline]
1333 fn add(self, rhs: Self) -> Self {
1334 Self {
1335 x: self.x.add(rhs.x),
1336 y: self.y.add(rhs.y),
1337 z: self.z.add(rhs.z),
1338 }
1339 }
1340}
1341
1342impl Add<&Vec3> for Vec3 {
1343 type Output = Vec3;
1344 #[inline]
1345 fn add(self, rhs: &Vec3) -> Vec3 {
1346 self.add(*rhs)
1347 }
1348}
1349
1350impl Add<&Vec3> for &Vec3 {
1351 type Output = Vec3;
1352 #[inline]
1353 fn add(self, rhs: &Vec3) -> Vec3 {
1354 (*self).add(*rhs)
1355 }
1356}
1357
1358impl Add<Vec3> for &Vec3 {
1359 type Output = Vec3;
1360 #[inline]
1361 fn add(self, rhs: Vec3) -> Vec3 {
1362 (*self).add(rhs)
1363 }
1364}
1365
1366impl AddAssign<Vec3> for Vec3 {
1367 #[inline]
1368 fn add_assign(&mut self, rhs: Self) {
1369 self.x.add_assign(rhs.x);
1370 self.y.add_assign(rhs.y);
1371 self.z.add_assign(rhs.z);
1372 }
1373}
1374
1375impl AddAssign<&Self> for Vec3 {
1376 #[inline]
1377 fn add_assign(&mut self, rhs: &Self) {
1378 self.add_assign(*rhs)
1379 }
1380}
1381
1382impl Add<f32> for Vec3 {
1383 type Output = Self;
1384 #[inline]
1385 fn add(self, rhs: f32) -> Self {
1386 Self {
1387 x: self.x.add(rhs),
1388 y: self.y.add(rhs),
1389 z: self.z.add(rhs),
1390 }
1391 }
1392}
1393
1394impl Add<&f32> for Vec3 {
1395 type Output = Vec3;
1396 #[inline]
1397 fn add(self, rhs: &f32) -> Vec3 {
1398 self.add(*rhs)
1399 }
1400}
1401
1402impl Add<&f32> for &Vec3 {
1403 type Output = Vec3;
1404 #[inline]
1405 fn add(self, rhs: &f32) -> Vec3 {
1406 (*self).add(*rhs)
1407 }
1408}
1409
1410impl Add<f32> for &Vec3 {
1411 type Output = Vec3;
1412 #[inline]
1413 fn add(self, rhs: f32) -> Vec3 {
1414 (*self).add(rhs)
1415 }
1416}
1417
1418impl AddAssign<f32> for Vec3 {
1419 #[inline]
1420 fn add_assign(&mut self, rhs: f32) {
1421 self.x.add_assign(rhs);
1422 self.y.add_assign(rhs);
1423 self.z.add_assign(rhs);
1424 }
1425}
1426
1427impl AddAssign<&f32> for Vec3 {
1428 #[inline]
1429 fn add_assign(&mut self, rhs: &f32) {
1430 self.add_assign(*rhs)
1431 }
1432}
1433
1434impl Add<Vec3> for f32 {
1435 type Output = Vec3;
1436 #[inline]
1437 fn add(self, rhs: Vec3) -> Vec3 {
1438 Vec3 {
1439 x: self.add(rhs.x),
1440 y: self.add(rhs.y),
1441 z: self.add(rhs.z),
1442 }
1443 }
1444}
1445
1446impl Add<&Vec3> for f32 {
1447 type Output = Vec3;
1448 #[inline]
1449 fn add(self, rhs: &Vec3) -> Vec3 {
1450 self.add(*rhs)
1451 }
1452}
1453
1454impl Add<&Vec3> for &f32 {
1455 type Output = Vec3;
1456 #[inline]
1457 fn add(self, rhs: &Vec3) -> Vec3 {
1458 (*self).add(*rhs)
1459 }
1460}
1461
1462impl Add<Vec3> for &f32 {
1463 type Output = Vec3;
1464 #[inline]
1465 fn add(self, rhs: Vec3) -> Vec3 {
1466 (*self).add(rhs)
1467 }
1468}
1469
1470impl Sub<Vec3> for Vec3 {
1471 type Output = Self;
1472 #[inline]
1473 fn sub(self, rhs: Self) -> Self {
1474 Self {
1475 x: self.x.sub(rhs.x),
1476 y: self.y.sub(rhs.y),
1477 z: self.z.sub(rhs.z),
1478 }
1479 }
1480}
1481
1482impl Sub<&Vec3> for Vec3 {
1483 type Output = Vec3;
1484 #[inline]
1485 fn sub(self, rhs: &Vec3) -> Vec3 {
1486 self.sub(*rhs)
1487 }
1488}
1489
1490impl Sub<&Vec3> for &Vec3 {
1491 type Output = Vec3;
1492 #[inline]
1493 fn sub(self, rhs: &Vec3) -> Vec3 {
1494 (*self).sub(*rhs)
1495 }
1496}
1497
1498impl Sub<Vec3> for &Vec3 {
1499 type Output = Vec3;
1500 #[inline]
1501 fn sub(self, rhs: Vec3) -> Vec3 {
1502 (*self).sub(rhs)
1503 }
1504}
1505
1506impl SubAssign<Vec3> for Vec3 {
1507 #[inline]
1508 fn sub_assign(&mut self, rhs: Vec3) {
1509 self.x.sub_assign(rhs.x);
1510 self.y.sub_assign(rhs.y);
1511 self.z.sub_assign(rhs.z);
1512 }
1513}
1514
1515impl SubAssign<&Self> for Vec3 {
1516 #[inline]
1517 fn sub_assign(&mut self, rhs: &Self) {
1518 self.sub_assign(*rhs)
1519 }
1520}
1521
1522impl Sub<f32> for Vec3 {
1523 type Output = Self;
1524 #[inline]
1525 fn sub(self, rhs: f32) -> Self {
1526 Self {
1527 x: self.x.sub(rhs),
1528 y: self.y.sub(rhs),
1529 z: self.z.sub(rhs),
1530 }
1531 }
1532}
1533
1534impl Sub<&f32> for Vec3 {
1535 type Output = Vec3;
1536 #[inline]
1537 fn sub(self, rhs: &f32) -> Vec3 {
1538 self.sub(*rhs)
1539 }
1540}
1541
1542impl Sub<&f32> for &Vec3 {
1543 type Output = Vec3;
1544 #[inline]
1545 fn sub(self, rhs: &f32) -> Vec3 {
1546 (*self).sub(*rhs)
1547 }
1548}
1549
1550impl Sub<f32> for &Vec3 {
1551 type Output = Vec3;
1552 #[inline]
1553 fn sub(self, rhs: f32) -> Vec3 {
1554 (*self).sub(rhs)
1555 }
1556}
1557
1558impl SubAssign<f32> for Vec3 {
1559 #[inline]
1560 fn sub_assign(&mut self, rhs: f32) {
1561 self.x.sub_assign(rhs);
1562 self.y.sub_assign(rhs);
1563 self.z.sub_assign(rhs);
1564 }
1565}
1566
1567impl SubAssign<&f32> for Vec3 {
1568 #[inline]
1569 fn sub_assign(&mut self, rhs: &f32) {
1570 self.sub_assign(*rhs)
1571 }
1572}
1573
1574impl Sub<Vec3> for f32 {
1575 type Output = Vec3;
1576 #[inline]
1577 fn sub(self, rhs: Vec3) -> Vec3 {
1578 Vec3 {
1579 x: self.sub(rhs.x),
1580 y: self.sub(rhs.y),
1581 z: self.sub(rhs.z),
1582 }
1583 }
1584}
1585
1586impl Sub<&Vec3> for f32 {
1587 type Output = Vec3;
1588 #[inline]
1589 fn sub(self, rhs: &Vec3) -> Vec3 {
1590 self.sub(*rhs)
1591 }
1592}
1593
1594impl Sub<&Vec3> for &f32 {
1595 type Output = Vec3;
1596 #[inline]
1597 fn sub(self, rhs: &Vec3) -> Vec3 {
1598 (*self).sub(*rhs)
1599 }
1600}
1601
1602impl Sub<Vec3> for &f32 {
1603 type Output = Vec3;
1604 #[inline]
1605 fn sub(self, rhs: Vec3) -> Vec3 {
1606 (*self).sub(rhs)
1607 }
1608}
1609
1610impl Rem<Vec3> for Vec3 {
1611 type Output = Self;
1612 #[inline]
1613 fn rem(self, rhs: Self) -> Self {
1614 Self {
1615 x: self.x.rem(rhs.x),
1616 y: self.y.rem(rhs.y),
1617 z: self.z.rem(rhs.z),
1618 }
1619 }
1620}
1621
1622impl Rem<&Vec3> for Vec3 {
1623 type Output = Vec3;
1624 #[inline]
1625 fn rem(self, rhs: &Vec3) -> Vec3 {
1626 self.rem(*rhs)
1627 }
1628}
1629
1630impl Rem<&Vec3> for &Vec3 {
1631 type Output = Vec3;
1632 #[inline]
1633 fn rem(self, rhs: &Vec3) -> Vec3 {
1634 (*self).rem(*rhs)
1635 }
1636}
1637
1638impl Rem<Vec3> for &Vec3 {
1639 type Output = Vec3;
1640 #[inline]
1641 fn rem(self, rhs: Vec3) -> Vec3 {
1642 (*self).rem(rhs)
1643 }
1644}
1645
1646impl RemAssign<Vec3> for Vec3 {
1647 #[inline]
1648 fn rem_assign(&mut self, rhs: Self) {
1649 self.x.rem_assign(rhs.x);
1650 self.y.rem_assign(rhs.y);
1651 self.z.rem_assign(rhs.z);
1652 }
1653}
1654
1655impl RemAssign<&Self> for Vec3 {
1656 #[inline]
1657 fn rem_assign(&mut self, rhs: &Self) {
1658 self.rem_assign(*rhs)
1659 }
1660}
1661
1662impl Rem<f32> for Vec3 {
1663 type Output = Self;
1664 #[inline]
1665 fn rem(self, rhs: f32) -> Self {
1666 Self {
1667 x: self.x.rem(rhs),
1668 y: self.y.rem(rhs),
1669 z: self.z.rem(rhs),
1670 }
1671 }
1672}
1673
1674impl Rem<&f32> for Vec3 {
1675 type Output = Vec3;
1676 #[inline]
1677 fn rem(self, rhs: &f32) -> Vec3 {
1678 self.rem(*rhs)
1679 }
1680}
1681
1682impl Rem<&f32> for &Vec3 {
1683 type Output = Vec3;
1684 #[inline]
1685 fn rem(self, rhs: &f32) -> Vec3 {
1686 (*self).rem(*rhs)
1687 }
1688}
1689
1690impl Rem<f32> for &Vec3 {
1691 type Output = Vec3;
1692 #[inline]
1693 fn rem(self, rhs: f32) -> Vec3 {
1694 (*self).rem(rhs)
1695 }
1696}
1697
1698impl RemAssign<f32> for Vec3 {
1699 #[inline]
1700 fn rem_assign(&mut self, rhs: f32) {
1701 self.x.rem_assign(rhs);
1702 self.y.rem_assign(rhs);
1703 self.z.rem_assign(rhs);
1704 }
1705}
1706
1707impl RemAssign<&f32> for Vec3 {
1708 #[inline]
1709 fn rem_assign(&mut self, rhs: &f32) {
1710 self.rem_assign(*rhs)
1711 }
1712}
1713
1714impl Rem<Vec3> for f32 {
1715 type Output = Vec3;
1716 #[inline]
1717 fn rem(self, rhs: Vec3) -> Vec3 {
1718 Vec3 {
1719 x: self.rem(rhs.x),
1720 y: self.rem(rhs.y),
1721 z: self.rem(rhs.z),
1722 }
1723 }
1724}
1725
1726impl Rem<&Vec3> for f32 {
1727 type Output = Vec3;
1728 #[inline]
1729 fn rem(self, rhs: &Vec3) -> Vec3 {
1730 self.rem(*rhs)
1731 }
1732}
1733
1734impl Rem<&Vec3> for &f32 {
1735 type Output = Vec3;
1736 #[inline]
1737 fn rem(self, rhs: &Vec3) -> Vec3 {
1738 (*self).rem(*rhs)
1739 }
1740}
1741
1742impl Rem<Vec3> for &f32 {
1743 type Output = Vec3;
1744 #[inline]
1745 fn rem(self, rhs: Vec3) -> Vec3 {
1746 (*self).rem(rhs)
1747 }
1748}
1749
1750#[cfg(not(target_arch = "spirv"))]
1751impl AsRef<[f32; 3]> for Vec3 {
1752 #[inline]
1753 fn as_ref(&self) -> &[f32; 3] {
1754 unsafe { &*(self as *const Vec3 as *const [f32; 3]) }
1755 }
1756}
1757
1758#[cfg(not(target_arch = "spirv"))]
1759impl AsMut<[f32; 3]> for Vec3 {
1760 #[inline]
1761 fn as_mut(&mut self) -> &mut [f32; 3] {
1762 unsafe { &mut *(self as *mut Vec3 as *mut [f32; 3]) }
1763 }
1764}
1765
1766impl Sum for Vec3 {
1767 #[inline]
1768 fn sum<I>(iter: I) -> Self
1769 where
1770 I: Iterator<Item = Self>,
1771 {
1772 iter.fold(Self::ZERO, Self::add)
1773 }
1774}
1775
1776impl<'a> Sum<&'a Self> for Vec3 {
1777 #[inline]
1778 fn sum<I>(iter: I) -> Self
1779 where
1780 I: Iterator<Item = &'a Self>,
1781 {
1782 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1783 }
1784}
1785
1786impl Product for Vec3 {
1787 #[inline]
1788 fn product<I>(iter: I) -> Self
1789 where
1790 I: Iterator<Item = Self>,
1791 {
1792 iter.fold(Self::ONE, Self::mul)
1793 }
1794}
1795
1796impl<'a> Product<&'a Self> for Vec3 {
1797 #[inline]
1798 fn product<I>(iter: I) -> Self
1799 where
1800 I: Iterator<Item = &'a Self>,
1801 {
1802 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1803 }
1804}
1805
1806impl Neg for Vec3 {
1807 type Output = Self;
1808 #[inline]
1809 fn neg(self) -> Self {
1810 Self {
1811 x: self.x.neg(),
1812 y: self.y.neg(),
1813 z: self.z.neg(),
1814 }
1815 }
1816}
1817
1818impl Neg for &Vec3 {
1819 type Output = Vec3;
1820 #[inline]
1821 fn neg(self) -> Vec3 {
1822 (*self).neg()
1823 }
1824}
1825
1826impl Index<usize> for Vec3 {
1827 type Output = f32;
1828 #[inline]
1829 fn index(&self, index: usize) -> &Self::Output {
1830 match index {
1831 0 => &self.x,
1832 1 => &self.y,
1833 2 => &self.z,
1834 _ => panic!("index out of bounds"),
1835 }
1836 }
1837}
1838
1839impl IndexMut<usize> for Vec3 {
1840 #[inline]
1841 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1842 match index {
1843 0 => &mut self.x,
1844 1 => &mut self.y,
1845 2 => &mut self.z,
1846 _ => panic!("index out of bounds"),
1847 }
1848 }
1849}
1850
1851impl fmt::Display for Vec3 {
1852 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1853 if let Some(p) = f.precision() {
1854 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
1855 } else {
1856 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
1857 }
1858 }
1859}
1860
1861impl fmt::Debug for Vec3 {
1862 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1863 fmt.debug_tuple(stringify!(Vec3))
1864 .field(&self.x)
1865 .field(&self.y)
1866 .field(&self.z)
1867 .finish()
1868 }
1869}
1870
1871impl From<[f32; 3]> for Vec3 {
1872 #[inline]
1873 fn from(a: [f32; 3]) -> Self {
1874 Self::new(a[0], a[1], a[2])
1875 }
1876}
1877
1878impl From<Vec3> for [f32; 3] {
1879 #[inline]
1880 fn from(v: Vec3) -> Self {
1881 [v.x, v.y, v.z]
1882 }
1883}
1884
1885impl From<(f32, f32, f32)> for Vec3 {
1886 #[inline]
1887 fn from(t: (f32, f32, f32)) -> Self {
1888 Self::new(t.0, t.1, t.2)
1889 }
1890}
1891
1892impl From<Vec3> for (f32, f32, f32) {
1893 #[inline]
1894 fn from(v: Vec3) -> Self {
1895 (v.x, v.y, v.z)
1896 }
1897}
1898
1899impl From<(Vec2, f32)> for Vec3 {
1900 #[inline]
1901 fn from((v, z): (Vec2, f32)) -> Self {
1902 Self::new(v.x, v.y, z)
1903 }
1904}
1905
1906impl From<BVec3> for Vec3 {
1907 #[inline]
1908 fn from(v: BVec3) -> Self {
1909 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
1910 }
1911}
1912
1913impl From<BVec3A> for Vec3 {
1914 #[inline]
1915 fn from(v: BVec3A) -> Self {
1916 let bool_array: [bool; 3] = v.into();
1917 Self::new(
1918 f32::from(bool_array[0]),
1919 f32::from(bool_array[1]),
1920 f32::from(bool_array[2]),
1921 )
1922 }
1923}