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