• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2 
3 use crate::{BVec4, Vec2, Vec3, Vec3A};
4 
5 #[cfg(not(target_arch = "spirv"))]
6 use core::fmt;
7 use core::iter::{Product, Sum};
8 use core::{f32, ops::*};
9 
10 #[cfg(feature = "libm")]
11 #[allow(unused_imports)]
12 use num_traits::Float;
13 
14 /// Creates a 4-dimensional vector.
15 #[inline(always)]
vec4(x: f32, y: f32, z: f32, w: f32) -> Vec416 pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
17     Vec4::new(x, y, z, w)
18 }
19 
20 /// A 4-dimensional vector.
21 #[derive(Clone, Copy, PartialEq)]
22 #[cfg_attr(
23     any(
24         not(any(feature = "scalar-math", target_arch = "spirv")),
25         feature = "cuda"
26     ),
27     repr(align(16))
28 )]
29 #[cfg_attr(not(target_arch = "spirv"), repr(C))]
30 #[cfg_attr(target_arch = "spirv", repr(simd))]
31 pub struct Vec4 {
32     pub x: f32,
33     pub y: f32,
34     pub z: f32,
35     pub w: f32,
36 }
37 
38 impl Vec4 {
39     /// All zeroes.
40     pub const ZERO: Self = Self::splat(0.0);
41 
42     /// All ones.
43     pub const ONE: Self = Self::splat(1.0);
44 
45     /// All negative ones.
46     pub const NEG_ONE: Self = Self::splat(-1.0);
47 
48     /// All NAN.
49     pub const NAN: Self = Self::splat(f32::NAN);
50 
51     /// A unit-length vector pointing along the positive X axis.
52     pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
53 
54     /// A unit-length vector pointing along the positive Y axis.
55     pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
56 
57     /// A unit-length vector pointing along the positive Z axis.
58     pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
59 
60     /// A unit-length vector pointing along the positive W axis.
61     pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
62 
63     /// A unit-length vector pointing along the negative X axis.
64     pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
65 
66     /// A unit-length vector pointing along the negative Y axis.
67     pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
68 
69     /// A unit-length vector pointing along the negative Z axis.
70     pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
71 
72     /// A unit-length vector pointing along the negative W axis.
73     pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
74 
75     /// The unit axes.
76     pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
77 
78     /// Creates a new vector.
79     #[inline(always)]
new(x: f32, y: f32, z: f32, w: f32) -> Self80     pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
81         Self { x, y, z, w }
82     }
83 
84     /// Creates a vector with all elements set to `v`.
85     #[inline]
splat(v: f32) -> Self86     pub const fn splat(v: f32) -> Self {
87         Self {
88             x: v,
89 
90             y: v,
91 
92             z: v,
93 
94             w: v,
95         }
96     }
97 
98     /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
99     /// for each element of `self`.
100     ///
101     /// A true element in the mask uses the corresponding element from `if_true`, and false
102     /// uses the element from `if_false`.
103     #[inline]
select(mask: BVec4, if_true: Self, if_false: Self) -> Self104     pub fn select(mask: BVec4, if_true: Self, if_false: Self) -> Self {
105         Self {
106             x: if mask.x { if_true.x } else { if_false.x },
107             y: if mask.y { if_true.y } else { if_false.y },
108             z: if mask.z { if_true.z } else { if_false.z },
109             w: if mask.w { if_true.w } else { if_false.w },
110         }
111     }
112 
113     /// Creates a new vector from an array.
114     #[inline]
from_array(a: [f32; 4]) -> Self115     pub const fn from_array(a: [f32; 4]) -> Self {
116         Self::new(a[0], a[1], a[2], a[3])
117     }
118 
119     /// `[x, y, z, w]`
120     #[inline]
to_array(&self) -> [f32; 4]121     pub const fn to_array(&self) -> [f32; 4] {
122         [self.x, self.y, self.z, self.w]
123     }
124 
125     /// Creates a vector from the first 4 values in `slice`.
126     ///
127     /// # Panics
128     ///
129     /// Panics if `slice` is less than 4 elements long.
130     #[inline]
from_slice(slice: &[f32]) -> Self131     pub const fn from_slice(slice: &[f32]) -> Self {
132         Self::new(slice[0], slice[1], slice[2], slice[3])
133     }
134 
135     /// Writes the elements of `self` to the first 4 elements in `slice`.
136     ///
137     /// # Panics
138     ///
139     /// Panics if `slice` is less than 4 elements long.
140     #[inline]
write_to_slice(self, slice: &mut [f32])141     pub fn write_to_slice(self, slice: &mut [f32]) {
142         slice[0] = self.x;
143         slice[1] = self.y;
144         slice[2] = self.z;
145         slice[3] = self.w;
146     }
147 
148     /// Creates a 2D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
149     ///
150     /// Truncation to `Vec3` may also be performed by using `self.xyz()` or `Vec3::from()`.
151     ///
152     /// To truncate to `Vec3A` use `Vec3A::from()`.
153     #[inline]
truncate(self) -> Vec3154     pub fn truncate(self) -> Vec3 {
155         use crate::swizzles::Vec4Swizzles;
156         self.xyz()
157     }
158 
159     /// Computes the dot product of `self` and `rhs`.
160     #[inline]
dot(self, rhs: Self) -> f32161     pub fn dot(self, rhs: Self) -> f32 {
162         (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
163     }
164 
165     /// Returns a vector where every component is the dot product of `self` and `rhs`.
166     #[inline]
dot_into_vec(self, rhs: Self) -> Self167     pub fn dot_into_vec(self, rhs: Self) -> Self {
168         Self::splat(self.dot(rhs))
169     }
170 
171     /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
172     ///
173     /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
174     #[inline]
min(self, rhs: Self) -> Self175     pub fn min(self, rhs: Self) -> Self {
176         Self {
177             x: self.x.min(rhs.x),
178             y: self.y.min(rhs.y),
179             z: self.z.min(rhs.z),
180             w: self.w.min(rhs.w),
181         }
182     }
183 
184     /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
185     ///
186     /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
187     #[inline]
max(self, rhs: Self) -> Self188     pub fn max(self, rhs: Self) -> Self {
189         Self {
190             x: self.x.max(rhs.x),
191             y: self.y.max(rhs.y),
192             z: self.z.max(rhs.z),
193             w: self.w.max(rhs.w),
194         }
195     }
196 
197     /// Component-wise clamping of values, similar to [`f32::clamp`].
198     ///
199     /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
200     ///
201     /// # Panics
202     ///
203     /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
204     #[inline]
clamp(self, min: Self, max: Self) -> Self205     pub fn clamp(self, min: Self, max: Self) -> Self {
206         glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
207         self.max(min).min(max)
208     }
209 
210     /// Returns the horizontal minimum of `self`.
211     ///
212     /// In other words this computes `min(x, y, ..)`.
213     #[inline]
min_element(self) -> f32214     pub fn min_element(self) -> f32 {
215         self.x.min(self.y.min(self.z.min(self.w)))
216     }
217 
218     /// Returns the horizontal maximum of `self`.
219     ///
220     /// In other words this computes `max(x, y, ..)`.
221     #[inline]
max_element(self) -> f32222     pub fn max_element(self) -> f32 {
223         self.x.max(self.y.max(self.z.max(self.w)))
224     }
225 
226     /// Returns a vector mask containing the result of a `==` comparison for each element of
227     /// `self` and `rhs`.
228     ///
229     /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
230     /// elements.
231     #[inline]
cmpeq(self, rhs: Self) -> BVec4232     pub fn cmpeq(self, rhs: Self) -> BVec4 {
233         BVec4::new(
234             self.x.eq(&rhs.x),
235             self.y.eq(&rhs.y),
236             self.z.eq(&rhs.z),
237             self.w.eq(&rhs.w),
238         )
239     }
240 
241     /// Returns a vector mask containing the result of a `!=` comparison for each element of
242     /// `self` and `rhs`.
243     ///
244     /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
245     /// elements.
246     #[inline]
cmpne(self, rhs: Self) -> BVec4247     pub fn cmpne(self, rhs: Self) -> BVec4 {
248         BVec4::new(
249             self.x.ne(&rhs.x),
250             self.y.ne(&rhs.y),
251             self.z.ne(&rhs.z),
252             self.w.ne(&rhs.w),
253         )
254     }
255 
256     /// Returns a vector mask containing the result of a `>=` comparison for each element of
257     /// `self` and `rhs`.
258     ///
259     /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
260     /// elements.
261     #[inline]
cmpge(self, rhs: Self) -> BVec4262     pub fn cmpge(self, rhs: Self) -> BVec4 {
263         BVec4::new(
264             self.x.ge(&rhs.x),
265             self.y.ge(&rhs.y),
266             self.z.ge(&rhs.z),
267             self.w.ge(&rhs.w),
268         )
269     }
270 
271     /// Returns a vector mask containing the result of a `>` comparison for each element of
272     /// `self` and `rhs`.
273     ///
274     /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
275     /// elements.
276     #[inline]
cmpgt(self, rhs: Self) -> BVec4277     pub fn cmpgt(self, rhs: Self) -> BVec4 {
278         BVec4::new(
279             self.x.gt(&rhs.x),
280             self.y.gt(&rhs.y),
281             self.z.gt(&rhs.z),
282             self.w.gt(&rhs.w),
283         )
284     }
285 
286     /// Returns a vector mask containing the result of a `<=` comparison for each element of
287     /// `self` and `rhs`.
288     ///
289     /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
290     /// elements.
291     #[inline]
cmple(self, rhs: Self) -> BVec4292     pub fn cmple(self, rhs: Self) -> BVec4 {
293         BVec4::new(
294             self.x.le(&rhs.x),
295             self.y.le(&rhs.y),
296             self.z.le(&rhs.z),
297             self.w.le(&rhs.w),
298         )
299     }
300 
301     /// Returns a vector mask containing the result of a `<` comparison for each element of
302     /// `self` and `rhs`.
303     ///
304     /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
305     /// elements.
306     #[inline]
cmplt(self, rhs: Self) -> BVec4307     pub fn cmplt(self, rhs: Self) -> BVec4 {
308         BVec4::new(
309             self.x.lt(&rhs.x),
310             self.y.lt(&rhs.y),
311             self.z.lt(&rhs.z),
312             self.w.lt(&rhs.w),
313         )
314     }
315 
316     /// Returns a vector containing the absolute value of each element of `self`.
317     #[inline]
abs(self) -> Self318     pub fn abs(self) -> Self {
319         Self {
320             x: self.x.abs(),
321             y: self.y.abs(),
322             z: self.z.abs(),
323             w: self.w.abs(),
324         }
325     }
326 
327     /// Returns a vector with elements representing the sign of `self`.
328     ///
329     /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
330     /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
331     /// - `NAN` if the number is `NAN`
332     #[inline]
signum(self) -> Self333     pub fn signum(self) -> Self {
334         Self {
335             x: self.x.signum(),
336             y: self.y.signum(),
337             z: self.z.signum(),
338             w: self.w.signum(),
339         }
340     }
341 
342     /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
343     #[inline]
copysign(self, rhs: Self) -> Self344     pub fn copysign(self, rhs: Self) -> Self {
345         Self {
346             x: self.x.copysign(rhs.x),
347             y: self.y.copysign(rhs.y),
348             z: self.z.copysign(rhs.z),
349             w: self.w.copysign(rhs.w),
350         }
351     }
352 
353     /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
354     ///
355     /// A negative element results in a `1` bit and a positive element in a `0` bit.  Element `x` goes
356     /// into the first lowest bit, element `y` into the second, etc.
357     #[inline]
is_negative_bitmask(self) -> u32358     pub fn is_negative_bitmask(self) -> u32 {
359         (self.x.is_sign_negative() as u32)
360             | (self.y.is_sign_negative() as u32) << 1
361             | (self.z.is_sign_negative() as u32) << 2
362             | (self.w.is_sign_negative() as u32) << 3
363     }
364 
365     /// Returns `true` if, and only if, all elements are finite.  If any element is either
366     /// `NaN`, positive or negative infinity, this will return `false`.
367     #[inline]
is_finite(self) -> bool368     pub fn is_finite(self) -> bool {
369         self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
370     }
371 
372     /// Returns `true` if any elements are `NaN`.
373     #[inline]
is_nan(self) -> bool374     pub fn is_nan(self) -> bool {
375         self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
376     }
377 
378     /// Performs `is_nan` on each element of self, returning a vector mask of the results.
379     ///
380     /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
381     #[inline]
is_nan_mask(self) -> BVec4382     pub fn is_nan_mask(self) -> BVec4 {
383         BVec4::new(
384             self.x.is_nan(),
385             self.y.is_nan(),
386             self.z.is_nan(),
387             self.w.is_nan(),
388         )
389     }
390 
391     /// Computes the length of `self`.
392     #[doc(alias = "magnitude")]
393     #[inline]
length(self) -> f32394     pub fn length(self) -> f32 {
395         self.dot(self).sqrt()
396     }
397 
398     /// Computes the squared length of `self`.
399     ///
400     /// This is faster than `length()` as it avoids a square root operation.
401     #[doc(alias = "magnitude2")]
402     #[inline]
length_squared(self) -> f32403     pub fn length_squared(self) -> f32 {
404         self.dot(self)
405     }
406 
407     /// Computes `1.0 / length()`.
408     ///
409     /// For valid results, `self` must _not_ be of length zero.
410     #[inline]
length_recip(self) -> f32411     pub fn length_recip(self) -> f32 {
412         self.length().recip()
413     }
414 
415     /// Computes the Euclidean distance between two points in space.
416     #[inline]
distance(self, rhs: Self) -> f32417     pub fn distance(self, rhs: Self) -> f32 {
418         (self - rhs).length()
419     }
420 
421     /// Compute the squared euclidean distance between two points in space.
422     #[inline]
distance_squared(self, rhs: Self) -> f32423     pub fn distance_squared(self, rhs: Self) -> f32 {
424         (self - rhs).length_squared()
425     }
426 
427     /// Returns `self` normalized to length 1.0.
428     ///
429     /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
430     ///
431     /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`].
432     ///
433     /// Panics
434     ///
435     /// Will panic if `self` is zero length when `glam_assert` is enabled.
436     #[must_use]
437     #[inline]
normalize(self) -> Self438     pub fn normalize(self) -> Self {
439         #[allow(clippy::let_and_return)]
440         let normalized = self.mul(self.length_recip());
441         glam_assert!(normalized.is_finite());
442         normalized
443     }
444 
445     /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
446     ///
447     /// In particular, if the input is zero (or very close to zero), or non-finite,
448     /// the result of this operation will be `None`.
449     ///
450     /// See also [`Self::normalize_or_zero`].
451     #[must_use]
452     #[inline]
try_normalize(self) -> Option<Self>453     pub fn try_normalize(self) -> Option<Self> {
454         let rcp = self.length_recip();
455         if rcp.is_finite() && rcp > 0.0 {
456             Some(self * rcp)
457         } else {
458             None
459         }
460     }
461 
462     /// Returns `self` normalized to length 1.0 if possible, else returns zero.
463     ///
464     /// In particular, if the input is zero (or very close to zero), or non-finite,
465     /// the result of this operation will be zero.
466     ///
467     /// See also [`Self::try_normalize`].
468     #[must_use]
469     #[inline]
normalize_or_zero(self) -> Self470     pub fn normalize_or_zero(self) -> Self {
471         let rcp = self.length_recip();
472         if rcp.is_finite() && rcp > 0.0 {
473             self * rcp
474         } else {
475             Self::ZERO
476         }
477     }
478 
479     /// Returns whether `self` is length `1.0` or not.
480     ///
481     /// Uses a precision threshold of `1e-6`.
482     #[inline]
is_normalized(self) -> bool483     pub fn is_normalized(self) -> bool {
484         // TODO: do something with epsilon
485         (self.length_squared() - 1.0).abs() <= 1e-4
486     }
487 
488     /// Returns the vector projection of `self` onto `rhs`.
489     ///
490     /// `rhs` must be of non-zero length.
491     ///
492     /// # Panics
493     ///
494     /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
495     #[must_use]
496     #[inline]
project_onto(self, rhs: Self) -> Self497     pub fn project_onto(self, rhs: Self) -> Self {
498         let other_len_sq_rcp = rhs.dot(rhs).recip();
499         glam_assert!(other_len_sq_rcp.is_finite());
500         rhs * self.dot(rhs) * other_len_sq_rcp
501     }
502 
503     /// Returns the vector rejection of `self` from `rhs`.
504     ///
505     /// The vector rejection is the vector perpendicular to the projection of `self` onto
506     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
507     ///
508     /// `rhs` must be of non-zero length.
509     ///
510     /// # Panics
511     ///
512     /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
513     #[must_use]
514     #[inline]
reject_from(self, rhs: Self) -> Self515     pub fn reject_from(self, rhs: Self) -> Self {
516         self - self.project_onto(rhs)
517     }
518 
519     /// Returns the vector projection of `self` onto `rhs`.
520     ///
521     /// `rhs` must be normalized.
522     ///
523     /// # Panics
524     ///
525     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
526     #[must_use]
527     #[inline]
project_onto_normalized(self, rhs: Self) -> Self528     pub fn project_onto_normalized(self, rhs: Self) -> Self {
529         glam_assert!(rhs.is_normalized());
530         rhs * self.dot(rhs)
531     }
532 
533     /// Returns the vector rejection of `self` from `rhs`.
534     ///
535     /// The vector rejection is the vector perpendicular to the projection of `self` onto
536     /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
537     ///
538     /// `rhs` must be normalized.
539     ///
540     /// # Panics
541     ///
542     /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
543     #[must_use]
544     #[inline]
reject_from_normalized(self, rhs: Self) -> Self545     pub fn reject_from_normalized(self, rhs: Self) -> Self {
546         self - self.project_onto_normalized(rhs)
547     }
548 
549     /// Returns a vector containing the nearest integer to a number for each element of `self`.
550     /// Round half-way cases away from 0.0.
551     #[inline]
round(self) -> Self552     pub fn round(self) -> Self {
553         Self {
554             x: self.x.round(),
555             y: self.y.round(),
556             z: self.z.round(),
557             w: self.w.round(),
558         }
559     }
560 
561     /// Returns a vector containing the largest integer less than or equal to a number for each
562     /// element of `self`.
563     #[inline]
floor(self) -> Self564     pub fn floor(self) -> Self {
565         Self {
566             x: self.x.floor(),
567             y: self.y.floor(),
568             z: self.z.floor(),
569             w: self.w.floor(),
570         }
571     }
572 
573     /// Returns a vector containing the smallest integer greater than or equal to a number for
574     /// each element of `self`.
575     #[inline]
ceil(self) -> Self576     pub fn ceil(self) -> Self {
577         Self {
578             x: self.x.ceil(),
579             y: self.y.ceil(),
580             z: self.z.ceil(),
581             w: self.w.ceil(),
582         }
583     }
584 
585     /// Returns a vector containing the fractional part of the vector, e.g. `self -
586     /// self.floor()`.
587     ///
588     /// Note that this is fast but not precise for large numbers.
589     #[inline]
fract(self) -> Self590     pub fn fract(self) -> Self {
591         self - self.floor()
592     }
593 
594     /// Returns a vector containing `e^self` (the exponential function) for each element of
595     /// `self`.
596     #[inline]
exp(self) -> Self597     pub fn exp(self) -> Self {
598         Self::new(self.x.exp(), self.y.exp(), self.z.exp(), self.w.exp())
599     }
600 
601     /// Returns a vector containing each element of `self` raised to the power of `n`.
602     #[inline]
powf(self, n: f32) -> Self603     pub fn powf(self, n: f32) -> Self {
604         Self::new(
605             self.x.powf(n),
606             self.y.powf(n),
607             self.z.powf(n),
608             self.w.powf(n),
609         )
610     }
611 
612     /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
613     #[inline]
recip(self) -> Self614     pub fn recip(self) -> Self {
615         Self {
616             x: self.x.recip(),
617             y: self.y.recip(),
618             z: self.z.recip(),
619             w: self.w.recip(),
620         }
621     }
622 
623     /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
624     ///
625     /// When `s` is `0.0`, the result will be equal to `self`.  When `s` is `1.0`, the result
626     /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
627     /// extrapolated.
628     #[doc(alias = "mix")]
629     #[inline]
lerp(self, rhs: Self, s: f32) -> Self630     pub fn lerp(self, rhs: Self, s: f32) -> Self {
631         self + ((rhs - self) * s)
632     }
633 
634     /// Returns true if the absolute difference of all elements between `self` and `rhs` is
635     /// less than or equal to `max_abs_diff`.
636     ///
637     /// This can be used to compare if two vectors contain similar elements. It works best when
638     /// comparing with a known value. The `max_abs_diff` that should be used used depends on
639     /// the values being compared against.
640     ///
641     /// For more see
642     /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
643     #[inline]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool644     pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
645         self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
646     }
647 
648     /// Returns a vector with a length no less than `min` and no more than `max`
649     ///
650     /// # Panics
651     ///
652     /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
653     #[inline]
clamp_length(self, min: f32, max: f32) -> Self654     pub fn clamp_length(self, min: f32, max: f32) -> Self {
655         glam_assert!(min <= max);
656         let length_sq = self.length_squared();
657         if length_sq < min * min {
658             self * (length_sq.sqrt().recip() * min)
659         } else if length_sq > max * max {
660             self * (length_sq.sqrt().recip() * max)
661         } else {
662             self
663         }
664     }
665 
666     /// Returns a vector with a length no more than `max`
clamp_length_max(self, max: f32) -> Self667     pub fn clamp_length_max(self, max: f32) -> Self {
668         let length_sq = self.length_squared();
669         if length_sq > max * max {
670             self * (length_sq.sqrt().recip() * max)
671         } else {
672             self
673         }
674     }
675 
676     /// Returns a vector with a length no less than `min`
clamp_length_min(self, min: f32) -> Self677     pub fn clamp_length_min(self, min: f32) -> Self {
678         let length_sq = self.length_squared();
679         if length_sq < min * min {
680             self * (length_sq.sqrt().recip() * min)
681         } else {
682             self
683         }
684     }
685 
686     /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
687     /// error, yielding a more accurate result than an unfused multiply-add.
688     ///
689     /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
690     /// architecture has a dedicated fma CPU instruction. However, this is not always true,
691     /// and will be heavily dependant on designing algorithms with specific target hardware in
692     /// mind.
693     #[inline]
mul_add(self, a: Self, b: Self) -> Self694     pub fn mul_add(self, a: Self, b: Self) -> Self {
695         Self::new(
696             self.x.mul_add(a.x, b.x),
697             self.y.mul_add(a.y, b.y),
698             self.z.mul_add(a.z, b.z),
699             self.w.mul_add(a.w, b.w),
700         )
701     }
702 
703     /// Casts all elements of `self` to `f64`.
704     #[inline]
as_dvec4(&self) -> crate::DVec4705     pub fn as_dvec4(&self) -> crate::DVec4 {
706         crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
707     }
708 
709     /// Casts all elements of `self` to `i32`.
710     #[inline]
as_ivec4(&self) -> crate::IVec4711     pub fn as_ivec4(&self) -> crate::IVec4 {
712         crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
713     }
714 
715     /// Casts all elements of `self` to `u32`.
716     #[inline]
as_uvec4(&self) -> crate::UVec4717     pub fn as_uvec4(&self) -> crate::UVec4 {
718         crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
719     }
720 }
721 
722 impl Default for Vec4 {
723     #[inline(always)]
default() -> Self724     fn default() -> Self {
725         Self::ZERO
726     }
727 }
728 
729 impl Div<Vec4> for Vec4 {
730     type Output = Self;
731     #[inline]
div(self, rhs: Self) -> Self732     fn div(self, rhs: Self) -> Self {
733         Self {
734             x: self.x.div(rhs.x),
735             y: self.y.div(rhs.y),
736             z: self.z.div(rhs.z),
737             w: self.w.div(rhs.w),
738         }
739     }
740 }
741 
742 impl DivAssign<Vec4> for Vec4 {
743     #[inline]
div_assign(&mut self, rhs: Self)744     fn div_assign(&mut self, rhs: Self) {
745         self.x.div_assign(rhs.x);
746         self.y.div_assign(rhs.y);
747         self.z.div_assign(rhs.z);
748         self.w.div_assign(rhs.w);
749     }
750 }
751 
752 impl Div<f32> for Vec4 {
753     type Output = Self;
754     #[inline]
div(self, rhs: f32) -> Self755     fn div(self, rhs: f32) -> Self {
756         Self {
757             x: self.x.div(rhs),
758             y: self.y.div(rhs),
759             z: self.z.div(rhs),
760             w: self.w.div(rhs),
761         }
762     }
763 }
764 
765 impl DivAssign<f32> for Vec4 {
766     #[inline]
div_assign(&mut self, rhs: f32)767     fn div_assign(&mut self, rhs: f32) {
768         self.x.div_assign(rhs);
769         self.y.div_assign(rhs);
770         self.z.div_assign(rhs);
771         self.w.div_assign(rhs);
772     }
773 }
774 
775 impl Div<Vec4> for f32 {
776     type Output = Vec4;
777     #[inline]
div(self, rhs: Vec4) -> Vec4778     fn div(self, rhs: Vec4) -> Vec4 {
779         Vec4 {
780             x: self.div(rhs.x),
781             y: self.div(rhs.y),
782             z: self.div(rhs.z),
783             w: self.div(rhs.w),
784         }
785     }
786 }
787 
788 impl Mul<Vec4> for Vec4 {
789     type Output = Self;
790     #[inline]
mul(self, rhs: Self) -> Self791     fn mul(self, rhs: Self) -> Self {
792         Self {
793             x: self.x.mul(rhs.x),
794             y: self.y.mul(rhs.y),
795             z: self.z.mul(rhs.z),
796             w: self.w.mul(rhs.w),
797         }
798     }
799 }
800 
801 impl MulAssign<Vec4> for Vec4 {
802     #[inline]
mul_assign(&mut self, rhs: Self)803     fn mul_assign(&mut self, rhs: Self) {
804         self.x.mul_assign(rhs.x);
805         self.y.mul_assign(rhs.y);
806         self.z.mul_assign(rhs.z);
807         self.w.mul_assign(rhs.w);
808     }
809 }
810 
811 impl Mul<f32> for Vec4 {
812     type Output = Self;
813     #[inline]
mul(self, rhs: f32) -> Self814     fn mul(self, rhs: f32) -> Self {
815         Self {
816             x: self.x.mul(rhs),
817             y: self.y.mul(rhs),
818             z: self.z.mul(rhs),
819             w: self.w.mul(rhs),
820         }
821     }
822 }
823 
824 impl MulAssign<f32> for Vec4 {
825     #[inline]
mul_assign(&mut self, rhs: f32)826     fn mul_assign(&mut self, rhs: f32) {
827         self.x.mul_assign(rhs);
828         self.y.mul_assign(rhs);
829         self.z.mul_assign(rhs);
830         self.w.mul_assign(rhs);
831     }
832 }
833 
834 impl Mul<Vec4> for f32 {
835     type Output = Vec4;
836     #[inline]
mul(self, rhs: Vec4) -> Vec4837     fn mul(self, rhs: Vec4) -> Vec4 {
838         Vec4 {
839             x: self.mul(rhs.x),
840             y: self.mul(rhs.y),
841             z: self.mul(rhs.z),
842             w: self.mul(rhs.w),
843         }
844     }
845 }
846 
847 impl Add<Vec4> for Vec4 {
848     type Output = Self;
849     #[inline]
add(self, rhs: Self) -> Self850     fn add(self, rhs: Self) -> Self {
851         Self {
852             x: self.x.add(rhs.x),
853             y: self.y.add(rhs.y),
854             z: self.z.add(rhs.z),
855             w: self.w.add(rhs.w),
856         }
857     }
858 }
859 
860 impl AddAssign<Vec4> for Vec4 {
861     #[inline]
add_assign(&mut self, rhs: Self)862     fn add_assign(&mut self, rhs: Self) {
863         self.x.add_assign(rhs.x);
864         self.y.add_assign(rhs.y);
865         self.z.add_assign(rhs.z);
866         self.w.add_assign(rhs.w);
867     }
868 }
869 
870 impl Add<f32> for Vec4 {
871     type Output = Self;
872     #[inline]
add(self, rhs: f32) -> Self873     fn add(self, rhs: f32) -> Self {
874         Self {
875             x: self.x.add(rhs),
876             y: self.y.add(rhs),
877             z: self.z.add(rhs),
878             w: self.w.add(rhs),
879         }
880     }
881 }
882 
883 impl AddAssign<f32> for Vec4 {
884     #[inline]
add_assign(&mut self, rhs: f32)885     fn add_assign(&mut self, rhs: f32) {
886         self.x.add_assign(rhs);
887         self.y.add_assign(rhs);
888         self.z.add_assign(rhs);
889         self.w.add_assign(rhs);
890     }
891 }
892 
893 impl Add<Vec4> for f32 {
894     type Output = Vec4;
895     #[inline]
add(self, rhs: Vec4) -> Vec4896     fn add(self, rhs: Vec4) -> Vec4 {
897         Vec4 {
898             x: self.add(rhs.x),
899             y: self.add(rhs.y),
900             z: self.add(rhs.z),
901             w: self.add(rhs.w),
902         }
903     }
904 }
905 
906 impl Sub<Vec4> for Vec4 {
907     type Output = Self;
908     #[inline]
sub(self, rhs: Self) -> Self909     fn sub(self, rhs: Self) -> Self {
910         Self {
911             x: self.x.sub(rhs.x),
912             y: self.y.sub(rhs.y),
913             z: self.z.sub(rhs.z),
914             w: self.w.sub(rhs.w),
915         }
916     }
917 }
918 
919 impl SubAssign<Vec4> for Vec4 {
920     #[inline]
sub_assign(&mut self, rhs: Vec4)921     fn sub_assign(&mut self, rhs: Vec4) {
922         self.x.sub_assign(rhs.x);
923         self.y.sub_assign(rhs.y);
924         self.z.sub_assign(rhs.z);
925         self.w.sub_assign(rhs.w);
926     }
927 }
928 
929 impl Sub<f32> for Vec4 {
930     type Output = Self;
931     #[inline]
sub(self, rhs: f32) -> Self932     fn sub(self, rhs: f32) -> Self {
933         Self {
934             x: self.x.sub(rhs),
935             y: self.y.sub(rhs),
936             z: self.z.sub(rhs),
937             w: self.w.sub(rhs),
938         }
939     }
940 }
941 
942 impl SubAssign<f32> for Vec4 {
943     #[inline]
sub_assign(&mut self, rhs: f32)944     fn sub_assign(&mut self, rhs: f32) {
945         self.x.sub_assign(rhs);
946         self.y.sub_assign(rhs);
947         self.z.sub_assign(rhs);
948         self.w.sub_assign(rhs);
949     }
950 }
951 
952 impl Sub<Vec4> for f32 {
953     type Output = Vec4;
954     #[inline]
sub(self, rhs: Vec4) -> Vec4955     fn sub(self, rhs: Vec4) -> Vec4 {
956         Vec4 {
957             x: self.sub(rhs.x),
958             y: self.sub(rhs.y),
959             z: self.sub(rhs.z),
960             w: self.sub(rhs.w),
961         }
962     }
963 }
964 
965 impl Rem<Vec4> for Vec4 {
966     type Output = Self;
967     #[inline]
rem(self, rhs: Self) -> Self968     fn rem(self, rhs: Self) -> Self {
969         Self {
970             x: self.x.rem(rhs.x),
971             y: self.y.rem(rhs.y),
972             z: self.z.rem(rhs.z),
973             w: self.w.rem(rhs.w),
974         }
975     }
976 }
977 
978 impl RemAssign<Vec4> for Vec4 {
979     #[inline]
rem_assign(&mut self, rhs: Self)980     fn rem_assign(&mut self, rhs: Self) {
981         self.x.rem_assign(rhs.x);
982         self.y.rem_assign(rhs.y);
983         self.z.rem_assign(rhs.z);
984         self.w.rem_assign(rhs.w);
985     }
986 }
987 
988 impl Rem<f32> for Vec4 {
989     type Output = Self;
990     #[inline]
rem(self, rhs: f32) -> Self991     fn rem(self, rhs: f32) -> Self {
992         Self {
993             x: self.x.rem(rhs),
994             y: self.y.rem(rhs),
995             z: self.z.rem(rhs),
996             w: self.w.rem(rhs),
997         }
998     }
999 }
1000 
1001 impl RemAssign<f32> for Vec4 {
1002     #[inline]
rem_assign(&mut self, rhs: f32)1003     fn rem_assign(&mut self, rhs: f32) {
1004         self.x.rem_assign(rhs);
1005         self.y.rem_assign(rhs);
1006         self.z.rem_assign(rhs);
1007         self.w.rem_assign(rhs);
1008     }
1009 }
1010 
1011 impl Rem<Vec4> for f32 {
1012     type Output = Vec4;
1013     #[inline]
rem(self, rhs: Vec4) -> Vec41014     fn rem(self, rhs: Vec4) -> Vec4 {
1015         Vec4 {
1016             x: self.rem(rhs.x),
1017             y: self.rem(rhs.y),
1018             z: self.rem(rhs.z),
1019             w: self.rem(rhs.w),
1020         }
1021     }
1022 }
1023 
1024 #[cfg(not(target_arch = "spirv"))]
1025 impl AsRef<[f32; 4]> for Vec4 {
1026     #[inline]
as_ref(&self) -> &[f32; 4]1027     fn as_ref(&self) -> &[f32; 4] {
1028         unsafe { &*(self as *const Vec4 as *const [f32; 4]) }
1029     }
1030 }
1031 
1032 #[cfg(not(target_arch = "spirv"))]
1033 impl AsMut<[f32; 4]> for Vec4 {
1034     #[inline]
as_mut(&mut self) -> &mut [f32; 4]1035     fn as_mut(&mut self) -> &mut [f32; 4] {
1036         unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) }
1037     }
1038 }
1039 
1040 impl Sum for Vec4 {
1041     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1042     fn sum<I>(iter: I) -> Self
1043     where
1044         I: Iterator<Item = Self>,
1045     {
1046         iter.fold(Self::ZERO, Self::add)
1047     }
1048 }
1049 
1050 impl<'a> Sum<&'a Self> for Vec4 {
1051     #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1052     fn sum<I>(iter: I) -> Self
1053     where
1054         I: Iterator<Item = &'a Self>,
1055     {
1056         iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1057     }
1058 }
1059 
1060 impl Product for Vec4 {
1061     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1062     fn product<I>(iter: I) -> Self
1063     where
1064         I: Iterator<Item = Self>,
1065     {
1066         iter.fold(Self::ONE, Self::mul)
1067     }
1068 }
1069 
1070 impl<'a> Product<&'a Self> for Vec4 {
1071     #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1072     fn product<I>(iter: I) -> Self
1073     where
1074         I: Iterator<Item = &'a Self>,
1075     {
1076         iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1077     }
1078 }
1079 
1080 impl Neg for Vec4 {
1081     type Output = Self;
1082     #[inline]
neg(self) -> Self1083     fn neg(self) -> Self {
1084         Self {
1085             x: self.x.neg(),
1086             y: self.y.neg(),
1087             z: self.z.neg(),
1088             w: self.w.neg(),
1089         }
1090     }
1091 }
1092 
1093 impl Index<usize> for Vec4 {
1094     type Output = f32;
1095     #[inline]
index(&self, index: usize) -> &Self::Output1096     fn index(&self, index: usize) -> &Self::Output {
1097         match index {
1098             0 => &self.x,
1099             1 => &self.y,
1100             2 => &self.z,
1101             3 => &self.w,
1102             _ => panic!("index out of bounds"),
1103         }
1104     }
1105 }
1106 
1107 impl IndexMut<usize> for Vec4 {
1108     #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1109     fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1110         match index {
1111             0 => &mut self.x,
1112             1 => &mut self.y,
1113             2 => &mut self.z,
1114             3 => &mut self.w,
1115             _ => panic!("index out of bounds"),
1116         }
1117     }
1118 }
1119 
1120 #[cfg(not(target_arch = "spirv"))]
1121 impl fmt::Display for Vec4 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1122     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1123         write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1124     }
1125 }
1126 
1127 #[cfg(not(target_arch = "spirv"))]
1128 impl fmt::Debug for Vec4 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1129     fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1130         fmt.debug_tuple(stringify!(Vec4))
1131             .field(&self.x)
1132             .field(&self.y)
1133             .field(&self.z)
1134             .field(&self.w)
1135             .finish()
1136     }
1137 }
1138 
1139 impl From<[f32; 4]> for Vec4 {
1140     #[inline]
from(a: [f32; 4]) -> Self1141     fn from(a: [f32; 4]) -> Self {
1142         Self::new(a[0], a[1], a[2], a[3])
1143     }
1144 }
1145 
1146 impl From<Vec4> for [f32; 4] {
1147     #[inline]
from(v: Vec4) -> Self1148     fn from(v: Vec4) -> Self {
1149         [v.x, v.y, v.z, v.w]
1150     }
1151 }
1152 
1153 impl From<(f32, f32, f32, f32)> for Vec4 {
1154     #[inline]
from(t: (f32, f32, f32, f32)) -> Self1155     fn from(t: (f32, f32, f32, f32)) -> Self {
1156         Self::new(t.0, t.1, t.2, t.3)
1157     }
1158 }
1159 
1160 impl From<Vec4> for (f32, f32, f32, f32) {
1161     #[inline]
from(v: Vec4) -> Self1162     fn from(v: Vec4) -> Self {
1163         (v.x, v.y, v.z, v.w)
1164     }
1165 }
1166 
1167 impl From<(Vec3A, f32)> for Vec4 {
1168     #[inline]
from((v, w): (Vec3A, f32)) -> Self1169     fn from((v, w): (Vec3A, f32)) -> Self {
1170         v.extend(w)
1171     }
1172 }
1173 
1174 impl From<(f32, Vec3A)> for Vec4 {
1175     #[inline]
from((x, v): (f32, Vec3A)) -> Self1176     fn from((x, v): (f32, Vec3A)) -> Self {
1177         Self::new(x, v.x, v.y, v.z)
1178     }
1179 }
1180 
1181 impl From<(Vec3, f32)> for Vec4 {
1182     #[inline]
from((v, w): (Vec3, f32)) -> Self1183     fn from((v, w): (Vec3, f32)) -> Self {
1184         Self::new(v.x, v.y, v.z, w)
1185     }
1186 }
1187 
1188 impl From<(f32, Vec3)> for Vec4 {
1189     #[inline]
from((x, v): (f32, Vec3)) -> Self1190     fn from((x, v): (f32, Vec3)) -> Self {
1191         Self::new(x, v.x, v.y, v.z)
1192     }
1193 }
1194 
1195 impl From<(Vec2, f32, f32)> for Vec4 {
1196     #[inline]
from((v, z, w): (Vec2, f32, f32)) -> Self1197     fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1198         Self::new(v.x, v.y, z, w)
1199     }
1200 }
1201 
1202 impl From<(Vec2, Vec2)> for Vec4 {
1203     #[inline]
from((v, u): (Vec2, Vec2)) -> Self1204     fn from((v, u): (Vec2, Vec2)) -> Self {
1205         Self::new(v.x, v.y, u.x, u.y)
1206     }
1207 }
1208