1use crate::{f32::math, BVec3, BVec3A, Quat, Vec2, Vec3A, Vec4};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::*;
8
9#[cfg(feature = "zerocopy-08")]
10use zerocopy_derive_08::*;
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-08",
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::new(v, v, 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::new(
126 if mask.test(0) { if_true.x } else { if_false.x },
127 if mask.test(1) { if_true.y } else { if_false.y },
128 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 #[track_caller]
154 pub const fn from_slice(slice: &[f32]) -> Self {
155 assert!(slice.len() >= 3);
156 Self::new(slice[0], slice[1], slice[2])
157 }
158
159 #[inline]
165 #[track_caller]
166 pub fn write_to_slice(self, slice: &mut [f32]) {
167 slice[..3].copy_from_slice(&self.to_array());
168 }
169
170 #[allow(dead_code)]
172 #[inline]
173 #[must_use]
174 pub(crate) fn from_vec4(v: Vec4) -> Self {
175 Self::new(v.x, v.y, v.z)
176 }
177
178 #[inline]
180 #[must_use]
181 pub fn extend(self, w: f32) -> Vec4 {
182 Vec4::new(self.x, self.y, self.z, w)
183 }
184
185 #[inline]
189 #[must_use]
190 pub fn truncate(self) -> Vec2 {
191 use crate::swizzles::Vec3Swizzles;
192 self.xy()
193 }
194
195 #[inline]
201 #[must_use]
202 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
203 pub fn from_homogeneous(v: Vec4) -> Self {
204 glam_assert!(v.w != 0.0);
205 Self::from_vec4(v) / v.w
206 }
207
208 #[inline]
210 #[must_use]
211 pub fn to_homogeneous(self) -> Vec4 {
212 self.extend(1.0)
213 }
214
215 #[inline]
217 #[must_use]
218 pub fn to_vec3a(self) -> Vec3A {
219 Vec3A::from(self)
220 }
221
222 #[inline]
224 #[must_use]
225 pub fn with_x(mut self, x: f32) -> Self {
226 self.x = x;
227 self
228 }
229
230 #[inline]
232 #[must_use]
233 pub fn with_y(mut self, y: f32) -> Self {
234 self.y = y;
235 self
236 }
237
238 #[inline]
240 #[must_use]
241 pub fn with_z(mut self, z: f32) -> Self {
242 self.z = z;
243 self
244 }
245
246 #[inline]
248 #[must_use]
249 pub fn dot(self, rhs: Self) -> f32 {
250 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
251 }
252
253 #[inline]
255 #[must_use]
256 pub fn dot_into_vec(self, rhs: Self) -> Self {
257 Self::splat(self.dot(rhs))
258 }
259
260 #[inline]
262 #[must_use]
263 pub fn cross(self, rhs: Self) -> Self {
264 Self::new(
265 self.y * rhs.z - rhs.y * self.z,
266 self.z * rhs.x - rhs.z * self.x,
267 self.x * rhs.y - rhs.x * self.y,
268 )
269 }
270
271 #[inline]
278 #[must_use]
279 pub fn min(self, rhs: Self) -> Self {
280 Self::new(
281 if self.x < rhs.x { self.x } else { rhs.x },
282 if self.y < rhs.y { self.y } else { rhs.y },
283 if self.z < rhs.z { self.z } else { rhs.z },
284 )
285 }
286
287 #[inline]
294 #[must_use]
295 pub fn max(self, rhs: Self) -> Self {
296 Self::new(
297 if self.x > rhs.x { self.x } else { rhs.x },
298 if self.y > rhs.y { self.y } else { rhs.y },
299 if self.z > rhs.z { self.z } else { rhs.z },
300 )
301 }
302
303 #[inline]
314 #[must_use]
315 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
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::new(math::abs(self.x), math::abs(self.y), math::abs(self.z))
470 }
471
472 #[inline]
478 #[must_use]
479 pub fn signum(self) -> Self {
480 Self::new(
481 math::signum(self.x),
482 math::signum(self.y),
483 math::signum(self.z),
484 )
485 }
486
487 #[inline]
489 #[must_use]
490 pub fn copysign(self, rhs: Self) -> Self {
491 Self::new(
492 math::copysign(self.x, rhs.x),
493 math::copysign(self.y, rhs.y),
494 math::copysign(self.z, rhs.z),
495 )
496 }
497
498 #[inline]
506 #[must_use]
507 pub fn is_negative_bitmask(self) -> u32 {
508 (self.x.is_sign_negative() as u32)
509 | ((self.y.is_sign_negative() as u32) << 1)
510 | ((self.z.is_sign_negative() as u32) << 2)
511 }
512
513 #[inline]
518 #[must_use]
519 pub fn is_negative_mask(self) -> BVec3 {
520 BVec3::new(
521 self.x.is_sign_negative(),
522 self.y.is_sign_negative(),
523 self.z.is_sign_negative(),
524 )
525 }
526
527 #[inline]
530 #[must_use]
531 pub fn is_finite(self) -> bool {
532 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
533 }
534
535 #[inline]
539 #[must_use]
540 pub fn is_finite_mask(self) -> BVec3 {
541 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
542 }
543
544 #[inline]
546 #[must_use]
547 pub fn is_nan(self) -> bool {
548 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
549 }
550
551 #[inline]
555 #[must_use]
556 pub fn is_nan_mask(self) -> BVec3 {
557 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
558 }
559
560 #[doc(alias = "magnitude")]
562 #[inline]
563 #[must_use]
564 pub fn length(self) -> f32 {
565 math::sqrt(self.dot(self))
566 }
567
568 #[allow(dead_code)]
570 fn is_non_zero(self) -> bool {
571 self.length_squared() > 0.0
572 }
573
574 #[doc(alias = "magnitude2")]
578 #[inline]
579 #[must_use]
580 pub fn length_squared(self) -> f32 {
581 self.dot(self)
582 }
583
584 #[inline]
588 #[must_use]
589 pub fn length_recip(self) -> f32 {
590 1.0 / self.length()
591 }
592
593 #[inline]
595 #[must_use]
596 pub fn distance(self, rhs: Self) -> f32 {
597 (self - rhs).length()
598 }
599
600 #[inline]
602 #[must_use]
603 pub fn distance_squared(self, rhs: Self) -> f32 {
604 (self - rhs).length_squared()
605 }
606
607 #[inline]
609 #[must_use]
610 pub fn div_euclid(self, rhs: Self) -> Self {
611 Self::new(
612 math::div_euclid(self.x, rhs.x),
613 math::div_euclid(self.y, rhs.y),
614 math::div_euclid(self.z, rhs.z),
615 )
616 }
617
618 #[inline]
622 #[must_use]
623 pub fn rem_euclid(self, rhs: Self) -> Self {
624 Self::new(
625 math::rem_euclid(self.x, rhs.x),
626 math::rem_euclid(self.y, rhs.y),
627 math::rem_euclid(self.z, rhs.z),
628 )
629 }
630
631 #[inline]
641 #[must_use]
642 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
643 pub fn normalize(self) -> Self {
644 #[allow(clippy::let_and_return)]
645 let normalized = self.mul(self.length_recip());
646 glam_assert!(normalized.is_finite());
647 normalized
648 }
649
650 #[inline]
657 #[must_use]
658 pub fn try_normalize(self) -> Option<Self> {
659 let rcp = self.length_recip();
660 if rcp.is_finite() && rcp > 0.0 {
661 Some(self * rcp)
662 } else {
663 None
664 }
665 }
666
667 #[inline]
675 #[must_use]
676 pub fn normalize_or(self, fallback: Self) -> Self {
677 let rcp = self.length_recip();
678 if rcp.is_finite() && rcp > 0.0 {
679 self * rcp
680 } else {
681 fallback
682 }
683 }
684
685 #[inline]
692 #[must_use]
693 pub fn normalize_or_zero(self) -> Self {
694 self.normalize_or(Self::ZERO)
695 }
696
697 #[inline]
701 #[must_use]
702 pub fn normalize_and_length(self) -> (Self, f32) {
703 let length = self.length();
704 let rcp = 1.0 / length;
705 if rcp.is_finite() && rcp > 0.0 {
706 (self * rcp, length)
707 } else {
708 (Self::X, 0.0)
709 }
710 }
711
712 #[inline]
716 #[must_use]
717 pub fn is_normalized(self) -> bool {
718 math::abs(self.length_squared() - 1.0) <= 2e-4
719 }
720
721 #[inline]
729 #[must_use]
730 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
731 pub fn project_onto(self, rhs: Self) -> Self {
732 let rhs_len_sq = rhs.dot(rhs);
733 glam_assert!(rhs_len_sq != 0.0);
734 rhs * (self.dot(rhs) / rhs_len_sq)
735 }
736
737 #[doc(alias("plane"))]
748 #[inline]
749 #[must_use]
750 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
751 pub fn reject_from(self, rhs: Self) -> Self {
752 self - self.project_onto(rhs)
753 }
754
755 #[inline]
763 #[must_use]
764 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
765 pub fn project_onto_normalized(self, rhs: Self) -> Self {
766 glam_assert!(rhs.is_normalized());
767 rhs * self.dot(rhs)
768 }
769
770 #[doc(alias("plane"))]
781 #[inline]
782 #[must_use]
783 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
784 pub fn reject_from_normalized(self, rhs: Self) -> Self {
785 self - self.project_onto_normalized(rhs)
786 }
787
788 #[inline]
791 #[must_use]
792 pub fn round(self) -> Self {
793 Self::new(
794 math::round(self.x),
795 math::round(self.y),
796 math::round(self.z),
797 )
798 }
799
800 #[inline]
803 #[must_use]
804 pub fn floor(self) -> Self {
805 Self::new(
806 math::floor(self.x),
807 math::floor(self.y),
808 math::floor(self.z),
809 )
810 }
811
812 #[inline]
815 #[must_use]
816 pub fn ceil(self) -> Self {
817 Self::new(math::ceil(self.x), math::ceil(self.y), math::ceil(self.z))
818 }
819
820 #[inline]
823 #[must_use]
824 pub fn trunc(self) -> Self {
825 Self::new(
826 math::trunc(self.x),
827 math::trunc(self.y),
828 math::trunc(self.z),
829 )
830 }
831
832 #[inline]
836 #[must_use]
837 pub fn step(self, rhs: Self) -> Self {
838 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
839 }
840
841 #[inline]
852 #[must_use]
853 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
854 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
855 glam_assert!(edge0.cmplt(edge1).all());
856 let t = ((self - edge0) / (edge1 - edge0)).saturate();
857 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
858 }
859
860 #[inline]
862 #[must_use]
863 pub fn saturate(self) -> Self {
864 self.clamp(Self::ZERO, Self::ONE)
865 }
866
867 #[inline]
874 #[must_use]
875 pub fn fract(self) -> Self {
876 self - self.trunc()
877 }
878
879 #[inline]
886 #[must_use]
887 pub fn fract_gl(self) -> Self {
888 self - self.floor()
889 }
890
891 #[inline]
894 #[must_use]
895 pub fn exp(self) -> Self {
896 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
897 }
898
899 #[inline]
901 #[must_use]
902 pub fn exp2(self) -> Self {
903 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
904 }
905
906 #[inline]
909 #[must_use]
910 pub fn ln(self) -> Self {
911 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
912 }
913
914 #[inline]
917 #[must_use]
918 pub fn log2(self) -> Self {
919 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
920 }
921
922 #[inline]
924 #[must_use]
925 pub fn powf(self, n: f32) -> Self {
926 Self::new(
927 math::powf(self.x, n),
928 math::powf(self.y, n),
929 math::powf(self.z, n),
930 )
931 }
932
933 #[inline]
936 #[must_use]
937 pub fn sqrt(self) -> Self {
938 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
939 }
940
941 #[inline]
943 #[must_use]
944 pub fn cos(self) -> Self {
945 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
946 }
947
948 #[inline]
950 #[must_use]
951 pub fn sin(self) -> Self {
952 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
953 }
954
955 #[inline]
957 #[must_use]
958 pub fn sin_cos(self) -> (Self, Self) {
959 let (sin_x, cos_x) = math::sin_cos(self.x);
960 let (sin_y, cos_y) = math::sin_cos(self.y);
961 let (sin_z, cos_z) = math::sin_cos(self.z);
962
963 (
964 Self::new(sin_x, sin_y, sin_z),
965 Self::new(cos_x, cos_y, cos_z),
966 )
967 }
968
969 #[inline]
971 #[must_use]
972 pub fn recip(self) -> Self {
973 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
974 }
975
976 #[doc(alias = "mix")]
988 #[inline]
989 #[must_use]
990 pub fn lerp(self, rhs: Self, s: f32) -> Self {
991 self * (1.0 - s) + rhs * s
992 }
993
994 #[doc(alias = "mix")]
1011 #[inline]
1012 #[must_use]
1013 pub fn lerp_monotonic(self, rhs: Self, s: f32) -> Self {
1014 self + (rhs - self) * s
1015 }
1016
1017 #[inline]
1022 #[must_use]
1023 pub fn move_towards(self, rhs: Self, d: f32) -> Self {
1024 let a = rhs - self;
1025 let len = a.length();
1026 if len <= d || len <= 1e-4 {
1027 return rhs;
1028 }
1029 self + a / len * d
1030 }
1031
1032 #[inline]
1038 pub fn midpoint(self, rhs: Self) -> Self {
1039 (self + rhs) * 0.5
1040 }
1041
1042 #[inline]
1052 #[must_use]
1053 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
1054 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1055 }
1056
1057 #[inline]
1063 #[must_use]
1064 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1065 pub fn clamp_length(self, min: f32, max: f32) -> Self {
1066 glam_assert!(0.0 <= min);
1067 glam_assert!(min <= max);
1068 let length_sq = self.length_squared();
1069 if length_sq < min * min {
1070 min * (self / math::sqrt(length_sq))
1071 } else if length_sq > max * max {
1072 max * (self / math::sqrt(length_sq))
1073 } else {
1074 self
1075 }
1076 }
1077
1078 #[inline]
1084 #[must_use]
1085 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1086 pub fn clamp_length_max(self, max: f32) -> Self {
1087 glam_assert!(0.0 <= max);
1088 let length_sq = self.length_squared();
1089 if length_sq > max * max {
1090 max * (self / math::sqrt(length_sq))
1091 } else {
1092 self
1093 }
1094 }
1095
1096 #[inline]
1102 #[must_use]
1103 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1104 pub fn clamp_length_min(self, min: f32) -> Self {
1105 glam_assert!(0.0 <= min);
1106 let length_sq = self.length_squared();
1107 if length_sq < min * min {
1108 min * (self / math::sqrt(length_sq))
1109 } else {
1110 self
1111 }
1112 }
1113
1114 #[inline]
1122 #[must_use]
1123 pub fn mul_add(self, a: Self, b: Self) -> Self {
1124 Self::new(
1125 math::mul_add(self.x, a.x, b.x),
1126 math::mul_add(self.y, a.y, b.y),
1127 math::mul_add(self.z, a.z, b.z),
1128 )
1129 }
1130
1131 #[inline]
1140 #[must_use]
1141 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1142 pub fn reflect(self, normal: Self) -> Self {
1143 glam_assert!(normal.is_normalized());
1144 self - 2.0 * self.dot(normal) * normal
1145 }
1146
1147 #[inline]
1157 #[must_use]
1158 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1159 pub fn refract(self, normal: Self, eta: f32) -> Self {
1160 glam_assert!(self.is_normalized());
1161 glam_assert!(normal.is_normalized());
1162 let n_dot_i = normal.dot(self);
1163 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1164 if k >= 0.0 {
1165 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1166 } else {
1167 Self::ZERO
1168 }
1169 }
1170
1171 #[inline]
1182 #[must_use]
1183 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1184 pub fn angle_between(self, rhs: Self) -> f32 {
1185 glam_assert!(self.is_non_zero());
1186 glam_assert!(rhs.is_non_zero());
1187 math::acos_approx(
1188 self.dot(rhs)
1189 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1190 )
1191 }
1192
1193 #[doc(alias = "signed_angle")]
1209 #[inline]
1210 #[must_use]
1211 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1212 pub fn angle_to(self, rhs: Self, axis: Self) -> f32 {
1213 glam_assert!(axis.is_normalized());
1214 glam_assert!(self.is_non_zero());
1215 glam_assert!(rhs.is_non_zero());
1216 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1217 }
1218
1219 #[inline]
1221 #[must_use]
1222 pub fn rotate_x(self, angle: f32) -> Self {
1223 let (sina, cosa) = math::sin_cos(angle);
1224 Self::new(
1225 self.x,
1226 self.y * cosa - self.z * sina,
1227 self.y * sina + self.z * cosa,
1228 )
1229 }
1230
1231 #[inline]
1233 #[must_use]
1234 pub fn rotate_y(self, angle: f32) -> Self {
1235 let (sina, cosa) = math::sin_cos(angle);
1236 Self::new(
1237 self.x * cosa + self.z * sina,
1238 self.y,
1239 self.x * -sina + self.z * cosa,
1240 )
1241 }
1242
1243 #[inline]
1245 #[must_use]
1246 pub fn rotate_z(self, angle: f32) -> Self {
1247 let (sina, cosa) = math::sin_cos(angle);
1248 Self::new(
1249 self.x * cosa - self.y * sina,
1250 self.x * sina + self.y * cosa,
1251 self.z,
1252 )
1253 }
1254
1255 #[inline]
1263 #[must_use]
1264 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1265 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1266 Quat::from_axis_angle(axis, angle) * self
1267 }
1268
1269 #[inline]
1279 #[must_use]
1280 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1281 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1282 let angle_between = self.angle_between(rhs);
1283 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1285 let axis = self
1286 .cross(rhs)
1287 .try_normalize()
1288 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1289 Quat::from_axis_angle(axis, angle) * self
1290 }
1291
1292 #[inline]
1299 #[must_use]
1300 pub fn any_orthogonal_vector(self) -> Self {
1301 if math::abs(self.x) > math::abs(self.y) {
1303 Self::new(-self.z, 0.0, self.x) } else {
1305 Self::new(0.0, self.z, -self.y) }
1307 }
1308
1309 #[inline]
1317 #[must_use]
1318 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1319 pub fn any_orthonormal_vector(self) -> Self {
1320 glam_assert!(self.is_normalized());
1321 let sign = math::signum(self.z);
1323 let a = -1.0 / (sign + self.z);
1324 let b = self.x * self.y * a;
1325 Self::new(b, sign + self.y * self.y * a, -self.y)
1326 }
1327
1328 #[inline]
1335 #[must_use]
1336 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1337 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1338 glam_assert!(self.is_normalized());
1339 let sign = math::signum(self.z);
1341 let a = -1.0 / (sign + self.z);
1342 let b = self.x * self.y * a;
1343 (
1344 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1345 Self::new(b, sign + self.y * self.y * a, -self.y),
1346 )
1347 }
1348
1349 #[inline]
1359 #[must_use]
1360 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1361 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1362 glam_assert!(self.is_non_zero());
1363 glam_assert!(rhs.is_non_zero());
1364 let self_length = self.length();
1365 let rhs_length = rhs.length();
1366 let dot = self.dot(rhs) / (self_length * rhs_length);
1368 if math::abs(dot) < 1.0 - 3e-7 {
1370 let theta = math::acos_approx(dot);
1372 let sin_theta = math::sin(theta);
1374 let t1 = math::sin(theta * (1.0 - s));
1375 let t2 = math::sin(theta * s);
1376
1377 let result_length = self_length + (rhs_length - self_length) * s;
1379 return (self * (result_length / self_length) * t1
1381 + rhs * (result_length / rhs_length) * t2)
1382 * (1.0 / sin_theta);
1383 }
1384 if dot < 0.0 {
1385 let axis = self.any_orthogonal_vector().normalize();
1389 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1390 let result_length = self_length + (rhs_length - self_length) * s;
1392 rotation * self * (result_length / self_length)
1393 } else {
1394 self.lerp(rhs, s)
1396 }
1397 }
1398
1399 #[cfg(feature = "f64")]
1401 #[inline]
1402 #[must_use]
1403 pub fn as_dvec3(self) -> crate::DVec3 {
1404 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1405 }
1406
1407 #[cfg(feature = "i8")]
1409 #[inline]
1410 #[must_use]
1411 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1412 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1413 }
1414
1415 #[cfg(feature = "u8")]
1417 #[inline]
1418 #[must_use]
1419 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1420 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1421 }
1422
1423 #[cfg(feature = "i16")]
1425 #[inline]
1426 #[must_use]
1427 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1428 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1429 }
1430
1431 #[cfg(feature = "u16")]
1433 #[inline]
1434 #[must_use]
1435 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1436 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1437 }
1438
1439 #[cfg(feature = "i32")]
1441 #[inline]
1442 #[must_use]
1443 pub fn as_ivec3(self) -> crate::IVec3 {
1444 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1445 }
1446
1447 #[cfg(feature = "u32")]
1449 #[inline]
1450 #[must_use]
1451 pub fn as_uvec3(self) -> crate::UVec3 {
1452 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1453 }
1454
1455 #[cfg(feature = "i64")]
1457 #[inline]
1458 #[must_use]
1459 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1460 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1461 }
1462
1463 #[cfg(feature = "u64")]
1465 #[inline]
1466 #[must_use]
1467 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1468 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1469 }
1470
1471 #[cfg(feature = "isize")]
1473 #[inline]
1474 #[must_use]
1475 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1476 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1477 }
1478
1479 #[cfg(feature = "usize")]
1481 #[inline]
1482 #[must_use]
1483 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1484 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1485 }
1486}
1487
1488impl Default for Vec3 {
1489 #[inline(always)]
1490 fn default() -> Self {
1491 Self::ZERO
1492 }
1493}
1494
1495impl Div for Vec3 {
1496 type Output = Self;
1497 #[inline]
1498 fn div(self, rhs: Self) -> Self {
1499 Self::new(self.x.div(rhs.x), self.y.div(rhs.y), self.z.div(rhs.z))
1500 }
1501}
1502
1503impl Div<&Self> for Vec3 {
1504 type Output = Self;
1505 #[inline]
1506 fn div(self, rhs: &Self) -> Self {
1507 self.div(*rhs)
1508 }
1509}
1510
1511impl Div<&Vec3> for &Vec3 {
1512 type Output = Vec3;
1513 #[inline]
1514 fn div(self, rhs: &Vec3) -> Vec3 {
1515 (*self).div(*rhs)
1516 }
1517}
1518
1519impl Div<Vec3> for &Vec3 {
1520 type Output = Vec3;
1521 #[inline]
1522 fn div(self, rhs: Vec3) -> Vec3 {
1523 (*self).div(rhs)
1524 }
1525}
1526
1527impl DivAssign for Vec3 {
1528 #[inline]
1529 fn div_assign(&mut self, rhs: Self) {
1530 self.x.div_assign(rhs.x);
1531 self.y.div_assign(rhs.y);
1532 self.z.div_assign(rhs.z);
1533 }
1534}
1535
1536impl DivAssign<&Self> for Vec3 {
1537 #[inline]
1538 fn div_assign(&mut self, rhs: &Self) {
1539 self.div_assign(*rhs);
1540 }
1541}
1542
1543impl Div<f32> for Vec3 {
1544 type Output = Self;
1545 #[inline]
1546 fn div(self, rhs: f32) -> Self {
1547 Self::new(self.x.div(rhs), self.y.div(rhs), self.z.div(rhs))
1548 }
1549}
1550
1551impl Div<&f32> for Vec3 {
1552 type Output = Self;
1553 #[inline]
1554 fn div(self, rhs: &f32) -> Self {
1555 self.div(*rhs)
1556 }
1557}
1558
1559impl Div<&f32> for &Vec3 {
1560 type Output = Vec3;
1561 #[inline]
1562 fn div(self, rhs: &f32) -> Vec3 {
1563 (*self).div(*rhs)
1564 }
1565}
1566
1567impl Div<f32> for &Vec3 {
1568 type Output = Vec3;
1569 #[inline]
1570 fn div(self, rhs: f32) -> Vec3 {
1571 (*self).div(rhs)
1572 }
1573}
1574
1575impl DivAssign<f32> for Vec3 {
1576 #[inline]
1577 fn div_assign(&mut self, rhs: f32) {
1578 self.x.div_assign(rhs);
1579 self.y.div_assign(rhs);
1580 self.z.div_assign(rhs);
1581 }
1582}
1583
1584impl DivAssign<&f32> for Vec3 {
1585 #[inline]
1586 fn div_assign(&mut self, rhs: &f32) {
1587 self.div_assign(*rhs);
1588 }
1589}
1590
1591impl Div<Vec3> for f32 {
1592 type Output = Vec3;
1593 #[inline]
1594 fn div(self, rhs: Vec3) -> Vec3 {
1595 Vec3::new(self.div(rhs.x), self.div(rhs.y), self.div(rhs.z))
1596 }
1597}
1598
1599impl Div<&Vec3> for f32 {
1600 type Output = Vec3;
1601 #[inline]
1602 fn div(self, rhs: &Vec3) -> Vec3 {
1603 self.div(*rhs)
1604 }
1605}
1606
1607impl Div<&Vec3> for &f32 {
1608 type Output = Vec3;
1609 #[inline]
1610 fn div(self, rhs: &Vec3) -> Vec3 {
1611 (*self).div(*rhs)
1612 }
1613}
1614
1615impl Div<Vec3> for &f32 {
1616 type Output = Vec3;
1617 #[inline]
1618 fn div(self, rhs: Vec3) -> Vec3 {
1619 (*self).div(rhs)
1620 }
1621}
1622
1623impl Mul for Vec3 {
1624 type Output = Self;
1625 #[inline]
1626 fn mul(self, rhs: Self) -> Self {
1627 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y), self.z.mul(rhs.z))
1628 }
1629}
1630
1631impl Mul<&Self> for Vec3 {
1632 type Output = Self;
1633 #[inline]
1634 fn mul(self, rhs: &Self) -> Self {
1635 self.mul(*rhs)
1636 }
1637}
1638
1639impl Mul<&Vec3> for &Vec3 {
1640 type Output = Vec3;
1641 #[inline]
1642 fn mul(self, rhs: &Vec3) -> Vec3 {
1643 (*self).mul(*rhs)
1644 }
1645}
1646
1647impl Mul<Vec3> for &Vec3 {
1648 type Output = Vec3;
1649 #[inline]
1650 fn mul(self, rhs: Vec3) -> Vec3 {
1651 (*self).mul(rhs)
1652 }
1653}
1654
1655impl MulAssign for Vec3 {
1656 #[inline]
1657 fn mul_assign(&mut self, rhs: Self) {
1658 self.x.mul_assign(rhs.x);
1659 self.y.mul_assign(rhs.y);
1660 self.z.mul_assign(rhs.z);
1661 }
1662}
1663
1664impl MulAssign<&Self> for Vec3 {
1665 #[inline]
1666 fn mul_assign(&mut self, rhs: &Self) {
1667 self.mul_assign(*rhs);
1668 }
1669}
1670
1671impl Mul<f32> for Vec3 {
1672 type Output = Self;
1673 #[inline]
1674 fn mul(self, rhs: f32) -> Self {
1675 Self::new(self.x.mul(rhs), self.y.mul(rhs), self.z.mul(rhs))
1676 }
1677}
1678
1679impl Mul<&f32> for Vec3 {
1680 type Output = Self;
1681 #[inline]
1682 fn mul(self, rhs: &f32) -> Self {
1683 self.mul(*rhs)
1684 }
1685}
1686
1687impl Mul<&f32> for &Vec3 {
1688 type Output = Vec3;
1689 #[inline]
1690 fn mul(self, rhs: &f32) -> Vec3 {
1691 (*self).mul(*rhs)
1692 }
1693}
1694
1695impl Mul<f32> for &Vec3 {
1696 type Output = Vec3;
1697 #[inline]
1698 fn mul(self, rhs: f32) -> Vec3 {
1699 (*self).mul(rhs)
1700 }
1701}
1702
1703impl MulAssign<f32> for Vec3 {
1704 #[inline]
1705 fn mul_assign(&mut self, rhs: f32) {
1706 self.x.mul_assign(rhs);
1707 self.y.mul_assign(rhs);
1708 self.z.mul_assign(rhs);
1709 }
1710}
1711
1712impl MulAssign<&f32> for Vec3 {
1713 #[inline]
1714 fn mul_assign(&mut self, rhs: &f32) {
1715 self.mul_assign(*rhs);
1716 }
1717}
1718
1719impl Mul<Vec3> for f32 {
1720 type Output = Vec3;
1721 #[inline]
1722 fn mul(self, rhs: Vec3) -> Vec3 {
1723 Vec3::new(self.mul(rhs.x), self.mul(rhs.y), self.mul(rhs.z))
1724 }
1725}
1726
1727impl Mul<&Vec3> for f32 {
1728 type Output = Vec3;
1729 #[inline]
1730 fn mul(self, rhs: &Vec3) -> Vec3 {
1731 self.mul(*rhs)
1732 }
1733}
1734
1735impl Mul<&Vec3> for &f32 {
1736 type Output = Vec3;
1737 #[inline]
1738 fn mul(self, rhs: &Vec3) -> Vec3 {
1739 (*self).mul(*rhs)
1740 }
1741}
1742
1743impl Mul<Vec3> for &f32 {
1744 type Output = Vec3;
1745 #[inline]
1746 fn mul(self, rhs: Vec3) -> Vec3 {
1747 (*self).mul(rhs)
1748 }
1749}
1750
1751impl Add for Vec3 {
1752 type Output = Self;
1753 #[inline]
1754 fn add(self, rhs: Self) -> Self {
1755 Self::new(self.x.add(rhs.x), self.y.add(rhs.y), self.z.add(rhs.z))
1756 }
1757}
1758
1759impl Add<&Self> for Vec3 {
1760 type Output = Self;
1761 #[inline]
1762 fn add(self, rhs: &Self) -> Self {
1763 self.add(*rhs)
1764 }
1765}
1766
1767impl Add<&Vec3> for &Vec3 {
1768 type Output = Vec3;
1769 #[inline]
1770 fn add(self, rhs: &Vec3) -> Vec3 {
1771 (*self).add(*rhs)
1772 }
1773}
1774
1775impl Add<Vec3> for &Vec3 {
1776 type Output = Vec3;
1777 #[inline]
1778 fn add(self, rhs: Vec3) -> Vec3 {
1779 (*self).add(rhs)
1780 }
1781}
1782
1783impl AddAssign for Vec3 {
1784 #[inline]
1785 fn add_assign(&mut self, rhs: Self) {
1786 self.x.add_assign(rhs.x);
1787 self.y.add_assign(rhs.y);
1788 self.z.add_assign(rhs.z);
1789 }
1790}
1791
1792impl AddAssign<&Self> for Vec3 {
1793 #[inline]
1794 fn add_assign(&mut self, rhs: &Self) {
1795 self.add_assign(*rhs);
1796 }
1797}
1798
1799impl Add<f32> for Vec3 {
1800 type Output = Self;
1801 #[inline]
1802 fn add(self, rhs: f32) -> Self {
1803 Self::new(self.x.add(rhs), self.y.add(rhs), self.z.add(rhs))
1804 }
1805}
1806
1807impl Add<&f32> for Vec3 {
1808 type Output = Self;
1809 #[inline]
1810 fn add(self, rhs: &f32) -> Self {
1811 self.add(*rhs)
1812 }
1813}
1814
1815impl Add<&f32> for &Vec3 {
1816 type Output = Vec3;
1817 #[inline]
1818 fn add(self, rhs: &f32) -> Vec3 {
1819 (*self).add(*rhs)
1820 }
1821}
1822
1823impl Add<f32> for &Vec3 {
1824 type Output = Vec3;
1825 #[inline]
1826 fn add(self, rhs: f32) -> Vec3 {
1827 (*self).add(rhs)
1828 }
1829}
1830
1831impl AddAssign<f32> for Vec3 {
1832 #[inline]
1833 fn add_assign(&mut self, rhs: f32) {
1834 self.x.add_assign(rhs);
1835 self.y.add_assign(rhs);
1836 self.z.add_assign(rhs);
1837 }
1838}
1839
1840impl AddAssign<&f32> for Vec3 {
1841 #[inline]
1842 fn add_assign(&mut self, rhs: &f32) {
1843 self.add_assign(*rhs);
1844 }
1845}
1846
1847impl Add<Vec3> for f32 {
1848 type Output = Vec3;
1849 #[inline]
1850 fn add(self, rhs: Vec3) -> Vec3 {
1851 Vec3::new(self.add(rhs.x), self.add(rhs.y), self.add(rhs.z))
1852 }
1853}
1854
1855impl Add<&Vec3> for f32 {
1856 type Output = Vec3;
1857 #[inline]
1858 fn add(self, rhs: &Vec3) -> Vec3 {
1859 self.add(*rhs)
1860 }
1861}
1862
1863impl Add<&Vec3> for &f32 {
1864 type Output = Vec3;
1865 #[inline]
1866 fn add(self, rhs: &Vec3) -> Vec3 {
1867 (*self).add(*rhs)
1868 }
1869}
1870
1871impl Add<Vec3> for &f32 {
1872 type Output = Vec3;
1873 #[inline]
1874 fn add(self, rhs: Vec3) -> Vec3 {
1875 (*self).add(rhs)
1876 }
1877}
1878
1879impl Sub for Vec3 {
1880 type Output = Self;
1881 #[inline]
1882 fn sub(self, rhs: Self) -> Self {
1883 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y), self.z.sub(rhs.z))
1884 }
1885}
1886
1887impl Sub<&Self> for Vec3 {
1888 type Output = Self;
1889 #[inline]
1890 fn sub(self, rhs: &Self) -> Self {
1891 self.sub(*rhs)
1892 }
1893}
1894
1895impl Sub<&Vec3> for &Vec3 {
1896 type Output = Vec3;
1897 #[inline]
1898 fn sub(self, rhs: &Vec3) -> Vec3 {
1899 (*self).sub(*rhs)
1900 }
1901}
1902
1903impl Sub<Vec3> for &Vec3 {
1904 type Output = Vec3;
1905 #[inline]
1906 fn sub(self, rhs: Vec3) -> Vec3 {
1907 (*self).sub(rhs)
1908 }
1909}
1910
1911impl SubAssign for Vec3 {
1912 #[inline]
1913 fn sub_assign(&mut self, rhs: Self) {
1914 self.x.sub_assign(rhs.x);
1915 self.y.sub_assign(rhs.y);
1916 self.z.sub_assign(rhs.z);
1917 }
1918}
1919
1920impl SubAssign<&Self> for Vec3 {
1921 #[inline]
1922 fn sub_assign(&mut self, rhs: &Self) {
1923 self.sub_assign(*rhs);
1924 }
1925}
1926
1927impl Sub<f32> for Vec3 {
1928 type Output = Self;
1929 #[inline]
1930 fn sub(self, rhs: f32) -> Self {
1931 Self::new(self.x.sub(rhs), self.y.sub(rhs), self.z.sub(rhs))
1932 }
1933}
1934
1935impl Sub<&f32> for Vec3 {
1936 type Output = Self;
1937 #[inline]
1938 fn sub(self, rhs: &f32) -> Self {
1939 self.sub(*rhs)
1940 }
1941}
1942
1943impl Sub<&f32> for &Vec3 {
1944 type Output = Vec3;
1945 #[inline]
1946 fn sub(self, rhs: &f32) -> Vec3 {
1947 (*self).sub(*rhs)
1948 }
1949}
1950
1951impl Sub<f32> for &Vec3 {
1952 type Output = Vec3;
1953 #[inline]
1954 fn sub(self, rhs: f32) -> Vec3 {
1955 (*self).sub(rhs)
1956 }
1957}
1958
1959impl SubAssign<f32> for Vec3 {
1960 #[inline]
1961 fn sub_assign(&mut self, rhs: f32) {
1962 self.x.sub_assign(rhs);
1963 self.y.sub_assign(rhs);
1964 self.z.sub_assign(rhs);
1965 }
1966}
1967
1968impl SubAssign<&f32> for Vec3 {
1969 #[inline]
1970 fn sub_assign(&mut self, rhs: &f32) {
1971 self.sub_assign(*rhs);
1972 }
1973}
1974
1975impl Sub<Vec3> for f32 {
1976 type Output = Vec3;
1977 #[inline]
1978 fn sub(self, rhs: Vec3) -> Vec3 {
1979 Vec3::new(self.sub(rhs.x), self.sub(rhs.y), self.sub(rhs.z))
1980 }
1981}
1982
1983impl Sub<&Vec3> for f32 {
1984 type Output = Vec3;
1985 #[inline]
1986 fn sub(self, rhs: &Vec3) -> Vec3 {
1987 self.sub(*rhs)
1988 }
1989}
1990
1991impl Sub<&Vec3> for &f32 {
1992 type Output = Vec3;
1993 #[inline]
1994 fn sub(self, rhs: &Vec3) -> Vec3 {
1995 (*self).sub(*rhs)
1996 }
1997}
1998
1999impl Sub<Vec3> for &f32 {
2000 type Output = Vec3;
2001 #[inline]
2002 fn sub(self, rhs: Vec3) -> Vec3 {
2003 (*self).sub(rhs)
2004 }
2005}
2006
2007impl Rem for Vec3 {
2008 type Output = Self;
2009 #[inline]
2010 fn rem(self, rhs: Self) -> Self {
2011 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y), self.z.rem(rhs.z))
2012 }
2013}
2014
2015impl Rem<&Self> for Vec3 {
2016 type Output = Self;
2017 #[inline]
2018 fn rem(self, rhs: &Self) -> Self {
2019 self.rem(*rhs)
2020 }
2021}
2022
2023impl Rem<&Vec3> for &Vec3 {
2024 type Output = Vec3;
2025 #[inline]
2026 fn rem(self, rhs: &Vec3) -> Vec3 {
2027 (*self).rem(*rhs)
2028 }
2029}
2030
2031impl Rem<Vec3> for &Vec3 {
2032 type Output = Vec3;
2033 #[inline]
2034 fn rem(self, rhs: Vec3) -> Vec3 {
2035 (*self).rem(rhs)
2036 }
2037}
2038
2039impl RemAssign for Vec3 {
2040 #[inline]
2041 fn rem_assign(&mut self, rhs: Self) {
2042 self.x.rem_assign(rhs.x);
2043 self.y.rem_assign(rhs.y);
2044 self.z.rem_assign(rhs.z);
2045 }
2046}
2047
2048impl RemAssign<&Self> for Vec3 {
2049 #[inline]
2050 fn rem_assign(&mut self, rhs: &Self) {
2051 self.rem_assign(*rhs);
2052 }
2053}
2054
2055impl Rem<f32> for Vec3 {
2056 type Output = Self;
2057 #[inline]
2058 fn rem(self, rhs: f32) -> Self {
2059 Self::new(self.x.rem(rhs), self.y.rem(rhs), self.z.rem(rhs))
2060 }
2061}
2062
2063impl Rem<&f32> for Vec3 {
2064 type Output = Self;
2065 #[inline]
2066 fn rem(self, rhs: &f32) -> Self {
2067 self.rem(*rhs)
2068 }
2069}
2070
2071impl Rem<&f32> for &Vec3 {
2072 type Output = Vec3;
2073 #[inline]
2074 fn rem(self, rhs: &f32) -> Vec3 {
2075 (*self).rem(*rhs)
2076 }
2077}
2078
2079impl Rem<f32> for &Vec3 {
2080 type Output = Vec3;
2081 #[inline]
2082 fn rem(self, rhs: f32) -> Vec3 {
2083 (*self).rem(rhs)
2084 }
2085}
2086
2087impl RemAssign<f32> for Vec3 {
2088 #[inline]
2089 fn rem_assign(&mut self, rhs: f32) {
2090 self.x.rem_assign(rhs);
2091 self.y.rem_assign(rhs);
2092 self.z.rem_assign(rhs);
2093 }
2094}
2095
2096impl RemAssign<&f32> for Vec3 {
2097 #[inline]
2098 fn rem_assign(&mut self, rhs: &f32) {
2099 self.rem_assign(*rhs);
2100 }
2101}
2102
2103impl Rem<Vec3> for f32 {
2104 type Output = Vec3;
2105 #[inline]
2106 fn rem(self, rhs: Vec3) -> Vec3 {
2107 Vec3::new(self.rem(rhs.x), self.rem(rhs.y), self.rem(rhs.z))
2108 }
2109}
2110
2111impl Rem<&Vec3> for f32 {
2112 type Output = Vec3;
2113 #[inline]
2114 fn rem(self, rhs: &Vec3) -> Vec3 {
2115 self.rem(*rhs)
2116 }
2117}
2118
2119impl Rem<&Vec3> for &f32 {
2120 type Output = Vec3;
2121 #[inline]
2122 fn rem(self, rhs: &Vec3) -> Vec3 {
2123 (*self).rem(*rhs)
2124 }
2125}
2126
2127impl Rem<Vec3> for &f32 {
2128 type Output = Vec3;
2129 #[inline]
2130 fn rem(self, rhs: Vec3) -> Vec3 {
2131 (*self).rem(rhs)
2132 }
2133}
2134
2135impl AsRef<[f32; 3]> for Vec3 {
2136 #[inline]
2137 fn as_ref(&self) -> &[f32; 3] {
2138 unsafe { &*(self as *const Self as *const [f32; 3]) }
2139 }
2140}
2141
2142impl AsMut<[f32; 3]> for Vec3 {
2143 #[inline]
2144 fn as_mut(&mut self) -> &mut [f32; 3] {
2145 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
2146 }
2147}
2148
2149impl Sum for Vec3 {
2150 #[inline]
2151 fn sum<I>(iter: I) -> Self
2152 where
2153 I: Iterator<Item = Self>,
2154 {
2155 iter.fold(Self::ZERO, Self::add)
2156 }
2157}
2158
2159impl<'a> Sum<&'a Self> for Vec3 {
2160 #[inline]
2161 fn sum<I>(iter: I) -> Self
2162 where
2163 I: Iterator<Item = &'a Self>,
2164 {
2165 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2166 }
2167}
2168
2169impl Product for Vec3 {
2170 #[inline]
2171 fn product<I>(iter: I) -> Self
2172 where
2173 I: Iterator<Item = Self>,
2174 {
2175 iter.fold(Self::ONE, Self::mul)
2176 }
2177}
2178
2179impl<'a> Product<&'a Self> for Vec3 {
2180 #[inline]
2181 fn product<I>(iter: I) -> Self
2182 where
2183 I: Iterator<Item = &'a Self>,
2184 {
2185 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2186 }
2187}
2188
2189impl Neg for Vec3 {
2190 type Output = Self;
2191 #[inline]
2192 fn neg(self) -> Self {
2193 Self::new(self.x.neg(), self.y.neg(), self.z.neg())
2194 }
2195}
2196
2197impl Neg for &Vec3 {
2198 type Output = Vec3;
2199 #[inline]
2200 fn neg(self) -> Vec3 {
2201 (*self).neg()
2202 }
2203}
2204
2205impl Index<usize> for Vec3 {
2206 type Output = f32;
2207 #[inline]
2208 #[track_caller]
2209 fn index(&self, index: usize) -> &Self::Output {
2210 match index {
2211 0 => &self.x,
2212 1 => &self.y,
2213 2 => &self.z,
2214 _ => panic!("index out of bounds"),
2215 }
2216 }
2217}
2218
2219impl IndexMut<usize> for Vec3 {
2220 #[inline]
2221 #[track_caller]
2222 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2223 match index {
2224 0 => &mut self.x,
2225 1 => &mut self.y,
2226 2 => &mut self.z,
2227 _ => panic!("index out of bounds"),
2228 }
2229 }
2230}
2231
2232impl fmt::Display for Vec3 {
2233 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2234 if let Some(p) = f.precision() {
2235 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2236 } else {
2237 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2238 }
2239 }
2240}
2241
2242impl fmt::Debug for Vec3 {
2243 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2244 fmt.debug_tuple(stringify!(Vec3))
2245 .field(&self.x)
2246 .field(&self.y)
2247 .field(&self.z)
2248 .finish()
2249 }
2250}
2251
2252impl From<[f32; 3]> for Vec3 {
2253 #[inline]
2254 fn from(a: [f32; 3]) -> Self {
2255 Self::new(a[0], a[1], a[2])
2256 }
2257}
2258
2259impl From<Vec3> for [f32; 3] {
2260 #[inline]
2261 fn from(v: Vec3) -> Self {
2262 [v.x, v.y, v.z]
2263 }
2264}
2265
2266impl From<(f32, f32, f32)> for Vec3 {
2267 #[inline]
2268 fn from(t: (f32, f32, f32)) -> Self {
2269 Self::new(t.0, t.1, t.2)
2270 }
2271}
2272
2273impl From<Vec3> for (f32, f32, f32) {
2274 #[inline]
2275 fn from(v: Vec3) -> Self {
2276 (v.x, v.y, v.z)
2277 }
2278}
2279
2280impl From<(Vec2, f32)> for Vec3 {
2281 #[inline]
2282 fn from((v, z): (Vec2, f32)) -> Self {
2283 Self::new(v.x, v.y, z)
2284 }
2285}
2286
2287impl From<BVec3> for Vec3 {
2288 #[inline]
2289 fn from(v: BVec3) -> Self {
2290 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2291 }
2292}
2293
2294impl From<BVec3A> for Vec3 {
2295 #[inline]
2296 fn from(v: BVec3A) -> Self {
2297 let bool_array: [bool; 3] = v.into();
2298 Self::new(
2299 f32::from(bool_array[0]),
2300 f32::from(bool_array[1]),
2301 f32::from(bool_array[2]),
2302 )
2303 }
2304}