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