Skip to main content

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