Skip to main content

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