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