1#[cfg(not(feature = "scalar-math"))]
4use crate::BVec4A;
5use crate::{BVec4, DVec2, DVec3, f64::math};
6
7use crate::Vec4;
8
9#[cfg(feature = "i32")]
10use crate::IVec4;
11
12#[cfg(feature = "u32")]
13use crate::UVec4;
14
15use core::fmt;
16use core::iter::{Product, Sum};
17use core::ops::*;
18
19#[cfg(feature = "zerocopy-08")]
20use zerocopy_derive_08::*;
21
22#[inline(always)]
24#[must_use]
25pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 {
26 DVec4::new(x, y, z, w)
27}
28
29#[derive(Clone, Copy, PartialEq)]
31#[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
32#[cfg_attr(
33 feature = "zerocopy-08",
34 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
35)]
36#[cfg_attr(feature = "cuda", repr(align(16)))]
37#[repr(C)]
38#[cfg_attr(target_arch = "spirv", rust_gpu::vector::v1)]
39pub struct DVec4 {
40 pub x: f64,
41 pub y: f64,
42 pub z: f64,
43 pub w: f64,
44}
45
46impl DVec4 {
47 pub const ZERO: Self = Self::splat(0.0);
49
50 pub const ONE: Self = Self::splat(1.0);
52
53 pub const NEG_ONE: Self = Self::splat(-1.0);
55
56 pub const MIN: Self = Self::splat(f64::MIN);
58
59 pub const MAX: Self = Self::splat(f64::MAX);
61
62 pub const NAN: Self = Self::splat(f64::NAN);
64
65 pub const INFINITY: Self = Self::splat(f64::INFINITY);
67
68 pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
70
71 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
73
74 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
76
77 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
79
80 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
82
83 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
85
86 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
88
89 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
91
92 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
94
95 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
97
98 pub const USES_CORE_SIMD: bool = false;
100 pub const USES_NEON: bool = false;
102 pub const USES_SCALAR_MATH: bool = true;
104 pub const USES_SSE2: bool = false;
106 pub const USES_WASM_SIMD: bool = false;
108
109 #[inline(always)]
111 #[must_use]
112 pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
113 Self { x, y, z, w }
114 }
115
116 #[inline]
118 #[must_use]
119 pub const fn splat(v: f64) -> Self {
120 Self::new(v, v, v, v)
121 }
122
123 #[inline]
125 #[must_use]
126 pub fn map<F>(self, mut f: F) -> Self
127 where
128 F: FnMut(f64) -> f64,
129 {
130 Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
131 }
132
133 #[inline]
139 #[must_use]
140 pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
141 Self::new(
142 if mask.test(0) { if_true.x } else { if_false.x },
143 if mask.test(1) { if_true.y } else { if_false.y },
144 if mask.test(2) { if_true.z } else { if_false.z },
145 if mask.test(3) { if_true.w } else { if_false.w },
146 )
147 }
148
149 #[inline]
151 #[must_use]
152 pub const fn from_array(a: [f64; 4]) -> Self {
153 Self::new(a[0], a[1], a[2], a[3])
154 }
155
156 #[inline]
158 #[must_use]
159 pub const fn to_array(&self) -> [f64; 4] {
160 [self.x, self.y, self.z, self.w]
161 }
162
163 #[inline]
169 #[must_use]
170 #[track_caller]
171 pub const fn from_slice(slice: &[f64]) -> Self {
172 assert!(slice.len() >= 4);
173 Self::new(slice[0], slice[1], slice[2], slice[3])
174 }
175
176 #[inline]
182 #[track_caller]
183 pub fn write_to_slice(self, slice: &mut [f64]) {
184 slice[..4].copy_from_slice(&self.to_array());
185 }
186
187 #[inline]
191 #[must_use]
192 pub fn truncate(self) -> DVec3 {
193 use crate::swizzles::Vec4Swizzles;
194 self.xyz()
195 }
196
197 #[inline]
203 #[must_use]
204 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
205 pub fn project(self) -> DVec3 {
206 DVec3::from_homogeneous(self)
207 }
208
209 #[inline]
211 #[must_use]
212 pub fn with_x(mut self, x: f64) -> Self {
213 self.x = x;
214 self
215 }
216
217 #[inline]
219 #[must_use]
220 pub fn with_y(mut self, y: f64) -> Self {
221 self.y = y;
222 self
223 }
224
225 #[inline]
227 #[must_use]
228 pub fn with_z(mut self, z: f64) -> Self {
229 self.z = z;
230 self
231 }
232
233 #[inline]
235 #[must_use]
236 pub fn with_w(mut self, w: f64) -> Self {
237 self.w = w;
238 self
239 }
240
241 #[inline]
243 #[must_use]
244 pub fn dot(self, rhs: Self) -> f64 {
245 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
246 }
247
248 #[inline]
250 #[must_use]
251 pub fn dot_into_vec(self, rhs: Self) -> Self {
252 Self::splat(self.dot(rhs))
253 }
254
255 #[inline]
263 #[must_use]
264 pub fn min(self, rhs: Self) -> Self {
265 Self::new(
266 if self.x < rhs.x { self.x } else { rhs.x },
267 if self.y < rhs.y { self.y } else { rhs.y },
268 if self.z < rhs.z { self.z } else { rhs.z },
269 if self.w < rhs.w { self.w } else { rhs.w },
270 )
271 }
272
273 #[inline]
281 #[must_use]
282 pub fn max(self, rhs: Self) -> Self {
283 Self::new(
284 if self.x > rhs.x { self.x } else { rhs.x },
285 if self.y > rhs.y { self.y } else { rhs.y },
286 if self.z > rhs.z { self.z } else { rhs.z },
287 if self.w > rhs.w { self.w } else { rhs.w },
288 )
289 }
290
291 #[inline]
301 #[must_use]
302 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
303 pub fn clamp(self, min: Self, max: Self) -> Self {
304 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
305 self.max(min).min(max)
306 }
307
308 #[inline]
314 #[must_use]
315 pub fn min_element(self) -> f64 {
316 let min = |a, b| if a < b { a } else { b };
317 min(min(self.x, self.y), min(self.z, self.w))
318 }
319
320 #[inline]
326 #[must_use]
327 pub fn max_element(self) -> f64 {
328 let max = |a, b| if a > b { a } else { b };
329 max(max(self.x, self.y), max(self.z, self.w))
330 }
331
332 #[doc(alias = "argmin")]
334 #[inline]
335 #[must_use]
336 pub fn min_position(self) -> usize {
337 let mut min = self.x;
338 let mut index = 0;
339 if self.y < min {
340 min = self.y;
341 index = 1;
342 }
343 if self.z < min {
344 min = self.z;
345 index = 2;
346 }
347 if self.w < min {
348 index = 3;
349 }
350 index
351 }
352
353 #[doc(alias = "argmax")]
355 #[inline]
356 #[must_use]
357 pub fn max_position(self) -> usize {
358 let mut max = self.x;
359 let mut index = 0;
360 if self.y > max {
361 max = self.y;
362 index = 1;
363 }
364 if self.z > max {
365 max = self.z;
366 index = 2;
367 }
368 if self.w > max {
369 index = 3;
370 }
371 index
372 }
373
374 #[inline]
378 #[must_use]
379 pub fn element_sum(self) -> f64 {
380 self.x + self.y + self.z + self.w
381 }
382
383 #[inline]
387 #[must_use]
388 pub fn element_product(self) -> f64 {
389 self.x * self.y * self.z * self.w
390 }
391
392 #[inline]
398 #[must_use]
399 pub fn cmpeq(self, rhs: Self) -> BVec4 {
400 BVec4::new(
401 self.x.eq(&rhs.x),
402 self.y.eq(&rhs.y),
403 self.z.eq(&rhs.z),
404 self.w.eq(&rhs.w),
405 )
406 }
407
408 #[inline]
414 #[must_use]
415 pub fn cmpne(self, rhs: Self) -> BVec4 {
416 BVec4::new(
417 self.x.ne(&rhs.x),
418 self.y.ne(&rhs.y),
419 self.z.ne(&rhs.z),
420 self.w.ne(&rhs.w),
421 )
422 }
423
424 #[inline]
430 #[must_use]
431 pub fn cmpge(self, rhs: Self) -> BVec4 {
432 BVec4::new(
433 self.x.ge(&rhs.x),
434 self.y.ge(&rhs.y),
435 self.z.ge(&rhs.z),
436 self.w.ge(&rhs.w),
437 )
438 }
439
440 #[inline]
446 #[must_use]
447 pub fn cmpgt(self, rhs: Self) -> BVec4 {
448 BVec4::new(
449 self.x.gt(&rhs.x),
450 self.y.gt(&rhs.y),
451 self.z.gt(&rhs.z),
452 self.w.gt(&rhs.w),
453 )
454 }
455
456 #[inline]
462 #[must_use]
463 pub fn cmple(self, rhs: Self) -> BVec4 {
464 BVec4::new(
465 self.x.le(&rhs.x),
466 self.y.le(&rhs.y),
467 self.z.le(&rhs.z),
468 self.w.le(&rhs.w),
469 )
470 }
471
472 #[inline]
478 #[must_use]
479 pub fn cmplt(self, rhs: Self) -> BVec4 {
480 BVec4::new(
481 self.x.lt(&rhs.x),
482 self.y.lt(&rhs.y),
483 self.z.lt(&rhs.z),
484 self.w.lt(&rhs.w),
485 )
486 }
487
488 #[inline]
490 #[must_use]
491 pub fn abs(self) -> Self {
492 Self::new(self.x.abs(), self.y.abs(), self.z.abs(), self.w.abs())
493 }
494
495 #[inline]
501 #[must_use]
502 pub fn signum(self) -> Self {
503 Self::new(
504 self.x.signum(),
505 self.y.signum(),
506 self.z.signum(),
507 self.w.signum(),
508 )
509 }
510
511 #[inline]
513 #[must_use]
514 pub fn copysign(self, rhs: Self) -> Self {
515 Self::new(
516 self.x.copysign(rhs.x),
517 self.y.copysign(rhs.y),
518 self.z.copysign(rhs.z),
519 self.w.copysign(rhs.w),
520 )
521 }
522
523 #[inline]
531 #[must_use]
532 pub fn is_negative_bitmask(self) -> u32 {
533 (self.x.is_sign_negative() as u32)
534 | ((self.y.is_sign_negative() as u32) << 1)
535 | ((self.z.is_sign_negative() as u32) << 2)
536 | ((self.w.is_sign_negative() as u32) << 3)
537 }
538
539 #[inline]
544 #[must_use]
545 pub fn is_negative_mask(self) -> BVec4 {
546 BVec4::new(
547 self.x.is_sign_negative(),
548 self.y.is_sign_negative(),
549 self.z.is_sign_negative(),
550 self.w.is_sign_negative(),
551 )
552 }
553
554 #[inline]
557 #[must_use]
558 pub fn is_finite(self) -> bool {
559 self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
560 }
561
562 #[inline]
566 #[must_use]
567 pub fn is_finite_mask(self) -> BVec4 {
568 BVec4::new(
569 self.x.is_finite(),
570 self.y.is_finite(),
571 self.z.is_finite(),
572 self.w.is_finite(),
573 )
574 }
575
576 #[inline]
578 #[must_use]
579 pub fn is_nan(self) -> bool {
580 self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
581 }
582
583 #[inline]
587 #[must_use]
588 pub fn is_nan_mask(self) -> BVec4 {
589 BVec4::new(
590 self.x.is_nan(),
591 self.y.is_nan(),
592 self.z.is_nan(),
593 self.w.is_nan(),
594 )
595 }
596
597 #[doc(alias = "magnitude")]
599 #[inline]
600 #[must_use]
601 pub fn length(self) -> f64 {
602 math::sqrt(self.dot(self))
603 }
604
605 #[allow(dead_code)]
607 fn is_non_zero(self) -> bool {
608 self.length_squared() > 0.0
609 }
610
611 #[doc(alias = "magnitude2")]
615 #[inline]
616 #[must_use]
617 pub fn length_squared(self) -> f64 {
618 self.dot(self)
619 }
620
621 #[inline]
625 #[must_use]
626 pub fn length_recip(self) -> f64 {
627 1.0 / self.length()
628 }
629
630 #[inline]
632 #[must_use]
633 pub fn distance(self, rhs: Self) -> f64 {
634 (self - rhs).length()
635 }
636
637 #[inline]
639 #[must_use]
640 pub fn distance_squared(self, rhs: Self) -> f64 {
641 (self - rhs).length_squared()
642 }
643
644 #[inline]
646 #[must_use]
647 pub fn div_euclid(self, rhs: Self) -> Self {
648 Self::new(
649 math::div_euclid(self.x, rhs.x),
650 math::div_euclid(self.y, rhs.y),
651 math::div_euclid(self.z, rhs.z),
652 math::div_euclid(self.w, rhs.w),
653 )
654 }
655
656 #[inline]
660 #[must_use]
661 pub fn rem_euclid(self, rhs: Self) -> Self {
662 Self::new(
663 math::rem_euclid(self.x, rhs.x),
664 math::rem_euclid(self.y, rhs.y),
665 math::rem_euclid(self.z, rhs.z),
666 math::rem_euclid(self.w, rhs.w),
667 )
668 }
669
670 #[inline]
680 #[must_use]
681 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
682 pub fn normalize(self) -> Self {
683 #[allow(clippy::let_and_return)]
684 let normalized = self.mul(self.length_recip());
685 glam_assert!(normalized.is_finite());
686 normalized
687 }
688
689 #[inline]
696 #[must_use]
697 pub fn try_normalize(self) -> Option<Self> {
698 let rcp = self.length_recip();
699 if rcp.is_finite() && rcp > 0.0 {
700 Some(self * rcp)
701 } else {
702 None
703 }
704 }
705
706 #[inline]
714 #[must_use]
715 pub fn normalize_or(self, fallback: Self) -> Self {
716 let rcp = self.length_recip();
717 if rcp.is_finite() && rcp > 0.0 {
718 self * rcp
719 } else {
720 fallback
721 }
722 }
723
724 #[inline]
731 #[must_use]
732 pub fn normalize_or_zero(self) -> Self {
733 self.normalize_or(Self::ZERO)
734 }
735
736 #[inline]
740 #[must_use]
741 pub fn normalize_and_length(self) -> (Self, f64) {
742 let length = self.length();
743 let rcp = 1.0 / length;
744 if rcp.is_finite() && rcp > 0.0 {
745 (self * rcp, length)
746 } else {
747 (Self::X, 0.0)
748 }
749 }
750
751 #[inline]
755 #[must_use]
756 pub fn is_normalized(self) -> bool {
757 (self.length_squared() - 1.0).abs() <= 2e-4
758 }
759
760 #[inline]
768 #[must_use]
769 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
770 pub fn project_onto(self, rhs: Self) -> Self {
771 let rhs_len_sq = rhs.dot(rhs);
772 glam_assert!(rhs_len_sq != 0.0);
773 rhs * (self.dot(rhs) / rhs_len_sq)
774 }
775
776 #[doc(alias("plane"))]
787 #[inline]
788 #[must_use]
789 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
790 pub fn reject_from(self, rhs: Self) -> Self {
791 self - self.project_onto(rhs)
792 }
793
794 #[inline]
802 #[must_use]
803 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
804 pub fn project_onto_normalized(self, rhs: Self) -> Self {
805 glam_assert!(rhs.is_normalized());
806 rhs * self.dot(rhs)
807 }
808
809 #[doc(alias("plane"))]
820 #[inline]
821 #[must_use]
822 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
823 pub fn reject_from_normalized(self, rhs: Self) -> Self {
824 self - self.project_onto_normalized(rhs)
825 }
826
827 #[inline]
830 #[must_use]
831 pub fn round(self) -> Self {
832 Self::new(
833 math::round(self.x),
834 math::round(self.y),
835 math::round(self.z),
836 math::round(self.w),
837 )
838 }
839
840 #[inline]
843 #[must_use]
844 pub fn floor(self) -> Self {
845 Self::new(
846 math::floor(self.x),
847 math::floor(self.y),
848 math::floor(self.z),
849 math::floor(self.w),
850 )
851 }
852
853 #[inline]
856 #[must_use]
857 pub fn ceil(self) -> Self {
858 Self::new(
859 math::ceil(self.x),
860 math::ceil(self.y),
861 math::ceil(self.z),
862 math::ceil(self.w),
863 )
864 }
865
866 #[inline]
869 #[must_use]
870 pub fn trunc(self) -> Self {
871 Self::new(
872 math::trunc(self.x),
873 math::trunc(self.y),
874 math::trunc(self.z),
875 math::trunc(self.w),
876 )
877 }
878
879 #[inline]
883 #[must_use]
884 pub fn step(self, rhs: Self) -> Self {
885 Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
886 }
887
888 #[inline]
899 #[must_use]
900 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
901 pub fn smoothstep(self, edge0: Self, edge1: Self) -> Self {
902 glam_assert!(edge0.cmplt(edge1).all());
903 let t = ((self - edge0) / (edge1 - edge0)).saturate();
904 t * t * (Self::splat(3.0) - Self::splat(2.0) * t)
905 }
906
907 #[inline]
909 #[must_use]
910 pub fn saturate(self) -> Self {
911 self.clamp(Self::ZERO, Self::ONE)
912 }
913
914 #[inline]
921 #[must_use]
922 pub fn fract(self) -> Self {
923 self - self.trunc()
924 }
925
926 #[inline]
933 #[must_use]
934 pub fn fract_gl(self) -> Self {
935 self - self.floor()
936 }
937
938 #[inline]
941 #[must_use]
942 pub fn exp(self) -> Self {
943 Self::new(
944 math::exp(self.x),
945 math::exp(self.y),
946 math::exp(self.z),
947 math::exp(self.w),
948 )
949 }
950
951 #[inline]
953 #[must_use]
954 pub fn exp2(self) -> Self {
955 Self::new(
956 math::exp2(self.x),
957 math::exp2(self.y),
958 math::exp2(self.z),
959 math::exp2(self.w),
960 )
961 }
962
963 #[inline]
966 #[must_use]
967 pub fn ln(self) -> Self {
968 Self::new(
969 math::ln(self.x),
970 math::ln(self.y),
971 math::ln(self.z),
972 math::ln(self.w),
973 )
974 }
975
976 #[inline]
979 #[must_use]
980 pub fn log2(self) -> Self {
981 Self::new(
982 math::log2(self.x),
983 math::log2(self.y),
984 math::log2(self.z),
985 math::log2(self.w),
986 )
987 }
988
989 #[inline]
991 #[must_use]
992 pub fn powf(self, n: f64) -> Self {
993 Self::new(
994 math::powf(self.x, n),
995 math::powf(self.y, n),
996 math::powf(self.z, n),
997 math::powf(self.w, n),
998 )
999 }
1000
1001 #[inline]
1004 #[must_use]
1005 pub fn sqrt(self) -> Self {
1006 Self::new(
1007 math::sqrt(self.x),
1008 math::sqrt(self.y),
1009 math::sqrt(self.z),
1010 math::sqrt(self.w),
1011 )
1012 }
1013
1014 #[inline]
1016 #[must_use]
1017 pub fn cos(self) -> Self {
1018 Self::new(
1019 math::cos(self.x),
1020 math::cos(self.y),
1021 math::cos(self.z),
1022 math::cos(self.w),
1023 )
1024 }
1025
1026 #[inline]
1028 #[must_use]
1029 pub fn sin(self) -> Self {
1030 Self::new(
1031 math::sin(self.x),
1032 math::sin(self.y),
1033 math::sin(self.z),
1034 math::sin(self.w),
1035 )
1036 }
1037
1038 #[inline]
1040 #[must_use]
1041 pub fn sin_cos(self) -> (Self, Self) {
1042 let (sin_x, cos_x) = math::sin_cos(self.x);
1043 let (sin_y, cos_y) = math::sin_cos(self.y);
1044 let (sin_z, cos_z) = math::sin_cos(self.z);
1045 let (sin_w, cos_w) = math::sin_cos(self.w);
1046
1047 (
1048 Self::new(sin_x, sin_y, sin_z, sin_w),
1049 Self::new(cos_x, cos_y, cos_z, cos_w),
1050 )
1051 }
1052
1053 #[inline]
1055 #[must_use]
1056 pub fn recip(self) -> Self {
1057 Self::new(1.0 / self.x, 1.0 / self.y, 1.0 / self.z, 1.0 / self.w)
1058 }
1059
1060 #[doc(alias = "mix")]
1072 #[inline]
1073 #[must_use]
1074 pub fn lerp(self, rhs: Self, s: f64) -> Self {
1075 self * (1.0 - s) + rhs * s
1076 }
1077
1078 #[doc(alias = "mix")]
1095 #[inline]
1096 #[must_use]
1097 pub fn lerp_monotonic(self, rhs: Self, s: f64) -> Self {
1098 self + (rhs - self) * s
1099 }
1100
1101 #[inline]
1106 #[must_use]
1107 pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1108 let a = rhs - self;
1109 let len = a.length();
1110 if len <= d || len <= 1e-4 {
1111 return rhs;
1112 }
1113 self + a / len * d
1114 }
1115
1116 #[inline]
1122 pub fn midpoint(self, rhs: Self) -> Self {
1123 (self + rhs) * 0.5
1124 }
1125
1126 #[inline]
1136 #[must_use]
1137 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1138 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1139 }
1140
1141 #[inline]
1147 #[must_use]
1148 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1149 pub fn clamp_length(self, min: f64, max: f64) -> Self {
1150 glam_assert!(0.0 <= min);
1151 glam_assert!(min <= max);
1152 let length_sq = self.length_squared();
1153 if length_sq < min * min {
1154 min * (self / math::sqrt(length_sq))
1155 } else if length_sq > max * max {
1156 max * (self / math::sqrt(length_sq))
1157 } else {
1158 self
1159 }
1160 }
1161
1162 #[inline]
1168 #[must_use]
1169 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1170 pub fn clamp_length_max(self, max: f64) -> Self {
1171 glam_assert!(0.0 <= max);
1172 let length_sq = self.length_squared();
1173 if length_sq > max * max {
1174 max * (self / math::sqrt(length_sq))
1175 } else {
1176 self
1177 }
1178 }
1179
1180 #[inline]
1186 #[must_use]
1187 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1188 pub fn clamp_length_min(self, min: f64) -> Self {
1189 glam_assert!(0.0 <= min);
1190 let length_sq = self.length_squared();
1191 if length_sq < min * min {
1192 min * (self / math::sqrt(length_sq))
1193 } else {
1194 self
1195 }
1196 }
1197
1198 #[inline]
1206 #[must_use]
1207 pub fn mul_add(self, a: Self, b: Self) -> Self {
1208 Self::new(
1209 math::mul_add(self.x, a.x, b.x),
1210 math::mul_add(self.y, a.y, b.y),
1211 math::mul_add(self.z, a.z, b.z),
1212 math::mul_add(self.w, a.w, b.w),
1213 )
1214 }
1215
1216 #[inline]
1225 #[must_use]
1226 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1227 pub fn reflect(self, normal: Self) -> Self {
1228 glam_assert!(normal.is_normalized());
1229 self - 2.0 * self.dot(normal) * normal
1230 }
1231
1232 #[inline]
1242 #[must_use]
1243 #[cfg_attr(any(debug_assertions, feature = "glam-assert"), track_caller)]
1244 pub fn refract(self, normal: Self, eta: f64) -> Self {
1245 glam_assert!(self.is_normalized());
1246 glam_assert!(normal.is_normalized());
1247 let n_dot_i = normal.dot(self);
1248 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1249 if k >= 0.0 {
1250 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1251 } else {
1252 Self::ZERO
1253 }
1254 }
1255
1256 #[inline]
1258 #[must_use]
1259 pub fn as_vec4(self) -> crate::Vec4 {
1260 crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
1261 }
1262
1263 #[cfg(feature = "i8")]
1265 #[inline]
1266 #[must_use]
1267 pub fn as_i8vec4(self) -> crate::I8Vec4 {
1268 crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1269 }
1270
1271 #[cfg(feature = "u8")]
1273 #[inline]
1274 #[must_use]
1275 pub fn as_u8vec4(self) -> crate::U8Vec4 {
1276 crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1277 }
1278
1279 #[cfg(feature = "i16")]
1281 #[inline]
1282 #[must_use]
1283 pub fn as_i16vec4(self) -> crate::I16Vec4 {
1284 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1285 }
1286
1287 #[cfg(feature = "u16")]
1289 #[inline]
1290 #[must_use]
1291 pub fn as_u16vec4(self) -> crate::U16Vec4 {
1292 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1293 }
1294
1295 #[cfg(feature = "i32")]
1297 #[inline]
1298 #[must_use]
1299 pub fn as_ivec4(self) -> crate::IVec4 {
1300 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1301 }
1302
1303 #[cfg(feature = "u32")]
1305 #[inline]
1306 #[must_use]
1307 pub fn as_uvec4(self) -> crate::UVec4 {
1308 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1309 }
1310
1311 #[cfg(feature = "i64")]
1313 #[inline]
1314 #[must_use]
1315 pub fn as_i64vec4(self) -> crate::I64Vec4 {
1316 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1317 }
1318
1319 #[cfg(feature = "u64")]
1321 #[inline]
1322 #[must_use]
1323 pub fn as_u64vec4(self) -> crate::U64Vec4 {
1324 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1325 }
1326
1327 #[cfg(feature = "isize")]
1329 #[inline]
1330 #[must_use]
1331 pub fn as_isizevec4(self) -> crate::ISizeVec4 {
1332 crate::ISizeVec4::new(
1333 self.x as isize,
1334 self.y as isize,
1335 self.z as isize,
1336 self.w as isize,
1337 )
1338 }
1339
1340 #[cfg(feature = "usize")]
1342 #[inline]
1343 #[must_use]
1344 pub fn as_usizevec4(self) -> crate::USizeVec4 {
1345 crate::USizeVec4::new(
1346 self.x as usize,
1347 self.y as usize,
1348 self.z as usize,
1349 self.w as usize,
1350 )
1351 }
1352}
1353
1354impl Default for DVec4 {
1355 #[inline(always)]
1356 fn default() -> Self {
1357 Self::ZERO
1358 }
1359}
1360
1361impl Div for DVec4 {
1362 type Output = Self;
1363 #[inline]
1364 fn div(self, rhs: Self) -> Self {
1365 Self::new(
1366 self.x.div(rhs.x),
1367 self.y.div(rhs.y),
1368 self.z.div(rhs.z),
1369 self.w.div(rhs.w),
1370 )
1371 }
1372}
1373
1374impl Div<&Self> for DVec4 {
1375 type Output = Self;
1376 #[inline]
1377 fn div(self, rhs: &Self) -> Self {
1378 self.div(*rhs)
1379 }
1380}
1381
1382impl Div<&DVec4> for &DVec4 {
1383 type Output = DVec4;
1384 #[inline]
1385 fn div(self, rhs: &DVec4) -> DVec4 {
1386 (*self).div(*rhs)
1387 }
1388}
1389
1390impl Div<DVec4> for &DVec4 {
1391 type Output = DVec4;
1392 #[inline]
1393 fn div(self, rhs: DVec4) -> DVec4 {
1394 (*self).div(rhs)
1395 }
1396}
1397
1398impl DivAssign for DVec4 {
1399 #[inline]
1400 fn div_assign(&mut self, rhs: Self) {
1401 self.x.div_assign(rhs.x);
1402 self.y.div_assign(rhs.y);
1403 self.z.div_assign(rhs.z);
1404 self.w.div_assign(rhs.w);
1405 }
1406}
1407
1408impl DivAssign<&Self> for DVec4 {
1409 #[inline]
1410 fn div_assign(&mut self, rhs: &Self) {
1411 self.div_assign(*rhs);
1412 }
1413}
1414
1415impl Div<f64> for DVec4 {
1416 type Output = Self;
1417 #[inline]
1418 fn div(self, rhs: f64) -> Self {
1419 Self::new(
1420 self.x.div(rhs),
1421 self.y.div(rhs),
1422 self.z.div(rhs),
1423 self.w.div(rhs),
1424 )
1425 }
1426}
1427
1428impl Div<&f64> for DVec4 {
1429 type Output = Self;
1430 #[inline]
1431 fn div(self, rhs: &f64) -> Self {
1432 self.div(*rhs)
1433 }
1434}
1435
1436impl Div<&f64> for &DVec4 {
1437 type Output = DVec4;
1438 #[inline]
1439 fn div(self, rhs: &f64) -> DVec4 {
1440 (*self).div(*rhs)
1441 }
1442}
1443
1444impl Div<f64> for &DVec4 {
1445 type Output = DVec4;
1446 #[inline]
1447 fn div(self, rhs: f64) -> DVec4 {
1448 (*self).div(rhs)
1449 }
1450}
1451
1452impl DivAssign<f64> for DVec4 {
1453 #[inline]
1454 fn div_assign(&mut self, rhs: f64) {
1455 self.x.div_assign(rhs);
1456 self.y.div_assign(rhs);
1457 self.z.div_assign(rhs);
1458 self.w.div_assign(rhs);
1459 }
1460}
1461
1462impl DivAssign<&f64> for DVec4 {
1463 #[inline]
1464 fn div_assign(&mut self, rhs: &f64) {
1465 self.div_assign(*rhs);
1466 }
1467}
1468
1469impl Div<DVec4> for f64 {
1470 type Output = DVec4;
1471 #[inline]
1472 fn div(self, rhs: DVec4) -> DVec4 {
1473 DVec4::new(
1474 self.div(rhs.x),
1475 self.div(rhs.y),
1476 self.div(rhs.z),
1477 self.div(rhs.w),
1478 )
1479 }
1480}
1481
1482impl Div<&DVec4> for f64 {
1483 type Output = DVec4;
1484 #[inline]
1485 fn div(self, rhs: &DVec4) -> DVec4 {
1486 self.div(*rhs)
1487 }
1488}
1489
1490impl Div<&DVec4> for &f64 {
1491 type Output = DVec4;
1492 #[inline]
1493 fn div(self, rhs: &DVec4) -> DVec4 {
1494 (*self).div(*rhs)
1495 }
1496}
1497
1498impl Div<DVec4> for &f64 {
1499 type Output = DVec4;
1500 #[inline]
1501 fn div(self, rhs: DVec4) -> DVec4 {
1502 (*self).div(rhs)
1503 }
1504}
1505
1506impl Mul for DVec4 {
1507 type Output = Self;
1508 #[inline]
1509 fn mul(self, rhs: Self) -> Self {
1510 Self::new(
1511 self.x.mul(rhs.x),
1512 self.y.mul(rhs.y),
1513 self.z.mul(rhs.z),
1514 self.w.mul(rhs.w),
1515 )
1516 }
1517}
1518
1519impl Mul<&Self> for DVec4 {
1520 type Output = Self;
1521 #[inline]
1522 fn mul(self, rhs: &Self) -> Self {
1523 self.mul(*rhs)
1524 }
1525}
1526
1527impl Mul<&DVec4> for &DVec4 {
1528 type Output = DVec4;
1529 #[inline]
1530 fn mul(self, rhs: &DVec4) -> DVec4 {
1531 (*self).mul(*rhs)
1532 }
1533}
1534
1535impl Mul<DVec4> for &DVec4 {
1536 type Output = DVec4;
1537 #[inline]
1538 fn mul(self, rhs: DVec4) -> DVec4 {
1539 (*self).mul(rhs)
1540 }
1541}
1542
1543impl MulAssign for DVec4 {
1544 #[inline]
1545 fn mul_assign(&mut self, rhs: Self) {
1546 self.x.mul_assign(rhs.x);
1547 self.y.mul_assign(rhs.y);
1548 self.z.mul_assign(rhs.z);
1549 self.w.mul_assign(rhs.w);
1550 }
1551}
1552
1553impl MulAssign<&Self> for DVec4 {
1554 #[inline]
1555 fn mul_assign(&mut self, rhs: &Self) {
1556 self.mul_assign(*rhs);
1557 }
1558}
1559
1560impl Mul<f64> for DVec4 {
1561 type Output = Self;
1562 #[inline]
1563 fn mul(self, rhs: f64) -> Self {
1564 Self::new(
1565 self.x.mul(rhs),
1566 self.y.mul(rhs),
1567 self.z.mul(rhs),
1568 self.w.mul(rhs),
1569 )
1570 }
1571}
1572
1573impl Mul<&f64> for DVec4 {
1574 type Output = Self;
1575 #[inline]
1576 fn mul(self, rhs: &f64) -> Self {
1577 self.mul(*rhs)
1578 }
1579}
1580
1581impl Mul<&f64> for &DVec4 {
1582 type Output = DVec4;
1583 #[inline]
1584 fn mul(self, rhs: &f64) -> DVec4 {
1585 (*self).mul(*rhs)
1586 }
1587}
1588
1589impl Mul<f64> for &DVec4 {
1590 type Output = DVec4;
1591 #[inline]
1592 fn mul(self, rhs: f64) -> DVec4 {
1593 (*self).mul(rhs)
1594 }
1595}
1596
1597impl MulAssign<f64> for DVec4 {
1598 #[inline]
1599 fn mul_assign(&mut self, rhs: f64) {
1600 self.x.mul_assign(rhs);
1601 self.y.mul_assign(rhs);
1602 self.z.mul_assign(rhs);
1603 self.w.mul_assign(rhs);
1604 }
1605}
1606
1607impl MulAssign<&f64> for DVec4 {
1608 #[inline]
1609 fn mul_assign(&mut self, rhs: &f64) {
1610 self.mul_assign(*rhs);
1611 }
1612}
1613
1614impl Mul<DVec4> for f64 {
1615 type Output = DVec4;
1616 #[inline]
1617 fn mul(self, rhs: DVec4) -> DVec4 {
1618 DVec4::new(
1619 self.mul(rhs.x),
1620 self.mul(rhs.y),
1621 self.mul(rhs.z),
1622 self.mul(rhs.w),
1623 )
1624 }
1625}
1626
1627impl Mul<&DVec4> for f64 {
1628 type Output = DVec4;
1629 #[inline]
1630 fn mul(self, rhs: &DVec4) -> DVec4 {
1631 self.mul(*rhs)
1632 }
1633}
1634
1635impl Mul<&DVec4> for &f64 {
1636 type Output = DVec4;
1637 #[inline]
1638 fn mul(self, rhs: &DVec4) -> DVec4 {
1639 (*self).mul(*rhs)
1640 }
1641}
1642
1643impl Mul<DVec4> for &f64 {
1644 type Output = DVec4;
1645 #[inline]
1646 fn mul(self, rhs: DVec4) -> DVec4 {
1647 (*self).mul(rhs)
1648 }
1649}
1650
1651impl Add for DVec4 {
1652 type Output = Self;
1653 #[inline]
1654 fn add(self, rhs: Self) -> Self {
1655 Self::new(
1656 self.x.add(rhs.x),
1657 self.y.add(rhs.y),
1658 self.z.add(rhs.z),
1659 self.w.add(rhs.w),
1660 )
1661 }
1662}
1663
1664impl Add<&Self> for DVec4 {
1665 type Output = Self;
1666 #[inline]
1667 fn add(self, rhs: &Self) -> Self {
1668 self.add(*rhs)
1669 }
1670}
1671
1672impl Add<&DVec4> for &DVec4 {
1673 type Output = DVec4;
1674 #[inline]
1675 fn add(self, rhs: &DVec4) -> DVec4 {
1676 (*self).add(*rhs)
1677 }
1678}
1679
1680impl Add<DVec4> for &DVec4 {
1681 type Output = DVec4;
1682 #[inline]
1683 fn add(self, rhs: DVec4) -> DVec4 {
1684 (*self).add(rhs)
1685 }
1686}
1687
1688impl AddAssign for DVec4 {
1689 #[inline]
1690 fn add_assign(&mut self, rhs: Self) {
1691 self.x.add_assign(rhs.x);
1692 self.y.add_assign(rhs.y);
1693 self.z.add_assign(rhs.z);
1694 self.w.add_assign(rhs.w);
1695 }
1696}
1697
1698impl AddAssign<&Self> for DVec4 {
1699 #[inline]
1700 fn add_assign(&mut self, rhs: &Self) {
1701 self.add_assign(*rhs);
1702 }
1703}
1704
1705impl Add<f64> for DVec4 {
1706 type Output = Self;
1707 #[inline]
1708 fn add(self, rhs: f64) -> Self {
1709 Self::new(
1710 self.x.add(rhs),
1711 self.y.add(rhs),
1712 self.z.add(rhs),
1713 self.w.add(rhs),
1714 )
1715 }
1716}
1717
1718impl Add<&f64> for DVec4 {
1719 type Output = Self;
1720 #[inline]
1721 fn add(self, rhs: &f64) -> Self {
1722 self.add(*rhs)
1723 }
1724}
1725
1726impl Add<&f64> for &DVec4 {
1727 type Output = DVec4;
1728 #[inline]
1729 fn add(self, rhs: &f64) -> DVec4 {
1730 (*self).add(*rhs)
1731 }
1732}
1733
1734impl Add<f64> for &DVec4 {
1735 type Output = DVec4;
1736 #[inline]
1737 fn add(self, rhs: f64) -> DVec4 {
1738 (*self).add(rhs)
1739 }
1740}
1741
1742impl AddAssign<f64> for DVec4 {
1743 #[inline]
1744 fn add_assign(&mut self, rhs: f64) {
1745 self.x.add_assign(rhs);
1746 self.y.add_assign(rhs);
1747 self.z.add_assign(rhs);
1748 self.w.add_assign(rhs);
1749 }
1750}
1751
1752impl AddAssign<&f64> for DVec4 {
1753 #[inline]
1754 fn add_assign(&mut self, rhs: &f64) {
1755 self.add_assign(*rhs);
1756 }
1757}
1758
1759impl Add<DVec4> for f64 {
1760 type Output = DVec4;
1761 #[inline]
1762 fn add(self, rhs: DVec4) -> DVec4 {
1763 DVec4::new(
1764 self.add(rhs.x),
1765 self.add(rhs.y),
1766 self.add(rhs.z),
1767 self.add(rhs.w),
1768 )
1769 }
1770}
1771
1772impl Add<&DVec4> for f64 {
1773 type Output = DVec4;
1774 #[inline]
1775 fn add(self, rhs: &DVec4) -> DVec4 {
1776 self.add(*rhs)
1777 }
1778}
1779
1780impl Add<&DVec4> for &f64 {
1781 type Output = DVec4;
1782 #[inline]
1783 fn add(self, rhs: &DVec4) -> DVec4 {
1784 (*self).add(*rhs)
1785 }
1786}
1787
1788impl Add<DVec4> for &f64 {
1789 type Output = DVec4;
1790 #[inline]
1791 fn add(self, rhs: DVec4) -> DVec4 {
1792 (*self).add(rhs)
1793 }
1794}
1795
1796impl Sub for DVec4 {
1797 type Output = Self;
1798 #[inline]
1799 fn sub(self, rhs: Self) -> Self {
1800 Self::new(
1801 self.x.sub(rhs.x),
1802 self.y.sub(rhs.y),
1803 self.z.sub(rhs.z),
1804 self.w.sub(rhs.w),
1805 )
1806 }
1807}
1808
1809impl Sub<&Self> for DVec4 {
1810 type Output = Self;
1811 #[inline]
1812 fn sub(self, rhs: &Self) -> Self {
1813 self.sub(*rhs)
1814 }
1815}
1816
1817impl Sub<&DVec4> for &DVec4 {
1818 type Output = DVec4;
1819 #[inline]
1820 fn sub(self, rhs: &DVec4) -> DVec4 {
1821 (*self).sub(*rhs)
1822 }
1823}
1824
1825impl Sub<DVec4> for &DVec4 {
1826 type Output = DVec4;
1827 #[inline]
1828 fn sub(self, rhs: DVec4) -> DVec4 {
1829 (*self).sub(rhs)
1830 }
1831}
1832
1833impl SubAssign for DVec4 {
1834 #[inline]
1835 fn sub_assign(&mut self, rhs: Self) {
1836 self.x.sub_assign(rhs.x);
1837 self.y.sub_assign(rhs.y);
1838 self.z.sub_assign(rhs.z);
1839 self.w.sub_assign(rhs.w);
1840 }
1841}
1842
1843impl SubAssign<&Self> for DVec4 {
1844 #[inline]
1845 fn sub_assign(&mut self, rhs: &Self) {
1846 self.sub_assign(*rhs);
1847 }
1848}
1849
1850impl Sub<f64> for DVec4 {
1851 type Output = Self;
1852 #[inline]
1853 fn sub(self, rhs: f64) -> Self {
1854 Self::new(
1855 self.x.sub(rhs),
1856 self.y.sub(rhs),
1857 self.z.sub(rhs),
1858 self.w.sub(rhs),
1859 )
1860 }
1861}
1862
1863impl Sub<&f64> for DVec4 {
1864 type Output = Self;
1865 #[inline]
1866 fn sub(self, rhs: &f64) -> Self {
1867 self.sub(*rhs)
1868 }
1869}
1870
1871impl Sub<&f64> for &DVec4 {
1872 type Output = DVec4;
1873 #[inline]
1874 fn sub(self, rhs: &f64) -> DVec4 {
1875 (*self).sub(*rhs)
1876 }
1877}
1878
1879impl Sub<f64> for &DVec4 {
1880 type Output = DVec4;
1881 #[inline]
1882 fn sub(self, rhs: f64) -> DVec4 {
1883 (*self).sub(rhs)
1884 }
1885}
1886
1887impl SubAssign<f64> for DVec4 {
1888 #[inline]
1889 fn sub_assign(&mut self, rhs: f64) {
1890 self.x.sub_assign(rhs);
1891 self.y.sub_assign(rhs);
1892 self.z.sub_assign(rhs);
1893 self.w.sub_assign(rhs);
1894 }
1895}
1896
1897impl SubAssign<&f64> for DVec4 {
1898 #[inline]
1899 fn sub_assign(&mut self, rhs: &f64) {
1900 self.sub_assign(*rhs);
1901 }
1902}
1903
1904impl Sub<DVec4> for f64 {
1905 type Output = DVec4;
1906 #[inline]
1907 fn sub(self, rhs: DVec4) -> DVec4 {
1908 DVec4::new(
1909 self.sub(rhs.x),
1910 self.sub(rhs.y),
1911 self.sub(rhs.z),
1912 self.sub(rhs.w),
1913 )
1914 }
1915}
1916
1917impl Sub<&DVec4> for f64 {
1918 type Output = DVec4;
1919 #[inline]
1920 fn sub(self, rhs: &DVec4) -> DVec4 {
1921 self.sub(*rhs)
1922 }
1923}
1924
1925impl Sub<&DVec4> for &f64 {
1926 type Output = DVec4;
1927 #[inline]
1928 fn sub(self, rhs: &DVec4) -> DVec4 {
1929 (*self).sub(*rhs)
1930 }
1931}
1932
1933impl Sub<DVec4> for &f64 {
1934 type Output = DVec4;
1935 #[inline]
1936 fn sub(self, rhs: DVec4) -> DVec4 {
1937 (*self).sub(rhs)
1938 }
1939}
1940
1941impl Rem for DVec4 {
1942 type Output = Self;
1943 #[inline]
1944 fn rem(self, rhs: Self) -> Self {
1945 Self::new(
1946 self.x.rem(rhs.x),
1947 self.y.rem(rhs.y),
1948 self.z.rem(rhs.z),
1949 self.w.rem(rhs.w),
1950 )
1951 }
1952}
1953
1954impl Rem<&Self> for DVec4 {
1955 type Output = Self;
1956 #[inline]
1957 fn rem(self, rhs: &Self) -> Self {
1958 self.rem(*rhs)
1959 }
1960}
1961
1962impl Rem<&DVec4> for &DVec4 {
1963 type Output = DVec4;
1964 #[inline]
1965 fn rem(self, rhs: &DVec4) -> DVec4 {
1966 (*self).rem(*rhs)
1967 }
1968}
1969
1970impl Rem<DVec4> for &DVec4 {
1971 type Output = DVec4;
1972 #[inline]
1973 fn rem(self, rhs: DVec4) -> DVec4 {
1974 (*self).rem(rhs)
1975 }
1976}
1977
1978impl RemAssign for DVec4 {
1979 #[inline]
1980 fn rem_assign(&mut self, rhs: Self) {
1981 self.x.rem_assign(rhs.x);
1982 self.y.rem_assign(rhs.y);
1983 self.z.rem_assign(rhs.z);
1984 self.w.rem_assign(rhs.w);
1985 }
1986}
1987
1988impl RemAssign<&Self> for DVec4 {
1989 #[inline]
1990 fn rem_assign(&mut self, rhs: &Self) {
1991 self.rem_assign(*rhs);
1992 }
1993}
1994
1995impl Rem<f64> for DVec4 {
1996 type Output = Self;
1997 #[inline]
1998 fn rem(self, rhs: f64) -> Self {
1999 Self::new(
2000 self.x.rem(rhs),
2001 self.y.rem(rhs),
2002 self.z.rem(rhs),
2003 self.w.rem(rhs),
2004 )
2005 }
2006}
2007
2008impl Rem<&f64> for DVec4 {
2009 type Output = Self;
2010 #[inline]
2011 fn rem(self, rhs: &f64) -> Self {
2012 self.rem(*rhs)
2013 }
2014}
2015
2016impl Rem<&f64> for &DVec4 {
2017 type Output = DVec4;
2018 #[inline]
2019 fn rem(self, rhs: &f64) -> DVec4 {
2020 (*self).rem(*rhs)
2021 }
2022}
2023
2024impl Rem<f64> for &DVec4 {
2025 type Output = DVec4;
2026 #[inline]
2027 fn rem(self, rhs: f64) -> DVec4 {
2028 (*self).rem(rhs)
2029 }
2030}
2031
2032impl RemAssign<f64> for DVec4 {
2033 #[inline]
2034 fn rem_assign(&mut self, rhs: f64) {
2035 self.x.rem_assign(rhs);
2036 self.y.rem_assign(rhs);
2037 self.z.rem_assign(rhs);
2038 self.w.rem_assign(rhs);
2039 }
2040}
2041
2042impl RemAssign<&f64> for DVec4 {
2043 #[inline]
2044 fn rem_assign(&mut self, rhs: &f64) {
2045 self.rem_assign(*rhs);
2046 }
2047}
2048
2049impl Rem<DVec4> for f64 {
2050 type Output = DVec4;
2051 #[inline]
2052 fn rem(self, rhs: DVec4) -> DVec4 {
2053 DVec4::new(
2054 self.rem(rhs.x),
2055 self.rem(rhs.y),
2056 self.rem(rhs.z),
2057 self.rem(rhs.w),
2058 )
2059 }
2060}
2061
2062impl Rem<&DVec4> for f64 {
2063 type Output = DVec4;
2064 #[inline]
2065 fn rem(self, rhs: &DVec4) -> DVec4 {
2066 self.rem(*rhs)
2067 }
2068}
2069
2070impl Rem<&DVec4> for &f64 {
2071 type Output = DVec4;
2072 #[inline]
2073 fn rem(self, rhs: &DVec4) -> DVec4 {
2074 (*self).rem(*rhs)
2075 }
2076}
2077
2078impl Rem<DVec4> for &f64 {
2079 type Output = DVec4;
2080 #[inline]
2081 fn rem(self, rhs: DVec4) -> DVec4 {
2082 (*self).rem(rhs)
2083 }
2084}
2085
2086impl AsRef<[f64; 4]> for DVec4 {
2087 #[inline]
2088 fn as_ref(&self) -> &[f64; 4] {
2089 unsafe { &*(self as *const Self as *const [f64; 4]) }
2090 }
2091}
2092
2093impl AsMut<[f64; 4]> for DVec4 {
2094 #[inline]
2095 fn as_mut(&mut self) -> &mut [f64; 4] {
2096 unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
2097 }
2098}
2099
2100impl Sum for DVec4 {
2101 #[inline]
2102 fn sum<I>(iter: I) -> Self
2103 where
2104 I: Iterator<Item = Self>,
2105 {
2106 iter.fold(Self::ZERO, Self::add)
2107 }
2108}
2109
2110impl<'a> Sum<&'a Self> for DVec4 {
2111 #[inline]
2112 fn sum<I>(iter: I) -> Self
2113 where
2114 I: Iterator<Item = &'a Self>,
2115 {
2116 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2117 }
2118}
2119
2120impl Product for DVec4 {
2121 #[inline]
2122 fn product<I>(iter: I) -> Self
2123 where
2124 I: Iterator<Item = Self>,
2125 {
2126 iter.fold(Self::ONE, Self::mul)
2127 }
2128}
2129
2130impl<'a> Product<&'a Self> for DVec4 {
2131 #[inline]
2132 fn product<I>(iter: I) -> Self
2133 where
2134 I: Iterator<Item = &'a Self>,
2135 {
2136 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2137 }
2138}
2139
2140impl Neg for DVec4 {
2141 type Output = Self;
2142 #[inline]
2143 fn neg(self) -> Self {
2144 Self::new(self.x.neg(), self.y.neg(), self.z.neg(), self.w.neg())
2145 }
2146}
2147
2148impl Neg for &DVec4 {
2149 type Output = DVec4;
2150 #[inline]
2151 fn neg(self) -> DVec4 {
2152 (*self).neg()
2153 }
2154}
2155
2156impl Index<usize> for DVec4 {
2157 type Output = f64;
2158 #[inline]
2159 #[track_caller]
2160 fn index(&self, index: usize) -> &Self::Output {
2161 match index {
2162 0 => &self.x,
2163 1 => &self.y,
2164 2 => &self.z,
2165 3 => &self.w,
2166 _ => panic!("index out of bounds"),
2167 }
2168 }
2169}
2170
2171impl IndexMut<usize> for DVec4 {
2172 #[inline]
2173 #[track_caller]
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 3 => &mut self.w,
2180 _ => panic!("index out of bounds"),
2181 }
2182 }
2183}
2184
2185impl fmt::Display for DVec4 {
2186 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2187 if let Some(p) = f.precision() {
2188 write!(
2189 f,
2190 "[{:.*}, {:.*}, {:.*}, {:.*}]",
2191 p, self.x, p, self.y, p, self.z, p, self.w
2192 )
2193 } else {
2194 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
2195 }
2196 }
2197}
2198
2199impl fmt::Debug for DVec4 {
2200 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2201 fmt.debug_tuple(stringify!(DVec4))
2202 .field(&self.x)
2203 .field(&self.y)
2204 .field(&self.z)
2205 .field(&self.w)
2206 .finish()
2207 }
2208}
2209
2210impl From<[f64; 4]> for DVec4 {
2211 #[inline]
2212 fn from(a: [f64; 4]) -> Self {
2213 Self::new(a[0], a[1], a[2], a[3])
2214 }
2215}
2216
2217impl From<DVec4> for [f64; 4] {
2218 #[inline]
2219 fn from(v: DVec4) -> Self {
2220 [v.x, v.y, v.z, v.w]
2221 }
2222}
2223
2224impl From<(f64, f64, f64, f64)> for DVec4 {
2225 #[inline]
2226 fn from(t: (f64, f64, f64, f64)) -> Self {
2227 Self::new(t.0, t.1, t.2, t.3)
2228 }
2229}
2230
2231impl From<DVec4> for (f64, f64, f64, f64) {
2232 #[inline]
2233 fn from(v: DVec4) -> Self {
2234 (v.x, v.y, v.z, v.w)
2235 }
2236}
2237
2238impl From<(DVec3, f64)> for DVec4 {
2239 #[inline]
2240 fn from((v, w): (DVec3, f64)) -> Self {
2241 Self::new(v.x, v.y, v.z, w)
2242 }
2243}
2244
2245impl From<(f64, DVec3)> for DVec4 {
2246 #[inline]
2247 fn from((x, v): (f64, DVec3)) -> Self {
2248 Self::new(x, v.x, v.y, v.z)
2249 }
2250}
2251
2252impl From<(DVec2, f64, f64)> for DVec4 {
2253 #[inline]
2254 fn from((v, z, w): (DVec2, f64, f64)) -> Self {
2255 Self::new(v.x, v.y, z, w)
2256 }
2257}
2258
2259impl From<(DVec2, DVec2)> for DVec4 {
2260 #[inline]
2261 fn from((v, u): (DVec2, DVec2)) -> Self {
2262 Self::new(v.x, v.y, u.x, u.y)
2263 }
2264}
2265
2266impl From<Vec4> for DVec4 {
2267 #[inline]
2268 fn from(v: Vec4) -> Self {
2269 Self::new(
2270 f64::from(v.x),
2271 f64::from(v.y),
2272 f64::from(v.z),
2273 f64::from(v.w),
2274 )
2275 }
2276}
2277
2278#[cfg(feature = "i32")]
2279impl From<IVec4> for DVec4 {
2280 #[inline]
2281 fn from(v: IVec4) -> Self {
2282 Self::new(
2283 f64::from(v.x),
2284 f64::from(v.y),
2285 f64::from(v.z),
2286 f64::from(v.w),
2287 )
2288 }
2289}
2290
2291#[cfg(feature = "u32")]
2292impl From<UVec4> for DVec4 {
2293 #[inline]
2294 fn from(v: UVec4) -> Self {
2295 Self::new(
2296 f64::from(v.x),
2297 f64::from(v.y),
2298 f64::from(v.z),
2299 f64::from(v.w),
2300 )
2301 }
2302}
2303
2304impl From<BVec4> for DVec4 {
2305 #[inline]
2306 fn from(v: BVec4) -> Self {
2307 Self::new(
2308 f64::from(v.x),
2309 f64::from(v.y),
2310 f64::from(v.z),
2311 f64::from(v.w),
2312 )
2313 }
2314}
2315
2316#[cfg(not(feature = "scalar-math"))]
2317impl From<BVec4A> for DVec4 {
2318 #[inline]
2319 fn from(v: BVec4A) -> Self {
2320 let bool_array: [bool; 4] = v.into();
2321 Self::new(
2322 f64::from(bool_array[0]),
2323 f64::from(bool_array[1]),
2324 f64::from(bool_array[2]),
2325 f64::from(bool_array[3]),
2326 )
2327 }
2328}