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