1use crate::{f64::math, BVec3, BVec3A, DQuat, DVec2, DVec4, FloatExt, IVec3, UVec3, Vec3};
4
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::{f32, ops::*};
8
9#[cfg(feature = "zerocopy")]
10use zerocopy_derive::*;
11
12#[inline(always)]
14#[must_use]
15pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3 {
16 DVec3::new(x, y, z)
17}
18
19#[derive(Clone, Copy, PartialEq)]
21#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
22#[cfg_attr(
23 feature = "zerocopy",
24 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
25)]
26#[repr(C)]
27#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
28pub struct DVec3 {
29 pub x: f64,
30 pub y: f64,
31 pub z: f64,
32}
33
34impl DVec3 {
35 pub const ZERO: Self = Self::splat(0.0);
37
38 pub const ONE: Self = Self::splat(1.0);
40
41 pub const NEG_ONE: Self = Self::splat(-1.0);
43
44 pub const MIN: Self = Self::splat(f64::MIN);
46
47 pub const MAX: Self = Self::splat(f64::MAX);
49
50 pub const NAN: Self = Self::splat(f64::NAN);
52
53 pub const INFINITY: Self = Self::splat(f64::INFINITY);
55
56 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
58
59 pub const X: Self = Self::new(1.0, 0.0, 0.0);
61
62 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
64
65 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
67
68 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
70
71 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
73
74 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
76
77 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
79
80 pub const USES_CORE_SIMD: bool = false;
82 pub const USES_NEON: bool = false;
84 pub const USES_SCALAR_MATH: bool = true;
86 pub const USES_SSE2: bool = false;
88 pub const USES_WASM_SIMD: bool = false;
90 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
91 pub const USES_WASM32_SIMD: bool = false;
92
93 #[inline(always)]
95 #[must_use]
96 pub const fn new(x: f64, y: f64, z: f64) -> Self {
97 Self { x, y, z }
98 }
99
100 #[inline]
102 #[must_use]
103 pub const fn splat(v: f64) -> Self {
104 Self { x: v, y: v, z: v }
105 }
106
107 #[inline]
109 #[must_use]
110 pub fn map<F>(self, f: F) -> Self
111 where
112 F: Fn(f64) -> f64,
113 {
114 Self::new(f(self.x), f(self.y), f(self.z))
115 }
116
117 #[inline]
123 #[must_use]
124 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
125 Self {
126 x: if mask.test(0) { if_true.x } else { if_false.x },
127 y: if mask.test(1) { if_true.y } else { if_false.y },
128 z: if mask.test(2) { if_true.z } else { if_false.z },
129 }
130 }
131
132 #[inline]
134 #[must_use]
135 pub const fn from_array(a: [f64; 3]) -> Self {
136 Self::new(a[0], a[1], a[2])
137 }
138
139 #[inline]
141 #[must_use]
142 pub const fn to_array(&self) -> [f64; 3] {
143 [self.x, self.y, self.z]
144 }
145
146 #[inline]
152 #[must_use]
153 pub const fn from_slice(slice: &[f64]) -> Self {
154 assert!(slice.len() >= 3);
155 Self::new(slice[0], slice[1], slice[2])
156 }
157
158 #[inline]
164 pub fn write_to_slice(self, slice: &mut [f64]) {
165 slice[..3].copy_from_slice(&self.to_array());
166 }
167
168 #[allow(dead_code)]
170 #[inline]
171 #[must_use]
172 pub(crate) fn from_vec4(v: DVec4) -> Self {
173 Self {
174 x: v.x,
175 y: v.y,
176 z: v.z,
177 }
178 }
179
180 #[inline]
182 #[must_use]
183 pub fn extend(self, w: f64) -> DVec4 {
184 DVec4::new(self.x, self.y, self.z, w)
185 }
186
187 #[inline]
191 #[must_use]
192 pub fn truncate(self) -> DVec2 {
193 use crate::swizzles::Vec3Swizzles;
194 self.xy()
195 }
196
197 #[inline]
203 #[must_use]
204 pub fn from_homogeneous(v: DVec4) -> Self {
205 glam_assert!(v.w != 0.0);
206 Self::from_vec4(v) / v.w
207 }
208
209 #[inline]
211 #[must_use]
212 pub fn to_homogeneous(self) -> DVec4 {
213 self.extend(1.0)
214 }
215
216 #[inline]
218 #[must_use]
219 pub fn with_x(mut self, x: f64) -> Self {
220 self.x = x;
221 self
222 }
223
224 #[inline]
226 #[must_use]
227 pub fn with_y(mut self, y: f64) -> Self {
228 self.y = y;
229 self
230 }
231
232 #[inline]
234 #[must_use]
235 pub fn with_z(mut self, z: f64) -> Self {
236 self.z = z;
237 self
238 }
239
240 #[inline]
242 #[must_use]
243 pub fn dot(self, rhs: Self) -> f64 {
244 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
245 }
246
247 #[inline]
249 #[must_use]
250 pub fn dot_into_vec(self, rhs: Self) -> Self {
251 Self::splat(self.dot(rhs))
252 }
253
254 #[inline]
256 #[must_use]
257 pub fn cross(self, rhs: Self) -> Self {
258 Self {
259 x: self.y * rhs.z - rhs.y * self.z,
260 y: self.z * rhs.x - rhs.z * self.x,
261 z: self.x * rhs.y - rhs.x * self.y,
262 }
263 }
264
265 #[inline]
272 #[must_use]
273 pub fn min(self, rhs: Self) -> Self {
274 Self {
275 x: if self.x < rhs.x { self.x } else { rhs.x },
276 y: if self.y < rhs.y { self.y } else { rhs.y },
277 z: if self.z < rhs.z { self.z } else { rhs.z },
278 }
279 }
280
281 #[inline]
288 #[must_use]
289 pub fn max(self, rhs: Self) -> Self {
290 Self {
291 x: if self.x > rhs.x { self.x } else { rhs.x },
292 y: if self.y > rhs.y { self.y } else { rhs.y },
293 z: if self.z > rhs.z { self.z } else { rhs.z },
294 }
295 }
296
297 #[inline]
308 #[must_use]
309 pub fn clamp(self, min: Self, max: Self) -> Self {
310 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
311 self.max(min).min(max)
312 }
313
314 #[inline]
321 #[must_use]
322 pub fn min_element(self) -> f64 {
323 let min = |a, b| if a < b { a } else { b };
324 min(self.x, min(self.y, self.z))
325 }
326
327 #[inline]
334 #[must_use]
335 pub fn max_element(self) -> f64 {
336 let max = |a, b| if a > b { a } else { b };
337 max(self.x, max(self.y, self.z))
338 }
339
340 #[doc(alias = "argmin")]
342 #[inline]
343 #[must_use]
344 pub fn min_position(self) -> usize {
345 let mut min = self.x;
346 let mut index = 0;
347 if self.y < min {
348 min = self.y;
349 index = 1;
350 }
351 if self.z < min {
352 index = 2;
353 }
354 index
355 }
356
357 #[doc(alias = "argmax")]
359 #[inline]
360 #[must_use]
361 pub fn max_position(self) -> usize {
362 let mut max = self.x;
363 let mut index = 0;
364 if self.y > max {
365 max = self.y;
366 index = 1;
367 }
368 if self.z > max {
369 index = 2;
370 }
371 index
372 }
373
374 #[inline]
378 #[must_use]
379 pub fn element_sum(self) -> f64 {
380 self.x + self.y + self.z
381 }
382
383 #[inline]
387 #[must_use]
388 pub fn element_product(self) -> f64 {
389 self.x * self.y * self.z
390 }
391
392 #[inline]
398 #[must_use]
399 pub fn cmpeq(self, rhs: Self) -> BVec3 {
400 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
401 }
402
403 #[inline]
409 #[must_use]
410 pub fn cmpne(self, rhs: Self) -> BVec3 {
411 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
412 }
413
414 #[inline]
420 #[must_use]
421 pub fn cmpge(self, rhs: Self) -> BVec3 {
422 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
423 }
424
425 #[inline]
431 #[must_use]
432 pub fn cmpgt(self, rhs: Self) -> BVec3 {
433 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
434 }
435
436 #[inline]
442 #[must_use]
443 pub fn cmple(self, rhs: Self) -> BVec3 {
444 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
445 }
446
447 #[inline]
453 #[must_use]
454 pub fn cmplt(self, rhs: Self) -> BVec3 {
455 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
456 }
457
458 #[inline]
460 #[must_use]
461 pub fn abs(self) -> Self {
462 Self {
463 x: math::abs(self.x),
464 y: math::abs(self.y),
465 z: math::abs(self.z),
466 }
467 }
468
469 #[inline]
475 #[must_use]
476 pub fn signum(self) -> Self {
477 Self {
478 x: math::signum(self.x),
479 y: math::signum(self.y),
480 z: math::signum(self.z),
481 }
482 }
483
484 #[inline]
486 #[must_use]
487 pub fn copysign(self, rhs: Self) -> Self {
488 Self {
489 x: math::copysign(self.x, rhs.x),
490 y: math::copysign(self.y, rhs.y),
491 z: math::copysign(self.z, rhs.z),
492 }
493 }
494
495 #[inline]
503 #[must_use]
504 pub fn is_negative_bitmask(self) -> u32 {
505 (self.x.is_sign_negative() as u32)
506 | ((self.y.is_sign_negative() as u32) << 1)
507 | ((self.z.is_sign_negative() as u32) << 2)
508 }
509
510 #[inline]
513 #[must_use]
514 pub fn is_finite(self) -> bool {
515 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
516 }
517
518 #[inline]
522 #[must_use]
523 pub fn is_finite_mask(self) -> BVec3 {
524 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
525 }
526
527 #[inline]
529 #[must_use]
530 pub fn is_nan(self) -> bool {
531 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
532 }
533
534 #[inline]
538 #[must_use]
539 pub fn is_nan_mask(self) -> BVec3 {
540 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
541 }
542
543 #[doc(alias = "magnitude")]
545 #[inline]
546 #[must_use]
547 pub fn length(self) -> f64 {
548 math::sqrt(self.dot(self))
549 }
550
551 #[doc(alias = "magnitude2")]
555 #[inline]
556 #[must_use]
557 pub fn length_squared(self) -> f64 {
558 self.dot(self)
559 }
560
561 #[inline]
565 #[must_use]
566 pub fn length_recip(self) -> f64 {
567 self.length().recip()
568 }
569
570 #[inline]
572 #[must_use]
573 pub fn distance(self, rhs: Self) -> f64 {
574 (self - rhs).length()
575 }
576
577 #[inline]
579 #[must_use]
580 pub fn distance_squared(self, rhs: Self) -> f64 {
581 (self - rhs).length_squared()
582 }
583
584 #[inline]
586 #[must_use]
587 pub fn div_euclid(self, rhs: Self) -> Self {
588 Self::new(
589 math::div_euclid(self.x, rhs.x),
590 math::div_euclid(self.y, rhs.y),
591 math::div_euclid(self.z, rhs.z),
592 )
593 }
594
595 #[inline]
599 #[must_use]
600 pub fn rem_euclid(self, rhs: Self) -> Self {
601 Self::new(
602 math::rem_euclid(self.x, rhs.x),
603 math::rem_euclid(self.y, rhs.y),
604 math::rem_euclid(self.z, rhs.z),
605 )
606 }
607
608 #[inline]
618 #[must_use]
619 pub fn normalize(self) -> Self {
620 #[allow(clippy::let_and_return)]
621 let normalized = self.mul(self.length_recip());
622 glam_assert!(normalized.is_finite());
623 normalized
624 }
625
626 #[inline]
633 #[must_use]
634 pub fn try_normalize(self) -> Option<Self> {
635 let rcp = self.length_recip();
636 if rcp.is_finite() && rcp > 0.0 {
637 Some(self * rcp)
638 } else {
639 None
640 }
641 }
642
643 #[inline]
651 #[must_use]
652 pub fn normalize_or(self, fallback: Self) -> Self {
653 let rcp = self.length_recip();
654 if rcp.is_finite() && rcp > 0.0 {
655 self * rcp
656 } else {
657 fallback
658 }
659 }
660
661 #[inline]
668 #[must_use]
669 pub fn normalize_or_zero(self) -> Self {
670 self.normalize_or(Self::ZERO)
671 }
672
673 #[inline]
677 #[must_use]
678 pub fn normalize_and_length(self) -> (Self, f64) {
679 let length = self.length();
680 let rcp = 1.0 / length;
681 if rcp.is_finite() && rcp > 0.0 {
682 (self * rcp, length)
683 } else {
684 (Self::X, 0.0)
685 }
686 }
687
688 #[inline]
692 #[must_use]
693 pub fn is_normalized(self) -> bool {
694 math::abs(self.length_squared() - 1.0) <= 2e-4
695 }
696
697 #[inline]
705 #[must_use]
706 pub fn project_onto(self, rhs: Self) -> Self {
707 let other_len_sq_rcp = rhs.dot(rhs).recip();
708 glam_assert!(other_len_sq_rcp.is_finite());
709 rhs * self.dot(rhs) * other_len_sq_rcp
710 }
711
712 #[doc(alias("plane"))]
723 #[inline]
724 #[must_use]
725 pub fn reject_from(self, rhs: Self) -> Self {
726 self - self.project_onto(rhs)
727 }
728
729 #[inline]
737 #[must_use]
738 pub fn project_onto_normalized(self, rhs: Self) -> Self {
739 glam_assert!(rhs.is_normalized());
740 rhs * self.dot(rhs)
741 }
742
743 #[doc(alias("plane"))]
754 #[inline]
755 #[must_use]
756 pub fn reject_from_normalized(self, rhs: Self) -> Self {
757 self - self.project_onto_normalized(rhs)
758 }
759
760 #[inline]
763 #[must_use]
764 pub fn round(self) -> Self {
765 Self {
766 x: math::round(self.x),
767 y: math::round(self.y),
768 z: math::round(self.z),
769 }
770 }
771
772 #[inline]
775 #[must_use]
776 pub fn floor(self) -> Self {
777 Self {
778 x: math::floor(self.x),
779 y: math::floor(self.y),
780 z: math::floor(self.z),
781 }
782 }
783
784 #[inline]
787 #[must_use]
788 pub fn ceil(self) -> Self {
789 Self {
790 x: math::ceil(self.x),
791 y: math::ceil(self.y),
792 z: math::ceil(self.z),
793 }
794 }
795
796 #[inline]
799 #[must_use]
800 pub fn trunc(self) -> Self {
801 Self {
802 x: math::trunc(self.x),
803 y: math::trunc(self.y),
804 z: math::trunc(self.z),
805 }
806 }
807
808 #[inline]
812 #[must_use]
813 pub fn step(self, rhs: Self) -> Self {
814 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
815 }
816
817 #[inline]
819 #[must_use]
820 pub fn saturate(self) -> Self {
821 self.clamp(Self::ZERO, Self::ONE)
822 }
823
824 #[inline]
831 #[must_use]
832 pub fn fract(self) -> Self {
833 self - self.trunc()
834 }
835
836 #[inline]
843 #[must_use]
844 pub fn fract_gl(self) -> Self {
845 self - self.floor()
846 }
847
848 #[inline]
851 #[must_use]
852 pub fn exp(self) -> Self {
853 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
854 }
855
856 #[inline]
858 #[must_use]
859 pub fn exp2(self) -> Self {
860 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
861 }
862
863 #[inline]
866 #[must_use]
867 pub fn ln(self) -> Self {
868 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
869 }
870
871 #[inline]
874 #[must_use]
875 pub fn log2(self) -> Self {
876 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
877 }
878
879 #[inline]
881 #[must_use]
882 pub fn powf(self, n: f64) -> Self {
883 Self::new(
884 math::powf(self.x, n),
885 math::powf(self.y, n),
886 math::powf(self.z, n),
887 )
888 }
889
890 #[inline]
893 #[must_use]
894 pub fn sqrt(self) -> Self {
895 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
896 }
897
898 #[inline]
900 #[must_use]
901 pub fn cos(self) -> Self {
902 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
903 }
904
905 #[inline]
907 #[must_use]
908 pub fn sin(self) -> Self {
909 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
910 }
911
912 #[inline]
914 #[must_use]
915 pub fn sin_cos(self) -> (Self, Self) {
916 let (sin_x, cos_x) = math::sin_cos(self.x);
917 let (sin_y, cos_y) = math::sin_cos(self.y);
918 let (sin_z, cos_z) = math::sin_cos(self.z);
919
920 (
921 Self::new(sin_x, sin_y, sin_z),
922 Self::new(cos_x, cos_y, cos_z),
923 )
924 }
925
926 #[inline]
928 #[must_use]
929 pub fn recip(self) -> Self {
930 Self {
931 x: 1.0 / self.x,
932 y: 1.0 / self.y,
933 z: 1.0 / self.z,
934 }
935 }
936
937 #[doc(alias = "mix")]
943 #[inline]
944 #[must_use]
945 pub fn lerp(self, rhs: Self, s: f64) -> Self {
946 self * (1.0 - s) + rhs * s
947 }
948
949 #[inline]
954 #[must_use]
955 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
956 let a = rhs - self;
957 let len = a.length();
958 if len <= d || len <= 1e-4 {
959 return rhs;
960 }
961 self + a / len * d
962 }
963
964 #[inline]
970 pub fn midpoint(self, rhs: Self) -> Self {
971 (self + rhs) * 0.5
972 }
973
974 #[inline]
984 #[must_use]
985 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
986 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
987 }
988
989 #[inline]
995 #[must_use]
996 pub fn clamp_length(self, min: f64, max: f64) -> Self {
997 glam_assert!(0.0 <= min);
998 glam_assert!(min <= max);
999 let length_sq = self.length_squared();
1000 if length_sq < min * min {
1001 min * (self / math::sqrt(length_sq))
1002 } else if length_sq > max * max {
1003 max * (self / math::sqrt(length_sq))
1004 } else {
1005 self
1006 }
1007 }
1008
1009 #[inline]
1015 #[must_use]
1016 pub fn clamp_length_max(self, max: f64) -> Self {
1017 glam_assert!(0.0 <= max);
1018 let length_sq = self.length_squared();
1019 if length_sq > max * max {
1020 max * (self / math::sqrt(length_sq))
1021 } else {
1022 self
1023 }
1024 }
1025
1026 #[inline]
1032 #[must_use]
1033 pub fn clamp_length_min(self, min: f64) -> Self {
1034 glam_assert!(0.0 <= min);
1035 let length_sq = self.length_squared();
1036 if length_sq < min * min {
1037 min * (self / math::sqrt(length_sq))
1038 } else {
1039 self
1040 }
1041 }
1042
1043 #[inline]
1051 #[must_use]
1052 pub fn mul_add(self, a: Self, b: Self) -> Self {
1053 Self::new(
1054 math::mul_add(self.x, a.x, b.x),
1055 math::mul_add(self.y, a.y, b.y),
1056 math::mul_add(self.z, a.z, b.z),
1057 )
1058 }
1059
1060 #[inline]
1069 #[must_use]
1070 pub fn reflect(self, normal: Self) -> Self {
1071 glam_assert!(normal.is_normalized());
1072 self - 2.0 * self.dot(normal) * normal
1073 }
1074
1075 #[inline]
1085 #[must_use]
1086 pub fn refract(self, normal: Self, eta: f64) -> Self {
1087 glam_assert!(self.is_normalized());
1088 glam_assert!(normal.is_normalized());
1089 let n_dot_i = normal.dot(self);
1090 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1091 if k >= 0.0 {
1092 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1093 } else {
1094 Self::ZERO
1095 }
1096 }
1097
1098 #[inline]
1102 #[must_use]
1103 pub fn angle_between(self, rhs: Self) -> f64 {
1104 math::acos_approx(
1105 self.dot(rhs)
1106 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1107 )
1108 }
1109
1110 #[inline]
1112 #[must_use]
1113 pub fn rotate_x(self, angle: f64) -> Self {
1114 let (sina, cosa) = math::sin_cos(angle);
1115 Self::new(
1116 self.x,
1117 self.y * cosa - self.z * sina,
1118 self.y * sina + self.z * cosa,
1119 )
1120 }
1121
1122 #[inline]
1124 #[must_use]
1125 pub fn rotate_y(self, angle: f64) -> Self {
1126 let (sina, cosa) = math::sin_cos(angle);
1127 Self::new(
1128 self.x * cosa + self.z * sina,
1129 self.y,
1130 self.x * -sina + self.z * cosa,
1131 )
1132 }
1133
1134 #[inline]
1136 #[must_use]
1137 pub fn rotate_z(self, angle: f64) -> Self {
1138 let (sina, cosa) = math::sin_cos(angle);
1139 Self::new(
1140 self.x * cosa - self.y * sina,
1141 self.x * sina + self.y * cosa,
1142 self.z,
1143 )
1144 }
1145
1146 #[inline]
1154 #[must_use]
1155 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1156 DQuat::from_axis_angle(axis, angle) * self
1157 }
1158
1159 #[inline]
1165 #[must_use]
1166 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1167 let angle_between = self.angle_between(rhs);
1168 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1170 let axis = self
1171 .cross(rhs)
1172 .try_normalize()
1173 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1174 DQuat::from_axis_angle(axis, angle) * self
1175 }
1176
1177 #[inline]
1184 #[must_use]
1185 pub fn any_orthogonal_vector(self) -> Self {
1186 if math::abs(self.x) > math::abs(self.y) {
1188 Self::new(-self.z, 0.0, self.x) } else {
1190 Self::new(0.0, self.z, -self.y) }
1192 }
1193
1194 #[inline]
1202 #[must_use]
1203 pub fn any_orthonormal_vector(self) -> Self {
1204 glam_assert!(self.is_normalized());
1205 let sign = math::signum(self.z);
1207 let a = -1.0 / (sign + self.z);
1208 let b = self.x * self.y * a;
1209 Self::new(b, sign + self.y * self.y * a, -self.y)
1210 }
1211
1212 #[inline]
1219 #[must_use]
1220 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1221 glam_assert!(self.is_normalized());
1222 let sign = math::signum(self.z);
1224 let a = -1.0 / (sign + self.z);
1225 let b = self.x * self.y * a;
1226 (
1227 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1228 Self::new(b, sign + self.y * self.y * a, -self.y),
1229 )
1230 }
1231
1232 #[inline]
1238 #[must_use]
1239 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1240 let self_length = self.length();
1241 let rhs_length = rhs.length();
1242 let dot = self.dot(rhs) / (self_length * rhs_length);
1244 if math::abs(dot) < 1.0 - 3e-7 {
1246 let theta = math::acos_approx(dot);
1248 let sin_theta = math::sin(theta);
1250 let t1 = math::sin(theta * (1. - s));
1251 let t2 = math::sin(theta * s);
1252
1253 let result_length = self_length.lerp(rhs_length, s);
1255 return (self * (result_length / self_length) * t1
1257 + rhs * (result_length / rhs_length) * t2)
1258 * sin_theta.recip();
1259 }
1260 if dot < 0.0 {
1261 let axis = self.any_orthogonal_vector().normalize();
1265 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1266 let result_length = self_length.lerp(rhs_length, s);
1268 rotation * self * (result_length / self_length)
1269 } else {
1270 self.lerp(rhs, s)
1272 }
1273 }
1274
1275 #[inline]
1277 #[must_use]
1278 pub fn as_vec3(self) -> crate::Vec3 {
1279 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1280 }
1281
1282 #[inline]
1284 #[must_use]
1285 pub fn as_vec3a(self) -> crate::Vec3A {
1286 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1287 }
1288
1289 #[inline]
1291 #[must_use]
1292 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1293 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1294 }
1295
1296 #[inline]
1298 #[must_use]
1299 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1300 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1301 }
1302
1303 #[inline]
1305 #[must_use]
1306 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1307 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1308 }
1309
1310 #[inline]
1312 #[must_use]
1313 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1314 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1315 }
1316
1317 #[inline]
1319 #[must_use]
1320 pub fn as_ivec3(self) -> crate::IVec3 {
1321 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1322 }
1323
1324 #[inline]
1326 #[must_use]
1327 pub fn as_uvec3(self) -> crate::UVec3 {
1328 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1329 }
1330
1331 #[inline]
1333 #[must_use]
1334 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1335 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1336 }
1337
1338 #[inline]
1340 #[must_use]
1341 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1342 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1343 }
1344
1345 #[inline]
1347 #[must_use]
1348 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1349 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1350 }
1351
1352 #[inline]
1354 #[must_use]
1355 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1356 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1357 }
1358}
1359
1360impl Default for DVec3 {
1361 #[inline(always)]
1362 fn default() -> Self {
1363 Self::ZERO
1364 }
1365}
1366
1367impl Div for DVec3 {
1368 type Output = Self;
1369 #[inline]
1370 fn div(self, rhs: Self) -> Self {
1371 Self {
1372 x: self.x.div(rhs.x),
1373 y: self.y.div(rhs.y),
1374 z: self.z.div(rhs.z),
1375 }
1376 }
1377}
1378
1379impl Div<&Self> for DVec3 {
1380 type Output = Self;
1381 #[inline]
1382 fn div(self, rhs: &Self) -> Self {
1383 self.div(*rhs)
1384 }
1385}
1386
1387impl Div<&DVec3> for &DVec3 {
1388 type Output = DVec3;
1389 #[inline]
1390 fn div(self, rhs: &DVec3) -> DVec3 {
1391 (*self).div(*rhs)
1392 }
1393}
1394
1395impl Div<DVec3> for &DVec3 {
1396 type Output = DVec3;
1397 #[inline]
1398 fn div(self, rhs: DVec3) -> DVec3 {
1399 (*self).div(rhs)
1400 }
1401}
1402
1403impl DivAssign for DVec3 {
1404 #[inline]
1405 fn div_assign(&mut self, rhs: Self) {
1406 self.x.div_assign(rhs.x);
1407 self.y.div_assign(rhs.y);
1408 self.z.div_assign(rhs.z);
1409 }
1410}
1411
1412impl DivAssign<&Self> for DVec3 {
1413 #[inline]
1414 fn div_assign(&mut self, rhs: &Self) {
1415 self.div_assign(*rhs);
1416 }
1417}
1418
1419impl Div<f64> for DVec3 {
1420 type Output = Self;
1421 #[inline]
1422 fn div(self, rhs: f64) -> Self {
1423 Self {
1424 x: self.x.div(rhs),
1425 y: self.y.div(rhs),
1426 z: self.z.div(rhs),
1427 }
1428 }
1429}
1430
1431impl Div<&f64> for DVec3 {
1432 type Output = Self;
1433 #[inline]
1434 fn div(self, rhs: &f64) -> Self {
1435 self.div(*rhs)
1436 }
1437}
1438
1439impl Div<&f64> for &DVec3 {
1440 type Output = DVec3;
1441 #[inline]
1442 fn div(self, rhs: &f64) -> DVec3 {
1443 (*self).div(*rhs)
1444 }
1445}
1446
1447impl Div<f64> for &DVec3 {
1448 type Output = DVec3;
1449 #[inline]
1450 fn div(self, rhs: f64) -> DVec3 {
1451 (*self).div(rhs)
1452 }
1453}
1454
1455impl DivAssign<f64> for DVec3 {
1456 #[inline]
1457 fn div_assign(&mut self, rhs: f64) {
1458 self.x.div_assign(rhs);
1459 self.y.div_assign(rhs);
1460 self.z.div_assign(rhs);
1461 }
1462}
1463
1464impl DivAssign<&f64> for DVec3 {
1465 #[inline]
1466 fn div_assign(&mut self, rhs: &f64) {
1467 self.div_assign(*rhs);
1468 }
1469}
1470
1471impl Div<DVec3> for f64 {
1472 type Output = DVec3;
1473 #[inline]
1474 fn div(self, rhs: DVec3) -> DVec3 {
1475 DVec3 {
1476 x: self.div(rhs.x),
1477 y: self.div(rhs.y),
1478 z: self.div(rhs.z),
1479 }
1480 }
1481}
1482
1483impl Div<&DVec3> for f64 {
1484 type Output = DVec3;
1485 #[inline]
1486 fn div(self, rhs: &DVec3) -> DVec3 {
1487 self.div(*rhs)
1488 }
1489}
1490
1491impl Div<&DVec3> for &f64 {
1492 type Output = DVec3;
1493 #[inline]
1494 fn div(self, rhs: &DVec3) -> DVec3 {
1495 (*self).div(*rhs)
1496 }
1497}
1498
1499impl Div<DVec3> for &f64 {
1500 type Output = DVec3;
1501 #[inline]
1502 fn div(self, rhs: DVec3) -> DVec3 {
1503 (*self).div(rhs)
1504 }
1505}
1506
1507impl Mul for DVec3 {
1508 type Output = Self;
1509 #[inline]
1510 fn mul(self, rhs: Self) -> Self {
1511 Self {
1512 x: self.x.mul(rhs.x),
1513 y: self.y.mul(rhs.y),
1514 z: self.z.mul(rhs.z),
1515 }
1516 }
1517}
1518
1519impl Mul<&Self> for DVec3 {
1520 type Output = Self;
1521 #[inline]
1522 fn mul(self, rhs: &Self) -> Self {
1523 self.mul(*rhs)
1524 }
1525}
1526
1527impl Mul<&DVec3> for &DVec3 {
1528 type Output = DVec3;
1529 #[inline]
1530 fn mul(self, rhs: &DVec3) -> DVec3 {
1531 (*self).mul(*rhs)
1532 }
1533}
1534
1535impl Mul<DVec3> for &DVec3 {
1536 type Output = DVec3;
1537 #[inline]
1538 fn mul(self, rhs: DVec3) -> DVec3 {
1539 (*self).mul(rhs)
1540 }
1541}
1542
1543impl MulAssign for DVec3 {
1544 #[inline]
1545 fn mul_assign(&mut self, rhs: Self) {
1546 self.x.mul_assign(rhs.x);
1547 self.y.mul_assign(rhs.y);
1548 self.z.mul_assign(rhs.z);
1549 }
1550}
1551
1552impl MulAssign<&Self> for DVec3 {
1553 #[inline]
1554 fn mul_assign(&mut self, rhs: &Self) {
1555 self.mul_assign(*rhs);
1556 }
1557}
1558
1559impl Mul<f64> for DVec3 {
1560 type Output = Self;
1561 #[inline]
1562 fn mul(self, rhs: f64) -> Self {
1563 Self {
1564 x: self.x.mul(rhs),
1565 y: self.y.mul(rhs),
1566 z: self.z.mul(rhs),
1567 }
1568 }
1569}
1570
1571impl Mul<&f64> for DVec3 {
1572 type Output = Self;
1573 #[inline]
1574 fn mul(self, rhs: &f64) -> Self {
1575 self.mul(*rhs)
1576 }
1577}
1578
1579impl Mul<&f64> for &DVec3 {
1580 type Output = DVec3;
1581 #[inline]
1582 fn mul(self, rhs: &f64) -> DVec3 {
1583 (*self).mul(*rhs)
1584 }
1585}
1586
1587impl Mul<f64> for &DVec3 {
1588 type Output = DVec3;
1589 #[inline]
1590 fn mul(self, rhs: f64) -> DVec3 {
1591 (*self).mul(rhs)
1592 }
1593}
1594
1595impl MulAssign<f64> for DVec3 {
1596 #[inline]
1597 fn mul_assign(&mut self, rhs: f64) {
1598 self.x.mul_assign(rhs);
1599 self.y.mul_assign(rhs);
1600 self.z.mul_assign(rhs);
1601 }
1602}
1603
1604impl MulAssign<&f64> for DVec3 {
1605 #[inline]
1606 fn mul_assign(&mut self, rhs: &f64) {
1607 self.mul_assign(*rhs);
1608 }
1609}
1610
1611impl Mul<DVec3> for f64 {
1612 type Output = DVec3;
1613 #[inline]
1614 fn mul(self, rhs: DVec3) -> DVec3 {
1615 DVec3 {
1616 x: self.mul(rhs.x),
1617 y: self.mul(rhs.y),
1618 z: self.mul(rhs.z),
1619 }
1620 }
1621}
1622
1623impl Mul<&DVec3> for f64 {
1624 type Output = DVec3;
1625 #[inline]
1626 fn mul(self, rhs: &DVec3) -> DVec3 {
1627 self.mul(*rhs)
1628 }
1629}
1630
1631impl Mul<&DVec3> for &f64 {
1632 type Output = DVec3;
1633 #[inline]
1634 fn mul(self, rhs: &DVec3) -> DVec3 {
1635 (*self).mul(*rhs)
1636 }
1637}
1638
1639impl Mul<DVec3> for &f64 {
1640 type Output = DVec3;
1641 #[inline]
1642 fn mul(self, rhs: DVec3) -> DVec3 {
1643 (*self).mul(rhs)
1644 }
1645}
1646
1647impl Add for DVec3 {
1648 type Output = Self;
1649 #[inline]
1650 fn add(self, rhs: Self) -> Self {
1651 Self {
1652 x: self.x.add(rhs.x),
1653 y: self.y.add(rhs.y),
1654 z: self.z.add(rhs.z),
1655 }
1656 }
1657}
1658
1659impl Add<&Self> for DVec3 {
1660 type Output = Self;
1661 #[inline]
1662 fn add(self, rhs: &Self) -> Self {
1663 self.add(*rhs)
1664 }
1665}
1666
1667impl Add<&DVec3> for &DVec3 {
1668 type Output = DVec3;
1669 #[inline]
1670 fn add(self, rhs: &DVec3) -> DVec3 {
1671 (*self).add(*rhs)
1672 }
1673}
1674
1675impl Add<DVec3> for &DVec3 {
1676 type Output = DVec3;
1677 #[inline]
1678 fn add(self, rhs: DVec3) -> DVec3 {
1679 (*self).add(rhs)
1680 }
1681}
1682
1683impl AddAssign for DVec3 {
1684 #[inline]
1685 fn add_assign(&mut self, rhs: Self) {
1686 self.x.add_assign(rhs.x);
1687 self.y.add_assign(rhs.y);
1688 self.z.add_assign(rhs.z);
1689 }
1690}
1691
1692impl AddAssign<&Self> for DVec3 {
1693 #[inline]
1694 fn add_assign(&mut self, rhs: &Self) {
1695 self.add_assign(*rhs);
1696 }
1697}
1698
1699impl Add<f64> for DVec3 {
1700 type Output = Self;
1701 #[inline]
1702 fn add(self, rhs: f64) -> Self {
1703 Self {
1704 x: self.x.add(rhs),
1705 y: self.y.add(rhs),
1706 z: self.z.add(rhs),
1707 }
1708 }
1709}
1710
1711impl Add<&f64> for DVec3 {
1712 type Output = Self;
1713 #[inline]
1714 fn add(self, rhs: &f64) -> Self {
1715 self.add(*rhs)
1716 }
1717}
1718
1719impl Add<&f64> for &DVec3 {
1720 type Output = DVec3;
1721 #[inline]
1722 fn add(self, rhs: &f64) -> DVec3 {
1723 (*self).add(*rhs)
1724 }
1725}
1726
1727impl Add<f64> for &DVec3 {
1728 type Output = DVec3;
1729 #[inline]
1730 fn add(self, rhs: f64) -> DVec3 {
1731 (*self).add(rhs)
1732 }
1733}
1734
1735impl AddAssign<f64> for DVec3 {
1736 #[inline]
1737 fn add_assign(&mut self, rhs: f64) {
1738 self.x.add_assign(rhs);
1739 self.y.add_assign(rhs);
1740 self.z.add_assign(rhs);
1741 }
1742}
1743
1744impl AddAssign<&f64> for DVec3 {
1745 #[inline]
1746 fn add_assign(&mut self, rhs: &f64) {
1747 self.add_assign(*rhs);
1748 }
1749}
1750
1751impl Add<DVec3> for f64 {
1752 type Output = DVec3;
1753 #[inline]
1754 fn add(self, rhs: DVec3) -> DVec3 {
1755 DVec3 {
1756 x: self.add(rhs.x),
1757 y: self.add(rhs.y),
1758 z: self.add(rhs.z),
1759 }
1760 }
1761}
1762
1763impl Add<&DVec3> for f64 {
1764 type Output = DVec3;
1765 #[inline]
1766 fn add(self, rhs: &DVec3) -> DVec3 {
1767 self.add(*rhs)
1768 }
1769}
1770
1771impl Add<&DVec3> for &f64 {
1772 type Output = DVec3;
1773 #[inline]
1774 fn add(self, rhs: &DVec3) -> DVec3 {
1775 (*self).add(*rhs)
1776 }
1777}
1778
1779impl Add<DVec3> for &f64 {
1780 type Output = DVec3;
1781 #[inline]
1782 fn add(self, rhs: DVec3) -> DVec3 {
1783 (*self).add(rhs)
1784 }
1785}
1786
1787impl Sub for DVec3 {
1788 type Output = Self;
1789 #[inline]
1790 fn sub(self, rhs: Self) -> Self {
1791 Self {
1792 x: self.x.sub(rhs.x),
1793 y: self.y.sub(rhs.y),
1794 z: self.z.sub(rhs.z),
1795 }
1796 }
1797}
1798
1799impl Sub<&Self> for DVec3 {
1800 type Output = Self;
1801 #[inline]
1802 fn sub(self, rhs: &Self) -> Self {
1803 self.sub(*rhs)
1804 }
1805}
1806
1807impl Sub<&DVec3> for &DVec3 {
1808 type Output = DVec3;
1809 #[inline]
1810 fn sub(self, rhs: &DVec3) -> DVec3 {
1811 (*self).sub(*rhs)
1812 }
1813}
1814
1815impl Sub<DVec3> for &DVec3 {
1816 type Output = DVec3;
1817 #[inline]
1818 fn sub(self, rhs: DVec3) -> DVec3 {
1819 (*self).sub(rhs)
1820 }
1821}
1822
1823impl SubAssign for DVec3 {
1824 #[inline]
1825 fn sub_assign(&mut self, rhs: Self) {
1826 self.x.sub_assign(rhs.x);
1827 self.y.sub_assign(rhs.y);
1828 self.z.sub_assign(rhs.z);
1829 }
1830}
1831
1832impl SubAssign<&Self> for DVec3 {
1833 #[inline]
1834 fn sub_assign(&mut self, rhs: &Self) {
1835 self.sub_assign(*rhs);
1836 }
1837}
1838
1839impl Sub<f64> for DVec3 {
1840 type Output = Self;
1841 #[inline]
1842 fn sub(self, rhs: f64) -> Self {
1843 Self {
1844 x: self.x.sub(rhs),
1845 y: self.y.sub(rhs),
1846 z: self.z.sub(rhs),
1847 }
1848 }
1849}
1850
1851impl Sub<&f64> for DVec3 {
1852 type Output = Self;
1853 #[inline]
1854 fn sub(self, rhs: &f64) -> Self {
1855 self.sub(*rhs)
1856 }
1857}
1858
1859impl Sub<&f64> for &DVec3 {
1860 type Output = DVec3;
1861 #[inline]
1862 fn sub(self, rhs: &f64) -> DVec3 {
1863 (*self).sub(*rhs)
1864 }
1865}
1866
1867impl Sub<f64> for &DVec3 {
1868 type Output = DVec3;
1869 #[inline]
1870 fn sub(self, rhs: f64) -> DVec3 {
1871 (*self).sub(rhs)
1872 }
1873}
1874
1875impl SubAssign<f64> for DVec3 {
1876 #[inline]
1877 fn sub_assign(&mut self, rhs: f64) {
1878 self.x.sub_assign(rhs);
1879 self.y.sub_assign(rhs);
1880 self.z.sub_assign(rhs);
1881 }
1882}
1883
1884impl SubAssign<&f64> for DVec3 {
1885 #[inline]
1886 fn sub_assign(&mut self, rhs: &f64) {
1887 self.sub_assign(*rhs);
1888 }
1889}
1890
1891impl Sub<DVec3> for f64 {
1892 type Output = DVec3;
1893 #[inline]
1894 fn sub(self, rhs: DVec3) -> DVec3 {
1895 DVec3 {
1896 x: self.sub(rhs.x),
1897 y: self.sub(rhs.y),
1898 z: self.sub(rhs.z),
1899 }
1900 }
1901}
1902
1903impl Sub<&DVec3> for f64 {
1904 type Output = DVec3;
1905 #[inline]
1906 fn sub(self, rhs: &DVec3) -> DVec3 {
1907 self.sub(*rhs)
1908 }
1909}
1910
1911impl Sub<&DVec3> for &f64 {
1912 type Output = DVec3;
1913 #[inline]
1914 fn sub(self, rhs: &DVec3) -> DVec3 {
1915 (*self).sub(*rhs)
1916 }
1917}
1918
1919impl Sub<DVec3> for &f64 {
1920 type Output = DVec3;
1921 #[inline]
1922 fn sub(self, rhs: DVec3) -> DVec3 {
1923 (*self).sub(rhs)
1924 }
1925}
1926
1927impl Rem for DVec3 {
1928 type Output = Self;
1929 #[inline]
1930 fn rem(self, rhs: Self) -> Self {
1931 Self {
1932 x: self.x.rem(rhs.x),
1933 y: self.y.rem(rhs.y),
1934 z: self.z.rem(rhs.z),
1935 }
1936 }
1937}
1938
1939impl Rem<&Self> for DVec3 {
1940 type Output = Self;
1941 #[inline]
1942 fn rem(self, rhs: &Self) -> Self {
1943 self.rem(*rhs)
1944 }
1945}
1946
1947impl Rem<&DVec3> for &DVec3 {
1948 type Output = DVec3;
1949 #[inline]
1950 fn rem(self, rhs: &DVec3) -> DVec3 {
1951 (*self).rem(*rhs)
1952 }
1953}
1954
1955impl Rem<DVec3> for &DVec3 {
1956 type Output = DVec3;
1957 #[inline]
1958 fn rem(self, rhs: DVec3) -> DVec3 {
1959 (*self).rem(rhs)
1960 }
1961}
1962
1963impl RemAssign for DVec3 {
1964 #[inline]
1965 fn rem_assign(&mut self, rhs: Self) {
1966 self.x.rem_assign(rhs.x);
1967 self.y.rem_assign(rhs.y);
1968 self.z.rem_assign(rhs.z);
1969 }
1970}
1971
1972impl RemAssign<&Self> for DVec3 {
1973 #[inline]
1974 fn rem_assign(&mut self, rhs: &Self) {
1975 self.rem_assign(*rhs);
1976 }
1977}
1978
1979impl Rem<f64> for DVec3 {
1980 type Output = Self;
1981 #[inline]
1982 fn rem(self, rhs: f64) -> Self {
1983 Self {
1984 x: self.x.rem(rhs),
1985 y: self.y.rem(rhs),
1986 z: self.z.rem(rhs),
1987 }
1988 }
1989}
1990
1991impl Rem<&f64> for DVec3 {
1992 type Output = Self;
1993 #[inline]
1994 fn rem(self, rhs: &f64) -> Self {
1995 self.rem(*rhs)
1996 }
1997}
1998
1999impl Rem<&f64> for &DVec3 {
2000 type Output = DVec3;
2001 #[inline]
2002 fn rem(self, rhs: &f64) -> DVec3 {
2003 (*self).rem(*rhs)
2004 }
2005}
2006
2007impl Rem<f64> for &DVec3 {
2008 type Output = DVec3;
2009 #[inline]
2010 fn rem(self, rhs: f64) -> DVec3 {
2011 (*self).rem(rhs)
2012 }
2013}
2014
2015impl RemAssign<f64> for DVec3 {
2016 #[inline]
2017 fn rem_assign(&mut self, rhs: f64) {
2018 self.x.rem_assign(rhs);
2019 self.y.rem_assign(rhs);
2020 self.z.rem_assign(rhs);
2021 }
2022}
2023
2024impl RemAssign<&f64> for DVec3 {
2025 #[inline]
2026 fn rem_assign(&mut self, rhs: &f64) {
2027 self.rem_assign(*rhs);
2028 }
2029}
2030
2031impl Rem<DVec3> for f64 {
2032 type Output = DVec3;
2033 #[inline]
2034 fn rem(self, rhs: DVec3) -> DVec3 {
2035 DVec3 {
2036 x: self.rem(rhs.x),
2037 y: self.rem(rhs.y),
2038 z: self.rem(rhs.z),
2039 }
2040 }
2041}
2042
2043impl Rem<&DVec3> for f64 {
2044 type Output = DVec3;
2045 #[inline]
2046 fn rem(self, rhs: &DVec3) -> DVec3 {
2047 self.rem(*rhs)
2048 }
2049}
2050
2051impl Rem<&DVec3> for &f64 {
2052 type Output = DVec3;
2053 #[inline]
2054 fn rem(self, rhs: &DVec3) -> DVec3 {
2055 (*self).rem(*rhs)
2056 }
2057}
2058
2059impl Rem<DVec3> for &f64 {
2060 type Output = DVec3;
2061 #[inline]
2062 fn rem(self, rhs: DVec3) -> DVec3 {
2063 (*self).rem(rhs)
2064 }
2065}
2066
2067impl AsRef<[f64; 3]> for DVec3 {
2068 #[inline]
2069 fn as_ref(&self) -> &[f64; 3] {
2070 unsafe { &*(self as *const Self as *const [f64; 3]) }
2071 }
2072}
2073
2074impl AsMut<[f64; 3]> for DVec3 {
2075 #[inline]
2076 fn as_mut(&mut self) -> &mut [f64; 3] {
2077 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2078 }
2079}
2080
2081impl Sum for DVec3 {
2082 #[inline]
2083 fn sum<I>(iter: I) -> Self
2084 where
2085 I: Iterator<Item = Self>,
2086 {
2087 iter.fold(Self::ZERO, Self::add)
2088 }
2089}
2090
2091impl<'a> Sum<&'a Self> for DVec3 {
2092 #[inline]
2093 fn sum<I>(iter: I) -> Self
2094 where
2095 I: Iterator<Item = &'a Self>,
2096 {
2097 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2098 }
2099}
2100
2101impl Product for DVec3 {
2102 #[inline]
2103 fn product<I>(iter: I) -> Self
2104 where
2105 I: Iterator<Item = Self>,
2106 {
2107 iter.fold(Self::ONE, Self::mul)
2108 }
2109}
2110
2111impl<'a> Product<&'a Self> for DVec3 {
2112 #[inline]
2113 fn product<I>(iter: I) -> Self
2114 where
2115 I: Iterator<Item = &'a Self>,
2116 {
2117 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2118 }
2119}
2120
2121impl Neg for DVec3 {
2122 type Output = Self;
2123 #[inline]
2124 fn neg(self) -> Self {
2125 Self {
2126 x: self.x.neg(),
2127 y: self.y.neg(),
2128 z: self.z.neg(),
2129 }
2130 }
2131}
2132
2133impl Neg for &DVec3 {
2134 type Output = DVec3;
2135 #[inline]
2136 fn neg(self) -> DVec3 {
2137 (*self).neg()
2138 }
2139}
2140
2141impl Index<usize> for DVec3 {
2142 type Output = f64;
2143 #[inline]
2144 fn index(&self, index: usize) -> &Self::Output {
2145 match index {
2146 0 => &self.x,
2147 1 => &self.y,
2148 2 => &self.z,
2149 _ => panic!("index out of bounds"),
2150 }
2151 }
2152}
2153
2154impl IndexMut<usize> for DVec3 {
2155 #[inline]
2156 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2157 match index {
2158 0 => &mut self.x,
2159 1 => &mut self.y,
2160 2 => &mut self.z,
2161 _ => panic!("index out of bounds"),
2162 }
2163 }
2164}
2165
2166impl fmt::Display for DVec3 {
2167 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2168 if let Some(p) = f.precision() {
2169 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2170 } else {
2171 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2172 }
2173 }
2174}
2175
2176impl fmt::Debug for DVec3 {
2177 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2178 fmt.debug_tuple(stringify!(DVec3))
2179 .field(&self.x)
2180 .field(&self.y)
2181 .field(&self.z)
2182 .finish()
2183 }
2184}
2185
2186impl From<[f64; 3]> for DVec3 {
2187 #[inline]
2188 fn from(a: [f64; 3]) -> Self {
2189 Self::new(a[0], a[1], a[2])
2190 }
2191}
2192
2193impl From<DVec3> for [f64; 3] {
2194 #[inline]
2195 fn from(v: DVec3) -> Self {
2196 [v.x, v.y, v.z]
2197 }
2198}
2199
2200impl From<(f64, f64, f64)> for DVec3 {
2201 #[inline]
2202 fn from(t: (f64, f64, f64)) -> Self {
2203 Self::new(t.0, t.1, t.2)
2204 }
2205}
2206
2207impl From<DVec3> for (f64, f64, f64) {
2208 #[inline]
2209 fn from(v: DVec3) -> Self {
2210 (v.x, v.y, v.z)
2211 }
2212}
2213
2214impl From<(DVec2, f64)> for DVec3 {
2215 #[inline]
2216 fn from((v, z): (DVec2, f64)) -> Self {
2217 Self::new(v.x, v.y, z)
2218 }
2219}
2220
2221impl From<Vec3> for DVec3 {
2222 #[inline]
2223 fn from(v: Vec3) -> Self {
2224 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2225 }
2226}
2227
2228impl From<IVec3> for DVec3 {
2229 #[inline]
2230 fn from(v: IVec3) -> Self {
2231 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2232 }
2233}
2234
2235impl From<UVec3> for DVec3 {
2236 #[inline]
2237 fn from(v: UVec3) -> Self {
2238 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2239 }
2240}
2241
2242impl From<BVec3> for DVec3 {
2243 #[inline]
2244 fn from(v: BVec3) -> Self {
2245 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2246 }
2247}
2248
2249impl From<BVec3A> for DVec3 {
2250 #[inline]
2251 fn from(v: BVec3A) -> Self {
2252 let bool_array: [bool; 3] = v.into();
2253 Self::new(
2254 f64::from(bool_array[0]),
2255 f64::from(bool_array[1]),
2256 f64::from(bool_array[2]),
2257 )
2258 }
2259}