1use crate::{f64::math, BVec3, BVec3A, DQuat, DVec2, DVec4};
4
5use crate::Vec3;
6
7#[cfg(feature = "i32")]
8use crate::IVec3;
9
10#[cfg(feature = "u32")]
11use crate::UVec3;
12
13use core::fmt;
14use core::iter::{Product, Sum};
15use core::ops::*;
16
17#[cfg(feature = "zerocopy-08")]
18use zerocopy_derive_08::*;
19
20#[inline(always)]
22#[must_use]
23pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3 {
24 DVec3::new(x, y, z)
25}
26
27#[derive(Clone, Copy, PartialEq)]
29#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
30#[cfg_attr(
31 feature = "zerocopy-08",
32 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
33)]
34#[repr(C)]
35#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
36pub struct DVec3 {
37 pub x: f64,
38 pub y: f64,
39 pub z: f64,
40}
41
42impl DVec3 {
43 pub const ZERO: Self = Self::splat(0.0);
45
46 pub const ONE: Self = Self::splat(1.0);
48
49 pub const NEG_ONE: Self = Self::splat(-1.0);
51
52 pub const MIN: Self = Self::splat(f64::MIN);
54
55 pub const MAX: Self = Self::splat(f64::MAX);
57
58 pub const NAN: Self = Self::splat(f64::NAN);
60
61 pub const INFINITY: Self = Self::splat(f64::INFINITY);
63
64 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
66
67 pub const X: Self = Self::new(1.0, 0.0, 0.0);
69
70 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
72
73 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
75
76 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
78
79 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
81
82 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
84
85 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
87
88 pub const USES_CORE_SIMD: bool = false;
90 pub const USES_NEON: bool = false;
92 pub const USES_SCALAR_MATH: bool = true;
94 pub const USES_SSE2: bool = false;
96 pub const USES_WASM_SIMD: bool = false;
98 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
99 pub const USES_WASM32_SIMD: bool = false;
100
101 #[inline(always)]
103 #[must_use]
104 pub const fn new(x: f64, y: f64, z: f64) -> Self {
105 Self { x, y, z }
106 }
107
108 #[inline]
110 #[must_use]
111 pub const fn splat(v: f64) -> Self {
112 Self::new(v, v, v)
113 }
114
115 #[inline]
117 #[must_use]
118 pub fn map<F>(self, mut f: F) -> Self
119 where
120 F: FnMut(f64) -> f64,
121 {
122 Self::new(f(self.x), f(self.y), f(self.z))
123 }
124
125 #[inline]
131 #[must_use]
132 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
133 Self::new(
134 if mask.test(0) { if_true.x } else { if_false.x },
135 if mask.test(1) { if_true.y } else { if_false.y },
136 if mask.test(2) { if_true.z } else { if_false.z },
137 )
138 }
139
140 #[inline]
142 #[must_use]
143 pub const fn from_array(a: [f64; 3]) -> Self {
144 Self::new(a[0], a[1], a[2])
145 }
146
147 #[inline]
149 #[must_use]
150 pub const fn to_array(&self) -> [f64; 3] {
151 [self.x, self.y, self.z]
152 }
153
154 #[inline]
160 #[must_use]
161 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::new(v.x, v.y, v.z)
182 }
183
184 #[inline]
186 #[must_use]
187 pub fn extend(self, w: f64) -> DVec4 {
188 DVec4::new(self.x, self.y, self.z, w)
189 }
190
191 #[inline]
195 #[must_use]
196 pub fn truncate(self) -> DVec2 {
197 use crate::swizzles::Vec3Swizzles;
198 self.xy()
199 }
200
201 #[inline]
207 #[must_use]
208 pub fn from_homogeneous(v: DVec4) -> Self {
209 glam_assert!(v.w != 0.0);
210 Self::from_vec4(v) / v.w
211 }
212
213 #[inline]
215 #[must_use]
216 pub fn to_homogeneous(self) -> DVec4 {
217 self.extend(1.0)
218 }
219
220 #[inline]
222 #[must_use]
223 pub fn with_x(mut self, x: f64) -> Self {
224 self.x = x;
225 self
226 }
227
228 #[inline]
230 #[must_use]
231 pub fn with_y(mut self, y: f64) -> Self {
232 self.y = y;
233 self
234 }
235
236 #[inline]
238 #[must_use]
239 pub fn with_z(mut self, z: f64) -> Self {
240 self.z = z;
241 self
242 }
243
244 #[inline]
246 #[must_use]
247 pub fn dot(self, rhs: Self) -> f64 {
248 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
249 }
250
251 #[inline]
253 #[must_use]
254 pub fn dot_into_vec(self, rhs: Self) -> Self {
255 Self::splat(self.dot(rhs))
256 }
257
258 #[inline]
260 #[must_use]
261 pub fn cross(self, rhs: Self) -> Self {
262 Self::new(
263 self.y * rhs.z - rhs.y * self.z,
264 self.z * rhs.x - rhs.z * self.x,
265 self.x * rhs.y - rhs.x * self.y,
266 )
267 }
268
269 #[inline]
276 #[must_use]
277 pub fn min(self, rhs: Self) -> Self {
278 Self::new(
279 if self.x < rhs.x { self.x } else { rhs.x },
280 if self.y < rhs.y { self.y } else { rhs.y },
281 if self.z < rhs.z { self.z } else { rhs.z },
282 )
283 }
284
285 #[inline]
292 #[must_use]
293 pub fn max(self, rhs: Self) -> Self {
294 Self::new(
295 if self.x > rhs.x { self.x } else { rhs.x },
296 if self.y > rhs.y { self.y } else { rhs.y },
297 if self.z > rhs.z { self.z } else { rhs.z },
298 )
299 }
300
301 #[inline]
312 #[must_use]
313 pub fn clamp(self, min: Self, max: Self) -> Self {
314 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
315 self.max(min).min(max)
316 }
317
318 #[inline]
325 #[must_use]
326 pub fn min_element(self) -> f64 {
327 let min = |a, b| if a < b { a } else { b };
328 min(self.x, min(self.y, self.z))
329 }
330
331 #[inline]
338 #[must_use]
339 pub fn max_element(self) -> f64 {
340 let max = |a, b| if a > b { a } else { b };
341 max(self.x, max(self.y, self.z))
342 }
343
344 #[doc(alias = "argmin")]
346 #[inline]
347 #[must_use]
348 pub fn min_position(self) -> usize {
349 let mut min = self.x;
350 let mut index = 0;
351 if self.y < min {
352 min = self.y;
353 index = 1;
354 }
355 if self.z < min {
356 index = 2;
357 }
358 index
359 }
360
361 #[doc(alias = "argmax")]
363 #[inline]
364 #[must_use]
365 pub fn max_position(self) -> usize {
366 let mut max = self.x;
367 let mut index = 0;
368 if self.y > max {
369 max = self.y;
370 index = 1;
371 }
372 if self.z > max {
373 index = 2;
374 }
375 index
376 }
377
378 #[inline]
382 #[must_use]
383 pub fn element_sum(self) -> f64 {
384 self.x + self.y + self.z
385 }
386
387 #[inline]
391 #[must_use]
392 pub fn element_product(self) -> f64 {
393 self.x * self.y * self.z
394 }
395
396 #[inline]
402 #[must_use]
403 pub fn cmpeq(self, rhs: Self) -> BVec3 {
404 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
405 }
406
407 #[inline]
413 #[must_use]
414 pub fn cmpne(self, rhs: Self) -> BVec3 {
415 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
416 }
417
418 #[inline]
424 #[must_use]
425 pub fn cmpge(self, rhs: Self) -> BVec3 {
426 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
427 }
428
429 #[inline]
435 #[must_use]
436 pub fn cmpgt(self, rhs: Self) -> BVec3 {
437 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
438 }
439
440 #[inline]
446 #[must_use]
447 pub fn cmple(self, rhs: Self) -> BVec3 {
448 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
449 }
450
451 #[inline]
457 #[must_use]
458 pub fn cmplt(self, rhs: Self) -> BVec3 {
459 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
460 }
461
462 #[inline]
464 #[must_use]
465 pub fn abs(self) -> Self {
466 Self::new(math::abs(self.x), math::abs(self.y), math::abs(self.z))
467 }
468
469 #[inline]
475 #[must_use]
476 pub fn signum(self) -> Self {
477 Self::new(
478 math::signum(self.x),
479 math::signum(self.y),
480 math::signum(self.z),
481 )
482 }
483
484 #[inline]
486 #[must_use]
487 pub fn copysign(self, rhs: Self) -> Self {
488 Self::new(
489 math::copysign(self.x, rhs.x),
490 math::copysign(self.y, rhs.y),
491 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]
515 #[must_use]
516 pub fn is_negative_mask(self) -> BVec3 {
517 BVec3::new(
518 self.x.is_sign_negative(),
519 self.y.is_sign_negative(),
520 self.z.is_sign_negative(),
521 )
522 }
523
524 #[inline]
527 #[must_use]
528 pub fn is_finite(self) -> bool {
529 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
530 }
531
532 #[inline]
536 #[must_use]
537 pub fn is_finite_mask(self) -> BVec3 {
538 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
539 }
540
541 #[inline]
543 #[must_use]
544 pub fn is_nan(self) -> bool {
545 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
546 }
547
548 #[inline]
552 #[must_use]
553 pub fn is_nan_mask(self) -> BVec3 {
554 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
555 }
556
557 #[doc(alias = "magnitude")]
559 #[inline]
560 #[must_use]
561 pub fn length(self) -> f64 {
562 math::sqrt(self.dot(self))
563 }
564
565 #[allow(dead_code)]
567 fn is_non_zero(self) -> bool {
568 self.length_squared() > 0.0
569 }
570
571 #[doc(alias = "magnitude2")]
575 #[inline]
576 #[must_use]
577 pub fn length_squared(self) -> f64 {
578 self.dot(self)
579 }
580
581 #[inline]
585 #[must_use]
586 pub fn length_recip(self) -> f64 {
587 1.0 / self.length()
588 }
589
590 #[inline]
592 #[must_use]
593 pub fn distance(self, rhs: Self) -> f64 {
594 (self - rhs).length()
595 }
596
597 #[inline]
599 #[must_use]
600 pub fn distance_squared(self, rhs: Self) -> f64 {
601 (self - rhs).length_squared()
602 }
603
604 #[inline]
606 #[must_use]
607 pub fn div_euclid(self, rhs: Self) -> Self {
608 Self::new(
609 math::div_euclid(self.x, rhs.x),
610 math::div_euclid(self.y, rhs.y),
611 math::div_euclid(self.z, rhs.z),
612 )
613 }
614
615 #[inline]
619 #[must_use]
620 pub fn rem_euclid(self, rhs: Self) -> Self {
621 Self::new(
622 math::rem_euclid(self.x, rhs.x),
623 math::rem_euclid(self.y, rhs.y),
624 math::rem_euclid(self.z, rhs.z),
625 )
626 }
627
628 #[inline]
638 #[must_use]
639 pub fn normalize(self) -> Self {
640 #[allow(clippy::let_and_return)]
641 let normalized = self.mul(self.length_recip());
642 glam_assert!(normalized.is_finite());
643 normalized
644 }
645
646 #[inline]
653 #[must_use]
654 pub fn try_normalize(self) -> Option<Self> {
655 let rcp = self.length_recip();
656 if rcp.is_finite() && rcp > 0.0 {
657 Some(self * rcp)
658 } else {
659 None
660 }
661 }
662
663 #[inline]
671 #[must_use]
672 pub fn normalize_or(self, fallback: Self) -> Self {
673 let rcp = self.length_recip();
674 if rcp.is_finite() && rcp > 0.0 {
675 self * rcp
676 } else {
677 fallback
678 }
679 }
680
681 #[inline]
688 #[must_use]
689 pub fn normalize_or_zero(self) -> Self {
690 self.normalize_or(Self::ZERO)
691 }
692
693 #[inline]
697 #[must_use]
698 pub fn normalize_and_length(self) -> (Self, f64) {
699 let length = self.length();
700 let rcp = 1.0 / length;
701 if rcp.is_finite() && rcp > 0.0 {
702 (self * rcp, length)
703 } else {
704 (Self::X, 0.0)
705 }
706 }
707
708 #[inline]
712 #[must_use]
713 pub fn is_normalized(self) -> bool {
714 math::abs(self.length_squared() - 1.0) <= 2e-4
715 }
716
717 #[inline]
725 #[must_use]
726 pub fn project_onto(self, rhs: Self) -> Self {
727 let other_len_sq_rcp = 1.0 / rhs.dot(rhs);
728 glam_assert!(other_len_sq_rcp.is_finite());
729 rhs * self.dot(rhs) * other_len_sq_rcp
730 }
731
732 #[doc(alias("plane"))]
743 #[inline]
744 #[must_use]
745 pub fn reject_from(self, rhs: Self) -> Self {
746 self - self.project_onto(rhs)
747 }
748
749 #[inline]
757 #[must_use]
758 pub fn project_onto_normalized(self, rhs: Self) -> Self {
759 glam_assert!(rhs.is_normalized());
760 rhs * self.dot(rhs)
761 }
762
763 #[doc(alias("plane"))]
774 #[inline]
775 #[must_use]
776 pub fn reject_from_normalized(self, rhs: Self) -> Self {
777 self - self.project_onto_normalized(rhs)
778 }
779
780 #[inline]
783 #[must_use]
784 pub fn round(self) -> Self {
785 Self::new(
786 math::round(self.x),
787 math::round(self.y),
788 math::round(self.z),
789 )
790 }
791
792 #[inline]
795 #[must_use]
796 pub fn floor(self) -> Self {
797 Self::new(
798 math::floor(self.x),
799 math::floor(self.y),
800 math::floor(self.z),
801 )
802 }
803
804 #[inline]
807 #[must_use]
808 pub fn ceil(self) -> Self {
809 Self::new(math::ceil(self.x), math::ceil(self.y), math::ceil(self.z))
810 }
811
812 #[inline]
815 #[must_use]
816 pub fn trunc(self) -> Self {
817 Self::new(
818 math::trunc(self.x),
819 math::trunc(self.y),
820 math::trunc(self.z),
821 )
822 }
823
824 #[inline]
828 #[must_use]
829 pub fn step(self, rhs: Self) -> Self {
830 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
831 }
832
833 #[inline]
844 #[must_use]
845 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
846 glam_assert!(edge0.cmplt(edge1).all());
847 let t = ((self - edge0) / (edge1 - edge0)).saturate();
848 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
849 }
850
851 #[inline]
853 #[must_use]
854 pub fn saturate(self) -> Self {
855 self.clamp(Self::ZERO, Self::ONE)
856 }
857
858 #[inline]
865 #[must_use]
866 pub fn fract(self) -> Self {
867 self - self.trunc()
868 }
869
870 #[inline]
877 #[must_use]
878 pub fn fract_gl(self) -> Self {
879 self - self.floor()
880 }
881
882 #[inline]
885 #[must_use]
886 pub fn exp(self) -> Self {
887 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
888 }
889
890 #[inline]
892 #[must_use]
893 pub fn exp2(self) -> Self {
894 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
895 }
896
897 #[inline]
900 #[must_use]
901 pub fn ln(self) -> Self {
902 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
903 }
904
905 #[inline]
908 #[must_use]
909 pub fn log2(self) -> Self {
910 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
911 }
912
913 #[inline]
915 #[must_use]
916 pub fn powf(self, n: f64) -> Self {
917 Self::new(
918 math::powf(self.x, n),
919 math::powf(self.y, n),
920 math::powf(self.z, n),
921 )
922 }
923
924 #[inline]
927 #[must_use]
928 pub fn sqrt(self) -> Self {
929 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
930 }
931
932 #[inline]
934 #[must_use]
935 pub fn cos(self) -> Self {
936 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
937 }
938
939 #[inline]
941 #[must_use]
942 pub fn sin(self) -> Self {
943 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
944 }
945
946 #[inline]
948 #[must_use]
949 pub fn sin_cos(self) -> (Self, Self) {
950 let (sin_x, cos_x) = math::sin_cos(self.x);
951 let (sin_y, cos_y) = math::sin_cos(self.y);
952 let (sin_z, cos_z) = math::sin_cos(self.z);
953
954 (
955 Self::new(sin_x, sin_y, sin_z),
956 Self::new(cos_x, cos_y, cos_z),
957 )
958 }
959
960 #[inline]
962 #[must_use]
963 pub fn recip(self) -> Self {
964 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
965 }
966
967 #[doc(alias = "mix")]
979 #[inline]
980 #[must_use]
981 pub fn lerp(self, rhs: Self, s: f64) -> Self {
982 self * (1.0 - s) + rhs * s
983 }
984
985 #[doc(alias = "mix")]
1002 #[inline]
1003 #[must_use]
1004 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
1005 self + (rhs - self) * s
1006 }
1007
1008 #[inline]
1013 #[must_use]
1014 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1015 let a = rhs - self;
1016 let len = a.length();
1017 if len <= d || len <= 1e-4 {
1018 return rhs;
1019 }
1020 self + a / len * d
1021 }
1022
1023 #[inline]
1029 pub fn midpoint(self, rhs: Self) -> Self {
1030 (self + rhs) * 0.5
1031 }
1032
1033 #[inline]
1043 #[must_use]
1044 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1045 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1046 }
1047
1048 #[inline]
1054 #[must_use]
1055 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1056 glam_assert!(0.0 <= min);
1057 glam_assert!(min <= max);
1058 let length_sq = self.length_squared();
1059 if length_sq < min * min {
1060 min * (self / math::sqrt(length_sq))
1061 } else if length_sq > max * max {
1062 max * (self / math::sqrt(length_sq))
1063 } else {
1064 self
1065 }
1066 }
1067
1068 #[inline]
1074 #[must_use]
1075 pub fn clamp_length_max(self, max: f64) -> Self {
1076 glam_assert!(0.0 <= max);
1077 let length_sq = self.length_squared();
1078 if length_sq > max * max {
1079 max * (self / math::sqrt(length_sq))
1080 } else {
1081 self
1082 }
1083 }
1084
1085 #[inline]
1091 #[must_use]
1092 pub fn clamp_length_min(self, min: f64) -> Self {
1093 glam_assert!(0.0 <= min);
1094 let length_sq = self.length_squared();
1095 if length_sq < min * min {
1096 min * (self / math::sqrt(length_sq))
1097 } else {
1098 self
1099 }
1100 }
1101
1102 #[inline]
1110 #[must_use]
1111 pub fn mul_add(self, a: Self, b: Self) -> Self {
1112 Self::new(
1113 math::mul_add(self.x, a.x, b.x),
1114 math::mul_add(self.y, a.y, b.y),
1115 math::mul_add(self.z, a.z, b.z),
1116 )
1117 }
1118
1119 #[inline]
1128 #[must_use]
1129 pub fn reflect(self, normal: Self) -> Self {
1130 glam_assert!(normal.is_normalized());
1131 self - 2.0 * self.dot(normal) * normal
1132 }
1133
1134 #[inline]
1144 #[must_use]
1145 pub fn refract(self, normal: Self, eta: f64) -> Self {
1146 glam_assert!(self.is_normalized());
1147 glam_assert!(normal.is_normalized());
1148 let n_dot_i = normal.dot(self);
1149 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1150 if k >= 0.0 {
1151 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1152 } else {
1153 Self::ZERO
1154 }
1155 }
1156
1157 #[inline]
1168 #[must_use]
1169 pub fn angle_between(self, rhs: Self) -> f64 {
1170 glam_assert!(self.is_non_zero());
1171 glam_assert!(rhs.is_non_zero());
1172 math::acos_approx(
1173 self.dot(rhs)
1174 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1175 )
1176 }
1177
1178 #[doc(alias = "signed_angle")]
1194 #[inline]
1195 #[must_use]
1196 pub fn angle_to(self, rhs: Self, axis: Self) -> f64 {
1197 glam_assert!(axis.is_normalized());
1198 glam_assert!(self.is_non_zero());
1199 glam_assert!(rhs.is_non_zero());
1200 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1201 }
1202
1203 #[inline]
1205 #[must_use]
1206 pub fn rotate_x(self, angle: f64) -> Self {
1207 let (sina, cosa) = math::sin_cos(angle);
1208 Self::new(
1209 self.x,
1210 self.y * cosa - self.z * sina,
1211 self.y * sina + self.z * cosa,
1212 )
1213 }
1214
1215 #[inline]
1217 #[must_use]
1218 pub fn rotate_y(self, angle: f64) -> Self {
1219 let (sina, cosa) = math::sin_cos(angle);
1220 Self::new(
1221 self.x * cosa + self.z * sina,
1222 self.y,
1223 self.x * -sina + self.z * cosa,
1224 )
1225 }
1226
1227 #[inline]
1229 #[must_use]
1230 pub fn rotate_z(self, angle: f64) -> Self {
1231 let (sina, cosa) = math::sin_cos(angle);
1232 Self::new(
1233 self.x * cosa - self.y * sina,
1234 self.x * sina + self.y * cosa,
1235 self.z,
1236 )
1237 }
1238
1239 #[inline]
1247 #[must_use]
1248 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1249 DQuat::from_axis_angle(axis, angle) * self
1250 }
1251
1252 #[inline]
1258 #[must_use]
1259 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1260 let angle_between = self.angle_between(rhs);
1261 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1263 let axis = self
1264 .cross(rhs)
1265 .try_normalize()
1266 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1267 DQuat::from_axis_angle(axis, angle) * self
1268 }
1269
1270 #[inline]
1277 #[must_use]
1278 pub fn any_orthogonal_vector(self) -> Self {
1279 if math::abs(self.x) > math::abs(self.y) {
1281 Self::new(-self.z, 0.0, self.x) } else {
1283 Self::new(0.0, self.z, -self.y) }
1285 }
1286
1287 #[inline]
1295 #[must_use]
1296 pub fn any_orthonormal_vector(self) -> Self {
1297 glam_assert!(self.is_normalized());
1298 let sign = math::signum(self.z);
1300 let a = -1.0 / (sign + self.z);
1301 let b = self.x * self.y * a;
1302 Self::new(b, sign + self.y * self.y * a, -self.y)
1303 }
1304
1305 #[inline]
1312 #[must_use]
1313 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1314 glam_assert!(self.is_normalized());
1315 let sign = math::signum(self.z);
1317 let a = -1.0 / (sign + self.z);
1318 let b = self.x * self.y * a;
1319 (
1320 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1321 Self::new(b, sign + self.y * self.y * a, -self.y),
1322 )
1323 }
1324
1325 #[inline]
1331 #[must_use]
1332 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1333 let self_length = self.length();
1334 let rhs_length = rhs.length();
1335 let dot = self.dot(rhs) / (self_length * rhs_length);
1337 if math::abs(dot) < 1.0 - 3e-7 {
1339 let theta = math::acos_approx(dot);
1341 let sin_theta = math::sin(theta);
1343 let t1 = math::sin(theta * (1.0 - s));
1344 let t2 = math::sin(theta * s);
1345
1346 let result_length = self_length + (rhs_length - self_length) * s;
1348 return (self * (result_length / self_length) * t1
1350 + rhs * (result_length / rhs_length) * t2)
1351 * (1.0 / sin_theta);
1352 }
1353 if dot < 0.0 {
1354 let axis = self.any_orthogonal_vector().normalize();
1358 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1359 let result_length = self_length + (rhs_length - self_length) * s;
1361 rotation * self * (result_length / self_length)
1362 } else {
1363 self.lerp(rhs, s)
1365 }
1366 }
1367
1368 #[inline]
1370 #[must_use]
1371 pub fn as_vec3(self) -> crate::Vec3 {
1372 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1373 }
1374
1375 #[inline]
1377 #[must_use]
1378 pub fn as_vec3a(self) -> crate::Vec3A {
1379 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1380 }
1381
1382 #[cfg(feature = "i8")]
1384 #[inline]
1385 #[must_use]
1386 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1387 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1388 }
1389
1390 #[cfg(feature = "u8")]
1392 #[inline]
1393 #[must_use]
1394 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1395 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1396 }
1397
1398 #[cfg(feature = "i16")]
1400 #[inline]
1401 #[must_use]
1402 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1403 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1404 }
1405
1406 #[cfg(feature = "u16")]
1408 #[inline]
1409 #[must_use]
1410 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1411 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1412 }
1413
1414 #[cfg(feature = "i32")]
1416 #[inline]
1417 #[must_use]
1418 pub fn as_ivec3(self) -> crate::IVec3 {
1419 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1420 }
1421
1422 #[cfg(feature = "u32")]
1424 #[inline]
1425 #[must_use]
1426 pub fn as_uvec3(self) -> crate::UVec3 {
1427 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1428 }
1429
1430 #[cfg(feature = "i64")]
1432 #[inline]
1433 #[must_use]
1434 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1435 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1436 }
1437
1438 #[cfg(feature = "u64")]
1440 #[inline]
1441 #[must_use]
1442 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1443 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1444 }
1445
1446 #[cfg(feature = "isize")]
1448 #[inline]
1449 #[must_use]
1450 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1451 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1452 }
1453
1454 #[cfg(feature = "usize")]
1456 #[inline]
1457 #[must_use]
1458 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1459 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1460 }
1461}
1462
1463impl Default for DVec3 {
1464 #[inline(always)]
1465 fn default() -> Self {
1466 Self::ZERO
1467 }
1468}
1469
1470impl Div for DVec3 {
1471 type Output = Self;
1472 #[inline]
1473 fn div(self, rhs: Self) -> Self {
1474 Self::new(self.x.div(rhs.x), self.y.div(rhs.y), self.z.div(rhs.z))
1475 }
1476}
1477
1478impl Div<&Self> for DVec3 {
1479 type Output = Self;
1480 #[inline]
1481 fn div(self, rhs: &Self) -> Self {
1482 self.div(*rhs)
1483 }
1484}
1485
1486impl Div<&DVec3> for &DVec3 {
1487 type Output = DVec3;
1488 #[inline]
1489 fn div(self, rhs: &DVec3) -> DVec3 {
1490 (*self).div(*rhs)
1491 }
1492}
1493
1494impl Div<DVec3> for &DVec3 {
1495 type Output = DVec3;
1496 #[inline]
1497 fn div(self, rhs: DVec3) -> DVec3 {
1498 (*self).div(rhs)
1499 }
1500}
1501
1502impl DivAssign for DVec3 {
1503 #[inline]
1504 fn div_assign(&mut self, rhs: Self) {
1505 self.x.div_assign(rhs.x);
1506 self.y.div_assign(rhs.y);
1507 self.z.div_assign(rhs.z);
1508 }
1509}
1510
1511impl DivAssign<&Self> for DVec3 {
1512 #[inline]
1513 fn div_assign(&mut self, rhs: &Self) {
1514 self.div_assign(*rhs);
1515 }
1516}
1517
1518impl Div<f64> for DVec3 {
1519 type Output = Self;
1520 #[inline]
1521 fn div(self, rhs: f64) -> Self {
1522 Self::new(self.x.div(rhs), self.y.div(rhs), self.z.div(rhs))
1523 }
1524}
1525
1526impl Div<&f64> for DVec3 {
1527 type Output = Self;
1528 #[inline]
1529 fn div(self, rhs: &f64) -> Self {
1530 self.div(*rhs)
1531 }
1532}
1533
1534impl Div<&f64> for &DVec3 {
1535 type Output = DVec3;
1536 #[inline]
1537 fn div(self, rhs: &f64) -> DVec3 {
1538 (*self).div(*rhs)
1539 }
1540}
1541
1542impl Div<f64> for &DVec3 {
1543 type Output = DVec3;
1544 #[inline]
1545 fn div(self, rhs: f64) -> DVec3 {
1546 (*self).div(rhs)
1547 }
1548}
1549
1550impl DivAssign<f64> for DVec3 {
1551 #[inline]
1552 fn div_assign(&mut self, rhs: f64) {
1553 self.x.div_assign(rhs);
1554 self.y.div_assign(rhs);
1555 self.z.div_assign(rhs);
1556 }
1557}
1558
1559impl DivAssign<&f64> for DVec3 {
1560 #[inline]
1561 fn div_assign(&mut self, rhs: &f64) {
1562 self.div_assign(*rhs);
1563 }
1564}
1565
1566impl Div<DVec3> for f64 {
1567 type Output = DVec3;
1568 #[inline]
1569 fn div(self, rhs: DVec3) -> DVec3 {
1570 DVec3::new(self.div(rhs.x), self.div(rhs.y), self.div(rhs.z))
1571 }
1572}
1573
1574impl Div<&DVec3> for f64 {
1575 type Output = DVec3;
1576 #[inline]
1577 fn div(self, rhs: &DVec3) -> DVec3 {
1578 self.div(*rhs)
1579 }
1580}
1581
1582impl Div<&DVec3> for &f64 {
1583 type Output = DVec3;
1584 #[inline]
1585 fn div(self, rhs: &DVec3) -> DVec3 {
1586 (*self).div(*rhs)
1587 }
1588}
1589
1590impl Div<DVec3> for &f64 {
1591 type Output = DVec3;
1592 #[inline]
1593 fn div(self, rhs: DVec3) -> DVec3 {
1594 (*self).div(rhs)
1595 }
1596}
1597
1598impl Mul for DVec3 {
1599 type Output = Self;
1600 #[inline]
1601 fn mul(self, rhs: Self) -> Self {
1602 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y), self.z.mul(rhs.z))
1603 }
1604}
1605
1606impl Mul<&Self> for DVec3 {
1607 type Output = Self;
1608 #[inline]
1609 fn mul(self, rhs: &Self) -> Self {
1610 self.mul(*rhs)
1611 }
1612}
1613
1614impl Mul<&DVec3> for &DVec3 {
1615 type Output = DVec3;
1616 #[inline]
1617 fn mul(self, rhs: &DVec3) -> DVec3 {
1618 (*self).mul(*rhs)
1619 }
1620}
1621
1622impl Mul<DVec3> for &DVec3 {
1623 type Output = DVec3;
1624 #[inline]
1625 fn mul(self, rhs: DVec3) -> DVec3 {
1626 (*self).mul(rhs)
1627 }
1628}
1629
1630impl MulAssign for DVec3 {
1631 #[inline]
1632 fn mul_assign(&mut self, rhs: Self) {
1633 self.x.mul_assign(rhs.x);
1634 self.y.mul_assign(rhs.y);
1635 self.z.mul_assign(rhs.z);
1636 }
1637}
1638
1639impl MulAssign<&Self> for DVec3 {
1640 #[inline]
1641 fn mul_assign(&mut self, rhs: &Self) {
1642 self.mul_assign(*rhs);
1643 }
1644}
1645
1646impl Mul<f64> for DVec3 {
1647 type Output = Self;
1648 #[inline]
1649 fn mul(self, rhs: f64) -> Self {
1650 Self::new(self.x.mul(rhs), self.y.mul(rhs), self.z.mul(rhs))
1651 }
1652}
1653
1654impl Mul<&f64> for DVec3 {
1655 type Output = Self;
1656 #[inline]
1657 fn mul(self, rhs: &f64) -> Self {
1658 self.mul(*rhs)
1659 }
1660}
1661
1662impl Mul<&f64> for &DVec3 {
1663 type Output = DVec3;
1664 #[inline]
1665 fn mul(self, rhs: &f64) -> DVec3 {
1666 (*self).mul(*rhs)
1667 }
1668}
1669
1670impl Mul<f64> for &DVec3 {
1671 type Output = DVec3;
1672 #[inline]
1673 fn mul(self, rhs: f64) -> DVec3 {
1674 (*self).mul(rhs)
1675 }
1676}
1677
1678impl MulAssign<f64> for DVec3 {
1679 #[inline]
1680 fn mul_assign(&mut self, rhs: f64) {
1681 self.x.mul_assign(rhs);
1682 self.y.mul_assign(rhs);
1683 self.z.mul_assign(rhs);
1684 }
1685}
1686
1687impl MulAssign<&f64> for DVec3 {
1688 #[inline]
1689 fn mul_assign(&mut self, rhs: &f64) {
1690 self.mul_assign(*rhs);
1691 }
1692}
1693
1694impl Mul<DVec3> for f64 {
1695 type Output = DVec3;
1696 #[inline]
1697 fn mul(self, rhs: DVec3) -> DVec3 {
1698 DVec3::new(self.mul(rhs.x), self.mul(rhs.y), self.mul(rhs.z))
1699 }
1700}
1701
1702impl Mul<&DVec3> for f64 {
1703 type Output = DVec3;
1704 #[inline]
1705 fn mul(self, rhs: &DVec3) -> DVec3 {
1706 self.mul(*rhs)
1707 }
1708}
1709
1710impl Mul<&DVec3> for &f64 {
1711 type Output = DVec3;
1712 #[inline]
1713 fn mul(self, rhs: &DVec3) -> DVec3 {
1714 (*self).mul(*rhs)
1715 }
1716}
1717
1718impl Mul<DVec3> for &f64 {
1719 type Output = DVec3;
1720 #[inline]
1721 fn mul(self, rhs: DVec3) -> DVec3 {
1722 (*self).mul(rhs)
1723 }
1724}
1725
1726impl Add for DVec3 {
1727 type Output = Self;
1728 #[inline]
1729 fn add(self, rhs: Self) -> Self {
1730 Self::new(self.x.add(rhs.x), self.y.add(rhs.y), self.z.add(rhs.z))
1731 }
1732}
1733
1734impl Add<&Self> for DVec3 {
1735 type Output = Self;
1736 #[inline]
1737 fn add(self, rhs: &Self) -> Self {
1738 self.add(*rhs)
1739 }
1740}
1741
1742impl Add<&DVec3> for &DVec3 {
1743 type Output = DVec3;
1744 #[inline]
1745 fn add(self, rhs: &DVec3) -> DVec3 {
1746 (*self).add(*rhs)
1747 }
1748}
1749
1750impl Add<DVec3> for &DVec3 {
1751 type Output = DVec3;
1752 #[inline]
1753 fn add(self, rhs: DVec3) -> DVec3 {
1754 (*self).add(rhs)
1755 }
1756}
1757
1758impl AddAssign for DVec3 {
1759 #[inline]
1760 fn add_assign(&mut self, rhs: Self) {
1761 self.x.add_assign(rhs.x);
1762 self.y.add_assign(rhs.y);
1763 self.z.add_assign(rhs.z);
1764 }
1765}
1766
1767impl AddAssign<&Self> for DVec3 {
1768 #[inline]
1769 fn add_assign(&mut self, rhs: &Self) {
1770 self.add_assign(*rhs);
1771 }
1772}
1773
1774impl Add<f64> for DVec3 {
1775 type Output = Self;
1776 #[inline]
1777 fn add(self, rhs: f64) -> Self {
1778 Self::new(self.x.add(rhs), self.y.add(rhs), self.z.add(rhs))
1779 }
1780}
1781
1782impl Add<&f64> for DVec3 {
1783 type Output = Self;
1784 #[inline]
1785 fn add(self, rhs: &f64) -> Self {
1786 self.add(*rhs)
1787 }
1788}
1789
1790impl Add<&f64> for &DVec3 {
1791 type Output = DVec3;
1792 #[inline]
1793 fn add(self, rhs: &f64) -> DVec3 {
1794 (*self).add(*rhs)
1795 }
1796}
1797
1798impl Add<f64> for &DVec3 {
1799 type Output = DVec3;
1800 #[inline]
1801 fn add(self, rhs: f64) -> DVec3 {
1802 (*self).add(rhs)
1803 }
1804}
1805
1806impl AddAssign<f64> for DVec3 {
1807 #[inline]
1808 fn add_assign(&mut self, rhs: f64) {
1809 self.x.add_assign(rhs);
1810 self.y.add_assign(rhs);
1811 self.z.add_assign(rhs);
1812 }
1813}
1814
1815impl AddAssign<&f64> for DVec3 {
1816 #[inline]
1817 fn add_assign(&mut self, rhs: &f64) {
1818 self.add_assign(*rhs);
1819 }
1820}
1821
1822impl Add<DVec3> for f64 {
1823 type Output = DVec3;
1824 #[inline]
1825 fn add(self, rhs: DVec3) -> DVec3 {
1826 DVec3::new(self.add(rhs.x), self.add(rhs.y), self.add(rhs.z))
1827 }
1828}
1829
1830impl Add<&DVec3> for f64 {
1831 type Output = DVec3;
1832 #[inline]
1833 fn add(self, rhs: &DVec3) -> DVec3 {
1834 self.add(*rhs)
1835 }
1836}
1837
1838impl Add<&DVec3> for &f64 {
1839 type Output = DVec3;
1840 #[inline]
1841 fn add(self, rhs: &DVec3) -> DVec3 {
1842 (*self).add(*rhs)
1843 }
1844}
1845
1846impl Add<DVec3> for &f64 {
1847 type Output = DVec3;
1848 #[inline]
1849 fn add(self, rhs: DVec3) -> DVec3 {
1850 (*self).add(rhs)
1851 }
1852}
1853
1854impl Sub for DVec3 {
1855 type Output = Self;
1856 #[inline]
1857 fn sub(self, rhs: Self) -> Self {
1858 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y), self.z.sub(rhs.z))
1859 }
1860}
1861
1862impl Sub<&Self> for DVec3 {
1863 type Output = Self;
1864 #[inline]
1865 fn sub(self, rhs: &Self) -> Self {
1866 self.sub(*rhs)
1867 }
1868}
1869
1870impl Sub<&DVec3> for &DVec3 {
1871 type Output = DVec3;
1872 #[inline]
1873 fn sub(self, rhs: &DVec3) -> DVec3 {
1874 (*self).sub(*rhs)
1875 }
1876}
1877
1878impl Sub<DVec3> for &DVec3 {
1879 type Output = DVec3;
1880 #[inline]
1881 fn sub(self, rhs: DVec3) -> DVec3 {
1882 (*self).sub(rhs)
1883 }
1884}
1885
1886impl SubAssign for DVec3 {
1887 #[inline]
1888 fn sub_assign(&mut self, rhs: Self) {
1889 self.x.sub_assign(rhs.x);
1890 self.y.sub_assign(rhs.y);
1891 self.z.sub_assign(rhs.z);
1892 }
1893}
1894
1895impl SubAssign<&Self> for DVec3 {
1896 #[inline]
1897 fn sub_assign(&mut self, rhs: &Self) {
1898 self.sub_assign(*rhs);
1899 }
1900}
1901
1902impl Sub<f64> for DVec3 {
1903 type Output = Self;
1904 #[inline]
1905 fn sub(self, rhs: f64) -> Self {
1906 Self::new(self.x.sub(rhs), self.y.sub(rhs), self.z.sub(rhs))
1907 }
1908}
1909
1910impl Sub<&f64> for DVec3 {
1911 type Output = Self;
1912 #[inline]
1913 fn sub(self, rhs: &f64) -> Self {
1914 self.sub(*rhs)
1915 }
1916}
1917
1918impl Sub<&f64> for &DVec3 {
1919 type Output = DVec3;
1920 #[inline]
1921 fn sub(self, rhs: &f64) -> DVec3 {
1922 (*self).sub(*rhs)
1923 }
1924}
1925
1926impl Sub<f64> for &DVec3 {
1927 type Output = DVec3;
1928 #[inline]
1929 fn sub(self, rhs: f64) -> DVec3 {
1930 (*self).sub(rhs)
1931 }
1932}
1933
1934impl SubAssign<f64> for DVec3 {
1935 #[inline]
1936 fn sub_assign(&mut self, rhs: f64) {
1937 self.x.sub_assign(rhs);
1938 self.y.sub_assign(rhs);
1939 self.z.sub_assign(rhs);
1940 }
1941}
1942
1943impl SubAssign<&f64> for DVec3 {
1944 #[inline]
1945 fn sub_assign(&mut self, rhs: &f64) {
1946 self.sub_assign(*rhs);
1947 }
1948}
1949
1950impl Sub<DVec3> for f64 {
1951 type Output = DVec3;
1952 #[inline]
1953 fn sub(self, rhs: DVec3) -> DVec3 {
1954 DVec3::new(self.sub(rhs.x), self.sub(rhs.y), self.sub(rhs.z))
1955 }
1956}
1957
1958impl Sub<&DVec3> for f64 {
1959 type Output = DVec3;
1960 #[inline]
1961 fn sub(self, rhs: &DVec3) -> DVec3 {
1962 self.sub(*rhs)
1963 }
1964}
1965
1966impl Sub<&DVec3> for &f64 {
1967 type Output = DVec3;
1968 #[inline]
1969 fn sub(self, rhs: &DVec3) -> DVec3 {
1970 (*self).sub(*rhs)
1971 }
1972}
1973
1974impl Sub<DVec3> for &f64 {
1975 type Output = DVec3;
1976 #[inline]
1977 fn sub(self, rhs: DVec3) -> DVec3 {
1978 (*self).sub(rhs)
1979 }
1980}
1981
1982impl Rem for DVec3 {
1983 type Output = Self;
1984 #[inline]
1985 fn rem(self, rhs: Self) -> Self {
1986 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y), self.z.rem(rhs.z))
1987 }
1988}
1989
1990impl Rem<&Self> for DVec3 {
1991 type Output = Self;
1992 #[inline]
1993 fn rem(self, rhs: &Self) -> Self {
1994 self.rem(*rhs)
1995 }
1996}
1997
1998impl Rem<&DVec3> for &DVec3 {
1999 type Output = DVec3;
2000 #[inline]
2001 fn rem(self, rhs: &DVec3) -> DVec3 {
2002 (*self).rem(*rhs)
2003 }
2004}
2005
2006impl Rem<DVec3> for &DVec3 {
2007 type Output = DVec3;
2008 #[inline]
2009 fn rem(self, rhs: DVec3) -> DVec3 {
2010 (*self).rem(rhs)
2011 }
2012}
2013
2014impl RemAssign for DVec3 {
2015 #[inline]
2016 fn rem_assign(&mut self, rhs: Self) {
2017 self.x.rem_assign(rhs.x);
2018 self.y.rem_assign(rhs.y);
2019 self.z.rem_assign(rhs.z);
2020 }
2021}
2022
2023impl RemAssign<&Self> for DVec3 {
2024 #[inline]
2025 fn rem_assign(&mut self, rhs: &Self) {
2026 self.rem_assign(*rhs);
2027 }
2028}
2029
2030impl Rem<f64> for DVec3 {
2031 type Output = Self;
2032 #[inline]
2033 fn rem(self, rhs: f64) -> Self {
2034 Self::new(self.x.rem(rhs), self.y.rem(rhs), self.z.rem(rhs))
2035 }
2036}
2037
2038impl Rem<&f64> for DVec3 {
2039 type Output = Self;
2040 #[inline]
2041 fn rem(self, rhs: &f64) -> Self {
2042 self.rem(*rhs)
2043 }
2044}
2045
2046impl Rem<&f64> for &DVec3 {
2047 type Output = DVec3;
2048 #[inline]
2049 fn rem(self, rhs: &f64) -> DVec3 {
2050 (*self).rem(*rhs)
2051 }
2052}
2053
2054impl Rem<f64> for &DVec3 {
2055 type Output = DVec3;
2056 #[inline]
2057 fn rem(self, rhs: f64) -> DVec3 {
2058 (*self).rem(rhs)
2059 }
2060}
2061
2062impl RemAssign<f64> for DVec3 {
2063 #[inline]
2064 fn rem_assign(&mut self, rhs: f64) {
2065 self.x.rem_assign(rhs);
2066 self.y.rem_assign(rhs);
2067 self.z.rem_assign(rhs);
2068 }
2069}
2070
2071impl RemAssign<&f64> for DVec3 {
2072 #[inline]
2073 fn rem_assign(&mut self, rhs: &f64) {
2074 self.rem_assign(*rhs);
2075 }
2076}
2077
2078impl Rem<DVec3> for f64 {
2079 type Output = DVec3;
2080 #[inline]
2081 fn rem(self, rhs: DVec3) -> DVec3 {
2082 DVec3::new(self.rem(rhs.x), self.rem(rhs.y), self.rem(rhs.z))
2083 }
2084}
2085
2086impl Rem<&DVec3> for f64 {
2087 type Output = DVec3;
2088 #[inline]
2089 fn rem(self, rhs: &DVec3) -> DVec3 {
2090 self.rem(*rhs)
2091 }
2092}
2093
2094impl Rem<&DVec3> for &f64 {
2095 type Output = DVec3;
2096 #[inline]
2097 fn rem(self, rhs: &DVec3) -> DVec3 {
2098 (*self).rem(*rhs)
2099 }
2100}
2101
2102impl Rem<DVec3> for &f64 {
2103 type Output = DVec3;
2104 #[inline]
2105 fn rem(self, rhs: DVec3) -> DVec3 {
2106 (*self).rem(rhs)
2107 }
2108}
2109
2110impl AsRef<[f64; 3]> for DVec3 {
2111 #[inline]
2112 fn as_ref(&self) -> &[f64; 3] {
2113 unsafe { &*(self as *const Self as *const [f64; 3]) }
2114 }
2115}
2116
2117impl AsMut<[f64; 3]> for DVec3 {
2118 #[inline]
2119 fn as_mut(&mut self) -> &mut [f64; 3] {
2120 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2121 }
2122}
2123
2124impl Sum for DVec3 {
2125 #[inline]
2126 fn sum<I>(iter: I) -> Self
2127 where
2128 I: Iterator<Item = Self>,
2129 {
2130 iter.fold(Self::ZERO, Self::add)
2131 }
2132}
2133
2134impl<'a> Sum<&'a Self> for DVec3 {
2135 #[inline]
2136 fn sum<I>(iter: I) -> Self
2137 where
2138 I: Iterator<Item = &'a Self>,
2139 {
2140 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2141 }
2142}
2143
2144impl Product for DVec3 {
2145 #[inline]
2146 fn product<I>(iter: I) -> Self
2147 where
2148 I: Iterator<Item = Self>,
2149 {
2150 iter.fold(Self::ONE, Self::mul)
2151 }
2152}
2153
2154impl<'a> Product<&'a Self> for DVec3 {
2155 #[inline]
2156 fn product<I>(iter: I) -> Self
2157 where
2158 I: Iterator<Item = &'a Self>,
2159 {
2160 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2161 }
2162}
2163
2164impl Neg for DVec3 {
2165 type Output = Self;
2166 #[inline]
2167 fn neg(self) -> Self {
2168 Self::new(self.x.neg(), self.y.neg(), self.z.neg())
2169 }
2170}
2171
2172impl Neg for &DVec3 {
2173 type Output = DVec3;
2174 #[inline]
2175 fn neg(self) -> DVec3 {
2176 (*self).neg()
2177 }
2178}
2179
2180impl Index<usize> for DVec3 {
2181 type Output = f64;
2182 #[inline]
2183 fn index(&self, index: usize) -> &Self::Output {
2184 match index {
2185 0 => &self.x,
2186 1 => &self.y,
2187 2 => &self.z,
2188 _ => panic!("index out of bounds"),
2189 }
2190 }
2191}
2192
2193impl IndexMut<usize> for DVec3 {
2194 #[inline]
2195 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2196 match index {
2197 0 => &mut self.x,
2198 1 => &mut self.y,
2199 2 => &mut self.z,
2200 _ => panic!("index out of bounds"),
2201 }
2202 }
2203}
2204
2205impl fmt::Display for DVec3 {
2206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2207 if let Some(p) = f.precision() {
2208 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2209 } else {
2210 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2211 }
2212 }
2213}
2214
2215impl fmt::Debug for DVec3 {
2216 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2217 fmt.debug_tuple(stringify!(DVec3))
2218 .field(&self.x)
2219 .field(&self.y)
2220 .field(&self.z)
2221 .finish()
2222 }
2223}
2224
2225impl From<[f64; 3]> for DVec3 {
2226 #[inline]
2227 fn from(a: [f64; 3]) -> Self {
2228 Self::new(a[0], a[1], a[2])
2229 }
2230}
2231
2232impl From<DVec3> for [f64; 3] {
2233 #[inline]
2234 fn from(v: DVec3) -> Self {
2235 [v.x, v.y, v.z]
2236 }
2237}
2238
2239impl From<(f64, f64, f64)> for DVec3 {
2240 #[inline]
2241 fn from(t: (f64, f64, f64)) -> Self {
2242 Self::new(t.0, t.1, t.2)
2243 }
2244}
2245
2246impl From<DVec3> for (f64, f64, f64) {
2247 #[inline]
2248 fn from(v: DVec3) -> Self {
2249 (v.x, v.y, v.z)
2250 }
2251}
2252
2253impl From<(DVec2, f64)> for DVec3 {
2254 #[inline]
2255 fn from((v, z): (DVec2, f64)) -> Self {
2256 Self::new(v.x, v.y, z)
2257 }
2258}
2259
2260impl From<Vec3> for DVec3 {
2261 #[inline]
2262 fn from(v: Vec3) -> Self {
2263 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2264 }
2265}
2266
2267#[cfg(feature = "i32")]
2268impl From<IVec3> for DVec3 {
2269 #[inline]
2270 fn from(v: IVec3) -> Self {
2271 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2272 }
2273}
2274
2275#[cfg(feature = "u32")]
2276impl From<UVec3> for DVec3 {
2277 #[inline]
2278 fn from(v: UVec3) -> Self {
2279 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2280 }
2281}
2282
2283impl From<BVec3> for DVec3 {
2284 #[inline]
2285 fn from(v: BVec3) -> Self {
2286 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2287 }
2288}
2289
2290impl From<BVec3A> for DVec3 {
2291 #[inline]
2292 fn from(v: BVec3A) -> Self {
2293 let bool_array: [bool; 3] = v.into();
2294 Self::new(
2295 f64::from(bool_array[0]),
2296 f64::from(bool_array[1]),
2297 f64::from(bool_array[2]),
2298 )
2299 }
2300}