1use crate::{BVec3, BVec3A, DQuat, DVec2, DVec4, FloatExt, f64::math};
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
99 #[inline(always)]
101 #[must_use]
102 pub const fn new(x: f64, y: f64, z: f64) -> Self {
103 Self { x, y, z }
104 }
105
106 #[inline]
108 #[must_use]
109 pub const fn splat(v: f64) -> Self {
110 Self::new(v, v, v)
111 }
112
113 #[inline]
115 #[must_use]
116 pub fn map<F>(self, mut f: F) -> Self
117 where
118 F: FnMut(f64) -> f64,
119 {
120 Self::new(f(self.x), f(self.y), f(self.z))
121 }
122
123 #[inline]
129 #[must_use]
130 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
131 Self::new(
132 if mask.test(0) { if_true.x } else { if_false.x },
133 if mask.test(1) { if_true.y } else { if_false.y },
134 if mask.test(2) { if_true.z } else { if_false.z },
135 )
136 }
137
138 #[inline]
140 #[must_use]
141 pub const fn from_array(a: [f64; 3]) -> Self {
142 Self::new(a[0], a[1], a[2])
143 }
144
145 #[inline]
147 #[must_use]
148 pub const fn to_array(&self) -> [f64; 3] {
149 [self.x, self.y, self.z]
150 }
151
152 #[inline]
158 #[must_use]
159 #[track_caller]
160 pub const fn from_slice(slice: &[f64]) -> Self {
161 assert!(slice.len() >= 3);
162 Self::new(slice[0], slice[1], slice[2])
163 }
164
165 #[inline]
171 #[track_caller]
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 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
209 pub fn from_homogeneous(v: DVec4) -> Self {
210 glam_assert!(v.w != 0.0);
211 Self::from_vec4(v) / v.w
212 }
213
214 #[inline]
216 #[must_use]
217 pub fn to_homogeneous(self) -> DVec4 {
218 self.extend(1.0)
219 }
220
221 #[inline]
223 #[must_use]
224 pub fn with_x(mut self, x: f64) -> Self {
225 self.x = x;
226 self
227 }
228
229 #[inline]
231 #[must_use]
232 pub fn with_y(mut self, y: f64) -> Self {
233 self.y = y;
234 self
235 }
236
237 #[inline]
239 #[must_use]
240 pub fn with_z(mut self, z: f64) -> Self {
241 self.z = z;
242 self
243 }
244
245 #[inline]
247 #[must_use]
248 pub fn dot(self, rhs: Self) -> f64 {
249 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
250 }
251
252 #[inline]
254 #[must_use]
255 pub fn dot_into_vec(self, rhs: Self) -> Self {
256 Self::splat(self.dot(rhs))
257 }
258
259 #[inline]
261 #[must_use]
262 pub fn cross(self, rhs: Self) -> Self {
263 Self::new(
264 self.y * rhs.z - rhs.y * self.z,
265 self.z * rhs.x - rhs.z * self.x,
266 self.x * rhs.y - rhs.x * self.y,
267 )
268 }
269
270 #[inline]
278 #[must_use]
279 pub fn min(self, rhs: Self) -> Self {
280 Self::new(
281 if self.x < rhs.x { self.x } else { rhs.x },
282 if self.y < rhs.y { self.y } else { rhs.y },
283 if self.z < rhs.z { self.z } else { rhs.z },
284 )
285 }
286
287 #[inline]
295 #[must_use]
296 pub fn max(self, rhs: Self) -> Self {
297 Self::new(
298 if self.x > rhs.x { self.x } else { rhs.x },
299 if self.y > rhs.y { self.y } else { rhs.y },
300 if self.z > rhs.z { self.z } else { rhs.z },
301 )
302 }
303
304 #[inline]
314 #[must_use]
315 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
316 pub fn clamp(self, min: Self, max: Self) -> Self {
317 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
318 self.max(min).min(max)
319 }
320
321 #[inline]
327 #[must_use]
328 pub fn min_element(self) -> f64 {
329 let min = |a, b| if a < b { a } else { b };
330 min(min(self.x, self.y), self.z)
331 }
332
333 #[inline]
339 #[must_use]
340 pub fn max_element(self) -> f64 {
341 let max = |a, b| if a > b { a } else { b };
342 max(max(self.x, self.y), self.z)
343 }
344
345 #[doc(alias = "argmin")]
347 #[inline]
348 #[must_use]
349 pub fn min_position(self) -> usize {
350 let mut min = self.x;
351 let mut index = 0;
352 if self.y < min {
353 min = self.y;
354 index = 1;
355 }
356 if self.z < min {
357 index = 2;
358 }
359 index
360 }
361
362 #[doc(alias = "argmax")]
364 #[inline]
365 #[must_use]
366 pub fn max_position(self) -> usize {
367 let mut max = self.x;
368 let mut index = 0;
369 if self.y > max {
370 max = self.y;
371 index = 1;
372 }
373 if self.z > max {
374 index = 2;
375 }
376 index
377 }
378
379 #[inline]
383 #[must_use]
384 pub fn element_sum(self) -> f64 {
385 self.x + self.y + self.z
386 }
387
388 #[inline]
392 #[must_use]
393 pub fn element_product(self) -> f64 {
394 self.x * self.y * self.z
395 }
396
397 #[inline]
403 #[must_use]
404 pub fn cmpeq(self, rhs: Self) -> BVec3 {
405 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
406 }
407
408 #[inline]
414 #[must_use]
415 pub fn cmpne(self, rhs: Self) -> BVec3 {
416 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
417 }
418
419 #[inline]
425 #[must_use]
426 pub fn cmpge(self, rhs: Self) -> BVec3 {
427 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
428 }
429
430 #[inline]
436 #[must_use]
437 pub fn cmpgt(self, rhs: Self) -> BVec3 {
438 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
439 }
440
441 #[inline]
447 #[must_use]
448 pub fn cmple(self, rhs: Self) -> BVec3 {
449 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
450 }
451
452 #[inline]
458 #[must_use]
459 pub fn cmplt(self, rhs: Self) -> BVec3 {
460 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
461 }
462
463 #[inline]
465 #[must_use]
466 pub fn abs(self) -> Self {
467 Self::new(self.x.abs(), self.y.abs(), self.z.abs())
468 }
469
470 #[inline]
476 #[must_use]
477 pub fn signum(self) -> Self {
478 Self::new(self.x.signum(), self.y.signum(), self.z.signum())
479 }
480
481 #[inline]
483 #[must_use]
484 pub fn copysign(self, rhs: Self) -> Self {
485 Self::new(
486 self.x.copysign(rhs.x),
487 self.y.copysign(rhs.y),
488 self.z.copysign(rhs.z),
489 )
490 }
491
492 #[inline]
500 #[must_use]
501 pub fn is_negative_bitmask(self) -> u32 {
502 (self.x.is_sign_negative() as u32)
503 | ((self.y.is_sign_negative() as u32) << 1)
504 | ((self.z.is_sign_negative() as u32) << 2)
505 }
506
507 #[inline]
512 #[must_use]
513 pub fn is_negative_mask(self) -> BVec3 {
514 BVec3::new(
515 self.x.is_sign_negative(),
516 self.y.is_sign_negative(),
517 self.z.is_sign_negative(),
518 )
519 }
520
521 #[inline]
524 #[must_use]
525 pub fn is_finite(self) -> bool {
526 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
527 }
528
529 #[inline]
533 #[must_use]
534 pub fn is_finite_mask(self) -> BVec3 {
535 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
536 }
537
538 #[inline]
540 #[must_use]
541 pub fn is_nan(self) -> bool {
542 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
543 }
544
545 #[inline]
549 #[must_use]
550 pub fn is_nan_mask(self) -> BVec3 {
551 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
552 }
553
554 #[doc(alias = "magnitude")]
556 #[inline]
557 #[must_use]
558 pub fn length(self) -> f64 {
559 math::sqrt(self.dot(self))
560 }
561
562 #[allow(dead_code)]
564 fn is_non_zero(self) -> bool {
565 self.length_squared() > 0.0
566 }
567
568 #[doc(alias = "magnitude2")]
572 #[inline]
573 #[must_use]
574 pub fn length_squared(self) -> f64 {
575 self.dot(self)
576 }
577
578 #[inline]
582 #[must_use]
583 pub fn length_recip(self) -> f64 {
584 1.0 / self.length()
585 }
586
587 #[inline]
589 #[must_use]
590 pub fn distance(self, rhs: Self) -> f64 {
591 (self - rhs).length()
592 }
593
594 #[inline]
596 #[must_use]
597 pub fn distance_squared(self, rhs: Self) -> f64 {
598 (self - rhs).length_squared()
599 }
600
601 #[inline]
603 #[must_use]
604 pub fn div_euclid(self, rhs: Self) -> Self {
605 Self::new(
606 math::div_euclid(self.x, rhs.x),
607 math::div_euclid(self.y, rhs.y),
608 math::div_euclid(self.z, rhs.z),
609 )
610 }
611
612 #[inline]
616 #[must_use]
617 pub fn rem_euclid(self, rhs: Self) -> Self {
618 Self::new(
619 math::rem_euclid(self.x, rhs.x),
620 math::rem_euclid(self.y, rhs.y),
621 math::rem_euclid(self.z, rhs.z),
622 )
623 }
624
625 #[inline]
635 #[must_use]
636 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
637 pub fn normalize(self) -> Self {
638 #[allow(clippy::let_and_return)]
639 let normalized = self.mul(self.length_recip());
640 glam_assert!(normalized.is_finite());
641 normalized
642 }
643
644 #[inline]
651 #[must_use]
652 pub fn try_normalize(self) -> Option<Self> {
653 let rcp = self.length_recip();
654 if rcp.is_finite() && rcp > 0.0 {
655 Some(self * rcp)
656 } else {
657 None
658 }
659 }
660
661 #[inline]
669 #[must_use]
670 pub fn normalize_or(self, fallback: Self) -> Self {
671 let rcp = self.length_recip();
672 if rcp.is_finite() && rcp > 0.0 {
673 self * rcp
674 } else {
675 fallback
676 }
677 }
678
679 #[inline]
686 #[must_use]
687 pub fn normalize_or_zero(self) -> Self {
688 self.normalize_or(Self::ZERO)
689 }
690
691 #[inline]
695 #[must_use]
696 pub fn normalize_and_length(self) -> (Self, f64) {
697 let length = self.length();
698 let rcp = 1.0 / length;
699 if rcp.is_finite() && rcp > 0.0 {
700 (self * rcp, length)
701 } else {
702 (Self::X, 0.0)
703 }
704 }
705
706 #[inline]
710 #[must_use]
711 pub fn is_normalized(self) -> bool {
712 (self.length_squared() - 1.0).abs() <= 2e-4
713 }
714
715 #[inline]
723 #[must_use]
724 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
725 pub fn project_onto(self, rhs: Self) -> Self {
726 let rhs_len_sq = rhs.dot(rhs);
727 glam_assert!(rhs_len_sq != 0.0);
728 rhs * (self.dot(rhs) / rhs_len_sq)
729 }
730
731 #[doc(alias("plane"))]
742 #[inline]
743 #[must_use]
744 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
745 pub fn reject_from(self, rhs: Self) -> Self {
746 self - self.project_onto(rhs)
747 }
748
749 #[inline]
757 #[must_use]
758 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
759 pub fn project_onto_normalized(self, rhs: Self) -> Self {
760 glam_assert!(rhs.is_normalized());
761 rhs * self.dot(rhs)
762 }
763
764 #[doc(alias("plane"))]
775 #[inline]
776 #[must_use]
777 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
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::new(
788 math::round(self.x),
789 math::round(self.y),
790 math::round(self.z),
791 )
792 }
793
794 #[inline]
797 #[must_use]
798 pub fn floor(self) -> Self {
799 Self::new(
800 math::floor(self.x),
801 math::floor(self.y),
802 math::floor(self.z),
803 )
804 }
805
806 #[inline]
809 #[must_use]
810 pub fn ceil(self) -> Self {
811 Self::new(math::ceil(self.x), math::ceil(self.y), math::ceil(self.z))
812 }
813
814 #[inline]
817 #[must_use]
818 pub fn trunc(self) -> Self {
819 Self::new(
820 math::trunc(self.x),
821 math::trunc(self.y),
822 math::trunc(self.z),
823 )
824 }
825
826 #[inline]
830 #[must_use]
831 pub fn step(self, rhs: Self) -> Self {
832 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
833 }
834
835 #[inline]
846 #[must_use]
847 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
848 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
849 glam_assert!(edge0.cmplt(edge1).all());
850 let t = ((self - edge0) / (edge1 - edge0)).saturate();
851 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
852 }
853
854 #[inline]
856 #[must_use]
857 pub fn saturate(self) -> Self {
858 self.clamp(Self::ZERO, Self::ONE)
859 }
860
861 #[inline]
868 #[must_use]
869 pub fn fract(self) -> Self {
870 self - self.trunc()
871 }
872
873 #[inline]
880 #[must_use]
881 pub fn fract_gl(self) -> Self {
882 self - self.floor()
883 }
884
885 #[inline]
888 #[must_use]
889 pub fn exp(self) -> Self {
890 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
891 }
892
893 #[inline]
895 #[must_use]
896 pub fn exp2(self) -> Self {
897 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
898 }
899
900 #[inline]
903 #[must_use]
904 pub fn ln(self) -> Self {
905 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
906 }
907
908 #[inline]
911 #[must_use]
912 pub fn log2(self) -> Self {
913 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
914 }
915
916 #[inline]
918 #[must_use]
919 pub fn powf(self, n: f64) -> Self {
920 Self::new(
921 math::powf(self.x, n),
922 math::powf(self.y, n),
923 math::powf(self.z, n),
924 )
925 }
926
927 #[inline]
930 #[must_use]
931 pub fn sqrt(self) -> Self {
932 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
933 }
934
935 #[inline]
937 #[must_use]
938 pub fn cos(self) -> Self {
939 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
940 }
941
942 #[inline]
944 #[must_use]
945 pub fn sin(self) -> Self {
946 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
947 }
948
949 #[inline]
951 #[must_use]
952 pub fn sin_cos(self) -> (Self, Self) {
953 let (sin_x, cos_x) = math::sin_cos(self.x);
954 let (sin_y, cos_y) = math::sin_cos(self.y);
955 let (sin_z, cos_z) = math::sin_cos(self.z);
956
957 (
958 Self::new(sin_x, sin_y, sin_z),
959 Self::new(cos_x, cos_y, cos_z),
960 )
961 }
962
963 #[inline]
965 #[must_use]
966 pub fn recip(self) -> Self {
967 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z)
968 }
969
970 #[doc(alias = "mix")]
982 #[inline]
983 #[must_use]
984 pub fn lerp(self, rhs: Self, s: f64) -> Self {
985 self * (1.0 - s) + rhs * s
986 }
987
988 #[doc(alias = "mix")]
1005 #[inline]
1006 #[must_use]
1007 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
1008 self + (rhs - self) * s
1009 }
1010
1011 #[inline]
1016 #[must_use]
1017 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1018 let a = rhs - self;
1019 let len = a.length();
1020 if len <= d || len <= 1e-4 {
1021 return rhs;
1022 }
1023 self + a / len * d
1024 }
1025
1026 #[inline]
1032 pub fn midpoint(self, rhs: Self) -> Self {
1033 (self + rhs) * 0.5
1034 }
1035
1036 #[inline]
1046 #[must_use]
1047 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1048 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1049 }
1050
1051 #[inline]
1057 #[must_use]
1058 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1059 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1060 glam_assert!(0.0 <= min);
1061 glam_assert!(min <= max);
1062 let length_sq = self.length_squared();
1063 if length_sq < min * min {
1064 min * (self / math::sqrt(length_sq))
1065 } else if length_sq > max * max {
1066 max * (self / math::sqrt(length_sq))
1067 } else {
1068 self
1069 }
1070 }
1071
1072 #[inline]
1078 #[must_use]
1079 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1080 pub fn clamp_length_max(self, max: f64) -> Self {
1081 glam_assert!(0.0 <= max);
1082 let length_sq = self.length_squared();
1083 if length_sq > max * max {
1084 max * (self / math::sqrt(length_sq))
1085 } else {
1086 self
1087 }
1088 }
1089
1090 #[inline]
1096 #[must_use]
1097 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1098 pub fn clamp_length_min(self, min: f64) -> Self {
1099 glam_assert!(0.0 <= min);
1100 let length_sq = self.length_squared();
1101 if length_sq < min * min {
1102 min * (self / math::sqrt(length_sq))
1103 } else {
1104 self
1105 }
1106 }
1107
1108 #[inline]
1116 #[must_use]
1117 pub fn mul_add(self, a: Self, b: Self) -> Self {
1118 Self::new(
1119 math::mul_add(self.x, a.x, b.x),
1120 math::mul_add(self.y, a.y, b.y),
1121 math::mul_add(self.z, a.z, b.z),
1122 )
1123 }
1124
1125 #[inline]
1134 #[must_use]
1135 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1136 pub fn reflect(self, normal: Self) -> Self {
1137 glam_assert!(normal.is_normalized());
1138 self - 2.0 * self.dot(normal) * normal
1139 }
1140
1141 #[inline]
1151 #[must_use]
1152 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1153 pub fn refract(self, normal: Self, eta: f64) -> Self {
1154 glam_assert!(self.is_normalized());
1155 glam_assert!(normal.is_normalized());
1156 let n_dot_i = normal.dot(self);
1157 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1158 if k >= 0.0 {
1159 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1160 } else {
1161 Self::ZERO
1162 }
1163 }
1164
1165 #[inline]
1176 #[must_use]
1177 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1178 pub fn angle_between(self, rhs: Self) -> f64 {
1179 glam_assert!(self.is_non_zero());
1180 glam_assert!(rhs.is_non_zero());
1181 math::acos_approx(
1182 self.dot(rhs)
1183 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1184 )
1185 }
1186
1187 #[doc(alias = "signed_angle")]
1203 #[inline]
1204 #[must_use]
1205 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1206 pub fn angle_to(self, rhs: Self, axis: Self) -> f64 {
1207 glam_assert!(axis.is_normalized());
1208 glam_assert!(self.is_non_zero());
1209 glam_assert!(rhs.is_non_zero());
1210 math::atan2(self.cross(rhs).dot(axis), self.dot(rhs))
1211 }
1212
1213 #[inline]
1215 #[must_use]
1216 pub fn rotate_x(self, angle: f64) -> Self {
1217 let (sina, cosa) = math::sin_cos(angle);
1218 Self::new(
1219 self.x,
1220 self.y * cosa - self.z * sina,
1221 self.y * sina + self.z * cosa,
1222 )
1223 }
1224
1225 #[inline]
1227 #[must_use]
1228 pub fn rotate_y(self, angle: f64) -> Self {
1229 let (sina, cosa) = math::sin_cos(angle);
1230 Self::new(
1231 self.x * cosa + self.z * sina,
1232 self.y,
1233 self.x * -sina + self.z * cosa,
1234 )
1235 }
1236
1237 #[inline]
1239 #[must_use]
1240 pub fn rotate_z(self, angle: f64) -> Self {
1241 let (sina, cosa) = math::sin_cos(angle);
1242 Self::new(
1243 self.x * cosa - self.y * sina,
1244 self.x * sina + self.y * cosa,
1245 self.z,
1246 )
1247 }
1248
1249 #[inline]
1257 #[must_use]
1258 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1259 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1260 DQuat::from_axis_angle(axis, angle) * self
1261 }
1262
1263 #[inline]
1273 #[must_use]
1274 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1275 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1276 let angle_between = self.angle_between(rhs);
1277 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1279 let axis = self
1280 .cross(rhs)
1281 .try_normalize()
1282 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1283 DQuat::from_axis_angle(axis, angle) * self
1284 }
1285
1286 #[inline]
1293 #[must_use]
1294 pub fn any_orthogonal_vector(self) -> Self {
1295 if self.x.abs() > self.y.abs() {
1297 Self::new(-self.z, 0.0, self.x) } else {
1299 Self::new(0.0, self.z, -self.y) }
1301 }
1302
1303 #[inline]
1311 #[must_use]
1312 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1313 pub fn any_orthonormal_vector(self) -> Self {
1314 glam_assert!(self.is_normalized());
1315 let sign = self.z.signum();
1317 let a = -1.0 / (sign + self.z);
1318 let b = self.x * self.y * a;
1319 Self::new(b, sign + self.y * self.y * a, -self.y)
1320 }
1321
1322 #[inline]
1329 #[must_use]
1330 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1331 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1332 glam_assert!(self.is_normalized());
1333 let sign = self.z.signum();
1335 let a = -1.0 / (sign + self.z);
1336 let b = self.x * self.y * a;
1337 (
1338 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1339 Self::new(b, sign + self.y * self.y * a, -self.y),
1340 )
1341 }
1342
1343 #[inline]
1353 #[must_use]
1354 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1355 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1356 glam_assert!(self.is_non_zero());
1357 glam_assert!(rhs.is_non_zero());
1358 let self_length = self.length();
1359 let rhs_length = rhs.length();
1360 let dot = self.dot(rhs) / (self_length * rhs_length);
1362 if dot.abs() < 1.0 - 3e-7 {
1364 let theta = math::acos_approx(dot);
1366 let sin_theta = math::sin(theta);
1368 let t1 = math::sin(theta * (1.0 - s));
1369 let t2 = math::sin(theta * s);
1370
1371 let result_length = self_length.lerp_monotonic(rhs_length, s);
1373 return (self * (result_length / self_length) * t1
1375 + rhs * (result_length / rhs_length) * t2)
1376 * (1.0 / sin_theta);
1377 }
1378 if dot < 0.0 {
1379 let axis = self.any_orthogonal_vector().normalize();
1383 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1384 let result_length = self_length.lerp_monotonic(rhs_length, s);
1386 rotation * self * (result_length / self_length)
1387 } else {
1388 self.lerp(rhs, s)
1390 }
1391 }
1392
1393 #[inline]
1395 #[must_use]
1396 pub fn as_vec3(self) -> crate::Vec3 {
1397 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1398 }
1399
1400 #[inline]
1402 #[must_use]
1403 pub fn as_vec3a(self) -> crate::Vec3A {
1404 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1405 }
1406
1407 #[cfg(feature = "i8")]
1409 #[inline]
1410 #[must_use]
1411 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1412 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1413 }
1414
1415 #[cfg(feature = "u8")]
1417 #[inline]
1418 #[must_use]
1419 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1420 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1421 }
1422
1423 #[cfg(feature = "i16")]
1425 #[inline]
1426 #[must_use]
1427 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1428 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1429 }
1430
1431 #[cfg(feature = "u16")]
1433 #[inline]
1434 #[must_use]
1435 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1436 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1437 }
1438
1439 #[cfg(feature = "i32")]
1441 #[inline]
1442 #[must_use]
1443 pub fn as_ivec3(self) -> crate::IVec3 {
1444 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1445 }
1446
1447 #[cfg(feature = "u32")]
1449 #[inline]
1450 #[must_use]
1451 pub fn as_uvec3(self) -> crate::UVec3 {
1452 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1453 }
1454
1455 #[cfg(feature = "i64")]
1457 #[inline]
1458 #[must_use]
1459 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1460 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1461 }
1462
1463 #[cfg(feature = "u64")]
1465 #[inline]
1466 #[must_use]
1467 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1468 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1469 }
1470
1471 #[cfg(feature = "isize")]
1473 #[inline]
1474 #[must_use]
1475 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1476 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1477 }
1478
1479 #[cfg(feature = "usize")]
1481 #[inline]
1482 #[must_use]
1483 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1484 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1485 }
1486}
1487
1488impl Default for DVec3 {
1489 #[inline(always)]
1490 fn default() -> Self {
1491 Self::ZERO
1492 }
1493}
1494
1495impl Div for DVec3 {
1496 type Output = Self;
1497 #[inline]
1498 fn div(self, rhs: Self) -> Self {
1499 Self::new(self.x.div(rhs.x), self.y.div(rhs.y), self.z.div(rhs.z))
1500 }
1501}
1502
1503impl Div<&Self> for DVec3 {
1504 type Output = Self;
1505 #[inline]
1506 fn div(self, rhs: &Self) -> Self {
1507 self.div(*rhs)
1508 }
1509}
1510
1511impl Div<&DVec3> for &DVec3 {
1512 type Output = DVec3;
1513 #[inline]
1514 fn div(self, rhs: &DVec3) -> DVec3 {
1515 (*self).div(*rhs)
1516 }
1517}
1518
1519impl Div<DVec3> for &DVec3 {
1520 type Output = DVec3;
1521 #[inline]
1522 fn div(self, rhs: DVec3) -> DVec3 {
1523 (*self).div(rhs)
1524 }
1525}
1526
1527impl DivAssign for DVec3 {
1528 #[inline]
1529 fn div_assign(&mut self, rhs: Self) {
1530 self.x.div_assign(rhs.x);
1531 self.y.div_assign(rhs.y);
1532 self.z.div_assign(rhs.z);
1533 }
1534}
1535
1536impl DivAssign<&Self> for DVec3 {
1537 #[inline]
1538 fn div_assign(&mut self, rhs: &Self) {
1539 self.div_assign(*rhs);
1540 }
1541}
1542
1543impl Div<f64> for DVec3 {
1544 type Output = Self;
1545 #[inline]
1546 fn div(self, rhs: f64) -> Self {
1547 Self::new(self.x.div(rhs), self.y.div(rhs), self.z.div(rhs))
1548 }
1549}
1550
1551impl Div<&f64> for DVec3 {
1552 type Output = Self;
1553 #[inline]
1554 fn div(self, rhs: &f64) -> Self {
1555 self.div(*rhs)
1556 }
1557}
1558
1559impl Div<&f64> for &DVec3 {
1560 type Output = DVec3;
1561 #[inline]
1562 fn div(self, rhs: &f64) -> DVec3 {
1563 (*self).div(*rhs)
1564 }
1565}
1566
1567impl Div<f64> for &DVec3 {
1568 type Output = DVec3;
1569 #[inline]
1570 fn div(self, rhs: f64) -> DVec3 {
1571 (*self).div(rhs)
1572 }
1573}
1574
1575impl DivAssign<f64> for DVec3 {
1576 #[inline]
1577 fn div_assign(&mut self, rhs: f64) {
1578 self.x.div_assign(rhs);
1579 self.y.div_assign(rhs);
1580 self.z.div_assign(rhs);
1581 }
1582}
1583
1584impl DivAssign<&f64> for DVec3 {
1585 #[inline]
1586 fn div_assign(&mut self, rhs: &f64) {
1587 self.div_assign(*rhs);
1588 }
1589}
1590
1591impl Div<DVec3> for f64 {
1592 type Output = DVec3;
1593 #[inline]
1594 fn div(self, rhs: DVec3) -> DVec3 {
1595 DVec3::new(self.div(rhs.x), self.div(rhs.y), self.div(rhs.z))
1596 }
1597}
1598
1599impl Div<&DVec3> for f64 {
1600 type Output = DVec3;
1601 #[inline]
1602 fn div(self, rhs: &DVec3) -> DVec3 {
1603 self.div(*rhs)
1604 }
1605}
1606
1607impl Div<&DVec3> for &f64 {
1608 type Output = DVec3;
1609 #[inline]
1610 fn div(self, rhs: &DVec3) -> DVec3 {
1611 (*self).div(*rhs)
1612 }
1613}
1614
1615impl Div<DVec3> for &f64 {
1616 type Output = DVec3;
1617 #[inline]
1618 fn div(self, rhs: DVec3) -> DVec3 {
1619 (*self).div(rhs)
1620 }
1621}
1622
1623impl Mul for DVec3 {
1624 type Output = Self;
1625 #[inline]
1626 fn mul(self, rhs: Self) -> Self {
1627 Self::new(self.x.mul(rhs.x), self.y.mul(rhs.y), self.z.mul(rhs.z))
1628 }
1629}
1630
1631impl Mul<&Self> for DVec3 {
1632 type Output = Self;
1633 #[inline]
1634 fn mul(self, rhs: &Self) -> Self {
1635 self.mul(*rhs)
1636 }
1637}
1638
1639impl Mul<&DVec3> for &DVec3 {
1640 type Output = DVec3;
1641 #[inline]
1642 fn mul(self, rhs: &DVec3) -> DVec3 {
1643 (*self).mul(*rhs)
1644 }
1645}
1646
1647impl Mul<DVec3> for &DVec3 {
1648 type Output = DVec3;
1649 #[inline]
1650 fn mul(self, rhs: DVec3) -> DVec3 {
1651 (*self).mul(rhs)
1652 }
1653}
1654
1655impl MulAssign for DVec3 {
1656 #[inline]
1657 fn mul_assign(&mut self, rhs: Self) {
1658 self.x.mul_assign(rhs.x);
1659 self.y.mul_assign(rhs.y);
1660 self.z.mul_assign(rhs.z);
1661 }
1662}
1663
1664impl MulAssign<&Self> for DVec3 {
1665 #[inline]
1666 fn mul_assign(&mut self, rhs: &Self) {
1667 self.mul_assign(*rhs);
1668 }
1669}
1670
1671impl Mul<f64> for DVec3 {
1672 type Output = Self;
1673 #[inline]
1674 fn mul(self, rhs: f64) -> Self {
1675 Self::new(self.x.mul(rhs), self.y.mul(rhs), self.z.mul(rhs))
1676 }
1677}
1678
1679impl Mul<&f64> for DVec3 {
1680 type Output = Self;
1681 #[inline]
1682 fn mul(self, rhs: &f64) -> Self {
1683 self.mul(*rhs)
1684 }
1685}
1686
1687impl Mul<&f64> for &DVec3 {
1688 type Output = DVec3;
1689 #[inline]
1690 fn mul(self, rhs: &f64) -> DVec3 {
1691 (*self).mul(*rhs)
1692 }
1693}
1694
1695impl Mul<f64> for &DVec3 {
1696 type Output = DVec3;
1697 #[inline]
1698 fn mul(self, rhs: f64) -> DVec3 {
1699 (*self).mul(rhs)
1700 }
1701}
1702
1703impl MulAssign<f64> for DVec3 {
1704 #[inline]
1705 fn mul_assign(&mut self, rhs: f64) {
1706 self.x.mul_assign(rhs);
1707 self.y.mul_assign(rhs);
1708 self.z.mul_assign(rhs);
1709 }
1710}
1711
1712impl MulAssign<&f64> for DVec3 {
1713 #[inline]
1714 fn mul_assign(&mut self, rhs: &f64) {
1715 self.mul_assign(*rhs);
1716 }
1717}
1718
1719impl Mul<DVec3> for f64 {
1720 type Output = DVec3;
1721 #[inline]
1722 fn mul(self, rhs: DVec3) -> DVec3 {
1723 DVec3::new(self.mul(rhs.x), self.mul(rhs.y), self.mul(rhs.z))
1724 }
1725}
1726
1727impl Mul<&DVec3> for f64 {
1728 type Output = DVec3;
1729 #[inline]
1730 fn mul(self, rhs: &DVec3) -> DVec3 {
1731 self.mul(*rhs)
1732 }
1733}
1734
1735impl Mul<&DVec3> for &f64 {
1736 type Output = DVec3;
1737 #[inline]
1738 fn mul(self, rhs: &DVec3) -> DVec3 {
1739 (*self).mul(*rhs)
1740 }
1741}
1742
1743impl Mul<DVec3> for &f64 {
1744 type Output = DVec3;
1745 #[inline]
1746 fn mul(self, rhs: DVec3) -> DVec3 {
1747 (*self).mul(rhs)
1748 }
1749}
1750
1751impl Add for DVec3 {
1752 type Output = Self;
1753 #[inline]
1754 fn add(self, rhs: Self) -> Self {
1755 Self::new(self.x.add(rhs.x), self.y.add(rhs.y), self.z.add(rhs.z))
1756 }
1757}
1758
1759impl Add<&Self> for DVec3 {
1760 type Output = Self;
1761 #[inline]
1762 fn add(self, rhs: &Self) -> Self {
1763 self.add(*rhs)
1764 }
1765}
1766
1767impl Add<&DVec3> for &DVec3 {
1768 type Output = DVec3;
1769 #[inline]
1770 fn add(self, rhs: &DVec3) -> DVec3 {
1771 (*self).add(*rhs)
1772 }
1773}
1774
1775impl Add<DVec3> for &DVec3 {
1776 type Output = DVec3;
1777 #[inline]
1778 fn add(self, rhs: DVec3) -> DVec3 {
1779 (*self).add(rhs)
1780 }
1781}
1782
1783impl AddAssign for DVec3 {
1784 #[inline]
1785 fn add_assign(&mut self, rhs: Self) {
1786 self.x.add_assign(rhs.x);
1787 self.y.add_assign(rhs.y);
1788 self.z.add_assign(rhs.z);
1789 }
1790}
1791
1792impl AddAssign<&Self> for DVec3 {
1793 #[inline]
1794 fn add_assign(&mut self, rhs: &Self) {
1795 self.add_assign(*rhs);
1796 }
1797}
1798
1799impl Add<f64> for DVec3 {
1800 type Output = Self;
1801 #[inline]
1802 fn add(self, rhs: f64) -> Self {
1803 Self::new(self.x.add(rhs), self.y.add(rhs), self.z.add(rhs))
1804 }
1805}
1806
1807impl Add<&f64> for DVec3 {
1808 type Output = Self;
1809 #[inline]
1810 fn add(self, rhs: &f64) -> Self {
1811 self.add(*rhs)
1812 }
1813}
1814
1815impl Add<&f64> for &DVec3 {
1816 type Output = DVec3;
1817 #[inline]
1818 fn add(self, rhs: &f64) -> DVec3 {
1819 (*self).add(*rhs)
1820 }
1821}
1822
1823impl Add<f64> for &DVec3 {
1824 type Output = DVec3;
1825 #[inline]
1826 fn add(self, rhs: f64) -> DVec3 {
1827 (*self).add(rhs)
1828 }
1829}
1830
1831impl AddAssign<f64> for DVec3 {
1832 #[inline]
1833 fn add_assign(&mut self, rhs: f64) {
1834 self.x.add_assign(rhs);
1835 self.y.add_assign(rhs);
1836 self.z.add_assign(rhs);
1837 }
1838}
1839
1840impl AddAssign<&f64> for DVec3 {
1841 #[inline]
1842 fn add_assign(&mut self, rhs: &f64) {
1843 self.add_assign(*rhs);
1844 }
1845}
1846
1847impl Add<DVec3> for f64 {
1848 type Output = DVec3;
1849 #[inline]
1850 fn add(self, rhs: DVec3) -> DVec3 {
1851 DVec3::new(self.add(rhs.x), self.add(rhs.y), self.add(rhs.z))
1852 }
1853}
1854
1855impl Add<&DVec3> for f64 {
1856 type Output = DVec3;
1857 #[inline]
1858 fn add(self, rhs: &DVec3) -> DVec3 {
1859 self.add(*rhs)
1860 }
1861}
1862
1863impl Add<&DVec3> for &f64 {
1864 type Output = DVec3;
1865 #[inline]
1866 fn add(self, rhs: &DVec3) -> DVec3 {
1867 (*self).add(*rhs)
1868 }
1869}
1870
1871impl Add<DVec3> for &f64 {
1872 type Output = DVec3;
1873 #[inline]
1874 fn add(self, rhs: DVec3) -> DVec3 {
1875 (*self).add(rhs)
1876 }
1877}
1878
1879impl Sub for DVec3 {
1880 type Output = Self;
1881 #[inline]
1882 fn sub(self, rhs: Self) -> Self {
1883 Self::new(self.x.sub(rhs.x), self.y.sub(rhs.y), self.z.sub(rhs.z))
1884 }
1885}
1886
1887impl Sub<&Self> for DVec3 {
1888 type Output = Self;
1889 #[inline]
1890 fn sub(self, rhs: &Self) -> Self {
1891 self.sub(*rhs)
1892 }
1893}
1894
1895impl Sub<&DVec3> for &DVec3 {
1896 type Output = DVec3;
1897 #[inline]
1898 fn sub(self, rhs: &DVec3) -> DVec3 {
1899 (*self).sub(*rhs)
1900 }
1901}
1902
1903impl Sub<DVec3> for &DVec3 {
1904 type Output = DVec3;
1905 #[inline]
1906 fn sub(self, rhs: DVec3) -> DVec3 {
1907 (*self).sub(rhs)
1908 }
1909}
1910
1911impl SubAssign for DVec3 {
1912 #[inline]
1913 fn sub_assign(&mut self, rhs: Self) {
1914 self.x.sub_assign(rhs.x);
1915 self.y.sub_assign(rhs.y);
1916 self.z.sub_assign(rhs.z);
1917 }
1918}
1919
1920impl SubAssign<&Self> for DVec3 {
1921 #[inline]
1922 fn sub_assign(&mut self, rhs: &Self) {
1923 self.sub_assign(*rhs);
1924 }
1925}
1926
1927impl Sub<f64> for DVec3 {
1928 type Output = Self;
1929 #[inline]
1930 fn sub(self, rhs: f64) -> Self {
1931 Self::new(self.x.sub(rhs), self.y.sub(rhs), self.z.sub(rhs))
1932 }
1933}
1934
1935impl Sub<&f64> for DVec3 {
1936 type Output = Self;
1937 #[inline]
1938 fn sub(self, rhs: &f64) -> Self {
1939 self.sub(*rhs)
1940 }
1941}
1942
1943impl Sub<&f64> for &DVec3 {
1944 type Output = DVec3;
1945 #[inline]
1946 fn sub(self, rhs: &f64) -> DVec3 {
1947 (*self).sub(*rhs)
1948 }
1949}
1950
1951impl Sub<f64> for &DVec3 {
1952 type Output = DVec3;
1953 #[inline]
1954 fn sub(self, rhs: f64) -> DVec3 {
1955 (*self).sub(rhs)
1956 }
1957}
1958
1959impl SubAssign<f64> for DVec3 {
1960 #[inline]
1961 fn sub_assign(&mut self, rhs: f64) {
1962 self.x.sub_assign(rhs);
1963 self.y.sub_assign(rhs);
1964 self.z.sub_assign(rhs);
1965 }
1966}
1967
1968impl SubAssign<&f64> for DVec3 {
1969 #[inline]
1970 fn sub_assign(&mut self, rhs: &f64) {
1971 self.sub_assign(*rhs);
1972 }
1973}
1974
1975impl Sub<DVec3> for f64 {
1976 type Output = DVec3;
1977 #[inline]
1978 fn sub(self, rhs: DVec3) -> DVec3 {
1979 DVec3::new(self.sub(rhs.x), self.sub(rhs.y), self.sub(rhs.z))
1980 }
1981}
1982
1983impl Sub<&DVec3> for f64 {
1984 type Output = DVec3;
1985 #[inline]
1986 fn sub(self, rhs: &DVec3) -> DVec3 {
1987 self.sub(*rhs)
1988 }
1989}
1990
1991impl Sub<&DVec3> for &f64 {
1992 type Output = DVec3;
1993 #[inline]
1994 fn sub(self, rhs: &DVec3) -> DVec3 {
1995 (*self).sub(*rhs)
1996 }
1997}
1998
1999impl Sub<DVec3> for &f64 {
2000 type Output = DVec3;
2001 #[inline]
2002 fn sub(self, rhs: DVec3) -> DVec3 {
2003 (*self).sub(rhs)
2004 }
2005}
2006
2007impl Rem for DVec3 {
2008 type Output = Self;
2009 #[inline]
2010 fn rem(self, rhs: Self) -> Self {
2011 Self::new(self.x.rem(rhs.x), self.y.rem(rhs.y), self.z.rem(rhs.z))
2012 }
2013}
2014
2015impl Rem<&Self> for DVec3 {
2016 type Output = Self;
2017 #[inline]
2018 fn rem(self, rhs: &Self) -> Self {
2019 self.rem(*rhs)
2020 }
2021}
2022
2023impl Rem<&DVec3> for &DVec3 {
2024 type Output = DVec3;
2025 #[inline]
2026 fn rem(self, rhs: &DVec3) -> DVec3 {
2027 (*self).rem(*rhs)
2028 }
2029}
2030
2031impl Rem<DVec3> for &DVec3 {
2032 type Output = DVec3;
2033 #[inline]
2034 fn rem(self, rhs: DVec3) -> DVec3 {
2035 (*self).rem(rhs)
2036 }
2037}
2038
2039impl RemAssign for DVec3 {
2040 #[inline]
2041 fn rem_assign(&mut self, rhs: Self) {
2042 self.x.rem_assign(rhs.x);
2043 self.y.rem_assign(rhs.y);
2044 self.z.rem_assign(rhs.z);
2045 }
2046}
2047
2048impl RemAssign<&Self> for DVec3 {
2049 #[inline]
2050 fn rem_assign(&mut self, rhs: &Self) {
2051 self.rem_assign(*rhs);
2052 }
2053}
2054
2055impl Rem<f64> for DVec3 {
2056 type Output = Self;
2057 #[inline]
2058 fn rem(self, rhs: f64) -> Self {
2059 Self::new(self.x.rem(rhs), self.y.rem(rhs), self.z.rem(rhs))
2060 }
2061}
2062
2063impl Rem<&f64> for DVec3 {
2064 type Output = Self;
2065 #[inline]
2066 fn rem(self, rhs: &f64) -> Self {
2067 self.rem(*rhs)
2068 }
2069}
2070
2071impl Rem<&f64> for &DVec3 {
2072 type Output = DVec3;
2073 #[inline]
2074 fn rem(self, rhs: &f64) -> DVec3 {
2075 (*self).rem(*rhs)
2076 }
2077}
2078
2079impl Rem<f64> for &DVec3 {
2080 type Output = DVec3;
2081 #[inline]
2082 fn rem(self, rhs: f64) -> DVec3 {
2083 (*self).rem(rhs)
2084 }
2085}
2086
2087impl RemAssign<f64> for DVec3 {
2088 #[inline]
2089 fn rem_assign(&mut self, rhs: f64) {
2090 self.x.rem_assign(rhs);
2091 self.y.rem_assign(rhs);
2092 self.z.rem_assign(rhs);
2093 }
2094}
2095
2096impl RemAssign<&f64> for DVec3 {
2097 #[inline]
2098 fn rem_assign(&mut self, rhs: &f64) {
2099 self.rem_assign(*rhs);
2100 }
2101}
2102
2103impl Rem<DVec3> for f64 {
2104 type Output = DVec3;
2105 #[inline]
2106 fn rem(self, rhs: DVec3) -> DVec3 {
2107 DVec3::new(self.rem(rhs.x), self.rem(rhs.y), self.rem(rhs.z))
2108 }
2109}
2110
2111impl Rem<&DVec3> for f64 {
2112 type Output = DVec3;
2113 #[inline]
2114 fn rem(self, rhs: &DVec3) -> DVec3 {
2115 self.rem(*rhs)
2116 }
2117}
2118
2119impl Rem<&DVec3> for &f64 {
2120 type Output = DVec3;
2121 #[inline]
2122 fn rem(self, rhs: &DVec3) -> DVec3 {
2123 (*self).rem(*rhs)
2124 }
2125}
2126
2127impl Rem<DVec3> for &f64 {
2128 type Output = DVec3;
2129 #[inline]
2130 fn rem(self, rhs: DVec3) -> DVec3 {
2131 (*self).rem(rhs)
2132 }
2133}
2134
2135impl AsRef<[f64; 3]> for DVec3 {
2136 #[inline]
2137 fn as_ref(&self) -> &[f64; 3] {
2138 unsafe { &*(self as *const Self as *const [f64; 3]) }
2139 }
2140}
2141
2142impl AsMut<[f64; 3]> for DVec3 {
2143 #[inline]
2144 fn as_mut(&mut self) -> &mut [f64; 3] {
2145 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2146 }
2147}
2148
2149impl Sum for DVec3 {
2150 #[inline]
2151 fn sum<I>(iter: I) -> Self
2152 where
2153 I: Iterator<Item = Self>,
2154 {
2155 iter.fold(Self::ZERO, Self::add)
2156 }
2157}
2158
2159impl<'a> Sum<&'a Self> for DVec3 {
2160 #[inline]
2161 fn sum<I>(iter: I) -> Self
2162 where
2163 I: Iterator<Item = &'a Self>,
2164 {
2165 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2166 }
2167}
2168
2169impl Product for DVec3 {
2170 #[inline]
2171 fn product<I>(iter: I) -> Self
2172 where
2173 I: Iterator<Item = Self>,
2174 {
2175 iter.fold(Self::ONE, Self::mul)
2176 }
2177}
2178
2179impl<'a> Product<&'a Self> for DVec3 {
2180 #[inline]
2181 fn product<I>(iter: I) -> Self
2182 where
2183 I: Iterator<Item = &'a Self>,
2184 {
2185 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2186 }
2187}
2188
2189impl Neg for DVec3 {
2190 type Output = Self;
2191 #[inline]
2192 fn neg(self) -> Self {
2193 Self::new(self.x.neg(), self.y.neg(), self.z.neg())
2194 }
2195}
2196
2197impl Neg for &DVec3 {
2198 type Output = DVec3;
2199 #[inline]
2200 fn neg(self) -> DVec3 {
2201 (*self).neg()
2202 }
2203}
2204
2205impl Index<usize> for DVec3 {
2206 type Output = f64;
2207 #[inline]
2208 #[track_caller]
2209 fn index(&self, index: usize) -> &Self::Output {
2210 match index {
2211 0 => &self.x,
2212 1 => &self.y,
2213 2 => &self.z,
2214 _ => panic!("index out of bounds"),
2215 }
2216 }
2217}
2218
2219impl IndexMut<usize> for DVec3 {
2220 #[inline]
2221 #[track_caller]
2222 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2223 match index {
2224 0 => &mut self.x,
2225 1 => &mut self.y,
2226 2 => &mut self.z,
2227 _ => panic!("index out of bounds"),
2228 }
2229 }
2230}
2231
2232impl fmt::Display for DVec3 {
2233 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2234 if let Some(p) = f.precision() {
2235 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2236 } else {
2237 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2238 }
2239 }
2240}
2241
2242impl fmt::Debug for DVec3 {
2243 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2244 fmt.debug_tuple(stringify!(DVec3))
2245 .field(&self.x)
2246 .field(&self.y)
2247 .field(&self.z)
2248 .finish()
2249 }
2250}
2251
2252impl From<[f64; 3]> for DVec3 {
2253 #[inline]
2254 fn from(a: [f64; 3]) -> Self {
2255 Self::new(a[0], a[1], a[2])
2256 }
2257}
2258
2259impl From<DVec3> for [f64; 3] {
2260 #[inline]
2261 fn from(v: DVec3) -> Self {
2262 [v.x, v.y, v.z]
2263 }
2264}
2265
2266impl From<(f64, f64, f64)> for DVec3 {
2267 #[inline]
2268 fn from(t: (f64, f64, f64)) -> Self {
2269 Self::new(t.0, t.1, t.2)
2270 }
2271}
2272
2273impl From<DVec3> for (f64, f64, f64) {
2274 #[inline]
2275 fn from(v: DVec3) -> Self {
2276 (v.x, v.y, v.z)
2277 }
2278}
2279
2280impl From<(DVec2, f64)> for DVec3 {
2281 #[inline]
2282 fn from((v, z): (DVec2, f64)) -> Self {
2283 Self::new(v.x, v.y, z)
2284 }
2285}
2286
2287impl From<Vec3> for DVec3 {
2288 #[inline]
2289 fn from(v: Vec3) -> Self {
2290 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2291 }
2292}
2293
2294#[cfg(feature = "i32")]
2295impl From<IVec3> for DVec3 {
2296 #[inline]
2297 fn from(v: IVec3) -> Self {
2298 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2299 }
2300}
2301
2302#[cfg(feature = "u32")]
2303impl From<UVec3> for DVec3 {
2304 #[inline]
2305 fn from(v: UVec3) -> Self {
2306 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2307 }
2308}
2309
2310impl From<BVec3> for DVec3 {
2311 #[inline]
2312 fn from(v: BVec3) -> Self {
2313 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2314 }
2315}
2316
2317impl From<BVec3A> for DVec3 {
2318 #[inline]
2319 fn from(v: BVec3A) -> Self {
2320 let bool_array: [bool; 3] = v.into();
2321 Self::new(
2322 f64::from(bool_array[0]),
2323 f64::from(bool_array[1]),
2324 f64::from(bool_array[2]),
2325 )
2326 }
2327}