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