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, f: F) -> Self
111 where
112 F: Fn(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]
520 #[must_use]
521 pub fn is_finite(self) -> bool {
522 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
523 }
524
525 #[inline]
529 #[must_use]
530 pub fn is_finite_mask(self) -> BVec3 {
531 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
532 }
533
534 #[inline]
536 #[must_use]
537 pub fn is_nan(self) -> bool {
538 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
539 }
540
541 #[inline]
545 #[must_use]
546 pub fn is_nan_mask(self) -> BVec3 {
547 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
548 }
549
550 #[doc(alias = "magnitude")]
552 #[inline]
553 #[must_use]
554 pub fn length(self) -> f32 {
555 math::sqrt(self.dot(self))
556 }
557
558 #[doc(alias = "magnitude2")]
562 #[inline]
563 #[must_use]
564 pub fn length_squared(self) -> f32 {
565 self.dot(self)
566 }
567
568 #[inline]
572 #[must_use]
573 pub fn length_recip(self) -> f32 {
574 self.length().recip()
575 }
576
577 #[inline]
579 #[must_use]
580 pub fn distance(self, rhs: Self) -> f32 {
581 (self - rhs).length()
582 }
583
584 #[inline]
586 #[must_use]
587 pub fn distance_squared(self, rhs: Self) -> f32 {
588 (self - rhs).length_squared()
589 }
590
591 #[inline]
593 #[must_use]
594 pub fn div_euclid(self, rhs: Self) -> Self {
595 Self::new(
596 math::div_euclid(self.x, rhs.x),
597 math::div_euclid(self.y, rhs.y),
598 math::div_euclid(self.z, rhs.z),
599 )
600 }
601
602 #[inline]
606 #[must_use]
607 pub fn rem_euclid(self, rhs: Self) -> Self {
608 Self::new(
609 math::rem_euclid(self.x, rhs.x),
610 math::rem_euclid(self.y, rhs.y),
611 math::rem_euclid(self.z, rhs.z),
612 )
613 }
614
615 #[inline]
625 #[must_use]
626 pub fn normalize(self) -> Self {
627 #[allow(clippy::let_and_return)]
628 let normalized = self.mul(self.length_recip());
629 glam_assert!(normalized.is_finite());
630 normalized
631 }
632
633 #[inline]
640 #[must_use]
641 pub fn try_normalize(self) -> Option<Self> {
642 let rcp = self.length_recip();
643 if rcp.is_finite() && rcp > 0.0 {
644 Some(self * rcp)
645 } else {
646 None
647 }
648 }
649
650 #[inline]
658 #[must_use]
659 pub fn normalize_or(self, fallback: Self) -> Self {
660 let rcp = self.length_recip();
661 if rcp.is_finite() && rcp > 0.0 {
662 self * rcp
663 } else {
664 fallback
665 }
666 }
667
668 #[inline]
675 #[must_use]
676 pub fn normalize_or_zero(self) -> Self {
677 self.normalize_or(Self::ZERO)
678 }
679
680 #[inline]
684 #[must_use]
685 pub fn normalize_and_length(self) -> (Self, f32) {
686 let length = self.length();
687 let rcp = 1.0 / length;
688 if rcp.is_finite() && rcp > 0.0 {
689 (self * rcp, length)
690 } else {
691 (Self::X, 0.0)
692 }
693 }
694
695 #[inline]
699 #[must_use]
700 pub fn is_normalized(self) -> bool {
701 math::abs(self.length_squared() - 1.0) <= 2e-4
702 }
703
704 #[inline]
712 #[must_use]
713 pub fn project_onto(self, rhs: Self) -> Self {
714 let other_len_sq_rcp = rhs.dot(rhs).recip();
715 glam_assert!(other_len_sq_rcp.is_finite());
716 rhs * self.dot(rhs) * other_len_sq_rcp
717 }
718
719 #[doc(alias("plane"))]
730 #[inline]
731 #[must_use]
732 pub fn reject_from(self, rhs: Self) -> Self {
733 self - self.project_onto(rhs)
734 }
735
736 #[inline]
744 #[must_use]
745 pub fn project_onto_normalized(self, rhs: Self) -> Self {
746 glam_assert!(rhs.is_normalized());
747 rhs * self.dot(rhs)
748 }
749
750 #[doc(alias("plane"))]
761 #[inline]
762 #[must_use]
763 pub fn reject_from_normalized(self, rhs: Self) -> Self {
764 self - self.project_onto_normalized(rhs)
765 }
766
767 #[inline]
770 #[must_use]
771 pub fn round(self) -> Self {
772 Self {
773 x: math::round(self.x),
774 y: math::round(self.y),
775 z: math::round(self.z),
776 }
777 }
778
779 #[inline]
782 #[must_use]
783 pub fn floor(self) -> Self {
784 Self {
785 x: math::floor(self.x),
786 y: math::floor(self.y),
787 z: math::floor(self.z),
788 }
789 }
790
791 #[inline]
794 #[must_use]
795 pub fn ceil(self) -> Self {
796 Self {
797 x: math::ceil(self.x),
798 y: math::ceil(self.y),
799 z: math::ceil(self.z),
800 }
801 }
802
803 #[inline]
806 #[must_use]
807 pub fn trunc(self) -> Self {
808 Self {
809 x: math::trunc(self.x),
810 y: math::trunc(self.y),
811 z: math::trunc(self.z),
812 }
813 }
814
815 #[inline]
819 #[must_use]
820 pub fn step(self, rhs: Self) -> Self {
821 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
822 }
823
824 #[inline]
826 #[must_use]
827 pub fn saturate(self) -> Self {
828 self.clamp(Self::ZERO, Self::ONE)
829 }
830
831 #[inline]
838 #[must_use]
839 pub fn fract(self) -> Self {
840 self - self.trunc()
841 }
842
843 #[inline]
850 #[must_use]
851 pub fn fract_gl(self) -> Self {
852 self - self.floor()
853 }
854
855 #[inline]
858 #[must_use]
859 pub fn exp(self) -> Self {
860 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
861 }
862
863 #[inline]
865 #[must_use]
866 pub fn exp2(self) -> Self {
867 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
868 }
869
870 #[inline]
873 #[must_use]
874 pub fn ln(self) -> Self {
875 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
876 }
877
878 #[inline]
881 #[must_use]
882 pub fn log2(self) -> Self {
883 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
884 }
885
886 #[inline]
888 #[must_use]
889 pub fn powf(self, n: f32) -> Self {
890 Self::new(
891 math::powf(self.x, n),
892 math::powf(self.y, n),
893 math::powf(self.z, n),
894 )
895 }
896
897 #[inline]
900 #[must_use]
901 pub fn sqrt(self) -> Self {
902 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
903 }
904
905 #[inline]
907 #[must_use]
908 pub fn cos(self) -> Self {
909 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
910 }
911
912 #[inline]
914 #[must_use]
915 pub fn sin(self) -> Self {
916 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
917 }
918
919 #[inline]
921 #[must_use]
922 pub fn sin_cos(self) -> (Self, Self) {
923 let (sin_x, cos_x) = math::sin_cos(self.x);
924 let (sin_y, cos_y) = math::sin_cos(self.y);
925 let (sin_z, cos_z) = math::sin_cos(self.z);
926
927 (
928 Self::new(sin_x, sin_y, sin_z),
929 Self::new(cos_x, cos_y, cos_z),
930 )
931 }
932
933 #[inline]
935 #[must_use]
936 pub fn recip(self) -> Self {
937 Self {
938 x: 1.0 / self.x,
939 y: 1.0 / self.y,
940 z: 1.0 / self.z,
941 }
942 }
943
944 #[doc(alias = "mix")]
950 #[inline]
951 #[must_use]
952 pub fn lerp(self, rhs: Self, s: f32) -> Self {
953 self * (1.0 - s) + rhs * s
954 }
955
956 #[inline]
961 #[must_use]
962 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
963 let a = rhs - self;
964 let len = a.length();
965 if len <= d || len <= 1e-4 {
966 return rhs;
967 }
968 self + a / len * d
969 }
970
971 #[inline]
977 pub fn midpoint(self, rhs: Self) -> Self {
978 (self + rhs) * 0.5
979 }
980
981 #[inline]
991 #[must_use]
992 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
993 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
994 }
995
996 #[inline]
1002 #[must_use]
1003 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1004 glam_assert!(0.0 <= min);
1005 glam_assert!(min <= max);
1006 let length_sq = self.length_squared();
1007 if length_sq < min * min {
1008 min * (self / math::sqrt(length_sq))
1009 } else if length_sq > max * max {
1010 max * (self / math::sqrt(length_sq))
1011 } else {
1012 self
1013 }
1014 }
1015
1016 #[inline]
1022 #[must_use]
1023 pub fn clamp_length_max(self, max: f32) -> Self {
1024 glam_assert!(0.0 <= max);
1025 let length_sq = self.length_squared();
1026 if length_sq > max * max {
1027 max * (self / math::sqrt(length_sq))
1028 } else {
1029 self
1030 }
1031 }
1032
1033 #[inline]
1039 #[must_use]
1040 pub fn clamp_length_min(self, min: f32) -> Self {
1041 glam_assert!(0.0 <= min);
1042 let length_sq = self.length_squared();
1043 if length_sq < min * min {
1044 min * (self / math::sqrt(length_sq))
1045 } else {
1046 self
1047 }
1048 }
1049
1050 #[inline]
1058 #[must_use]
1059 pub fn mul_add(self, a: Self, b: Self) -> Self {
1060 Self::new(
1061 math::mul_add(self.x, a.x, b.x),
1062 math::mul_add(self.y, a.y, b.y),
1063 math::mul_add(self.z, a.z, b.z),
1064 )
1065 }
1066
1067 #[inline]
1076 #[must_use]
1077 pub fn reflect(self, normal: Self) -> Self {
1078 glam_assert!(normal.is_normalized());
1079 self - 2.0 * self.dot(normal) * normal
1080 }
1081
1082 #[inline]
1092 #[must_use]
1093 pub fn refract(self, normal: Self, eta: f32) -> Self {
1094 glam_assert!(self.is_normalized());
1095 glam_assert!(normal.is_normalized());
1096 let n_dot_i = normal.dot(self);
1097 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1098 if k >= 0.0 {
1099 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1100 } else {
1101 Self::ZERO
1102 }
1103 }
1104
1105 #[inline]
1109 #[must_use]
1110 pub fn angle_between(self, rhs: Self) -> f32 {
1111 math::acos_approx(
1112 self.dot(rhs)
1113 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1114 )
1115 }
1116
1117 #[inline]
1119 #[must_use]
1120 pub fn rotate_x(self, angle: f32) -> Self {
1121 let (sina, cosa) = math::sin_cos(angle);
1122 Self::new(
1123 self.x,
1124 self.y * cosa - self.z * sina,
1125 self.y * sina + self.z * cosa,
1126 )
1127 }
1128
1129 #[inline]
1131 #[must_use]
1132 pub fn rotate_y(self, angle: f32) -> Self {
1133 let (sina, cosa) = math::sin_cos(angle);
1134 Self::new(
1135 self.x * cosa + self.z * sina,
1136 self.y,
1137 self.x * -sina + self.z * cosa,
1138 )
1139 }
1140
1141 #[inline]
1143 #[must_use]
1144 pub fn rotate_z(self, angle: f32) -> Self {
1145 let (sina, cosa) = math::sin_cos(angle);
1146 Self::new(
1147 self.x * cosa - self.y * sina,
1148 self.x * sina + self.y * cosa,
1149 self.z,
1150 )
1151 }
1152
1153 #[inline]
1161 #[must_use]
1162 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1163 Quat::from_axis_angle(axis, angle) * self
1164 }
1165
1166 #[inline]
1172 #[must_use]
1173 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1174 let angle_between = self.angle_between(rhs);
1175 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1177 let axis = self
1178 .cross(rhs)
1179 .try_normalize()
1180 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1181 Quat::from_axis_angle(axis, angle) * self
1182 }
1183
1184 #[inline]
1191 #[must_use]
1192 pub fn any_orthogonal_vector(self) -> Self {
1193 if math::abs(self.x) > math::abs(self.y) {
1195 Self::new(-self.z, 0.0, self.x) } else {
1197 Self::new(0.0, self.z, -self.y) }
1199 }
1200
1201 #[inline]
1209 #[must_use]
1210 pub fn any_orthonormal_vector(self) -> Self {
1211 glam_assert!(self.is_normalized());
1212 let sign = math::signum(self.z);
1214 let a = -1.0 / (sign + self.z);
1215 let b = self.x * self.y * a;
1216 Self::new(b, sign + self.y * self.y * a, -self.y)
1217 }
1218
1219 #[inline]
1226 #[must_use]
1227 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1228 glam_assert!(self.is_normalized());
1229 let sign = math::signum(self.z);
1231 let a = -1.0 / (sign + self.z);
1232 let b = self.x * self.y * a;
1233 (
1234 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1235 Self::new(b, sign + self.y * self.y * a, -self.y),
1236 )
1237 }
1238
1239 #[inline]
1245 #[must_use]
1246 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1247 let self_length = self.length();
1248 let rhs_length = rhs.length();
1249 let dot = self.dot(rhs) / (self_length * rhs_length);
1251 if math::abs(dot) < 1.0 - 3e-7 {
1253 let theta = math::acos_approx(dot);
1255 let sin_theta = math::sin(theta);
1257 let t1 = math::sin(theta * (1. - s));
1258 let t2 = math::sin(theta * s);
1259
1260 let result_length = self_length.lerp(rhs_length, s);
1262 return (self * (result_length / self_length) * t1
1264 + rhs * (result_length / rhs_length) * t2)
1265 * sin_theta.recip();
1266 }
1267 if dot < 0.0 {
1268 let axis = self.any_orthogonal_vector().normalize();
1272 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1273 let result_length = self_length.lerp(rhs_length, s);
1275 rotation * self * (result_length / self_length)
1276 } else {
1277 self.lerp(rhs, s)
1279 }
1280 }
1281
1282 #[inline]
1284 #[must_use]
1285 pub fn as_dvec3(self) -> crate::DVec3 {
1286 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1287 }
1288
1289 #[inline]
1291 #[must_use]
1292 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1293 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1294 }
1295
1296 #[inline]
1298 #[must_use]
1299 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1300 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1301 }
1302
1303 #[inline]
1305 #[must_use]
1306 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1307 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1308 }
1309
1310 #[inline]
1312 #[must_use]
1313 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1314 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1315 }
1316
1317 #[inline]
1319 #[must_use]
1320 pub fn as_ivec3(self) -> crate::IVec3 {
1321 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1322 }
1323
1324 #[inline]
1326 #[must_use]
1327 pub fn as_uvec3(self) -> crate::UVec3 {
1328 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1329 }
1330
1331 #[inline]
1333 #[must_use]
1334 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1335 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1336 }
1337
1338 #[inline]
1340 #[must_use]
1341 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1342 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1343 }
1344
1345 #[inline]
1347 #[must_use]
1348 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1349 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1350 }
1351
1352 #[inline]
1354 #[must_use]
1355 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1356 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1357 }
1358}
1359
1360impl Default for Vec3 {
1361 #[inline(always)]
1362 fn default() -> Self {
1363 Self::ZERO
1364 }
1365}
1366
1367impl Div for Vec3 {
1368 type Output = Self;
1369 #[inline]
1370 fn div(self, rhs: Self) -> Self {
1371 Self {
1372 x: self.x.div(rhs.x),
1373 y: self.y.div(rhs.y),
1374 z: self.z.div(rhs.z),
1375 }
1376 }
1377}
1378
1379impl Div<&Self> for Vec3 {
1380 type Output = Self;
1381 #[inline]
1382 fn div(self, rhs: &Self) -> Self {
1383 self.div(*rhs)
1384 }
1385}
1386
1387impl Div<&Vec3> for &Vec3 {
1388 type Output = Vec3;
1389 #[inline]
1390 fn div(self, rhs: &Vec3) -> Vec3 {
1391 (*self).div(*rhs)
1392 }
1393}
1394
1395impl Div<Vec3> for &Vec3 {
1396 type Output = Vec3;
1397 #[inline]
1398 fn div(self, rhs: Vec3) -> Vec3 {
1399 (*self).div(rhs)
1400 }
1401}
1402
1403impl DivAssign for Vec3 {
1404 #[inline]
1405 fn div_assign(&mut self, rhs: Self) {
1406 self.x.div_assign(rhs.x);
1407 self.y.div_assign(rhs.y);
1408 self.z.div_assign(rhs.z);
1409 }
1410}
1411
1412impl DivAssign<&Self> for Vec3 {
1413 #[inline]
1414 fn div_assign(&mut self, rhs: &Self) {
1415 self.div_assign(*rhs);
1416 }
1417}
1418
1419impl Div<f32> for Vec3 {
1420 type Output = Self;
1421 #[inline]
1422 fn div(self, rhs: f32) -> Self {
1423 Self {
1424 x: self.x.div(rhs),
1425 y: self.y.div(rhs),
1426 z: self.z.div(rhs),
1427 }
1428 }
1429}
1430
1431impl Div<&f32> for Vec3 {
1432 type Output = Self;
1433 #[inline]
1434 fn div(self, rhs: &f32) -> Self {
1435 self.div(*rhs)
1436 }
1437}
1438
1439impl Div<&f32> for &Vec3 {
1440 type Output = Vec3;
1441 #[inline]
1442 fn div(self, rhs: &f32) -> Vec3 {
1443 (*self).div(*rhs)
1444 }
1445}
1446
1447impl Div<f32> for &Vec3 {
1448 type Output = Vec3;
1449 #[inline]
1450 fn div(self, rhs: f32) -> Vec3 {
1451 (*self).div(rhs)
1452 }
1453}
1454
1455impl DivAssign<f32> for Vec3 {
1456 #[inline]
1457 fn div_assign(&mut self, rhs: f32) {
1458 self.x.div_assign(rhs);
1459 self.y.div_assign(rhs);
1460 self.z.div_assign(rhs);
1461 }
1462}
1463
1464impl DivAssign<&f32> for Vec3 {
1465 #[inline]
1466 fn div_assign(&mut self, rhs: &f32) {
1467 self.div_assign(*rhs);
1468 }
1469}
1470
1471impl Div<Vec3> for f32 {
1472 type Output = Vec3;
1473 #[inline]
1474 fn div(self, rhs: Vec3) -> Vec3 {
1475 Vec3 {
1476 x: self.div(rhs.x),
1477 y: self.div(rhs.y),
1478 z: self.div(rhs.z),
1479 }
1480 }
1481}
1482
1483impl Div<&Vec3> for f32 {
1484 type Output = Vec3;
1485 #[inline]
1486 fn div(self, rhs: &Vec3) -> Vec3 {
1487 self.div(*rhs)
1488 }
1489}
1490
1491impl Div<&Vec3> for &f32 {
1492 type Output = Vec3;
1493 #[inline]
1494 fn div(self, rhs: &Vec3) -> Vec3 {
1495 (*self).div(*rhs)
1496 }
1497}
1498
1499impl Div<Vec3> for &f32 {
1500 type Output = Vec3;
1501 #[inline]
1502 fn div(self, rhs: Vec3) -> Vec3 {
1503 (*self).div(rhs)
1504 }
1505}
1506
1507impl Mul for Vec3 {
1508 type Output = Self;
1509 #[inline]
1510 fn mul(self, rhs: Self) -> Self {
1511 Self {
1512 x: self.x.mul(rhs.x),
1513 y: self.y.mul(rhs.y),
1514 z: self.z.mul(rhs.z),
1515 }
1516 }
1517}
1518
1519impl Mul<&Self> for Vec3 {
1520 type Output = Self;
1521 #[inline]
1522 fn mul(self, rhs: &Self) -> Self {
1523 self.mul(*rhs)
1524 }
1525}
1526
1527impl Mul<&Vec3> for &Vec3 {
1528 type Output = Vec3;
1529 #[inline]
1530 fn mul(self, rhs: &Vec3) -> Vec3 {
1531 (*self).mul(*rhs)
1532 }
1533}
1534
1535impl Mul<Vec3> for &Vec3 {
1536 type Output = Vec3;
1537 #[inline]
1538 fn mul(self, rhs: Vec3) -> Vec3 {
1539 (*self).mul(rhs)
1540 }
1541}
1542
1543impl MulAssign for Vec3 {
1544 #[inline]
1545 fn mul_assign(&mut self, rhs: Self) {
1546 self.x.mul_assign(rhs.x);
1547 self.y.mul_assign(rhs.y);
1548 self.z.mul_assign(rhs.z);
1549 }
1550}
1551
1552impl MulAssign<&Self> for Vec3 {
1553 #[inline]
1554 fn mul_assign(&mut self, rhs: &Self) {
1555 self.mul_assign(*rhs);
1556 }
1557}
1558
1559impl Mul<f32> for Vec3 {
1560 type Output = Self;
1561 #[inline]
1562 fn mul(self, rhs: f32) -> Self {
1563 Self {
1564 x: self.x.mul(rhs),
1565 y: self.y.mul(rhs),
1566 z: self.z.mul(rhs),
1567 }
1568 }
1569}
1570
1571impl Mul<&f32> for Vec3 {
1572 type Output = Self;
1573 #[inline]
1574 fn mul(self, rhs: &f32) -> Self {
1575 self.mul(*rhs)
1576 }
1577}
1578
1579impl Mul<&f32> for &Vec3 {
1580 type Output = Vec3;
1581 #[inline]
1582 fn mul(self, rhs: &f32) -> Vec3 {
1583 (*self).mul(*rhs)
1584 }
1585}
1586
1587impl Mul<f32> for &Vec3 {
1588 type Output = Vec3;
1589 #[inline]
1590 fn mul(self, rhs: f32) -> Vec3 {
1591 (*self).mul(rhs)
1592 }
1593}
1594
1595impl MulAssign<f32> for Vec3 {
1596 #[inline]
1597 fn mul_assign(&mut self, rhs: f32) {
1598 self.x.mul_assign(rhs);
1599 self.y.mul_assign(rhs);
1600 self.z.mul_assign(rhs);
1601 }
1602}
1603
1604impl MulAssign<&f32> for Vec3 {
1605 #[inline]
1606 fn mul_assign(&mut self, rhs: &f32) {
1607 self.mul_assign(*rhs);
1608 }
1609}
1610
1611impl Mul<Vec3> for f32 {
1612 type Output = Vec3;
1613 #[inline]
1614 fn mul(self, rhs: Vec3) -> Vec3 {
1615 Vec3 {
1616 x: self.mul(rhs.x),
1617 y: self.mul(rhs.y),
1618 z: self.mul(rhs.z),
1619 }
1620 }
1621}
1622
1623impl Mul<&Vec3> for f32 {
1624 type Output = Vec3;
1625 #[inline]
1626 fn mul(self, rhs: &Vec3) -> Vec3 {
1627 self.mul(*rhs)
1628 }
1629}
1630
1631impl Mul<&Vec3> for &f32 {
1632 type Output = Vec3;
1633 #[inline]
1634 fn mul(self, rhs: &Vec3) -> Vec3 {
1635 (*self).mul(*rhs)
1636 }
1637}
1638
1639impl Mul<Vec3> for &f32 {
1640 type Output = Vec3;
1641 #[inline]
1642 fn mul(self, rhs: Vec3) -> Vec3 {
1643 (*self).mul(rhs)
1644 }
1645}
1646
1647impl Add for Vec3 {
1648 type Output = Self;
1649 #[inline]
1650 fn add(self, rhs: Self) -> Self {
1651 Self {
1652 x: self.x.add(rhs.x),
1653 y: self.y.add(rhs.y),
1654 z: self.z.add(rhs.z),
1655 }
1656 }
1657}
1658
1659impl Add<&Self> for Vec3 {
1660 type Output = Self;
1661 #[inline]
1662 fn add(self, rhs: &Self) -> Self {
1663 self.add(*rhs)
1664 }
1665}
1666
1667impl Add<&Vec3> for &Vec3 {
1668 type Output = Vec3;
1669 #[inline]
1670 fn add(self, rhs: &Vec3) -> Vec3 {
1671 (*self).add(*rhs)
1672 }
1673}
1674
1675impl Add<Vec3> for &Vec3 {
1676 type Output = Vec3;
1677 #[inline]
1678 fn add(self, rhs: Vec3) -> Vec3 {
1679 (*self).add(rhs)
1680 }
1681}
1682
1683impl AddAssign for Vec3 {
1684 #[inline]
1685 fn add_assign(&mut self, rhs: Self) {
1686 self.x.add_assign(rhs.x);
1687 self.y.add_assign(rhs.y);
1688 self.z.add_assign(rhs.z);
1689 }
1690}
1691
1692impl AddAssign<&Self> for Vec3 {
1693 #[inline]
1694 fn add_assign(&mut self, rhs: &Self) {
1695 self.add_assign(*rhs);
1696 }
1697}
1698
1699impl Add<f32> for Vec3 {
1700 type Output = Self;
1701 #[inline]
1702 fn add(self, rhs: f32) -> Self {
1703 Self {
1704 x: self.x.add(rhs),
1705 y: self.y.add(rhs),
1706 z: self.z.add(rhs),
1707 }
1708 }
1709}
1710
1711impl Add<&f32> for Vec3 {
1712 type Output = Self;
1713 #[inline]
1714 fn add(self, rhs: &f32) -> Self {
1715 self.add(*rhs)
1716 }
1717}
1718
1719impl Add<&f32> for &Vec3 {
1720 type Output = Vec3;
1721 #[inline]
1722 fn add(self, rhs: &f32) -> Vec3 {
1723 (*self).add(*rhs)
1724 }
1725}
1726
1727impl Add<f32> for &Vec3 {
1728 type Output = Vec3;
1729 #[inline]
1730 fn add(self, rhs: f32) -> Vec3 {
1731 (*self).add(rhs)
1732 }
1733}
1734
1735impl AddAssign<f32> for Vec3 {
1736 #[inline]
1737 fn add_assign(&mut self, rhs: f32) {
1738 self.x.add_assign(rhs);
1739 self.y.add_assign(rhs);
1740 self.z.add_assign(rhs);
1741 }
1742}
1743
1744impl AddAssign<&f32> for Vec3 {
1745 #[inline]
1746 fn add_assign(&mut self, rhs: &f32) {
1747 self.add_assign(*rhs);
1748 }
1749}
1750
1751impl Add<Vec3> for f32 {
1752 type Output = Vec3;
1753 #[inline]
1754 fn add(self, rhs: Vec3) -> Vec3 {
1755 Vec3 {
1756 x: self.add(rhs.x),
1757 y: self.add(rhs.y),
1758 z: self.add(rhs.z),
1759 }
1760 }
1761}
1762
1763impl Add<&Vec3> for f32 {
1764 type Output = Vec3;
1765 #[inline]
1766 fn add(self, rhs: &Vec3) -> Vec3 {
1767 self.add(*rhs)
1768 }
1769}
1770
1771impl Add<&Vec3> for &f32 {
1772 type Output = Vec3;
1773 #[inline]
1774 fn add(self, rhs: &Vec3) -> Vec3 {
1775 (*self).add(*rhs)
1776 }
1777}
1778
1779impl Add<Vec3> for &f32 {
1780 type Output = Vec3;
1781 #[inline]
1782 fn add(self, rhs: Vec3) -> Vec3 {
1783 (*self).add(rhs)
1784 }
1785}
1786
1787impl Sub for Vec3 {
1788 type Output = Self;
1789 #[inline]
1790 fn sub(self, rhs: Self) -> Self {
1791 Self {
1792 x: self.x.sub(rhs.x),
1793 y: self.y.sub(rhs.y),
1794 z: self.z.sub(rhs.z),
1795 }
1796 }
1797}
1798
1799impl Sub<&Self> for Vec3 {
1800 type Output = Self;
1801 #[inline]
1802 fn sub(self, rhs: &Self) -> Self {
1803 self.sub(*rhs)
1804 }
1805}
1806
1807impl Sub<&Vec3> for &Vec3 {
1808 type Output = Vec3;
1809 #[inline]
1810 fn sub(self, rhs: &Vec3) -> Vec3 {
1811 (*self).sub(*rhs)
1812 }
1813}
1814
1815impl Sub<Vec3> for &Vec3 {
1816 type Output = Vec3;
1817 #[inline]
1818 fn sub(self, rhs: Vec3) -> Vec3 {
1819 (*self).sub(rhs)
1820 }
1821}
1822
1823impl SubAssign for Vec3 {
1824 #[inline]
1825 fn sub_assign(&mut self, rhs: Self) {
1826 self.x.sub_assign(rhs.x);
1827 self.y.sub_assign(rhs.y);
1828 self.z.sub_assign(rhs.z);
1829 }
1830}
1831
1832impl SubAssign<&Self> for Vec3 {
1833 #[inline]
1834 fn sub_assign(&mut self, rhs: &Self) {
1835 self.sub_assign(*rhs);
1836 }
1837}
1838
1839impl Sub<f32> for Vec3 {
1840 type Output = Self;
1841 #[inline]
1842 fn sub(self, rhs: f32) -> Self {
1843 Self {
1844 x: self.x.sub(rhs),
1845 y: self.y.sub(rhs),
1846 z: self.z.sub(rhs),
1847 }
1848 }
1849}
1850
1851impl Sub<&f32> for Vec3 {
1852 type Output = Self;
1853 #[inline]
1854 fn sub(self, rhs: &f32) -> Self {
1855 self.sub(*rhs)
1856 }
1857}
1858
1859impl Sub<&f32> for &Vec3 {
1860 type Output = Vec3;
1861 #[inline]
1862 fn sub(self, rhs: &f32) -> Vec3 {
1863 (*self).sub(*rhs)
1864 }
1865}
1866
1867impl Sub<f32> for &Vec3 {
1868 type Output = Vec3;
1869 #[inline]
1870 fn sub(self, rhs: f32) -> Vec3 {
1871 (*self).sub(rhs)
1872 }
1873}
1874
1875impl SubAssign<f32> for Vec3 {
1876 #[inline]
1877 fn sub_assign(&mut self, rhs: f32) {
1878 self.x.sub_assign(rhs);
1879 self.y.sub_assign(rhs);
1880 self.z.sub_assign(rhs);
1881 }
1882}
1883
1884impl SubAssign<&f32> for Vec3 {
1885 #[inline]
1886 fn sub_assign(&mut self, rhs: &f32) {
1887 self.sub_assign(*rhs);
1888 }
1889}
1890
1891impl Sub<Vec3> for f32 {
1892 type Output = Vec3;
1893 #[inline]
1894 fn sub(self, rhs: Vec3) -> Vec3 {
1895 Vec3 {
1896 x: self.sub(rhs.x),
1897 y: self.sub(rhs.y),
1898 z: self.sub(rhs.z),
1899 }
1900 }
1901}
1902
1903impl Sub<&Vec3> for f32 {
1904 type Output = Vec3;
1905 #[inline]
1906 fn sub(self, rhs: &Vec3) -> Vec3 {
1907 self.sub(*rhs)
1908 }
1909}
1910
1911impl Sub<&Vec3> for &f32 {
1912 type Output = Vec3;
1913 #[inline]
1914 fn sub(self, rhs: &Vec3) -> Vec3 {
1915 (*self).sub(*rhs)
1916 }
1917}
1918
1919impl Sub<Vec3> for &f32 {
1920 type Output = Vec3;
1921 #[inline]
1922 fn sub(self, rhs: Vec3) -> Vec3 {
1923 (*self).sub(rhs)
1924 }
1925}
1926
1927impl Rem for Vec3 {
1928 type Output = Self;
1929 #[inline]
1930 fn rem(self, rhs: Self) -> Self {
1931 Self {
1932 x: self.x.rem(rhs.x),
1933 y: self.y.rem(rhs.y),
1934 z: self.z.rem(rhs.z),
1935 }
1936 }
1937}
1938
1939impl Rem<&Self> for Vec3 {
1940 type Output = Self;
1941 #[inline]
1942 fn rem(self, rhs: &Self) -> Self {
1943 self.rem(*rhs)
1944 }
1945}
1946
1947impl Rem<&Vec3> for &Vec3 {
1948 type Output = Vec3;
1949 #[inline]
1950 fn rem(self, rhs: &Vec3) -> Vec3 {
1951 (*self).rem(*rhs)
1952 }
1953}
1954
1955impl Rem<Vec3> for &Vec3 {
1956 type Output = Vec3;
1957 #[inline]
1958 fn rem(self, rhs: Vec3) -> Vec3 {
1959 (*self).rem(rhs)
1960 }
1961}
1962
1963impl RemAssign for Vec3 {
1964 #[inline]
1965 fn rem_assign(&mut self, rhs: Self) {
1966 self.x.rem_assign(rhs.x);
1967 self.y.rem_assign(rhs.y);
1968 self.z.rem_assign(rhs.z);
1969 }
1970}
1971
1972impl RemAssign<&Self> for Vec3 {
1973 #[inline]
1974 fn rem_assign(&mut self, rhs: &Self) {
1975 self.rem_assign(*rhs);
1976 }
1977}
1978
1979impl Rem<f32> for Vec3 {
1980 type Output = Self;
1981 #[inline]
1982 fn rem(self, rhs: f32) -> Self {
1983 Self {
1984 x: self.x.rem(rhs),
1985 y: self.y.rem(rhs),
1986 z: self.z.rem(rhs),
1987 }
1988 }
1989}
1990
1991impl Rem<&f32> for Vec3 {
1992 type Output = Self;
1993 #[inline]
1994 fn rem(self, rhs: &f32) -> Self {
1995 self.rem(*rhs)
1996 }
1997}
1998
1999impl Rem<&f32> for &Vec3 {
2000 type Output = Vec3;
2001 #[inline]
2002 fn rem(self, rhs: &f32) -> Vec3 {
2003 (*self).rem(*rhs)
2004 }
2005}
2006
2007impl Rem<f32> for &Vec3 {
2008 type Output = Vec3;
2009 #[inline]
2010 fn rem(self, rhs: f32) -> Vec3 {
2011 (*self).rem(rhs)
2012 }
2013}
2014
2015impl RemAssign<f32> for Vec3 {
2016 #[inline]
2017 fn rem_assign(&mut self, rhs: f32) {
2018 self.x.rem_assign(rhs);
2019 self.y.rem_assign(rhs);
2020 self.z.rem_assign(rhs);
2021 }
2022}
2023
2024impl RemAssign<&f32> for Vec3 {
2025 #[inline]
2026 fn rem_assign(&mut self, rhs: &f32) {
2027 self.rem_assign(*rhs);
2028 }
2029}
2030
2031impl Rem<Vec3> for f32 {
2032 type Output = Vec3;
2033 #[inline]
2034 fn rem(self, rhs: Vec3) -> Vec3 {
2035 Vec3 {
2036 x: self.rem(rhs.x),
2037 y: self.rem(rhs.y),
2038 z: self.rem(rhs.z),
2039 }
2040 }
2041}
2042
2043impl Rem<&Vec3> for f32 {
2044 type Output = Vec3;
2045 #[inline]
2046 fn rem(self, rhs: &Vec3) -> Vec3 {
2047 self.rem(*rhs)
2048 }
2049}
2050
2051impl Rem<&Vec3> for &f32 {
2052 type Output = Vec3;
2053 #[inline]
2054 fn rem(self, rhs: &Vec3) -> Vec3 {
2055 (*self).rem(*rhs)
2056 }
2057}
2058
2059impl Rem<Vec3> for &f32 {
2060 type Output = Vec3;
2061 #[inline]
2062 fn rem(self, rhs: Vec3) -> Vec3 {
2063 (*self).rem(rhs)
2064 }
2065}
2066
2067impl AsRef<[f32; 3]> for Vec3 {
2068 #[inline]
2069 fn as_ref(&self) -> &[f32; 3] {
2070 unsafe { &*(self as *const Self as *const [f32; 3]) }
2071 }
2072}
2073
2074impl AsMut<[f32; 3]> for Vec3 {
2075 #[inline]
2076 fn as_mut(&mut self) -> &mut [f32; 3] {
2077 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2078 }
2079}
2080
2081impl Sum for Vec3 {
2082 #[inline]
2083 fn sum<I>(iter: I) -> Self
2084 where
2085 I: Iterator<Item = Self>,
2086 {
2087 iter.fold(Self::ZERO, Self::add)
2088 }
2089}
2090
2091impl<'a> Sum<&'a Self> for Vec3 {
2092 #[inline]
2093 fn sum<I>(iter: I) -> Self
2094 where
2095 I: Iterator<Item = &'a Self>,
2096 {
2097 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2098 }
2099}
2100
2101impl Product for Vec3 {
2102 #[inline]
2103 fn product<I>(iter: I) -> Self
2104 where
2105 I: Iterator<Item = Self>,
2106 {
2107 iter.fold(Self::ONE, Self::mul)
2108 }
2109}
2110
2111impl<'a> Product<&'a Self> for Vec3 {
2112 #[inline]
2113 fn product<I>(iter: I) -> Self
2114 where
2115 I: Iterator<Item = &'a Self>,
2116 {
2117 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2118 }
2119}
2120
2121impl Neg for Vec3 {
2122 type Output = Self;
2123 #[inline]
2124 fn neg(self) -> Self {
2125 Self {
2126 x: self.x.neg(),
2127 y: self.y.neg(),
2128 z: self.z.neg(),
2129 }
2130 }
2131}
2132
2133impl Neg for &Vec3 {
2134 type Output = Vec3;
2135 #[inline]
2136 fn neg(self) -> Vec3 {
2137 (*self).neg()
2138 }
2139}
2140
2141impl Index<usize> for Vec3 {
2142 type Output = f32;
2143 #[inline]
2144 fn index(&self, index: usize) -> &Self::Output {
2145 match index {
2146 0 => &self.x,
2147 1 => &self.y,
2148 2 => &self.z,
2149 _ => panic!("index out of bounds"),
2150 }
2151 }
2152}
2153
2154impl IndexMut<usize> for Vec3 {
2155 #[inline]
2156 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2157 match index {
2158 0 => &mut self.x,
2159 1 => &mut self.y,
2160 2 => &mut self.z,
2161 _ => panic!("index out of bounds"),
2162 }
2163 }
2164}
2165
2166impl fmt::Display for Vec3 {
2167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2168 if let Some(p) = f.precision() {
2169 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2170 } else {
2171 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2172 }
2173 }
2174}
2175
2176impl fmt::Debug for Vec3 {
2177 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2178 fmt.debug_tuple(stringify!(Vec3))
2179 .field(&self.x)
2180 .field(&self.y)
2181 .field(&self.z)
2182 .finish()
2183 }
2184}
2185
2186impl From<[f32; 3]> for Vec3 {
2187 #[inline]
2188 fn from(a: [f32; 3]) -> Self {
2189 Self::new(a[0], a[1], a[2])
2190 }
2191}
2192
2193impl From<Vec3> for [f32; 3] {
2194 #[inline]
2195 fn from(v: Vec3) -> Self {
2196 [v.x, v.y, v.z]
2197 }
2198}
2199
2200impl From<(f32, f32, f32)> for Vec3 {
2201 #[inline]
2202 fn from(t: (f32, f32, f32)) -> Self {
2203 Self::new(t.0, t.1, t.2)
2204 }
2205}
2206
2207impl From<Vec3> for (f32, f32, f32) {
2208 #[inline]
2209 fn from(v: Vec3) -> Self {
2210 (v.x, v.y, v.z)
2211 }
2212}
2213
2214impl From<(Vec2, f32)> for Vec3 {
2215 #[inline]
2216 fn from((v, z): (Vec2, f32)) -> Self {
2217 Self::new(v.x, v.y, z)
2218 }
2219}
2220
2221impl From<BVec3> for Vec3 {
2222 #[inline]
2223 fn from(v: BVec3) -> Self {
2224 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2225 }
2226}
2227
2228impl From<BVec3A> for Vec3 {
2229 #[inline]
2230 fn from(v: BVec3A) -> Self {
2231 let bool_array: [bool; 3] = v.into();
2232 Self::new(
2233 f32::from(bool_array[0]),
2234 f32::from(bool_array[1]),
2235 f32::from(bool_array[2]),
2236 )
2237 }
2238}