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