1use crate::{f64::math, BVec3, BVec3A, DQuat, DVec2, DVec4, FloatExt};
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::{f32, ops::*};
16
17#[cfg(feature = "zerocopy")]
18use zerocopy_derive::*;
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",
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 { x: v, y: v, z: 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 {
134 x: if mask.test(0) { if_true.x } else { if_false.x },
135 y: if mask.test(1) { if_true.y } else { if_false.y },
136 z: 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 pub const fn from_slice(slice: &[f64]) -> Self {
162 assert!(slice.len() >= 3);
163 Self::new(slice[0], slice[1], slice[2])
164 }
165
166 #[inline]
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 {
182 x: v.x,
183 y: v.y,
184 z: v.z,
185 }
186 }
187
188 #[inline]
190 #[must_use]
191 pub fn extend(self, w: f64) -> DVec4 {
192 DVec4::new(self.x, self.y, self.z, w)
193 }
194
195 #[inline]
199 #[must_use]
200 pub fn truncate(self) -> DVec2 {
201 use crate::swizzles::Vec3Swizzles;
202 self.xy()
203 }
204
205 #[inline]
211 #[must_use]
212 pub fn from_homogeneous(v: DVec4) -> Self {
213 glam_assert!(v.w != 0.0);
214 Self::from_vec4(v) / v.w
215 }
216
217 #[inline]
219 #[must_use]
220 pub fn to_homogeneous(self) -> DVec4 {
221 self.extend(1.0)
222 }
223
224 #[inline]
226 #[must_use]
227 pub fn with_x(mut self, x: f64) -> Self {
228 self.x = x;
229 self
230 }
231
232 #[inline]
234 #[must_use]
235 pub fn with_y(mut self, y: f64) -> Self {
236 self.y = y;
237 self
238 }
239
240 #[inline]
242 #[must_use]
243 pub fn with_z(mut self, z: f64) -> Self {
244 self.z = z;
245 self
246 }
247
248 #[inline]
250 #[must_use]
251 pub fn dot(self, rhs: Self) -> f64 {
252 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
253 }
254
255 #[inline]
257 #[must_use]
258 pub fn dot_into_vec(self, rhs: Self) -> Self {
259 Self::splat(self.dot(rhs))
260 }
261
262 #[inline]
264 #[must_use]
265 pub fn cross(self, rhs: Self) -> Self {
266 Self {
267 x: self.y * rhs.z - rhs.y * self.z,
268 y: self.z * rhs.x - rhs.z * self.x,
269 z: self.x * rhs.y - rhs.x * self.y,
270 }
271 }
272
273 #[inline]
280 #[must_use]
281 pub fn min(self, rhs: Self) -> Self {
282 Self {
283 x: if self.x < rhs.x { self.x } else { rhs.x },
284 y: if self.y < rhs.y { self.y } else { rhs.y },
285 z: if self.z < rhs.z { self.z } else { rhs.z },
286 }
287 }
288
289 #[inline]
296 #[must_use]
297 pub fn max(self, rhs: Self) -> Self {
298 Self {
299 x: if self.x > rhs.x { self.x } else { rhs.x },
300 y: if self.y > rhs.y { self.y } else { rhs.y },
301 z: if self.z > rhs.z { self.z } else { rhs.z },
302 }
303 }
304
305 #[inline]
316 #[must_use]
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 {
471 x: math::abs(self.x),
472 y: math::abs(self.y),
473 z: math::abs(self.z),
474 }
475 }
476
477 #[inline]
483 #[must_use]
484 pub fn signum(self) -> Self {
485 Self {
486 x: math::signum(self.x),
487 y: math::signum(self.y),
488 z: math::signum(self.z),
489 }
490 }
491
492 #[inline]
494 #[must_use]
495 pub fn copysign(self, rhs: Self) -> Self {
496 Self {
497 x: math::copysign(self.x, rhs.x),
498 y: math::copysign(self.y, rhs.y),
499 z: math::copysign(self.z, rhs.z),
500 }
501 }
502
503 #[inline]
511 #[must_use]
512 pub fn is_negative_bitmask(self) -> u32 {
513 (self.x.is_sign_negative() as u32)
514 | ((self.y.is_sign_negative() as u32) << 1)
515 | ((self.z.is_sign_negative() as u32) << 2)
516 }
517
518 #[inline]
523 #[must_use]
524 pub fn is_negative_mask(self) -> BVec3 {
525 BVec3::new(
526 self.x.is_sign_negative(),
527 self.y.is_sign_negative(),
528 self.z.is_sign_negative(),
529 )
530 }
531
532 #[inline]
535 #[must_use]
536 pub fn is_finite(self) -> bool {
537 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
538 }
539
540 #[inline]
544 #[must_use]
545 pub fn is_finite_mask(self) -> BVec3 {
546 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
547 }
548
549 #[inline]
551 #[must_use]
552 pub fn is_nan(self) -> bool {
553 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
554 }
555
556 #[inline]
560 #[must_use]
561 pub fn is_nan_mask(self) -> BVec3 {
562 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
563 }
564
565 #[doc(alias = "magnitude")]
567 #[inline]
568 #[must_use]
569 pub fn length(self) -> f64 {
570 math::sqrt(self.dot(self))
571 }
572
573 #[allow(dead_code)]
575 fn is_non_zero(self) -> bool {
576 self.length_squared() > 0.0
577 }
578
579 #[doc(alias = "magnitude2")]
583 #[inline]
584 #[must_use]
585 pub fn length_squared(self) -> f64 {
586 self.dot(self)
587 }
588
589 #[inline]
593 #[must_use]
594 pub fn length_recip(self) -> f64 {
595 self.length().recip()
596 }
597
598 #[inline]
600 #[must_use]
601 pub fn distance(self, rhs: Self) -> f64 {
602 (self - rhs).length()
603 }
604
605 #[inline]
607 #[must_use]
608 pub fn distance_squared(self, rhs: Self) -> f64 {
609 (self - rhs).length_squared()
610 }
611
612 #[inline]
614 #[must_use]
615 pub fn div_euclid(self, rhs: Self) -> Self {
616 Self::new(
617 math::div_euclid(self.x, rhs.x),
618 math::div_euclid(self.y, rhs.y),
619 math::div_euclid(self.z, rhs.z),
620 )
621 }
622
623 #[inline]
627 #[must_use]
628 pub fn rem_euclid(self, rhs: Self) -> Self {
629 Self::new(
630 math::rem_euclid(self.x, rhs.x),
631 math::rem_euclid(self.y, rhs.y),
632 math::rem_euclid(self.z, rhs.z),
633 )
634 }
635
636 #[inline]
646 #[must_use]
647 pub fn normalize(self) -> Self {
648 #[allow(clippy::let_and_return)]
649 let normalized = self.mul(self.length_recip());
650 glam_assert!(normalized.is_finite());
651 normalized
652 }
653
654 #[inline]
661 #[must_use]
662 pub fn try_normalize(self) -> Option<Self> {
663 let rcp = self.length_recip();
664 if rcp.is_finite() && rcp > 0.0 {
665 Some(self * rcp)
666 } else {
667 None
668 }
669 }
670
671 #[inline]
679 #[must_use]
680 pub fn normalize_or(self, fallback: Self) -> Self {
681 let rcp = self.length_recip();
682 if rcp.is_finite() && rcp > 0.0 {
683 self * rcp
684 } else {
685 fallback
686 }
687 }
688
689 #[inline]
696 #[must_use]
697 pub fn normalize_or_zero(self) -> Self {
698 self.normalize_or(Self::ZERO)
699 }
700
701 #[inline]
705 #[must_use]
706 pub fn normalize_and_length(self) -> (Self, f64) {
707 let length = self.length();
708 let rcp = 1.0 / length;
709 if rcp.is_finite() && rcp > 0.0 {
710 (self * rcp, length)
711 } else {
712 (Self::X, 0.0)
713 }
714 }
715
716 #[inline]
720 #[must_use]
721 pub fn is_normalized(self) -> bool {
722 math::abs(self.length_squared() - 1.0) <= 2e-4
723 }
724
725 #[inline]
733 #[must_use]
734 pub fn project_onto(self, rhs: Self) -> Self {
735 let other_len_sq_rcp = rhs.dot(rhs).recip();
736 glam_assert!(other_len_sq_rcp.is_finite());
737 rhs * self.dot(rhs) * other_len_sq_rcp
738 }
739
740 #[doc(alias("plane"))]
751 #[inline]
752 #[must_use]
753 pub fn reject_from(self, rhs: Self) -> Self {
754 self - self.project_onto(rhs)
755 }
756
757 #[inline]
765 #[must_use]
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 pub fn reject_from_normalized(self, rhs: Self) -> Self {
785 self - self.project_onto_normalized(rhs)
786 }
787
788 #[inline]
791 #[must_use]
792 pub fn round(self) -> Self {
793 Self {
794 x: math::round(self.x),
795 y: math::round(self.y),
796 z: math::round(self.z),
797 }
798 }
799
800 #[inline]
803 #[must_use]
804 pub fn floor(self) -> Self {
805 Self {
806 x: math::floor(self.x),
807 y: math::floor(self.y),
808 z: math::floor(self.z),
809 }
810 }
811
812 #[inline]
815 #[must_use]
816 pub fn ceil(self) -> Self {
817 Self {
818 x: math::ceil(self.x),
819 y: math::ceil(self.y),
820 z: math::ceil(self.z),
821 }
822 }
823
824 #[inline]
827 #[must_use]
828 pub fn trunc(self) -> Self {
829 Self {
830 x: math::trunc(self.x),
831 y: math::trunc(self.y),
832 z: math::trunc(self.z),
833 }
834 }
835
836 #[inline]
840 #[must_use]
841 pub fn step(self, rhs: Self) -> Self {
842 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
843 }
844
845 #[inline]
847 #[must_use]
848 pub fn saturate(self) -> Self {
849 self.clamp(Self::ZERO, Self::ONE)
850 }
851
852 #[inline]
859 #[must_use]
860 pub fn fract(self) -> Self {
861 self - self.trunc()
862 }
863
864 #[inline]
871 #[must_use]
872 pub fn fract_gl(self) -> Self {
873 self - self.floor()
874 }
875
876 #[inline]
879 #[must_use]
880 pub fn exp(self) -> Self {
881 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
882 }
883
884 #[inline]
886 #[must_use]
887 pub fn exp2(self) -> Self {
888 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
889 }
890
891 #[inline]
894 #[must_use]
895 pub fn ln(self) -> Self {
896 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
897 }
898
899 #[inline]
902 #[must_use]
903 pub fn log2(self) -> Self {
904 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
905 }
906
907 #[inline]
909 #[must_use]
910 pub fn powf(self, n: f64) -> Self {
911 Self::new(
912 math::powf(self.x, n),
913 math::powf(self.y, n),
914 math::powf(self.z, n),
915 )
916 }
917
918 #[inline]
921 #[must_use]
922 pub fn sqrt(self) -> Self {
923 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
924 }
925
926 #[inline]
928 #[must_use]
929 pub fn cos(self) -> Self {
930 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
931 }
932
933 #[inline]
935 #[must_use]
936 pub fn sin(self) -> Self {
937 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
938 }
939
940 #[inline]
942 #[must_use]
943 pub fn sin_cos(self) -> (Self, Self) {
944 let (sin_x, cos_x) = math::sin_cos(self.x);
945 let (sin_y, cos_y) = math::sin_cos(self.y);
946 let (sin_z, cos_z) = math::sin_cos(self.z);
947
948 (
949 Self::new(sin_x, sin_y, sin_z),
950 Self::new(cos_x, cos_y, cos_z),
951 )
952 }
953
954 #[inline]
956 #[must_use]
957 pub fn recip(self) -> Self {
958 Self {
959 x: 1.0 / self.x,
960 y: 1.0 / self.y,
961 z: 1.0 / self.z,
962 }
963 }
964
965 #[doc(alias = "mix")]
971 #[inline]
972 #[must_use]
973 pub fn lerp(self, rhs: Self, s: f64) -> Self {
974 self * (1.0 - s) + rhs * s
975 }
976
977 #[inline]
982 #[must_use]
983 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
984 let a = rhs - self;
985 let len = a.length();
986 if len <= d || len <= 1e-4 {
987 return rhs;
988 }
989 self + a / len * d
990 }
991
992 #[inline]
998 pub fn midpoint(self, rhs: Self) -> Self {
999 (self + rhs) * 0.5
1000 }
1001
1002 #[inline]
1012 #[must_use]
1013 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1014 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1015 }
1016
1017 #[inline]
1023 #[must_use]
1024 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1025 glam_assert!(0.0 <= min);
1026 glam_assert!(min <= max);
1027 let length_sq = self.length_squared();
1028 if length_sq < min * min {
1029 min * (self / math::sqrt(length_sq))
1030 } else if length_sq > max * max {
1031 max * (self / math::sqrt(length_sq))
1032 } else {
1033 self
1034 }
1035 }
1036
1037 #[inline]
1043 #[must_use]
1044 pub fn clamp_length_max(self, max: f64) -> Self {
1045 glam_assert!(0.0 <= max);
1046 let length_sq = self.length_squared();
1047 if length_sq > max * max {
1048 max * (self / math::sqrt(length_sq))
1049 } else {
1050 self
1051 }
1052 }
1053
1054 #[inline]
1060 #[must_use]
1061 pub fn clamp_length_min(self, min: f64) -> Self {
1062 glam_assert!(0.0 <= min);
1063 let length_sq = self.length_squared();
1064 if length_sq < min * min {
1065 min * (self / math::sqrt(length_sq))
1066 } else {
1067 self
1068 }
1069 }
1070
1071 #[inline]
1079 #[must_use]
1080 pub fn mul_add(self, a: Self, b: Self) -> Self {
1081 Self::new(
1082 math::mul_add(self.x, a.x, b.x),
1083 math::mul_add(self.y, a.y, b.y),
1084 math::mul_add(self.z, a.z, b.z),
1085 )
1086 }
1087
1088 #[inline]
1097 #[must_use]
1098 pub fn reflect(self, normal: Self) -> Self {
1099 glam_assert!(normal.is_normalized());
1100 self - 2.0 * self.dot(normal) * normal
1101 }
1102
1103 #[inline]
1113 #[must_use]
1114 pub fn refract(self, normal: Self, eta: f64) -> Self {
1115 glam_assert!(self.is_normalized());
1116 glam_assert!(normal.is_normalized());
1117 let n_dot_i = normal.dot(self);
1118 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1119 if k >= 0.0 {
1120 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1121 } else {
1122 Self::ZERO
1123 }
1124 }
1125
1126 #[inline]
1137 #[must_use]
1138 pub fn angle_between(self, rhs: Self) -> f64 {
1139 glam_assert!(self.is_non_zero());
1140 glam_assert!(rhs.is_non_zero());
1141 math::acos_approx(
1142 self.dot(rhs)
1143 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1144 )
1145 }
1146
1147 #[doc(alias = "signed_angle")]
1163 #[inline]
1164 #[must_use]
1165 pub fn angle_to(self, rhs: Self, axis: Self) -> f64 {
1166 glam_assert!(axis.is_normalized());
1167 glam_assert!(self.is_non_zero());
1168 glam_assert!(rhs.is_non_zero());
1169 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1170 }
1171
1172 #[inline]
1174 #[must_use]
1175 pub fn rotate_x(self, angle: f64) -> Self {
1176 let (sina, cosa) = math::sin_cos(angle);
1177 Self::new(
1178 self.x,
1179 self.y * cosa - self.z * sina,
1180 self.y * sina + self.z * cosa,
1181 )
1182 }
1183
1184 #[inline]
1186 #[must_use]
1187 pub fn rotate_y(self, angle: f64) -> Self {
1188 let (sina, cosa) = math::sin_cos(angle);
1189 Self::new(
1190 self.x * cosa + self.z * sina,
1191 self.y,
1192 self.x * -sina + self.z * cosa,
1193 )
1194 }
1195
1196 #[inline]
1198 #[must_use]
1199 pub fn rotate_z(self, angle: f64) -> Self {
1200 let (sina, cosa) = math::sin_cos(angle);
1201 Self::new(
1202 self.x * cosa - self.y * sina,
1203 self.x * sina + self.y * cosa,
1204 self.z,
1205 )
1206 }
1207
1208 #[inline]
1216 #[must_use]
1217 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1218 DQuat::from_axis_angle(axis, angle) * self
1219 }
1220
1221 #[inline]
1227 #[must_use]
1228 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1229 let angle_between = self.angle_between(rhs);
1230 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1232 let axis = self
1233 .cross(rhs)
1234 .try_normalize()
1235 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1236 DQuat::from_axis_angle(axis, angle) * self
1237 }
1238
1239 #[inline]
1246 #[must_use]
1247 pub fn any_orthogonal_vector(self) -> Self {
1248 if math::abs(self.x) > math::abs(self.y) {
1250 Self::new(-self.z, 0.0, self.x) } else {
1252 Self::new(0.0, self.z, -self.y) }
1254 }
1255
1256 #[inline]
1264 #[must_use]
1265 pub fn any_orthonormal_vector(self) -> Self {
1266 glam_assert!(self.is_normalized());
1267 let sign = math::signum(self.z);
1269 let a = -1.0 / (sign + self.z);
1270 let b = self.x * self.y * a;
1271 Self::new(b, sign + self.y * self.y * a, -self.y)
1272 }
1273
1274 #[inline]
1281 #[must_use]
1282 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1283 glam_assert!(self.is_normalized());
1284 let sign = math::signum(self.z);
1286 let a = -1.0 / (sign + self.z);
1287 let b = self.x * self.y * a;
1288 (
1289 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1290 Self::new(b, sign + self.y * self.y * a, -self.y),
1291 )
1292 }
1293
1294 #[inline]
1300 #[must_use]
1301 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1302 let self_length = self.length();
1303 let rhs_length = rhs.length();
1304 let dot = self.dot(rhs) / (self_length * rhs_length);
1306 if math::abs(dot) < 1.0 - 3e-7 {
1308 let theta = math::acos_approx(dot);
1310 let sin_theta = math::sin(theta);
1312 let t1 = math::sin(theta * (1.0 - s));
1313 let t2 = math::sin(theta * s);
1314
1315 let result_length = self_length.lerp(rhs_length, s);
1317 return (self * (result_length / self_length) * t1
1319 + rhs * (result_length / rhs_length) * t2)
1320 * sin_theta.recip();
1321 }
1322 if dot < 0.0 {
1323 let axis = self.any_orthogonal_vector().normalize();
1327 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1328 let result_length = self_length.lerp(rhs_length, s);
1330 rotation * self * (result_length / self_length)
1331 } else {
1332 self.lerp(rhs, s)
1334 }
1335 }
1336
1337 #[inline]
1339 #[must_use]
1340 pub fn as_vec3(self) -> crate::Vec3 {
1341 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1342 }
1343
1344 #[inline]
1346 #[must_use]
1347 pub fn as_vec3a(self) -> crate::Vec3A {
1348 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1349 }
1350
1351 #[cfg(feature = "i8")]
1353 #[inline]
1354 #[must_use]
1355 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1356 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1357 }
1358
1359 #[cfg(feature = "u8")]
1361 #[inline]
1362 #[must_use]
1363 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1364 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1365 }
1366
1367 #[cfg(feature = "i16")]
1369 #[inline]
1370 #[must_use]
1371 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1372 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1373 }
1374
1375 #[cfg(feature = "u16")]
1377 #[inline]
1378 #[must_use]
1379 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1380 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1381 }
1382
1383 #[cfg(feature = "i32")]
1385 #[inline]
1386 #[must_use]
1387 pub fn as_ivec3(self) -> crate::IVec3 {
1388 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1389 }
1390
1391 #[cfg(feature = "u32")]
1393 #[inline]
1394 #[must_use]
1395 pub fn as_uvec3(self) -> crate::UVec3 {
1396 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1397 }
1398
1399 #[cfg(feature = "i64")]
1401 #[inline]
1402 #[must_use]
1403 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1404 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1405 }
1406
1407 #[cfg(feature = "u64")]
1409 #[inline]
1410 #[must_use]
1411 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1412 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1413 }
1414
1415 #[cfg(feature = "isize")]
1417 #[inline]
1418 #[must_use]
1419 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1420 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1421 }
1422
1423 #[cfg(feature = "usize")]
1425 #[inline]
1426 #[must_use]
1427 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1428 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1429 }
1430}
1431
1432impl Default for DVec3 {
1433 #[inline(always)]
1434 fn default() -> Self {
1435 Self::ZERO
1436 }
1437}
1438
1439impl Div for DVec3 {
1440 type Output = Self;
1441 #[inline]
1442 fn div(self, rhs: Self) -> Self {
1443 Self {
1444 x: self.x.div(rhs.x),
1445 y: self.y.div(rhs.y),
1446 z: self.z.div(rhs.z),
1447 }
1448 }
1449}
1450
1451impl Div<&Self> for DVec3 {
1452 type Output = Self;
1453 #[inline]
1454 fn div(self, rhs: &Self) -> Self {
1455 self.div(*rhs)
1456 }
1457}
1458
1459impl Div<&DVec3> for &DVec3 {
1460 type Output = DVec3;
1461 #[inline]
1462 fn div(self, rhs: &DVec3) -> DVec3 {
1463 (*self).div(*rhs)
1464 }
1465}
1466
1467impl Div<DVec3> for &DVec3 {
1468 type Output = DVec3;
1469 #[inline]
1470 fn div(self, rhs: DVec3) -> DVec3 {
1471 (*self).div(rhs)
1472 }
1473}
1474
1475impl DivAssign for DVec3 {
1476 #[inline]
1477 fn div_assign(&mut self, rhs: Self) {
1478 self.x.div_assign(rhs.x);
1479 self.y.div_assign(rhs.y);
1480 self.z.div_assign(rhs.z);
1481 }
1482}
1483
1484impl DivAssign<&Self> for DVec3 {
1485 #[inline]
1486 fn div_assign(&mut self, rhs: &Self) {
1487 self.div_assign(*rhs);
1488 }
1489}
1490
1491impl Div<f64> for DVec3 {
1492 type Output = Self;
1493 #[inline]
1494 fn div(self, rhs: f64) -> Self {
1495 Self {
1496 x: self.x.div(rhs),
1497 y: self.y.div(rhs),
1498 z: self.z.div(rhs),
1499 }
1500 }
1501}
1502
1503impl Div<&f64> for DVec3 {
1504 type Output = Self;
1505 #[inline]
1506 fn div(self, rhs: &f64) -> Self {
1507 self.div(*rhs)
1508 }
1509}
1510
1511impl Div<&f64> for &DVec3 {
1512 type Output = DVec3;
1513 #[inline]
1514 fn div(self, rhs: &f64) -> DVec3 {
1515 (*self).div(*rhs)
1516 }
1517}
1518
1519impl Div<f64> for &DVec3 {
1520 type Output = DVec3;
1521 #[inline]
1522 fn div(self, rhs: f64) -> DVec3 {
1523 (*self).div(rhs)
1524 }
1525}
1526
1527impl DivAssign<f64> for DVec3 {
1528 #[inline]
1529 fn div_assign(&mut self, rhs: f64) {
1530 self.x.div_assign(rhs);
1531 self.y.div_assign(rhs);
1532 self.z.div_assign(rhs);
1533 }
1534}
1535
1536impl DivAssign<&f64> for DVec3 {
1537 #[inline]
1538 fn div_assign(&mut self, rhs: &f64) {
1539 self.div_assign(*rhs);
1540 }
1541}
1542
1543impl Div<DVec3> for f64 {
1544 type Output = DVec3;
1545 #[inline]
1546 fn div(self, rhs: DVec3) -> DVec3 {
1547 DVec3 {
1548 x: self.div(rhs.x),
1549 y: self.div(rhs.y),
1550 z: self.div(rhs.z),
1551 }
1552 }
1553}
1554
1555impl Div<&DVec3> for f64 {
1556 type Output = DVec3;
1557 #[inline]
1558 fn div(self, rhs: &DVec3) -> DVec3 {
1559 self.div(*rhs)
1560 }
1561}
1562
1563impl Div<&DVec3> for &f64 {
1564 type Output = DVec3;
1565 #[inline]
1566 fn div(self, rhs: &DVec3) -> DVec3 {
1567 (*self).div(*rhs)
1568 }
1569}
1570
1571impl Div<DVec3> for &f64 {
1572 type Output = DVec3;
1573 #[inline]
1574 fn div(self, rhs: DVec3) -> DVec3 {
1575 (*self).div(rhs)
1576 }
1577}
1578
1579impl Mul for DVec3 {
1580 type Output = Self;
1581 #[inline]
1582 fn mul(self, rhs: Self) -> Self {
1583 Self {
1584 x: self.x.mul(rhs.x),
1585 y: self.y.mul(rhs.y),
1586 z: self.z.mul(rhs.z),
1587 }
1588 }
1589}
1590
1591impl Mul<&Self> for DVec3 {
1592 type Output = Self;
1593 #[inline]
1594 fn mul(self, rhs: &Self) -> Self {
1595 self.mul(*rhs)
1596 }
1597}
1598
1599impl Mul<&DVec3> for &DVec3 {
1600 type Output = DVec3;
1601 #[inline]
1602 fn mul(self, rhs: &DVec3) -> DVec3 {
1603 (*self).mul(*rhs)
1604 }
1605}
1606
1607impl Mul<DVec3> for &DVec3 {
1608 type Output = DVec3;
1609 #[inline]
1610 fn mul(self, rhs: DVec3) -> DVec3 {
1611 (*self).mul(rhs)
1612 }
1613}
1614
1615impl MulAssign for DVec3 {
1616 #[inline]
1617 fn mul_assign(&mut self, rhs: Self) {
1618 self.x.mul_assign(rhs.x);
1619 self.y.mul_assign(rhs.y);
1620 self.z.mul_assign(rhs.z);
1621 }
1622}
1623
1624impl MulAssign<&Self> for DVec3 {
1625 #[inline]
1626 fn mul_assign(&mut self, rhs: &Self) {
1627 self.mul_assign(*rhs);
1628 }
1629}
1630
1631impl Mul<f64> for DVec3 {
1632 type Output = Self;
1633 #[inline]
1634 fn mul(self, rhs: f64) -> Self {
1635 Self {
1636 x: self.x.mul(rhs),
1637 y: self.y.mul(rhs),
1638 z: self.z.mul(rhs),
1639 }
1640 }
1641}
1642
1643impl Mul<&f64> for DVec3 {
1644 type Output = Self;
1645 #[inline]
1646 fn mul(self, rhs: &f64) -> Self {
1647 self.mul(*rhs)
1648 }
1649}
1650
1651impl Mul<&f64> for &DVec3 {
1652 type Output = DVec3;
1653 #[inline]
1654 fn mul(self, rhs: &f64) -> DVec3 {
1655 (*self).mul(*rhs)
1656 }
1657}
1658
1659impl Mul<f64> for &DVec3 {
1660 type Output = DVec3;
1661 #[inline]
1662 fn mul(self, rhs: f64) -> DVec3 {
1663 (*self).mul(rhs)
1664 }
1665}
1666
1667impl MulAssign<f64> for DVec3 {
1668 #[inline]
1669 fn mul_assign(&mut self, rhs: f64) {
1670 self.x.mul_assign(rhs);
1671 self.y.mul_assign(rhs);
1672 self.z.mul_assign(rhs);
1673 }
1674}
1675
1676impl MulAssign<&f64> for DVec3 {
1677 #[inline]
1678 fn mul_assign(&mut self, rhs: &f64) {
1679 self.mul_assign(*rhs);
1680 }
1681}
1682
1683impl Mul<DVec3> for f64 {
1684 type Output = DVec3;
1685 #[inline]
1686 fn mul(self, rhs: DVec3) -> DVec3 {
1687 DVec3 {
1688 x: self.mul(rhs.x),
1689 y: self.mul(rhs.y),
1690 z: self.mul(rhs.z),
1691 }
1692 }
1693}
1694
1695impl Mul<&DVec3> for f64 {
1696 type Output = DVec3;
1697 #[inline]
1698 fn mul(self, rhs: &DVec3) -> DVec3 {
1699 self.mul(*rhs)
1700 }
1701}
1702
1703impl Mul<&DVec3> for &f64 {
1704 type Output = DVec3;
1705 #[inline]
1706 fn mul(self, rhs: &DVec3) -> DVec3 {
1707 (*self).mul(*rhs)
1708 }
1709}
1710
1711impl Mul<DVec3> for &f64 {
1712 type Output = DVec3;
1713 #[inline]
1714 fn mul(self, rhs: DVec3) -> DVec3 {
1715 (*self).mul(rhs)
1716 }
1717}
1718
1719impl Add for DVec3 {
1720 type Output = Self;
1721 #[inline]
1722 fn add(self, rhs: Self) -> Self {
1723 Self {
1724 x: self.x.add(rhs.x),
1725 y: self.y.add(rhs.y),
1726 z: self.z.add(rhs.z),
1727 }
1728 }
1729}
1730
1731impl Add<&Self> for DVec3 {
1732 type Output = Self;
1733 #[inline]
1734 fn add(self, rhs: &Self) -> Self {
1735 self.add(*rhs)
1736 }
1737}
1738
1739impl Add<&DVec3> for &DVec3 {
1740 type Output = DVec3;
1741 #[inline]
1742 fn add(self, rhs: &DVec3) -> DVec3 {
1743 (*self).add(*rhs)
1744 }
1745}
1746
1747impl Add<DVec3> for &DVec3 {
1748 type Output = DVec3;
1749 #[inline]
1750 fn add(self, rhs: DVec3) -> DVec3 {
1751 (*self).add(rhs)
1752 }
1753}
1754
1755impl AddAssign for DVec3 {
1756 #[inline]
1757 fn add_assign(&mut self, rhs: Self) {
1758 self.x.add_assign(rhs.x);
1759 self.y.add_assign(rhs.y);
1760 self.z.add_assign(rhs.z);
1761 }
1762}
1763
1764impl AddAssign<&Self> for DVec3 {
1765 #[inline]
1766 fn add_assign(&mut self, rhs: &Self) {
1767 self.add_assign(*rhs);
1768 }
1769}
1770
1771impl Add<f64> for DVec3 {
1772 type Output = Self;
1773 #[inline]
1774 fn add(self, rhs: f64) -> Self {
1775 Self {
1776 x: self.x.add(rhs),
1777 y: self.y.add(rhs),
1778 z: self.z.add(rhs),
1779 }
1780 }
1781}
1782
1783impl Add<&f64> for DVec3 {
1784 type Output = Self;
1785 #[inline]
1786 fn add(self, rhs: &f64) -> Self {
1787 self.add(*rhs)
1788 }
1789}
1790
1791impl Add<&f64> for &DVec3 {
1792 type Output = DVec3;
1793 #[inline]
1794 fn add(self, rhs: &f64) -> DVec3 {
1795 (*self).add(*rhs)
1796 }
1797}
1798
1799impl Add<f64> for &DVec3 {
1800 type Output = DVec3;
1801 #[inline]
1802 fn add(self, rhs: f64) -> DVec3 {
1803 (*self).add(rhs)
1804 }
1805}
1806
1807impl AddAssign<f64> for DVec3 {
1808 #[inline]
1809 fn add_assign(&mut self, rhs: f64) {
1810 self.x.add_assign(rhs);
1811 self.y.add_assign(rhs);
1812 self.z.add_assign(rhs);
1813 }
1814}
1815
1816impl AddAssign<&f64> for DVec3 {
1817 #[inline]
1818 fn add_assign(&mut self, rhs: &f64) {
1819 self.add_assign(*rhs);
1820 }
1821}
1822
1823impl Add<DVec3> for f64 {
1824 type Output = DVec3;
1825 #[inline]
1826 fn add(self, rhs: DVec3) -> DVec3 {
1827 DVec3 {
1828 x: self.add(rhs.x),
1829 y: self.add(rhs.y),
1830 z: self.add(rhs.z),
1831 }
1832 }
1833}
1834
1835impl Add<&DVec3> for f64 {
1836 type Output = DVec3;
1837 #[inline]
1838 fn add(self, rhs: &DVec3) -> DVec3 {
1839 self.add(*rhs)
1840 }
1841}
1842
1843impl Add<&DVec3> for &f64 {
1844 type Output = DVec3;
1845 #[inline]
1846 fn add(self, rhs: &DVec3) -> DVec3 {
1847 (*self).add(*rhs)
1848 }
1849}
1850
1851impl Add<DVec3> for &f64 {
1852 type Output = DVec3;
1853 #[inline]
1854 fn add(self, rhs: DVec3) -> DVec3 {
1855 (*self).add(rhs)
1856 }
1857}
1858
1859impl Sub for DVec3 {
1860 type Output = Self;
1861 #[inline]
1862 fn sub(self, rhs: Self) -> Self {
1863 Self {
1864 x: self.x.sub(rhs.x),
1865 y: self.y.sub(rhs.y),
1866 z: self.z.sub(rhs.z),
1867 }
1868 }
1869}
1870
1871impl Sub<&Self> for DVec3 {
1872 type Output = Self;
1873 #[inline]
1874 fn sub(self, rhs: &Self) -> Self {
1875 self.sub(*rhs)
1876 }
1877}
1878
1879impl Sub<&DVec3> for &DVec3 {
1880 type Output = DVec3;
1881 #[inline]
1882 fn sub(self, rhs: &DVec3) -> DVec3 {
1883 (*self).sub(*rhs)
1884 }
1885}
1886
1887impl Sub<DVec3> for &DVec3 {
1888 type Output = DVec3;
1889 #[inline]
1890 fn sub(self, rhs: DVec3) -> DVec3 {
1891 (*self).sub(rhs)
1892 }
1893}
1894
1895impl SubAssign for DVec3 {
1896 #[inline]
1897 fn sub_assign(&mut self, rhs: Self) {
1898 self.x.sub_assign(rhs.x);
1899 self.y.sub_assign(rhs.y);
1900 self.z.sub_assign(rhs.z);
1901 }
1902}
1903
1904impl SubAssign<&Self> for DVec3 {
1905 #[inline]
1906 fn sub_assign(&mut self, rhs: &Self) {
1907 self.sub_assign(*rhs);
1908 }
1909}
1910
1911impl Sub<f64> for DVec3 {
1912 type Output = Self;
1913 #[inline]
1914 fn sub(self, rhs: f64) -> Self {
1915 Self {
1916 x: self.x.sub(rhs),
1917 y: self.y.sub(rhs),
1918 z: self.z.sub(rhs),
1919 }
1920 }
1921}
1922
1923impl Sub<&f64> for DVec3 {
1924 type Output = Self;
1925 #[inline]
1926 fn sub(self, rhs: &f64) -> Self {
1927 self.sub(*rhs)
1928 }
1929}
1930
1931impl Sub<&f64> for &DVec3 {
1932 type Output = DVec3;
1933 #[inline]
1934 fn sub(self, rhs: &f64) -> DVec3 {
1935 (*self).sub(*rhs)
1936 }
1937}
1938
1939impl Sub<f64> for &DVec3 {
1940 type Output = DVec3;
1941 #[inline]
1942 fn sub(self, rhs: f64) -> DVec3 {
1943 (*self).sub(rhs)
1944 }
1945}
1946
1947impl SubAssign<f64> for DVec3 {
1948 #[inline]
1949 fn sub_assign(&mut self, rhs: f64) {
1950 self.x.sub_assign(rhs);
1951 self.y.sub_assign(rhs);
1952 self.z.sub_assign(rhs);
1953 }
1954}
1955
1956impl SubAssign<&f64> for DVec3 {
1957 #[inline]
1958 fn sub_assign(&mut self, rhs: &f64) {
1959 self.sub_assign(*rhs);
1960 }
1961}
1962
1963impl Sub<DVec3> for f64 {
1964 type Output = DVec3;
1965 #[inline]
1966 fn sub(self, rhs: DVec3) -> DVec3 {
1967 DVec3 {
1968 x: self.sub(rhs.x),
1969 y: self.sub(rhs.y),
1970 z: self.sub(rhs.z),
1971 }
1972 }
1973}
1974
1975impl Sub<&DVec3> for f64 {
1976 type Output = DVec3;
1977 #[inline]
1978 fn sub(self, rhs: &DVec3) -> DVec3 {
1979 self.sub(*rhs)
1980 }
1981}
1982
1983impl Sub<&DVec3> for &f64 {
1984 type Output = DVec3;
1985 #[inline]
1986 fn sub(self, rhs: &DVec3) -> DVec3 {
1987 (*self).sub(*rhs)
1988 }
1989}
1990
1991impl Sub<DVec3> for &f64 {
1992 type Output = DVec3;
1993 #[inline]
1994 fn sub(self, rhs: DVec3) -> DVec3 {
1995 (*self).sub(rhs)
1996 }
1997}
1998
1999impl Rem for DVec3 {
2000 type Output = Self;
2001 #[inline]
2002 fn rem(self, rhs: Self) -> Self {
2003 Self {
2004 x: self.x.rem(rhs.x),
2005 y: self.y.rem(rhs.y),
2006 z: self.z.rem(rhs.z),
2007 }
2008 }
2009}
2010
2011impl Rem<&Self> for DVec3 {
2012 type Output = Self;
2013 #[inline]
2014 fn rem(self, rhs: &Self) -> Self {
2015 self.rem(*rhs)
2016 }
2017}
2018
2019impl Rem<&DVec3> for &DVec3 {
2020 type Output = DVec3;
2021 #[inline]
2022 fn rem(self, rhs: &DVec3) -> DVec3 {
2023 (*self).rem(*rhs)
2024 }
2025}
2026
2027impl Rem<DVec3> for &DVec3 {
2028 type Output = DVec3;
2029 #[inline]
2030 fn rem(self, rhs: DVec3) -> DVec3 {
2031 (*self).rem(rhs)
2032 }
2033}
2034
2035impl RemAssign for DVec3 {
2036 #[inline]
2037 fn rem_assign(&mut self, rhs: Self) {
2038 self.x.rem_assign(rhs.x);
2039 self.y.rem_assign(rhs.y);
2040 self.z.rem_assign(rhs.z);
2041 }
2042}
2043
2044impl RemAssign<&Self> for DVec3 {
2045 #[inline]
2046 fn rem_assign(&mut self, rhs: &Self) {
2047 self.rem_assign(*rhs);
2048 }
2049}
2050
2051impl Rem<f64> for DVec3 {
2052 type Output = Self;
2053 #[inline]
2054 fn rem(self, rhs: f64) -> Self {
2055 Self {
2056 x: self.x.rem(rhs),
2057 y: self.y.rem(rhs),
2058 z: self.z.rem(rhs),
2059 }
2060 }
2061}
2062
2063impl Rem<&f64> for DVec3 {
2064 type Output = Self;
2065 #[inline]
2066 fn rem(self, rhs: &f64) -> Self {
2067 self.rem(*rhs)
2068 }
2069}
2070
2071impl Rem<&f64> for &DVec3 {
2072 type Output = DVec3;
2073 #[inline]
2074 fn rem(self, rhs: &f64) -> DVec3 {
2075 (*self).rem(*rhs)
2076 }
2077}
2078
2079impl Rem<f64> for &DVec3 {
2080 type Output = DVec3;
2081 #[inline]
2082 fn rem(self, rhs: f64) -> DVec3 {
2083 (*self).rem(rhs)
2084 }
2085}
2086
2087impl RemAssign<f64> for DVec3 {
2088 #[inline]
2089 fn rem_assign(&mut self, rhs: f64) {
2090 self.x.rem_assign(rhs);
2091 self.y.rem_assign(rhs);
2092 self.z.rem_assign(rhs);
2093 }
2094}
2095
2096impl RemAssign<&f64> for DVec3 {
2097 #[inline]
2098 fn rem_assign(&mut self, rhs: &f64) {
2099 self.rem_assign(*rhs);
2100 }
2101}
2102
2103impl Rem<DVec3> for f64 {
2104 type Output = DVec3;
2105 #[inline]
2106 fn rem(self, rhs: DVec3) -> DVec3 {
2107 DVec3 {
2108 x: self.rem(rhs.x),
2109 y: self.rem(rhs.y),
2110 z: self.rem(rhs.z),
2111 }
2112 }
2113}
2114
2115impl Rem<&DVec3> for f64 {
2116 type Output = DVec3;
2117 #[inline]
2118 fn rem(self, rhs: &DVec3) -> DVec3 {
2119 self.rem(*rhs)
2120 }
2121}
2122
2123impl Rem<&DVec3> for &f64 {
2124 type Output = DVec3;
2125 #[inline]
2126 fn rem(self, rhs: &DVec3) -> DVec3 {
2127 (*self).rem(*rhs)
2128 }
2129}
2130
2131impl Rem<DVec3> for &f64 {
2132 type Output = DVec3;
2133 #[inline]
2134 fn rem(self, rhs: DVec3) -> DVec3 {
2135 (*self).rem(rhs)
2136 }
2137}
2138
2139impl AsRef<[f64; 3]> for DVec3 {
2140 #[inline]
2141 fn as_ref(&self) -> &[f64; 3] {
2142 unsafe { &*(self as *const Self as *const [f64; 3]) }
2143 }
2144}
2145
2146impl AsMut<[f64; 3]> for DVec3 {
2147 #[inline]
2148 fn as_mut(&mut self) -> &mut [f64; 3] {
2149 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2150 }
2151}
2152
2153impl Sum for DVec3 {
2154 #[inline]
2155 fn sum<I>(iter: I) -> Self
2156 where
2157 I: Iterator<Item = Self>,
2158 {
2159 iter.fold(Self::ZERO, Self::add)
2160 }
2161}
2162
2163impl<'a> Sum<&'a Self> for DVec3 {
2164 #[inline]
2165 fn sum<I>(iter: I) -> Self
2166 where
2167 I: Iterator<Item = &'a Self>,
2168 {
2169 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2170 }
2171}
2172
2173impl Product for DVec3 {
2174 #[inline]
2175 fn product<I>(iter: I) -> Self
2176 where
2177 I: Iterator<Item = Self>,
2178 {
2179 iter.fold(Self::ONE, Self::mul)
2180 }
2181}
2182
2183impl<'a> Product<&'a Self> for DVec3 {
2184 #[inline]
2185 fn product<I>(iter: I) -> Self
2186 where
2187 I: Iterator<Item = &'a Self>,
2188 {
2189 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2190 }
2191}
2192
2193impl Neg for DVec3 {
2194 type Output = Self;
2195 #[inline]
2196 fn neg(self) -> Self {
2197 Self {
2198 x: self.x.neg(),
2199 y: self.y.neg(),
2200 z: self.z.neg(),
2201 }
2202 }
2203}
2204
2205impl Neg for &DVec3 {
2206 type Output = DVec3;
2207 #[inline]
2208 fn neg(self) -> DVec3 {
2209 (*self).neg()
2210 }
2211}
2212
2213impl Index<usize> for DVec3 {
2214 type Output = f64;
2215 #[inline]
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 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2229 match index {
2230 0 => &mut self.x,
2231 1 => &mut self.y,
2232 2 => &mut self.z,
2233 _ => panic!("index out of bounds"),
2234 }
2235 }
2236}
2237
2238impl fmt::Display for DVec3 {
2239 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2240 if let Some(p) = f.precision() {
2241 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2242 } else {
2243 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2244 }
2245 }
2246}
2247
2248impl fmt::Debug for DVec3 {
2249 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2250 fmt.debug_tuple(stringify!(DVec3))
2251 .field(&self.x)
2252 .field(&self.y)
2253 .field(&self.z)
2254 .finish()
2255 }
2256}
2257
2258impl From<[f64; 3]> for DVec3 {
2259 #[inline]
2260 fn from(a: [f64; 3]) -> Self {
2261 Self::new(a[0], a[1], a[2])
2262 }
2263}
2264
2265impl From<DVec3> for [f64; 3] {
2266 #[inline]
2267 fn from(v: DVec3) -> Self {
2268 [v.x, v.y, v.z]
2269 }
2270}
2271
2272impl From<(f64, f64, f64)> for DVec3 {
2273 #[inline]
2274 fn from(t: (f64, f64, f64)) -> Self {
2275 Self::new(t.0, t.1, t.2)
2276 }
2277}
2278
2279impl From<DVec3> for (f64, f64, f64) {
2280 #[inline]
2281 fn from(v: DVec3) -> Self {
2282 (v.x, v.y, v.z)
2283 }
2284}
2285
2286impl From<(DVec2, f64)> for DVec3 {
2287 #[inline]
2288 fn from((v, z): (DVec2, f64)) -> Self {
2289 Self::new(v.x, v.y, z)
2290 }
2291}
2292
2293impl From<Vec3> for DVec3 {
2294 #[inline]
2295 fn from(v: Vec3) -> Self {
2296 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2297 }
2298}
2299
2300#[cfg(feature = "i32")]
2301impl From<IVec3> for DVec3 {
2302 #[inline]
2303 fn from(v: IVec3) -> Self {
2304 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2305 }
2306}
2307
2308#[cfg(feature = "u32")]
2309impl From<UVec3> for DVec3 {
2310 #[inline]
2311 fn from(v: UVec3) -> Self {
2312 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2313 }
2314}
2315
2316impl From<BVec3> for DVec3 {
2317 #[inline]
2318 fn from(v: BVec3) -> Self {
2319 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2320 }
2321}
2322
2323impl From<BVec3A> for DVec3 {
2324 #[inline]
2325 fn from(v: BVec3A) -> Self {
2326 let bool_array: [bool; 3] = v.into();
2327 Self::new(
2328 f64::from(bool_array[0]),
2329 f64::from(bool_array[1]),
2330 f64::from(bool_array[2]),
2331 )
2332 }
2333}