Skip to main content

glam/f64/
dvec3.rs

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