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