revier_glam/f64/
dvec4.rs

1// Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3use crate::{f64::math, BVec4, DVec2, DVec3, IVec4, UVec4, Vec4};
4
5#[cfg(not(target_arch = "spirv"))]
6use core::fmt;
7use core::iter::{Product, Sum};
8use core::{f32, ops::*};
9
10/// Creates a 4-dimensional vector.
11#[inline(always)]
12pub const fn dvec4(x: f64, y: f64, z: f64, w: f64) -> DVec4 {
13    DVec4::new(x, y, z, w)
14}
15
16/// A 4-dimensional vector.
17#[derive(Clone, Copy, PartialEq)]
18#[cfg_attr(feature = "cuda", repr(align(16)))]
19#[cfg_attr(not(target_arch = "spirv"), repr(C))]
20#[cfg_attr(target_arch = "spirv", repr(simd))]
21pub struct DVec4 {
22    pub x: f64,
23    pub y: f64,
24    pub z: f64,
25    pub w: f64,
26}
27
28impl DVec4 {
29    /// All zeroes.
30    pub const ZERO: Self = Self::splat(0.0);
31
32    /// All ones.
33    pub const ONE: Self = Self::splat(1.0);
34
35    /// All negative ones.
36    pub const NEG_ONE: Self = Self::splat(-1.0);
37
38    /// All `f64::MIN`.
39    pub const MIN: Self = Self::splat(f64::MIN);
40
41    /// All `f64::MAX`.
42    pub const MAX: Self = Self::splat(f64::MAX);
43
44    /// All `f64::NAN`.
45    pub const NAN: Self = Self::splat(f64::NAN);
46
47    /// All `f64::INFINITY`.
48    pub const INFINITY: Self = Self::splat(f64::INFINITY);
49
50    /// All `f64::NEG_INFINITY`.
51    pub const NEG_INFINITY: Self = Self::splat(f64::NEG_INFINITY);
52
53    /// A unit vector pointing along the positive X axis.
54    pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
55
56    /// A unit vector pointing along the positive Y axis.
57    pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
58
59    /// A unit vector pointing along the positive Z axis.
60    pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
61
62    /// A unit vector pointing along the positive W axis.
63    pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
64
65    /// A unit vector pointing along the negative X axis.
66    pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
67
68    /// A unit vector pointing along the negative Y axis.
69    pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
70
71    /// A unit vector pointing along the negative Z axis.
72    pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
73
74    /// A unit vector pointing along the negative W axis.
75    pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
76
77    /// The unit axes.
78    pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
79
80    /// Creates a new vector.
81    #[inline(always)]
82    pub const fn new(x: f64, y: f64, z: f64, w: f64) -> Self {
83        Self { x, y, z, w }
84    }
85
86    /// Creates a vector with all elements set to `v`.
87    #[inline]
88    pub const fn splat(v: f64) -> Self {
89        Self {
90            x: v,
91
92            y: v,
93
94            z: v,
95
96            w: v,
97        }
98    }
99
100    /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
101    /// for each element of `self`.
102    ///
103    /// A true element in the mask uses the corresponding element from `if_true`, and false
104    /// uses the element from `if_false`.
105    #[inline]
106    pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
107        Self {
108            x: if mask.x { if_true.x } else { if_false.x },
109            y: if mask.y { if_true.y } else { if_false.y },
110            z: if mask.z { if_true.z } else { if_false.z },
111            w: if mask.w { if_true.w } else { if_false.w },
112        }
113    }
114
115    /// Creates a new vector from an array.
116    #[inline]
117    pub const fn from_array(a: [f64; 4]) -> Self {
118        Self::new(a[0], a[1], a[2], a[3])
119    }
120
121    /// `[x, y, z, w]`
122    #[inline]
123    pub const fn to_array(&self) -> [f64; 4] {
124        [self.x, self.y, self.z, self.w]
125    }
126
127    /// Creates a vector from the first 4 values in `slice`.
128    ///
129    /// # Panics
130    ///
131    /// Panics if `slice` is less than 4 elements long.
132    #[inline]
133    pub const fn from_slice(slice: &[f64]) -> Self {
134        Self::new(slice[0], slice[1], slice[2], slice[3])
135    }
136
137    /// Writes the elements of `self` to the first 4 elements in `slice`.
138    ///
139    /// # Panics
140    ///
141    /// Panics if `slice` is less than 4 elements long.
142    #[inline]
143    pub fn write_to_slice(self, slice: &mut [f64]) {
144        slice[0] = self.x;
145        slice[1] = self.y;
146        slice[2] = self.z;
147        slice[3] = self.w;
148    }
149
150    /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
151    ///
152    /// Truncation to [`DVec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
153    #[inline]
154    pub fn truncate(self) -> DVec3 {
155        use crate::swizzles::Vec4Swizzles;
156        self.xyz()
157    }
158
159    /// Computes the dot product of `self` and `rhs`.
160    #[inline]
161    pub fn dot(self, rhs: Self) -> f64 {
162        (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
163    }
164
165    /// Returns a vector where every component is the dot product of `self` and `rhs`.
166    #[inline]
167    pub fn dot_into_vec(self, rhs: Self) -> Self {
168        Self::splat(self.dot(rhs))
169    }
170
171    /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
172    ///
173    /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
174    #[inline]
175    pub fn min(self, rhs: Self) -> Self {
176        Self {
177            x: self.x.min(rhs.x),
178            y: self.y.min(rhs.y),
179            z: self.z.min(rhs.z),
180            w: self.w.min(rhs.w),
181        }
182    }
183
184    /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
185    ///
186    /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
187    #[inline]
188    pub fn max(self, rhs: Self) -> Self {
189        Self {
190            x: self.x.max(rhs.x),
191            y: self.y.max(rhs.y),
192            z: self.z.max(rhs.z),
193            w: self.w.max(rhs.w),
194        }
195    }
196
197    /// Component-wise clamping of values, similar to [`f64::clamp`].
198    ///
199    /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
200    ///
201    /// # Panics
202    ///
203    /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
204    #[inline]
205    pub fn clamp(self, min: Self, max: Self) -> Self {
206        glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
207        self.max(min).min(max)
208    }
209
210    /// Returns the horizontal minimum of `self`.
211    ///
212    /// In other words this computes `min(x, y, ..)`.
213    #[inline]
214    pub fn min_element(self) -> f64 {
215        self.x.min(self.y.min(self.z.min(self.w)))
216    }
217
218    /// Returns the horizontal maximum of `self`.
219    ///
220    /// In other words this computes `max(x, y, ..)`.
221    #[inline]
222    pub fn max_element(self) -> f64 {
223        self.x.max(self.y.max(self.z.max(self.w)))
224    }
225
226    /// Returns a vector mask containing the result of a `==` comparison for each element of
227    /// `self` and `rhs`.
228    ///
229    /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
230    /// elements.
231    #[inline]
232    pub fn cmpeq(self, rhs: Self) -> BVec4 {
233        BVec4::new(
234            self.x.eq(&rhs.x),
235            self.y.eq(&rhs.y),
236            self.z.eq(&rhs.z),
237            self.w.eq(&rhs.w),
238        )
239    }
240
241    /// Returns a vector mask containing the result of a `!=` comparison for each element of
242    /// `self` and `rhs`.
243    ///
244    /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
245    /// elements.
246    #[inline]
247    pub fn cmpne(self, rhs: Self) -> BVec4 {
248        BVec4::new(
249            self.x.ne(&rhs.x),
250            self.y.ne(&rhs.y),
251            self.z.ne(&rhs.z),
252            self.w.ne(&rhs.w),
253        )
254    }
255
256    /// Returns a vector mask containing the result of a `>=` comparison for each element of
257    /// `self` and `rhs`.
258    ///
259    /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
260    /// elements.
261    #[inline]
262    pub fn cmpge(self, rhs: Self) -> BVec4 {
263        BVec4::new(
264            self.x.ge(&rhs.x),
265            self.y.ge(&rhs.y),
266            self.z.ge(&rhs.z),
267            self.w.ge(&rhs.w),
268        )
269    }
270
271    /// Returns a vector mask containing the result of a `>` comparison for each element of
272    /// `self` and `rhs`.
273    ///
274    /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
275    /// elements.
276    #[inline]
277    pub fn cmpgt(self, rhs: Self) -> BVec4 {
278        BVec4::new(
279            self.x.gt(&rhs.x),
280            self.y.gt(&rhs.y),
281            self.z.gt(&rhs.z),
282            self.w.gt(&rhs.w),
283        )
284    }
285
286    /// Returns a vector mask containing the result of a `<=` comparison for each element of
287    /// `self` and `rhs`.
288    ///
289    /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
290    /// elements.
291    #[inline]
292    pub fn cmple(self, rhs: Self) -> BVec4 {
293        BVec4::new(
294            self.x.le(&rhs.x),
295            self.y.le(&rhs.y),
296            self.z.le(&rhs.z),
297            self.w.le(&rhs.w),
298        )
299    }
300
301    /// Returns a vector mask containing the result of a `<` comparison for each element of
302    /// `self` and `rhs`.
303    ///
304    /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
305    /// elements.
306    #[inline]
307    pub fn cmplt(self, rhs: Self) -> BVec4 {
308        BVec4::new(
309            self.x.lt(&rhs.x),
310            self.y.lt(&rhs.y),
311            self.z.lt(&rhs.z),
312            self.w.lt(&rhs.w),
313        )
314    }
315
316    /// Returns a vector containing the absolute value of each element of `self`.
317    #[inline]
318    pub fn abs(self) -> Self {
319        Self {
320            x: math::abs(self.x),
321            y: math::abs(self.y),
322            z: math::abs(self.z),
323            w: math::abs(self.w),
324        }
325    }
326
327    /// Returns a vector with elements representing the sign of `self`.
328    ///
329    /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
330    /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
331    /// - `NAN` if the number is `NAN`
332    #[inline]
333    pub fn signum(self) -> Self {
334        Self {
335            x: math::signum(self.x),
336            y: math::signum(self.y),
337            z: math::signum(self.z),
338            w: math::signum(self.w),
339        }
340    }
341
342    /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
343    #[inline]
344    pub fn copysign(self, rhs: Self) -> Self {
345        Self {
346            x: math::copysign(self.x, rhs.x),
347            y: math::copysign(self.y, rhs.y),
348            z: math::copysign(self.z, rhs.z),
349            w: math::copysign(self.w, rhs.w),
350        }
351    }
352
353    /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
354    ///
355    /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
356    /// into the first lowest bit, element `y` into the second, etc.
357    #[inline]
358    pub fn is_negative_bitmask(self) -> u32 {
359        (self.x.is_sign_negative() as u32)
360            | (self.y.is_sign_negative() as u32) << 1
361            | (self.z.is_sign_negative() as u32) << 2
362            | (self.w.is_sign_negative() as u32) << 3
363    }
364
365    /// Returns `true` if, and only if, all elements are finite.  If any element is either
366    /// `NaN`, positive or negative infinity, this will return `false`.
367    #[inline]
368    pub fn is_finite(self) -> bool {
369        self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
370    }
371
372    /// Returns `true` if any elements are `NaN`.
373    #[inline]
374    pub fn is_nan(self) -> bool {
375        self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
376    }
377
378    /// Performs `is_nan` on each element of self, returning a vector mask of the results.
379    ///
380    /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
381    #[inline]
382    pub fn is_nan_mask(self) -> BVec4 {
383        BVec4::new(
384            self.x.is_nan(),
385            self.y.is_nan(),
386            self.z.is_nan(),
387            self.w.is_nan(),
388        )
389    }
390
391    /// Computes the length of `self`.
392    #[doc(alias = "magnitude")]
393    #[inline]
394    pub fn length(self) -> f64 {
395        math::sqrt(self.dot(self))
396    }
397
398    /// Computes the squared length of `self`.
399    ///
400    /// This is faster than `length()` as it avoids a square root operation.
401    #[doc(alias = "magnitude2")]
402    #[inline]
403    pub fn length_squared(self) -> f64 {
404        self.dot(self)
405    }
406
407    /// Computes `1.0 / length()`.
408    ///
409    /// For valid results, `self` must _not_ be of length zero.
410    #[inline]
411    pub fn length_recip(self) -> f64 {
412        self.length().recip()
413    }
414
415    /// Computes the Euclidean distance between two points in space.
416    #[inline]
417    pub fn distance(self, rhs: Self) -> f64 {
418        (self - rhs).length()
419    }
420
421    /// Compute the squared euclidean distance between two points in space.
422    #[inline]
423    pub fn distance_squared(self, rhs: Self) -> f64 {
424        (self - rhs).length_squared()
425    }
426
427    /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
428    #[inline]
429    pub fn div_euclid(self, rhs: Self) -> Self {
430        Self::new(
431            math::div_euclid(self.x, rhs.x),
432            math::div_euclid(self.y, rhs.y),
433            math::div_euclid(self.z, rhs.z),
434            math::div_euclid(self.w, rhs.w),
435        )
436    }
437
438    /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
439    ///
440    /// [Euclidean division]: f64::rem_euclid
441    #[inline]
442    pub fn rem_euclid(self, rhs: Self) -> Self {
443        Self::new(
444            math::rem_euclid(self.x, rhs.x),
445            math::rem_euclid(self.y, rhs.y),
446            math::rem_euclid(self.z, rhs.z),
447            math::rem_euclid(self.w, rhs.w),
448        )
449    }
450
451    /// Returns `self` normalized to length 1.0.
452    ///
453    /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
454    ///
455    /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
456    ///
457    /// Panics
458    ///
459    /// Will panic if `self` is zero length when `glam_assert` is enabled.
460    #[must_use]
461    #[inline]
462    pub fn normalize(self) -> Self {
463        #[allow(clippy::let_and_return)]
464        let normalized = self.mul(self.length_recip());
465        glam_assert!(normalized.is_finite());
466        normalized
467    }
468
469    /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
470    ///
471    /// In particular, if the input is zero (or very close to zero), or non-finite,
472    /// the result of this operation will be `None`.
473    ///
474    /// See also [`Self::normalize_or_zero()`].
475    #[must_use]
476    #[inline]
477    pub fn try_normalize(self) -> Option<Self> {
478        let rcp = self.length_recip();
479        if rcp.is_finite() && rcp > 0.0 {
480            Some(self * rcp)
481        } else {
482            None
483        }
484    }
485
486    /// Returns `self` normalized to length 1.0 if possible, else returns zero.
487    ///
488    /// In particular, if the input is zero (or very close to zero), or non-finite,
489    /// the result of this operation will be zero.
490    ///
491    /// See also [`Self::try_normalize()`].
492    #[must_use]
493    #[inline]
494    pub fn normalize_or_zero(self) -> Self {
495        let rcp = self.length_recip();
496        if rcp.is_finite() && rcp > 0.0 {
497            self * rcp
498        } else {
499            Self::ZERO
500        }
501    }
502
503    /// Returns whether `self` is length `1.0` or not.
504    ///
505    /// Uses a precision threshold of `1e-6`.
506    #[inline]
507    pub fn is_normalized(self) -> bool {
508        // TODO: do something with epsilon
509        math::abs(self.length_squared() - 1.0) <= 1e-4
510    }
511
512    /// Returns the vector projection of `self` onto `rhs`.
513    ///
514    /// `rhs` must be of non-zero length.
515    ///
516    /// # Panics
517    ///
518    /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
519    #[must_use]
520    #[inline]
521    pub fn project_onto(self, rhs: Self) -> Self {
522        let other_len_sq_rcp = rhs.dot(rhs).recip();
523        glam_assert!(other_len_sq_rcp.is_finite());
524        rhs * self.dot(rhs) * other_len_sq_rcp
525    }
526
527    /// Returns the vector rejection of `self` from `rhs`.
528    ///
529    /// The vector rejection is the vector perpendicular to the projection of `self` onto
530    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
531    ///
532    /// `rhs` must be of non-zero length.
533    ///
534    /// # Panics
535    ///
536    /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
537    #[must_use]
538    #[inline]
539    pub fn reject_from(self, rhs: Self) -> Self {
540        self - self.project_onto(rhs)
541    }
542
543    /// Returns the vector projection of `self` onto `rhs`.
544    ///
545    /// `rhs` must be normalized.
546    ///
547    /// # Panics
548    ///
549    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
550    #[must_use]
551    #[inline]
552    pub fn project_onto_normalized(self, rhs: Self) -> Self {
553        glam_assert!(rhs.is_normalized());
554        rhs * self.dot(rhs)
555    }
556
557    /// Returns the vector rejection of `self` from `rhs`.
558    ///
559    /// The vector rejection is the vector perpendicular to the projection of `self` onto
560    /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
561    ///
562    /// `rhs` must be normalized.
563    ///
564    /// # Panics
565    ///
566    /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
567    #[must_use]
568    #[inline]
569    pub fn reject_from_normalized(self, rhs: Self) -> Self {
570        self - self.project_onto_normalized(rhs)
571    }
572
573    /// Returns a vector containing the nearest integer to a number for each element of `self`.
574    /// Round half-way cases away from 0.0.
575    #[inline]
576    pub fn round(self) -> Self {
577        Self {
578            x: math::round(self.x),
579            y: math::round(self.y),
580            z: math::round(self.z),
581            w: math::round(self.w),
582        }
583    }
584
585    /// Returns a vector containing the largest integer less than or equal to a number for each
586    /// element of `self`.
587    #[inline]
588    pub fn floor(self) -> Self {
589        Self {
590            x: math::floor(self.x),
591            y: math::floor(self.y),
592            z: math::floor(self.z),
593            w: math::floor(self.w),
594        }
595    }
596
597    /// Returns a vector containing the smallest integer greater than or equal to a number for
598    /// each element of `self`.
599    #[inline]
600    pub fn ceil(self) -> Self {
601        Self {
602            x: math::ceil(self.x),
603            y: math::ceil(self.y),
604            z: math::ceil(self.z),
605            w: math::ceil(self.w),
606        }
607    }
608
609    /// Returns a vector containing the integer part each element of `self`. This means numbers are
610    /// always truncated towards zero.
611    #[inline]
612    pub fn trunc(self) -> Self {
613        Self {
614            x: math::trunc(self.x),
615            y: math::trunc(self.y),
616            z: math::trunc(self.z),
617            w: math::trunc(self.w),
618        }
619    }
620
621    /// Returns a vector containing the fractional part of the vector, e.g. `self -
622    /// self.floor()`.
623    ///
624    /// Note that this is fast but not precise for large numbers.
625    #[inline]
626    pub fn fract(self) -> Self {
627        self - self.floor()
628    }
629
630    /// Returns a vector containing `e^self` (the exponential function) for each element of
631    /// `self`.
632    #[inline]
633    pub fn exp(self) -> Self {
634        Self::new(
635            math::exp(self.x),
636            math::exp(self.y),
637            math::exp(self.z),
638            math::exp(self.w),
639        )
640    }
641
642    /// Returns a vector containing each element of `self` raised to the power of `n`.
643    #[inline]
644    pub fn powf(self, n: f64) -> Self {
645        Self::new(
646            math::powf(self.x, n),
647            math::powf(self.y, n),
648            math::powf(self.z, n),
649            math::powf(self.w, n),
650        )
651    }
652
653    /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
654    #[inline]
655    pub fn recip(self) -> Self {
656        Self {
657            x: 1.0 / self.x,
658            y: 1.0 / self.y,
659            z: 1.0 / self.z,
660            w: 1.0 / self.w,
661        }
662    }
663
664    /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
665    ///
666    /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
667    /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
668    /// extrapolated.
669    #[doc(alias = "mix")]
670    #[inline]
671    pub fn lerp(self, rhs: Self, s: f64) -> Self {
672        self + ((rhs - self) * s)
673    }
674
675    /// Returns true if the absolute difference of all elements between `self` and `rhs` is
676    /// less than or equal to `max_abs_diff`.
677    ///
678    /// This can be used to compare if two vectors contain similar elements. It works best when
679    /// comparing with a known value. The `max_abs_diff` that should be used used depends on
680    /// the values being compared against.
681    ///
682    /// For more see
683    /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
684    #[inline]
685    pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f64) -> bool {
686        self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
687    }
688
689    /// Returns a vector with a length no less than `min` and no more than `max`
690    ///
691    /// # Panics
692    ///
693    /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
694    #[inline]
695    pub fn clamp_length(self, min: f64, max: f64) -> Self {
696        glam_assert!(min <= max);
697        let length_sq = self.length_squared();
698        if length_sq < min * min {
699            min * (self / math::sqrt(length_sq))
700        } else if length_sq > max * max {
701            max * (self / math::sqrt(length_sq))
702        } else {
703            self
704        }
705    }
706
707    /// Returns a vector with a length no more than `max`
708    pub fn clamp_length_max(self, max: f64) -> Self {
709        let length_sq = self.length_squared();
710        if length_sq > max * max {
711            max * (self / math::sqrt(length_sq))
712        } else {
713            self
714        }
715    }
716
717    /// Returns a vector with a length no less than `min`
718    pub fn clamp_length_min(self, min: f64) -> Self {
719        let length_sq = self.length_squared();
720        if length_sq < min * min {
721            min * (self / math::sqrt(length_sq))
722        } else {
723            self
724        }
725    }
726
727    /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
728    /// error, yielding a more accurate result than an unfused multiply-add.
729    ///
730    /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
731    /// architecture has a dedicated fma CPU instruction. However, this is not always true,
732    /// and will be heavily dependant on designing algorithms with specific target hardware in
733    /// mind.
734    #[inline]
735    pub fn mul_add(self, a: Self, b: Self) -> Self {
736        Self::new(
737            math::mul_add(self.x, a.x, b.x),
738            math::mul_add(self.y, a.y, b.y),
739            math::mul_add(self.z, a.z, b.z),
740            math::mul_add(self.w, a.w, b.w),
741        )
742    }
743
744    /// Casts all elements of `self` to `f32`.
745    #[inline]
746    pub fn as_vec4(&self) -> crate::Vec4 {
747        crate::Vec4::new(self.x as f32, self.y as f32, self.z as f32, self.w as f32)
748    }
749
750    /// Casts all elements of `self` to `i32`.
751    #[inline]
752    pub fn as_ivec4(&self) -> crate::IVec4 {
753        crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
754    }
755
756    /// Casts all elements of `self` to `u32`.
757    #[inline]
758    pub fn as_uvec4(&self) -> crate::UVec4 {
759        crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
760    }
761
762    /// Casts all elements of `self` to `i64`.
763    #[inline]
764    pub fn as_i64vec4(&self) -> crate::I64Vec4 {
765        crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
766    }
767
768    /// Casts all elements of `self` to `u64`.
769    #[inline]
770    pub fn as_u64vec4(&self) -> crate::U64Vec4 {
771        crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
772    }
773}
774
775impl Default for DVec4 {
776    #[inline(always)]
777    fn default() -> Self {
778        Self::ZERO
779    }
780}
781
782impl Div<DVec4> for DVec4 {
783    type Output = Self;
784    #[inline]
785    fn div(self, rhs: Self) -> Self {
786        Self {
787            x: self.x.div(rhs.x),
788            y: self.y.div(rhs.y),
789            z: self.z.div(rhs.z),
790            w: self.w.div(rhs.w),
791        }
792    }
793}
794
795impl DivAssign<DVec4> for DVec4 {
796    #[inline]
797    fn div_assign(&mut self, rhs: Self) {
798        self.x.div_assign(rhs.x);
799        self.y.div_assign(rhs.y);
800        self.z.div_assign(rhs.z);
801        self.w.div_assign(rhs.w);
802    }
803}
804
805impl Div<f64> for DVec4 {
806    type Output = Self;
807    #[inline]
808    fn div(self, rhs: f64) -> Self {
809        Self {
810            x: self.x.div(rhs),
811            y: self.y.div(rhs),
812            z: self.z.div(rhs),
813            w: self.w.div(rhs),
814        }
815    }
816}
817
818impl DivAssign<f64> for DVec4 {
819    #[inline]
820    fn div_assign(&mut self, rhs: f64) {
821        self.x.div_assign(rhs);
822        self.y.div_assign(rhs);
823        self.z.div_assign(rhs);
824        self.w.div_assign(rhs);
825    }
826}
827
828impl Div<DVec4> for f64 {
829    type Output = DVec4;
830    #[inline]
831    fn div(self, rhs: DVec4) -> DVec4 {
832        DVec4 {
833            x: self.div(rhs.x),
834            y: self.div(rhs.y),
835            z: self.div(rhs.z),
836            w: self.div(rhs.w),
837        }
838    }
839}
840
841impl Mul<DVec4> for DVec4 {
842    type Output = Self;
843    #[inline]
844    fn mul(self, rhs: Self) -> Self {
845        Self {
846            x: self.x.mul(rhs.x),
847            y: self.y.mul(rhs.y),
848            z: self.z.mul(rhs.z),
849            w: self.w.mul(rhs.w),
850        }
851    }
852}
853
854impl MulAssign<DVec4> for DVec4 {
855    #[inline]
856    fn mul_assign(&mut self, rhs: Self) {
857        self.x.mul_assign(rhs.x);
858        self.y.mul_assign(rhs.y);
859        self.z.mul_assign(rhs.z);
860        self.w.mul_assign(rhs.w);
861    }
862}
863
864impl Mul<f64> for DVec4 {
865    type Output = Self;
866    #[inline]
867    fn mul(self, rhs: f64) -> Self {
868        Self {
869            x: self.x.mul(rhs),
870            y: self.y.mul(rhs),
871            z: self.z.mul(rhs),
872            w: self.w.mul(rhs),
873        }
874    }
875}
876
877impl MulAssign<f64> for DVec4 {
878    #[inline]
879    fn mul_assign(&mut self, rhs: f64) {
880        self.x.mul_assign(rhs);
881        self.y.mul_assign(rhs);
882        self.z.mul_assign(rhs);
883        self.w.mul_assign(rhs);
884    }
885}
886
887impl Mul<DVec4> for f64 {
888    type Output = DVec4;
889    #[inline]
890    fn mul(self, rhs: DVec4) -> DVec4 {
891        DVec4 {
892            x: self.mul(rhs.x),
893            y: self.mul(rhs.y),
894            z: self.mul(rhs.z),
895            w: self.mul(rhs.w),
896        }
897    }
898}
899
900impl Add<DVec4> for DVec4 {
901    type Output = Self;
902    #[inline]
903    fn add(self, rhs: Self) -> Self {
904        Self {
905            x: self.x.add(rhs.x),
906            y: self.y.add(rhs.y),
907            z: self.z.add(rhs.z),
908            w: self.w.add(rhs.w),
909        }
910    }
911}
912
913impl AddAssign<DVec4> for DVec4 {
914    #[inline]
915    fn add_assign(&mut self, rhs: Self) {
916        self.x.add_assign(rhs.x);
917        self.y.add_assign(rhs.y);
918        self.z.add_assign(rhs.z);
919        self.w.add_assign(rhs.w);
920    }
921}
922
923impl Add<f64> for DVec4 {
924    type Output = Self;
925    #[inline]
926    fn add(self, rhs: f64) -> Self {
927        Self {
928            x: self.x.add(rhs),
929            y: self.y.add(rhs),
930            z: self.z.add(rhs),
931            w: self.w.add(rhs),
932        }
933    }
934}
935
936impl AddAssign<f64> for DVec4 {
937    #[inline]
938    fn add_assign(&mut self, rhs: f64) {
939        self.x.add_assign(rhs);
940        self.y.add_assign(rhs);
941        self.z.add_assign(rhs);
942        self.w.add_assign(rhs);
943    }
944}
945
946impl Add<DVec4> for f64 {
947    type Output = DVec4;
948    #[inline]
949    fn add(self, rhs: DVec4) -> DVec4 {
950        DVec4 {
951            x: self.add(rhs.x),
952            y: self.add(rhs.y),
953            z: self.add(rhs.z),
954            w: self.add(rhs.w),
955        }
956    }
957}
958
959impl Sub<DVec4> for DVec4 {
960    type Output = Self;
961    #[inline]
962    fn sub(self, rhs: Self) -> Self {
963        Self {
964            x: self.x.sub(rhs.x),
965            y: self.y.sub(rhs.y),
966            z: self.z.sub(rhs.z),
967            w: self.w.sub(rhs.w),
968        }
969    }
970}
971
972impl SubAssign<DVec4> for DVec4 {
973    #[inline]
974    fn sub_assign(&mut self, rhs: DVec4) {
975        self.x.sub_assign(rhs.x);
976        self.y.sub_assign(rhs.y);
977        self.z.sub_assign(rhs.z);
978        self.w.sub_assign(rhs.w);
979    }
980}
981
982impl Sub<f64> for DVec4 {
983    type Output = Self;
984    #[inline]
985    fn sub(self, rhs: f64) -> Self {
986        Self {
987            x: self.x.sub(rhs),
988            y: self.y.sub(rhs),
989            z: self.z.sub(rhs),
990            w: self.w.sub(rhs),
991        }
992    }
993}
994
995impl SubAssign<f64> for DVec4 {
996    #[inline]
997    fn sub_assign(&mut self, rhs: f64) {
998        self.x.sub_assign(rhs);
999        self.y.sub_assign(rhs);
1000        self.z.sub_assign(rhs);
1001        self.w.sub_assign(rhs);
1002    }
1003}
1004
1005impl Sub<DVec4> for f64 {
1006    type Output = DVec4;
1007    #[inline]
1008    fn sub(self, rhs: DVec4) -> DVec4 {
1009        DVec4 {
1010            x: self.sub(rhs.x),
1011            y: self.sub(rhs.y),
1012            z: self.sub(rhs.z),
1013            w: self.sub(rhs.w),
1014        }
1015    }
1016}
1017
1018impl Rem<DVec4> for DVec4 {
1019    type Output = Self;
1020    #[inline]
1021    fn rem(self, rhs: Self) -> Self {
1022        Self {
1023            x: self.x.rem(rhs.x),
1024            y: self.y.rem(rhs.y),
1025            z: self.z.rem(rhs.z),
1026            w: self.w.rem(rhs.w),
1027        }
1028    }
1029}
1030
1031impl RemAssign<DVec4> for DVec4 {
1032    #[inline]
1033    fn rem_assign(&mut self, rhs: Self) {
1034        self.x.rem_assign(rhs.x);
1035        self.y.rem_assign(rhs.y);
1036        self.z.rem_assign(rhs.z);
1037        self.w.rem_assign(rhs.w);
1038    }
1039}
1040
1041impl Rem<f64> for DVec4 {
1042    type Output = Self;
1043    #[inline]
1044    fn rem(self, rhs: f64) -> Self {
1045        Self {
1046            x: self.x.rem(rhs),
1047            y: self.y.rem(rhs),
1048            z: self.z.rem(rhs),
1049            w: self.w.rem(rhs),
1050        }
1051    }
1052}
1053
1054impl RemAssign<f64> for DVec4 {
1055    #[inline]
1056    fn rem_assign(&mut self, rhs: f64) {
1057        self.x.rem_assign(rhs);
1058        self.y.rem_assign(rhs);
1059        self.z.rem_assign(rhs);
1060        self.w.rem_assign(rhs);
1061    }
1062}
1063
1064impl Rem<DVec4> for f64 {
1065    type Output = DVec4;
1066    #[inline]
1067    fn rem(self, rhs: DVec4) -> DVec4 {
1068        DVec4 {
1069            x: self.rem(rhs.x),
1070            y: self.rem(rhs.y),
1071            z: self.rem(rhs.z),
1072            w: self.rem(rhs.w),
1073        }
1074    }
1075}
1076
1077#[cfg(not(target_arch = "spirv"))]
1078impl AsRef<[f64; 4]> for DVec4 {
1079    #[inline]
1080    fn as_ref(&self) -> &[f64; 4] {
1081        unsafe { &*(self as *const DVec4 as *const [f64; 4]) }
1082    }
1083}
1084
1085#[cfg(not(target_arch = "spirv"))]
1086impl AsMut<[f64; 4]> for DVec4 {
1087    #[inline]
1088    fn as_mut(&mut self) -> &mut [f64; 4] {
1089        unsafe { &mut *(self as *mut DVec4 as *mut [f64; 4]) }
1090    }
1091}
1092
1093impl Sum for DVec4 {
1094    #[inline]
1095    fn sum<I>(iter: I) -> Self
1096    where
1097        I: Iterator<Item = Self>,
1098    {
1099        iter.fold(Self::ZERO, Self::add)
1100    }
1101}
1102
1103impl<'a> Sum<&'a Self> for DVec4 {
1104    #[inline]
1105    fn sum<I>(iter: I) -> Self
1106    where
1107        I: Iterator<Item = &'a Self>,
1108    {
1109        iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1110    }
1111}
1112
1113impl Product for DVec4 {
1114    #[inline]
1115    fn product<I>(iter: I) -> Self
1116    where
1117        I: Iterator<Item = Self>,
1118    {
1119        iter.fold(Self::ONE, Self::mul)
1120    }
1121}
1122
1123impl<'a> Product<&'a Self> for DVec4 {
1124    #[inline]
1125    fn product<I>(iter: I) -> Self
1126    where
1127        I: Iterator<Item = &'a Self>,
1128    {
1129        iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1130    }
1131}
1132
1133impl Neg for DVec4 {
1134    type Output = Self;
1135    #[inline]
1136    fn neg(self) -> Self {
1137        Self {
1138            x: self.x.neg(),
1139            y: self.y.neg(),
1140            z: self.z.neg(),
1141            w: self.w.neg(),
1142        }
1143    }
1144}
1145
1146impl Index<usize> for DVec4 {
1147    type Output = f64;
1148    #[inline]
1149    fn index(&self, index: usize) -> &Self::Output {
1150        match index {
1151            0 => &self.x,
1152            1 => &self.y,
1153            2 => &self.z,
1154            3 => &self.w,
1155            _ => panic!("index out of bounds"),
1156        }
1157    }
1158}
1159
1160impl IndexMut<usize> for DVec4 {
1161    #[inline]
1162    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1163        match index {
1164            0 => &mut self.x,
1165            1 => &mut self.y,
1166            2 => &mut self.z,
1167            3 => &mut self.w,
1168            _ => panic!("index out of bounds"),
1169        }
1170    }
1171}
1172
1173#[cfg(not(target_arch = "spirv"))]
1174impl fmt::Display for DVec4 {
1175    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1176        write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1177    }
1178}
1179
1180#[cfg(not(target_arch = "spirv"))]
1181impl fmt::Debug for DVec4 {
1182    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1183        fmt.debug_tuple(stringify!(DVec4))
1184            .field(&self.x)
1185            .field(&self.y)
1186            .field(&self.z)
1187            .field(&self.w)
1188            .finish()
1189    }
1190}
1191
1192impl From<[f64; 4]> for DVec4 {
1193    #[inline]
1194    fn from(a: [f64; 4]) -> Self {
1195        Self::new(a[0], a[1], a[2], a[3])
1196    }
1197}
1198
1199impl From<DVec4> for [f64; 4] {
1200    #[inline]
1201    fn from(v: DVec4) -> Self {
1202        [v.x, v.y, v.z, v.w]
1203    }
1204}
1205
1206impl From<(f64, f64, f64, f64)> for DVec4 {
1207    #[inline]
1208    fn from(t: (f64, f64, f64, f64)) -> Self {
1209        Self::new(t.0, t.1, t.2, t.3)
1210    }
1211}
1212
1213impl From<DVec4> for (f64, f64, f64, f64) {
1214    #[inline]
1215    fn from(v: DVec4) -> Self {
1216        (v.x, v.y, v.z, v.w)
1217    }
1218}
1219
1220impl From<(DVec3, f64)> for DVec4 {
1221    #[inline]
1222    fn from((v, w): (DVec3, f64)) -> Self {
1223        Self::new(v.x, v.y, v.z, w)
1224    }
1225}
1226
1227impl From<(f64, DVec3)> for DVec4 {
1228    #[inline]
1229    fn from((x, v): (f64, DVec3)) -> Self {
1230        Self::new(x, v.x, v.y, v.z)
1231    }
1232}
1233
1234impl From<(DVec2, f64, f64)> for DVec4 {
1235    #[inline]
1236    fn from((v, z, w): (DVec2, f64, f64)) -> Self {
1237        Self::new(v.x, v.y, z, w)
1238    }
1239}
1240
1241impl From<(DVec2, DVec2)> for DVec4 {
1242    #[inline]
1243    fn from((v, u): (DVec2, DVec2)) -> Self {
1244        Self::new(v.x, v.y, u.x, u.y)
1245    }
1246}
1247
1248impl From<Vec4> for DVec4 {
1249    #[inline]
1250    fn from(v: Vec4) -> Self {
1251        Self::new(
1252            f64::from(v.x),
1253            f64::from(v.y),
1254            f64::from(v.z),
1255            f64::from(v.w),
1256        )
1257    }
1258}
1259
1260impl From<IVec4> for DVec4 {
1261    #[inline]
1262    fn from(v: IVec4) -> Self {
1263        Self::new(
1264            f64::from(v.x),
1265            f64::from(v.y),
1266            f64::from(v.z),
1267            f64::from(v.w),
1268        )
1269    }
1270}
1271
1272impl From<UVec4> for DVec4 {
1273    #[inline]
1274    fn from(v: UVec4) -> Self {
1275        Self::new(
1276            f64::from(v.x),
1277            f64::from(v.y),
1278            f64::from(v.z),
1279            f64::from(v.w),
1280        )
1281    }
1282}