1use crate::{f64::math, BVec3, BVec3A, DQuat, DVec2, DVec4, FloatExt};
4
5use crate::Vec3;
6
7#[cfg(feature = "i32")]
8use crate::IVec3;
9
10#[cfg(feature = "u32")]
11use crate::UVec3;
12
13use core::fmt;
14use core::iter::{Product, Sum};
15use core::{f32, ops::*};
16
17#[cfg(feature = "zerocopy")]
18use zerocopy_derive::*;
19
20#[inline(always)]
22#[must_use]
23pub const fn dvec3(x: f64, y: f64, z: f64) -> DVec3 {
24 DVec3::new(x, y, z)
25}
26
27#[derive(Clone, Copy, PartialEq)]
29#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
30#[cfg_attr(
31 feature = "zerocopy",
32 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
33)]
34#[repr(C)]
35#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
36pub struct DVec3 {
37 pub x: f64,
38 pub y: f64,
39 pub z: f64,
40}
41
42impl DVec3 {
43 pub const ZERO: Self = Self::splat(0.0);
45
46 pub const ONE: Self = Self::splat(1.0);
48
49 pub const NEG_ONE: Self = Self::splat(-1.0);
51
52 pub const MIN: Self = Self::splat(f64::MIN);
54
55 pub const MAX: Self = Self::splat(f64::MAX);
57
58 pub const NAN: Self = Self::splat(f64::NAN);
60
61 pub const INFINITY: Self = Self::splat(f64::INFINITY);
63
64 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
66
67 pub const X: Self = Self::new(1.0, 0.0, 0.0);
69
70 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
72
73 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
75
76 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
78
79 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
81
82 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
84
85 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
87
88 pub const USES_CORE_SIMD: bool = false;
90 pub const USES_NEON: bool = false;
92 pub const USES_SCALAR_MATH: bool = true;
94 pub const USES_SSE2: bool = false;
96 pub const USES_WASM_SIMD: bool = false;
98 #[deprecated(since = "0.31.0", note = "Renamed to USES_WASM_SIMD")]
99 pub const USES_WASM32_SIMD: bool = false;
100
101 #[inline(always)]
103 #[must_use]
104 pub const fn new(x: f64, y: f64, z: f64) -> Self {
105 Self { x, y, z }
106 }
107
108 #[inline]
110 #[must_use]
111 pub const fn splat(v: f64) -> Self {
112 Self { x: v, y: v, z: v }
113 }
114
115 #[inline]
117 #[must_use]
118 pub fn map<F>(self, mut f: F) -> Self
119 where
120 F: FnMut(f64) -> f64,
121 {
122 Self::new(f(self.x), f(self.y), f(self.z))
123 }
124
125 #[inline]
131 #[must_use]
132 pub fn select(mask: BVec3, if_true: Self, if_false: Self) -> Self {
133 Self {
134 x: if mask.test(0) { if_true.x } else { if_false.x },
135 y: if mask.test(1) { if_true.y } else { if_false.y },
136 z: if mask.test(2) { if_true.z } else { if_false.z },
137 }
138 }
139
140 #[inline]
142 #[must_use]
143 pub const fn from_array(a: [f64; 3]) -> Self {
144 Self::new(a[0], a[1], a[2])
145 }
146
147 #[inline]
149 #[must_use]
150 pub const fn to_array(&self) -> [f64; 3] {
151 [self.x, self.y, self.z]
152 }
153
154 #[inline]
160 #[must_use]
161 pub const fn from_slice(slice: &[f64]) -> Self {
162 assert!(slice.len() >= 3);
163 Self::new(slice[0], slice[1], slice[2])
164 }
165
166 #[inline]
172 pub fn write_to_slice(self, slice: &mut [f64]) {
173 slice[..3].copy_from_slice(&self.to_array());
174 }
175
176 #[allow(dead_code)]
178 #[inline]
179 #[must_use]
180 pub(crate) fn from_vec4(v: DVec4) -> Self {
181 Self {
182 x: v.x,
183 y: v.y,
184 z: v.z,
185 }
186 }
187
188 #[inline]
190 #[must_use]
191 pub fn extend(self, w: f64) -> DVec4 {
192 DVec4::new(self.x, self.y, self.z, w)
193 }
194
195 #[inline]
199 #[must_use]
200 pub fn truncate(self) -> DVec2 {
201 use crate::swizzles::Vec3Swizzles;
202 self.xy()
203 }
204
205 #[inline]
211 #[must_use]
212 pub fn from_homogeneous(v: DVec4) -> Self {
213 glam_assert!(v.w != 0.0);
214 Self::from_vec4(v) / v.w
215 }
216
217 #[inline]
219 #[must_use]
220 pub fn to_homogeneous(self) -> DVec4 {
221 self.extend(1.0)
222 }
223
224 #[inline]
226 #[must_use]
227 pub fn with_x(mut self, x: f64) -> Self {
228 self.x = x;
229 self
230 }
231
232 #[inline]
234 #[must_use]
235 pub fn with_y(mut self, y: f64) -> Self {
236 self.y = y;
237 self
238 }
239
240 #[inline]
242 #[must_use]
243 pub fn with_z(mut self, z: f64) -> Self {
244 self.z = z;
245 self
246 }
247
248 #[inline]
250 #[must_use]
251 pub fn dot(self, rhs: Self) -> f64 {
252 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z)
253 }
254
255 #[inline]
257 #[must_use]
258 pub fn dot_into_vec(self, rhs: Self) -> Self {
259 Self::splat(self.dot(rhs))
260 }
261
262 #[inline]
264 #[must_use]
265 pub fn cross(self, rhs: Self) -> Self {
266 Self {
267 x: self.y * rhs.z - rhs.y * self.z,
268 y: self.z * rhs.x - rhs.z * self.x,
269 z: self.x * rhs.y - rhs.x * self.y,
270 }
271 }
272
273 #[inline]
280 #[must_use]
281 pub fn min(self, rhs: Self) -> Self {
282 Self {
283 x: if self.x < rhs.x { self.x } else { rhs.x },
284 y: if self.y < rhs.y { self.y } else { rhs.y },
285 z: if self.z < rhs.z { self.z } else { rhs.z },
286 }
287 }
288
289 #[inline]
296 #[must_use]
297 pub fn max(self, rhs: Self) -> Self {
298 Self {
299 x: if self.x > rhs.x { self.x } else { rhs.x },
300 y: if self.y > rhs.y { self.y } else { rhs.y },
301 z: if self.z > rhs.z { self.z } else { rhs.z },
302 }
303 }
304
305 #[inline]
316 #[must_use]
317 pub fn clamp(self, min: Self, max: Self) -> Self {
318 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
319 self.max(min).min(max)
320 }
321
322 #[inline]
329 #[must_use]
330 pub fn min_element(self) -> f64 {
331 let min = |a, b| if a < b { a } else { b };
332 min(self.x, min(self.y, self.z))
333 }
334
335 #[inline]
342 #[must_use]
343 pub fn max_element(self) -> f64 {
344 let max = |a, b| if a > b { a } else { b };
345 max(self.x, max(self.y, self.z))
346 }
347
348 #[doc(alias = "argmin")]
350 #[inline]
351 #[must_use]
352 pub fn min_position(self) -> usize {
353 let mut min = self.x;
354 let mut index = 0;
355 if self.y < min {
356 min = self.y;
357 index = 1;
358 }
359 if self.z < min {
360 index = 2;
361 }
362 index
363 }
364
365 #[doc(alias = "argmax")]
367 #[inline]
368 #[must_use]
369 pub fn max_position(self) -> usize {
370 let mut max = self.x;
371 let mut index = 0;
372 if self.y > max {
373 max = self.y;
374 index = 1;
375 }
376 if self.z > max {
377 index = 2;
378 }
379 index
380 }
381
382 #[inline]
386 #[must_use]
387 pub fn element_sum(self) -> f64 {
388 self.x + self.y + self.z
389 }
390
391 #[inline]
395 #[must_use]
396 pub fn element_product(self) -> f64 {
397 self.x * self.y * self.z
398 }
399
400 #[inline]
406 #[must_use]
407 pub fn cmpeq(self, rhs: Self) -> BVec3 {
408 BVec3::new(self.x.eq(&rhs.x), self.y.eq(&rhs.y), self.z.eq(&rhs.z))
409 }
410
411 #[inline]
417 #[must_use]
418 pub fn cmpne(self, rhs: Self) -> BVec3 {
419 BVec3::new(self.x.ne(&rhs.x), self.y.ne(&rhs.y), self.z.ne(&rhs.z))
420 }
421
422 #[inline]
428 #[must_use]
429 pub fn cmpge(self, rhs: Self) -> BVec3 {
430 BVec3::new(self.x.ge(&rhs.x), self.y.ge(&rhs.y), self.z.ge(&rhs.z))
431 }
432
433 #[inline]
439 #[must_use]
440 pub fn cmpgt(self, rhs: Self) -> BVec3 {
441 BVec3::new(self.x.gt(&rhs.x), self.y.gt(&rhs.y), self.z.gt(&rhs.z))
442 }
443
444 #[inline]
450 #[must_use]
451 pub fn cmple(self, rhs: Self) -> BVec3 {
452 BVec3::new(self.x.le(&rhs.x), self.y.le(&rhs.y), self.z.le(&rhs.z))
453 }
454
455 #[inline]
461 #[must_use]
462 pub fn cmplt(self, rhs: Self) -> BVec3 {
463 BVec3::new(self.x.lt(&rhs.x), self.y.lt(&rhs.y), self.z.lt(&rhs.z))
464 }
465
466 #[inline]
468 #[must_use]
469 pub fn abs(self) -> Self {
470 Self {
471 x: math::abs(self.x),
472 y: math::abs(self.y),
473 z: math::abs(self.z),
474 }
475 }
476
477 #[inline]
483 #[must_use]
484 pub fn signum(self) -> Self {
485 Self {
486 x: math::signum(self.x),
487 y: math::signum(self.y),
488 z: math::signum(self.z),
489 }
490 }
491
492 #[inline]
494 #[must_use]
495 pub fn copysign(self, rhs: Self) -> Self {
496 Self {
497 x: math::copysign(self.x, rhs.x),
498 y: math::copysign(self.y, rhs.y),
499 z: math::copysign(self.z, rhs.z),
500 }
501 }
502
503 #[inline]
511 #[must_use]
512 pub fn is_negative_bitmask(self) -> u32 {
513 (self.x.is_sign_negative() as u32)
514 | ((self.y.is_sign_negative() as u32) << 1)
515 | ((self.z.is_sign_negative() as u32) << 2)
516 }
517
518 #[inline]
521 #[must_use]
522 pub fn is_finite(self) -> bool {
523 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
524 }
525
526 #[inline]
530 #[must_use]
531 pub fn is_finite_mask(self) -> BVec3 {
532 BVec3::new(self.x.is_finite(), self.y.is_finite(), self.z.is_finite())
533 }
534
535 #[inline]
537 #[must_use]
538 pub fn is_nan(self) -> bool {
539 self.x.is_nan() || self.y.is_nan() || self.z.is_nan()
540 }
541
542 #[inline]
546 #[must_use]
547 pub fn is_nan_mask(self) -> BVec3 {
548 BVec3::new(self.x.is_nan(), self.y.is_nan(), self.z.is_nan())
549 }
550
551 #[doc(alias = "magnitude")]
553 #[inline]
554 #[must_use]
555 pub fn length(self) -> f64 {
556 math::sqrt(self.dot(self))
557 }
558
559 #[doc(alias = "magnitude2")]
563 #[inline]
564 #[must_use]
565 pub fn length_squared(self) -> f64 {
566 self.dot(self)
567 }
568
569 #[inline]
573 #[must_use]
574 pub fn length_recip(self) -> f64 {
575 self.length().recip()
576 }
577
578 #[inline]
580 #[must_use]
581 pub fn distance(self, rhs: Self) -> f64 {
582 (self - rhs).length()
583 }
584
585 #[inline]
587 #[must_use]
588 pub fn distance_squared(self, rhs: Self) -> f64 {
589 (self - rhs).length_squared()
590 }
591
592 #[inline]
594 #[must_use]
595 pub fn div_euclid(self, rhs: Self) -> Self {
596 Self::new(
597 math::div_euclid(self.x, rhs.x),
598 math::div_euclid(self.y, rhs.y),
599 math::div_euclid(self.z, rhs.z),
600 )
601 }
602
603 #[inline]
607 #[must_use]
608 pub fn rem_euclid(self, rhs: Self) -> Self {
609 Self::new(
610 math::rem_euclid(self.x, rhs.x),
611 math::rem_euclid(self.y, rhs.y),
612 math::rem_euclid(self.z, rhs.z),
613 )
614 }
615
616 #[inline]
626 #[must_use]
627 pub fn normalize(self) -> Self {
628 #[allow(clippy::let_and_return)]
629 let normalized = self.mul(self.length_recip());
630 glam_assert!(normalized.is_finite());
631 normalized
632 }
633
634 #[inline]
641 #[must_use]
642 pub fn try_normalize(self) -> Option<Self> {
643 let rcp = self.length_recip();
644 if rcp.is_finite() && rcp > 0.0 {
645 Some(self * rcp)
646 } else {
647 None
648 }
649 }
650
651 #[inline]
659 #[must_use]
660 pub fn normalize_or(self, fallback: Self) -> Self {
661 let rcp = self.length_recip();
662 if rcp.is_finite() && rcp > 0.0 {
663 self * rcp
664 } else {
665 fallback
666 }
667 }
668
669 #[inline]
676 #[must_use]
677 pub fn normalize_or_zero(self) -> Self {
678 self.normalize_or(Self::ZERO)
679 }
680
681 #[inline]
685 #[must_use]
686 pub fn normalize_and_length(self) -> (Self, f64) {
687 let length = self.length();
688 let rcp = 1.0 / length;
689 if rcp.is_finite() && rcp > 0.0 {
690 (self * rcp, length)
691 } else {
692 (Self::X, 0.0)
693 }
694 }
695
696 #[inline]
700 #[must_use]
701 pub fn is_normalized(self) -> bool {
702 math::abs(self.length_squared() - 1.0) <= 2e-4
703 }
704
705 #[inline]
713 #[must_use]
714 pub fn project_onto(self, rhs: Self) -> Self {
715 let other_len_sq_rcp = rhs.dot(rhs).recip();
716 glam_assert!(other_len_sq_rcp.is_finite());
717 rhs * self.dot(rhs) * other_len_sq_rcp
718 }
719
720 #[doc(alias("plane"))]
731 #[inline]
732 #[must_use]
733 pub fn reject_from(self, rhs: Self) -> Self {
734 self - self.project_onto(rhs)
735 }
736
737 #[inline]
745 #[must_use]
746 pub fn project_onto_normalized(self, rhs: Self) -> Self {
747 glam_assert!(rhs.is_normalized());
748 rhs * self.dot(rhs)
749 }
750
751 #[doc(alias("plane"))]
762 #[inline]
763 #[must_use]
764 pub fn reject_from_normalized(self, rhs: Self) -> Self {
765 self - self.project_onto_normalized(rhs)
766 }
767
768 #[inline]
771 #[must_use]
772 pub fn round(self) -> Self {
773 Self {
774 x: math::round(self.x),
775 y: math::round(self.y),
776 z: math::round(self.z),
777 }
778 }
779
780 #[inline]
783 #[must_use]
784 pub fn floor(self) -> Self {
785 Self {
786 x: math::floor(self.x),
787 y: math::floor(self.y),
788 z: math::floor(self.z),
789 }
790 }
791
792 #[inline]
795 #[must_use]
796 pub fn ceil(self) -> Self {
797 Self {
798 x: math::ceil(self.x),
799 y: math::ceil(self.y),
800 z: math::ceil(self.z),
801 }
802 }
803
804 #[inline]
807 #[must_use]
808 pub fn trunc(self) -> Self {
809 Self {
810 x: math::trunc(self.x),
811 y: math::trunc(self.y),
812 z: math::trunc(self.z),
813 }
814 }
815
816 #[inline]
820 #[must_use]
821 pub fn step(self, rhs: Self) -> Self {
822 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
823 }
824
825 #[inline]
827 #[must_use]
828 pub fn saturate(self) -> Self {
829 self.clamp(Self::ZERO, Self::ONE)
830 }
831
832 #[inline]
839 #[must_use]
840 pub fn fract(self) -> Self {
841 self - self.trunc()
842 }
843
844 #[inline]
851 #[must_use]
852 pub fn fract_gl(self) -> Self {
853 self - self.floor()
854 }
855
856 #[inline]
859 #[must_use]
860 pub fn exp(self) -> Self {
861 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
862 }
863
864 #[inline]
866 #[must_use]
867 pub fn exp2(self) -> Self {
868 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
869 }
870
871 #[inline]
874 #[must_use]
875 pub fn ln(self) -> Self {
876 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
877 }
878
879 #[inline]
882 #[must_use]
883 pub fn log2(self) -> Self {
884 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
885 }
886
887 #[inline]
889 #[must_use]
890 pub fn powf(self, n: f64) -> Self {
891 Self::new(
892 math::powf(self.x, n),
893 math::powf(self.y, n),
894 math::powf(self.z, n),
895 )
896 }
897
898 #[inline]
901 #[must_use]
902 pub fn sqrt(self) -> Self {
903 Self::new(math::sqrt(self.x), math::sqrt(self.y), math::sqrt(self.z))
904 }
905
906 #[inline]
908 #[must_use]
909 pub fn cos(self) -> Self {
910 Self::new(math::cos(self.x), math::cos(self.y), math::cos(self.z))
911 }
912
913 #[inline]
915 #[must_use]
916 pub fn sin(self) -> Self {
917 Self::new(math::sin(self.x), math::sin(self.y), math::sin(self.z))
918 }
919
920 #[inline]
922 #[must_use]
923 pub fn sin_cos(self) -> (Self, Self) {
924 let (sin_x, cos_x) = math::sin_cos(self.x);
925 let (sin_y, cos_y) = math::sin_cos(self.y);
926 let (sin_z, cos_z) = math::sin_cos(self.z);
927
928 (
929 Self::new(sin_x, sin_y, sin_z),
930 Self::new(cos_x, cos_y, cos_z),
931 )
932 }
933
934 #[inline]
936 #[must_use]
937 pub fn recip(self) -> Self {
938 Self {
939 x: 1.0 / self.x,
940 y: 1.0 / self.y,
941 z: 1.0 / self.z,
942 }
943 }
944
945 #[doc(alias = "mix")]
951 #[inline]
952 #[must_use]
953 pub fn lerp(self, rhs: Self, s: f64) -> Self {
954 self * (1.0 - s) + rhs * s
955 }
956
957 #[inline]
962 #[must_use]
963 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
964 let a = rhs - self;
965 let len = a.length();
966 if len <= d || len <= 1e-4 {
967 return rhs;
968 }
969 self + a / len * d
970 }
971
972 #[inline]
978 pub fn midpoint(self, rhs: Self) -> Self {
979 (self + rhs) * 0.5
980 }
981
982 #[inline]
992 #[must_use]
993 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
994 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
995 }
996
997 #[inline]
1003 #[must_use]
1004 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1005 glam_assert!(0.0 <= min);
1006 glam_assert!(min <= max);
1007 let length_sq = self.length_squared();
1008 if length_sq < min * min {
1009 min * (self / math::sqrt(length_sq))
1010 } else if length_sq > max * max {
1011 max * (self / math::sqrt(length_sq))
1012 } else {
1013 self
1014 }
1015 }
1016
1017 #[inline]
1023 #[must_use]
1024 pub fn clamp_length_max(self, max: f64) -> Self {
1025 glam_assert!(0.0 <= max);
1026 let length_sq = self.length_squared();
1027 if length_sq > max * max {
1028 max * (self / math::sqrt(length_sq))
1029 } else {
1030 self
1031 }
1032 }
1033
1034 #[inline]
1040 #[must_use]
1041 pub fn clamp_length_min(self, min: f64) -> Self {
1042 glam_assert!(0.0 <= min);
1043 let length_sq = self.length_squared();
1044 if length_sq < min * min {
1045 min * (self / math::sqrt(length_sq))
1046 } else {
1047 self
1048 }
1049 }
1050
1051 #[inline]
1059 #[must_use]
1060 pub fn mul_add(self, a: Self, b: Self) -> Self {
1061 Self::new(
1062 math::mul_add(self.x, a.x, b.x),
1063 math::mul_add(self.y, a.y, b.y),
1064 math::mul_add(self.z, a.z, b.z),
1065 )
1066 }
1067
1068 #[inline]
1077 #[must_use]
1078 pub fn reflect(self, normal: Self) -> Self {
1079 glam_assert!(normal.is_normalized());
1080 self - 2.0 * self.dot(normal) * normal
1081 }
1082
1083 #[inline]
1093 #[must_use]
1094 pub fn refract(self, normal: Self, eta: f64) -> Self {
1095 glam_assert!(self.is_normalized());
1096 glam_assert!(normal.is_normalized());
1097 let n_dot_i = normal.dot(self);
1098 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1099 if k >= 0.0 {
1100 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1101 } else {
1102 Self::ZERO
1103 }
1104 }
1105
1106 #[inline]
1110 #[must_use]
1111 pub fn angle_between(self, rhs: Self) -> f64 {
1112 math::acos_approx(
1113 self.dot(rhs)
1114 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1115 )
1116 }
1117
1118 #[inline]
1120 #[must_use]
1121 pub fn rotate_x(self, angle: f64) -> Self {
1122 let (sina, cosa) = math::sin_cos(angle);
1123 Self::new(
1124 self.x,
1125 self.y * cosa - self.z * sina,
1126 self.y * sina + self.z * cosa,
1127 )
1128 }
1129
1130 #[inline]
1132 #[must_use]
1133 pub fn rotate_y(self, angle: f64) -> Self {
1134 let (sina, cosa) = math::sin_cos(angle);
1135 Self::new(
1136 self.x * cosa + self.z * sina,
1137 self.y,
1138 self.x * -sina + self.z * cosa,
1139 )
1140 }
1141
1142 #[inline]
1144 #[must_use]
1145 pub fn rotate_z(self, angle: f64) -> Self {
1146 let (sina, cosa) = math::sin_cos(angle);
1147 Self::new(
1148 self.x * cosa - self.y * sina,
1149 self.x * sina + self.y * cosa,
1150 self.z,
1151 )
1152 }
1153
1154 #[inline]
1162 #[must_use]
1163 pub fn rotate_axis(self, axis: Self, angle: f64) -> Self {
1164 DQuat::from_axis_angle(axis, angle) * self
1165 }
1166
1167 #[inline]
1173 #[must_use]
1174 pub fn rotate_towards(self, rhs: Self, max_angle: f64) -> Self {
1175 let angle_between = self.angle_between(rhs);
1176 let angle = max_angle.clamp(angle_between - core::f64::consts::PI, angle_between);
1178 let axis = self
1179 .cross(rhs)
1180 .try_normalize()
1181 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1182 DQuat::from_axis_angle(axis, angle) * self
1183 }
1184
1185 #[inline]
1192 #[must_use]
1193 pub fn any_orthogonal_vector(self) -> Self {
1194 if math::abs(self.x) > math::abs(self.y) {
1196 Self::new(-self.z, 0.0, self.x) } else {
1198 Self::new(0.0, self.z, -self.y) }
1200 }
1201
1202 #[inline]
1210 #[must_use]
1211 pub fn any_orthonormal_vector(self) -> Self {
1212 glam_assert!(self.is_normalized());
1213 let sign = math::signum(self.z);
1215 let a = -1.0 / (sign + self.z);
1216 let b = self.x * self.y * a;
1217 Self::new(b, sign + self.y * self.y * a, -self.y)
1218 }
1219
1220 #[inline]
1227 #[must_use]
1228 pub fn any_orthonormal_pair(self) -> (Self, Self) {
1229 glam_assert!(self.is_normalized());
1230 let sign = math::signum(self.z);
1232 let a = -1.0 / (sign + self.z);
1233 let b = self.x * self.y * a;
1234 (
1235 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1236 Self::new(b, sign + self.y * self.y * a, -self.y),
1237 )
1238 }
1239
1240 #[inline]
1246 #[must_use]
1247 pub fn slerp(self, rhs: Self, s: f64) -> Self {
1248 let self_length = self.length();
1249 let rhs_length = rhs.length();
1250 let dot = self.dot(rhs) / (self_length * rhs_length);
1252 if math::abs(dot) < 1.0 - 3e-7 {
1254 let theta = math::acos_approx(dot);
1256 let sin_theta = math::sin(theta);
1258 let t1 = math::sin(theta * (1. - s));
1259 let t2 = math::sin(theta * s);
1260
1261 let result_length = self_length.lerp(rhs_length, s);
1263 return (self * (result_length / self_length) * t1
1265 + rhs * (result_length / rhs_length) * t2)
1266 * sin_theta.recip();
1267 }
1268 if dot < 0.0 {
1269 let axis = self.any_orthogonal_vector().normalize();
1273 let rotation = DQuat::from_axis_angle(axis, core::f64::consts::PI * s);
1274 let result_length = self_length.lerp(rhs_length, s);
1276 rotation * self * (result_length / self_length)
1277 } else {
1278 self.lerp(rhs, s)
1280 }
1281 }
1282
1283 #[inline]
1285 #[must_use]
1286 pub fn as_vec3(self) -> crate::Vec3 {
1287 crate::Vec3::new(self.x as f32, self.y as f32, self.z as f32)
1288 }
1289
1290 #[inline]
1292 #[must_use]
1293 pub fn as_vec3a(self) -> crate::Vec3A {
1294 crate::Vec3A::new(self.x as f32, self.y as f32, self.z as f32)
1295 }
1296
1297 #[cfg(feature = "i8")]
1299 #[inline]
1300 #[must_use]
1301 pub fn as_i8vec3(self) -> crate::I8Vec3 {
1302 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1303 }
1304
1305 #[cfg(feature = "u8")]
1307 #[inline]
1308 #[must_use]
1309 pub fn as_u8vec3(self) -> crate::U8Vec3 {
1310 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1311 }
1312
1313 #[cfg(feature = "i16")]
1315 #[inline]
1316 #[must_use]
1317 pub fn as_i16vec3(self) -> crate::I16Vec3 {
1318 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1319 }
1320
1321 #[cfg(feature = "u16")]
1323 #[inline]
1324 #[must_use]
1325 pub fn as_u16vec3(self) -> crate::U16Vec3 {
1326 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1327 }
1328
1329 #[cfg(feature = "i32")]
1331 #[inline]
1332 #[must_use]
1333 pub fn as_ivec3(self) -> crate::IVec3 {
1334 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1335 }
1336
1337 #[cfg(feature = "u32")]
1339 #[inline]
1340 #[must_use]
1341 pub fn as_uvec3(self) -> crate::UVec3 {
1342 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1343 }
1344
1345 #[cfg(feature = "i64")]
1347 #[inline]
1348 #[must_use]
1349 pub fn as_i64vec3(self) -> crate::I64Vec3 {
1350 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1351 }
1352
1353 #[cfg(feature = "u64")]
1355 #[inline]
1356 #[must_use]
1357 pub fn as_u64vec3(self) -> crate::U64Vec3 {
1358 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1359 }
1360
1361 #[cfg(feature = "isize")]
1363 #[inline]
1364 #[must_use]
1365 pub fn as_isizevec3(self) -> crate::ISizeVec3 {
1366 crate::ISizeVec3::new(self.x as isize, self.y as isize, self.z as isize)
1367 }
1368
1369 #[cfg(feature = "usize")]
1371 #[inline]
1372 #[must_use]
1373 pub fn as_usizevec3(self) -> crate::USizeVec3 {
1374 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1375 }
1376}
1377
1378impl Default for DVec3 {
1379 #[inline(always)]
1380 fn default() -> Self {
1381 Self::ZERO
1382 }
1383}
1384
1385impl Div for DVec3 {
1386 type Output = Self;
1387 #[inline]
1388 fn div(self, rhs: Self) -> Self {
1389 Self {
1390 x: self.x.div(rhs.x),
1391 y: self.y.div(rhs.y),
1392 z: self.z.div(rhs.z),
1393 }
1394 }
1395}
1396
1397impl Div<&Self> for DVec3 {
1398 type Output = Self;
1399 #[inline]
1400 fn div(self, rhs: &Self) -> Self {
1401 self.div(*rhs)
1402 }
1403}
1404
1405impl Div<&DVec3> for &DVec3 {
1406 type Output = DVec3;
1407 #[inline]
1408 fn div(self, rhs: &DVec3) -> DVec3 {
1409 (*self).div(*rhs)
1410 }
1411}
1412
1413impl Div<DVec3> for &DVec3 {
1414 type Output = DVec3;
1415 #[inline]
1416 fn div(self, rhs: DVec3) -> DVec3 {
1417 (*self).div(rhs)
1418 }
1419}
1420
1421impl DivAssign for DVec3 {
1422 #[inline]
1423 fn div_assign(&mut self, rhs: Self) {
1424 self.x.div_assign(rhs.x);
1425 self.y.div_assign(rhs.y);
1426 self.z.div_assign(rhs.z);
1427 }
1428}
1429
1430impl DivAssign<&Self> for DVec3 {
1431 #[inline]
1432 fn div_assign(&mut self, rhs: &Self) {
1433 self.div_assign(*rhs);
1434 }
1435}
1436
1437impl Div<f64> for DVec3 {
1438 type Output = Self;
1439 #[inline]
1440 fn div(self, rhs: f64) -> Self {
1441 Self {
1442 x: self.x.div(rhs),
1443 y: self.y.div(rhs),
1444 z: self.z.div(rhs),
1445 }
1446 }
1447}
1448
1449impl Div<&f64> for DVec3 {
1450 type Output = Self;
1451 #[inline]
1452 fn div(self, rhs: &f64) -> Self {
1453 self.div(*rhs)
1454 }
1455}
1456
1457impl Div<&f64> for &DVec3 {
1458 type Output = DVec3;
1459 #[inline]
1460 fn div(self, rhs: &f64) -> DVec3 {
1461 (*self).div(*rhs)
1462 }
1463}
1464
1465impl Div<f64> for &DVec3 {
1466 type Output = DVec3;
1467 #[inline]
1468 fn div(self, rhs: f64) -> DVec3 {
1469 (*self).div(rhs)
1470 }
1471}
1472
1473impl DivAssign<f64> for DVec3 {
1474 #[inline]
1475 fn div_assign(&mut self, rhs: f64) {
1476 self.x.div_assign(rhs);
1477 self.y.div_assign(rhs);
1478 self.z.div_assign(rhs);
1479 }
1480}
1481
1482impl DivAssign<&f64> for DVec3 {
1483 #[inline]
1484 fn div_assign(&mut self, rhs: &f64) {
1485 self.div_assign(*rhs);
1486 }
1487}
1488
1489impl Div<DVec3> for f64 {
1490 type Output = DVec3;
1491 #[inline]
1492 fn div(self, rhs: DVec3) -> DVec3 {
1493 DVec3 {
1494 x: self.div(rhs.x),
1495 y: self.div(rhs.y),
1496 z: self.div(rhs.z),
1497 }
1498 }
1499}
1500
1501impl Div<&DVec3> for f64 {
1502 type Output = DVec3;
1503 #[inline]
1504 fn div(self, rhs: &DVec3) -> DVec3 {
1505 self.div(*rhs)
1506 }
1507}
1508
1509impl Div<&DVec3> for &f64 {
1510 type Output = DVec3;
1511 #[inline]
1512 fn div(self, rhs: &DVec3) -> DVec3 {
1513 (*self).div(*rhs)
1514 }
1515}
1516
1517impl Div<DVec3> for &f64 {
1518 type Output = DVec3;
1519 #[inline]
1520 fn div(self, rhs: DVec3) -> DVec3 {
1521 (*self).div(rhs)
1522 }
1523}
1524
1525impl Mul for DVec3 {
1526 type Output = Self;
1527 #[inline]
1528 fn mul(self, rhs: Self) -> Self {
1529 Self {
1530 x: self.x.mul(rhs.x),
1531 y: self.y.mul(rhs.y),
1532 z: self.z.mul(rhs.z),
1533 }
1534 }
1535}
1536
1537impl Mul<&Self> for DVec3 {
1538 type Output = Self;
1539 #[inline]
1540 fn mul(self, rhs: &Self) -> Self {
1541 self.mul(*rhs)
1542 }
1543}
1544
1545impl Mul<&DVec3> for &DVec3 {
1546 type Output = DVec3;
1547 #[inline]
1548 fn mul(self, rhs: &DVec3) -> DVec3 {
1549 (*self).mul(*rhs)
1550 }
1551}
1552
1553impl Mul<DVec3> for &DVec3 {
1554 type Output = DVec3;
1555 #[inline]
1556 fn mul(self, rhs: DVec3) -> DVec3 {
1557 (*self).mul(rhs)
1558 }
1559}
1560
1561impl MulAssign for DVec3 {
1562 #[inline]
1563 fn mul_assign(&mut self, rhs: Self) {
1564 self.x.mul_assign(rhs.x);
1565 self.y.mul_assign(rhs.y);
1566 self.z.mul_assign(rhs.z);
1567 }
1568}
1569
1570impl MulAssign<&Self> for DVec3 {
1571 #[inline]
1572 fn mul_assign(&mut self, rhs: &Self) {
1573 self.mul_assign(*rhs);
1574 }
1575}
1576
1577impl Mul<f64> for DVec3 {
1578 type Output = Self;
1579 #[inline]
1580 fn mul(self, rhs: f64) -> Self {
1581 Self {
1582 x: self.x.mul(rhs),
1583 y: self.y.mul(rhs),
1584 z: self.z.mul(rhs),
1585 }
1586 }
1587}
1588
1589impl Mul<&f64> for DVec3 {
1590 type Output = Self;
1591 #[inline]
1592 fn mul(self, rhs: &f64) -> Self {
1593 self.mul(*rhs)
1594 }
1595}
1596
1597impl Mul<&f64> for &DVec3 {
1598 type Output = DVec3;
1599 #[inline]
1600 fn mul(self, rhs: &f64) -> DVec3 {
1601 (*self).mul(*rhs)
1602 }
1603}
1604
1605impl Mul<f64> for &DVec3 {
1606 type Output = DVec3;
1607 #[inline]
1608 fn mul(self, rhs: f64) -> DVec3 {
1609 (*self).mul(rhs)
1610 }
1611}
1612
1613impl MulAssign<f64> for DVec3 {
1614 #[inline]
1615 fn mul_assign(&mut self, rhs: f64) {
1616 self.x.mul_assign(rhs);
1617 self.y.mul_assign(rhs);
1618 self.z.mul_assign(rhs);
1619 }
1620}
1621
1622impl MulAssign<&f64> for DVec3 {
1623 #[inline]
1624 fn mul_assign(&mut self, rhs: &f64) {
1625 self.mul_assign(*rhs);
1626 }
1627}
1628
1629impl Mul<DVec3> for f64 {
1630 type Output = DVec3;
1631 #[inline]
1632 fn mul(self, rhs: DVec3) -> DVec3 {
1633 DVec3 {
1634 x: self.mul(rhs.x),
1635 y: self.mul(rhs.y),
1636 z: self.mul(rhs.z),
1637 }
1638 }
1639}
1640
1641impl Mul<&DVec3> for f64 {
1642 type Output = DVec3;
1643 #[inline]
1644 fn mul(self, rhs: &DVec3) -> DVec3 {
1645 self.mul(*rhs)
1646 }
1647}
1648
1649impl Mul<&DVec3> for &f64 {
1650 type Output = DVec3;
1651 #[inline]
1652 fn mul(self, rhs: &DVec3) -> DVec3 {
1653 (*self).mul(*rhs)
1654 }
1655}
1656
1657impl Mul<DVec3> for &f64 {
1658 type Output = DVec3;
1659 #[inline]
1660 fn mul(self, rhs: DVec3) -> DVec3 {
1661 (*self).mul(rhs)
1662 }
1663}
1664
1665impl Add for DVec3 {
1666 type Output = Self;
1667 #[inline]
1668 fn add(self, rhs: Self) -> Self {
1669 Self {
1670 x: self.x.add(rhs.x),
1671 y: self.y.add(rhs.y),
1672 z: self.z.add(rhs.z),
1673 }
1674 }
1675}
1676
1677impl Add<&Self> for DVec3 {
1678 type Output = Self;
1679 #[inline]
1680 fn add(self, rhs: &Self) -> Self {
1681 self.add(*rhs)
1682 }
1683}
1684
1685impl Add<&DVec3> for &DVec3 {
1686 type Output = DVec3;
1687 #[inline]
1688 fn add(self, rhs: &DVec3) -> DVec3 {
1689 (*self).add(*rhs)
1690 }
1691}
1692
1693impl Add<DVec3> for &DVec3 {
1694 type Output = DVec3;
1695 #[inline]
1696 fn add(self, rhs: DVec3) -> DVec3 {
1697 (*self).add(rhs)
1698 }
1699}
1700
1701impl AddAssign for DVec3 {
1702 #[inline]
1703 fn add_assign(&mut self, rhs: Self) {
1704 self.x.add_assign(rhs.x);
1705 self.y.add_assign(rhs.y);
1706 self.z.add_assign(rhs.z);
1707 }
1708}
1709
1710impl AddAssign<&Self> for DVec3 {
1711 #[inline]
1712 fn add_assign(&mut self, rhs: &Self) {
1713 self.add_assign(*rhs);
1714 }
1715}
1716
1717impl Add<f64> for DVec3 {
1718 type Output = Self;
1719 #[inline]
1720 fn add(self, rhs: f64) -> Self {
1721 Self {
1722 x: self.x.add(rhs),
1723 y: self.y.add(rhs),
1724 z: self.z.add(rhs),
1725 }
1726 }
1727}
1728
1729impl Add<&f64> for DVec3 {
1730 type Output = Self;
1731 #[inline]
1732 fn add(self, rhs: &f64) -> Self {
1733 self.add(*rhs)
1734 }
1735}
1736
1737impl Add<&f64> for &DVec3 {
1738 type Output = DVec3;
1739 #[inline]
1740 fn add(self, rhs: &f64) -> DVec3 {
1741 (*self).add(*rhs)
1742 }
1743}
1744
1745impl Add<f64> for &DVec3 {
1746 type Output = DVec3;
1747 #[inline]
1748 fn add(self, rhs: f64) -> DVec3 {
1749 (*self).add(rhs)
1750 }
1751}
1752
1753impl AddAssign<f64> for DVec3 {
1754 #[inline]
1755 fn add_assign(&mut self, rhs: f64) {
1756 self.x.add_assign(rhs);
1757 self.y.add_assign(rhs);
1758 self.z.add_assign(rhs);
1759 }
1760}
1761
1762impl AddAssign<&f64> for DVec3 {
1763 #[inline]
1764 fn add_assign(&mut self, rhs: &f64) {
1765 self.add_assign(*rhs);
1766 }
1767}
1768
1769impl Add<DVec3> for f64 {
1770 type Output = DVec3;
1771 #[inline]
1772 fn add(self, rhs: DVec3) -> DVec3 {
1773 DVec3 {
1774 x: self.add(rhs.x),
1775 y: self.add(rhs.y),
1776 z: self.add(rhs.z),
1777 }
1778 }
1779}
1780
1781impl Add<&DVec3> for f64 {
1782 type Output = DVec3;
1783 #[inline]
1784 fn add(self, rhs: &DVec3) -> DVec3 {
1785 self.add(*rhs)
1786 }
1787}
1788
1789impl Add<&DVec3> for &f64 {
1790 type Output = DVec3;
1791 #[inline]
1792 fn add(self, rhs: &DVec3) -> DVec3 {
1793 (*self).add(*rhs)
1794 }
1795}
1796
1797impl Add<DVec3> for &f64 {
1798 type Output = DVec3;
1799 #[inline]
1800 fn add(self, rhs: DVec3) -> DVec3 {
1801 (*self).add(rhs)
1802 }
1803}
1804
1805impl Sub for DVec3 {
1806 type Output = Self;
1807 #[inline]
1808 fn sub(self, rhs: Self) -> Self {
1809 Self {
1810 x: self.x.sub(rhs.x),
1811 y: self.y.sub(rhs.y),
1812 z: self.z.sub(rhs.z),
1813 }
1814 }
1815}
1816
1817impl Sub<&Self> for DVec3 {
1818 type Output = Self;
1819 #[inline]
1820 fn sub(self, rhs: &Self) -> Self {
1821 self.sub(*rhs)
1822 }
1823}
1824
1825impl Sub<&DVec3> for &DVec3 {
1826 type Output = DVec3;
1827 #[inline]
1828 fn sub(self, rhs: &DVec3) -> DVec3 {
1829 (*self).sub(*rhs)
1830 }
1831}
1832
1833impl Sub<DVec3> for &DVec3 {
1834 type Output = DVec3;
1835 #[inline]
1836 fn sub(self, rhs: DVec3) -> DVec3 {
1837 (*self).sub(rhs)
1838 }
1839}
1840
1841impl SubAssign for DVec3 {
1842 #[inline]
1843 fn sub_assign(&mut self, rhs: Self) {
1844 self.x.sub_assign(rhs.x);
1845 self.y.sub_assign(rhs.y);
1846 self.z.sub_assign(rhs.z);
1847 }
1848}
1849
1850impl SubAssign<&Self> for DVec3 {
1851 #[inline]
1852 fn sub_assign(&mut self, rhs: &Self) {
1853 self.sub_assign(*rhs);
1854 }
1855}
1856
1857impl Sub<f64> for DVec3 {
1858 type Output = Self;
1859 #[inline]
1860 fn sub(self, rhs: f64) -> Self {
1861 Self {
1862 x: self.x.sub(rhs),
1863 y: self.y.sub(rhs),
1864 z: self.z.sub(rhs),
1865 }
1866 }
1867}
1868
1869impl Sub<&f64> for DVec3 {
1870 type Output = Self;
1871 #[inline]
1872 fn sub(self, rhs: &f64) -> Self {
1873 self.sub(*rhs)
1874 }
1875}
1876
1877impl Sub<&f64> for &DVec3 {
1878 type Output = DVec3;
1879 #[inline]
1880 fn sub(self, rhs: &f64) -> DVec3 {
1881 (*self).sub(*rhs)
1882 }
1883}
1884
1885impl Sub<f64> for &DVec3 {
1886 type Output = DVec3;
1887 #[inline]
1888 fn sub(self, rhs: f64) -> DVec3 {
1889 (*self).sub(rhs)
1890 }
1891}
1892
1893impl SubAssign<f64> for DVec3 {
1894 #[inline]
1895 fn sub_assign(&mut self, rhs: f64) {
1896 self.x.sub_assign(rhs);
1897 self.y.sub_assign(rhs);
1898 self.z.sub_assign(rhs);
1899 }
1900}
1901
1902impl SubAssign<&f64> for DVec3 {
1903 #[inline]
1904 fn sub_assign(&mut self, rhs: &f64) {
1905 self.sub_assign(*rhs);
1906 }
1907}
1908
1909impl Sub<DVec3> for f64 {
1910 type Output = DVec3;
1911 #[inline]
1912 fn sub(self, rhs: DVec3) -> DVec3 {
1913 DVec3 {
1914 x: self.sub(rhs.x),
1915 y: self.sub(rhs.y),
1916 z: self.sub(rhs.z),
1917 }
1918 }
1919}
1920
1921impl Sub<&DVec3> for f64 {
1922 type Output = DVec3;
1923 #[inline]
1924 fn sub(self, rhs: &DVec3) -> DVec3 {
1925 self.sub(*rhs)
1926 }
1927}
1928
1929impl Sub<&DVec3> for &f64 {
1930 type Output = DVec3;
1931 #[inline]
1932 fn sub(self, rhs: &DVec3) -> DVec3 {
1933 (*self).sub(*rhs)
1934 }
1935}
1936
1937impl Sub<DVec3> for &f64 {
1938 type Output = DVec3;
1939 #[inline]
1940 fn sub(self, rhs: DVec3) -> DVec3 {
1941 (*self).sub(rhs)
1942 }
1943}
1944
1945impl Rem for DVec3 {
1946 type Output = Self;
1947 #[inline]
1948 fn rem(self, rhs: Self) -> Self {
1949 Self {
1950 x: self.x.rem(rhs.x),
1951 y: self.y.rem(rhs.y),
1952 z: self.z.rem(rhs.z),
1953 }
1954 }
1955}
1956
1957impl Rem<&Self> for DVec3 {
1958 type Output = Self;
1959 #[inline]
1960 fn rem(self, rhs: &Self) -> Self {
1961 self.rem(*rhs)
1962 }
1963}
1964
1965impl Rem<&DVec3> for &DVec3 {
1966 type Output = DVec3;
1967 #[inline]
1968 fn rem(self, rhs: &DVec3) -> DVec3 {
1969 (*self).rem(*rhs)
1970 }
1971}
1972
1973impl Rem<DVec3> for &DVec3 {
1974 type Output = DVec3;
1975 #[inline]
1976 fn rem(self, rhs: DVec3) -> DVec3 {
1977 (*self).rem(rhs)
1978 }
1979}
1980
1981impl RemAssign for DVec3 {
1982 #[inline]
1983 fn rem_assign(&mut self, rhs: Self) {
1984 self.x.rem_assign(rhs.x);
1985 self.y.rem_assign(rhs.y);
1986 self.z.rem_assign(rhs.z);
1987 }
1988}
1989
1990impl RemAssign<&Self> for DVec3 {
1991 #[inline]
1992 fn rem_assign(&mut self, rhs: &Self) {
1993 self.rem_assign(*rhs);
1994 }
1995}
1996
1997impl Rem<f64> for DVec3 {
1998 type Output = Self;
1999 #[inline]
2000 fn rem(self, rhs: f64) -> Self {
2001 Self {
2002 x: self.x.rem(rhs),
2003 y: self.y.rem(rhs),
2004 z: self.z.rem(rhs),
2005 }
2006 }
2007}
2008
2009impl Rem<&f64> for DVec3 {
2010 type Output = Self;
2011 #[inline]
2012 fn rem(self, rhs: &f64) -> Self {
2013 self.rem(*rhs)
2014 }
2015}
2016
2017impl Rem<&f64> for &DVec3 {
2018 type Output = DVec3;
2019 #[inline]
2020 fn rem(self, rhs: &f64) -> DVec3 {
2021 (*self).rem(*rhs)
2022 }
2023}
2024
2025impl Rem<f64> for &DVec3 {
2026 type Output = DVec3;
2027 #[inline]
2028 fn rem(self, rhs: f64) -> DVec3 {
2029 (*self).rem(rhs)
2030 }
2031}
2032
2033impl RemAssign<f64> for DVec3 {
2034 #[inline]
2035 fn rem_assign(&mut self, rhs: f64) {
2036 self.x.rem_assign(rhs);
2037 self.y.rem_assign(rhs);
2038 self.z.rem_assign(rhs);
2039 }
2040}
2041
2042impl RemAssign<&f64> for DVec3 {
2043 #[inline]
2044 fn rem_assign(&mut self, rhs: &f64) {
2045 self.rem_assign(*rhs);
2046 }
2047}
2048
2049impl Rem<DVec3> for f64 {
2050 type Output = DVec3;
2051 #[inline]
2052 fn rem(self, rhs: DVec3) -> DVec3 {
2053 DVec3 {
2054 x: self.rem(rhs.x),
2055 y: self.rem(rhs.y),
2056 z: self.rem(rhs.z),
2057 }
2058 }
2059}
2060
2061impl Rem<&DVec3> for f64 {
2062 type Output = DVec3;
2063 #[inline]
2064 fn rem(self, rhs: &DVec3) -> DVec3 {
2065 self.rem(*rhs)
2066 }
2067}
2068
2069impl Rem<&DVec3> for &f64 {
2070 type Output = DVec3;
2071 #[inline]
2072 fn rem(self, rhs: &DVec3) -> DVec3 {
2073 (*self).rem(*rhs)
2074 }
2075}
2076
2077impl Rem<DVec3> for &f64 {
2078 type Output = DVec3;
2079 #[inline]
2080 fn rem(self, rhs: DVec3) -> DVec3 {
2081 (*self).rem(rhs)
2082 }
2083}
2084
2085impl AsRef<[f64; 3]> for DVec3 {
2086 #[inline]
2087 fn as_ref(&self) -> &[f64; 3] {
2088 unsafe { &*(self as *const Self as *const [f64; 3]) }
2089 }
2090}
2091
2092impl AsMut<[f64; 3]> for DVec3 {
2093 #[inline]
2094 fn as_mut(&mut self) -> &mut [f64; 3] {
2095 unsafe { &mut *(self as *mut Self as *mut [f64; 3]) }
2096 }
2097}
2098
2099impl Sum for DVec3 {
2100 #[inline]
2101 fn sum<I>(iter: I) -> Self
2102 where
2103 I: Iterator<Item = Self>,
2104 {
2105 iter.fold(Self::ZERO, Self::add)
2106 }
2107}
2108
2109impl<'a> Sum<&'a Self> for DVec3 {
2110 #[inline]
2111 fn sum<I>(iter: I) -> Self
2112 where
2113 I: Iterator<Item = &'a Self>,
2114 {
2115 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2116 }
2117}
2118
2119impl Product for DVec3 {
2120 #[inline]
2121 fn product<I>(iter: I) -> Self
2122 where
2123 I: Iterator<Item = Self>,
2124 {
2125 iter.fold(Self::ONE, Self::mul)
2126 }
2127}
2128
2129impl<'a> Product<&'a Self> for DVec3 {
2130 #[inline]
2131 fn product<I>(iter: I) -> Self
2132 where
2133 I: Iterator<Item = &'a Self>,
2134 {
2135 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2136 }
2137}
2138
2139impl Neg for DVec3 {
2140 type Output = Self;
2141 #[inline]
2142 fn neg(self) -> Self {
2143 Self {
2144 x: self.x.neg(),
2145 y: self.y.neg(),
2146 z: self.z.neg(),
2147 }
2148 }
2149}
2150
2151impl Neg for &DVec3 {
2152 type Output = DVec3;
2153 #[inline]
2154 fn neg(self) -> DVec3 {
2155 (*self).neg()
2156 }
2157}
2158
2159impl Index<usize> for DVec3 {
2160 type Output = f64;
2161 #[inline]
2162 fn index(&self, index: usize) -> &Self::Output {
2163 match index {
2164 0 => &self.x,
2165 1 => &self.y,
2166 2 => &self.z,
2167 _ => panic!("index out of bounds"),
2168 }
2169 }
2170}
2171
2172impl IndexMut<usize> for DVec3 {
2173 #[inline]
2174 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2175 match index {
2176 0 => &mut self.x,
2177 1 => &mut self.y,
2178 2 => &mut self.z,
2179 _ => panic!("index out of bounds"),
2180 }
2181 }
2182}
2183
2184impl fmt::Display for DVec3 {
2185 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2186 if let Some(p) = f.precision() {
2187 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2188 } else {
2189 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2190 }
2191 }
2192}
2193
2194impl fmt::Debug for DVec3 {
2195 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2196 fmt.debug_tuple(stringify!(DVec3))
2197 .field(&self.x)
2198 .field(&self.y)
2199 .field(&self.z)
2200 .finish()
2201 }
2202}
2203
2204impl From<[f64; 3]> for DVec3 {
2205 #[inline]
2206 fn from(a: [f64; 3]) -> Self {
2207 Self::new(a[0], a[1], a[2])
2208 }
2209}
2210
2211impl From<DVec3> for [f64; 3] {
2212 #[inline]
2213 fn from(v: DVec3) -> Self {
2214 [v.x, v.y, v.z]
2215 }
2216}
2217
2218impl From<(f64, f64, f64)> for DVec3 {
2219 #[inline]
2220 fn from(t: (f64, f64, f64)) -> Self {
2221 Self::new(t.0, t.1, t.2)
2222 }
2223}
2224
2225impl From<DVec3> for (f64, f64, f64) {
2226 #[inline]
2227 fn from(v: DVec3) -> Self {
2228 (v.x, v.y, v.z)
2229 }
2230}
2231
2232impl From<(DVec2, f64)> for DVec3 {
2233 #[inline]
2234 fn from((v, z): (DVec2, f64)) -> Self {
2235 Self::new(v.x, v.y, z)
2236 }
2237}
2238
2239impl From<Vec3> for DVec3 {
2240 #[inline]
2241 fn from(v: Vec3) -> Self {
2242 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2243 }
2244}
2245
2246#[cfg(feature = "i32")]
2247impl From<IVec3> for DVec3 {
2248 #[inline]
2249 fn from(v: IVec3) -> Self {
2250 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2251 }
2252}
2253
2254#[cfg(feature = "u32")]
2255impl From<UVec3> for DVec3 {
2256 #[inline]
2257 fn from(v: UVec3) -> Self {
2258 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2259 }
2260}
2261
2262impl From<BVec3> for DVec3 {
2263 #[inline]
2264 fn from(v: BVec3) -> Self {
2265 Self::new(f64::from(v.x), f64::from(v.y), f64::from(v.z))
2266 }
2267}
2268
2269impl From<BVec3A> for DVec3 {
2270 #[inline]
2271 fn from(v: BVec3A) -> Self {
2272 let bool_array: [bool; 3] = v.into();
2273 Self::new(
2274 f64::from(bool_array[0]),
2275 f64::from(bool_array[1]),
2276 f64::from(bool_array[2]),
2277 )
2278 }
2279}