glam/f32/
vec3.rs

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