1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2 
3 use crate::{coresimd::*, f32::math, BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4};
4 
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::{f32, ops::*};
8 
9 use core::simd::{cmp::SimdPartialEq, cmp::SimdPartialOrd, num::SimdFloat, *};
10 use std::simd::StdFloat;
11 
12 /// Creates a 3-dimensional vector.
13 #[inline(always)]
14 #[must_use]
vec3a(x: f32, y: f32, z: f32) -> Vec3A15 pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
16     Vec3A::new(x, y, z)
17 }
18 
19 /// A 3-dimensional vector.
20 ///
21 /// SIMD vector types are used for storage on supported platforms for better
22 /// performance than the [`Vec3`] type.
23 ///
24 /// It is possible to convert between [`Vec3`] and [`Vec3A`] types using [`From`]
25 /// or [`Into`] trait implementations.
26 ///
27 /// This type is 16 byte aligned.
28 #[derive(Clone, Copy)]
29 #[repr(transparent)]
30 pub struct Vec3A(pub(crate) f32x4);
31 
32 impl Vec3A {
33     /// All zeroes.
34     pub const ZERO: Self = Self::splat(0.0);
35 
36     /// All ones.
37     pub const ONE: Self = Self::splat(1.0);
38 
39     /// All negative ones.
40     pub const NEG_ONE: Self = Self::splat(-1.0);
41 
42     /// All `f32::MIN`.
43     pub const MIN: Self = Self::splat(f32::MIN);
44 
45     /// All `f32::MAX`.
46     pub const MAX: Self = Self::splat(f32::MAX);
47 
48     /// All `f32::NAN`.
49     pub const NAN: Self = Self::splat(f32::NAN);
50 
51     /// All `f32::INFINITY`.
52     pub const INFINITY: Self = Self::splat(f32::INFINITY);
53 
54     /// All `f32::NEG_INFINITY`.
55     pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
56 
57     /// A unit vector pointing along the positive X axis.
58     pub const X: Self = Self::new(1.0, 0.0, 0.0);
59 
60     /// A unit vector pointing along the positive Y axis.
61     pub const Y: Self = Self::new(0.0, 1.0, 0.0);
62 
63     /// A unit vector pointing along the positive Z axis.
64     pub const Z: Self = Self::new(0.0, 0.0, 1.0);
65 
66     /// A unit vector pointing along the negative X axis.
67     pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
68 
69     /// A unit vector pointing along the negative Y axis.
70     pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
71 
72     /// A unit vector pointing along the negative Z axis.
73     pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
74 
75     /// The unit axes.
76     pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
77 
78     /// Creates a new vector.
79     #[inline(always)]
80     #[must_use]
new(x: f32, y: f32, z: f32) -> Self81     pub const fn new(x: f32, y: f32, z: f32) -> Self {
82         Self(f32x4::from_array([x, y, z, z]))
83     }
84 
85     /// Creates a vector with all elements set to `v`.
86     #[inline]
87     #[must_use]
splat(v: f32) -> Self88     pub const fn splat(v: f32) -> Self {
89         Self(Simd::from_array([v; 4]))
90     }
91 
92     /// Returns a vector containing each element of `self` modified by a mapping function `f`.
93     #[inline]
94     #[must_use]
map<F>(self, f: F) -> Self where F: Fn(f32) -> f32,95     pub fn map<F>(self, f: F) -> Self
96     where
97         F: Fn(f32) -> f32,
98     {
99         Self::new(f(self.x), f(self.y), f(self.z))
100     }
101 
102     /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
103     /// for each element of `self`.
104     ///
105     /// A true element in the mask uses the corresponding element from `if_true`, and false
106     /// uses the element from `if_false`.
107     #[inline]
108     #[must_use]
select(mask: BVec3A, if_true: Self, if_false: Self) -> Self109     pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
110         Self(mask.0.select(if_true.0, if_false.0))
111     }
112 
113     /// Creates a new vector from an array.
114     #[inline]
115     #[must_use]
from_array(a: [f32; 3]) -> Self116     pub const fn from_array(a: [f32; 3]) -> Self {
117         Self::new(a[0], a[1], a[2])
118     }
119 
120     /// `[x, y, z]`
121     #[inline]
122     #[must_use]
to_array(&self) -> [f32; 3]123     pub const fn to_array(&self) -> [f32; 3] {
124         unsafe { *(self as *const Vec3A as *const [f32; 3]) }
125     }
126 
127     /// Creates a vector from the first 3 values in `slice`.
128     ///
129     /// # Panics
130     ///
131     /// Panics if `slice` is less than 3 elements long.
132     #[inline]
133     #[must_use]
from_slice(slice: &[f32]) -> Self134     pub const fn from_slice(slice: &[f32]) -> Self {
135         assert!(slice.len() >= 3);
136         Self::new(slice[0], slice[1], slice[2])
137     }
138 
139     /// Writes the elements of `self` to the first 3 elements in `slice`.
140     ///
141     /// # Panics
142     ///
143     /// Panics if `slice` is less than 3 elements long.
144     #[inline]
write_to_slice(self, slice: &mut [f32])145     pub fn write_to_slice(self, slice: &mut [f32]) {
146         slice[..3].copy_from_slice(&self.to_array());
147     }
148 
149     /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
150     ///
151     /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
152     #[inline]
153     #[must_use]
from_vec4(v: Vec4) -> Self154     pub fn from_vec4(v: Vec4) -> Self {
155         Self(v.0)
156     }
157 
158     /// Creates a 4D vector from `self` and the given `w` value.
159     #[inline]
160     #[must_use]
extend(self, w: f32) -> Vec4161     pub fn extend(self, w: f32) -> Vec4 {
162         Vec4::new(self.x, self.y, self.z, w)
163     }
164 
165     /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
166     ///
167     /// Truncation may also be performed by using [`self.xy()`][crate::swizzles::Vec3Swizzles::xy()].
168     #[inline]
169     #[must_use]
truncate(self) -> Vec2170     pub fn truncate(self) -> Vec2 {
171         use crate::swizzles::Vec3Swizzles;
172         self.xy()
173     }
174 
175     /// Creates a 3D vector from `self` with the given value of `x`.
176     #[inline]
177     #[must_use]
with_x(mut self, x: f32) -> Self178     pub fn with_x(mut self, x: f32) -> Self {
179         self.x = x;
180         self
181     }
182 
183     /// Creates a 3D vector from `self` with the given value of `y`.
184     #[inline]
185     #[must_use]
with_y(mut self, y: f32) -> Self186     pub fn with_y(mut self, y: f32) -> Self {
187         self.y = y;
188         self
189     }
190 
191     /// Creates a 3D vector from `self` with the given value of `z`.
192     #[inline]
193     #[must_use]
with_z(mut self, z: f32) -> Self194     pub fn with_z(mut self, z: f32) -> Self {
195         self.z = z;
196         self
197     }
198 
199     /// Computes the dot product of `self` and `rhs`.
200     #[inline]
201     #[must_use]
dot(self, rhs: Self) -> f32202     pub fn dot(self, rhs: Self) -> f32 {
203         dot3(self.0, rhs.0)
204     }
205 
206     /// Returns a vector where every component is the dot product of `self` and `rhs`.
207     #[inline]
208     #[must_use]
dot_into_vec(self, rhs: Self) -> Self209     pub fn dot_into_vec(self, rhs: Self) -> Self {
210         Self(dot3_into_f32x4(self.0, rhs.0))
211     }
212 
213     /// Computes the cross product of `self` and `rhs`.
214     #[inline]
215     #[must_use]
cross(self, rhs: Self) -> Self216     pub fn cross(self, rhs: Self) -> Self {
217         let lhszxy = simd_swizzle!(self.0, [2, 0, 1, 1]);
218         let rhszxy = simd_swizzle!(rhs.0, [2, 0, 1, 1]);
219         let lhszxy_rhs = lhszxy * rhs.0;
220         let rhszxy_lhs = rhszxy * self.0;
221         let sub = lhszxy_rhs - rhszxy_lhs;
222         Self(simd_swizzle!(sub, [2, 0, 1, 1]))
223     }
224 
225     /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
226     ///
227     /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
228     #[inline]
229     #[must_use]
min(self, rhs: Self) -> Self230     pub fn min(self, rhs: Self) -> Self {
231         Self(self.0.simd_min(rhs.0))
232     }
233 
234     /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
235     ///
236     /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
237     #[inline]
238     #[must_use]
max(self, rhs: Self) -> Self239     pub fn max(self, rhs: Self) -> Self {
240         Self(self.0.simd_max(rhs.0))
241     }
242 
243     /// Component-wise clamping of values, similar to [`f32::clamp`].
244     ///
245     /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
246     ///
247     /// # Panics
248     ///
249     /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
250     #[inline]
251     #[must_use]
clamp(self, min: Self, max: Self) -> Self252     pub fn clamp(self, min: Self, max: Self) -> Self {
253         glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
254         self.max(min).min(max)
255     }
256 
257     /// Returns the horizontal minimum of `self`.
258     ///
259     /// In other words this computes `min(x, y, ..)`.
260     #[inline]
261     #[must_use]
min_element(self) -> f32262     pub fn min_element(self) -> f32 {
263         let v = self.0;
264         let v = v.simd_min(simd_swizzle!(v, [2, 2, 1, 1]));
265         let v = v.simd_min(simd_swizzle!(v, [1, 0, 0, 0]));
266         v[0]
267     }
268 
269     /// Returns the horizontal maximum of `self`.
270     ///
271     /// In other words this computes `max(x, y, ..)`.
272     #[inline]
273     #[must_use]
max_element(self) -> f32274     pub fn max_element(self) -> f32 {
275         let v = self.0;
276         let v = v.simd_max(simd_swizzle!(v, [2, 2, 0, 0]));
277         let v = v.simd_max(simd_swizzle!(v, [1, 0, 0, 0]));
278         v[0]
279     }
280 
281     /// Returns the sum of all elements of `self`.
282     ///
283     /// In other words, this computes `self.x + self.y + ..`.
284     #[inline]
285     #[must_use]
element_sum(self) -> f32286     pub fn element_sum(self) -> f32 {
287         simd_swizzle!(self.0, Self::ZERO.0, [0, 1, 2, 4]).reduce_sum()
288     }
289 
290     /// Returns the product of all elements of `self`.
291     ///
292     /// In other words, this computes `self.x * self.y * ..`.
293     #[inline]
294     #[must_use]
element_product(self) -> f32295     pub fn element_product(self) -> f32 {
296         simd_swizzle!(self.0, Self::ONE.0, [0, 1, 2, 4]).reduce_product()
297     }
298 
299     /// Returns a vector mask containing the result of a `==` comparison for each element of
300     /// `self` and `rhs`.
301     ///
302     /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
303     /// elements.
304     #[inline]
305     #[must_use]
cmpeq(self, rhs: Self) -> BVec3A306     pub fn cmpeq(self, rhs: Self) -> BVec3A {
307         BVec3A(f32x4::simd_eq(self.0, rhs.0))
308     }
309 
310     /// Returns a vector mask containing the result of a `!=` comparison for each element of
311     /// `self` and `rhs`.
312     ///
313     /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
314     /// elements.
315     #[inline]
316     #[must_use]
cmpne(self, rhs: Self) -> BVec3A317     pub fn cmpne(self, rhs: Self) -> BVec3A {
318         BVec3A(f32x4::simd_ne(self.0, rhs.0))
319     }
320 
321     /// Returns a vector mask containing the result of a `>=` comparison for each element of
322     /// `self` and `rhs`.
323     ///
324     /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
325     /// elements.
326     #[inline]
327     #[must_use]
cmpge(self, rhs: Self) -> BVec3A328     pub fn cmpge(self, rhs: Self) -> BVec3A {
329         BVec3A(f32x4::simd_ge(self.0, rhs.0))
330     }
331 
332     /// Returns a vector mask containing the result of a `>` comparison for each element of
333     /// `self` and `rhs`.
334     ///
335     /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
336     /// elements.
337     #[inline]
338     #[must_use]
cmpgt(self, rhs: Self) -> BVec3A339     pub fn cmpgt(self, rhs: Self) -> BVec3A {
340         BVec3A(f32x4::simd_gt(self.0, rhs.0))
341     }
342 
343     /// Returns a vector mask containing the result of a `<=` comparison for each element of
344     /// `self` and `rhs`.
345     ///
346     /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
347     /// elements.
348     #[inline]
349     #[must_use]
cmple(self, rhs: Self) -> BVec3A350     pub fn cmple(self, rhs: Self) -> BVec3A {
351         BVec3A(f32x4::simd_le(self.0, rhs.0))
352     }
353 
354     /// Returns a vector mask containing the result of a `<` comparison for each element of
355     /// `self` and `rhs`.
356     ///
357     /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
358     /// elements.
359     #[inline]
360     #[must_use]
cmplt(self, rhs: Self) -> BVec3A361     pub fn cmplt(self, rhs: Self) -> BVec3A {
362         BVec3A(f32x4::simd_lt(self.0, rhs.0))
363     }
364 
365     /// Returns a vector containing the absolute value of each element of `self`.
366     #[inline]
367     #[must_use]
abs(self) -> Self368     pub fn abs(self) -> Self {
369         Self(self.0.abs())
370     }
371 
372     /// Returns a vector with elements representing the sign of `self`.
373     ///
374     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
375     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
376     /// - `NAN` if the number is `NAN`
377     #[inline]
378     #[must_use]
signum(self) -> Self379     pub fn signum(self) -> Self {
380         Self(self.0.signum())
381     }
382 
383     /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
384     #[inline]
385     #[must_use]
copysign(self, rhs: Self) -> Self386     pub fn copysign(self, rhs: Self) -> Self {
387         Self(self.0.copysign(rhs.0))
388     }
389 
390     /// Returns a bitmask with the lowest 3 bits set to the sign bits from the elements of `self`.
391     ///
392     /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
393     /// into the first lowest bit, element `y` into the second, etc.
394     #[inline]
395     #[must_use]
is_negative_bitmask(self) -> u32396     pub fn is_negative_bitmask(self) -> u32 {
397         (self.0.is_sign_negative().to_bitmask() & 0x7) as u32
398     }
399 
400     /// Returns `true` if, and only if, all elements are finite.  If any element is either
401     /// `NaN`, positive or negative infinity, this will return `false`.
402     #[inline]
403     #[must_use]
is_finite(self) -> bool404     pub fn is_finite(self) -> bool {
405         self.is_finite_mask().all()
406     }
407 
408     /// Performs `is_finite` on each element of self, returning a vector mask of the results.
409     ///
410     /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
is_finite_mask(self) -> BVec3A411     pub fn is_finite_mask(self) -> BVec3A {
412         BVec3A(f32x4::is_finite(self.0))
413     }
414 
415     /// Returns `true` if any elements are `NaN`.
416     #[inline]
417     #[must_use]
is_nan(self) -> bool418     pub fn is_nan(self) -> bool {
419         self.is_nan_mask().any()
420     }
421 
422     /// Performs `is_nan` on each element of self, returning a vector mask of the results.
423     ///
424     /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
425     #[inline]
426     #[must_use]
is_nan_mask(self) -> BVec3A427     pub fn is_nan_mask(self) -> BVec3A {
428         BVec3A(f32x4::is_nan(self.0))
429     }
430 
431     /// Computes the length of `self`.
432     #[doc(alias = "magnitude")]
433     #[inline]
434     #[must_use]
length(self) -> f32435     pub fn length(self) -> f32 {
436         let dot = dot3_in_x(self.0, self.0);
437         dot.sqrt()[0]
438     }
439 
440     /// Computes the squared length of `self`.
441     ///
442     /// This is faster than `length()` as it avoids a square root operation.
443     #[doc(alias = "magnitude2")]
444     #[inline]
445     #[must_use]
length_squared(self) -> f32446     pub fn length_squared(self) -> f32 {
447         self.dot(self)
448     }
449 
450     /// Computes `1.0 / length()`.
451     ///
452     /// For valid results, `self` must _not_ be of length zero.
453     #[inline]
454     #[must_use]
length_recip(self) -> f32455     pub fn length_recip(self) -> f32 {
456         let dot = dot3_in_x(self.0, self.0);
457         dot.sqrt().recip()[0]
458     }
459 
460     /// Computes the Euclidean distance between two points in space.
461     #[inline]
462     #[must_use]
distance(self, rhs: Self) -> f32463     pub fn distance(self, rhs: Self) -> f32 {
464         (self - rhs).length()
465     }
466 
467     /// Compute the squared euclidean distance between two points in space.
468     #[inline]
469     #[must_use]
distance_squared(self, rhs: Self) -> f32470     pub fn distance_squared(self, rhs: Self) -> f32 {
471         (self - rhs).length_squared()
472     }
473 
474     /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
475     #[inline]
476     #[must_use]
div_euclid(self, rhs: Self) -> Self477     pub fn div_euclid(self, rhs: Self) -> Self {
478         Self::new(
479             math::div_euclid(self.x, rhs.x),
480             math::div_euclid(self.y, rhs.y),
481             math::div_euclid(self.z, rhs.z),
482         )
483     }
484 
485     /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
486     ///
487     /// [Euclidean division]: f32::rem_euclid
488     #[inline]
489     #[must_use]
rem_euclid(self, rhs: Self) -> Self490     pub fn rem_euclid(self, rhs: Self) -> Self {
491         Self::new(
492             math::rem_euclid(self.x, rhs.x),
493             math::rem_euclid(self.y, rhs.y),
494             math::rem_euclid(self.z, rhs.z),
495         )
496     }
497 
498     /// Returns `self` normalized to length 1.0.
499     ///
500     /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
501     ///
502     /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
503     ///
504     /// Panics
505     ///
506     /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
507     #[inline]
508     #[must_use]
normalize(self) -> Self509     pub fn normalize(self) -> Self {
510         let length = dot3_into_f32x4(self.0, self.0).sqrt();
511         #[allow(clippy::let_and_return)]
512         let normalized = Self(self.0 / length);
513         glam_assert!(normalized.is_finite());
514         normalized
515     }
516 
517     /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
518     ///
519     /// In particular, if the input is zero (or very close to zero), or non-finite,
520     /// the result of this operation will be `None`.
521     ///
522     /// See also [`Self::normalize_or_zero()`].
523     #[inline]
524     #[must_use]
try_normalize(self) -> Option<Self>525     pub fn try_normalize(self) -> Option<Self> {
526         let rcp = self.length_recip();
527         if rcp.is_finite() && rcp > 0.0 {
528             Some(self * rcp)
529         } else {
530             None
531         }
532     }
533 
534     /// Returns `self` normalized to length 1.0 if possible, else returns a
535     /// fallback value.
536     ///
537     /// In particular, if the input is zero (or very close to zero), or non-finite,
538     /// the result of this operation will be the fallback value.
539     ///
540     /// See also [`Self::try_normalize()`].
541     #[inline]
542     #[must_use]
normalize_or(self, fallback: Self) -> Self543     pub fn normalize_or(self, fallback: Self) -> Self {
544         let rcp = self.length_recip();
545         if rcp.is_finite() && rcp > 0.0 {
546             self * rcp
547         } else {
548             fallback
549         }
550     }
551 
552     /// Returns `self` normalized to length 1.0 if possible, else returns zero.
553     ///
554     /// In particular, if the input is zero (or very close to zero), or non-finite,
555     /// the result of this operation will be zero.
556     ///
557     /// See also [`Self::try_normalize()`].
558     #[inline]
559     #[must_use]
normalize_or_zero(self) -> Self560     pub fn normalize_or_zero(self) -> Self {
561         self.normalize_or(Self::ZERO)
562     }
563 
564     /// Returns whether `self` is length `1.0` or not.
565     ///
566     /// Uses a precision threshold of approximately `1e-4`.
567     #[inline]
568     #[must_use]
is_normalized(self) -> bool569     pub fn is_normalized(self) -> bool {
570         math::abs(self.length_squared() - 1.0) <= 2e-4
571     }
572 
573     /// Returns the vector projection of `self` onto `rhs`.
574     ///
575     /// `rhs` must be of non-zero length.
576     ///
577     /// # Panics
578     ///
579     /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
580     #[inline]
581     #[must_use]
project_onto(self, rhs: Self) -> Self582     pub fn project_onto(self, rhs: Self) -> Self {
583         let other_len_sq_rcp = rhs.dot(rhs).recip();
584         glam_assert!(other_len_sq_rcp.is_finite());
585         rhs * self.dot(rhs) * other_len_sq_rcp
586     }
587 
588     /// Returns the vector rejection of `self` from `rhs`.
589     ///
590     /// The vector rejection is the vector perpendicular to the projection of `self` onto
591     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
592     ///
593     /// `rhs` must be of non-zero length.
594     ///
595     /// # Panics
596     ///
597     /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
598     #[doc(alias("plane"))]
599     #[inline]
600     #[must_use]
reject_from(self, rhs: Self) -> Self601     pub fn reject_from(self, rhs: Self) -> Self {
602         self - self.project_onto(rhs)
603     }
604 
605     /// Returns the vector projection of `self` onto `rhs`.
606     ///
607     /// `rhs` must be normalized.
608     ///
609     /// # Panics
610     ///
611     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
612     #[inline]
613     #[must_use]
project_onto_normalized(self, rhs: Self) -> Self614     pub fn project_onto_normalized(self, rhs: Self) -> Self {
615         glam_assert!(rhs.is_normalized());
616         rhs * self.dot(rhs)
617     }
618 
619     /// Returns the vector rejection of `self` from `rhs`.
620     ///
621     /// The vector rejection is the vector perpendicular to the projection of `self` onto
622     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
623     ///
624     /// `rhs` must be normalized.
625     ///
626     /// # Panics
627     ///
628     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
629     #[doc(alias("plane"))]
630     #[inline]
631     #[must_use]
reject_from_normalized(self, rhs: Self) -> Self632     pub fn reject_from_normalized(self, rhs: Self) -> Self {
633         self - self.project_onto_normalized(rhs)
634     }
635 
636     /// Returns a vector containing the nearest integer to a number for each element of `self`.
637     /// Round half-way cases away from 0.0.
638     #[inline]
639     #[must_use]
round(self) -> Self640     pub fn round(self) -> Self {
641         Self(self.0.round())
642     }
643 
644     /// Returns a vector containing the largest integer less than or equal to a number for each
645     /// element of `self`.
646     #[inline]
647     #[must_use]
floor(self) -> Self648     pub fn floor(self) -> Self {
649         Self(self.0.floor())
650     }
651 
652     /// Returns a vector containing the smallest integer greater than or equal to a number for
653     /// each element of `self`.
654     #[inline]
655     #[must_use]
ceil(self) -> Self656     pub fn ceil(self) -> Self {
657         Self(self.0.ceil())
658     }
659 
660     /// Returns a vector containing the integer part each element of `self`. This means numbers are
661     /// always truncated towards zero.
662     #[inline]
663     #[must_use]
trunc(self) -> Self664     pub fn trunc(self) -> Self {
665         Self(self.0.trunc())
666     }
667 
668     /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
669     ///
670     /// Note that this differs from the GLSL implementation of `fract` which returns
671     /// `self - self.floor()`.
672     ///
673     /// Note that this is fast but not precise for large numbers.
674     #[inline]
675     #[must_use]
fract(self) -> Self676     pub fn fract(self) -> Self {
677         self - self.trunc()
678     }
679 
680     /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
681     ///
682     /// Note that this differs from the Rust implementation of `fract` which returns
683     /// `self - self.trunc()`.
684     ///
685     /// Note that this is fast but not precise for large numbers.
686     #[inline]
687     #[must_use]
fract_gl(self) -> Self688     pub fn fract_gl(self) -> Self {
689         self - self.floor()
690     }
691 
692     /// Returns a vector containing `e^self` (the exponential function) for each element of
693     /// `self`.
694     #[inline]
695     #[must_use]
exp(self) -> Self696     pub fn exp(self) -> Self {
697         Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
698     }
699 
700     /// Returns a vector containing each element of `self` raised to the power of `n`.
701     #[inline]
702     #[must_use]
powf(self, n: f32) -> Self703     pub fn powf(self, n: f32) -> Self {
704         Self::new(
705             math::powf(self.x, n),
706             math::powf(self.y, n),
707             math::powf(self.z, n),
708         )
709     }
710 
711     /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
712     #[inline]
713     #[must_use]
recip(self) -> Self714     pub fn recip(self) -> Self {
715         Self(self.0.recip())
716     }
717 
718     /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
719     ///
720     /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
721     /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
722     /// extrapolated.
723     #[doc(alias = "mix")]
724     #[inline]
725     #[must_use]
lerp(self, rhs: Self, s: f32) -> Self726     pub fn lerp(self, rhs: Self, s: f32) -> Self {
727         self * (1.0 - s) + rhs * s
728     }
729 
730     /// Moves towards `rhs` based on the value `d`.
731     ///
732     /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
733     /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
734     #[inline]
735     #[must_use]
move_towards(&self, rhs: Self, d: f32) -> Self736     pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
737         let a = rhs - *self;
738         let len = a.length();
739         if len <= d || len <= 1e-4 {
740             return rhs;
741         }
742         *self + a / len * d
743     }
744 
745     /// Calculates the midpoint between `self` and `rhs`.
746     ///
747     /// The midpoint is the average of, or halfway point between, two vectors.
748     /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
749     /// while being slightly cheaper to compute.
750     #[inline]
midpoint(self, rhs: Self) -> Self751     pub fn midpoint(self, rhs: Self) -> Self {
752         (self + rhs) * 0.5
753     }
754 
755     /// Returns true if the absolute difference of all elements between `self` and `rhs` is
756     /// less than or equal to `max_abs_diff`.
757     ///
758     /// This can be used to compare if two vectors contain similar elements. It works best when
759     /// comparing with a known value. The `max_abs_diff` that should be used used depends on
760     /// the values being compared against.
761     ///
762     /// For more see
763     /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
764     #[inline]
765     #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool766     pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
767         self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
768     }
769 
770     /// Returns a vector with a length no less than `min` and no more than `max`.
771     ///
772     /// # Panics
773     ///
774     /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
775     #[inline]
776     #[must_use]
clamp_length(self, min: f32, max: f32) -> Self777     pub fn clamp_length(self, min: f32, max: f32) -> Self {
778         glam_assert!(0.0 <= min);
779         glam_assert!(min <= max);
780         let length_sq = self.length_squared();
781         if length_sq < min * min {
782             min * (self / math::sqrt(length_sq))
783         } else if length_sq > max * max {
784             max * (self / math::sqrt(length_sq))
785         } else {
786             self
787         }
788     }
789 
790     /// Returns a vector with a length no more than `max`.
791     ///
792     /// # Panics
793     ///
794     /// Will panic if `max` is negative when `glam_assert` is enabled.
795     #[inline]
796     #[must_use]
clamp_length_max(self, max: f32) -> Self797     pub fn clamp_length_max(self, max: f32) -> Self {
798         glam_assert!(0.0 <= max);
799         let length_sq = self.length_squared();
800         if length_sq > max * max {
801             max * (self / math::sqrt(length_sq))
802         } else {
803             self
804         }
805     }
806 
807     /// Returns a vector with a length no less than `min`.
808     ///
809     /// # Panics
810     ///
811     /// Will panic if `min` is negative when `glam_assert` is enabled.
812     #[inline]
813     #[must_use]
clamp_length_min(self, min: f32) -> Self814     pub fn clamp_length_min(self, min: f32) -> Self {
815         glam_assert!(0.0 <= min);
816         let length_sq = self.length_squared();
817         if length_sq < min * min {
818             min * (self / math::sqrt(length_sq))
819         } else {
820             self
821         }
822     }
823 
824     /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
825     /// error, yielding a more accurate result than an unfused multiply-add.
826     ///
827     /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
828     /// architecture has a dedicated fma CPU instruction. However, this is not always true,
829     /// and will be heavily dependant on designing algorithms with specific target hardware in
830     /// mind.
831     #[inline]
832     #[must_use]
mul_add(self, a: Self, b: Self) -> Self833     pub fn mul_add(self, a: Self, b: Self) -> Self {
834         Self(self.0.mul_add(a.0, b.0))
835     }
836 
837     /// Returns the reflection vector for a given incident vector `self` and surface normal
838     /// `normal`.
839     ///
840     /// `normal` must be normalized.
841     ///
842     /// # Panics
843     ///
844     /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
845     #[inline]
846     #[must_use]
reflect(self, normal: Self) -> Self847     pub fn reflect(self, normal: Self) -> Self {
848         glam_assert!(normal.is_normalized());
849         self - 2.0 * self.dot(normal) * normal
850     }
851 
852     /// Returns the refraction direction for a given incident vector `self`, surface normal
853     /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
854     /// a zero vector will be returned.
855     ///
856     /// `self` and `normal` must be normalized.
857     ///
858     /// # Panics
859     ///
860     /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
861     #[inline]
862     #[must_use]
refract(self, normal: Self, eta: f32) -> Self863     pub fn refract(self, normal: Self, eta: f32) -> Self {
864         glam_assert!(self.is_normalized());
865         glam_assert!(normal.is_normalized());
866         let n_dot_i = normal.dot(self);
867         let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
868         if k >= 0.0 {
869             eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
870         } else {
871             Self::ZERO
872         }
873     }
874 
875     /// Returns the angle (in radians) between two vectors in the range `[0, +π]`.
876     ///
877     /// The inputs do not need to be unit vectors however they must be non-zero.
878     #[inline]
879     #[must_use]
angle_between(self, rhs: Self) -> f32880     pub fn angle_between(self, rhs: Self) -> f32 {
881         math::acos_approx(
882             self.dot(rhs)
883                 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
884         )
885     }
886 
887     /// Returns some vector that is orthogonal to the given one.
888     ///
889     /// The input vector must be finite and non-zero.
890     ///
891     /// The output vector is not necessarily unit length. For that use
892     /// [`Self::any_orthonormal_vector()`] instead.
893     #[inline]
894     #[must_use]
any_orthogonal_vector(&self) -> Self895     pub fn any_orthogonal_vector(&self) -> Self {
896         // This can probably be optimized
897         if math::abs(self.x) > math::abs(self.y) {
898             Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
899         } else {
900             Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
901         }
902     }
903 
904     /// Returns any unit vector that is orthogonal to the given one.
905     ///
906     /// The input vector must be unit length.
907     ///
908     /// # Panics
909     ///
910     /// Will panic if `self` is not normalized when `glam_assert` is enabled.
911     #[inline]
912     #[must_use]
any_orthonormal_vector(&self) -> Self913     pub fn any_orthonormal_vector(&self) -> Self {
914         glam_assert!(self.is_normalized());
915         // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
916         let sign = math::signum(self.z);
917         let a = -1.0 / (sign + self.z);
918         let b = self.x * self.y * a;
919         Self::new(b, sign + self.y * self.y * a, -self.y)
920     }
921 
922     /// Given a unit vector return two other vectors that together form an orthonormal
923     /// basis. That is, all three vectors are orthogonal to each other and are normalized.
924     ///
925     /// # Panics
926     ///
927     /// Will panic if `self` is not normalized when `glam_assert` is enabled.
928     #[inline]
929     #[must_use]
any_orthonormal_pair(&self) -> (Self, Self)930     pub fn any_orthonormal_pair(&self) -> (Self, Self) {
931         glam_assert!(self.is_normalized());
932         // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
933         let sign = math::signum(self.z);
934         let a = -1.0 / (sign + self.z);
935         let b = self.x * self.y * a;
936         (
937             Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
938             Self::new(b, sign + self.y * self.y * a, -self.y),
939         )
940     }
941 
942     /// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`.
943     ///
944     /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
945     /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
946     /// extrapolated.
947     #[inline]
948     #[must_use]
slerp(self, rhs: Self, s: f32) -> Self949     pub fn slerp(self, rhs: Self, s: f32) -> Self {
950         let self_length = self.length();
951         let rhs_length = rhs.length();
952         // Cosine of the angle between the vectors [-1, 1], or NaN if either vector has a zero length
953         let dot = self.dot(rhs) / (self_length * rhs_length);
954         // If dot is close to 1 or -1, or is NaN the calculations for t1 and t2 break down
955         if math::abs(dot) < 1.0 - 3e-7 {
956             // Angle between the vectors [0, +π]
957             let theta = math::acos_approx(dot);
958             // Sine of the angle between vectors [0, 1]
959             let sin_theta = math::sin(theta);
960             let t1 = math::sin(theta * (1. - s));
961             let t2 = math::sin(theta * s);
962 
963             // Interpolate vector lengths
964             let result_length = self_length.lerp(rhs_length, s);
965             // Scale the vectors to the target length and interpolate them
966             return (self * (result_length / self_length) * t1
967                 + rhs * (result_length / rhs_length) * t2)
968                 * sin_theta.recip();
969         }
970         if dot < 0.0 {
971             // Vectors are almost parallel in opposing directions
972 
973             // Create a rotation from self to rhs along some axis
974             let axis = self.any_orthogonal_vector().normalize().into();
975             let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
976             // Interpolate vector lengths
977             let result_length = self_length.lerp(rhs_length, s);
978             rotation * self * (result_length / self_length)
979         } else {
980             // Vectors are almost parallel in the same direction, or dot was NaN
981             self.lerp(rhs, s)
982         }
983     }
984 
985     /// Casts all elements of `self` to `f64`.
986     #[inline]
987     #[must_use]
as_dvec3(&self) -> crate::DVec3988     pub fn as_dvec3(&self) -> crate::DVec3 {
989         crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
990     }
991 
992     /// Casts all elements of `self` to `i8`.
993     #[inline]
994     #[must_use]
as_i8vec3(&self) -> crate::I8Vec3995     pub fn as_i8vec3(&self) -> crate::I8Vec3 {
996         crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
997     }
998 
999     /// Casts all elements of `self` to `u8`.
1000     #[inline]
1001     #[must_use]
as_u8vec3(&self) -> crate::U8Vec31002     pub fn as_u8vec3(&self) -> crate::U8Vec3 {
1003         crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1004     }
1005 
1006     /// Casts all elements of `self` to `i16`.
1007     #[inline]
1008     #[must_use]
as_i16vec3(&self) -> crate::I16Vec31009     pub fn as_i16vec3(&self) -> crate::I16Vec3 {
1010         crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1011     }
1012 
1013     /// Casts all elements of `self` to `u16`.
1014     #[inline]
1015     #[must_use]
as_u16vec3(&self) -> crate::U16Vec31016     pub fn as_u16vec3(&self) -> crate::U16Vec3 {
1017         crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1018     }
1019 
1020     /// Casts all elements of `self` to `i32`.
1021     #[inline]
1022     #[must_use]
as_ivec3(&self) -> crate::IVec31023     pub fn as_ivec3(&self) -> crate::IVec3 {
1024         crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1025     }
1026 
1027     /// Casts all elements of `self` to `u32`.
1028     #[inline]
1029     #[must_use]
as_uvec3(&self) -> crate::UVec31030     pub fn as_uvec3(&self) -> crate::UVec3 {
1031         crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1032     }
1033 
1034     /// Casts all elements of `self` to `i64`.
1035     #[inline]
1036     #[must_use]
as_i64vec3(&self) -> crate::I64Vec31037     pub fn as_i64vec3(&self) -> crate::I64Vec3 {
1038         crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1039     }
1040 
1041     /// Casts all elements of `self` to `u64`.
1042     #[inline]
1043     #[must_use]
as_u64vec3(&self) -> crate::U64Vec31044     pub fn as_u64vec3(&self) -> crate::U64Vec3 {
1045         crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1046     }
1047 }
1048 
1049 impl Default for Vec3A {
1050     #[inline(always)]
default() -> Self1051     fn default() -> Self {
1052         Self::ZERO
1053     }
1054 }
1055 
1056 impl PartialEq for Vec3A {
1057     #[inline]
eq(&self, rhs: &Self) -> bool1058     fn eq(&self, rhs: &Self) -> bool {
1059         self.cmpeq(*rhs).all()
1060     }
1061 }
1062 
1063 impl Div<Vec3A> for Vec3A {
1064     type Output = Self;
1065     #[inline]
div(self, rhs: Self) -> Self1066     fn div(self, rhs: Self) -> Self {
1067         Self(self.0 / rhs.0)
1068     }
1069 }
1070 
1071 impl Div<&Vec3A> for Vec3A {
1072     type Output = Vec3A;
1073     #[inline]
div(self, rhs: &Vec3A) -> Vec3A1074     fn div(self, rhs: &Vec3A) -> Vec3A {
1075         self.div(*rhs)
1076     }
1077 }
1078 
1079 impl Div<&Vec3A> for &Vec3A {
1080     type Output = Vec3A;
1081     #[inline]
div(self, rhs: &Vec3A) -> Vec3A1082     fn div(self, rhs: &Vec3A) -> Vec3A {
1083         (*self).div(*rhs)
1084     }
1085 }
1086 
1087 impl Div<Vec3A> for &Vec3A {
1088     type Output = Vec3A;
1089     #[inline]
div(self, rhs: Vec3A) -> Vec3A1090     fn div(self, rhs: Vec3A) -> Vec3A {
1091         (*self).div(rhs)
1092     }
1093 }
1094 
1095 impl DivAssign<Vec3A> for Vec3A {
1096     #[inline]
div_assign(&mut self, rhs: Self)1097     fn div_assign(&mut self, rhs: Self) {
1098         self.0 /= rhs.0;
1099     }
1100 }
1101 
1102 impl DivAssign<&Vec3A> for Vec3A {
1103     #[inline]
div_assign(&mut self, rhs: &Vec3A)1104     fn div_assign(&mut self, rhs: &Vec3A) {
1105         self.div_assign(*rhs)
1106     }
1107 }
1108 
1109 impl Div<f32> for Vec3A {
1110     type Output = Self;
1111     #[inline]
div(self, rhs: f32) -> Self1112     fn div(self, rhs: f32) -> Self {
1113         Self(self.0 / f32x4::splat(rhs))
1114     }
1115 }
1116 
1117 impl Div<&f32> for Vec3A {
1118     type Output = Vec3A;
1119     #[inline]
div(self, rhs: &f32) -> Vec3A1120     fn div(self, rhs: &f32) -> Vec3A {
1121         self.div(*rhs)
1122     }
1123 }
1124 
1125 impl Div<&f32> for &Vec3A {
1126     type Output = Vec3A;
1127     #[inline]
div(self, rhs: &f32) -> Vec3A1128     fn div(self, rhs: &f32) -> Vec3A {
1129         (*self).div(*rhs)
1130     }
1131 }
1132 
1133 impl Div<f32> for &Vec3A {
1134     type Output = Vec3A;
1135     #[inline]
div(self, rhs: f32) -> Vec3A1136     fn div(self, rhs: f32) -> Vec3A {
1137         (*self).div(rhs)
1138     }
1139 }
1140 
1141 impl DivAssign<f32> for Vec3A {
1142     #[inline]
div_assign(&mut self, rhs: f32)1143     fn div_assign(&mut self, rhs: f32) {
1144         self.0 /= f32x4::splat(rhs);
1145     }
1146 }
1147 
1148 impl DivAssign<&f32> for Vec3A {
1149     #[inline]
div_assign(&mut self, rhs: &f32)1150     fn div_assign(&mut self, rhs: &f32) {
1151         self.div_assign(*rhs)
1152     }
1153 }
1154 
1155 impl Div<Vec3A> for f32 {
1156     type Output = Vec3A;
1157     #[inline]
div(self, rhs: Vec3A) -> Vec3A1158     fn div(self, rhs: Vec3A) -> Vec3A {
1159         Vec3A(f32x4::splat(self) / rhs.0)
1160     }
1161 }
1162 
1163 impl Div<&Vec3A> for f32 {
1164     type Output = Vec3A;
1165     #[inline]
div(self, rhs: &Vec3A) -> Vec3A1166     fn div(self, rhs: &Vec3A) -> Vec3A {
1167         self.div(*rhs)
1168     }
1169 }
1170 
1171 impl Div<&Vec3A> for &f32 {
1172     type Output = Vec3A;
1173     #[inline]
div(self, rhs: &Vec3A) -> Vec3A1174     fn div(self, rhs: &Vec3A) -> Vec3A {
1175         (*self).div(*rhs)
1176     }
1177 }
1178 
1179 impl Div<Vec3A> for &f32 {
1180     type Output = Vec3A;
1181     #[inline]
div(self, rhs: Vec3A) -> Vec3A1182     fn div(self, rhs: Vec3A) -> Vec3A {
1183         (*self).div(rhs)
1184     }
1185 }
1186 
1187 impl Mul<Vec3A> for Vec3A {
1188     type Output = Self;
1189     #[inline]
mul(self, rhs: Self) -> Self1190     fn mul(self, rhs: Self) -> Self {
1191         Self(self.0 * rhs.0)
1192     }
1193 }
1194 
1195 impl Mul<&Vec3A> for Vec3A {
1196     type Output = Vec3A;
1197     #[inline]
mul(self, rhs: &Vec3A) -> Vec3A1198     fn mul(self, rhs: &Vec3A) -> Vec3A {
1199         self.mul(*rhs)
1200     }
1201 }
1202 
1203 impl Mul<&Vec3A> for &Vec3A {
1204     type Output = Vec3A;
1205     #[inline]
mul(self, rhs: &Vec3A) -> Vec3A1206     fn mul(self, rhs: &Vec3A) -> Vec3A {
1207         (*self).mul(*rhs)
1208     }
1209 }
1210 
1211 impl Mul<Vec3A> for &Vec3A {
1212     type Output = Vec3A;
1213     #[inline]
mul(self, rhs: Vec3A) -> Vec3A1214     fn mul(self, rhs: Vec3A) -> Vec3A {
1215         (*self).mul(rhs)
1216     }
1217 }
1218 
1219 impl MulAssign<Vec3A> for Vec3A {
1220     #[inline]
mul_assign(&mut self, rhs: Self)1221     fn mul_assign(&mut self, rhs: Self) {
1222         self.0 *= rhs.0;
1223     }
1224 }
1225 
1226 impl MulAssign<&Vec3A> for Vec3A {
1227     #[inline]
mul_assign(&mut self, rhs: &Vec3A)1228     fn mul_assign(&mut self, rhs: &Vec3A) {
1229         self.mul_assign(*rhs)
1230     }
1231 }
1232 
1233 impl Mul<f32> for Vec3A {
1234     type Output = Self;
1235     #[inline]
mul(self, rhs: f32) -> Self1236     fn mul(self, rhs: f32) -> Self {
1237         Self(self.0 * f32x4::splat(rhs))
1238     }
1239 }
1240 
1241 impl Mul<&f32> for Vec3A {
1242     type Output = Vec3A;
1243     #[inline]
mul(self, rhs: &f32) -> Vec3A1244     fn mul(self, rhs: &f32) -> Vec3A {
1245         self.mul(*rhs)
1246     }
1247 }
1248 
1249 impl Mul<&f32> for &Vec3A {
1250     type Output = Vec3A;
1251     #[inline]
mul(self, rhs: &f32) -> Vec3A1252     fn mul(self, rhs: &f32) -> Vec3A {
1253         (*self).mul(*rhs)
1254     }
1255 }
1256 
1257 impl Mul<f32> for &Vec3A {
1258     type Output = Vec3A;
1259     #[inline]
mul(self, rhs: f32) -> Vec3A1260     fn mul(self, rhs: f32) -> Vec3A {
1261         (*self).mul(rhs)
1262     }
1263 }
1264 
1265 impl MulAssign<f32> for Vec3A {
1266     #[inline]
mul_assign(&mut self, rhs: f32)1267     fn mul_assign(&mut self, rhs: f32) {
1268         self.0 *= f32x4::splat(rhs);
1269     }
1270 }
1271 
1272 impl MulAssign<&f32> for Vec3A {
1273     #[inline]
mul_assign(&mut self, rhs: &f32)1274     fn mul_assign(&mut self, rhs: &f32) {
1275         self.mul_assign(*rhs)
1276     }
1277 }
1278 
1279 impl Mul<Vec3A> for f32 {
1280     type Output = Vec3A;
1281     #[inline]
mul(self, rhs: Vec3A) -> Vec3A1282     fn mul(self, rhs: Vec3A) -> Vec3A {
1283         Vec3A(f32x4::splat(self) * rhs.0)
1284     }
1285 }
1286 
1287 impl Mul<&Vec3A> for f32 {
1288     type Output = Vec3A;
1289     #[inline]
mul(self, rhs: &Vec3A) -> Vec3A1290     fn mul(self, rhs: &Vec3A) -> Vec3A {
1291         self.mul(*rhs)
1292     }
1293 }
1294 
1295 impl Mul<&Vec3A> for &f32 {
1296     type Output = Vec3A;
1297     #[inline]
mul(self, rhs: &Vec3A) -> Vec3A1298     fn mul(self, rhs: &Vec3A) -> Vec3A {
1299         (*self).mul(*rhs)
1300     }
1301 }
1302 
1303 impl Mul<Vec3A> for &f32 {
1304     type Output = Vec3A;
1305     #[inline]
mul(self, rhs: Vec3A) -> Vec3A1306     fn mul(self, rhs: Vec3A) -> Vec3A {
1307         (*self).mul(rhs)
1308     }
1309 }
1310 
1311 impl Add<Vec3A> for Vec3A {
1312     type Output = Self;
1313     #[inline]
add(self, rhs: Self) -> Self1314     fn add(self, rhs: Self) -> Self {
1315         Self(self.0 + rhs.0)
1316     }
1317 }
1318 
1319 impl Add<&Vec3A> for Vec3A {
1320     type Output = Vec3A;
1321     #[inline]
add(self, rhs: &Vec3A) -> Vec3A1322     fn add(self, rhs: &Vec3A) -> Vec3A {
1323         self.add(*rhs)
1324     }
1325 }
1326 
1327 impl Add<&Vec3A> for &Vec3A {
1328     type Output = Vec3A;
1329     #[inline]
add(self, rhs: &Vec3A) -> Vec3A1330     fn add(self, rhs: &Vec3A) -> Vec3A {
1331         (*self).add(*rhs)
1332     }
1333 }
1334 
1335 impl Add<Vec3A> for &Vec3A {
1336     type Output = Vec3A;
1337     #[inline]
add(self, rhs: Vec3A) -> Vec3A1338     fn add(self, rhs: Vec3A) -> Vec3A {
1339         (*self).add(rhs)
1340     }
1341 }
1342 
1343 impl AddAssign<Vec3A> for Vec3A {
1344     #[inline]
add_assign(&mut self, rhs: Self)1345     fn add_assign(&mut self, rhs: Self) {
1346         self.0 += rhs.0;
1347     }
1348 }
1349 
1350 impl AddAssign<&Vec3A> for Vec3A {
1351     #[inline]
add_assign(&mut self, rhs: &Vec3A)1352     fn add_assign(&mut self, rhs: &Vec3A) {
1353         self.add_assign(*rhs)
1354     }
1355 }
1356 
1357 impl Add<f32> for Vec3A {
1358     type Output = Self;
1359     #[inline]
add(self, rhs: f32) -> Self1360     fn add(self, rhs: f32) -> Self {
1361         Self(self.0 + f32x4::splat(rhs))
1362     }
1363 }
1364 
1365 impl Add<&f32> for Vec3A {
1366     type Output = Vec3A;
1367     #[inline]
add(self, rhs: &f32) -> Vec3A1368     fn add(self, rhs: &f32) -> Vec3A {
1369         self.add(*rhs)
1370     }
1371 }
1372 
1373 impl Add<&f32> for &Vec3A {
1374     type Output = Vec3A;
1375     #[inline]
add(self, rhs: &f32) -> Vec3A1376     fn add(self, rhs: &f32) -> Vec3A {
1377         (*self).add(*rhs)
1378     }
1379 }
1380 
1381 impl Add<f32> for &Vec3A {
1382     type Output = Vec3A;
1383     #[inline]
add(self, rhs: f32) -> Vec3A1384     fn add(self, rhs: f32) -> Vec3A {
1385         (*self).add(rhs)
1386     }
1387 }
1388 
1389 impl AddAssign<f32> for Vec3A {
1390     #[inline]
add_assign(&mut self, rhs: f32)1391     fn add_assign(&mut self, rhs: f32) {
1392         self.0 += f32x4::splat(rhs);
1393     }
1394 }
1395 
1396 impl AddAssign<&f32> for Vec3A {
1397     #[inline]
add_assign(&mut self, rhs: &f32)1398     fn add_assign(&mut self, rhs: &f32) {
1399         self.add_assign(*rhs)
1400     }
1401 }
1402 
1403 impl Add<Vec3A> for f32 {
1404     type Output = Vec3A;
1405     #[inline]
add(self, rhs: Vec3A) -> Vec3A1406     fn add(self, rhs: Vec3A) -> Vec3A {
1407         Vec3A(f32x4::splat(self) + rhs.0)
1408     }
1409 }
1410 
1411 impl Add<&Vec3A> for f32 {
1412     type Output = Vec3A;
1413     #[inline]
add(self, rhs: &Vec3A) -> Vec3A1414     fn add(self, rhs: &Vec3A) -> Vec3A {
1415         self.add(*rhs)
1416     }
1417 }
1418 
1419 impl Add<&Vec3A> for &f32 {
1420     type Output = Vec3A;
1421     #[inline]
add(self, rhs: &Vec3A) -> Vec3A1422     fn add(self, rhs: &Vec3A) -> Vec3A {
1423         (*self).add(*rhs)
1424     }
1425 }
1426 
1427 impl Add<Vec3A> for &f32 {
1428     type Output = Vec3A;
1429     #[inline]
add(self, rhs: Vec3A) -> Vec3A1430     fn add(self, rhs: Vec3A) -> Vec3A {
1431         (*self).add(rhs)
1432     }
1433 }
1434 
1435 impl Sub<Vec3A> for Vec3A {
1436     type Output = Self;
1437     #[inline]
sub(self, rhs: Self) -> Self1438     fn sub(self, rhs: Self) -> Self {
1439         Self(self.0 - rhs.0)
1440     }
1441 }
1442 
1443 impl Sub<&Vec3A> for Vec3A {
1444     type Output = Vec3A;
1445     #[inline]
sub(self, rhs: &Vec3A) -> Vec3A1446     fn sub(self, rhs: &Vec3A) -> Vec3A {
1447         self.sub(*rhs)
1448     }
1449 }
1450 
1451 impl Sub<&Vec3A> for &Vec3A {
1452     type Output = Vec3A;
1453     #[inline]
sub(self, rhs: &Vec3A) -> Vec3A1454     fn sub(self, rhs: &Vec3A) -> Vec3A {
1455         (*self).sub(*rhs)
1456     }
1457 }
1458 
1459 impl Sub<Vec3A> for &Vec3A {
1460     type Output = Vec3A;
1461     #[inline]
sub(self, rhs: Vec3A) -> Vec3A1462     fn sub(self, rhs: Vec3A) -> Vec3A {
1463         (*self).sub(rhs)
1464     }
1465 }
1466 
1467 impl SubAssign<Vec3A> for Vec3A {
1468     #[inline]
sub_assign(&mut self, rhs: Vec3A)1469     fn sub_assign(&mut self, rhs: Vec3A) {
1470         self.0 -= rhs.0;
1471     }
1472 }
1473 
1474 impl SubAssign<&Vec3A> for Vec3A {
1475     #[inline]
sub_assign(&mut self, rhs: &Vec3A)1476     fn sub_assign(&mut self, rhs: &Vec3A) {
1477         self.sub_assign(*rhs)
1478     }
1479 }
1480 
1481 impl Sub<f32> for Vec3A {
1482     type Output = Self;
1483     #[inline]
sub(self, rhs: f32) -> Self1484     fn sub(self, rhs: f32) -> Self {
1485         Self(self.0 - f32x4::splat(rhs))
1486     }
1487 }
1488 
1489 impl Sub<&f32> for Vec3A {
1490     type Output = Vec3A;
1491     #[inline]
sub(self, rhs: &f32) -> Vec3A1492     fn sub(self, rhs: &f32) -> Vec3A {
1493         self.sub(*rhs)
1494     }
1495 }
1496 
1497 impl Sub<&f32> for &Vec3A {
1498     type Output = Vec3A;
1499     #[inline]
sub(self, rhs: &f32) -> Vec3A1500     fn sub(self, rhs: &f32) -> Vec3A {
1501         (*self).sub(*rhs)
1502     }
1503 }
1504 
1505 impl Sub<f32> for &Vec3A {
1506     type Output = Vec3A;
1507     #[inline]
sub(self, rhs: f32) -> Vec3A1508     fn sub(self, rhs: f32) -> Vec3A {
1509         (*self).sub(rhs)
1510     }
1511 }
1512 
1513 impl SubAssign<f32> for Vec3A {
1514     #[inline]
sub_assign(&mut self, rhs: f32)1515     fn sub_assign(&mut self, rhs: f32) {
1516         self.0 -= f32x4::splat(rhs);
1517     }
1518 }
1519 
1520 impl SubAssign<&f32> for Vec3A {
1521     #[inline]
sub_assign(&mut self, rhs: &f32)1522     fn sub_assign(&mut self, rhs: &f32) {
1523         self.sub_assign(*rhs)
1524     }
1525 }
1526 
1527 impl Sub<Vec3A> for f32 {
1528     type Output = Vec3A;
1529     #[inline]
sub(self, rhs: Vec3A) -> Vec3A1530     fn sub(self, rhs: Vec3A) -> Vec3A {
1531         Vec3A(f32x4::splat(self) - rhs.0)
1532     }
1533 }
1534 
1535 impl Sub<&Vec3A> for f32 {
1536     type Output = Vec3A;
1537     #[inline]
sub(self, rhs: &Vec3A) -> Vec3A1538     fn sub(self, rhs: &Vec3A) -> Vec3A {
1539         self.sub(*rhs)
1540     }
1541 }
1542 
1543 impl Sub<&Vec3A> for &f32 {
1544     type Output = Vec3A;
1545     #[inline]
sub(self, rhs: &Vec3A) -> Vec3A1546     fn sub(self, rhs: &Vec3A) -> Vec3A {
1547         (*self).sub(*rhs)
1548     }
1549 }
1550 
1551 impl Sub<Vec3A> for &f32 {
1552     type Output = Vec3A;
1553     #[inline]
sub(self, rhs: Vec3A) -> Vec3A1554     fn sub(self, rhs: Vec3A) -> Vec3A {
1555         (*self).sub(rhs)
1556     }
1557 }
1558 
1559 impl Rem<Vec3A> for Vec3A {
1560     type Output = Self;
1561     #[inline]
rem(self, rhs: Self) -> Self1562     fn rem(self, rhs: Self) -> Self {
1563         Self(self.0 % rhs.0)
1564     }
1565 }
1566 
1567 impl Rem<&Vec3A> for Vec3A {
1568     type Output = Vec3A;
1569     #[inline]
rem(self, rhs: &Vec3A) -> Vec3A1570     fn rem(self, rhs: &Vec3A) -> Vec3A {
1571         self.rem(*rhs)
1572     }
1573 }
1574 
1575 impl Rem<&Vec3A> for &Vec3A {
1576     type Output = Vec3A;
1577     #[inline]
rem(self, rhs: &Vec3A) -> Vec3A1578     fn rem(self, rhs: &Vec3A) -> Vec3A {
1579         (*self).rem(*rhs)
1580     }
1581 }
1582 
1583 impl Rem<Vec3A> for &Vec3A {
1584     type Output = Vec3A;
1585     #[inline]
rem(self, rhs: Vec3A) -> Vec3A1586     fn rem(self, rhs: Vec3A) -> Vec3A {
1587         (*self).rem(rhs)
1588     }
1589 }
1590 
1591 impl RemAssign<Vec3A> for Vec3A {
1592     #[inline]
rem_assign(&mut self, rhs: Self)1593     fn rem_assign(&mut self, rhs: Self) {
1594         self.0 %= rhs.0;
1595     }
1596 }
1597 
1598 impl RemAssign<&Vec3A> for Vec3A {
1599     #[inline]
rem_assign(&mut self, rhs: &Vec3A)1600     fn rem_assign(&mut self, rhs: &Vec3A) {
1601         self.rem_assign(*rhs)
1602     }
1603 }
1604 
1605 impl Rem<f32> for Vec3A {
1606     type Output = Self;
1607     #[inline]
rem(self, rhs: f32) -> Self1608     fn rem(self, rhs: f32) -> Self {
1609         self.rem(Self::splat(rhs))
1610     }
1611 }
1612 
1613 impl Rem<&f32> for Vec3A {
1614     type Output = Vec3A;
1615     #[inline]
rem(self, rhs: &f32) -> Vec3A1616     fn rem(self, rhs: &f32) -> Vec3A {
1617         self.rem(*rhs)
1618     }
1619 }
1620 
1621 impl Rem<&f32> for &Vec3A {
1622     type Output = Vec3A;
1623     #[inline]
rem(self, rhs: &f32) -> Vec3A1624     fn rem(self, rhs: &f32) -> Vec3A {
1625         (*self).rem(*rhs)
1626     }
1627 }
1628 
1629 impl Rem<f32> for &Vec3A {
1630     type Output = Vec3A;
1631     #[inline]
rem(self, rhs: f32) -> Vec3A1632     fn rem(self, rhs: f32) -> Vec3A {
1633         (*self).rem(rhs)
1634     }
1635 }
1636 
1637 impl RemAssign<f32> for Vec3A {
1638     #[inline]
rem_assign(&mut self, rhs: f32)1639     fn rem_assign(&mut self, rhs: f32) {
1640         self.0 %= f32x4::splat(rhs);
1641     }
1642 }
1643 
1644 impl RemAssign<&f32> for Vec3A {
1645     #[inline]
rem_assign(&mut self, rhs: &f32)1646     fn rem_assign(&mut self, rhs: &f32) {
1647         self.rem_assign(*rhs)
1648     }
1649 }
1650 
1651 impl Rem<Vec3A> for f32 {
1652     type Output = Vec3A;
1653     #[inline]
rem(self, rhs: Vec3A) -> Vec3A1654     fn rem(self, rhs: Vec3A) -> Vec3A {
1655         Vec3A::splat(self).rem(rhs)
1656     }
1657 }
1658 
1659 impl Rem<&Vec3A> for f32 {
1660     type Output = Vec3A;
1661     #[inline]
rem(self, rhs: &Vec3A) -> Vec3A1662     fn rem(self, rhs: &Vec3A) -> Vec3A {
1663         self.rem(*rhs)
1664     }
1665 }
1666 
1667 impl Rem<&Vec3A> for &f32 {
1668     type Output = Vec3A;
1669     #[inline]
rem(self, rhs: &Vec3A) -> Vec3A1670     fn rem(self, rhs: &Vec3A) -> Vec3A {
1671         (*self).rem(*rhs)
1672     }
1673 }
1674 
1675 impl Rem<Vec3A> for &f32 {
1676     type Output = Vec3A;
1677     #[inline]
rem(self, rhs: Vec3A) -> Vec3A1678     fn rem(self, rhs: Vec3A) -> Vec3A {
1679         (*self).rem(rhs)
1680     }
1681 }
1682 
1683 #[cfg(not(target_arch = "spirv"))]
1684 impl AsRef<[f32; 3]> for Vec3A {
1685     #[inline]
as_ref(&self) -> &[f32; 3]1686     fn as_ref(&self) -> &[f32; 3] {
1687         unsafe { &*(self as *const Vec3A as *const [f32; 3]) }
1688     }
1689 }
1690 
1691 #[cfg(not(target_arch = "spirv"))]
1692 impl AsMut<[f32; 3]> for Vec3A {
1693     #[inline]
as_mut(&mut self) -> &mut [f32; 3]1694     fn as_mut(&mut self) -> &mut [f32; 3] {
1695         unsafe { &mut *(self as *mut Vec3A as *mut [f32; 3]) }
1696     }
1697 }
1698 
1699 impl Sum for Vec3A {
1700     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1701     fn sum<I>(iter: I) -> Self
1702     where
1703         I: Iterator<Item = Self>,
1704     {
1705         iter.fold(Self::ZERO, Self::add)
1706     }
1707 }
1708 
1709 impl<'a> Sum<&'a Self> for Vec3A {
1710     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1711     fn sum<I>(iter: I) -> Self
1712     where
1713         I: Iterator<Item = &'a Self>,
1714     {
1715         iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1716     }
1717 }
1718 
1719 impl Product for Vec3A {
1720     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1721     fn product<I>(iter: I) -> Self
1722     where
1723         I: Iterator<Item = Self>,
1724     {
1725         iter.fold(Self::ONE, Self::mul)
1726     }
1727 }
1728 
1729 impl<'a> Product<&'a Self> for Vec3A {
1730     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1731     fn product<I>(iter: I) -> Self
1732     where
1733         I: Iterator<Item = &'a Self>,
1734     {
1735         iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1736     }
1737 }
1738 
1739 impl Neg for Vec3A {
1740     type Output = Self;
1741     #[inline]
neg(self) -> Self1742     fn neg(self) -> Self {
1743         Self(-self.0)
1744     }
1745 }
1746 
1747 impl Neg for &Vec3A {
1748     type Output = Vec3A;
1749     #[inline]
neg(self) -> Vec3A1750     fn neg(self) -> Vec3A {
1751         (*self).neg()
1752     }
1753 }
1754 
1755 impl Index<usize> for Vec3A {
1756     type Output = f32;
1757     #[inline]
index(&self, index: usize) -> &Self::Output1758     fn index(&self, index: usize) -> &Self::Output {
1759         &self.0[index]
1760     }
1761 }
1762 
1763 impl IndexMut<usize> for Vec3A {
1764     #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1765     fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1766         &mut self.0[index]
1767     }
1768 }
1769 
1770 impl fmt::Display for Vec3A {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1771     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1772         if let Some(p) = f.precision() {
1773             write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
1774         } else {
1775             write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
1776         }
1777     }
1778 }
1779 
1780 impl fmt::Debug for Vec3A {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1781     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1782         fmt.debug_tuple(stringify!(Vec3A))
1783             .field(&self.x)
1784             .field(&self.y)
1785             .field(&self.z)
1786             .finish()
1787     }
1788 }
1789 
1790 impl From<Vec3A> for f32x4 {
1791     #[inline(always)]
from(t: Vec3A) -> Self1792     fn from(t: Vec3A) -> Self {
1793         t.0
1794     }
1795 }
1796 
1797 impl From<f32x4> for Vec3A {
1798     #[inline(always)]
from(t: f32x4) -> Self1799     fn from(t: f32x4) -> Self {
1800         Self(t)
1801     }
1802 }
1803 
1804 impl From<[f32; 3]> for Vec3A {
1805     #[inline]
from(a: [f32; 3]) -> Self1806     fn from(a: [f32; 3]) -> Self {
1807         Self::new(a[0], a[1], a[2])
1808     }
1809 }
1810 
1811 impl From<Vec3A> for [f32; 3] {
1812     #[inline]
from(v: Vec3A) -> Self1813     fn from(v: Vec3A) -> Self {
1814         unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1815     }
1816 }
1817 
1818 impl From<(f32, f32, f32)> for Vec3A {
1819     #[inline]
from(t: (f32, f32, f32)) -> Self1820     fn from(t: (f32, f32, f32)) -> Self {
1821         Self::new(t.0, t.1, t.2)
1822     }
1823 }
1824 
1825 impl From<Vec3A> for (f32, f32, f32) {
1826     #[inline]
from(v: Vec3A) -> Self1827     fn from(v: Vec3A) -> Self {
1828         unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1829     }
1830 }
1831 
1832 impl From<Vec3> for Vec3A {
1833     #[inline]
from(v: Vec3) -> Self1834     fn from(v: Vec3) -> Self {
1835         Self::new(v.x, v.y, v.z)
1836     }
1837 }
1838 
1839 impl From<Vec3A> for Vec3 {
1840     #[inline]
from(v: Vec3A) -> Self1841     fn from(v: Vec3A) -> Self {
1842         unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1843     }
1844 }
1845 
1846 impl From<(Vec2, f32)> for Vec3A {
1847     #[inline]
from((v, z): (Vec2, f32)) -> Self1848     fn from((v, z): (Vec2, f32)) -> Self {
1849         Self::new(v.x, v.y, z)
1850     }
1851 }
1852 
1853 impl Deref for Vec3A {
1854     type Target = crate::deref::Vec3<f32>;
1855     #[inline]
deref(&self) -> &Self::Target1856     fn deref(&self) -> &Self::Target {
1857         unsafe { &*(self as *const Self).cast() }
1858     }
1859 }
1860 
1861 impl DerefMut for Vec3A {
1862     #[inline]
deref_mut(&mut self) -> &mut Self::Target1863     fn deref_mut(&mut self) -> &mut Self::Target {
1864         unsafe { &mut *(self as *mut Self).cast() }
1865     }
1866 }
1867 
1868 impl From<BVec3> for Vec3A {
1869     #[inline]
from(v: BVec3) -> Self1870     fn from(v: BVec3) -> Self {
1871         Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
1872     }
1873 }
1874 
1875 impl From<BVec3A> for Vec3A {
1876     #[inline]
from(v: BVec3A) -> Self1877     fn from(v: BVec3A) -> Self {
1878         let bool_array: [bool; 3] = v.into();
1879         Self::new(
1880             f32::from(bool_array[0]),
1881             f32::from(bool_array[1]),
1882             f32::from(bool_array[2]),
1883         )
1884     }
1885 }
1886