glam/f32/
vec3.rs

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