glam/f64/
dvec3.rs

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