Skip to main content

glam/f64/
dvec4.rs

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