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