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