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