1use crate::{f64::math, BVec3, BVec3A, DQuat, DVec2, DVec4};
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 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
99 pub const USES_WASM32_SIMD: bool = false;
100
101 #[inline(always)]
103 #[must_use]
104 pub const fn new(x: f64, y: f64, z: f64) -> Self {
105 Self { x, y, z }
106 }
107
108 #[inline]
110 #[must_use]
111 pub const fn splat(v: f64) -> Self {
112 Self::new(v, v, v)
113 }
114
115 #[inline]
117 #[must_use]
118 pub fn map<F>(self, mut f: F) -> Self
119 where
120 F: FnMut(f64) -> f64,
121 {
122 Self::new(f(self.x), f(self.y), f(self.z))
123 }
124
125 #[inline]
131 #[must_use]
132 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
133 Self::new(
134 if mask.test(0) { if_true.x } else { if_false.x },
135 if mask.test(1) { if_true.y } else { if_false.y },
136 if mask.test(2) { if_true.z } else { if_false.z },
137 )
138 }
139
140 #[inline]
142 #[must_use]
143 pub const fn from_array(a: [f64; 3]) -> Self {
144 Self::new(a[0], a[1], a[2])
145 }
146
147 #[inline]
149 #[must_use]
150 pub const fn to_array(&self) -> [f64; 3] {
151 [self.x, self.y, self.z]
152 }
153
154 #[inline]
160 #[must_use]
161 #[track_caller]
162 pub const fn from_slice(slice: &[f64]) -> Self {
163 assert!(slice.len() >= 3);
164 Self::new(slice[0], slice[1], slice[2])
165 }
166
167 #[inline]
173 #[track_caller]
174 pub fn write_to_slice(self, slice: &mut [f64]) {
175 slice[..3].copy_from_slice(&self.to_array());
176 }
177
178 #[allow(dead_code)]
180 #[inline]
181 #[must_use]
182 pub(crate) fn from_vec4(v: DVec4) -> Self {
183 Self::new(v.x, v.y, v.z)
184 }
185
186 #[inline]
188 #[must_use]
189 pub fn extend(self, w: f64) -> DVec4 {
190 DVec4::new(self.x, self.y, self.z, w)
191 }
192
193 #[inline]
197 #[must_use]
198 pub fn truncate(self) -> DVec2 {
199 use crate::swizzles::Vec3Swizzles;
200 self.xy()
201 }
202
203 #[inline]
209 #[must_use]
210 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
211 pub fn from_homogeneous(v: DVec4) -> Self {
212 glam_assert!(v.w != 0.0);
213 Self::from_vec4(v) / v.w
214 }
215
216 #[inline]
218 #[must_use]
219 pub fn to_homogeneous(self) -> DVec4 {
220 self.extend(1.0)
221 }
222
223 #[inline]
225 #[must_use]
226 pub fn with_x(mut self, x: f64) -> Self {
227 self.x = x;
228 self
229 }
230
231 #[inline]
233 #[must_use]
234 pub fn with_y(mut self, y: f64) -> Self {
235 self.y = y;
236 self
237 }
238
239 #[inline]
241 #[must_use]
242 pub fn with_z(mut self, z: f64) -> Self {
243 self.z = z;
244 self
245 }
246
247 #[inline]
249 #[must_use]
250 pub fn dot(self, rhs: Self) -> f64 {
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::new(
266 self.y * rhs.z - rhs.y * self.z,
267 self.z * rhs.x - rhs.z * self.x,
268 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::new(
282 if self.x < rhs.x { self.x } else { rhs.x },
283 if self.y < rhs.y { self.y } else { rhs.y },
284 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::new(
298 if self.x > rhs.x { self.x } else { rhs.x },
299 if self.y > rhs.y { self.y } else { rhs.y },
300 if self.z > rhs.z { self.z } else { rhs.z },
301 )
302 }
303
304 #[inline]
315 #[must_use]
316 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
317 pub fn clamp(self, min: Self, max: Self) -> Self {
318 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
319 self.max(min).min(max)
320 }
321
322 #[inline]
329 #[must_use]
330 pub fn min_element(self) -> f64 {
331 let min = |a, b| if a < b { a } else { b };
332 min(self.x, min(self.y, self.z))
333 }
334
335 #[inline]
342 #[must_use]
343 pub fn max_element(self) -> f64 {
344 let max = |a, b| if a > b { a } else { b };
345 max(self.x, max(self.y, self.z))
346 }
347
348 #[doc(alias = "argmin")]
350 #[inline]
351 #[must_use]
352 pub fn min_position(self) -> usize {
353 let mut min = self.x;
354 let mut index = 0;
355 if self.y < min {
356 min = self.y;
357 index = 1;
358 }
359 if self.z < min {
360 index = 2;
361 }
362 index
363 }
364
365 #[doc(alias = "argmax")]
367 #[inline]
368 #[must_use]
369 pub fn max_position(self) -> usize {
370 let mut max = self.x;
371 let mut index = 0;
372 if self.y > max {
373 max = self.y;
374 index = 1;
375 }
376 if self.z > max {
377 index = 2;
378 }
379 index
380 }
381
382 #[inline]
386 #[must_use]
387 pub fn element_sum(self) -> f64 {
388 self.x + self.y + self.z
389 }
390
391 #[inline]
395 #[must_use]
396 pub fn element_product(self) -> f64 {
397 self.x * self.y * self.z
398 }
399
400 #[inline]
406 #[must_use]
407 pub fn cmpeq(self, rhs: Self) -> BVec3 {
408 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
409 }
410
411 #[inline]
417 #[must_use]
418 pub fn cmpne(self, rhs: Self) -> BVec3 {
419 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
420 }
421
422 #[inline]
428 #[must_use]
429 pub fn cmpge(self, rhs: Self) -> BVec3 {
430 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
431 }
432
433 #[inline]
439 #[must_use]
440 pub fn cmpgt(self, rhs: Self) -> BVec3 {
441 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
442 }
443
444 #[inline]
450 #[must_use]
451 pub fn cmple(self, rhs: Self) -> BVec3 {
452 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
453 }
454
455 #[inline]
461 #[must_use]
462 pub fn cmplt(self, rhs: Self) -> BVec3 {
463 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
464 }
465
466 #[inline]
468 #[must_use]
469 pub fn abs(self) -> Self {
470 Self::new(math::abs(self.x), math::abs(self.y), math::abs(self.z))
471 }
472
473 #[inline]
479 #[must_use]
480 pub fn signum(self) -> Self {
481 Self::new(
482 math::signum(self.x),
483 math::signum(self.y),
484 math::signum(self.z),
485 )
486 }
487
488 #[inline]
490 #[must_use]
491 pub fn copysign(self, rhs: Self) -> Self {
492 Self::new(
493 math::copysign(self.x, rhs.x),
494 math::copysign(self.y, rhs.y),
495 math::copysign(self.z, rhs.z),
496 )
497 }
498
499 #[inline]
507 #[must_use]
508 pub fn is_negative_bitmask(self) -> u32 {
509 (self.x.is_sign_negative() as u32)
510 | ((self.y.is_sign_negative() as u32) << 1)
511 | ((self.z.is_sign_negative() as u32) << 2)
512 }
513
514 #[inline]
519 #[must_use]
520 pub fn is_negative_mask(self) -> BVec3 {
521 BVec3::new(
522 self.x.is_sign_negative(),
523 self.y.is_sign_negative(),
524 self.z.is_sign_negative(),
525 )
526 }
527
528 #[inline]
531 #[must_use]
532 pub fn is_finite(self) -> bool {
533 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
534 }
535
536 #[inline]
540 #[must_use]
541 pub fn is_finite_mask(self) -> BVec3 {
542 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
543 }
544
545 #[inline]
547 #[must_use]
548 pub fn is_nan(self) -> bool {
549 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
550 }
551
552 #[inline]
556 #[must_use]
557 pub fn is_nan_mask(self) -> BVec3 {
558 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
559 }
560
561 #[doc(alias = "magnitude")]
563 #[inline]
564 #[must_use]
565 pub fn length(self) -> f64 {
566 math::sqrt(self.dot(self))
567 }
568
569 #[allow(dead_code)]
571 fn is_non_zero(self) -> bool {
572 self.length_squared() > 0.0
573 }
574
575 #[doc(alias = "magnitude2")]
579 #[inline]
580 #[must_use]
581 pub fn length_squared(self) -> f64 {
582 self.dot(self)
583 }
584
585 #[inline]
589 #[must_use]
590 pub fn length_recip(self) -> f64 {
591 1.0 / self.length()
592 }
593
594 #[inline]
596 #[must_use]
597 pub fn distance(self, rhs: Self) -> f64 {
598 (self - rhs).length()
599 }
600
601 #[inline]
603 #[must_use]
604 pub fn distance_squared(self, rhs: Self) -> f64 {
605 (self - rhs).length_squared()
606 }
607
608 #[inline]
610 #[must_use]
611 pub fn div_euclid(self, rhs: Self) -> Self {
612 Self::new(
613 math::div_euclid(self.x, rhs.x),
614 math::div_euclid(self.y, rhs.y),
615 math::div_euclid(self.z, rhs.z),
616 )
617 }
618
619 #[inline]
623 #[must_use]
624 pub fn rem_euclid(self, rhs: Self) -> Self {
625 Self::new(
626 math::rem_euclid(self.x, rhs.x),
627 math::rem_euclid(self.y, rhs.y),
628 math::rem_euclid(self.z, rhs.z),
629 )
630 }
631
632 #[inline]
642 #[must_use]
643 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
644 pub fn normalize(self) -> Self {
645 #[allow(clippy::let_and_return)]
646 let normalized = self.mul(self.length_recip());
647 glam_assert!(normalized.is_finite());
648 normalized
649 }
650
651 #[inline]
658 #[must_use]
659 pub fn try_normalize(self) -> Option<Self> {
660 let rcp = self.length_recip();
661 if rcp.is_finite() && rcp > 0.0 {
662 Some(self * rcp)
663 } else {
664 None
665 }
666 }
667
668 #[inline]
676 #[must_use]
677 pub fn normalize_or(self, fallback: Self) -> Self {
678 let rcp = self.length_recip();
679 if rcp.is_finite() && rcp > 0.0 {
680 self * rcp
681 } else {
682 fallback
683 }
684 }
685
686 #[inline]
693 #[must_use]
694 pub fn normalize_or_zero(self) -> Self {
695 self.normalize_or(Self::ZERO)
696 }
697
698 #[inline]
702 #[must_use]
703 pub fn normalize_and_length(self) -> (Self, f64) {
704 let length = self.length();
705 let rcp = 1.0 / length;
706 if rcp.is_finite() && rcp > 0.0 {
707 (self * rcp, length)
708 } else {
709 (Self::X, 0.0)
710 }
711 }
712
713 #[inline]
717 #[must_use]
718 pub fn is_normalized(self) -> bool {
719 math::abs(self.length_squared() - 1.0) <= 2e-4
720 }
721
722 #[inline]
730 #[must_use]
731 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
732 pub fn project_onto(self, rhs: Self) -> Self {
733 let rhs_len_sq = rhs.dot(rhs);
734 glam_assert!(rhs_len_sq != 0.0);
735 rhs * (self.dot(rhs) / rhs_len_sq)
736 }
737
738 #[doc(alias("plane"))]
749 #[inline]
750 #[must_use]
751 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
752 pub fn reject_from(self, rhs: Self) -> Self {
753 self - self.project_onto(rhs)
754 }
755
756 #[inline]
764 #[must_use]
765 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
766 pub fn project_onto_normalized(self, rhs: Self) -> Self {
767 glam_assert!(rhs.is_normalized());
768 rhs * self.dot(rhs)
769 }
770
771 #[doc(alias("plane"))]
782 #[inline]
783 #[must_use]
784 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
785 pub fn reject_from_normalized(self, rhs: Self) -> Self {
786 self - self.project_onto_normalized(rhs)
787 }
788
789 #[inline]
792 #[must_use]
793 pub fn round(self) -> Self {
794 Self::new(
795 math::round(self.x),
796 math::round(self.y),
797 math::round(self.z),
798 )
799 }
800
801 #[inline]
804 #[must_use]
805 pub fn floor(self) -> Self {
806 Self::new(
807 math::floor(self.x),
808 math::floor(self.y),
809 math::floor(self.z),
810 )
811 }
812
813 #[inline]
816 #[must_use]
817 pub fn ceil(self) -> Self {
818 Self::new(math::ceil(self.x), math::ceil(self.y), math::ceil(self.z))
819 }
820
821 #[inline]
824 #[must_use]
825 pub fn trunc(self) -> Self {
826 Self::new(
827 math::trunc(self.x),
828 math::trunc(self.y),
829 math::trunc(self.z),
830 )
831 }
832
833 #[inline]
837 #[must_use]
838 pub fn step(self, rhs: Self) -> Self {
839 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
840 }
841
842 #[inline]
853 #[must_use]
854 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
855 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
856 glam_assert!(edge0.cmplt(edge1).all());
857 let t = ((self - edge0) / (edge1 - edge0)).saturate();
858 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
859 }
860
861 #[inline]
863 #[must_use]
864 pub fn saturate(self) -> Self {
865 self.clamp(Self::ZERO, Self::ONE)
866 }
867
868 #[inline]
875 #[must_use]
876 pub fn fract(self) -> Self {
877 self - self.trunc()
878 }
879
880 #[inline]
887 #[must_use]
888 pub fn fract_gl(self) -> Self {
889 self - self.floor()
890 }
891
892 #[inline]
895 #[must_use]
896 pub fn exp(self) -> Self {
897 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
898 }
899
900 #[inline]
902 #[must_use]
903 pub fn exp2(self) -> Self {
904 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
905 }
906
907 #[inline]
910 #[must_use]
911 pub fn ln(self) -> Self {
912 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
913 }
914
915 #[inline]
918 #[must_use]
919 pub fn log2(self) -> Self {
920 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
921 }
922
923 #[inline]
925 #[must_use]
926 pub fn powf(self, n: f64) -> Self {
927 Self::new(
928 math::powf(self.x, n),
929 math::powf(self.y, n),
930 math::powf(self.z, n),
931 )
932 }
933
934 #[inline]
937 #[must_use]
938 pub fn sqrt(self) -> Self {
939 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
940 }
941
942 #[inline]
944 #[must_use]
945 pub fn cos(self) -> Self {
946 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
947 }
948
949 #[inline]
951 #[must_use]
952 pub fn sin(self) -> Self {
953 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
954 }
955
956 #[inline]
958 #[must_use]
959 pub fn sin_cos(self) -> (Self, Self) {
960 let (sin_x, cos_x) = math::sin_cos(self.x);
961 let (sin_y, cos_y) = math::sin_cos(self.y);
962 let (sin_z, cos_z) = math::sin_cos(self.z);
963
964 (
965 Self::new(sin_x, sin_y, sin_z),
966 Self::new(cos_x, cos_y, cos_z),
967 )
968 }
969
970 #[inline]
972 #[must_use]
973 pub fn recip(self) -> Self {
974 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
975 }
976
977 #[doc(alias = "mix")]
989 #[inline]
990 #[must_use]
991 pub fn lerp(self, rhs: Self, s: f64) -> Self {
992 self * (1.0 - s) + rhs * s
993 }
994
995 #[doc(alias = "mix")]
1012 #[inline]
1013 #[must_use]
1014 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
1015 self + (rhs - self) * s
1016 }
1017
1018 #[inline]
1023 #[must_use]
1024 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1025 let a = rhs - self;
1026 let len = a.length();
1027 if len <= d || len <= 1e-4 {
1028 return rhs;
1029 }
1030 self + a / len * d
1031 }
1032
1033 #[inline]
1039 pub fn midpoint(self, rhs: Self) -> Self {
1040 (self + rhs) * 0.5
1041 }
1042
1043 #[inline]
1053 #[must_use]
1054 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1055 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1056 }
1057
1058 #[inline]
1064 #[must_use]
1065 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1066 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1067 glam_assert!(0.0 <= min);
1068 glam_assert!(min <= max);
1069 let length_sq = self.length_squared();
1070 if length_sq < min * min {
1071 min * (self / math::sqrt(length_sq))
1072 } else if length_sq > max * max {
1073 max * (self / math::sqrt(length_sq))
1074 } else {
1075 self
1076 }
1077 }
1078
1079 #[inline]
1085 #[must_use]
1086 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1087 pub fn clamp_length_max(self, max: f64) -> Self {
1088 glam_assert!(0.0 <= max);
1089 let length_sq = self.length_squared();
1090 if length_sq > max * max {
1091 max * (self / math::sqrt(length_sq))
1092 } else {
1093 self
1094 }
1095 }
1096
1097 #[inline]
1103 #[must_use]
1104 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1105 pub fn clamp_length_min(self, min: f64) -> Self {
1106 glam_assert!(0.0 <= min);
1107 let length_sq = self.length_squared();
1108 if length_sq < min * min {
1109 min * (self / math::sqrt(length_sq))
1110 } else {
1111 self
1112 }
1113 }
1114
1115 #[inline]
1123 #[must_use]
1124 pub fn mul_add(self, a: Self, b: Self) -> Self {
1125 Self::new(
1126 math::mul_add(self.x, a.x, b.x),
1127 math::mul_add(self.y, a.y, b.y),
1128 math::mul_add(self.z, a.z, b.z),
1129 )
1130 }
1131
1132 #[inline]
1141 #[must_use]
1142 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1143 pub fn reflect(self, normal: Self) -> Self {
1144 glam_assert!(normal.is_normalized());
1145 self - 2.0 * self.dot(normal) * normal
1146 }
1147
1148 #[inline]
1158 #[must_use]
1159 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1160 pub fn refract(self, normal: Self, eta: f64) -> Self {
1161 glam_assert!(self.is_normalized());
1162 glam_assert!(normal.is_normalized());
1163 let n_dot_i = normal.dot(self);
1164 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1165 if k >= 0.0 {
1166 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1167 } else {
1168 Self::ZERO
1169 }
1170 }
1171
1172 #[inline]
1183 #[must_use]
1184 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1185 pub fn angle_between(self, rhs: Self) -> f64 {
1186 glam_assert!(self.is_non_zero());
1187 glam_assert!(rhs.is_non_zero());
1188 math::acos_approx(
1189 self.dot(rhs)
1190 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1191 )
1192 }
1193
1194 #[doc(alias = "signed_angle")]
1210 #[inline]
1211 #[must_use]
1212 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1213 pub fn angle_to(self, rhs: Self, axis: Self) -> f64 {
1214 glam_assert!(axis.is_normalized());
1215 glam_assert!(self.is_non_zero());
1216 glam_assert!(rhs.is_non_zero());
1217 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1218 }
1219
1220 #[inline]
1222 #[must_use]
1223 pub fn rotate_x(self, angle: f64) -> Self {
1224 let (sina, cosa) = math::sin_cos(angle);
1225 Self::new(
1226 self.x,
1227 self.y * cosa - self.z * sina,
1228 self.y * sina + self.z * cosa,
1229 )
1230 }
1231
1232 #[inline]
1234 #[must_use]
1235 pub fn rotate_y(self, angle: f64) -> Self {
1236 let (sina, cosa) = math::sin_cos(angle);
1237 Self::new(
1238 self.x * cosa + self.z * sina,
1239 self.y,
1240 self.x * -sina + self.z * cosa,
1241 )
1242 }
1243
1244 #[inline]
1246 #[must_use]
1247 pub fn rotate_z(self, angle: f64) -> Self {
1248 let (sina, cosa) = math::sin_cos(angle);
1249 Self::new(
1250 self.x * cosa - self.y * sina,
1251 self.x * sina + self.y * cosa,
1252 self.z,
1253 )
1254 }
1255
1256 #[inline]
1264 #[must_use]
1265 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1266 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1267 DQuat::from_axis_angle(axis, angle) * self
1268 }
1269
1270 #[inline]
1280 #[must_use]
1281 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1282 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1283 let angle_between = self.angle_between(rhs);
1284 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1286 let axis = self
1287 .cross(rhs)
1288 .try_normalize()
1289 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1290 DQuat::from_axis_angle(axis, angle) * self
1291 }
1292
1293 #[inline]
1300 #[must_use]
1301 pub fn any_orthogonal_vector(self) -> Self {
1302 if math::abs(self.x) > math::abs(self.y) {
1304 Self::new(-self.z, 0.0, self.x) } else {
1306 Self::new(0.0, self.z, -self.y) }
1308 }
1309
1310 #[inline]
1318 #[must_use]
1319 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1320 pub fn any_orthonormal_vector(self) -> Self {
1321 glam_assert!(self.is_normalized());
1322 let sign = math::signum(self.z);
1324 let a = -1.0 / (sign + self.z);
1325 let b = self.x * self.y * a;
1326 Self::new(b, sign + self.y * self.y * a, -self.y)
1327 }
1328
1329 #[inline]
1336 #[must_use]
1337 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1338 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1339 glam_assert!(self.is_normalized());
1340 let sign = math::signum(self.z);
1342 let a = -1.0 / (sign + self.z);
1343 let b = self.x * self.y * a;
1344 (
1345 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1346 Self::new(b, sign + self.y * self.y * a, -self.y),
1347 )
1348 }
1349
1350 #[inline]
1360 #[must_use]
1361 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1362 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1363 glam_assert!(self.is_non_zero());
1364 glam_assert!(rhs.is_non_zero());
1365 let self_length = self.length();
1366 let rhs_length = rhs.length();
1367 let dot = self.dot(rhs) / (self_length * rhs_length);
1369 if math::abs(dot) < 1.0 - 3e-7 {
1371 let theta = math::acos_approx(dot);
1373 let sin_theta = math::sin(theta);
1375 let t1 = math::sin(theta * (1.0 - s));
1376 let t2 = math::sin(theta * s);
1377
1378 let result_length = self_length + (rhs_length - self_length) * s;
1380 return (self * (result_length / self_length) * t1
1382 + rhs * (result_length / rhs_length) * t2)
1383 * (1.0 / sin_theta);
1384 }
1385 if dot < 0.0 {
1386 let axis = self.any_orthogonal_vector().normalize();
1390 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1391 let result_length = self_length + (rhs_length - self_length) * s;
1393 rotation * self * (result_length / self_length)
1394 } else {
1395 self.lerp(rhs, s)
1397 }
1398 }
1399
1400 #[inline]
1402 #[must_use]
1403 pub fn as_vec3(self) -> crate::Vec3 {
1404 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1405 }
1406
1407 #[inline]
1409 #[must_use]
1410 pub fn as_vec3a(self) -> crate::Vec3A {
1411 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1412 }
1413
1414 #[cfg(feature = "i8")]
1416 #[inline]
1417 #[must_use]
1418 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1419 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1420 }
1421
1422 #[cfg(feature = "u8")]
1424 #[inline]
1425 #[must_use]
1426 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1427 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1428 }
1429
1430 #[cfg(feature = "i16")]
1432 #[inline]
1433 #[must_use]
1434 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1435 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1436 }
1437
1438 #[cfg(feature = "u16")]
1440 #[inline]
1441 #[must_use]
1442 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1443 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1444 }
1445
1446 #[cfg(feature = "i32")]
1448 #[inline]
1449 #[must_use]
1450 pub fn as_ivec3(self) -> crate::IVec3 {
1451 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1452 }
1453
1454 #[cfg(feature = "u32")]
1456 #[inline]
1457 #[must_use]
1458 pub fn as_uvec3(self) -> crate::UVec3 {
1459 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1460 }
1461
1462 #[cfg(feature = "i64")]
1464 #[inline]
1465 #[must_use]
1466 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1467 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1468 }
1469
1470 #[cfg(feature = "u64")]
1472 #[inline]
1473 #[must_use]
1474 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1475 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1476 }
1477
1478 #[cfg(feature = "isize")]
1480 #[inline]
1481 #[must_use]
1482 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1483 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1484 }
1485
1486 #[cfg(feature = "usize")]
1488 #[inline]
1489 #[must_use]
1490 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1491 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1492 }
1493}
1494
1495impl Default for DVec3 {
1496 #[inline(always)]
1497 fn default() -> Self {
1498 Self::ZERO
1499 }
1500}
1501
1502impl Div for DVec3 {
1503 type Output = Self;
1504 #[inline]
1505 fn div(self, rhs: Self) -> Self {
1506 Self::new(self.x.div(rhs.x), self.y.div(rhs.y), self.z.div(rhs.z))
1507 }
1508}
1509
1510impl Div<&Self> for DVec3 {
1511 type Output = Self;
1512 #[inline]
1513 fn div(self, rhs: &Self) -> Self {
1514 self.div(*rhs)
1515 }
1516}
1517
1518impl Div<&DVec3> for &DVec3 {
1519 type Output = DVec3;
1520 #[inline]
1521 fn div(self, rhs: &DVec3) -> DVec3 {
1522 (*self).div(*rhs)
1523 }
1524}
1525
1526impl Div<DVec3> for &DVec3 {
1527 type Output = DVec3;
1528 #[inline]
1529 fn div(self, rhs: DVec3) -> DVec3 {
1530 (*self).div(rhs)
1531 }
1532}
1533
1534impl DivAssign for DVec3 {
1535 #[inline]
1536 fn div_assign(&mut self, rhs: Self) {
1537 self.x.div_assign(rhs.x);
1538 self.y.div_assign(rhs.y);
1539 self.z.div_assign(rhs.z);
1540 }
1541}
1542
1543impl DivAssign<&Self> for DVec3 {
1544 #[inline]
1545 fn div_assign(&mut self, rhs: &Self) {
1546 self.div_assign(*rhs);
1547 }
1548}
1549
1550impl Div<f64> for DVec3 {
1551 type Output = Self;
1552 #[inline]
1553 fn div(self, rhs: f64) -> Self {
1554 Self::new(self.x.div(rhs), self.y.div(rhs), self.z.div(rhs))
1555 }
1556}
1557
1558impl Div<&f64> for DVec3 {
1559 type Output = Self;
1560 #[inline]
1561 fn div(self, rhs: &f64) -> Self {
1562 self.div(*rhs)
1563 }
1564}
1565
1566impl Div<&f64> for &DVec3 {
1567 type Output = DVec3;
1568 #[inline]
1569 fn div(self, rhs: &f64) -> DVec3 {
1570 (*self).div(*rhs)
1571 }
1572}
1573
1574impl Div<f64> for &DVec3 {
1575 type Output = DVec3;
1576 #[inline]
1577 fn div(self, rhs: f64) -> DVec3 {
1578 (*self).div(rhs)
1579 }
1580}
1581
1582impl DivAssign<f64> for DVec3 {
1583 #[inline]
1584 fn div_assign(&mut self, rhs: f64) {
1585 self.x.div_assign(rhs);
1586 self.y.div_assign(rhs);
1587 self.z.div_assign(rhs);
1588 }
1589}
1590
1591impl DivAssign<&f64> for DVec3 {
1592 #[inline]
1593 fn div_assign(&mut self, rhs: &f64) {
1594 self.div_assign(*rhs);
1595 }
1596}
1597
1598impl Div<DVec3> for f64 {
1599 type Output = DVec3;
1600 #[inline]
1601 fn div(self, rhs: DVec3) -> DVec3 {
1602 DVec3::new(self.div(rhs.x), self.div(rhs.y), self.div(rhs.z))
1603 }
1604}
1605
1606impl Div<&DVec3> for f64 {
1607 type Output = DVec3;
1608 #[inline]
1609 fn div(self, rhs: &DVec3) -> DVec3 {
1610 self.div(*rhs)
1611 }
1612}
1613
1614impl Div<&DVec3> for &f64 {
1615 type Output = DVec3;
1616 #[inline]
1617 fn div(self, rhs: &DVec3) -> DVec3 {
1618 (*self).div(*rhs)
1619 }
1620}
1621
1622impl Div<DVec3> for &f64 {
1623 type Output = DVec3;
1624 #[inline]
1625 fn div(self, rhs: DVec3) -> DVec3 {
1626 (*self).div(rhs)
1627 }
1628}
1629
1630impl Mul for DVec3 {
1631 type Output = Self;
1632 #[inline]
1633 fn mul(self, rhs: Self) -> Self {
1634 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y), self.z.mul(rhs.z))
1635 }
1636}
1637
1638impl Mul<&Self> for DVec3 {
1639 type Output = Self;
1640 #[inline]
1641 fn mul(self, rhs: &Self) -> Self {
1642 self.mul(*rhs)
1643 }
1644}
1645
1646impl Mul<&DVec3> for &DVec3 {
1647 type Output = DVec3;
1648 #[inline]
1649 fn mul(self, rhs: &DVec3) -> DVec3 {
1650 (*self).mul(*rhs)
1651 }
1652}
1653
1654impl Mul<DVec3> for &DVec3 {
1655 type Output = DVec3;
1656 #[inline]
1657 fn mul(self, rhs: DVec3) -> DVec3 {
1658 (*self).mul(rhs)
1659 }
1660}
1661
1662impl MulAssign for DVec3 {
1663 #[inline]
1664 fn mul_assign(&mut self, rhs: Self) {
1665 self.x.mul_assign(rhs.x);
1666 self.y.mul_assign(rhs.y);
1667 self.z.mul_assign(rhs.z);
1668 }
1669}
1670
1671impl MulAssign<&Self> for DVec3 {
1672 #[inline]
1673 fn mul_assign(&mut self, rhs: &Self) {
1674 self.mul_assign(*rhs);
1675 }
1676}
1677
1678impl Mul<f64> for DVec3 {
1679 type Output = Self;
1680 #[inline]
1681 fn mul(self, rhs: f64) -> Self {
1682 Self::new(self.x.mul(rhs), self.y.mul(rhs), self.z.mul(rhs))
1683 }
1684}
1685
1686impl Mul<&f64> for DVec3 {
1687 type Output = Self;
1688 #[inline]
1689 fn mul(self, rhs: &f64) -> Self {
1690 self.mul(*rhs)
1691 }
1692}
1693
1694impl Mul<&f64> for &DVec3 {
1695 type Output = DVec3;
1696 #[inline]
1697 fn mul(self, rhs: &f64) -> DVec3 {
1698 (*self).mul(*rhs)
1699 }
1700}
1701
1702impl Mul<f64> for &DVec3 {
1703 type Output = DVec3;
1704 #[inline]
1705 fn mul(self, rhs: f64) -> DVec3 {
1706 (*self).mul(rhs)
1707 }
1708}
1709
1710impl MulAssign<f64> for DVec3 {
1711 #[inline]
1712 fn mul_assign(&mut self, rhs: f64) {
1713 self.x.mul_assign(rhs);
1714 self.y.mul_assign(rhs);
1715 self.z.mul_assign(rhs);
1716 }
1717}
1718
1719impl MulAssign<&f64> for DVec3 {
1720 #[inline]
1721 fn mul_assign(&mut self, rhs: &f64) {
1722 self.mul_assign(*rhs);
1723 }
1724}
1725
1726impl Mul<DVec3> for f64 {
1727 type Output = DVec3;
1728 #[inline]
1729 fn mul(self, rhs: DVec3) -> DVec3 {
1730 DVec3::new(self.mul(rhs.x), self.mul(rhs.y), self.mul(rhs.z))
1731 }
1732}
1733
1734impl Mul<&DVec3> for f64 {
1735 type Output = DVec3;
1736 #[inline]
1737 fn mul(self, rhs: &DVec3) -> DVec3 {
1738 self.mul(*rhs)
1739 }
1740}
1741
1742impl Mul<&DVec3> for &f64 {
1743 type Output = DVec3;
1744 #[inline]
1745 fn mul(self, rhs: &DVec3) -> DVec3 {
1746 (*self).mul(*rhs)
1747 }
1748}
1749
1750impl Mul<DVec3> for &f64 {
1751 type Output = DVec3;
1752 #[inline]
1753 fn mul(self, rhs: DVec3) -> DVec3 {
1754 (*self).mul(rhs)
1755 }
1756}
1757
1758impl Add for DVec3 {
1759 type Output = Self;
1760 #[inline]
1761 fn add(self, rhs: Self) -> Self {
1762 Self::new(self.x.add(rhs.x), self.y.add(rhs.y), self.z.add(rhs.z))
1763 }
1764}
1765
1766impl Add<&Self> for DVec3 {
1767 type Output = Self;
1768 #[inline]
1769 fn add(self, rhs: &Self) -> Self {
1770 self.add(*rhs)
1771 }
1772}
1773
1774impl Add<&DVec3> for &DVec3 {
1775 type Output = DVec3;
1776 #[inline]
1777 fn add(self, rhs: &DVec3) -> DVec3 {
1778 (*self).add(*rhs)
1779 }
1780}
1781
1782impl Add<DVec3> for &DVec3 {
1783 type Output = DVec3;
1784 #[inline]
1785 fn add(self, rhs: DVec3) -> DVec3 {
1786 (*self).add(rhs)
1787 }
1788}
1789
1790impl AddAssign for DVec3 {
1791 #[inline]
1792 fn add_assign(&mut self, rhs: Self) {
1793 self.x.add_assign(rhs.x);
1794 self.y.add_assign(rhs.y);
1795 self.z.add_assign(rhs.z);
1796 }
1797}
1798
1799impl AddAssign<&Self> for DVec3 {
1800 #[inline]
1801 fn add_assign(&mut self, rhs: &Self) {
1802 self.add_assign(*rhs);
1803 }
1804}
1805
1806impl Add<f64> for DVec3 {
1807 type Output = Self;
1808 #[inline]
1809 fn add(self, rhs: f64) -> Self {
1810 Self::new(self.x.add(rhs), self.y.add(rhs), self.z.add(rhs))
1811 }
1812}
1813
1814impl Add<&f64> for DVec3 {
1815 type Output = Self;
1816 #[inline]
1817 fn add(self, rhs: &f64) -> Self {
1818 self.add(*rhs)
1819 }
1820}
1821
1822impl Add<&f64> for &DVec3 {
1823 type Output = DVec3;
1824 #[inline]
1825 fn add(self, rhs: &f64) -> DVec3 {
1826 (*self).add(*rhs)
1827 }
1828}
1829
1830impl Add<f64> for &DVec3 {
1831 type Output = DVec3;
1832 #[inline]
1833 fn add(self, rhs: f64) -> DVec3 {
1834 (*self).add(rhs)
1835 }
1836}
1837
1838impl AddAssign<f64> for DVec3 {
1839 #[inline]
1840 fn add_assign(&mut self, rhs: f64) {
1841 self.x.add_assign(rhs);
1842 self.y.add_assign(rhs);
1843 self.z.add_assign(rhs);
1844 }
1845}
1846
1847impl AddAssign<&f64> for DVec3 {
1848 #[inline]
1849 fn add_assign(&mut self, rhs: &f64) {
1850 self.add_assign(*rhs);
1851 }
1852}
1853
1854impl Add<DVec3> for f64 {
1855 type Output = DVec3;
1856 #[inline]
1857 fn add(self, rhs: DVec3) -> DVec3 {
1858 DVec3::new(self.add(rhs.x), self.add(rhs.y), self.add(rhs.z))
1859 }
1860}
1861
1862impl Add<&DVec3> for f64 {
1863 type Output = DVec3;
1864 #[inline]
1865 fn add(self, rhs: &DVec3) -> DVec3 {
1866 self.add(*rhs)
1867 }
1868}
1869
1870impl Add<&DVec3> for &f64 {
1871 type Output = DVec3;
1872 #[inline]
1873 fn add(self, rhs: &DVec3) -> DVec3 {
1874 (*self).add(*rhs)
1875 }
1876}
1877
1878impl Add<DVec3> for &f64 {
1879 type Output = DVec3;
1880 #[inline]
1881 fn add(self, rhs: DVec3) -> DVec3 {
1882 (*self).add(rhs)
1883 }
1884}
1885
1886impl Sub for DVec3 {
1887 type Output = Self;
1888 #[inline]
1889 fn sub(self, rhs: Self) -> Self {
1890 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y), self.z.sub(rhs.z))
1891 }
1892}
1893
1894impl Sub<&Self> for DVec3 {
1895 type Output = Self;
1896 #[inline]
1897 fn sub(self, rhs: &Self) -> Self {
1898 self.sub(*rhs)
1899 }
1900}
1901
1902impl Sub<&DVec3> for &DVec3 {
1903 type Output = DVec3;
1904 #[inline]
1905 fn sub(self, rhs: &DVec3) -> DVec3 {
1906 (*self).sub(*rhs)
1907 }
1908}
1909
1910impl Sub<DVec3> for &DVec3 {
1911 type Output = DVec3;
1912 #[inline]
1913 fn sub(self, rhs: DVec3) -> DVec3 {
1914 (*self).sub(rhs)
1915 }
1916}
1917
1918impl SubAssign for DVec3 {
1919 #[inline]
1920 fn sub_assign(&mut self, rhs: Self) {
1921 self.x.sub_assign(rhs.x);
1922 self.y.sub_assign(rhs.y);
1923 self.z.sub_assign(rhs.z);
1924 }
1925}
1926
1927impl SubAssign<&Self> for DVec3 {
1928 #[inline]
1929 fn sub_assign(&mut self, rhs: &Self) {
1930 self.sub_assign(*rhs);
1931 }
1932}
1933
1934impl Sub<f64> for DVec3 {
1935 type Output = Self;
1936 #[inline]
1937 fn sub(self, rhs: f64) -> Self {
1938 Self::new(self.x.sub(rhs), self.y.sub(rhs), self.z.sub(rhs))
1939 }
1940}
1941
1942impl Sub<&f64> for DVec3 {
1943 type Output = Self;
1944 #[inline]
1945 fn sub(self, rhs: &f64) -> Self {
1946 self.sub(*rhs)
1947 }
1948}
1949
1950impl Sub<&f64> for &DVec3 {
1951 type Output = DVec3;
1952 #[inline]
1953 fn sub(self, rhs: &f64) -> DVec3 {
1954 (*self).sub(*rhs)
1955 }
1956}
1957
1958impl Sub<f64> for &DVec3 {
1959 type Output = DVec3;
1960 #[inline]
1961 fn sub(self, rhs: f64) -> DVec3 {
1962 (*self).sub(rhs)
1963 }
1964}
1965
1966impl SubAssign<f64> for DVec3 {
1967 #[inline]
1968 fn sub_assign(&mut self, rhs: f64) {
1969 self.x.sub_assign(rhs);
1970 self.y.sub_assign(rhs);
1971 self.z.sub_assign(rhs);
1972 }
1973}
1974
1975impl SubAssign<&f64> for DVec3 {
1976 #[inline]
1977 fn sub_assign(&mut self, rhs: &f64) {
1978 self.sub_assign(*rhs);
1979 }
1980}
1981
1982impl Sub<DVec3> for f64 {
1983 type Output = DVec3;
1984 #[inline]
1985 fn sub(self, rhs: DVec3) -> DVec3 {
1986 DVec3::new(self.sub(rhs.x), self.sub(rhs.y), self.sub(rhs.z))
1987 }
1988}
1989
1990impl Sub<&DVec3> for f64 {
1991 type Output = DVec3;
1992 #[inline]
1993 fn sub(self, rhs: &DVec3) -> DVec3 {
1994 self.sub(*rhs)
1995 }
1996}
1997
1998impl Sub<&DVec3> for &f64 {
1999 type Output = DVec3;
2000 #[inline]
2001 fn sub(self, rhs: &DVec3) -> DVec3 {
2002 (*self).sub(*rhs)
2003 }
2004}
2005
2006impl Sub<DVec3> for &f64 {
2007 type Output = DVec3;
2008 #[inline]
2009 fn sub(self, rhs: DVec3) -> DVec3 {
2010 (*self).sub(rhs)
2011 }
2012}
2013
2014impl Rem for DVec3 {
2015 type Output = Self;
2016 #[inline]
2017 fn rem(self, rhs: Self) -> Self {
2018 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y), self.z.rem(rhs.z))
2019 }
2020}
2021
2022impl Rem<&Self> for DVec3 {
2023 type Output = Self;
2024 #[inline]
2025 fn rem(self, rhs: &Self) -> Self {
2026 self.rem(*rhs)
2027 }
2028}
2029
2030impl Rem<&DVec3> for &DVec3 {
2031 type Output = DVec3;
2032 #[inline]
2033 fn rem(self, rhs: &DVec3) -> DVec3 {
2034 (*self).rem(*rhs)
2035 }
2036}
2037
2038impl Rem<DVec3> for &DVec3 {
2039 type Output = DVec3;
2040 #[inline]
2041 fn rem(self, rhs: DVec3) -> DVec3 {
2042 (*self).rem(rhs)
2043 }
2044}
2045
2046impl RemAssign for DVec3 {
2047 #[inline]
2048 fn rem_assign(&mut self, rhs: Self) {
2049 self.x.rem_assign(rhs.x);
2050 self.y.rem_assign(rhs.y);
2051 self.z.rem_assign(rhs.z);
2052 }
2053}
2054
2055impl RemAssign<&Self> for DVec3 {
2056 #[inline]
2057 fn rem_assign(&mut self, rhs: &Self) {
2058 self.rem_assign(*rhs);
2059 }
2060}
2061
2062impl Rem<f64> for DVec3 {
2063 type Output = Self;
2064 #[inline]
2065 fn rem(self, rhs: f64) -> Self {
2066 Self::new(self.x.rem(rhs), self.y.rem(rhs), self.z.rem(rhs))
2067 }
2068}
2069
2070impl Rem<&f64> for DVec3 {
2071 type Output = Self;
2072 #[inline]
2073 fn rem(self, rhs: &f64) -> Self {
2074 self.rem(*rhs)
2075 }
2076}
2077
2078impl Rem<&f64> for &DVec3 {
2079 type Output = DVec3;
2080 #[inline]
2081 fn rem(self, rhs: &f64) -> DVec3 {
2082 (*self).rem(*rhs)
2083 }
2084}
2085
2086impl Rem<f64> for &DVec3 {
2087 type Output = DVec3;
2088 #[inline]
2089 fn rem(self, rhs: f64) -> DVec3 {
2090 (*self).rem(rhs)
2091 }
2092}
2093
2094impl RemAssign<f64> for DVec3 {
2095 #[inline]
2096 fn rem_assign(&mut self, rhs: f64) {
2097 self.x.rem_assign(rhs);
2098 self.y.rem_assign(rhs);
2099 self.z.rem_assign(rhs);
2100 }
2101}
2102
2103impl RemAssign<&f64> for DVec3 {
2104 #[inline]
2105 fn rem_assign(&mut self, rhs: &f64) {
2106 self.rem_assign(*rhs);
2107 }
2108}
2109
2110impl Rem<DVec3> for f64 {
2111 type Output = DVec3;
2112 #[inline]
2113 fn rem(self, rhs: DVec3) -> DVec3 {
2114 DVec3::new(self.rem(rhs.x), self.rem(rhs.y), self.rem(rhs.z))
2115 }
2116}
2117
2118impl Rem<&DVec3> for f64 {
2119 type Output = DVec3;
2120 #[inline]
2121 fn rem(self, rhs: &DVec3) -> DVec3 {
2122 self.rem(*rhs)
2123 }
2124}
2125
2126impl Rem<&DVec3> for &f64 {
2127 type Output = DVec3;
2128 #[inline]
2129 fn rem(self, rhs: &DVec3) -> DVec3 {
2130 (*self).rem(*rhs)
2131 }
2132}
2133
2134impl Rem<DVec3> for &f64 {
2135 type Output = DVec3;
2136 #[inline]
2137 fn rem(self, rhs: DVec3) -> DVec3 {
2138 (*self).rem(rhs)
2139 }
2140}
2141
2142impl AsRef<[f64; 3]> for DVec3 {
2143 #[inline]
2144 fn as_ref(&self) -> &[f64; 3] {
2145 unsafe { &*(self as *const Self as *const [f64; 3]) }
2146 }
2147}
2148
2149impl AsMut<[f64; 3]> for DVec3 {
2150 #[inline]
2151 fn as_mut(&mut self) -> &mut [f64; 3] {
2152 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2153 }
2154}
2155
2156impl Sum for DVec3 {
2157 #[inline]
2158 fn sum<I>(iter: I) -> Self
2159 where
2160 I: Iterator<Item = Self>,
2161 {
2162 iter.fold(Self::ZERO, Self::add)
2163 }
2164}
2165
2166impl<'a> Sum<&'a Self> for DVec3 {
2167 #[inline]
2168 fn sum<I>(iter: I) -> Self
2169 where
2170 I: Iterator<Item = &'a Self>,
2171 {
2172 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2173 }
2174}
2175
2176impl Product for DVec3 {
2177 #[inline]
2178 fn product<I>(iter: I) -> Self
2179 where
2180 I: Iterator<Item = Self>,
2181 {
2182 iter.fold(Self::ONE, Self::mul)
2183 }
2184}
2185
2186impl<'a> Product<&'a Self> for DVec3 {
2187 #[inline]
2188 fn product<I>(iter: I) -> Self
2189 where
2190 I: Iterator<Item = &'a Self>,
2191 {
2192 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2193 }
2194}
2195
2196impl Neg for DVec3 {
2197 type Output = Self;
2198 #[inline]
2199 fn neg(self) -> Self {
2200 Self::new(self.x.neg(), self.y.neg(), self.z.neg())
2201 }
2202}
2203
2204impl Neg for &DVec3 {
2205 type Output = DVec3;
2206 #[inline]
2207 fn neg(self) -> DVec3 {
2208 (*self).neg()
2209 }
2210}
2211
2212impl Index<usize> for DVec3 {
2213 type Output = f64;
2214 #[inline]
2215 #[track_caller]
2216 fn index(&self, index: usize) -> &Self::Output {
2217 match index {
2218 0 => &self.x,
2219 1 => &self.y,
2220 2 => &self.z,
2221 _ => panic!("index out of bounds"),
2222 }
2223 }
2224}
2225
2226impl IndexMut<usize> for DVec3 {
2227 #[inline]
2228 #[track_caller]
2229 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2230 match index {
2231 0 => &mut self.x,
2232 1 => &mut self.y,
2233 2 => &mut self.z,
2234 _ => panic!("index out of bounds"),
2235 }
2236 }
2237}
2238
2239impl fmt::Display for DVec3 {
2240 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2241 if let Some(p) = f.precision() {
2242 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2243 } else {
2244 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2245 }
2246 }
2247}
2248
2249impl fmt::Debug for DVec3 {
2250 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2251 fmt.debug_tuple(stringify!(DVec3))
2252 .field(&self.x)
2253 .field(&self.y)
2254 .field(&self.z)
2255 .finish()
2256 }
2257}
2258
2259impl From<[f64; 3]> for DVec3 {
2260 #[inline]
2261 fn from(a: [f64; 3]) -> Self {
2262 Self::new(a[0], a[1], a[2])
2263 }
2264}
2265
2266impl From<DVec3> for [f64; 3] {
2267 #[inline]
2268 fn from(v: DVec3) -> Self {
2269 [v.x, v.y, v.z]
2270 }
2271}
2272
2273impl From<(f64, f64, f64)> for DVec3 {
2274 #[inline]
2275 fn from(t: (f64, f64, f64)) -> Self {
2276 Self::new(t.0, t.1, t.2)
2277 }
2278}
2279
2280impl From<DVec3> for (f64, f64, f64) {
2281 #[inline]
2282 fn from(v: DVec3) -> Self {
2283 (v.x, v.y, v.z)
2284 }
2285}
2286
2287impl From<(DVec2, f64)> for DVec3 {
2288 #[inline]
2289 fn from((v, z): (DVec2, f64)) -> Self {
2290 Self::new(v.x, v.y, z)
2291 }
2292}
2293
2294impl From<Vec3> for DVec3 {
2295 #[inline]
2296 fn from(v: Vec3) -> Self {
2297 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2298 }
2299}
2300
2301#[cfg(feature = "i32")]
2302impl From<IVec3> for DVec3 {
2303 #[inline]
2304 fn from(v: IVec3) -> Self {
2305 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2306 }
2307}
2308
2309#[cfg(feature = "u32")]
2310impl From<UVec3> for DVec3 {
2311 #[inline]
2312 fn from(v: UVec3) -> Self {
2313 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2314 }
2315}
2316
2317impl From<BVec3> for DVec3 {
2318 #[inline]
2319 fn from(v: BVec3) -> Self {
2320 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2321 }
2322}
2323
2324impl From<BVec3A> for DVec3 {
2325 #[inline]
2326 fn from(v: BVec3A) -> Self {
2327 let bool_array: [bool; 3] = v.into();
2328 Self::new(
2329 f64::from(bool_array[0]),
2330 f64::from(bool_array[1]),
2331 f64::from(bool_array[2]),
2332 )
2333 }
2334}