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