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