glam/f64/
dvec4.rs

1// Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3#[cfg(not(feature = "scalar-math"))]
4use crate::BVec4A;
5use crate::{f64::math, BVec4, DVec2, DVec3, IVec4, UVec4, Vec4};
6
7use core::fmt;
8use core::iter::{Product, Sum};
9use core::{f32, ops::*};
10
11/// Creates a 4-dimensional vector.
12#[inline(always)]
13#[must_use]
14pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 {
15    DVec4::new(x, y, z, w)
16}
17
18/// A 4-dimensional vector.
19#[derive(Clone, Copy, PartialEq)]
20#[cfg_attr(feature = "cuda", repr(align(16)))]
21#[cfg_attr(not(target_arch = "spirv"), repr(C))]
22#[cfg_attr(target_arch = "spirv", repr(simd))]
23pub struct DVec4 {
24    pub x: f64,
25    pub y: f64,
26    pub z: f64,
27    pub w: f64,
28}
29
30impl DVec4 {
31    /// All zeroes.
32    pub const ZERO: Self = Self::splat(0.0);
33
34    /// All ones.
35    pub const ONE: Self = Self::splat(1.0);
36
37    /// All negative ones.
38    pub const NEG_ONE: Self = Self::splat(-1.0);
39
40    /// All `f64::MIN`.
41    pub const MIN: Self = Self::splat(f64::MIN);
42
43    /// All `f64::MAX`.
44    pub const MAX: Self = Self::splat(f64::MAX);
45
46    /// All `f64::NAN`.
47    pub const NAN: Self = Self::splat(f64::NAN);
48
49    /// All `f64::INFINITY`.
50    pub const INFINITY: Self = Self::splat(f64::INFINITY);
51
52    /// All `f64::NEG_INFINITY`.
53    pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
54
55    /// A unit vector pointing along the positive X axis.
56    pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
57
58    /// A unit vector pointing along the positive Y axis.
59    pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
60
61    /// A unit vector pointing along the positive Z axis.
62    pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
63
64    /// A unit vector pointing along the positive W axis.
65    pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
66
67    /// A unit vector pointing along the negative X axis.
68    pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
69
70    /// A unit vector pointing along the negative Y axis.
71    pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
72
73    /// A unit vector pointing along the negative Z axis.
74    pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
75
76    /// A unit vector pointing along the negative W axis.
77    pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
78
79    /// The unit axes.
80    pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
81
82    /// DVec4 uses Rust Portable SIMD
83    pub const USES_CORE_SIMD: bool = false;
84    /// DVec4 uses Arm NEON
85    pub const USES_NEON: bool = false;
86    /// DVec4 uses scalar math
87    pub const USES_SCALAR_MATH: bool = true;
88    /// DVec4 uses Intel SSE2
89    pub const USES_SSE2: bool = false;
90    /// DVec4 uses WebAssembly 128-bit SIMD
91    pub const USES_WASM32_SIMD: bool = false;
92
93    /// Creates a new vector.
94    #[inline(always)]
95    #[must_use]
96    pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
97        Self { x, y, z, w }
98    }
99
100    /// Creates a vector with all elements set to `v`.
101    #[inline]
102    #[must_use]
103    pub const fn splat(v: f64) -> Self {
104        Self {
105            x: v,
106
107            y: v,
108
109            z: v,
110
111            w: v,
112        }
113    }
114
115    /// Returns a vector containing each element of `self` modified by a mapping function `f`.
116    #[inline]
117    #[must_use]
118    pub fn map<F>(self, f: F) -> Self
119    where
120        F: Fn(f64) -> f64,
121    {
122        Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
123    }
124
125    /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
126    /// for each element of `self`.
127    ///
128    /// A true element in the mask uses the corresponding element from `if_true`, and false
129    /// uses the element from `if_false`.
130    #[inline]
131    #[must_use]
132    pub fn select(mask: BVec4, 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            w: if mask.test(3) { if_true.w } else { if_false.w },
138        }
139    }
140
141    /// Creates a new vector from an array.
142    #[inline]
143    #[must_use]
144    pub const fn from_array(a: [f64; 4]) -> Self {
145        Self::new(a[0], a[1], a[2], a[3])
146    }
147
148    /// `[x, y, z, w]`
149    #[inline]
150    #[must_use]
151    pub const fn to_array(&self) -> [f64; 4] {
152        [self.x, self.y, self.z, self.w]
153    }
154
155    /// Creates a vector from the first 4 values in `slice`.
156    ///
157    /// # Panics
158    ///
159    /// Panics if `slice` is less than 4 elements long.
160    #[inline]
161    #[must_use]
162    pub const fn from_slice(slice: &[f64]) -> Self {
163        assert!(slice.len() >= 4);
164        Self::new(slice[0], slice[1], slice[2], slice[3])
165    }
166
167    /// Writes the elements of `self` to the first 4 elements in `slice`.
168    ///
169    /// # Panics
170    ///
171    /// Panics if `slice` is less than 4 elements long.
172    #[inline]
173    pub fn write_to_slice(self, slice: &mut [f64]) {
174        slice[..4].copy_from_slice(&self.to_array());
175    }
176
177    /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
178    ///
179    /// Truncation to [`DVec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
180    #[inline]
181    #[must_use]
182    pub fn truncate(self) -> DVec3 {
183        use crate::swizzles::Vec4Swizzles;
184        self.xyz()
185    }
186
187    /// Creates a 4D vector from `self` with the given value of `x`.
188    #[inline]
189    #[must_use]
190    pub fn with_x(mut self, x: f64) -> Self {
191        self.x = x;
192        self
193    }
194
195    /// Creates a 4D vector from `self` with the given value of `y`.
196    #[inline]
197    #[must_use]
198    pub fn with_y(mut self, y: f64) -> Self {
199        self.y = y;
200        self
201    }
202
203    /// Creates a 4D vector from `self` with the given value of `z`.
204    #[inline]
205    #[must_use]
206    pub fn with_z(mut self, z: f64) -> Self {
207        self.z = z;
208        self
209    }
210
211    /// Creates a 4D vector from `self` with the given value of `w`.
212    #[inline]
213    #[must_use]
214    pub fn with_w(mut self, w: f64) -> Self {
215        self.w = w;
216        self
217    }
218
219    /// Computes the dot product of `self` and `rhs`.
220    #[inline]
221    #[must_use]
222    pub fn dot(self, rhs: Self) -> f64 {
223        (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
224    }
225
226    /// Returns a vector where every component is the dot product of `self` and `rhs`.
227    #[inline]
228    #[must_use]
229    pub fn dot_into_vec(self, rhs: Self) -> Self {
230        Self::splat(self.dot(rhs))
231    }
232
233    /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
234    ///
235    /// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`.
236    ///
237    /// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on
238    /// different SIMD architectures.
239    #[inline]
240    #[must_use]
241    pub fn min(self, rhs: Self) -> Self {
242        Self {
243            x: if self.x < rhs.x { self.x } else { rhs.x },
244            y: if self.y < rhs.y { self.y } else { rhs.y },
245            z: if self.z < rhs.z { self.z } else { rhs.z },
246            w: if self.w < rhs.w { self.w } else { rhs.w },
247        }
248    }
249
250    /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
251    ///
252    /// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`.
253    ///
254    /// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on
255    /// different SIMD architectures.
256    #[inline]
257    #[must_use]
258    pub fn max(self, rhs: Self) -> Self {
259        Self {
260            x: if self.x > rhs.x { self.x } else { rhs.x },
261            y: if self.y > rhs.y { self.y } else { rhs.y },
262            z: if self.z > rhs.z { self.z } else { rhs.z },
263            w: if self.w > rhs.w { self.w } else { rhs.w },
264        }
265    }
266
267    /// Component-wise clamping of values, similar to [`f64::clamp`].
268    ///
269    /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
270    ///
271    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
272    /// different SIMD architectures.
273    ///
274    /// # Panics
275    ///
276    /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
277    #[inline]
278    #[must_use]
279    pub fn clamp(self, min: Self, max: Self) -> Self {
280        glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
281        self.max(min).min(max)
282    }
283
284    /// Returns the horizontal minimum of `self`.
285    ///
286    /// In other words this computes `min(x, y, ..)`.
287    ///
288    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
289    /// different SIMD architectures.
290    #[inline]
291    #[must_use]
292    pub fn min_element(self) -> f64 {
293        let min = |a, b| if a < b { a } else { b };
294        min(self.x, min(self.y, min(self.z, self.w)))
295    }
296
297    /// Returns the horizontal maximum of `self`.
298    ///
299    /// In other words this computes `max(x, y, ..)`.
300    ///
301    /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
302    /// different SIMD architectures.
303    #[inline]
304    #[must_use]
305    pub fn max_element(self) -> f64 {
306        let max = |a, b| if a > b { a } else { b };
307        max(self.x, max(self.y, max(self.z, self.w)))
308    }
309
310    /// Returns the index of the first minimum element of `self`.
311    #[doc(alias = "argmin")]
312    #[inline]
313    #[must_use]
314    pub fn min_position(self) -> usize {
315        let mut min = self.x;
316        let mut index = 0;
317        if self.y < min {
318            min = self.y;
319            index = 1;
320        }
321        if self.z < min {
322            min = self.z;
323            index = 2;
324        }
325        if self.w < min {
326            index = 3;
327        }
328        index
329    }
330
331    /// Returns the index of the first maximum element of `self`.
332    #[doc(alias = "argmax")]
333    #[inline]
334    #[must_use]
335    pub fn max_position(self) -> usize {
336        let mut max = self.x;
337        let mut index = 0;
338        if self.y > max {
339            max = self.y;
340            index = 1;
341        }
342        if self.z > max {
343            max = self.z;
344            index = 2;
345        }
346        if self.w > max {
347            index = 3;
348        }
349        index
350    }
351
352    /// Returns the sum of all elements of `self`.
353    ///
354    /// In other words, this computes `self.x + self.y + ..`.
355    #[inline]
356    #[must_use]
357    pub fn element_sum(self) -> f64 {
358        self.x + self.y + self.z + self.w
359    }
360
361    /// Returns the product of all elements of `self`.
362    ///
363    /// In other words, this computes `self.x * self.y * ..`.
364    #[inline]
365    #[must_use]
366    pub fn element_product(self) -> f64 {
367        self.x * self.y * self.z * self.w
368    }
369
370    /// Returns a vector mask containing the result of a `==` comparison for each element of
371    /// `self` and `rhs`.
372    ///
373    /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
374    /// elements.
375    #[inline]
376    #[must_use]
377    pub fn cmpeq(self, rhs: Self) -> BVec4 {
378        BVec4::new(
379            self.x.eq(&rhs.x),
380            self.y.eq(&rhs.y),
381            self.z.eq(&rhs.z),
382            self.w.eq(&rhs.w),
383        )
384    }
385
386    /// Returns a vector mask containing the result of a `!=` comparison for each element of
387    /// `self` and `rhs`.
388    ///
389    /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
390    /// elements.
391    #[inline]
392    #[must_use]
393    pub fn cmpne(self, rhs: Self) -> BVec4 {
394        BVec4::new(
395            self.x.ne(&rhs.x),
396            self.y.ne(&rhs.y),
397            self.z.ne(&rhs.z),
398            self.w.ne(&rhs.w),
399        )
400    }
401
402    /// Returns a vector mask containing the result of a `>=` comparison for each element of
403    /// `self` and `rhs`.
404    ///
405    /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
406    /// elements.
407    #[inline]
408    #[must_use]
409    pub fn cmpge(self, rhs: Self) -> BVec4 {
410        BVec4::new(
411            self.x.ge(&rhs.x),
412            self.y.ge(&rhs.y),
413            self.z.ge(&rhs.z),
414            self.w.ge(&rhs.w),
415        )
416    }
417
418    /// Returns a vector mask containing the result of a `>` comparison for each element of
419    /// `self` and `rhs`.
420    ///
421    /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
422    /// elements.
423    #[inline]
424    #[must_use]
425    pub fn cmpgt(self, rhs: Self) -> BVec4 {
426        BVec4::new(
427            self.x.gt(&rhs.x),
428            self.y.gt(&rhs.y),
429            self.z.gt(&rhs.z),
430            self.w.gt(&rhs.w),
431        )
432    }
433
434    /// Returns a vector mask containing the result of a `<=` comparison for each element of
435    /// `self` and `rhs`.
436    ///
437    /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
438    /// elements.
439    #[inline]
440    #[must_use]
441    pub fn cmple(self, rhs: Self) -> BVec4 {
442        BVec4::new(
443            self.x.le(&rhs.x),
444            self.y.le(&rhs.y),
445            self.z.le(&rhs.z),
446            self.w.le(&rhs.w),
447        )
448    }
449
450    /// Returns a vector mask containing the result of a `<` comparison for each element of
451    /// `self` and `rhs`.
452    ///
453    /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
454    /// elements.
455    #[inline]
456    #[must_use]
457    pub fn cmplt(self, rhs: Self) -> BVec4 {
458        BVec4::new(
459            self.x.lt(&rhs.x),
460            self.y.lt(&rhs.y),
461            self.z.lt(&rhs.z),
462            self.w.lt(&rhs.w),
463        )
464    }
465
466    /// Returns a vector containing the absolute value of each element of `self`.
467    #[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            w: math::abs(self.w),
475        }
476    }
477
478    /// Returns a vector with elements representing the sign of `self`.
479    ///
480    /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
481    /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
482    /// - `NAN` if the number is `NAN`
483    #[inline]
484    #[must_use]
485    pub fn signum(self) -> Self {
486        Self {
487            x: math::signum(self.x),
488            y: math::signum(self.y),
489            z: math::signum(self.z),
490            w: math::signum(self.w),
491        }
492    }
493
494    /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
495    #[inline]
496    #[must_use]
497    pub fn copysign(self, rhs: Self) -> Self {
498        Self {
499            x: math::copysign(self.x, rhs.x),
500            y: math::copysign(self.y, rhs.y),
501            z: math::copysign(self.z, rhs.z),
502            w: math::copysign(self.w, rhs.w),
503        }
504    }
505
506    /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
507    ///
508    /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
509    /// into the first lowest bit, element `y` into the second, etc.
510    ///
511    /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
512    /// bit and negative infinity.
513    #[inline]
514    #[must_use]
515    pub fn is_negative_bitmask(self) -> u32 {
516        (self.x.is_sign_negative() as u32)
517            | ((self.y.is_sign_negative() as u32) << 1)
518            | ((self.z.is_sign_negative() as u32) << 2)
519            | ((self.w.is_sign_negative() as u32) << 3)
520    }
521
522    /// Returns `true` if, and only if, all elements are finite.  If any element is either
523    /// `NaN`, positive or negative infinity, this will return `false`.
524    #[inline]
525    #[must_use]
526    pub fn is_finite(self) -> bool {
527        self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
528    }
529
530    /// Performs `is_finite` on each element of self, returning a vector mask of the results.
531    ///
532    /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
533    pub fn is_finite_mask(self) -> BVec4 {
534        BVec4::new(
535            self.x.is_finite(),
536            self.y.is_finite(),
537            self.z.is_finite(),
538            self.w.is_finite(),
539        )
540    }
541
542    /// Returns `true` if any elements are `NaN`.
543    #[inline]
544    #[must_use]
545    pub fn is_nan(self) -> bool {
546        self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
547    }
548
549    /// Performs `is_nan` on each element of self, returning a vector mask of the results.
550    ///
551    /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
552    #[inline]
553    #[must_use]
554    pub fn is_nan_mask(self) -> BVec4 {
555        BVec4::new(
556            self.x.is_nan(),
557            self.y.is_nan(),
558            self.z.is_nan(),
559            self.w.is_nan(),
560        )
561    }
562
563    /// Computes the length of `self`.
564    #[doc(alias = "magnitude")]
565    #[inline]
566    #[must_use]
567    pub fn length(self) -> f64 {
568        math::sqrt(self.dot(self))
569    }
570
571    /// Computes the squared length of `self`.
572    ///
573    /// This is faster than `length()` as it avoids a square root operation.
574    #[doc(alias = "magnitude2")]
575    #[inline]
576    #[must_use]
577    pub fn length_squared(self) -> f64 {
578        self.dot(self)
579    }
580
581    /// Computes `1.0 / length()`.
582    ///
583    /// For valid results, `self` must _not_ be of length zero.
584    #[inline]
585    #[must_use]
586    pub fn length_recip(self) -> f64 {
587        self.length().recip()
588    }
589
590    /// Computes the Euclidean distance between two points in space.
591    #[inline]
592    #[must_use]
593    pub fn distance(self, rhs: Self) -> f64 {
594        (self - rhs).length()
595    }
596
597    /// Compute the squared euclidean distance between two points in space.
598    #[inline]
599    #[must_use]
600    pub fn distance_squared(self, rhs: Self) -> f64 {
601        (self - rhs).length_squared()
602    }
603
604    /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
605    #[inline]
606    #[must_use]
607    pub fn div_euclid(self, rhs: Self) -> Self {
608        Self::new(
609            math::div_euclid(self.x, rhs.x),
610            math::div_euclid(self.y, rhs.y),
611            math::div_euclid(self.z, rhs.z),
612            math::div_euclid(self.w, rhs.w),
613        )
614    }
615
616    /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
617    ///
618    /// [Euclidean division]: f64::rem_euclid
619    #[inline]
620    #[must_use]
621    pub fn rem_euclid(self, rhs: Self) -> Self {
622        Self::new(
623            math::rem_euclid(self.x, rhs.x),
624            math::rem_euclid(self.y, rhs.y),
625            math::rem_euclid(self.z, rhs.z),
626            math::rem_euclid(self.w, rhs.w),
627        )
628    }
629
630    /// Returns `self` normalized to length 1.0.
631    ///
632    /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
633    ///
634    /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
635    ///
636    /// Panics
637    ///
638    /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
639    #[inline]
640    #[must_use]
641    pub fn normalize(self) -> Self {
642        #[allow(clippy::let_and_return)]
643        let normalized = self.mul(self.length_recip());
644        glam_assert!(normalized.is_finite());
645        normalized
646    }
647
648    /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
649    ///
650    /// In particular, if the input is zero (or very close to zero), or non-finite,
651    /// the result of this operation will be `None`.
652    ///
653    /// See also [`Self::normalize_or_zero()`].
654    #[inline]
655    #[must_use]
656    pub fn try_normalize(self) -> Option<Self> {
657        let rcp = self.length_recip();
658        if rcp.is_finite() && rcp > 0.0 {
659            Some(self * rcp)
660        } else {
661            None
662        }
663    }
664
665    /// Returns `self` normalized to length 1.0 if possible, else returns a
666    /// fallback value.
667    ///
668    /// In particular, if the input is zero (or very close to zero), or non-finite,
669    /// the result of this operation will be the fallback value.
670    ///
671    /// See also [`Self::try_normalize()`].
672    #[inline]
673    #[must_use]
674    pub fn normalize_or(self, fallback: Self) -> Self {
675        let rcp = self.length_recip();
676        if rcp.is_finite() && rcp > 0.0 {
677            self * rcp
678        } else {
679            fallback
680        }
681    }
682
683    /// Returns `self` normalized to length 1.0 if possible, else returns zero.
684    ///
685    /// In particular, if the input is zero (or very close to zero), or non-finite,
686    /// the result of this operation will be zero.
687    ///
688    /// See also [`Self::try_normalize()`].
689    #[inline]
690    #[must_use]
691    pub fn normalize_or_zero(self) -> Self {
692        self.normalize_or(Self::ZERO)
693    }
694
695    /// Returns `self` normalized to length 1.0 and the length of `self`.
696    ///
697    /// If `self` is zero length then `(Self::X, 0.0)` is returned.
698    #[inline]
699    #[must_use]
700    pub fn normalize_and_length(self) -> (Self, f64) {
701        let length = self.length();
702        let rcp = 1.0 / length;
703        if rcp.is_finite() && rcp > 0.0 {
704            (self * rcp, length)
705        } else {
706            (Self::X, 0.0)
707        }
708    }
709
710    /// Returns whether `self` is length `1.0` or not.
711    ///
712    /// Uses a precision threshold of approximately `1e-4`.
713    #[inline]
714    #[must_use]
715    pub fn is_normalized(self) -> bool {
716        math::abs(self.length_squared() - 1.0) <= 2e-4
717    }
718
719    /// Returns the vector projection of `self` onto `rhs`.
720    ///
721    /// `rhs` must be of non-zero length.
722    ///
723    /// # Panics
724    ///
725    /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
726    #[inline]
727    #[must_use]
728    pub fn project_onto(self, rhs: Self) -> Self {
729        let other_len_sq_rcp = rhs.dot(rhs).recip();
730        glam_assert!(other_len_sq_rcp.is_finite());
731        rhs * self.dot(rhs) * other_len_sq_rcp
732    }
733
734    /// Returns the vector rejection of `self` from `rhs`.
735    ///
736    /// The vector rejection is the vector perpendicular to the projection of `self` onto
737    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
738    ///
739    /// `rhs` must be of non-zero length.
740    ///
741    /// # Panics
742    ///
743    /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
744    #[doc(alias("plane"))]
745    #[inline]
746    #[must_use]
747    pub fn reject_from(self, rhs: Self) -> Self {
748        self - self.project_onto(rhs)
749    }
750
751    /// Returns the vector projection of `self` onto `rhs`.
752    ///
753    /// `rhs` must be normalized.
754    ///
755    /// # Panics
756    ///
757    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
758    #[inline]
759    #[must_use]
760    pub fn project_onto_normalized(self, rhs: Self) -> Self {
761        glam_assert!(rhs.is_normalized());
762        rhs * self.dot(rhs)
763    }
764
765    /// Returns the vector rejection of `self` from `rhs`.
766    ///
767    /// The vector rejection is the vector perpendicular to the projection of `self` onto
768    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
769    ///
770    /// `rhs` must be normalized.
771    ///
772    /// # Panics
773    ///
774    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
775    #[doc(alias("plane"))]
776    #[inline]
777    #[must_use]
778    pub fn reject_from_normalized(self, rhs: Self) -> Self {
779        self - self.project_onto_normalized(rhs)
780    }
781
782    /// Returns a vector containing the nearest integer to a number for each element of `self`.
783    /// Round half-way cases away from 0.0.
784    #[inline]
785    #[must_use]
786    pub fn round(self) -> Self {
787        Self {
788            x: math::round(self.x),
789            y: math::round(self.y),
790            z: math::round(self.z),
791            w: math::round(self.w),
792        }
793    }
794
795    /// Returns a vector containing the largest integer less than or equal to a number for each
796    /// element of `self`.
797    #[inline]
798    #[must_use]
799    pub fn floor(self) -> Self {
800        Self {
801            x: math::floor(self.x),
802            y: math::floor(self.y),
803            z: math::floor(self.z),
804            w: math::floor(self.w),
805        }
806    }
807
808    /// Returns a vector containing the smallest integer greater than or equal to a number for
809    /// each element of `self`.
810    #[inline]
811    #[must_use]
812    pub fn ceil(self) -> Self {
813        Self {
814            x: math::ceil(self.x),
815            y: math::ceil(self.y),
816            z: math::ceil(self.z),
817            w: math::ceil(self.w),
818        }
819    }
820
821    /// Returns a vector containing the integer part each element of `self`. This means numbers are
822    /// always truncated towards zero.
823    #[inline]
824    #[must_use]
825    pub fn trunc(self) -> Self {
826        Self {
827            x: math::trunc(self.x),
828            y: math::trunc(self.y),
829            z: math::trunc(self.z),
830            w: math::trunc(self.w),
831        }
832    }
833
834    /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
835    ///
836    /// Note that this differs from the GLSL implementation of `fract` which returns
837    /// `self - self.floor()`.
838    ///
839    /// Note that this is fast but not precise for large numbers.
840    #[inline]
841    #[must_use]
842    pub fn fract(self) -> Self {
843        self - self.trunc()
844    }
845
846    /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
847    ///
848    /// Note that this differs from the Rust implementation of `fract` which returns
849    /// `self - self.trunc()`.
850    ///
851    /// Note that this is fast but not precise for large numbers.
852    #[inline]
853    #[must_use]
854    pub fn fract_gl(self) -> Self {
855        self - self.floor()
856    }
857
858    /// Returns a vector containing `e^self` (the exponential function) for each element of
859    /// `self`.
860    #[inline]
861    #[must_use]
862    pub fn exp(self) -> Self {
863        Self::new(
864            math::exp(self.x),
865            math::exp(self.y),
866            math::exp(self.z),
867            math::exp(self.w),
868        )
869    }
870
871    /// Returns a vector containing each element of `self` raised to the power of `n`.
872    #[inline]
873    #[must_use]
874    pub fn powf(self, n: f64) -> Self {
875        Self::new(
876            math::powf(self.x, n),
877            math::powf(self.y, n),
878            math::powf(self.z, n),
879            math::powf(self.w, n),
880        )
881    }
882
883    /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
884    #[inline]
885    #[must_use]
886    pub fn recip(self) -> Self {
887        Self {
888            x: 1.0 / self.x,
889            y: 1.0 / self.y,
890            z: 1.0 / self.z,
891            w: 1.0 / self.w,
892        }
893    }
894
895    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
896    ///
897    /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
898    /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
899    /// extrapolated.
900    #[doc(alias = "mix")]
901    #[inline]
902    #[must_use]
903    pub fn lerp(self, rhs: Self, s: f64) -> Self {
904        self * (1.0 - s) + rhs * s
905    }
906
907    /// Moves towards `rhs` based on the value `d`.
908    ///
909    /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
910    /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
911    #[inline]
912    #[must_use]
913    pub fn move_towards(&self, rhs: Self, d: f64) -> Self {
914        let a = rhs - *self;
915        let len = a.length();
916        if len <= d || len <= 1e-4 {
917            return rhs;
918        }
919        *self + a / len * d
920    }
921
922    /// Calculates the midpoint between `self` and `rhs`.
923    ///
924    /// The midpoint is the average of, or halfway point between, two vectors.
925    /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
926    /// while being slightly cheaper to compute.
927    #[inline]
928    pub fn midpoint(self, rhs: Self) -> Self {
929        (self + rhs) * 0.5
930    }
931
932    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
933    /// less than or equal to `max_abs_diff`.
934    ///
935    /// This can be used to compare if two vectors contain similar elements. It works best when
936    /// comparing with a known value. The `max_abs_diff` that should be used used depends on
937    /// the values being compared against.
938    ///
939    /// For more see
940    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
941    #[inline]
942    #[must_use]
943    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
944        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
945    }
946
947    /// Returns a vector with a length no less than `min` and no more than `max`.
948    ///
949    /// # Panics
950    ///
951    /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
952    #[inline]
953    #[must_use]
954    pub fn clamp_length(self, min: f64, max: f64) -> Self {
955        glam_assert!(0.0 <= min);
956        glam_assert!(min <= max);
957        let length_sq = self.length_squared();
958        if length_sq < min * min {
959            min * (self / math::sqrt(length_sq))
960        } else if length_sq > max * max {
961            max * (self / math::sqrt(length_sq))
962        } else {
963            self
964        }
965    }
966
967    /// Returns a vector with a length no more than `max`.
968    ///
969    /// # Panics
970    ///
971    /// Will panic if `max` is negative when `glam_assert` is enabled.
972    #[inline]
973    #[must_use]
974    pub fn clamp_length_max(self, max: f64) -> Self {
975        glam_assert!(0.0 <= max);
976        let length_sq = self.length_squared();
977        if length_sq > max * max {
978            max * (self / math::sqrt(length_sq))
979        } else {
980            self
981        }
982    }
983
984    /// Returns a vector with a length no less than `min`.
985    ///
986    /// # Panics
987    ///
988    /// Will panic if `min` is negative when `glam_assert` is enabled.
989    #[inline]
990    #[must_use]
991    pub fn clamp_length_min(self, min: f64) -> Self {
992        glam_assert!(0.0 <= min);
993        let length_sq = self.length_squared();
994        if length_sq < min * min {
995            min * (self / math::sqrt(length_sq))
996        } else {
997            self
998        }
999    }
1000
1001    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
1002    /// error, yielding a more accurate result than an unfused multiply-add.
1003    ///
1004    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
1005    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
1006    /// and will be heavily dependant on designing algorithms with specific target hardware in
1007    /// mind.
1008    #[inline]
1009    #[must_use]
1010    pub fn mul_add(self, a: Self, b: Self) -> Self {
1011        Self::new(
1012            math::mul_add(self.x, a.x, b.x),
1013            math::mul_add(self.y, a.y, b.y),
1014            math::mul_add(self.z, a.z, b.z),
1015            math::mul_add(self.w, a.w, b.w),
1016        )
1017    }
1018
1019    /// Returns the reflection vector for a given incident vector `self` and surface normal
1020    /// `normal`.
1021    ///
1022    /// `normal` must be normalized.
1023    ///
1024    /// # Panics
1025    ///
1026    /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
1027    #[inline]
1028    #[must_use]
1029    pub fn reflect(self, normal: Self) -> Self {
1030        glam_assert!(normal.is_normalized());
1031        self - 2.0 * self.dot(normal) * normal
1032    }
1033
1034    /// Returns the refraction direction for a given incident vector `self`, surface normal
1035    /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
1036    /// a zero vector will be returned.
1037    ///
1038    /// `self` and `normal` must be normalized.
1039    ///
1040    /// # Panics
1041    ///
1042    /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
1043    #[inline]
1044    #[must_use]
1045    pub fn refract(self, normal: Self, eta: f64) -> Self {
1046        glam_assert!(self.is_normalized());
1047        glam_assert!(normal.is_normalized());
1048        let n_dot_i = normal.dot(self);
1049        let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1050        if k >= 0.0 {
1051            eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1052        } else {
1053            Self::ZERO
1054        }
1055    }
1056
1057    /// Casts all elements of `self` to `f32`.
1058    #[inline]
1059    #[must_use]
1060    pub fn as_vec4(&self) -> crate::Vec4 {
1061        crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
1062    }
1063
1064    /// Casts all elements of `self` to `i8`.
1065    #[inline]
1066    #[must_use]
1067    pub fn as_i8vec4(&self) -> crate::I8Vec4 {
1068        crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1069    }
1070
1071    /// Casts all elements of `self` to `u8`.
1072    #[inline]
1073    #[must_use]
1074    pub fn as_u8vec4(&self) -> crate::U8Vec4 {
1075        crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1076    }
1077
1078    /// Casts all elements of `self` to `i16`.
1079    #[inline]
1080    #[must_use]
1081    pub fn as_i16vec4(&self) -> crate::I16Vec4 {
1082        crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1083    }
1084
1085    /// Casts all elements of `self` to `u16`.
1086    #[inline]
1087    #[must_use]
1088    pub fn as_u16vec4(&self) -> crate::U16Vec4 {
1089        crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1090    }
1091
1092    /// Casts all elements of `self` to `i32`.
1093    #[inline]
1094    #[must_use]
1095    pub fn as_ivec4(&self) -> crate::IVec4 {
1096        crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1097    }
1098
1099    /// Casts all elements of `self` to `u32`.
1100    #[inline]
1101    #[must_use]
1102    pub fn as_uvec4(&self) -> crate::UVec4 {
1103        crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1104    }
1105
1106    /// Casts all elements of `self` to `i64`.
1107    #[inline]
1108    #[must_use]
1109    pub fn as_i64vec4(&self) -> crate::I64Vec4 {
1110        crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1111    }
1112
1113    /// Casts all elements of `self` to `u64`.
1114    #[inline]
1115    #[must_use]
1116    pub fn as_u64vec4(&self) -> crate::U64Vec4 {
1117        crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1118    }
1119
1120    /// Casts all elements of `self` to `usize`.
1121    #[inline]
1122    #[must_use]
1123    pub fn as_usizevec4(&self) -> crate::USizeVec4 {
1124        crate::USizeVec4::new(
1125            self.x as usize,
1126            self.y as usize,
1127            self.z as usize,
1128            self.w as usize,
1129        )
1130    }
1131}
1132
1133impl Default for DVec4 {
1134    #[inline(always)]
1135    fn default() -> Self {
1136        Self::ZERO
1137    }
1138}
1139
1140impl Div<DVec4> for DVec4 {
1141    type Output = Self;
1142    #[inline]
1143    fn div(self, rhs: Self) -> Self {
1144        Self {
1145            x: self.x.div(rhs.x),
1146            y: self.y.div(rhs.y),
1147            z: self.z.div(rhs.z),
1148            w: self.w.div(rhs.w),
1149        }
1150    }
1151}
1152
1153impl Div<&DVec4> for DVec4 {
1154    type Output = DVec4;
1155    #[inline]
1156    fn div(self, rhs: &DVec4) -> DVec4 {
1157        self.div(*rhs)
1158    }
1159}
1160
1161impl Div<&DVec4> for &DVec4 {
1162    type Output = DVec4;
1163    #[inline]
1164    fn div(self, rhs: &DVec4) -> DVec4 {
1165        (*self).div(*rhs)
1166    }
1167}
1168
1169impl Div<DVec4> for &DVec4 {
1170    type Output = DVec4;
1171    #[inline]
1172    fn div(self, rhs: DVec4) -> DVec4 {
1173        (*self).div(rhs)
1174    }
1175}
1176
1177impl DivAssign<DVec4> for DVec4 {
1178    #[inline]
1179    fn div_assign(&mut self, rhs: Self) {
1180        self.x.div_assign(rhs.x);
1181        self.y.div_assign(rhs.y);
1182        self.z.div_assign(rhs.z);
1183        self.w.div_assign(rhs.w);
1184    }
1185}
1186
1187impl DivAssign<&DVec4> for DVec4 {
1188    #[inline]
1189    fn div_assign(&mut self, rhs: &DVec4) {
1190        self.div_assign(*rhs)
1191    }
1192}
1193
1194impl Div<f64> for DVec4 {
1195    type Output = Self;
1196    #[inline]
1197    fn div(self, rhs: f64) -> Self {
1198        Self {
1199            x: self.x.div(rhs),
1200            y: self.y.div(rhs),
1201            z: self.z.div(rhs),
1202            w: self.w.div(rhs),
1203        }
1204    }
1205}
1206
1207impl Div<&f64> for DVec4 {
1208    type Output = DVec4;
1209    #[inline]
1210    fn div(self, rhs: &f64) -> DVec4 {
1211        self.div(*rhs)
1212    }
1213}
1214
1215impl Div<&f64> for &DVec4 {
1216    type Output = DVec4;
1217    #[inline]
1218    fn div(self, rhs: &f64) -> DVec4 {
1219        (*self).div(*rhs)
1220    }
1221}
1222
1223impl Div<f64> for &DVec4 {
1224    type Output = DVec4;
1225    #[inline]
1226    fn div(self, rhs: f64) -> DVec4 {
1227        (*self).div(rhs)
1228    }
1229}
1230
1231impl DivAssign<f64> for DVec4 {
1232    #[inline]
1233    fn div_assign(&mut self, rhs: f64) {
1234        self.x.div_assign(rhs);
1235        self.y.div_assign(rhs);
1236        self.z.div_assign(rhs);
1237        self.w.div_assign(rhs);
1238    }
1239}
1240
1241impl DivAssign<&f64> for DVec4 {
1242    #[inline]
1243    fn div_assign(&mut self, rhs: &f64) {
1244        self.div_assign(*rhs)
1245    }
1246}
1247
1248impl Div<DVec4> for f64 {
1249    type Output = DVec4;
1250    #[inline]
1251    fn div(self, rhs: DVec4) -> DVec4 {
1252        DVec4 {
1253            x: self.div(rhs.x),
1254            y: self.div(rhs.y),
1255            z: self.div(rhs.z),
1256            w: self.div(rhs.w),
1257        }
1258    }
1259}
1260
1261impl Div<&DVec4> for f64 {
1262    type Output = DVec4;
1263    #[inline]
1264    fn div(self, rhs: &DVec4) -> DVec4 {
1265        self.div(*rhs)
1266    }
1267}
1268
1269impl Div<&DVec4> for &f64 {
1270    type Output = DVec4;
1271    #[inline]
1272    fn div(self, rhs: &DVec4) -> DVec4 {
1273        (*self).div(*rhs)
1274    }
1275}
1276
1277impl Div<DVec4> for &f64 {
1278    type Output = DVec4;
1279    #[inline]
1280    fn div(self, rhs: DVec4) -> DVec4 {
1281        (*self).div(rhs)
1282    }
1283}
1284
1285impl Mul<DVec4> for DVec4 {
1286    type Output = Self;
1287    #[inline]
1288    fn mul(self, rhs: Self) -> Self {
1289        Self {
1290            x: self.x.mul(rhs.x),
1291            y: self.y.mul(rhs.y),
1292            z: self.z.mul(rhs.z),
1293            w: self.w.mul(rhs.w),
1294        }
1295    }
1296}
1297
1298impl Mul<&DVec4> for DVec4 {
1299    type Output = DVec4;
1300    #[inline]
1301    fn mul(self, rhs: &DVec4) -> DVec4 {
1302        self.mul(*rhs)
1303    }
1304}
1305
1306impl Mul<&DVec4> for &DVec4 {
1307    type Output = DVec4;
1308    #[inline]
1309    fn mul(self, rhs: &DVec4) -> DVec4 {
1310        (*self).mul(*rhs)
1311    }
1312}
1313
1314impl Mul<DVec4> for &DVec4 {
1315    type Output = DVec4;
1316    #[inline]
1317    fn mul(self, rhs: DVec4) -> DVec4 {
1318        (*self).mul(rhs)
1319    }
1320}
1321
1322impl MulAssign<DVec4> for DVec4 {
1323    #[inline]
1324    fn mul_assign(&mut self, rhs: Self) {
1325        self.x.mul_assign(rhs.x);
1326        self.y.mul_assign(rhs.y);
1327        self.z.mul_assign(rhs.z);
1328        self.w.mul_assign(rhs.w);
1329    }
1330}
1331
1332impl MulAssign<&DVec4> for DVec4 {
1333    #[inline]
1334    fn mul_assign(&mut self, rhs: &DVec4) {
1335        self.mul_assign(*rhs)
1336    }
1337}
1338
1339impl Mul<f64> for DVec4 {
1340    type Output = Self;
1341    #[inline]
1342    fn mul(self, rhs: f64) -> Self {
1343        Self {
1344            x: self.x.mul(rhs),
1345            y: self.y.mul(rhs),
1346            z: self.z.mul(rhs),
1347            w: self.w.mul(rhs),
1348        }
1349    }
1350}
1351
1352impl Mul<&f64> for DVec4 {
1353    type Output = DVec4;
1354    #[inline]
1355    fn mul(self, rhs: &f64) -> DVec4 {
1356        self.mul(*rhs)
1357    }
1358}
1359
1360impl Mul<&f64> for &DVec4 {
1361    type Output = DVec4;
1362    #[inline]
1363    fn mul(self, rhs: &f64) -> DVec4 {
1364        (*self).mul(*rhs)
1365    }
1366}
1367
1368impl Mul<f64> for &DVec4 {
1369    type Output = DVec4;
1370    #[inline]
1371    fn mul(self, rhs: f64) -> DVec4 {
1372        (*self).mul(rhs)
1373    }
1374}
1375
1376impl MulAssign<f64> for DVec4 {
1377    #[inline]
1378    fn mul_assign(&mut self, rhs: f64) {
1379        self.x.mul_assign(rhs);
1380        self.y.mul_assign(rhs);
1381        self.z.mul_assign(rhs);
1382        self.w.mul_assign(rhs);
1383    }
1384}
1385
1386impl MulAssign<&f64> for DVec4 {
1387    #[inline]
1388    fn mul_assign(&mut self, rhs: &f64) {
1389        self.mul_assign(*rhs)
1390    }
1391}
1392
1393impl Mul<DVec4> for f64 {
1394    type Output = DVec4;
1395    #[inline]
1396    fn mul(self, rhs: DVec4) -> DVec4 {
1397        DVec4 {
1398            x: self.mul(rhs.x),
1399            y: self.mul(rhs.y),
1400            z: self.mul(rhs.z),
1401            w: self.mul(rhs.w),
1402        }
1403    }
1404}
1405
1406impl Mul<&DVec4> for f64 {
1407    type Output = DVec4;
1408    #[inline]
1409    fn mul(self, rhs: &DVec4) -> DVec4 {
1410        self.mul(*rhs)
1411    }
1412}
1413
1414impl Mul<&DVec4> for &f64 {
1415    type Output = DVec4;
1416    #[inline]
1417    fn mul(self, rhs: &DVec4) -> DVec4 {
1418        (*self).mul(*rhs)
1419    }
1420}
1421
1422impl Mul<DVec4> for &f64 {
1423    type Output = DVec4;
1424    #[inline]
1425    fn mul(self, rhs: DVec4) -> DVec4 {
1426        (*self).mul(rhs)
1427    }
1428}
1429
1430impl Add<DVec4> for DVec4 {
1431    type Output = Self;
1432    #[inline]
1433    fn add(self, rhs: Self) -> Self {
1434        Self {
1435            x: self.x.add(rhs.x),
1436            y: self.y.add(rhs.y),
1437            z: self.z.add(rhs.z),
1438            w: self.w.add(rhs.w),
1439        }
1440    }
1441}
1442
1443impl Add<&DVec4> for DVec4 {
1444    type Output = DVec4;
1445    #[inline]
1446    fn add(self, rhs: &DVec4) -> DVec4 {
1447        self.add(*rhs)
1448    }
1449}
1450
1451impl Add<&DVec4> for &DVec4 {
1452    type Output = DVec4;
1453    #[inline]
1454    fn add(self, rhs: &DVec4) -> DVec4 {
1455        (*self).add(*rhs)
1456    }
1457}
1458
1459impl Add<DVec4> for &DVec4 {
1460    type Output = DVec4;
1461    #[inline]
1462    fn add(self, rhs: DVec4) -> DVec4 {
1463        (*self).add(rhs)
1464    }
1465}
1466
1467impl AddAssign<DVec4> for DVec4 {
1468    #[inline]
1469    fn add_assign(&mut self, rhs: Self) {
1470        self.x.add_assign(rhs.x);
1471        self.y.add_assign(rhs.y);
1472        self.z.add_assign(rhs.z);
1473        self.w.add_assign(rhs.w);
1474    }
1475}
1476
1477impl AddAssign<&DVec4> for DVec4 {
1478    #[inline]
1479    fn add_assign(&mut self, rhs: &DVec4) {
1480        self.add_assign(*rhs)
1481    }
1482}
1483
1484impl Add<f64> for DVec4 {
1485    type Output = Self;
1486    #[inline]
1487    fn add(self, rhs: f64) -> Self {
1488        Self {
1489            x: self.x.add(rhs),
1490            y: self.y.add(rhs),
1491            z: self.z.add(rhs),
1492            w: self.w.add(rhs),
1493        }
1494    }
1495}
1496
1497impl Add<&f64> for DVec4 {
1498    type Output = DVec4;
1499    #[inline]
1500    fn add(self, rhs: &f64) -> DVec4 {
1501        self.add(*rhs)
1502    }
1503}
1504
1505impl Add<&f64> for &DVec4 {
1506    type Output = DVec4;
1507    #[inline]
1508    fn add(self, rhs: &f64) -> DVec4 {
1509        (*self).add(*rhs)
1510    }
1511}
1512
1513impl Add<f64> for &DVec4 {
1514    type Output = DVec4;
1515    #[inline]
1516    fn add(self, rhs: f64) -> DVec4 {
1517        (*self).add(rhs)
1518    }
1519}
1520
1521impl AddAssign<f64> for DVec4 {
1522    #[inline]
1523    fn add_assign(&mut self, rhs: f64) {
1524        self.x.add_assign(rhs);
1525        self.y.add_assign(rhs);
1526        self.z.add_assign(rhs);
1527        self.w.add_assign(rhs);
1528    }
1529}
1530
1531impl AddAssign<&f64> for DVec4 {
1532    #[inline]
1533    fn add_assign(&mut self, rhs: &f64) {
1534        self.add_assign(*rhs)
1535    }
1536}
1537
1538impl Add<DVec4> for f64 {
1539    type Output = DVec4;
1540    #[inline]
1541    fn add(self, rhs: DVec4) -> DVec4 {
1542        DVec4 {
1543            x: self.add(rhs.x),
1544            y: self.add(rhs.y),
1545            z: self.add(rhs.z),
1546            w: self.add(rhs.w),
1547        }
1548    }
1549}
1550
1551impl Add<&DVec4> for f64 {
1552    type Output = DVec4;
1553    #[inline]
1554    fn add(self, rhs: &DVec4) -> DVec4 {
1555        self.add(*rhs)
1556    }
1557}
1558
1559impl Add<&DVec4> for &f64 {
1560    type Output = DVec4;
1561    #[inline]
1562    fn add(self, rhs: &DVec4) -> DVec4 {
1563        (*self).add(*rhs)
1564    }
1565}
1566
1567impl Add<DVec4> for &f64 {
1568    type Output = DVec4;
1569    #[inline]
1570    fn add(self, rhs: DVec4) -> DVec4 {
1571        (*self).add(rhs)
1572    }
1573}
1574
1575impl Sub<DVec4> for DVec4 {
1576    type Output = Self;
1577    #[inline]
1578    fn sub(self, rhs: Self) -> Self {
1579        Self {
1580            x: self.x.sub(rhs.x),
1581            y: self.y.sub(rhs.y),
1582            z: self.z.sub(rhs.z),
1583            w: self.w.sub(rhs.w),
1584        }
1585    }
1586}
1587
1588impl Sub<&DVec4> for DVec4 {
1589    type Output = DVec4;
1590    #[inline]
1591    fn sub(self, rhs: &DVec4) -> DVec4 {
1592        self.sub(*rhs)
1593    }
1594}
1595
1596impl Sub<&DVec4> for &DVec4 {
1597    type Output = DVec4;
1598    #[inline]
1599    fn sub(self, rhs: &DVec4) -> DVec4 {
1600        (*self).sub(*rhs)
1601    }
1602}
1603
1604impl Sub<DVec4> for &DVec4 {
1605    type Output = DVec4;
1606    #[inline]
1607    fn sub(self, rhs: DVec4) -> DVec4 {
1608        (*self).sub(rhs)
1609    }
1610}
1611
1612impl SubAssign<DVec4> for DVec4 {
1613    #[inline]
1614    fn sub_assign(&mut self, rhs: DVec4) {
1615        self.x.sub_assign(rhs.x);
1616        self.y.sub_assign(rhs.y);
1617        self.z.sub_assign(rhs.z);
1618        self.w.sub_assign(rhs.w);
1619    }
1620}
1621
1622impl SubAssign<&DVec4> for DVec4 {
1623    #[inline]
1624    fn sub_assign(&mut self, rhs: &DVec4) {
1625        self.sub_assign(*rhs)
1626    }
1627}
1628
1629impl Sub<f64> for DVec4 {
1630    type Output = Self;
1631    #[inline]
1632    fn sub(self, rhs: f64) -> Self {
1633        Self {
1634            x: self.x.sub(rhs),
1635            y: self.y.sub(rhs),
1636            z: self.z.sub(rhs),
1637            w: self.w.sub(rhs),
1638        }
1639    }
1640}
1641
1642impl Sub<&f64> for DVec4 {
1643    type Output = DVec4;
1644    #[inline]
1645    fn sub(self, rhs: &f64) -> DVec4 {
1646        self.sub(*rhs)
1647    }
1648}
1649
1650impl Sub<&f64> for &DVec4 {
1651    type Output = DVec4;
1652    #[inline]
1653    fn sub(self, rhs: &f64) -> DVec4 {
1654        (*self).sub(*rhs)
1655    }
1656}
1657
1658impl Sub<f64> for &DVec4 {
1659    type Output = DVec4;
1660    #[inline]
1661    fn sub(self, rhs: f64) -> DVec4 {
1662        (*self).sub(rhs)
1663    }
1664}
1665
1666impl SubAssign<f64> for DVec4 {
1667    #[inline]
1668    fn sub_assign(&mut self, rhs: f64) {
1669        self.x.sub_assign(rhs);
1670        self.y.sub_assign(rhs);
1671        self.z.sub_assign(rhs);
1672        self.w.sub_assign(rhs);
1673    }
1674}
1675
1676impl SubAssign<&f64> for DVec4 {
1677    #[inline]
1678    fn sub_assign(&mut self, rhs: &f64) {
1679        self.sub_assign(*rhs)
1680    }
1681}
1682
1683impl Sub<DVec4> for f64 {
1684    type Output = DVec4;
1685    #[inline]
1686    fn sub(self, rhs: DVec4) -> DVec4 {
1687        DVec4 {
1688            x: self.sub(rhs.x),
1689            y: self.sub(rhs.y),
1690            z: self.sub(rhs.z),
1691            w: self.sub(rhs.w),
1692        }
1693    }
1694}
1695
1696impl Sub<&DVec4> for f64 {
1697    type Output = DVec4;
1698    #[inline]
1699    fn sub(self, rhs: &DVec4) -> DVec4 {
1700        self.sub(*rhs)
1701    }
1702}
1703
1704impl Sub<&DVec4> for &f64 {
1705    type Output = DVec4;
1706    #[inline]
1707    fn sub(self, rhs: &DVec4) -> DVec4 {
1708        (*self).sub(*rhs)
1709    }
1710}
1711
1712impl Sub<DVec4> for &f64 {
1713    type Output = DVec4;
1714    #[inline]
1715    fn sub(self, rhs: DVec4) -> DVec4 {
1716        (*self).sub(rhs)
1717    }
1718}
1719
1720impl Rem<DVec4> for DVec4 {
1721    type Output = Self;
1722    #[inline]
1723    fn rem(self, rhs: Self) -> Self {
1724        Self {
1725            x: self.x.rem(rhs.x),
1726            y: self.y.rem(rhs.y),
1727            z: self.z.rem(rhs.z),
1728            w: self.w.rem(rhs.w),
1729        }
1730    }
1731}
1732
1733impl Rem<&DVec4> for DVec4 {
1734    type Output = DVec4;
1735    #[inline]
1736    fn rem(self, rhs: &DVec4) -> DVec4 {
1737        self.rem(*rhs)
1738    }
1739}
1740
1741impl Rem<&DVec4> for &DVec4 {
1742    type Output = DVec4;
1743    #[inline]
1744    fn rem(self, rhs: &DVec4) -> DVec4 {
1745        (*self).rem(*rhs)
1746    }
1747}
1748
1749impl Rem<DVec4> for &DVec4 {
1750    type Output = DVec4;
1751    #[inline]
1752    fn rem(self, rhs: DVec4) -> DVec4 {
1753        (*self).rem(rhs)
1754    }
1755}
1756
1757impl RemAssign<DVec4> for DVec4 {
1758    #[inline]
1759    fn rem_assign(&mut self, rhs: Self) {
1760        self.x.rem_assign(rhs.x);
1761        self.y.rem_assign(rhs.y);
1762        self.z.rem_assign(rhs.z);
1763        self.w.rem_assign(rhs.w);
1764    }
1765}
1766
1767impl RemAssign<&DVec4> for DVec4 {
1768    #[inline]
1769    fn rem_assign(&mut self, rhs: &DVec4) {
1770        self.rem_assign(*rhs)
1771    }
1772}
1773
1774impl Rem<f64> for DVec4 {
1775    type Output = Self;
1776    #[inline]
1777    fn rem(self, rhs: f64) -> Self {
1778        Self {
1779            x: self.x.rem(rhs),
1780            y: self.y.rem(rhs),
1781            z: self.z.rem(rhs),
1782            w: self.w.rem(rhs),
1783        }
1784    }
1785}
1786
1787impl Rem<&f64> for DVec4 {
1788    type Output = DVec4;
1789    #[inline]
1790    fn rem(self, rhs: &f64) -> DVec4 {
1791        self.rem(*rhs)
1792    }
1793}
1794
1795impl Rem<&f64> for &DVec4 {
1796    type Output = DVec4;
1797    #[inline]
1798    fn rem(self, rhs: &f64) -> DVec4 {
1799        (*self).rem(*rhs)
1800    }
1801}
1802
1803impl Rem<f64> for &DVec4 {
1804    type Output = DVec4;
1805    #[inline]
1806    fn rem(self, rhs: f64) -> DVec4 {
1807        (*self).rem(rhs)
1808    }
1809}
1810
1811impl RemAssign<f64> for DVec4 {
1812    #[inline]
1813    fn rem_assign(&mut self, rhs: f64) {
1814        self.x.rem_assign(rhs);
1815        self.y.rem_assign(rhs);
1816        self.z.rem_assign(rhs);
1817        self.w.rem_assign(rhs);
1818    }
1819}
1820
1821impl RemAssign<&f64> for DVec4 {
1822    #[inline]
1823    fn rem_assign(&mut self, rhs: &f64) {
1824        self.rem_assign(*rhs)
1825    }
1826}
1827
1828impl Rem<DVec4> for f64 {
1829    type Output = DVec4;
1830    #[inline]
1831    fn rem(self, rhs: DVec4) -> DVec4 {
1832        DVec4 {
1833            x: self.rem(rhs.x),
1834            y: self.rem(rhs.y),
1835            z: self.rem(rhs.z),
1836            w: self.rem(rhs.w),
1837        }
1838    }
1839}
1840
1841impl Rem<&DVec4> for f64 {
1842    type Output = DVec4;
1843    #[inline]
1844    fn rem(self, rhs: &DVec4) -> DVec4 {
1845        self.rem(*rhs)
1846    }
1847}
1848
1849impl Rem<&DVec4> for &f64 {
1850    type Output = DVec4;
1851    #[inline]
1852    fn rem(self, rhs: &DVec4) -> DVec4 {
1853        (*self).rem(*rhs)
1854    }
1855}
1856
1857impl Rem<DVec4> for &f64 {
1858    type Output = DVec4;
1859    #[inline]
1860    fn rem(self, rhs: DVec4) -> DVec4 {
1861        (*self).rem(rhs)
1862    }
1863}
1864
1865#[cfg(not(target_arch = "spirv"))]
1866impl AsRef<[f64; 4]> for DVec4 {
1867    #[inline]
1868    fn as_ref(&self) -> &[f64; 4] {
1869        unsafe { &*(self as *const DVec4 as *const [f64; 4]) }
1870    }
1871}
1872
1873#[cfg(not(target_arch = "spirv"))]
1874impl AsMut<[f64; 4]> for DVec4 {
1875    #[inline]
1876    fn as_mut(&mut self) -> &mut [f64; 4] {
1877        unsafe { &mut *(self as *mut DVec4 as *mut [f64; 4]) }
1878    }
1879}
1880
1881impl Sum for DVec4 {
1882    #[inline]
1883    fn sum<I>(iter: I) -> Self
1884    where
1885        I: Iterator<Item = Self>,
1886    {
1887        iter.fold(Self::ZERO, Self::add)
1888    }
1889}
1890
1891impl<'a> Sum<&'a Self> for DVec4 {
1892    #[inline]
1893    fn sum<I>(iter: I) -> Self
1894    where
1895        I: Iterator<Item = &'a Self>,
1896    {
1897        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1898    }
1899}
1900
1901impl Product for DVec4 {
1902    #[inline]
1903    fn product<I>(iter: I) -> Self
1904    where
1905        I: Iterator<Item = Self>,
1906    {
1907        iter.fold(Self::ONE, Self::mul)
1908    }
1909}
1910
1911impl<'a> Product<&'a Self> for DVec4 {
1912    #[inline]
1913    fn product<I>(iter: I) -> Self
1914    where
1915        I: Iterator<Item = &'a Self>,
1916    {
1917        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1918    }
1919}
1920
1921impl Neg for DVec4 {
1922    type Output = Self;
1923    #[inline]
1924    fn neg(self) -> Self {
1925        Self {
1926            x: self.x.neg(),
1927            y: self.y.neg(),
1928            z: self.z.neg(),
1929            w: self.w.neg(),
1930        }
1931    }
1932}
1933
1934impl Neg for &DVec4 {
1935    type Output = DVec4;
1936    #[inline]
1937    fn neg(self) -> DVec4 {
1938        (*self).neg()
1939    }
1940}
1941
1942impl Index<usize> for DVec4 {
1943    type Output = f64;
1944    #[inline]
1945    fn index(&self, index: usize) -> &Self::Output {
1946        match index {
1947            0 => &self.x,
1948            1 => &self.y,
1949            2 => &self.z,
1950            3 => &self.w,
1951            _ => panic!("index out of bounds"),
1952        }
1953    }
1954}
1955
1956impl IndexMut<usize> for DVec4 {
1957    #[inline]
1958    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1959        match index {
1960            0 => &mut self.x,
1961            1 => &mut self.y,
1962            2 => &mut self.z,
1963            3 => &mut self.w,
1964            _ => panic!("index out of bounds"),
1965        }
1966    }
1967}
1968
1969impl fmt::Display for DVec4 {
1970    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1971        if let Some(p) = f.precision() {
1972            write!(
1973                f,
1974                "[{:.*}, {:.*}, {:.*}, {:.*}]",
1975                p, self.x, p, self.y, p, self.z, p, self.w
1976            )
1977        } else {
1978            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1979        }
1980    }
1981}
1982
1983impl fmt::Debug for DVec4 {
1984    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1985        fmt.debug_tuple(stringify!(DVec4))
1986            .field(&self.x)
1987            .field(&self.y)
1988            .field(&self.z)
1989            .field(&self.w)
1990            .finish()
1991    }
1992}
1993
1994impl From<[f64; 4]> for DVec4 {
1995    #[inline]
1996    fn from(a: [f64; 4]) -> Self {
1997        Self::new(a[0], a[1], a[2], a[3])
1998    }
1999}
2000
2001impl From<DVec4> for [f64; 4] {
2002    #[inline]
2003    fn from(v: DVec4) -> Self {
2004        [v.x, v.y, v.z, v.w]
2005    }
2006}
2007
2008impl From<(f64, f64, f64, f64)> for DVec4 {
2009    #[inline]
2010    fn from(t: (f64, f64, f64, f64)) -> Self {
2011        Self::new(t.0, t.1, t.2, t.3)
2012    }
2013}
2014
2015impl From<DVec4> for (f64, f64, f64, f64) {
2016    #[inline]
2017    fn from(v: DVec4) -> Self {
2018        (v.x, v.y, v.z, v.w)
2019    }
2020}
2021
2022impl From<(DVec3, f64)> for DVec4 {
2023    #[inline]
2024    fn from((v, w): (DVec3, f64)) -> Self {
2025        Self::new(v.x, v.y, v.z, w)
2026    }
2027}
2028
2029impl From<(f64, DVec3)> for DVec4 {
2030    #[inline]
2031    fn from((x, v): (f64, DVec3)) -> Self {
2032        Self::new(x, v.x, v.y, v.z)
2033    }
2034}
2035
2036impl From<(DVec2, f64, f64)> for DVec4 {
2037    #[inline]
2038    fn from((v, z, w): (DVec2, f64, f64)) -> Self {
2039        Self::new(v.x, v.y, z, w)
2040    }
2041}
2042
2043impl From<(DVec2, DVec2)> for DVec4 {
2044    #[inline]
2045    fn from((v, u): (DVec2, DVec2)) -> Self {
2046        Self::new(v.x, v.y, u.x, u.y)
2047    }
2048}
2049
2050impl From<Vec4> for DVec4 {
2051    #[inline]
2052    fn from(v: Vec4) -> Self {
2053        Self::new(
2054            f64::from(v.x),
2055            f64::from(v.y),
2056            f64::from(v.z),
2057            f64::from(v.w),
2058        )
2059    }
2060}
2061
2062impl From<IVec4> for DVec4 {
2063    #[inline]
2064    fn from(v: IVec4) -> Self {
2065        Self::new(
2066            f64::from(v.x),
2067            f64::from(v.y),
2068            f64::from(v.z),
2069            f64::from(v.w),
2070        )
2071    }
2072}
2073
2074impl From<UVec4> for DVec4 {
2075    #[inline]
2076    fn from(v: UVec4) -> Self {
2077        Self::new(
2078            f64::from(v.x),
2079            f64::from(v.y),
2080            f64::from(v.z),
2081            f64::from(v.w),
2082        )
2083    }
2084}
2085
2086impl From<BVec4> for DVec4 {
2087    #[inline]
2088    fn from(v: BVec4) -> Self {
2089        Self::new(
2090            f64::from(v.x),
2091            f64::from(v.y),
2092            f64::from(v.z),
2093            f64::from(v.w),
2094        )
2095    }
2096}
2097
2098#[cfg(not(feature = "scalar-math"))]
2099impl From<BVec4A> for DVec4 {
2100    #[inline]
2101    fn from(v: BVec4A) -> Self {
2102        let bool_array: [bool; 4] = v.into();
2103        Self::new(
2104            f64::from(bool_array[0]),
2105            f64::from(bool_array[1]),
2106            f64::from(bool_array[2]),
2107            f64::from(bool_array[3]),
2108        )
2109    }
2110}