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 #[doc(alias = "magnitude2")]
577 #[inline]
578 #[must_use]
579 pub fn length_squared(self) -> f64 {
580 self.dot(self)
581 }
582
583 #[inline]
587 #[must_use]
588 pub fn length_recip(self) -> f64 {
589 self.length().recip()
590 }
591
592 #[inline]
594 #[must_use]
595 pub fn distance(self, rhs: Self) -> f64 {
596 (self - rhs).length()
597 }
598
599 #[inline]
601 #[must_use]
602 pub fn distance_squared(self, rhs: Self) -> f64 {
603 (self - rhs).length_squared()
604 }
605
606 #[inline]
608 #[must_use]
609 pub fn div_euclid(self, rhs: Self) -> Self {
610 Self::new(
611 math::div_euclid(self.x, rhs.x),
612 math::div_euclid(self.y, rhs.y),
613 math::div_euclid(self.z, rhs.z),
614 )
615 }
616
617 #[inline]
621 #[must_use]
622 pub fn rem_euclid(self, rhs: Self) -> Self {
623 Self::new(
624 math::rem_euclid(self.x, rhs.x),
625 math::rem_euclid(self.y, rhs.y),
626 math::rem_euclid(self.z, rhs.z),
627 )
628 }
629
630 #[inline]
640 #[must_use]
641 pub fn normalize(self) -> Self {
642 #[allow(clippy::let_and_return)]
643 let normalized = self.mul(self.length_recip());
644 glam_assert!(normalized.is_finite());
645 normalized
646 }
647
648 #[inline]
655 #[must_use]
656 pub fn try_normalize(self) -> Option<Self> {
657 let rcp = self.length_recip();
658 if rcp.is_finite() && rcp > 0.0 {
659 Some(self * rcp)
660 } else {
661 None
662 }
663 }
664
665 #[inline]
673 #[must_use]
674 pub fn normalize_or(self, fallback: Self) -> Self {
675 let rcp = self.length_recip();
676 if rcp.is_finite() && rcp > 0.0 {
677 self * rcp
678 } else {
679 fallback
680 }
681 }
682
683 #[inline]
690 #[must_use]
691 pub fn normalize_or_zero(self) -> Self {
692 self.normalize_or(Self::ZERO)
693 }
694
695 #[inline]
699 #[must_use]
700 pub fn normalize_and_length(self) -> (Self, f64) {
701 let length = self.length();
702 let rcp = 1.0 / length;
703 if rcp.is_finite() && rcp > 0.0 {
704 (self * rcp, length)
705 } else {
706 (Self::X, 0.0)
707 }
708 }
709
710 #[inline]
714 #[must_use]
715 pub fn is_normalized(self) -> bool {
716 math::abs(self.length_squared() - 1.0) <= 2e-4
717 }
718
719 #[inline]
727 #[must_use]
728 pub fn project_onto(self, rhs: Self) -> Self {
729 let other_len_sq_rcp = rhs.dot(rhs).recip();
730 glam_assert!(other_len_sq_rcp.is_finite());
731 rhs * self.dot(rhs) * other_len_sq_rcp
732 }
733
734 #[doc(alias("plane"))]
745 #[inline]
746 #[must_use]
747 pub fn reject_from(self, rhs: Self) -> Self {
748 self - self.project_onto(rhs)
749 }
750
751 #[inline]
759 #[must_use]
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 pub fn reject_from_normalized(self, rhs: Self) -> Self {
779 self - self.project_onto_normalized(rhs)
780 }
781
782 #[inline]
785 #[must_use]
786 pub fn round(self) -> Self {
787 Self {
788 x: math::round(self.x),
789 y: math::round(self.y),
790 z: math::round(self.z),
791 }
792 }
793
794 #[inline]
797 #[must_use]
798 pub fn floor(self) -> Self {
799 Self {
800 x: math::floor(self.x),
801 y: math::floor(self.y),
802 z: math::floor(self.z),
803 }
804 }
805
806 #[inline]
809 #[must_use]
810 pub fn ceil(self) -> Self {
811 Self {
812 x: math::ceil(self.x),
813 y: math::ceil(self.y),
814 z: math::ceil(self.z),
815 }
816 }
817
818 #[inline]
821 #[must_use]
822 pub fn trunc(self) -> Self {
823 Self {
824 x: math::trunc(self.x),
825 y: math::trunc(self.y),
826 z: math::trunc(self.z),
827 }
828 }
829
830 #[inline]
834 #[must_use]
835 pub fn step(self, rhs: Self) -> Self {
836 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
837 }
838
839 #[inline]
841 #[must_use]
842 pub fn saturate(self) -> Self {
843 self.clamp(Self::ZERO, Self::ONE)
844 }
845
846 #[inline]
853 #[must_use]
854 pub fn fract(self) -> Self {
855 self - self.trunc()
856 }
857
858 #[inline]
865 #[must_use]
866 pub fn fract_gl(self) -> Self {
867 self - self.floor()
868 }
869
870 #[inline]
873 #[must_use]
874 pub fn exp(self) -> Self {
875 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
876 }
877
878 #[inline]
880 #[must_use]
881 pub fn exp2(self) -> Self {
882 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
883 }
884
885 #[inline]
888 #[must_use]
889 pub fn ln(self) -> Self {
890 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
891 }
892
893 #[inline]
896 #[must_use]
897 pub fn log2(self) -> Self {
898 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
899 }
900
901 #[inline]
903 #[must_use]
904 pub fn powf(self, n: f64) -> Self {
905 Self::new(
906 math::powf(self.x, n),
907 math::powf(self.y, n),
908 math::powf(self.z, n),
909 )
910 }
911
912 #[inline]
915 #[must_use]
916 pub fn sqrt(self) -> Self {
917 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
918 }
919
920 #[inline]
922 #[must_use]
923 pub fn cos(self) -> Self {
924 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
925 }
926
927 #[inline]
929 #[must_use]
930 pub fn sin(self) -> Self {
931 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
932 }
933
934 #[inline]
936 #[must_use]
937 pub fn sin_cos(self) -> (Self, Self) {
938 let (sin_x, cos_x) = math::sin_cos(self.x);
939 let (sin_y, cos_y) = math::sin_cos(self.y);
940 let (sin_z, cos_z) = math::sin_cos(self.z);
941
942 (
943 Self::new(sin_x, sin_y, sin_z),
944 Self::new(cos_x, cos_y, cos_z),
945 )
946 }
947
948 #[inline]
950 #[must_use]
951 pub fn recip(self) -> Self {
952 Self {
953 x: 1.0 / self.x,
954 y: 1.0 / self.y,
955 z: 1.0 / self.z,
956 }
957 }
958
959 #[doc(alias = "mix")]
965 #[inline]
966 #[must_use]
967 pub fn lerp(self, rhs: Self, s: f64) -> Self {
968 self * (1.0 - s) + rhs * s
969 }
970
971 #[inline]
976 #[must_use]
977 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
978 let a = rhs - self;
979 let len = a.length();
980 if len <= d || len <= 1e-4 {
981 return rhs;
982 }
983 self + a / len * d
984 }
985
986 #[inline]
992 pub fn midpoint(self, rhs: Self) -> Self {
993 (self + rhs) * 0.5
994 }
995
996 #[inline]
1006 #[must_use]
1007 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1008 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1009 }
1010
1011 #[inline]
1017 #[must_use]
1018 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1019 glam_assert!(0.0 <= min);
1020 glam_assert!(min <= max);
1021 let length_sq = self.length_squared();
1022 if length_sq < min * min {
1023 min * (self / math::sqrt(length_sq))
1024 } else if length_sq > max * max {
1025 max * (self / math::sqrt(length_sq))
1026 } else {
1027 self
1028 }
1029 }
1030
1031 #[inline]
1037 #[must_use]
1038 pub fn clamp_length_max(self, max: f64) -> Self {
1039 glam_assert!(0.0 <= max);
1040 let length_sq = self.length_squared();
1041 if length_sq > max * max {
1042 max * (self / math::sqrt(length_sq))
1043 } else {
1044 self
1045 }
1046 }
1047
1048 #[inline]
1054 #[must_use]
1055 pub fn clamp_length_min(self, min: f64) -> Self {
1056 glam_assert!(0.0 <= min);
1057 let length_sq = self.length_squared();
1058 if length_sq < min * min {
1059 min * (self / math::sqrt(length_sq))
1060 } else {
1061 self
1062 }
1063 }
1064
1065 #[inline]
1073 #[must_use]
1074 pub fn mul_add(self, a: Self, b: Self) -> Self {
1075 Self::new(
1076 math::mul_add(self.x, a.x, b.x),
1077 math::mul_add(self.y, a.y, b.y),
1078 math::mul_add(self.z, a.z, b.z),
1079 )
1080 }
1081
1082 #[inline]
1091 #[must_use]
1092 pub fn reflect(self, normal: Self) -> Self {
1093 glam_assert!(normal.is_normalized());
1094 self - 2.0 * self.dot(normal) * normal
1095 }
1096
1097 #[inline]
1107 #[must_use]
1108 pub fn refract(self, normal: Self, eta: f64) -> Self {
1109 glam_assert!(self.is_normalized());
1110 glam_assert!(normal.is_normalized());
1111 let n_dot_i = normal.dot(self);
1112 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1113 if k >= 0.0 {
1114 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1115 } else {
1116 Self::ZERO
1117 }
1118 }
1119
1120 #[inline]
1124 #[must_use]
1125 pub fn angle_between(self, rhs: Self) -> f64 {
1126 math::acos_approx(
1127 self.dot(rhs)
1128 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1129 )
1130 }
1131
1132 #[inline]
1134 #[must_use]
1135 pub fn rotate_x(self, angle: f64) -> Self {
1136 let (sina, cosa) = math::sin_cos(angle);
1137 Self::new(
1138 self.x,
1139 self.y * cosa - self.z * sina,
1140 self.y * sina + self.z * cosa,
1141 )
1142 }
1143
1144 #[inline]
1146 #[must_use]
1147 pub fn rotate_y(self, angle: f64) -> Self {
1148 let (sina, cosa) = math::sin_cos(angle);
1149 Self::new(
1150 self.x * cosa + self.z * sina,
1151 self.y,
1152 self.x * -sina + self.z * cosa,
1153 )
1154 }
1155
1156 #[inline]
1158 #[must_use]
1159 pub fn rotate_z(self, angle: f64) -> Self {
1160 let (sina, cosa) = math::sin_cos(angle);
1161 Self::new(
1162 self.x * cosa - self.y * sina,
1163 self.x * sina + self.y * cosa,
1164 self.z,
1165 )
1166 }
1167
1168 #[inline]
1176 #[must_use]
1177 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1178 DQuat::from_axis_angle(axis, angle) * self
1179 }
1180
1181 #[inline]
1187 #[must_use]
1188 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1189 let angle_between = self.angle_between(rhs);
1190 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1192 let axis = self
1193 .cross(rhs)
1194 .try_normalize()
1195 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1196 DQuat::from_axis_angle(axis, angle) * self
1197 }
1198
1199 #[inline]
1206 #[must_use]
1207 pub fn any_orthogonal_vector(self) -> Self {
1208 if math::abs(self.x) > math::abs(self.y) {
1210 Self::new(-self.z, 0.0, self.x) } else {
1212 Self::new(0.0, self.z, -self.y) }
1214 }
1215
1216 #[inline]
1224 #[must_use]
1225 pub fn any_orthonormal_vector(self) -> Self {
1226 glam_assert!(self.is_normalized());
1227 let sign = math::signum(self.z);
1229 let a = -1.0 / (sign + self.z);
1230 let b = self.x * self.y * a;
1231 Self::new(b, sign + self.y * self.y * a, -self.y)
1232 }
1233
1234 #[inline]
1241 #[must_use]
1242 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1243 glam_assert!(self.is_normalized());
1244 let sign = math::signum(self.z);
1246 let a = -1.0 / (sign + self.z);
1247 let b = self.x * self.y * a;
1248 (
1249 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1250 Self::new(b, sign + self.y * self.y * a, -self.y),
1251 )
1252 }
1253
1254 #[inline]
1260 #[must_use]
1261 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1262 let self_length = self.length();
1263 let rhs_length = rhs.length();
1264 let dot = self.dot(rhs) / (self_length * rhs_length);
1266 if math::abs(dot) < 1.0 - 3e-7 {
1268 let theta = math::acos_approx(dot);
1270 let sin_theta = math::sin(theta);
1272 let t1 = math::sin(theta * (1. - s));
1273 let t2 = math::sin(theta * s);
1274
1275 let result_length = self_length.lerp(rhs_length, s);
1277 return (self * (result_length / self_length) * t1
1279 + rhs * (result_length / rhs_length) * t2)
1280 * sin_theta.recip();
1281 }
1282 if dot < 0.0 {
1283 let axis = self.any_orthogonal_vector().normalize();
1287 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1288 let result_length = self_length.lerp(rhs_length, s);
1290 rotation * self * (result_length / self_length)
1291 } else {
1292 self.lerp(rhs, s)
1294 }
1295 }
1296
1297 #[inline]
1299 #[must_use]
1300 pub fn as_vec3(self) -> crate::Vec3 {
1301 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1302 }
1303
1304 #[inline]
1306 #[must_use]
1307 pub fn as_vec3a(self) -> crate::Vec3A {
1308 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1309 }
1310
1311 #[cfg(feature = "i8")]
1313 #[inline]
1314 #[must_use]
1315 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1316 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1317 }
1318
1319 #[cfg(feature = "u8")]
1321 #[inline]
1322 #[must_use]
1323 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1324 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1325 }
1326
1327 #[cfg(feature = "i16")]
1329 #[inline]
1330 #[must_use]
1331 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1332 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1333 }
1334
1335 #[cfg(feature = "u16")]
1337 #[inline]
1338 #[must_use]
1339 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1340 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1341 }
1342
1343 #[cfg(feature = "i32")]
1345 #[inline]
1346 #[must_use]
1347 pub fn as_ivec3(self) -> crate::IVec3 {
1348 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1349 }
1350
1351 #[cfg(feature = "u32")]
1353 #[inline]
1354 #[must_use]
1355 pub fn as_uvec3(self) -> crate::UVec3 {
1356 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1357 }
1358
1359 #[cfg(feature = "i64")]
1361 #[inline]
1362 #[must_use]
1363 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1364 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1365 }
1366
1367 #[cfg(feature = "u64")]
1369 #[inline]
1370 #[must_use]
1371 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1372 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1373 }
1374
1375 #[cfg(feature = "isize")]
1377 #[inline]
1378 #[must_use]
1379 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1380 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1381 }
1382
1383 #[cfg(feature = "usize")]
1385 #[inline]
1386 #[must_use]
1387 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1388 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1389 }
1390}
1391
1392impl Default for DVec3 {
1393 #[inline(always)]
1394 fn default() -> Self {
1395 Self::ZERO
1396 }
1397}
1398
1399impl Div for DVec3 {
1400 type Output = Self;
1401 #[inline]
1402 fn div(self, rhs: Self) -> Self {
1403 Self {
1404 x: self.x.div(rhs.x),
1405 y: self.y.div(rhs.y),
1406 z: self.z.div(rhs.z),
1407 }
1408 }
1409}
1410
1411impl Div<&Self> for DVec3 {
1412 type Output = Self;
1413 #[inline]
1414 fn div(self, rhs: &Self) -> Self {
1415 self.div(*rhs)
1416 }
1417}
1418
1419impl Div<&DVec3> for &DVec3 {
1420 type Output = DVec3;
1421 #[inline]
1422 fn div(self, rhs: &DVec3) -> DVec3 {
1423 (*self).div(*rhs)
1424 }
1425}
1426
1427impl Div<DVec3> for &DVec3 {
1428 type Output = DVec3;
1429 #[inline]
1430 fn div(self, rhs: DVec3) -> DVec3 {
1431 (*self).div(rhs)
1432 }
1433}
1434
1435impl DivAssign for DVec3 {
1436 #[inline]
1437 fn div_assign(&mut self, rhs: Self) {
1438 self.x.div_assign(rhs.x);
1439 self.y.div_assign(rhs.y);
1440 self.z.div_assign(rhs.z);
1441 }
1442}
1443
1444impl DivAssign<&Self> for DVec3 {
1445 #[inline]
1446 fn div_assign(&mut self, rhs: &Self) {
1447 self.div_assign(*rhs);
1448 }
1449}
1450
1451impl Div<f64> for DVec3 {
1452 type Output = Self;
1453 #[inline]
1454 fn div(self, rhs: f64) -> Self {
1455 Self {
1456 x: self.x.div(rhs),
1457 y: self.y.div(rhs),
1458 z: self.z.div(rhs),
1459 }
1460 }
1461}
1462
1463impl Div<&f64> for DVec3 {
1464 type Output = Self;
1465 #[inline]
1466 fn div(self, rhs: &f64) -> Self {
1467 self.div(*rhs)
1468 }
1469}
1470
1471impl Div<&f64> for &DVec3 {
1472 type Output = DVec3;
1473 #[inline]
1474 fn div(self, rhs: &f64) -> DVec3 {
1475 (*self).div(*rhs)
1476 }
1477}
1478
1479impl Div<f64> for &DVec3 {
1480 type Output = DVec3;
1481 #[inline]
1482 fn div(self, rhs: f64) -> DVec3 {
1483 (*self).div(rhs)
1484 }
1485}
1486
1487impl DivAssign<f64> for DVec3 {
1488 #[inline]
1489 fn div_assign(&mut self, rhs: f64) {
1490 self.x.div_assign(rhs);
1491 self.y.div_assign(rhs);
1492 self.z.div_assign(rhs);
1493 }
1494}
1495
1496impl DivAssign<&f64> for DVec3 {
1497 #[inline]
1498 fn div_assign(&mut self, rhs: &f64) {
1499 self.div_assign(*rhs);
1500 }
1501}
1502
1503impl Div<DVec3> for f64 {
1504 type Output = DVec3;
1505 #[inline]
1506 fn div(self, rhs: DVec3) -> DVec3 {
1507 DVec3 {
1508 x: self.div(rhs.x),
1509 y: self.div(rhs.y),
1510 z: self.div(rhs.z),
1511 }
1512 }
1513}
1514
1515impl Div<&DVec3> for f64 {
1516 type Output = DVec3;
1517 #[inline]
1518 fn div(self, rhs: &DVec3) -> DVec3 {
1519 self.div(*rhs)
1520 }
1521}
1522
1523impl Div<&DVec3> for &f64 {
1524 type Output = DVec3;
1525 #[inline]
1526 fn div(self, rhs: &DVec3) -> DVec3 {
1527 (*self).div(*rhs)
1528 }
1529}
1530
1531impl Div<DVec3> for &f64 {
1532 type Output = DVec3;
1533 #[inline]
1534 fn div(self, rhs: DVec3) -> DVec3 {
1535 (*self).div(rhs)
1536 }
1537}
1538
1539impl Mul for DVec3 {
1540 type Output = Self;
1541 #[inline]
1542 fn mul(self, rhs: Self) -> Self {
1543 Self {
1544 x: self.x.mul(rhs.x),
1545 y: self.y.mul(rhs.y),
1546 z: self.z.mul(rhs.z),
1547 }
1548 }
1549}
1550
1551impl Mul<&Self> for DVec3 {
1552 type Output = Self;
1553 #[inline]
1554 fn mul(self, rhs: &Self) -> Self {
1555 self.mul(*rhs)
1556 }
1557}
1558
1559impl Mul<&DVec3> for &DVec3 {
1560 type Output = DVec3;
1561 #[inline]
1562 fn mul(self, rhs: &DVec3) -> DVec3 {
1563 (*self).mul(*rhs)
1564 }
1565}
1566
1567impl Mul<DVec3> for &DVec3 {
1568 type Output = DVec3;
1569 #[inline]
1570 fn mul(self, rhs: DVec3) -> DVec3 {
1571 (*self).mul(rhs)
1572 }
1573}
1574
1575impl MulAssign for DVec3 {
1576 #[inline]
1577 fn mul_assign(&mut self, rhs: Self) {
1578 self.x.mul_assign(rhs.x);
1579 self.y.mul_assign(rhs.y);
1580 self.z.mul_assign(rhs.z);
1581 }
1582}
1583
1584impl MulAssign<&Self> for DVec3 {
1585 #[inline]
1586 fn mul_assign(&mut self, rhs: &Self) {
1587 self.mul_assign(*rhs);
1588 }
1589}
1590
1591impl Mul<f64> for DVec3 {
1592 type Output = Self;
1593 #[inline]
1594 fn mul(self, rhs: f64) -> Self {
1595 Self {
1596 x: self.x.mul(rhs),
1597 y: self.y.mul(rhs),
1598 z: self.z.mul(rhs),
1599 }
1600 }
1601}
1602
1603impl Mul<&f64> for DVec3 {
1604 type Output = Self;
1605 #[inline]
1606 fn mul(self, rhs: &f64) -> Self {
1607 self.mul(*rhs)
1608 }
1609}
1610
1611impl Mul<&f64> for &DVec3 {
1612 type Output = DVec3;
1613 #[inline]
1614 fn mul(self, rhs: &f64) -> DVec3 {
1615 (*self).mul(*rhs)
1616 }
1617}
1618
1619impl Mul<f64> for &DVec3 {
1620 type Output = DVec3;
1621 #[inline]
1622 fn mul(self, rhs: f64) -> DVec3 {
1623 (*self).mul(rhs)
1624 }
1625}
1626
1627impl MulAssign<f64> for DVec3 {
1628 #[inline]
1629 fn mul_assign(&mut self, rhs: f64) {
1630 self.x.mul_assign(rhs);
1631 self.y.mul_assign(rhs);
1632 self.z.mul_assign(rhs);
1633 }
1634}
1635
1636impl MulAssign<&f64> for DVec3 {
1637 #[inline]
1638 fn mul_assign(&mut self, rhs: &f64) {
1639 self.mul_assign(*rhs);
1640 }
1641}
1642
1643impl Mul<DVec3> for f64 {
1644 type Output = DVec3;
1645 #[inline]
1646 fn mul(self, rhs: DVec3) -> DVec3 {
1647 DVec3 {
1648 x: self.mul(rhs.x),
1649 y: self.mul(rhs.y),
1650 z: self.mul(rhs.z),
1651 }
1652 }
1653}
1654
1655impl Mul<&DVec3> for f64 {
1656 type Output = DVec3;
1657 #[inline]
1658 fn mul(self, rhs: &DVec3) -> DVec3 {
1659 self.mul(*rhs)
1660 }
1661}
1662
1663impl Mul<&DVec3> for &f64 {
1664 type Output = DVec3;
1665 #[inline]
1666 fn mul(self, rhs: &DVec3) -> DVec3 {
1667 (*self).mul(*rhs)
1668 }
1669}
1670
1671impl Mul<DVec3> for &f64 {
1672 type Output = DVec3;
1673 #[inline]
1674 fn mul(self, rhs: DVec3) -> DVec3 {
1675 (*self).mul(rhs)
1676 }
1677}
1678
1679impl Add for DVec3 {
1680 type Output = Self;
1681 #[inline]
1682 fn add(self, rhs: Self) -> Self {
1683 Self {
1684 x: self.x.add(rhs.x),
1685 y: self.y.add(rhs.y),
1686 z: self.z.add(rhs.z),
1687 }
1688 }
1689}
1690
1691impl Add<&Self> for DVec3 {
1692 type Output = Self;
1693 #[inline]
1694 fn add(self, rhs: &Self) -> Self {
1695 self.add(*rhs)
1696 }
1697}
1698
1699impl Add<&DVec3> for &DVec3 {
1700 type Output = DVec3;
1701 #[inline]
1702 fn add(self, rhs: &DVec3) -> DVec3 {
1703 (*self).add(*rhs)
1704 }
1705}
1706
1707impl Add<DVec3> for &DVec3 {
1708 type Output = DVec3;
1709 #[inline]
1710 fn add(self, rhs: DVec3) -> DVec3 {
1711 (*self).add(rhs)
1712 }
1713}
1714
1715impl AddAssign for DVec3 {
1716 #[inline]
1717 fn add_assign(&mut self, rhs: Self) {
1718 self.x.add_assign(rhs.x);
1719 self.y.add_assign(rhs.y);
1720 self.z.add_assign(rhs.z);
1721 }
1722}
1723
1724impl AddAssign<&Self> for DVec3 {
1725 #[inline]
1726 fn add_assign(&mut self, rhs: &Self) {
1727 self.add_assign(*rhs);
1728 }
1729}
1730
1731impl Add<f64> for DVec3 {
1732 type Output = Self;
1733 #[inline]
1734 fn add(self, rhs: f64) -> Self {
1735 Self {
1736 x: self.x.add(rhs),
1737 y: self.y.add(rhs),
1738 z: self.z.add(rhs),
1739 }
1740 }
1741}
1742
1743impl Add<&f64> for DVec3 {
1744 type Output = Self;
1745 #[inline]
1746 fn add(self, rhs: &f64) -> Self {
1747 self.add(*rhs)
1748 }
1749}
1750
1751impl Add<&f64> for &DVec3 {
1752 type Output = DVec3;
1753 #[inline]
1754 fn add(self, rhs: &f64) -> DVec3 {
1755 (*self).add(*rhs)
1756 }
1757}
1758
1759impl Add<f64> for &DVec3 {
1760 type Output = DVec3;
1761 #[inline]
1762 fn add(self, rhs: f64) -> DVec3 {
1763 (*self).add(rhs)
1764 }
1765}
1766
1767impl AddAssign<f64> for DVec3 {
1768 #[inline]
1769 fn add_assign(&mut self, rhs: f64) {
1770 self.x.add_assign(rhs);
1771 self.y.add_assign(rhs);
1772 self.z.add_assign(rhs);
1773 }
1774}
1775
1776impl AddAssign<&f64> for DVec3 {
1777 #[inline]
1778 fn add_assign(&mut self, rhs: &f64) {
1779 self.add_assign(*rhs);
1780 }
1781}
1782
1783impl Add<DVec3> for f64 {
1784 type Output = DVec3;
1785 #[inline]
1786 fn add(self, rhs: DVec3) -> DVec3 {
1787 DVec3 {
1788 x: self.add(rhs.x),
1789 y: self.add(rhs.y),
1790 z: self.add(rhs.z),
1791 }
1792 }
1793}
1794
1795impl Add<&DVec3> for f64 {
1796 type Output = DVec3;
1797 #[inline]
1798 fn add(self, rhs: &DVec3) -> DVec3 {
1799 self.add(*rhs)
1800 }
1801}
1802
1803impl Add<&DVec3> for &f64 {
1804 type Output = DVec3;
1805 #[inline]
1806 fn add(self, rhs: &DVec3) -> DVec3 {
1807 (*self).add(*rhs)
1808 }
1809}
1810
1811impl Add<DVec3> for &f64 {
1812 type Output = DVec3;
1813 #[inline]
1814 fn add(self, rhs: DVec3) -> DVec3 {
1815 (*self).add(rhs)
1816 }
1817}
1818
1819impl Sub for DVec3 {
1820 type Output = Self;
1821 #[inline]
1822 fn sub(self, rhs: Self) -> Self {
1823 Self {
1824 x: self.x.sub(rhs.x),
1825 y: self.y.sub(rhs.y),
1826 z: self.z.sub(rhs.z),
1827 }
1828 }
1829}
1830
1831impl Sub<&Self> for DVec3 {
1832 type Output = Self;
1833 #[inline]
1834 fn sub(self, rhs: &Self) -> Self {
1835 self.sub(*rhs)
1836 }
1837}
1838
1839impl Sub<&DVec3> for &DVec3 {
1840 type Output = DVec3;
1841 #[inline]
1842 fn sub(self, rhs: &DVec3) -> DVec3 {
1843 (*self).sub(*rhs)
1844 }
1845}
1846
1847impl Sub<DVec3> for &DVec3 {
1848 type Output = DVec3;
1849 #[inline]
1850 fn sub(self, rhs: DVec3) -> DVec3 {
1851 (*self).sub(rhs)
1852 }
1853}
1854
1855impl SubAssign for DVec3 {
1856 #[inline]
1857 fn sub_assign(&mut self, rhs: Self) {
1858 self.x.sub_assign(rhs.x);
1859 self.y.sub_assign(rhs.y);
1860 self.z.sub_assign(rhs.z);
1861 }
1862}
1863
1864impl SubAssign<&Self> for DVec3 {
1865 #[inline]
1866 fn sub_assign(&mut self, rhs: &Self) {
1867 self.sub_assign(*rhs);
1868 }
1869}
1870
1871impl Sub<f64> for DVec3 {
1872 type Output = Self;
1873 #[inline]
1874 fn sub(self, rhs: f64) -> Self {
1875 Self {
1876 x: self.x.sub(rhs),
1877 y: self.y.sub(rhs),
1878 z: self.z.sub(rhs),
1879 }
1880 }
1881}
1882
1883impl Sub<&f64> for DVec3 {
1884 type Output = Self;
1885 #[inline]
1886 fn sub(self, rhs: &f64) -> Self {
1887 self.sub(*rhs)
1888 }
1889}
1890
1891impl Sub<&f64> for &DVec3 {
1892 type Output = DVec3;
1893 #[inline]
1894 fn sub(self, rhs: &f64) -> DVec3 {
1895 (*self).sub(*rhs)
1896 }
1897}
1898
1899impl Sub<f64> for &DVec3 {
1900 type Output = DVec3;
1901 #[inline]
1902 fn sub(self, rhs: f64) -> DVec3 {
1903 (*self).sub(rhs)
1904 }
1905}
1906
1907impl SubAssign<f64> for DVec3 {
1908 #[inline]
1909 fn sub_assign(&mut self, rhs: f64) {
1910 self.x.sub_assign(rhs);
1911 self.y.sub_assign(rhs);
1912 self.z.sub_assign(rhs);
1913 }
1914}
1915
1916impl SubAssign<&f64> for DVec3 {
1917 #[inline]
1918 fn sub_assign(&mut self, rhs: &f64) {
1919 self.sub_assign(*rhs);
1920 }
1921}
1922
1923impl Sub<DVec3> for f64 {
1924 type Output = DVec3;
1925 #[inline]
1926 fn sub(self, rhs: DVec3) -> DVec3 {
1927 DVec3 {
1928 x: self.sub(rhs.x),
1929 y: self.sub(rhs.y),
1930 z: self.sub(rhs.z),
1931 }
1932 }
1933}
1934
1935impl Sub<&DVec3> for f64 {
1936 type Output = DVec3;
1937 #[inline]
1938 fn sub(self, rhs: &DVec3) -> DVec3 {
1939 self.sub(*rhs)
1940 }
1941}
1942
1943impl Sub<&DVec3> for &f64 {
1944 type Output = DVec3;
1945 #[inline]
1946 fn sub(self, rhs: &DVec3) -> DVec3 {
1947 (*self).sub(*rhs)
1948 }
1949}
1950
1951impl Sub<DVec3> for &f64 {
1952 type Output = DVec3;
1953 #[inline]
1954 fn sub(self, rhs: DVec3) -> DVec3 {
1955 (*self).sub(rhs)
1956 }
1957}
1958
1959impl Rem for DVec3 {
1960 type Output = Self;
1961 #[inline]
1962 fn rem(self, rhs: Self) -> Self {
1963 Self {
1964 x: self.x.rem(rhs.x),
1965 y: self.y.rem(rhs.y),
1966 z: self.z.rem(rhs.z),
1967 }
1968 }
1969}
1970
1971impl Rem<&Self> for DVec3 {
1972 type Output = Self;
1973 #[inline]
1974 fn rem(self, rhs: &Self) -> Self {
1975 self.rem(*rhs)
1976 }
1977}
1978
1979impl Rem<&DVec3> for &DVec3 {
1980 type Output = DVec3;
1981 #[inline]
1982 fn rem(self, rhs: &DVec3) -> DVec3 {
1983 (*self).rem(*rhs)
1984 }
1985}
1986
1987impl Rem<DVec3> for &DVec3 {
1988 type Output = DVec3;
1989 #[inline]
1990 fn rem(self, rhs: DVec3) -> DVec3 {
1991 (*self).rem(rhs)
1992 }
1993}
1994
1995impl RemAssign for DVec3 {
1996 #[inline]
1997 fn rem_assign(&mut self, rhs: Self) {
1998 self.x.rem_assign(rhs.x);
1999 self.y.rem_assign(rhs.y);
2000 self.z.rem_assign(rhs.z);
2001 }
2002}
2003
2004impl RemAssign<&Self> for DVec3 {
2005 #[inline]
2006 fn rem_assign(&mut self, rhs: &Self) {
2007 self.rem_assign(*rhs);
2008 }
2009}
2010
2011impl Rem<f64> for DVec3 {
2012 type Output = Self;
2013 #[inline]
2014 fn rem(self, rhs: f64) -> Self {
2015 Self {
2016 x: self.x.rem(rhs),
2017 y: self.y.rem(rhs),
2018 z: self.z.rem(rhs),
2019 }
2020 }
2021}
2022
2023impl Rem<&f64> for DVec3 {
2024 type Output = Self;
2025 #[inline]
2026 fn rem(self, rhs: &f64) -> Self {
2027 self.rem(*rhs)
2028 }
2029}
2030
2031impl Rem<&f64> for &DVec3 {
2032 type Output = DVec3;
2033 #[inline]
2034 fn rem(self, rhs: &f64) -> DVec3 {
2035 (*self).rem(*rhs)
2036 }
2037}
2038
2039impl Rem<f64> for &DVec3 {
2040 type Output = DVec3;
2041 #[inline]
2042 fn rem(self, rhs: f64) -> DVec3 {
2043 (*self).rem(rhs)
2044 }
2045}
2046
2047impl RemAssign<f64> for DVec3 {
2048 #[inline]
2049 fn rem_assign(&mut self, rhs: f64) {
2050 self.x.rem_assign(rhs);
2051 self.y.rem_assign(rhs);
2052 self.z.rem_assign(rhs);
2053 }
2054}
2055
2056impl RemAssign<&f64> for DVec3 {
2057 #[inline]
2058 fn rem_assign(&mut self, rhs: &f64) {
2059 self.rem_assign(*rhs);
2060 }
2061}
2062
2063impl Rem<DVec3> for f64 {
2064 type Output = DVec3;
2065 #[inline]
2066 fn rem(self, rhs: DVec3) -> DVec3 {
2067 DVec3 {
2068 x: self.rem(rhs.x),
2069 y: self.rem(rhs.y),
2070 z: self.rem(rhs.z),
2071 }
2072 }
2073}
2074
2075impl Rem<&DVec3> for f64 {
2076 type Output = DVec3;
2077 #[inline]
2078 fn rem(self, rhs: &DVec3) -> DVec3 {
2079 self.rem(*rhs)
2080 }
2081}
2082
2083impl Rem<&DVec3> for &f64 {
2084 type Output = DVec3;
2085 #[inline]
2086 fn rem(self, rhs: &DVec3) -> DVec3 {
2087 (*self).rem(*rhs)
2088 }
2089}
2090
2091impl Rem<DVec3> for &f64 {
2092 type Output = DVec3;
2093 #[inline]
2094 fn rem(self, rhs: DVec3) -> DVec3 {
2095 (*self).rem(rhs)
2096 }
2097}
2098
2099impl AsRef<[f64; 3]> for DVec3 {
2100 #[inline]
2101 fn as_ref(&self) -> &[f64; 3] {
2102 unsafe { &*(self as *const Self as *const [f64; 3]) }
2103 }
2104}
2105
2106impl AsMut<[f64; 3]> for DVec3 {
2107 #[inline]
2108 fn as_mut(&mut self) -> &mut [f64; 3] {
2109 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2110 }
2111}
2112
2113impl Sum for DVec3 {
2114 #[inline]
2115 fn sum<I>(iter: I) -> Self
2116 where
2117 I: Iterator<Item = Self>,
2118 {
2119 iter.fold(Self::ZERO, Self::add)
2120 }
2121}
2122
2123impl<'a> Sum<&'a Self> for DVec3 {
2124 #[inline]
2125 fn sum<I>(iter: I) -> Self
2126 where
2127 I: Iterator<Item = &'a Self>,
2128 {
2129 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2130 }
2131}
2132
2133impl Product for DVec3 {
2134 #[inline]
2135 fn product<I>(iter: I) -> Self
2136 where
2137 I: Iterator<Item = Self>,
2138 {
2139 iter.fold(Self::ONE, Self::mul)
2140 }
2141}
2142
2143impl<'a> Product<&'a Self> for DVec3 {
2144 #[inline]
2145 fn product<I>(iter: I) -> Self
2146 where
2147 I: Iterator<Item = &'a Self>,
2148 {
2149 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2150 }
2151}
2152
2153impl Neg for DVec3 {
2154 type Output = Self;
2155 #[inline]
2156 fn neg(self) -> Self {
2157 Self {
2158 x: self.x.neg(),
2159 y: self.y.neg(),
2160 z: self.z.neg(),
2161 }
2162 }
2163}
2164
2165impl Neg for &DVec3 {
2166 type Output = DVec3;
2167 #[inline]
2168 fn neg(self) -> DVec3 {
2169 (*self).neg()
2170 }
2171}
2172
2173impl Index<usize> for DVec3 {
2174 type Output = f64;
2175 #[inline]
2176 fn index(&self, index: usize) -> &Self::Output {
2177 match index {
2178 0 => &self.x,
2179 1 => &self.y,
2180 2 => &self.z,
2181 _ => panic!("index out of bounds"),
2182 }
2183 }
2184}
2185
2186impl IndexMut<usize> for DVec3 {
2187 #[inline]
2188 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2189 match index {
2190 0 => &mut self.x,
2191 1 => &mut self.y,
2192 2 => &mut self.z,
2193 _ => panic!("index out of bounds"),
2194 }
2195 }
2196}
2197
2198impl fmt::Display for DVec3 {
2199 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2200 if let Some(p) = f.precision() {
2201 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2202 } else {
2203 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2204 }
2205 }
2206}
2207
2208impl fmt::Debug for DVec3 {
2209 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2210 fmt.debug_tuple(stringify!(DVec3))
2211 .field(&self.x)
2212 .field(&self.y)
2213 .field(&self.z)
2214 .finish()
2215 }
2216}
2217
2218impl From<[f64; 3]> for DVec3 {
2219 #[inline]
2220 fn from(a: [f64; 3]) -> Self {
2221 Self::new(a[0], a[1], a[2])
2222 }
2223}
2224
2225impl From<DVec3> for [f64; 3] {
2226 #[inline]
2227 fn from(v: DVec3) -> Self {
2228 [v.x, v.y, v.z]
2229 }
2230}
2231
2232impl From<(f64, f64, f64)> for DVec3 {
2233 #[inline]
2234 fn from(t: (f64, f64, f64)) -> Self {
2235 Self::new(t.0, t.1, t.2)
2236 }
2237}
2238
2239impl From<DVec3> for (f64, f64, f64) {
2240 #[inline]
2241 fn from(v: DVec3) -> Self {
2242 (v.x, v.y, v.z)
2243 }
2244}
2245
2246impl From<(DVec2, f64)> for DVec3 {
2247 #[inline]
2248 fn from((v, z): (DVec2, f64)) -> Self {
2249 Self::new(v.x, v.y, z)
2250 }
2251}
2252
2253impl From<Vec3> for DVec3 {
2254 #[inline]
2255 fn from(v: Vec3) -> Self {
2256 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2257 }
2258}
2259
2260#[cfg(feature = "i32")]
2261impl From<IVec3> for DVec3 {
2262 #[inline]
2263 fn from(v: IVec3) -> Self {
2264 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2265 }
2266}
2267
2268#[cfg(feature = "u32")]
2269impl From<UVec3> for DVec3 {
2270 #[inline]
2271 fn from(v: UVec3) -> Self {
2272 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2273 }
2274}
2275
2276impl From<BVec3> for DVec3 {
2277 #[inline]
2278 fn from(v: BVec3) -> Self {
2279 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2280 }
2281}
2282
2283impl From<BVec3A> for DVec3 {
2284 #[inline]
2285 fn from(v: BVec3A) -> Self {
2286 let bool_array: [bool; 3] = v.into();
2287 Self::new(
2288 f64::from(bool_array[0]),
2289 f64::from(bool_array[1]),
2290 f64::from(bool_array[2]),
2291 )
2292 }
2293}