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