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