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