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 `0.0` if `rhs < self` and 1.0 otherwise.
856    ///
857    /// Similar to glsl's step(edge, x), which translates into edge.step(x)
858    #[inline]
859    #[must_use]
860    pub fn step(self, rhs: Self) -> Self {
861        Self::select(rhs.cmplt(self), Self::ZERO, Self::ONE)
862    }
863
864    /// Returns a vector containing all elements of `self` clamped to the range of `[0, 1]`.
865    #[inline]
866    #[must_use]
867    pub fn saturate(self) -> Self {
868        self.clamp(Self::ZERO, Self::ONE)
869    }
870
871    /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
872    ///
873    /// Note that this differs from the GLSL implementation of `fract` which returns
874    /// `self - self.floor()`.
875    ///
876    /// Note that this is fast but not precise for large numbers.
877    #[inline]
878    #[must_use]
879    pub fn fract(self) -> Self {
880        self - self.trunc()
881    }
882
883    /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
884    ///
885    /// Note that this differs from the Rust implementation of `fract` which returns
886    /// `self - self.trunc()`.
887    ///
888    /// Note that this is fast but not precise for large numbers.
889    #[inline]
890    #[must_use]
891    pub fn fract_gl(self) -> Self {
892        self - self.floor()
893    }
894
895    /// Returns a vector containing `e^self` (the exponential function) for each element of
896    /// `self`.
897    #[inline]
898    #[must_use]
899    pub fn exp(self) -> Self {
900        Self::new(
901            math::exp(self.x),
902            math::exp(self.y),
903            math::exp(self.z),
904            math::exp(self.w),
905        )
906    }
907
908    /// Returns a vector containing `2^self` for each element of `self`.
909    #[inline]
910    #[must_use]
911    pub fn exp2(self) -> Self {
912        Self::new(
913            math::exp2(self.x),
914            math::exp2(self.y),
915            math::exp2(self.z),
916            math::exp2(self.w),
917        )
918    }
919
920    /// Returns a vector containing the natural logarithm for each element of `self`.
921    /// This returns NaN when the element is negative and negative infinity when the element is zero.
922    #[inline]
923    #[must_use]
924    pub fn ln(self) -> Self {
925        Self::new(
926            math::ln(self.x),
927            math::ln(self.y),
928            math::ln(self.z),
929            math::ln(self.w),
930        )
931    }
932
933    /// Returns a vector containing the base 2 logarithm for each element of `self`.
934    /// This returns NaN when the element is negative and negative infinity when the element is zero.
935    #[inline]
936    #[must_use]
937    pub fn log2(self) -> Self {
938        Self::new(
939            math::log2(self.x),
940            math::log2(self.y),
941            math::log2(self.z),
942            math::log2(self.w),
943        )
944    }
945
946    /// Returns a vector containing each element of `self` raised to the power of `n`.
947    #[inline]
948    #[must_use]
949    pub fn powf(self, n: f64) -> Self {
950        Self::new(
951            math::powf(self.x, n),
952            math::powf(self.y, n),
953            math::powf(self.z, n),
954            math::powf(self.w, n),
955        )
956    }
957
958    /// Returns a vector containing the square root for each element of `self`.
959    /// This returns NaN when the element is negative.
960    #[inline]
961    #[must_use]
962    pub fn sqrt(self) -> Self {
963        Self::new(
964            math::sqrt(self.x),
965            math::sqrt(self.y),
966            math::sqrt(self.z),
967            math::sqrt(self.w),
968        )
969    }
970
971    /// Returns a vector containing the cosine for each element of `self`.
972    #[inline]
973    #[must_use]
974    pub fn cos(self) -> Self {
975        Self::new(
976            math::cos(self.x),
977            math::cos(self.y),
978            math::cos(self.z),
979            math::cos(self.w),
980        )
981    }
982
983    /// Returns a vector containing the sine for each element of `self`.
984    #[inline]
985    #[must_use]
986    pub fn sin(self) -> Self {
987        Self::new(
988            math::sin(self.x),
989            math::sin(self.y),
990            math::sin(self.z),
991            math::sin(self.w),
992        )
993    }
994
995    /// Returns a tuple of two vectors containing the sine and cosine for each element of `self`.
996    #[inline]
997    #[must_use]
998    pub fn sin_cos(self) -> (Self, Self) {
999        let (sin_x, cos_x) = math::sin_cos(self.x);
1000        let (sin_y, cos_y) = math::sin_cos(self.y);
1001        let (sin_z, cos_z) = math::sin_cos(self.z);
1002        let (sin_w, cos_w) = math::sin_cos(self.w);
1003
1004        (
1005            Self::new(sin_x, sin_y, sin_z, sin_w),
1006            Self::new(cos_x, cos_y, cos_z, cos_w),
1007        )
1008    }
1009
1010    /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
1011    #[inline]
1012    #[must_use]
1013    pub fn recip(self) -> Self {
1014        Self {
1015            x: 1.0 / self.x,
1016            y: 1.0 / self.y,
1017            z: 1.0 / self.z,
1018            w: 1.0 / self.w,
1019        }
1020    }
1021
1022    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
1023    ///
1024    /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
1025    /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
1026    /// extrapolated.
1027    #[doc(alias = "mix")]
1028    #[inline]
1029    #[must_use]
1030    pub fn lerp(self, rhs: Self, s: f64) -> Self {
1031        self * (1.0 - s) + rhs * s
1032    }
1033
1034    /// Moves towards `rhs` based on the value `d`.
1035    ///
1036    /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
1037    /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
1038    #[inline]
1039    #[must_use]
1040    pub fn move_towards(self, rhs: Self, d: f64) -> Self {
1041        let a = rhs - self;
1042        let len = a.length();
1043        if len <= d || len <= 1e-4 {
1044            return rhs;
1045        }
1046        self + a / len * d
1047    }
1048
1049    /// Calculates the midpoint between `self` and `rhs`.
1050    ///
1051    /// The midpoint is the average of, or halfway point between, two vectors.
1052    /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
1053    /// while being slightly cheaper to compute.
1054    #[inline]
1055    pub fn midpoint(self, rhs: Self) -> Self {
1056        (self + rhs) * 0.5
1057    }
1058
1059    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
1060    /// less than or equal to `max_abs_diff`.
1061    ///
1062    /// This can be used to compare if two vectors contain similar elements. It works best when
1063    /// comparing with a known value. The `max_abs_diff` that should be used used depends on
1064    /// the values being compared against.
1065    ///
1066    /// For more see
1067    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
1068    #[inline]
1069    #[must_use]
1070    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
1071        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
1072    }
1073
1074    /// Returns a vector with a length no less than `min` and no more than `max`.
1075    ///
1076    /// # Panics
1077    ///
1078    /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
1079    #[inline]
1080    #[must_use]
1081    pub fn clamp_length(self, min: f64, max: f64) -> Self {
1082        glam_assert!(0.0 <= min);
1083        glam_assert!(min <= max);
1084        let length_sq = self.length_squared();
1085        if length_sq < min * min {
1086            min * (self / math::sqrt(length_sq))
1087        } else if length_sq > max * max {
1088            max * (self / math::sqrt(length_sq))
1089        } else {
1090            self
1091        }
1092    }
1093
1094    /// Returns a vector with a length no more than `max`.
1095    ///
1096    /// # Panics
1097    ///
1098    /// Will panic if `max` is negative when `glam_assert` is enabled.
1099    #[inline]
1100    #[must_use]
1101    pub fn clamp_length_max(self, max: f64) -> Self {
1102        glam_assert!(0.0 <= max);
1103        let length_sq = self.length_squared();
1104        if length_sq > max * max {
1105            max * (self / math::sqrt(length_sq))
1106        } else {
1107            self
1108        }
1109    }
1110
1111    /// Returns a vector with a length no less than `min`.
1112    ///
1113    /// # Panics
1114    ///
1115    /// Will panic if `min` is negative when `glam_assert` is enabled.
1116    #[inline]
1117    #[must_use]
1118    pub fn clamp_length_min(self, min: f64) -> Self {
1119        glam_assert!(0.0 <= min);
1120        let length_sq = self.length_squared();
1121        if length_sq < min * min {
1122            min * (self / math::sqrt(length_sq))
1123        } else {
1124            self
1125        }
1126    }
1127
1128    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
1129    /// error, yielding a more accurate result than an unfused multiply-add.
1130    ///
1131    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
1132    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
1133    /// and will be heavily dependant on designing algorithms with specific target hardware in
1134    /// mind.
1135    #[inline]
1136    #[must_use]
1137    pub fn mul_add(self, a: Self, b: Self) -> Self {
1138        Self::new(
1139            math::mul_add(self.x, a.x, b.x),
1140            math::mul_add(self.y, a.y, b.y),
1141            math::mul_add(self.z, a.z, b.z),
1142            math::mul_add(self.w, a.w, b.w),
1143        )
1144    }
1145
1146    /// Returns the reflection vector for a given incident vector `self` and surface normal
1147    /// `normal`.
1148    ///
1149    /// `normal` must be normalized.
1150    ///
1151    /// # Panics
1152    ///
1153    /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
1154    #[inline]
1155    #[must_use]
1156    pub fn reflect(self, normal: Self) -> Self {
1157        glam_assert!(normal.is_normalized());
1158        self - 2.0 * self.dot(normal) * normal
1159    }
1160
1161    /// Returns the refraction direction for a given incident vector `self`, surface normal
1162    /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
1163    /// a zero vector will be returned.
1164    ///
1165    /// `self` and `normal` must be normalized.
1166    ///
1167    /// # Panics
1168    ///
1169    /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
1170    #[inline]
1171    #[must_use]
1172    pub fn refract(self, normal: Self, eta: f64) -> Self {
1173        glam_assert!(self.is_normalized());
1174        glam_assert!(normal.is_normalized());
1175        let n_dot_i = normal.dot(self);
1176        let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1177        if k >= 0.0 {
1178            eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1179        } else {
1180            Self::ZERO
1181        }
1182    }
1183
1184    /// Casts all elements of `self` to `f32`.
1185    #[inline]
1186    #[must_use]
1187    pub fn as_vec4(self) -> crate::Vec4 {
1188        crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
1189    }
1190
1191    /// Casts all elements of `self` to `i8`.
1192    #[inline]
1193    #[must_use]
1194    pub fn as_i8vec4(self) -> crate::I8Vec4 {
1195        crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1196    }
1197
1198    /// Casts all elements of `self` to `u8`.
1199    #[inline]
1200    #[must_use]
1201    pub fn as_u8vec4(self) -> crate::U8Vec4 {
1202        crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1203    }
1204
1205    /// Casts all elements of `self` to `i16`.
1206    #[inline]
1207    #[must_use]
1208    pub fn as_i16vec4(self) -> crate::I16Vec4 {
1209        crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1210    }
1211
1212    /// Casts all elements of `self` to `u16`.
1213    #[inline]
1214    #[must_use]
1215    pub fn as_u16vec4(self) -> crate::U16Vec4 {
1216        crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1217    }
1218
1219    /// Casts all elements of `self` to `i32`.
1220    #[inline]
1221    #[must_use]
1222    pub fn as_ivec4(self) -> crate::IVec4 {
1223        crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1224    }
1225
1226    /// Casts all elements of `self` to `u32`.
1227    #[inline]
1228    #[must_use]
1229    pub fn as_uvec4(self) -> crate::UVec4 {
1230        crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1231    }
1232
1233    /// Casts all elements of `self` to `i64`.
1234    #[inline]
1235    #[must_use]
1236    pub fn as_i64vec4(self) -> crate::I64Vec4 {
1237        crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1238    }
1239
1240    /// Casts all elements of `self` to `u64`.
1241    #[inline]
1242    #[must_use]
1243    pub fn as_u64vec4(self) -> crate::U64Vec4 {
1244        crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1245    }
1246
1247    /// Casts all elements of `self` to `usize`.
1248    #[inline]
1249    #[must_use]
1250    pub fn as_usizevec4(self) -> crate::USizeVec4 {
1251        crate::USizeVec4::new(
1252            self.x as usize,
1253            self.y as usize,
1254            self.z as usize,
1255            self.w as usize,
1256        )
1257    }
1258}
1259
1260impl Default for DVec4 {
1261    #[inline(always)]
1262    fn default() -> Self {
1263        Self::ZERO
1264    }
1265}
1266
1267impl Div for DVec4 {
1268    type Output = Self;
1269    #[inline]
1270    fn div(self, rhs: Self) -> Self {
1271        Self {
1272            x: self.x.div(rhs.x),
1273            y: self.y.div(rhs.y),
1274            z: self.z.div(rhs.z),
1275            w: self.w.div(rhs.w),
1276        }
1277    }
1278}
1279
1280impl Div<&Self> for DVec4 {
1281    type Output = Self;
1282    #[inline]
1283    fn div(self, rhs: &Self) -> Self {
1284        self.div(*rhs)
1285    }
1286}
1287
1288impl Div<&DVec4> for &DVec4 {
1289    type Output = DVec4;
1290    #[inline]
1291    fn div(self, rhs: &DVec4) -> DVec4 {
1292        (*self).div(*rhs)
1293    }
1294}
1295
1296impl Div<DVec4> for &DVec4 {
1297    type Output = DVec4;
1298    #[inline]
1299    fn div(self, rhs: DVec4) -> DVec4 {
1300        (*self).div(rhs)
1301    }
1302}
1303
1304impl DivAssign for DVec4 {
1305    #[inline]
1306    fn div_assign(&mut self, rhs: Self) {
1307        self.x.div_assign(rhs.x);
1308        self.y.div_assign(rhs.y);
1309        self.z.div_assign(rhs.z);
1310        self.w.div_assign(rhs.w);
1311    }
1312}
1313
1314impl DivAssign<&Self> for DVec4 {
1315    #[inline]
1316    fn div_assign(&mut self, rhs: &Self) {
1317        self.div_assign(*rhs);
1318    }
1319}
1320
1321impl Div<f64> for DVec4 {
1322    type Output = Self;
1323    #[inline]
1324    fn div(self, rhs: f64) -> Self {
1325        Self {
1326            x: self.x.div(rhs),
1327            y: self.y.div(rhs),
1328            z: self.z.div(rhs),
1329            w: self.w.div(rhs),
1330        }
1331    }
1332}
1333
1334impl Div<&f64> for DVec4 {
1335    type Output = Self;
1336    #[inline]
1337    fn div(self, rhs: &f64) -> Self {
1338        self.div(*rhs)
1339    }
1340}
1341
1342impl Div<&f64> for &DVec4 {
1343    type Output = DVec4;
1344    #[inline]
1345    fn div(self, rhs: &f64) -> DVec4 {
1346        (*self).div(*rhs)
1347    }
1348}
1349
1350impl Div<f64> for &DVec4 {
1351    type Output = DVec4;
1352    #[inline]
1353    fn div(self, rhs: f64) -> DVec4 {
1354        (*self).div(rhs)
1355    }
1356}
1357
1358impl DivAssign<f64> for DVec4 {
1359    #[inline]
1360    fn div_assign(&mut self, rhs: f64) {
1361        self.x.div_assign(rhs);
1362        self.y.div_assign(rhs);
1363        self.z.div_assign(rhs);
1364        self.w.div_assign(rhs);
1365    }
1366}
1367
1368impl DivAssign<&f64> for DVec4 {
1369    #[inline]
1370    fn div_assign(&mut self, rhs: &f64) {
1371        self.div_assign(*rhs);
1372    }
1373}
1374
1375impl Div<DVec4> for f64 {
1376    type Output = DVec4;
1377    #[inline]
1378    fn div(self, rhs: DVec4) -> DVec4 {
1379        DVec4 {
1380            x: self.div(rhs.x),
1381            y: self.div(rhs.y),
1382            z: self.div(rhs.z),
1383            w: self.div(rhs.w),
1384        }
1385    }
1386}
1387
1388impl Div<&DVec4> for f64 {
1389    type Output = DVec4;
1390    #[inline]
1391    fn div(self, rhs: &DVec4) -> DVec4 {
1392        self.div(*rhs)
1393    }
1394}
1395
1396impl Div<&DVec4> for &f64 {
1397    type Output = DVec4;
1398    #[inline]
1399    fn div(self, rhs: &DVec4) -> DVec4 {
1400        (*self).div(*rhs)
1401    }
1402}
1403
1404impl Div<DVec4> for &f64 {
1405    type Output = DVec4;
1406    #[inline]
1407    fn div(self, rhs: DVec4) -> DVec4 {
1408        (*self).div(rhs)
1409    }
1410}
1411
1412impl Mul for DVec4 {
1413    type Output = Self;
1414    #[inline]
1415    fn mul(self, rhs: Self) -> Self {
1416        Self {
1417            x: self.x.mul(rhs.x),
1418            y: self.y.mul(rhs.y),
1419            z: self.z.mul(rhs.z),
1420            w: self.w.mul(rhs.w),
1421        }
1422    }
1423}
1424
1425impl Mul<&Self> for DVec4 {
1426    type Output = Self;
1427    #[inline]
1428    fn mul(self, rhs: &Self) -> Self {
1429        self.mul(*rhs)
1430    }
1431}
1432
1433impl Mul<&DVec4> for &DVec4 {
1434    type Output = DVec4;
1435    #[inline]
1436    fn mul(self, rhs: &DVec4) -> DVec4 {
1437        (*self).mul(*rhs)
1438    }
1439}
1440
1441impl Mul<DVec4> for &DVec4 {
1442    type Output = DVec4;
1443    #[inline]
1444    fn mul(self, rhs: DVec4) -> DVec4 {
1445        (*self).mul(rhs)
1446    }
1447}
1448
1449impl MulAssign for DVec4 {
1450    #[inline]
1451    fn mul_assign(&mut self, rhs: Self) {
1452        self.x.mul_assign(rhs.x);
1453        self.y.mul_assign(rhs.y);
1454        self.z.mul_assign(rhs.z);
1455        self.w.mul_assign(rhs.w);
1456    }
1457}
1458
1459impl MulAssign<&Self> for DVec4 {
1460    #[inline]
1461    fn mul_assign(&mut self, rhs: &Self) {
1462        self.mul_assign(*rhs);
1463    }
1464}
1465
1466impl Mul<f64> for DVec4 {
1467    type Output = Self;
1468    #[inline]
1469    fn mul(self, rhs: f64) -> Self {
1470        Self {
1471            x: self.x.mul(rhs),
1472            y: self.y.mul(rhs),
1473            z: self.z.mul(rhs),
1474            w: self.w.mul(rhs),
1475        }
1476    }
1477}
1478
1479impl Mul<&f64> for DVec4 {
1480    type Output = Self;
1481    #[inline]
1482    fn mul(self, rhs: &f64) -> Self {
1483        self.mul(*rhs)
1484    }
1485}
1486
1487impl Mul<&f64> for &DVec4 {
1488    type Output = DVec4;
1489    #[inline]
1490    fn mul(self, rhs: &f64) -> DVec4 {
1491        (*self).mul(*rhs)
1492    }
1493}
1494
1495impl Mul<f64> for &DVec4 {
1496    type Output = DVec4;
1497    #[inline]
1498    fn mul(self, rhs: f64) -> DVec4 {
1499        (*self).mul(rhs)
1500    }
1501}
1502
1503impl MulAssign<f64> for DVec4 {
1504    #[inline]
1505    fn mul_assign(&mut self, rhs: f64) {
1506        self.x.mul_assign(rhs);
1507        self.y.mul_assign(rhs);
1508        self.z.mul_assign(rhs);
1509        self.w.mul_assign(rhs);
1510    }
1511}
1512
1513impl MulAssign<&f64> for DVec4 {
1514    #[inline]
1515    fn mul_assign(&mut self, rhs: &f64) {
1516        self.mul_assign(*rhs);
1517    }
1518}
1519
1520impl Mul<DVec4> for f64 {
1521    type Output = DVec4;
1522    #[inline]
1523    fn mul(self, rhs: DVec4) -> DVec4 {
1524        DVec4 {
1525            x: self.mul(rhs.x),
1526            y: self.mul(rhs.y),
1527            z: self.mul(rhs.z),
1528            w: self.mul(rhs.w),
1529        }
1530    }
1531}
1532
1533impl Mul<&DVec4> for f64 {
1534    type Output = DVec4;
1535    #[inline]
1536    fn mul(self, rhs: &DVec4) -> DVec4 {
1537        self.mul(*rhs)
1538    }
1539}
1540
1541impl Mul<&DVec4> for &f64 {
1542    type Output = DVec4;
1543    #[inline]
1544    fn mul(self, rhs: &DVec4) -> DVec4 {
1545        (*self).mul(*rhs)
1546    }
1547}
1548
1549impl Mul<DVec4> for &f64 {
1550    type Output = DVec4;
1551    #[inline]
1552    fn mul(self, rhs: DVec4) -> DVec4 {
1553        (*self).mul(rhs)
1554    }
1555}
1556
1557impl Add for DVec4 {
1558    type Output = Self;
1559    #[inline]
1560    fn add(self, rhs: Self) -> Self {
1561        Self {
1562            x: self.x.add(rhs.x),
1563            y: self.y.add(rhs.y),
1564            z: self.z.add(rhs.z),
1565            w: self.w.add(rhs.w),
1566        }
1567    }
1568}
1569
1570impl Add<&Self> for DVec4 {
1571    type Output = Self;
1572    #[inline]
1573    fn add(self, rhs: &Self) -> Self {
1574        self.add(*rhs)
1575    }
1576}
1577
1578impl Add<&DVec4> for &DVec4 {
1579    type Output = DVec4;
1580    #[inline]
1581    fn add(self, rhs: &DVec4) -> DVec4 {
1582        (*self).add(*rhs)
1583    }
1584}
1585
1586impl Add<DVec4> for &DVec4 {
1587    type Output = DVec4;
1588    #[inline]
1589    fn add(self, rhs: DVec4) -> DVec4 {
1590        (*self).add(rhs)
1591    }
1592}
1593
1594impl AddAssign for DVec4 {
1595    #[inline]
1596    fn add_assign(&mut self, rhs: Self) {
1597        self.x.add_assign(rhs.x);
1598        self.y.add_assign(rhs.y);
1599        self.z.add_assign(rhs.z);
1600        self.w.add_assign(rhs.w);
1601    }
1602}
1603
1604impl AddAssign<&Self> for DVec4 {
1605    #[inline]
1606    fn add_assign(&mut self, rhs: &Self) {
1607        self.add_assign(*rhs);
1608    }
1609}
1610
1611impl Add<f64> for DVec4 {
1612    type Output = Self;
1613    #[inline]
1614    fn add(self, rhs: f64) -> Self {
1615        Self {
1616            x: self.x.add(rhs),
1617            y: self.y.add(rhs),
1618            z: self.z.add(rhs),
1619            w: self.w.add(rhs),
1620        }
1621    }
1622}
1623
1624impl Add<&f64> for DVec4 {
1625    type Output = Self;
1626    #[inline]
1627    fn add(self, rhs: &f64) -> Self {
1628        self.add(*rhs)
1629    }
1630}
1631
1632impl Add<&f64> for &DVec4 {
1633    type Output = DVec4;
1634    #[inline]
1635    fn add(self, rhs: &f64) -> DVec4 {
1636        (*self).add(*rhs)
1637    }
1638}
1639
1640impl Add<f64> for &DVec4 {
1641    type Output = DVec4;
1642    #[inline]
1643    fn add(self, rhs: f64) -> DVec4 {
1644        (*self).add(rhs)
1645    }
1646}
1647
1648impl AddAssign<f64> for DVec4 {
1649    #[inline]
1650    fn add_assign(&mut self, rhs: f64) {
1651        self.x.add_assign(rhs);
1652        self.y.add_assign(rhs);
1653        self.z.add_assign(rhs);
1654        self.w.add_assign(rhs);
1655    }
1656}
1657
1658impl AddAssign<&f64> for DVec4 {
1659    #[inline]
1660    fn add_assign(&mut self, rhs: &f64) {
1661        self.add_assign(*rhs);
1662    }
1663}
1664
1665impl Add<DVec4> for f64 {
1666    type Output = DVec4;
1667    #[inline]
1668    fn add(self, rhs: DVec4) -> DVec4 {
1669        DVec4 {
1670            x: self.add(rhs.x),
1671            y: self.add(rhs.y),
1672            z: self.add(rhs.z),
1673            w: self.add(rhs.w),
1674        }
1675    }
1676}
1677
1678impl Add<&DVec4> for f64 {
1679    type Output = DVec4;
1680    #[inline]
1681    fn add(self, rhs: &DVec4) -> DVec4 {
1682        self.add(*rhs)
1683    }
1684}
1685
1686impl Add<&DVec4> for &f64 {
1687    type Output = DVec4;
1688    #[inline]
1689    fn add(self, rhs: &DVec4) -> DVec4 {
1690        (*self).add(*rhs)
1691    }
1692}
1693
1694impl Add<DVec4> for &f64 {
1695    type Output = DVec4;
1696    #[inline]
1697    fn add(self, rhs: DVec4) -> DVec4 {
1698        (*self).add(rhs)
1699    }
1700}
1701
1702impl Sub for DVec4 {
1703    type Output = Self;
1704    #[inline]
1705    fn sub(self, rhs: Self) -> Self {
1706        Self {
1707            x: self.x.sub(rhs.x),
1708            y: self.y.sub(rhs.y),
1709            z: self.z.sub(rhs.z),
1710            w: self.w.sub(rhs.w),
1711        }
1712    }
1713}
1714
1715impl Sub<&Self> for DVec4 {
1716    type Output = Self;
1717    #[inline]
1718    fn sub(self, rhs: &Self) -> Self {
1719        self.sub(*rhs)
1720    }
1721}
1722
1723impl Sub<&DVec4> for &DVec4 {
1724    type Output = DVec4;
1725    #[inline]
1726    fn sub(self, rhs: &DVec4) -> DVec4 {
1727        (*self).sub(*rhs)
1728    }
1729}
1730
1731impl Sub<DVec4> for &DVec4 {
1732    type Output = DVec4;
1733    #[inline]
1734    fn sub(self, rhs: DVec4) -> DVec4 {
1735        (*self).sub(rhs)
1736    }
1737}
1738
1739impl SubAssign for DVec4 {
1740    #[inline]
1741    fn sub_assign(&mut self, rhs: Self) {
1742        self.x.sub_assign(rhs.x);
1743        self.y.sub_assign(rhs.y);
1744        self.z.sub_assign(rhs.z);
1745        self.w.sub_assign(rhs.w);
1746    }
1747}
1748
1749impl SubAssign<&Self> for DVec4 {
1750    #[inline]
1751    fn sub_assign(&mut self, rhs: &Self) {
1752        self.sub_assign(*rhs);
1753    }
1754}
1755
1756impl Sub<f64> for DVec4 {
1757    type Output = Self;
1758    #[inline]
1759    fn sub(self, rhs: f64) -> Self {
1760        Self {
1761            x: self.x.sub(rhs),
1762            y: self.y.sub(rhs),
1763            z: self.z.sub(rhs),
1764            w: self.w.sub(rhs),
1765        }
1766    }
1767}
1768
1769impl Sub<&f64> for DVec4 {
1770    type Output = Self;
1771    #[inline]
1772    fn sub(self, rhs: &f64) -> Self {
1773        self.sub(*rhs)
1774    }
1775}
1776
1777impl Sub<&f64> for &DVec4 {
1778    type Output = DVec4;
1779    #[inline]
1780    fn sub(self, rhs: &f64) -> DVec4 {
1781        (*self).sub(*rhs)
1782    }
1783}
1784
1785impl Sub<f64> for &DVec4 {
1786    type Output = DVec4;
1787    #[inline]
1788    fn sub(self, rhs: f64) -> DVec4 {
1789        (*self).sub(rhs)
1790    }
1791}
1792
1793impl SubAssign<f64> for DVec4 {
1794    #[inline]
1795    fn sub_assign(&mut self, rhs: f64) {
1796        self.x.sub_assign(rhs);
1797        self.y.sub_assign(rhs);
1798        self.z.sub_assign(rhs);
1799        self.w.sub_assign(rhs);
1800    }
1801}
1802
1803impl SubAssign<&f64> for DVec4 {
1804    #[inline]
1805    fn sub_assign(&mut self, rhs: &f64) {
1806        self.sub_assign(*rhs);
1807    }
1808}
1809
1810impl Sub<DVec4> for f64 {
1811    type Output = DVec4;
1812    #[inline]
1813    fn sub(self, rhs: DVec4) -> DVec4 {
1814        DVec4 {
1815            x: self.sub(rhs.x),
1816            y: self.sub(rhs.y),
1817            z: self.sub(rhs.z),
1818            w: self.sub(rhs.w),
1819        }
1820    }
1821}
1822
1823impl Sub<&DVec4> for f64 {
1824    type Output = DVec4;
1825    #[inline]
1826    fn sub(self, rhs: &DVec4) -> DVec4 {
1827        self.sub(*rhs)
1828    }
1829}
1830
1831impl Sub<&DVec4> for &f64 {
1832    type Output = DVec4;
1833    #[inline]
1834    fn sub(self, rhs: &DVec4) -> DVec4 {
1835        (*self).sub(*rhs)
1836    }
1837}
1838
1839impl Sub<DVec4> for &f64 {
1840    type Output = DVec4;
1841    #[inline]
1842    fn sub(self, rhs: DVec4) -> DVec4 {
1843        (*self).sub(rhs)
1844    }
1845}
1846
1847impl Rem for DVec4 {
1848    type Output = Self;
1849    #[inline]
1850    fn rem(self, rhs: Self) -> Self {
1851        Self {
1852            x: self.x.rem(rhs.x),
1853            y: self.y.rem(rhs.y),
1854            z: self.z.rem(rhs.z),
1855            w: self.w.rem(rhs.w),
1856        }
1857    }
1858}
1859
1860impl Rem<&Self> for DVec4 {
1861    type Output = Self;
1862    #[inline]
1863    fn rem(self, rhs: &Self) -> Self {
1864        self.rem(*rhs)
1865    }
1866}
1867
1868impl Rem<&DVec4> for &DVec4 {
1869    type Output = DVec4;
1870    #[inline]
1871    fn rem(self, rhs: &DVec4) -> DVec4 {
1872        (*self).rem(*rhs)
1873    }
1874}
1875
1876impl Rem<DVec4> for &DVec4 {
1877    type Output = DVec4;
1878    #[inline]
1879    fn rem(self, rhs: DVec4) -> DVec4 {
1880        (*self).rem(rhs)
1881    }
1882}
1883
1884impl RemAssign for DVec4 {
1885    #[inline]
1886    fn rem_assign(&mut self, rhs: Self) {
1887        self.x.rem_assign(rhs.x);
1888        self.y.rem_assign(rhs.y);
1889        self.z.rem_assign(rhs.z);
1890        self.w.rem_assign(rhs.w);
1891    }
1892}
1893
1894impl RemAssign<&Self> for DVec4 {
1895    #[inline]
1896    fn rem_assign(&mut self, rhs: &Self) {
1897        self.rem_assign(*rhs);
1898    }
1899}
1900
1901impl Rem<f64> for DVec4 {
1902    type Output = Self;
1903    #[inline]
1904    fn rem(self, rhs: f64) -> Self {
1905        Self {
1906            x: self.x.rem(rhs),
1907            y: self.y.rem(rhs),
1908            z: self.z.rem(rhs),
1909            w: self.w.rem(rhs),
1910        }
1911    }
1912}
1913
1914impl Rem<&f64> for DVec4 {
1915    type Output = Self;
1916    #[inline]
1917    fn rem(self, rhs: &f64) -> Self {
1918        self.rem(*rhs)
1919    }
1920}
1921
1922impl Rem<&f64> for &DVec4 {
1923    type Output = DVec4;
1924    #[inline]
1925    fn rem(self, rhs: &f64) -> DVec4 {
1926        (*self).rem(*rhs)
1927    }
1928}
1929
1930impl Rem<f64> for &DVec4 {
1931    type Output = DVec4;
1932    #[inline]
1933    fn rem(self, rhs: f64) -> DVec4 {
1934        (*self).rem(rhs)
1935    }
1936}
1937
1938impl RemAssign<f64> for DVec4 {
1939    #[inline]
1940    fn rem_assign(&mut self, rhs: f64) {
1941        self.x.rem_assign(rhs);
1942        self.y.rem_assign(rhs);
1943        self.z.rem_assign(rhs);
1944        self.w.rem_assign(rhs);
1945    }
1946}
1947
1948impl RemAssign<&f64> for DVec4 {
1949    #[inline]
1950    fn rem_assign(&mut self, rhs: &f64) {
1951        self.rem_assign(*rhs);
1952    }
1953}
1954
1955impl Rem<DVec4> for f64 {
1956    type Output = DVec4;
1957    #[inline]
1958    fn rem(self, rhs: DVec4) -> DVec4 {
1959        DVec4 {
1960            x: self.rem(rhs.x),
1961            y: self.rem(rhs.y),
1962            z: self.rem(rhs.z),
1963            w: self.rem(rhs.w),
1964        }
1965    }
1966}
1967
1968impl Rem<&DVec4> for f64 {
1969    type Output = DVec4;
1970    #[inline]
1971    fn rem(self, rhs: &DVec4) -> DVec4 {
1972        self.rem(*rhs)
1973    }
1974}
1975
1976impl Rem<&DVec4> for &f64 {
1977    type Output = DVec4;
1978    #[inline]
1979    fn rem(self, rhs: &DVec4) -> DVec4 {
1980        (*self).rem(*rhs)
1981    }
1982}
1983
1984impl Rem<DVec4> for &f64 {
1985    type Output = DVec4;
1986    #[inline]
1987    fn rem(self, rhs: DVec4) -> DVec4 {
1988        (*self).rem(rhs)
1989    }
1990}
1991
1992impl AsRef<[f64; 4]> for DVec4 {
1993    #[inline]
1994    fn as_ref(&self) -> &[f64; 4] {
1995        unsafe { &*(self as *const Self as *const [f64; 4]) }
1996    }
1997}
1998
1999impl AsMut<[f64; 4]> for DVec4 {
2000    #[inline]
2001    fn as_mut(&mut self) -> &mut [f64; 4] {
2002        unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
2003    }
2004}
2005
2006impl Sum for DVec4 {
2007    #[inline]
2008    fn sum<I>(iter: I) -> Self
2009    where
2010        I: Iterator<Item = Self>,
2011    {
2012        iter.fold(Self::ZERO, Self::add)
2013    }
2014}
2015
2016impl<'a> Sum<&'a Self> for DVec4 {
2017    #[inline]
2018    fn sum<I>(iter: I) -> Self
2019    where
2020        I: Iterator<Item = &'a Self>,
2021    {
2022        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
2023    }
2024}
2025
2026impl Product for DVec4 {
2027    #[inline]
2028    fn product<I>(iter: I) -> Self
2029    where
2030        I: Iterator<Item = Self>,
2031    {
2032        iter.fold(Self::ONE, Self::mul)
2033    }
2034}
2035
2036impl<'a> Product<&'a Self> for DVec4 {
2037    #[inline]
2038    fn product<I>(iter: I) -> Self
2039    where
2040        I: Iterator<Item = &'a Self>,
2041    {
2042        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
2043    }
2044}
2045
2046impl Neg for DVec4 {
2047    type Output = Self;
2048    #[inline]
2049    fn neg(self) -> Self {
2050        Self {
2051            x: self.x.neg(),
2052            y: self.y.neg(),
2053            z: self.z.neg(),
2054            w: self.w.neg(),
2055        }
2056    }
2057}
2058
2059impl Neg for &DVec4 {
2060    type Output = DVec4;
2061    #[inline]
2062    fn neg(self) -> DVec4 {
2063        (*self).neg()
2064    }
2065}
2066
2067impl Index<usize> for DVec4 {
2068    type Output = f64;
2069    #[inline]
2070    fn index(&self, index: usize) -> &Self::Output {
2071        match index {
2072            0 => &self.x,
2073            1 => &self.y,
2074            2 => &self.z,
2075            3 => &self.w,
2076            _ => panic!("index out of bounds"),
2077        }
2078    }
2079}
2080
2081impl IndexMut<usize> for DVec4 {
2082    #[inline]
2083    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
2084        match index {
2085            0 => &mut self.x,
2086            1 => &mut self.y,
2087            2 => &mut self.z,
2088            3 => &mut self.w,
2089            _ => panic!("index out of bounds"),
2090        }
2091    }
2092}
2093
2094impl fmt::Display for DVec4 {
2095    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2096        if let Some(p) = f.precision() {
2097            write!(
2098                f,
2099                "[{:.*}, {:.*}, {:.*}, {:.*}]",
2100                p, self.x, p, self.y, p, self.z, p, self.w
2101            )
2102        } else {
2103            write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
2104        }
2105    }
2106}
2107
2108impl fmt::Debug for DVec4 {
2109    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2110        fmt.debug_tuple(stringify!(DVec4))
2111            .field(&self.x)
2112            .field(&self.y)
2113            .field(&self.z)
2114            .field(&self.w)
2115            .finish()
2116    }
2117}
2118
2119impl From<[f64; 4]> for DVec4 {
2120    #[inline]
2121    fn from(a: [f64; 4]) -> Self {
2122        Self::new(a[0], a[1], a[2], a[3])
2123    }
2124}
2125
2126impl From<DVec4> for [f64; 4] {
2127    #[inline]
2128    fn from(v: DVec4) -> Self {
2129        [v.x, v.y, v.z, v.w]
2130    }
2131}
2132
2133impl From<(f64, f64, f64, f64)> for DVec4 {
2134    #[inline]
2135    fn from(t: (f64, f64, f64, f64)) -> Self {
2136        Self::new(t.0, t.1, t.2, t.3)
2137    }
2138}
2139
2140impl From<DVec4> for (f64, f64, f64, f64) {
2141    #[inline]
2142    fn from(v: DVec4) -> Self {
2143        (v.x, v.y, v.z, v.w)
2144    }
2145}
2146
2147impl From<(DVec3, f64)> for DVec4 {
2148    #[inline]
2149    fn from((v, w): (DVec3, f64)) -> Self {
2150        Self::new(v.x, v.y, v.z, w)
2151    }
2152}
2153
2154impl From<(f64, DVec3)> for DVec4 {
2155    #[inline]
2156    fn from((x, v): (f64, DVec3)) -> Self {
2157        Self::new(x, v.x, v.y, v.z)
2158    }
2159}
2160
2161impl From<(DVec2, f64, f64)> for DVec4 {
2162    #[inline]
2163    fn from((v, z, w): (DVec2, f64, f64)) -> Self {
2164        Self::new(v.x, v.y, z, w)
2165    }
2166}
2167
2168impl From<(DVec2, DVec2)> for DVec4 {
2169    #[inline]
2170    fn from((v, u): (DVec2, DVec2)) -> Self {
2171        Self::new(v.x, v.y, u.x, u.y)
2172    }
2173}
2174
2175impl From<Vec4> for DVec4 {
2176    #[inline]
2177    fn from(v: Vec4) -> Self {
2178        Self::new(
2179            f64::from(v.x),
2180            f64::from(v.y),
2181            f64::from(v.z),
2182            f64::from(v.w),
2183        )
2184    }
2185}
2186
2187impl From<IVec4> for DVec4 {
2188    #[inline]
2189    fn from(v: IVec4) -> Self {
2190        Self::new(
2191            f64::from(v.x),
2192            f64::from(v.y),
2193            f64::from(v.z),
2194            f64::from(v.w),
2195        )
2196    }
2197}
2198
2199impl From<UVec4> for DVec4 {
2200    #[inline]
2201    fn from(v: UVec4) -> Self {
2202        Self::new(
2203            f64::from(v.x),
2204            f64::from(v.y),
2205            f64::from(v.z),
2206            f64::from(v.w),
2207        )
2208    }
2209}
2210
2211impl From<BVec4> for DVec4 {
2212    #[inline]
2213    fn from(v: BVec4) -> Self {
2214        Self::new(
2215            f64::from(v.x),
2216            f64::from(v.y),
2217            f64::from(v.z),
2218            f64::from(v.w),
2219        )
2220    }
2221}
2222
2223#[cfg(not(feature = "scalar-math"))]
2224impl From<BVec4A> for DVec4 {
2225    #[inline]
2226    fn from(v: BVec4A) -> Self {
2227        let bool_array: [bool; 4] = v.into();
2228        Self::new(
2229            f64::from(bool_array[0]),
2230            f64::from(bool_array[1]),
2231            f64::from(bool_array[2]),
2232            f64::from(bool_array[3]),
2233        )
2234    }
2235}