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