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