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