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