1use crate::{BVec3, BVec3A, DQuat, DVec2, DVec4, FloatExt, f64::math};
4
5use crate::Vec3;
6
7#[cfg(feature = "i32")]
8use crate::IVec3;
9
10#[cfg(feature = "u32")]
11use crate::UVec3;
12
13use core::fmt;
14use core::iter::{Product, Sum};
15use core::ops::*;
16
17#[cfg(feature = "zerocopy-08")]
18use zerocopy_derive_08::*;
19
20#[inline(always)]
22#[must_use]
23pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3 {
24 DVec3::new(x, y, z)
25}
26
27#[derive(Clone, Copy, PartialEq)]
29#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
30#[cfg_attr(
31 feature = "zerocopy-08",
32 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
33)]
34#[repr(C)]
35#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
36pub struct DVec3 {
37 pub x: f64,
38 pub y: f64,
39 pub z: f64,
40}
41
42impl DVec3 {
43 pub const ZERO: Self = Self::splat(0.0);
45
46 pub const ONE: Self = Self::splat(1.0);
48
49 pub const NEG_ONE: Self = Self::splat(-1.0);
51
52 pub const MIN: Self = Self::splat(f64::MIN);
54
55 pub const MAX: Self = Self::splat(f64::MAX);
57
58 pub const NAN: Self = Self::splat(f64::NAN);
60
61 pub const INFINITY: Self = Self::splat(f64::INFINITY);
63
64 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
66
67 pub const X: Self = Self::new(1.0, 0.0, 0.0);
69
70 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
72
73 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
75
76 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
78
79 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
81
82 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
84
85 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
87
88 pub const USES_CORE_SIMD: bool = false;
90 pub const USES_NEON: bool = false;
92 pub const USES_SCALAR_MATH: bool = true;
94 pub const USES_SSE2: bool = false;
96 pub const USES_WASM_SIMD: bool = false;
98
99 #[inline(always)]
101 #[must_use]
102 pub const fn new(x: f64, y: f64, z: f64) -> Self {
103 Self { x, y, z }
104 }
105
106 #[inline]
108 #[must_use]
109 pub const fn splat(v: f64) -> Self {
110 Self::new(v, v, v)
111 }
112
113 #[inline]
115 #[must_use]
116 pub fn map<F>(self, mut f: F) -> Self
117 where
118 F: FnMut(f64) -> f64,
119 {
120 Self::new(f(self.x), f(self.y), f(self.z))
121 }
122
123 #[inline]
129 #[must_use]
130 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
131 Self::new(
132 if mask.test(0) { if_true.x } else { if_false.x },
133 if mask.test(1) { if_true.y } else { if_false.y },
134 if mask.test(2) { if_true.z } else { if_false.z },
135 )
136 }
137
138 #[inline]
140 #[must_use]
141 pub const fn from_array(a: [f64; 3]) -> Self {
142 Self::new(a[0], a[1], a[2])
143 }
144
145 #[inline]
147 #[must_use]
148 pub const fn to_array(&self) -> [f64; 3] {
149 [self.x, self.y, self.z]
150 }
151
152 #[inline]
158 #[must_use]
159 #[track_caller]
160 pub const fn from_slice(slice: &[f64]) -> Self {
161 assert!(slice.len() >= 3);
162 Self::new(slice[0], slice[1], slice[2])
163 }
164
165 #[inline]
171 #[track_caller]
172 pub fn write_to_slice(self, slice: &mut [f64]) {
173 slice[..3].copy_from_slice(&self.to_array());
174 }
175
176 #[allow(dead_code)]
178 #[inline]
179 #[must_use]
180 pub(crate) fn from_vec4(v: DVec4) -> Self {
181 Self::new(v.x, v.y, v.z)
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn extend(self, w: f64) -> DVec4 {
188 DVec4::new(self.x, self.y, self.z, w)
189 }
190
191 #[inline]
195 #[must_use]
196 pub fn truncate(self) -> DVec2 {
197 use crate::swizzles::Vec3Swizzles;
198 self.xy()
199 }
200
201 #[inline]
207 #[must_use]
208 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
209 pub fn from_homogeneous(v: DVec4) -> Self {
210 glam_assert!(v.w != 0.0);
211 Self::from_vec4(v) / v.w
212 }
213
214 #[inline]
216 #[must_use]
217 pub fn to_homogeneous(self) -> DVec4 {
218 self.extend(1.0)
219 }
220
221 #[inline]
223 #[must_use]
224 pub fn with_x(mut self, x: f64) -> Self {
225 self.x = x;
226 self
227 }
228
229 #[inline]
231 #[must_use]
232 pub fn with_y(mut self, y: f64) -> Self {
233 self.y = y;
234 self
235 }
236
237 #[inline]
239 #[must_use]
240 pub fn with_z(mut self, z: f64) -> Self {
241 self.z = z;
242 self
243 }
244
245 #[inline]
247 #[must_use]
248 pub fn dot(self, rhs: Self) -> f64 {
249 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
250 }
251
252 #[inline]
254 #[must_use]
255 pub fn dot_into_vec(self, rhs: Self) -> Self {
256 Self::splat(self.dot(rhs))
257 }
258
259 #[inline]
261 #[must_use]
262 pub fn cross(self, rhs: Self) -> Self {
263 Self::new(
264 self.y * rhs.z - rhs.y * self.z,
265 self.z * rhs.x - rhs.z * self.x,
266 self.x * rhs.y - rhs.x * self.y,
267 )
268 }
269
270 #[inline]
277 #[must_use]
278 pub fn min(self, rhs: Self) -> Self {
279 Self::new(
280 if self.x < rhs.x { self.x } else { rhs.x },
281 if self.y < rhs.y { self.y } else { rhs.y },
282 if self.z < rhs.z { self.z } else { rhs.z },
283 )
284 }
285
286 #[inline]
293 #[must_use]
294 pub fn max(self, rhs: Self) -> Self {
295 Self::new(
296 if self.x > rhs.x { self.x } else { rhs.x },
297 if self.y > rhs.y { self.y } else { rhs.y },
298 if self.z > rhs.z { self.z } else { rhs.z },
299 )
300 }
301
302 #[inline]
313 #[must_use]
314 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
315 pub fn clamp(self, min: Self, max: Self) -> Self {
316 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
317 self.max(min).min(max)
318 }
319
320 #[inline]
327 #[must_use]
328 pub fn min_element(self) -> f64 {
329 let min = |a, b| if a < b { a } else { b };
330 min(self.x, min(self.y, self.z))
331 }
332
333 #[inline]
340 #[must_use]
341 pub fn max_element(self) -> f64 {
342 let max = |a, b| if a > b { a } else { b };
343 max(self.x, max(self.y, self.z))
344 }
345
346 #[doc(alias = "argmin")]
348 #[inline]
349 #[must_use]
350 pub fn min_position(self) -> usize {
351 let mut min = self.x;
352 let mut index = 0;
353 if self.y < min {
354 min = self.y;
355 index = 1;
356 }
357 if self.z < min {
358 index = 2;
359 }
360 index
361 }
362
363 #[doc(alias = "argmax")]
365 #[inline]
366 #[must_use]
367 pub fn max_position(self) -> usize {
368 let mut max = self.x;
369 let mut index = 0;
370 if self.y > max {
371 max = self.y;
372 index = 1;
373 }
374 if self.z > max {
375 index = 2;
376 }
377 index
378 }
379
380 #[inline]
384 #[must_use]
385 pub fn element_sum(self) -> f64 {
386 self.x + self.y + self.z
387 }
388
389 #[inline]
393 #[must_use]
394 pub fn element_product(self) -> f64 {
395 self.x * self.y * self.z
396 }
397
398 #[inline]
404 #[must_use]
405 pub fn cmpeq(self, rhs: Self) -> BVec3 {
406 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
407 }
408
409 #[inline]
415 #[must_use]
416 pub fn cmpne(self, rhs: Self) -> BVec3 {
417 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
418 }
419
420 #[inline]
426 #[must_use]
427 pub fn cmpge(self, rhs: Self) -> BVec3 {
428 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
429 }
430
431 #[inline]
437 #[must_use]
438 pub fn cmpgt(self, rhs: Self) -> BVec3 {
439 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
440 }
441
442 #[inline]
448 #[must_use]
449 pub fn cmple(self, rhs: Self) -> BVec3 {
450 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
451 }
452
453 #[inline]
459 #[must_use]
460 pub fn cmplt(self, rhs: Self) -> BVec3 {
461 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
462 }
463
464 #[inline]
466 #[must_use]
467 pub fn abs(self) -> Self {
468 Self::new(self.x.abs(), self.y.abs(), self.z.abs())
469 }
470
471 #[inline]
477 #[must_use]
478 pub fn signum(self) -> Self {
479 Self::new(self.x.signum(), self.y.signum(), self.z.signum())
480 }
481
482 #[inline]
484 #[must_use]
485 pub fn copysign(self, rhs: Self) -> Self {
486 Self::new(
487 self.x.copysign(rhs.x),
488 self.y.copysign(rhs.y),
489 self.z.copysign(rhs.z),
490 )
491 }
492
493 #[inline]
501 #[must_use]
502 pub fn is_negative_bitmask(self) -> u32 {
503 (self.x.is_sign_negative() as u32)
504 | ((self.y.is_sign_negative() as u32) << 1)
505 | ((self.z.is_sign_negative() as u32) << 2)
506 }
507
508 #[inline]
513 #[must_use]
514 pub fn is_negative_mask(self) -> BVec3 {
515 BVec3::new(
516 self.x.is_sign_negative(),
517 self.y.is_sign_negative(),
518 self.z.is_sign_negative(),
519 )
520 }
521
522 #[inline]
525 #[must_use]
526 pub fn is_finite(self) -> bool {
527 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
528 }
529
530 #[inline]
534 #[must_use]
535 pub fn is_finite_mask(self) -> BVec3 {
536 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
537 }
538
539 #[inline]
541 #[must_use]
542 pub fn is_nan(self) -> bool {
543 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
544 }
545
546 #[inline]
550 #[must_use]
551 pub fn is_nan_mask(self) -> BVec3 {
552 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
553 }
554
555 #[doc(alias = "magnitude")]
557 #[inline]
558 #[must_use]
559 pub fn length(self) -> f64 {
560 math::sqrt(self.dot(self))
561 }
562
563 #[allow(dead_code)]
565 fn is_non_zero(self) -> bool {
566 self.length_squared() > 0.0
567 }
568
569 #[doc(alias = "magnitude2")]
573 #[inline]
574 #[must_use]
575 pub fn length_squared(self) -> f64 {
576 self.dot(self)
577 }
578
579 #[inline]
583 #[must_use]
584 pub fn length_recip(self) -> f64 {
585 1.0 / self.length()
586 }
587
588 #[inline]
590 #[must_use]
591 pub fn distance(self, rhs: Self) -> f64 {
592 (self - rhs).length()
593 }
594
595 #[inline]
597 #[must_use]
598 pub fn distance_squared(self, rhs: Self) -> f64 {
599 (self - rhs).length_squared()
600 }
601
602 #[inline]
604 #[must_use]
605 pub fn div_euclid(self, rhs: Self) -> Self {
606 Self::new(
607 math::div_euclid(self.x, rhs.x),
608 math::div_euclid(self.y, rhs.y),
609 math::div_euclid(self.z, rhs.z),
610 )
611 }
612
613 #[inline]
617 #[must_use]
618 pub fn rem_euclid(self, rhs: Self) -> Self {
619 Self::new(
620 math::rem_euclid(self.x, rhs.x),
621 math::rem_euclid(self.y, rhs.y),
622 math::rem_euclid(self.z, rhs.z),
623 )
624 }
625
626 #[inline]
636 #[must_use]
637 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
638 pub fn normalize(self) -> Self {
639 #[allow(clippy::let_and_return)]
640 let normalized = self.mul(self.length_recip());
641 glam_assert!(normalized.is_finite());
642 normalized
643 }
644
645 #[inline]
652 #[must_use]
653 pub fn try_normalize(self) -> Option<Self> {
654 let rcp = self.length_recip();
655 if rcp.is_finite() && rcp > 0.0 {
656 Some(self * rcp)
657 } else {
658 None
659 }
660 }
661
662 #[inline]
670 #[must_use]
671 pub fn normalize_or(self, fallback: Self) -> Self {
672 let rcp = self.length_recip();
673 if rcp.is_finite() && rcp > 0.0 {
674 self * rcp
675 } else {
676 fallback
677 }
678 }
679
680 #[inline]
687 #[must_use]
688 pub fn normalize_or_zero(self) -> Self {
689 self.normalize_or(Self::ZERO)
690 }
691
692 #[inline]
696 #[must_use]
697 pub fn normalize_and_length(self) -> (Self, f64) {
698 let length = self.length();
699 let rcp = 1.0 / length;
700 if rcp.is_finite() && rcp > 0.0 {
701 (self * rcp, length)
702 } else {
703 (Self::X, 0.0)
704 }
705 }
706
707 #[inline]
711 #[must_use]
712 pub fn is_normalized(self) -> bool {
713 (self.length_squared() - 1.0).abs() <= 2e-4
714 }
715
716 #[inline]
724 #[must_use]
725 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
726 pub fn project_onto(self, rhs: Self) -> Self {
727 let rhs_len_sq = rhs.dot(rhs);
728 glam_assert!(rhs_len_sq != 0.0);
729 rhs * (self.dot(rhs) / rhs_len_sq)
730 }
731
732 #[doc(alias("plane"))]
743 #[inline]
744 #[must_use]
745 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
746 pub fn reject_from(self, rhs: Self) -> Self {
747 self - self.project_onto(rhs)
748 }
749
750 #[inline]
758 #[must_use]
759 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
760 pub fn project_onto_normalized(self, rhs: Self) -> Self {
761 glam_assert!(rhs.is_normalized());
762 rhs * self.dot(rhs)
763 }
764
765 #[doc(alias("plane"))]
776 #[inline]
777 #[must_use]
778 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
779 pub fn reject_from_normalized(self, rhs: Self) -> Self {
780 self - self.project_onto_normalized(rhs)
781 }
782
783 #[inline]
786 #[must_use]
787 pub fn round(self) -> Self {
788 Self::new(
789 math::round(self.x),
790 math::round(self.y),
791 math::round(self.z),
792 )
793 }
794
795 #[inline]
798 #[must_use]
799 pub fn floor(self) -> Self {
800 Self::new(
801 math::floor(self.x),
802 math::floor(self.y),
803 math::floor(self.z),
804 )
805 }
806
807 #[inline]
810 #[must_use]
811 pub fn ceil(self) -> Self {
812 Self::new(math::ceil(self.x), math::ceil(self.y), math::ceil(self.z))
813 }
814
815 #[inline]
818 #[must_use]
819 pub fn trunc(self) -> Self {
820 Self::new(
821 math::trunc(self.x),
822 math::trunc(self.y),
823 math::trunc(self.z),
824 )
825 }
826
827 #[inline]
831 #[must_use]
832 pub fn step(self, rhs: Self) -> Self {
833 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
834 }
835
836 #[inline]
847 #[must_use]
848 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
849 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
850 glam_assert!(edge0.cmplt(edge1).all());
851 let t = ((self - edge0) / (edge1 - edge0)).saturate();
852 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
853 }
854
855 #[inline]
857 #[must_use]
858 pub fn saturate(self) -> Self {
859 self.clamp(Self::ZERO, Self::ONE)
860 }
861
862 #[inline]
869 #[must_use]
870 pub fn fract(self) -> Self {
871 self - self.trunc()
872 }
873
874 #[inline]
881 #[must_use]
882 pub fn fract_gl(self) -> Self {
883 self - self.floor()
884 }
885
886 #[inline]
889 #[must_use]
890 pub fn exp(self) -> Self {
891 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
892 }
893
894 #[inline]
896 #[must_use]
897 pub fn exp2(self) -> Self {
898 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
899 }
900
901 #[inline]
904 #[must_use]
905 pub fn ln(self) -> Self {
906 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
907 }
908
909 #[inline]
912 #[must_use]
913 pub fn log2(self) -> Self {
914 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
915 }
916
917 #[inline]
919 #[must_use]
920 pub fn powf(self, n: f64) -> Self {
921 Self::new(
922 math::powf(self.x, n),
923 math::powf(self.y, n),
924 math::powf(self.z, n),
925 )
926 }
927
928 #[inline]
931 #[must_use]
932 pub fn sqrt(self) -> Self {
933 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
934 }
935
936 #[inline]
938 #[must_use]
939 pub fn cos(self) -> Self {
940 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
941 }
942
943 #[inline]
945 #[must_use]
946 pub fn sin(self) -> Self {
947 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
948 }
949
950 #[inline]
952 #[must_use]
953 pub fn sin_cos(self) -> (Self, Self) {
954 let (sin_x, cos_x) = math::sin_cos(self.x);
955 let (sin_y, cos_y) = math::sin_cos(self.y);
956 let (sin_z, cos_z) = math::sin_cos(self.z);
957
958 (
959 Self::new(sin_x, sin_y, sin_z),
960 Self::new(cos_x, cos_y, cos_z),
961 )
962 }
963
964 #[inline]
966 #[must_use]
967 pub fn recip(self) -> Self {
968 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
969 }
970
971 #[doc(alias = "mix")]
983 #[inline]
984 #[must_use]
985 pub fn lerp(self, rhs: Self, s: f64) -> Self {
986 self * (1.0 - s) + rhs * s
987 }
988
989 #[doc(alias = "mix")]
1006 #[inline]
1007 #[must_use]
1008 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
1009 self + (rhs - self) * s
1010 }
1011
1012 #[inline]
1017 #[must_use]
1018 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1019 let a = rhs - self;
1020 let len = a.length();
1021 if len <= d || len <= 1e-4 {
1022 return rhs;
1023 }
1024 self + a / len * d
1025 }
1026
1027 #[inline]
1033 pub fn midpoint(self, rhs: Self) -> Self {
1034 (self + rhs) * 0.5
1035 }
1036
1037 #[inline]
1047 #[must_use]
1048 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1049 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1050 }
1051
1052 #[inline]
1058 #[must_use]
1059 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1060 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1061 glam_assert!(0.0 <= min);
1062 glam_assert!(min <= max);
1063 let length_sq = self.length_squared();
1064 if length_sq < min * min {
1065 min * (self / math::sqrt(length_sq))
1066 } else if length_sq > max * max {
1067 max * (self / math::sqrt(length_sq))
1068 } else {
1069 self
1070 }
1071 }
1072
1073 #[inline]
1079 #[must_use]
1080 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1081 pub fn clamp_length_max(self, max: f64) -> Self {
1082 glam_assert!(0.0 <= max);
1083 let length_sq = self.length_squared();
1084 if length_sq > max * max {
1085 max * (self / math::sqrt(length_sq))
1086 } else {
1087 self
1088 }
1089 }
1090
1091 #[inline]
1097 #[must_use]
1098 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1099 pub fn clamp_length_min(self, min: f64) -> Self {
1100 glam_assert!(0.0 <= min);
1101 let length_sq = self.length_squared();
1102 if length_sq < min * min {
1103 min * (self / math::sqrt(length_sq))
1104 } else {
1105 self
1106 }
1107 }
1108
1109 #[inline]
1117 #[must_use]
1118 pub fn mul_add(self, a: Self, b: Self) -> Self {
1119 Self::new(
1120 math::mul_add(self.x, a.x, b.x),
1121 math::mul_add(self.y, a.y, b.y),
1122 math::mul_add(self.z, a.z, b.z),
1123 )
1124 }
1125
1126 #[inline]
1135 #[must_use]
1136 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1137 pub fn reflect(self, normal: Self) -> Self {
1138 glam_assert!(normal.is_normalized());
1139 self - 2.0 * self.dot(normal) * normal
1140 }
1141
1142 #[inline]
1152 #[must_use]
1153 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1154 pub fn refract(self, normal: Self, eta: f64) -> Self {
1155 glam_assert!(self.is_normalized());
1156 glam_assert!(normal.is_normalized());
1157 let n_dot_i = normal.dot(self);
1158 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1159 if k >= 0.0 {
1160 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1161 } else {
1162 Self::ZERO
1163 }
1164 }
1165
1166 #[inline]
1177 #[must_use]
1178 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1179 pub fn angle_between(self, rhs: Self) -> f64 {
1180 glam_assert!(self.is_non_zero());
1181 glam_assert!(rhs.is_non_zero());
1182 math::acos_approx(
1183 self.dot(rhs)
1184 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1185 )
1186 }
1187
1188 #[doc(alias = "signed_angle")]
1204 #[inline]
1205 #[must_use]
1206 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1207 pub fn angle_to(self, rhs: Self, axis: Self) -> f64 {
1208 glam_assert!(axis.is_normalized());
1209 glam_assert!(self.is_non_zero());
1210 glam_assert!(rhs.is_non_zero());
1211 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1212 }
1213
1214 #[inline]
1216 #[must_use]
1217 pub fn rotate_x(self, angle: f64) -> Self {
1218 let (sina, cosa) = math::sin_cos(angle);
1219 Self::new(
1220 self.x,
1221 self.y * cosa - self.z * sina,
1222 self.y * sina + self.z * cosa,
1223 )
1224 }
1225
1226 #[inline]
1228 #[must_use]
1229 pub fn rotate_y(self, angle: f64) -> Self {
1230 let (sina, cosa) = math::sin_cos(angle);
1231 Self::new(
1232 self.x * cosa + self.z * sina,
1233 self.y,
1234 self.x * -sina + self.z * cosa,
1235 )
1236 }
1237
1238 #[inline]
1240 #[must_use]
1241 pub fn rotate_z(self, angle: f64) -> Self {
1242 let (sina, cosa) = math::sin_cos(angle);
1243 Self::new(
1244 self.x * cosa - self.y * sina,
1245 self.x * sina + self.y * cosa,
1246 self.z,
1247 )
1248 }
1249
1250 #[inline]
1258 #[must_use]
1259 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1260 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1261 DQuat::from_axis_angle(axis, angle) * self
1262 }
1263
1264 #[inline]
1274 #[must_use]
1275 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1276 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1277 let angle_between = self.angle_between(rhs);
1278 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1280 let axis = self
1281 .cross(rhs)
1282 .try_normalize()
1283 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1284 DQuat::from_axis_angle(axis, angle) * self
1285 }
1286
1287 #[inline]
1294 #[must_use]
1295 pub fn any_orthogonal_vector(self) -> Self {
1296 if self.x.abs() > self.y.abs() {
1298 Self::new(-self.z, 0.0, self.x) } else {
1300 Self::new(0.0, self.z, -self.y) }
1302 }
1303
1304 #[inline]
1312 #[must_use]
1313 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1314 pub fn any_orthonormal_vector(self) -> Self {
1315 glam_assert!(self.is_normalized());
1316 let sign = self.z.signum();
1318 let a = -1.0 / (sign + self.z);
1319 let b = self.x * self.y * a;
1320 Self::new(b, sign + self.y * self.y * a, -self.y)
1321 }
1322
1323 #[inline]
1330 #[must_use]
1331 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1332 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1333 glam_assert!(self.is_normalized());
1334 let sign = self.z.signum();
1336 let a = -1.0 / (sign + self.z);
1337 let b = self.x * self.y * a;
1338 (
1339 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1340 Self::new(b, sign + self.y * self.y * a, -self.y),
1341 )
1342 }
1343
1344 #[inline]
1354 #[must_use]
1355 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1356 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1357 glam_assert!(self.is_non_zero());
1358 glam_assert!(rhs.is_non_zero());
1359 let self_length = self.length();
1360 let rhs_length = rhs.length();
1361 let dot = self.dot(rhs) / (self_length * rhs_length);
1363 if dot.abs() < 1.0 - 3e-7 {
1365 let theta = math::acos_approx(dot);
1367 let sin_theta = math::sin(theta);
1369 let t1 = math::sin(theta * (1.0 - s));
1370 let t2 = math::sin(theta * s);
1371
1372 let result_length = self_length.lerp_monotonic(rhs_length, s);
1374 return (self * (result_length / self_length) * t1
1376 + rhs * (result_length / rhs_length) * t2)
1377 * (1.0 / sin_theta);
1378 }
1379 if dot < 0.0 {
1380 let axis = self.any_orthogonal_vector().normalize();
1384 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1385 let result_length = self_length.lerp_monotonic(rhs_length, s);
1387 rotation * self * (result_length / self_length)
1388 } else {
1389 self.lerp(rhs, s)
1391 }
1392 }
1393
1394 #[inline]
1396 #[must_use]
1397 pub fn as_vec3(self) -> crate::Vec3 {
1398 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1399 }
1400
1401 #[inline]
1403 #[must_use]
1404 pub fn as_vec3a(self) -> crate::Vec3A {
1405 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1406 }
1407
1408 #[cfg(feature = "i8")]
1410 #[inline]
1411 #[must_use]
1412 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1413 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1414 }
1415
1416 #[cfg(feature = "u8")]
1418 #[inline]
1419 #[must_use]
1420 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1421 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1422 }
1423
1424 #[cfg(feature = "i16")]
1426 #[inline]
1427 #[must_use]
1428 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1429 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1430 }
1431
1432 #[cfg(feature = "u16")]
1434 #[inline]
1435 #[must_use]
1436 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1437 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1438 }
1439
1440 #[cfg(feature = "i32")]
1442 #[inline]
1443 #[must_use]
1444 pub fn as_ivec3(self) -> crate::IVec3 {
1445 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1446 }
1447
1448 #[cfg(feature = "u32")]
1450 #[inline]
1451 #[must_use]
1452 pub fn as_uvec3(self) -> crate::UVec3 {
1453 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1454 }
1455
1456 #[cfg(feature = "i64")]
1458 #[inline]
1459 #[must_use]
1460 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1461 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1462 }
1463
1464 #[cfg(feature = "u64")]
1466 #[inline]
1467 #[must_use]
1468 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1469 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1470 }
1471
1472 #[cfg(feature = "isize")]
1474 #[inline]
1475 #[must_use]
1476 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1477 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1478 }
1479
1480 #[cfg(feature = "usize")]
1482 #[inline]
1483 #[must_use]
1484 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1485 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1486 }
1487}
1488
1489impl Default for DVec3 {
1490 #[inline(always)]
1491 fn default() -> Self {
1492 Self::ZERO
1493 }
1494}
1495
1496impl Div for DVec3 {
1497 type Output = Self;
1498 #[inline]
1499 fn div(self, rhs: Self) -> Self {
1500 Self::new(self.x.div(rhs.x), self.y.div(rhs.y), self.z.div(rhs.z))
1501 }
1502}
1503
1504impl Div<&Self> for DVec3 {
1505 type Output = Self;
1506 #[inline]
1507 fn div(self, rhs: &Self) -> Self {
1508 self.div(*rhs)
1509 }
1510}
1511
1512impl Div<&DVec3> for &DVec3 {
1513 type Output = DVec3;
1514 #[inline]
1515 fn div(self, rhs: &DVec3) -> DVec3 {
1516 (*self).div(*rhs)
1517 }
1518}
1519
1520impl Div<DVec3> for &DVec3 {
1521 type Output = DVec3;
1522 #[inline]
1523 fn div(self, rhs: DVec3) -> DVec3 {
1524 (*self).div(rhs)
1525 }
1526}
1527
1528impl DivAssign for DVec3 {
1529 #[inline]
1530 fn div_assign(&mut self, rhs: Self) {
1531 self.x.div_assign(rhs.x);
1532 self.y.div_assign(rhs.y);
1533 self.z.div_assign(rhs.z);
1534 }
1535}
1536
1537impl DivAssign<&Self> for DVec3 {
1538 #[inline]
1539 fn div_assign(&mut self, rhs: &Self) {
1540 self.div_assign(*rhs);
1541 }
1542}
1543
1544impl Div<f64> for DVec3 {
1545 type Output = Self;
1546 #[inline]
1547 fn div(self, rhs: f64) -> Self {
1548 Self::new(self.x.div(rhs), self.y.div(rhs), self.z.div(rhs))
1549 }
1550}
1551
1552impl Div<&f64> for DVec3 {
1553 type Output = Self;
1554 #[inline]
1555 fn div(self, rhs: &f64) -> Self {
1556 self.div(*rhs)
1557 }
1558}
1559
1560impl Div<&f64> for &DVec3 {
1561 type Output = DVec3;
1562 #[inline]
1563 fn div(self, rhs: &f64) -> DVec3 {
1564 (*self).div(*rhs)
1565 }
1566}
1567
1568impl Div<f64> for &DVec3 {
1569 type Output = DVec3;
1570 #[inline]
1571 fn div(self, rhs: f64) -> DVec3 {
1572 (*self).div(rhs)
1573 }
1574}
1575
1576impl DivAssign<f64> for DVec3 {
1577 #[inline]
1578 fn div_assign(&mut self, rhs: f64) {
1579 self.x.div_assign(rhs);
1580 self.y.div_assign(rhs);
1581 self.z.div_assign(rhs);
1582 }
1583}
1584
1585impl DivAssign<&f64> for DVec3 {
1586 #[inline]
1587 fn div_assign(&mut self, rhs: &f64) {
1588 self.div_assign(*rhs);
1589 }
1590}
1591
1592impl Div<DVec3> for f64 {
1593 type Output = DVec3;
1594 #[inline]
1595 fn div(self, rhs: DVec3) -> DVec3 {
1596 DVec3::new(self.div(rhs.x), self.div(rhs.y), self.div(rhs.z))
1597 }
1598}
1599
1600impl Div<&DVec3> for f64 {
1601 type Output = DVec3;
1602 #[inline]
1603 fn div(self, rhs: &DVec3) -> DVec3 {
1604 self.div(*rhs)
1605 }
1606}
1607
1608impl Div<&DVec3> for &f64 {
1609 type Output = DVec3;
1610 #[inline]
1611 fn div(self, rhs: &DVec3) -> DVec3 {
1612 (*self).div(*rhs)
1613 }
1614}
1615
1616impl Div<DVec3> for &f64 {
1617 type Output = DVec3;
1618 #[inline]
1619 fn div(self, rhs: DVec3) -> DVec3 {
1620 (*self).div(rhs)
1621 }
1622}
1623
1624impl Mul for DVec3 {
1625 type Output = Self;
1626 #[inline]
1627 fn mul(self, rhs: Self) -> Self {
1628 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y), self.z.mul(rhs.z))
1629 }
1630}
1631
1632impl Mul<&Self> for DVec3 {
1633 type Output = Self;
1634 #[inline]
1635 fn mul(self, rhs: &Self) -> Self {
1636 self.mul(*rhs)
1637 }
1638}
1639
1640impl Mul<&DVec3> for &DVec3 {
1641 type Output = DVec3;
1642 #[inline]
1643 fn mul(self, rhs: &DVec3) -> DVec3 {
1644 (*self).mul(*rhs)
1645 }
1646}
1647
1648impl Mul<DVec3> for &DVec3 {
1649 type Output = DVec3;
1650 #[inline]
1651 fn mul(self, rhs: DVec3) -> DVec3 {
1652 (*self).mul(rhs)
1653 }
1654}
1655
1656impl MulAssign for DVec3 {
1657 #[inline]
1658 fn mul_assign(&mut self, rhs: Self) {
1659 self.x.mul_assign(rhs.x);
1660 self.y.mul_assign(rhs.y);
1661 self.z.mul_assign(rhs.z);
1662 }
1663}
1664
1665impl MulAssign<&Self> for DVec3 {
1666 #[inline]
1667 fn mul_assign(&mut self, rhs: &Self) {
1668 self.mul_assign(*rhs);
1669 }
1670}
1671
1672impl Mul<f64> for DVec3 {
1673 type Output = Self;
1674 #[inline]
1675 fn mul(self, rhs: f64) -> Self {
1676 Self::new(self.x.mul(rhs), self.y.mul(rhs), self.z.mul(rhs))
1677 }
1678}
1679
1680impl Mul<&f64> for DVec3 {
1681 type Output = Self;
1682 #[inline]
1683 fn mul(self, rhs: &f64) -> Self {
1684 self.mul(*rhs)
1685 }
1686}
1687
1688impl Mul<&f64> for &DVec3 {
1689 type Output = DVec3;
1690 #[inline]
1691 fn mul(self, rhs: &f64) -> DVec3 {
1692 (*self).mul(*rhs)
1693 }
1694}
1695
1696impl Mul<f64> for &DVec3 {
1697 type Output = DVec3;
1698 #[inline]
1699 fn mul(self, rhs: f64) -> DVec3 {
1700 (*self).mul(rhs)
1701 }
1702}
1703
1704impl MulAssign<f64> for DVec3 {
1705 #[inline]
1706 fn mul_assign(&mut self, rhs: f64) {
1707 self.x.mul_assign(rhs);
1708 self.y.mul_assign(rhs);
1709 self.z.mul_assign(rhs);
1710 }
1711}
1712
1713impl MulAssign<&f64> for DVec3 {
1714 #[inline]
1715 fn mul_assign(&mut self, rhs: &f64) {
1716 self.mul_assign(*rhs);
1717 }
1718}
1719
1720impl Mul<DVec3> for f64 {
1721 type Output = DVec3;
1722 #[inline]
1723 fn mul(self, rhs: DVec3) -> DVec3 {
1724 DVec3::new(self.mul(rhs.x), self.mul(rhs.y), self.mul(rhs.z))
1725 }
1726}
1727
1728impl Mul<&DVec3> for f64 {
1729 type Output = DVec3;
1730 #[inline]
1731 fn mul(self, rhs: &DVec3) -> DVec3 {
1732 self.mul(*rhs)
1733 }
1734}
1735
1736impl Mul<&DVec3> for &f64 {
1737 type Output = DVec3;
1738 #[inline]
1739 fn mul(self, rhs: &DVec3) -> DVec3 {
1740 (*self).mul(*rhs)
1741 }
1742}
1743
1744impl Mul<DVec3> for &f64 {
1745 type Output = DVec3;
1746 #[inline]
1747 fn mul(self, rhs: DVec3) -> DVec3 {
1748 (*self).mul(rhs)
1749 }
1750}
1751
1752impl Add for DVec3 {
1753 type Output = Self;
1754 #[inline]
1755 fn add(self, rhs: Self) -> Self {
1756 Self::new(self.x.add(rhs.x), self.y.add(rhs.y), self.z.add(rhs.z))
1757 }
1758}
1759
1760impl Add<&Self> for DVec3 {
1761 type Output = Self;
1762 #[inline]
1763 fn add(self, rhs: &Self) -> Self {
1764 self.add(*rhs)
1765 }
1766}
1767
1768impl Add<&DVec3> for &DVec3 {
1769 type Output = DVec3;
1770 #[inline]
1771 fn add(self, rhs: &DVec3) -> DVec3 {
1772 (*self).add(*rhs)
1773 }
1774}
1775
1776impl Add<DVec3> for &DVec3 {
1777 type Output = DVec3;
1778 #[inline]
1779 fn add(self, rhs: DVec3) -> DVec3 {
1780 (*self).add(rhs)
1781 }
1782}
1783
1784impl AddAssign for DVec3 {
1785 #[inline]
1786 fn add_assign(&mut self, rhs: Self) {
1787 self.x.add_assign(rhs.x);
1788 self.y.add_assign(rhs.y);
1789 self.z.add_assign(rhs.z);
1790 }
1791}
1792
1793impl AddAssign<&Self> for DVec3 {
1794 #[inline]
1795 fn add_assign(&mut self, rhs: &Self) {
1796 self.add_assign(*rhs);
1797 }
1798}
1799
1800impl Add<f64> for DVec3 {
1801 type Output = Self;
1802 #[inline]
1803 fn add(self, rhs: f64) -> Self {
1804 Self::new(self.x.add(rhs), self.y.add(rhs), self.z.add(rhs))
1805 }
1806}
1807
1808impl Add<&f64> for DVec3 {
1809 type Output = Self;
1810 #[inline]
1811 fn add(self, rhs: &f64) -> Self {
1812 self.add(*rhs)
1813 }
1814}
1815
1816impl Add<&f64> for &DVec3 {
1817 type Output = DVec3;
1818 #[inline]
1819 fn add(self, rhs: &f64) -> DVec3 {
1820 (*self).add(*rhs)
1821 }
1822}
1823
1824impl Add<f64> for &DVec3 {
1825 type Output = DVec3;
1826 #[inline]
1827 fn add(self, rhs: f64) -> DVec3 {
1828 (*self).add(rhs)
1829 }
1830}
1831
1832impl AddAssign<f64> for DVec3 {
1833 #[inline]
1834 fn add_assign(&mut self, rhs: f64) {
1835 self.x.add_assign(rhs);
1836 self.y.add_assign(rhs);
1837 self.z.add_assign(rhs);
1838 }
1839}
1840
1841impl AddAssign<&f64> for DVec3 {
1842 #[inline]
1843 fn add_assign(&mut self, rhs: &f64) {
1844 self.add_assign(*rhs);
1845 }
1846}
1847
1848impl Add<DVec3> for f64 {
1849 type Output = DVec3;
1850 #[inline]
1851 fn add(self, rhs: DVec3) -> DVec3 {
1852 DVec3::new(self.add(rhs.x), self.add(rhs.y), self.add(rhs.z))
1853 }
1854}
1855
1856impl Add<&DVec3> for f64 {
1857 type Output = DVec3;
1858 #[inline]
1859 fn add(self, rhs: &DVec3) -> DVec3 {
1860 self.add(*rhs)
1861 }
1862}
1863
1864impl Add<&DVec3> for &f64 {
1865 type Output = DVec3;
1866 #[inline]
1867 fn add(self, rhs: &DVec3) -> DVec3 {
1868 (*self).add(*rhs)
1869 }
1870}
1871
1872impl Add<DVec3> for &f64 {
1873 type Output = DVec3;
1874 #[inline]
1875 fn add(self, rhs: DVec3) -> DVec3 {
1876 (*self).add(rhs)
1877 }
1878}
1879
1880impl Sub for DVec3 {
1881 type Output = Self;
1882 #[inline]
1883 fn sub(self, rhs: Self) -> Self {
1884 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y), self.z.sub(rhs.z))
1885 }
1886}
1887
1888impl Sub<&Self> for DVec3 {
1889 type Output = Self;
1890 #[inline]
1891 fn sub(self, rhs: &Self) -> Self {
1892 self.sub(*rhs)
1893 }
1894}
1895
1896impl Sub<&DVec3> for &DVec3 {
1897 type Output = DVec3;
1898 #[inline]
1899 fn sub(self, rhs: &DVec3) -> DVec3 {
1900 (*self).sub(*rhs)
1901 }
1902}
1903
1904impl Sub<DVec3> for &DVec3 {
1905 type Output = DVec3;
1906 #[inline]
1907 fn sub(self, rhs: DVec3) -> DVec3 {
1908 (*self).sub(rhs)
1909 }
1910}
1911
1912impl SubAssign for DVec3 {
1913 #[inline]
1914 fn sub_assign(&mut self, rhs: Self) {
1915 self.x.sub_assign(rhs.x);
1916 self.y.sub_assign(rhs.y);
1917 self.z.sub_assign(rhs.z);
1918 }
1919}
1920
1921impl SubAssign<&Self> for DVec3 {
1922 #[inline]
1923 fn sub_assign(&mut self, rhs: &Self) {
1924 self.sub_assign(*rhs);
1925 }
1926}
1927
1928impl Sub<f64> for DVec3 {
1929 type Output = Self;
1930 #[inline]
1931 fn sub(self, rhs: f64) -> Self {
1932 Self::new(self.x.sub(rhs), self.y.sub(rhs), self.z.sub(rhs))
1933 }
1934}
1935
1936impl Sub<&f64> for DVec3 {
1937 type Output = Self;
1938 #[inline]
1939 fn sub(self, rhs: &f64) -> Self {
1940 self.sub(*rhs)
1941 }
1942}
1943
1944impl Sub<&f64> for &DVec3 {
1945 type Output = DVec3;
1946 #[inline]
1947 fn sub(self, rhs: &f64) -> DVec3 {
1948 (*self).sub(*rhs)
1949 }
1950}
1951
1952impl Sub<f64> for &DVec3 {
1953 type Output = DVec3;
1954 #[inline]
1955 fn sub(self, rhs: f64) -> DVec3 {
1956 (*self).sub(rhs)
1957 }
1958}
1959
1960impl SubAssign<f64> for DVec3 {
1961 #[inline]
1962 fn sub_assign(&mut self, rhs: f64) {
1963 self.x.sub_assign(rhs);
1964 self.y.sub_assign(rhs);
1965 self.z.sub_assign(rhs);
1966 }
1967}
1968
1969impl SubAssign<&f64> for DVec3 {
1970 #[inline]
1971 fn sub_assign(&mut self, rhs: &f64) {
1972 self.sub_assign(*rhs);
1973 }
1974}
1975
1976impl Sub<DVec3> for f64 {
1977 type Output = DVec3;
1978 #[inline]
1979 fn sub(self, rhs: DVec3) -> DVec3 {
1980 DVec3::new(self.sub(rhs.x), self.sub(rhs.y), self.sub(rhs.z))
1981 }
1982}
1983
1984impl Sub<&DVec3> for f64 {
1985 type Output = DVec3;
1986 #[inline]
1987 fn sub(self, rhs: &DVec3) -> DVec3 {
1988 self.sub(*rhs)
1989 }
1990}
1991
1992impl Sub<&DVec3> for &f64 {
1993 type Output = DVec3;
1994 #[inline]
1995 fn sub(self, rhs: &DVec3) -> DVec3 {
1996 (*self).sub(*rhs)
1997 }
1998}
1999
2000impl Sub<DVec3> for &f64 {
2001 type Output = DVec3;
2002 #[inline]
2003 fn sub(self, rhs: DVec3) -> DVec3 {
2004 (*self).sub(rhs)
2005 }
2006}
2007
2008impl Rem for DVec3 {
2009 type Output = Self;
2010 #[inline]
2011 fn rem(self, rhs: Self) -> Self {
2012 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y), self.z.rem(rhs.z))
2013 }
2014}
2015
2016impl Rem<&Self> for DVec3 {
2017 type Output = Self;
2018 #[inline]
2019 fn rem(self, rhs: &Self) -> Self {
2020 self.rem(*rhs)
2021 }
2022}
2023
2024impl Rem<&DVec3> for &DVec3 {
2025 type Output = DVec3;
2026 #[inline]
2027 fn rem(self, rhs: &DVec3) -> DVec3 {
2028 (*self).rem(*rhs)
2029 }
2030}
2031
2032impl Rem<DVec3> for &DVec3 {
2033 type Output = DVec3;
2034 #[inline]
2035 fn rem(self, rhs: DVec3) -> DVec3 {
2036 (*self).rem(rhs)
2037 }
2038}
2039
2040impl RemAssign for DVec3 {
2041 #[inline]
2042 fn rem_assign(&mut self, rhs: Self) {
2043 self.x.rem_assign(rhs.x);
2044 self.y.rem_assign(rhs.y);
2045 self.z.rem_assign(rhs.z);
2046 }
2047}
2048
2049impl RemAssign<&Self> for DVec3 {
2050 #[inline]
2051 fn rem_assign(&mut self, rhs: &Self) {
2052 self.rem_assign(*rhs);
2053 }
2054}
2055
2056impl Rem<f64> for DVec3 {
2057 type Output = Self;
2058 #[inline]
2059 fn rem(self, rhs: f64) -> Self {
2060 Self::new(self.x.rem(rhs), self.y.rem(rhs), self.z.rem(rhs))
2061 }
2062}
2063
2064impl Rem<&f64> for DVec3 {
2065 type Output = Self;
2066 #[inline]
2067 fn rem(self, rhs: &f64) -> Self {
2068 self.rem(*rhs)
2069 }
2070}
2071
2072impl Rem<&f64> for &DVec3 {
2073 type Output = DVec3;
2074 #[inline]
2075 fn rem(self, rhs: &f64) -> DVec3 {
2076 (*self).rem(*rhs)
2077 }
2078}
2079
2080impl Rem<f64> for &DVec3 {
2081 type Output = DVec3;
2082 #[inline]
2083 fn rem(self, rhs: f64) -> DVec3 {
2084 (*self).rem(rhs)
2085 }
2086}
2087
2088impl RemAssign<f64> for DVec3 {
2089 #[inline]
2090 fn rem_assign(&mut self, rhs: f64) {
2091 self.x.rem_assign(rhs);
2092 self.y.rem_assign(rhs);
2093 self.z.rem_assign(rhs);
2094 }
2095}
2096
2097impl RemAssign<&f64> for DVec3 {
2098 #[inline]
2099 fn rem_assign(&mut self, rhs: &f64) {
2100 self.rem_assign(*rhs);
2101 }
2102}
2103
2104impl Rem<DVec3> for f64 {
2105 type Output = DVec3;
2106 #[inline]
2107 fn rem(self, rhs: DVec3) -> DVec3 {
2108 DVec3::new(self.rem(rhs.x), self.rem(rhs.y), self.rem(rhs.z))
2109 }
2110}
2111
2112impl Rem<&DVec3> for f64 {
2113 type Output = DVec3;
2114 #[inline]
2115 fn rem(self, rhs: &DVec3) -> DVec3 {
2116 self.rem(*rhs)
2117 }
2118}
2119
2120impl Rem<&DVec3> for &f64 {
2121 type Output = DVec3;
2122 #[inline]
2123 fn rem(self, rhs: &DVec3) -> DVec3 {
2124 (*self).rem(*rhs)
2125 }
2126}
2127
2128impl Rem<DVec3> for &f64 {
2129 type Output = DVec3;
2130 #[inline]
2131 fn rem(self, rhs: DVec3) -> DVec3 {
2132 (*self).rem(rhs)
2133 }
2134}
2135
2136impl AsRef<[f64; 3]> for DVec3 {
2137 #[inline]
2138 fn as_ref(&self) -> &[f64; 3] {
2139 unsafe { &*(self as *const Self as *const [f64; 3]) }
2140 }
2141}
2142
2143impl AsMut<[f64; 3]> for DVec3 {
2144 #[inline]
2145 fn as_mut(&mut self) -> &mut [f64; 3] {
2146 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2147 }
2148}
2149
2150impl Sum for DVec3 {
2151 #[inline]
2152 fn sum<I>(iter: I) -> Self
2153 where
2154 I: Iterator<Item = Self>,
2155 {
2156 iter.fold(Self::ZERO, Self::add)
2157 }
2158}
2159
2160impl<'a> Sum<&'a Self> for DVec3 {
2161 #[inline]
2162 fn sum<I>(iter: I) -> Self
2163 where
2164 I: Iterator<Item = &'a Self>,
2165 {
2166 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2167 }
2168}
2169
2170impl Product for DVec3 {
2171 #[inline]
2172 fn product<I>(iter: I) -> Self
2173 where
2174 I: Iterator<Item = Self>,
2175 {
2176 iter.fold(Self::ONE, Self::mul)
2177 }
2178}
2179
2180impl<'a> Product<&'a Self> for DVec3 {
2181 #[inline]
2182 fn product<I>(iter: I) -> Self
2183 where
2184 I: Iterator<Item = &'a Self>,
2185 {
2186 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2187 }
2188}
2189
2190impl Neg for DVec3 {
2191 type Output = Self;
2192 #[inline]
2193 fn neg(self) -> Self {
2194 Self::new(self.x.neg(), self.y.neg(), self.z.neg())
2195 }
2196}
2197
2198impl Neg for &DVec3 {
2199 type Output = DVec3;
2200 #[inline]
2201 fn neg(self) -> DVec3 {
2202 (*self).neg()
2203 }
2204}
2205
2206impl Index<usize> for DVec3 {
2207 type Output = f64;
2208 #[inline]
2209 #[track_caller]
2210 fn index(&self, index: usize) -> &Self::Output {
2211 match index {
2212 0 => &self.x,
2213 1 => &self.y,
2214 2 => &self.z,
2215 _ => panic!("index out of bounds"),
2216 }
2217 }
2218}
2219
2220impl IndexMut<usize> for DVec3 {
2221 #[inline]
2222 #[track_caller]
2223 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2224 match index {
2225 0 => &mut self.x,
2226 1 => &mut self.y,
2227 2 => &mut self.z,
2228 _ => panic!("index out of bounds"),
2229 }
2230 }
2231}
2232
2233impl fmt::Display for DVec3 {
2234 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2235 if let Some(p) = f.precision() {
2236 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2237 } else {
2238 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2239 }
2240 }
2241}
2242
2243impl fmt::Debug for DVec3 {
2244 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2245 fmt.debug_tuple(stringify!(DVec3))
2246 .field(&self.x)
2247 .field(&self.y)
2248 .field(&self.z)
2249 .finish()
2250 }
2251}
2252
2253impl From<[f64; 3]> for DVec3 {
2254 #[inline]
2255 fn from(a: [f64; 3]) -> Self {
2256 Self::new(a[0], a[1], a[2])
2257 }
2258}
2259
2260impl From<DVec3> for [f64; 3] {
2261 #[inline]
2262 fn from(v: DVec3) -> Self {
2263 [v.x, v.y, v.z]
2264 }
2265}
2266
2267impl From<(f64, f64, f64)> for DVec3 {
2268 #[inline]
2269 fn from(t: (f64, f64, f64)) -> Self {
2270 Self::new(t.0, t.1, t.2)
2271 }
2272}
2273
2274impl From<DVec3> for (f64, f64, f64) {
2275 #[inline]
2276 fn from(v: DVec3) -> Self {
2277 (v.x, v.y, v.z)
2278 }
2279}
2280
2281impl From<(DVec2, f64)> for DVec3 {
2282 #[inline]
2283 fn from((v, z): (DVec2, f64)) -> Self {
2284 Self::new(v.x, v.y, z)
2285 }
2286}
2287
2288impl From<Vec3> for DVec3 {
2289 #[inline]
2290 fn from(v: Vec3) -> Self {
2291 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2292 }
2293}
2294
2295#[cfg(feature = "i32")]
2296impl From<IVec3> for DVec3 {
2297 #[inline]
2298 fn from(v: IVec3) -> Self {
2299 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2300 }
2301}
2302
2303#[cfg(feature = "u32")]
2304impl From<UVec3> for DVec3 {
2305 #[inline]
2306 fn from(v: UVec3) -> Self {
2307 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2308 }
2309}
2310
2311impl From<BVec3> for DVec3 {
2312 #[inline]
2313 fn from(v: BVec3) -> Self {
2314 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2315 }
2316}
2317
2318impl From<BVec3A> for DVec3 {
2319 #[inline]
2320 fn from(v: BVec3A) -> Self {
2321 let bool_array: [bool; 3] = v.into();
2322 Self::new(
2323 f64::from(bool_array[0]),
2324 f64::from(bool_array[1]),
2325 f64::from(bool_array[2]),
2326 )
2327 }
2328}