1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3 #[cfg(feature = "scalar-math")]
4 use crate::BVec4 as BVec4A;
5 #[cfg(not(feature = "scalar-math"))]
6 use crate::BVec4A;
7 use crate::{f32::math, Vec2, Vec3, Vec3A};
8
9 #[cfg(not(target_arch = "spirv"))]
10 use core::fmt;
11 use core::iter::{Product, Sum};
12 use core::{f32, ops::*};
13
14 /// Creates a 4-dimensional vector.
15 #[inline(always)]
16 #[must_use]
vec4(x: f32, y: f32, z: f32, w: f32) -> Vec417 pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
18 Vec4::new(x, y, z, w)
19 }
20
21 /// A 4-dimensional vector.
22 #[derive(Clone, Copy, PartialEq)]
23 #[cfg_attr(
24 any(
25 not(any(feature = "scalar-math", target_arch = "spirv")),
26 feature = "cuda"
27 ),
28 repr(align(16))
29 )]
30 #[cfg_attr(not(target_arch = "spirv"), repr(C))]
31 #[cfg_attr(target_arch = "spirv", repr(simd))]
32 pub struct Vec4 {
33 pub x: f32,
34 pub y: f32,
35 pub z: f32,
36 pub w: f32,
37 }
38
39 impl Vec4 {
40 /// All zeroes.
41 pub const ZERO: Self = Self::splat(0.0);
42
43 /// All ones.
44 pub const ONE: Self = Self::splat(1.0);
45
46 /// All negative ones.
47 pub const NEG_ONE: Self = Self::splat(-1.0);
48
49 /// All `f32::MIN`.
50 pub const MIN: Self = Self::splat(f32::MIN);
51
52 /// All `f32::MAX`.
53 pub const MAX: Self = Self::splat(f32::MAX);
54
55 /// All `f32::NAN`.
56 pub const NAN: Self = Self::splat(f32::NAN);
57
58 /// All `f32::INFINITY`.
59 pub const INFINITY: Self = Self::splat(f32::INFINITY);
60
61 /// All `f32::NEG_INFINITY`.
62 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
63
64 /// A unit vector pointing along the positive X axis.
65 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
66
67 /// A unit vector pointing along the positive Y axis.
68 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
69
70 /// A unit vector pointing along the positive Z axis.
71 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
72
73 /// A unit vector pointing along the positive W axis.
74 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
75
76 /// A unit vector pointing along the negative X axis.
77 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
78
79 /// A unit vector pointing along the negative Y axis.
80 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
81
82 /// A unit vector pointing along the negative Z axis.
83 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
84
85 /// A unit vector pointing along the negative W axis.
86 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
87
88 /// The unit axes.
89 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
90
91 /// Creates a new vector.
92 #[inline(always)]
93 #[must_use]
new(x: f32, y: f32, z: f32, w: f32) -> Self94 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
95 Self { x, y, z, w }
96 }
97
98 /// Creates a vector with all elements set to `v`.
99 #[inline]
100 #[must_use]
splat(v: f32) -> Self101 pub const fn splat(v: f32) -> Self {
102 Self {
103 x: v,
104
105 y: v,
106
107 z: v,
108
109 w: v,
110 }
111 }
112
113 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
114 /// for each element of `self`.
115 ///
116 /// A true element in the mask uses the corresponding element from `if_true`, and false
117 /// uses the element from `if_false`.
118 #[inline]
119 #[must_use]
select(mask: BVec4A, if_true: Self, if_false: Self) -> Self120 pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
121 Self {
122 x: if mask.test(0) { if_true.x } else { if_false.x },
123 y: if mask.test(1) { if_true.y } else { if_false.y },
124 z: if mask.test(2) { if_true.z } else { if_false.z },
125 w: if mask.test(3) { if_true.w } else { if_false.w },
126 }
127 }
128
129 /// Creates a new vector from an array.
130 #[inline]
131 #[must_use]
from_array(a: [f32; 4]) -> Self132 pub const fn from_array(a: [f32; 4]) -> Self {
133 Self::new(a[0], a[1], a[2], a[3])
134 }
135
136 /// `[x, y, z, w]`
137 #[inline]
138 #[must_use]
to_array(&self) -> [f32; 4]139 pub const fn to_array(&self) -> [f32; 4] {
140 [self.x, self.y, self.z, self.w]
141 }
142
143 /// Creates a vector from the first 4 values in `slice`.
144 ///
145 /// # Panics
146 ///
147 /// Panics if `slice` is less than 4 elements long.
148 #[inline]
149 #[must_use]
from_slice(slice: &[f32]) -> Self150 pub const fn from_slice(slice: &[f32]) -> Self {
151 Self::new(slice[0], slice[1], slice[2], slice[3])
152 }
153
154 /// Writes the elements of `self` to the first 4 elements in `slice`.
155 ///
156 /// # Panics
157 ///
158 /// Panics if `slice` is less than 4 elements long.
159 #[inline]
write_to_slice(self, slice: &mut [f32])160 pub fn write_to_slice(self, slice: &mut [f32]) {
161 slice[0] = self.x;
162 slice[1] = self.y;
163 slice[2] = self.z;
164 slice[3] = self.w;
165 }
166
167 /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
168 ///
169 /// Truncation to [`Vec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
170 ///
171 /// To truncate to [`Vec3A`] use [`Vec3A::from()`].
172 #[inline]
173 #[must_use]
truncate(self) -> Vec3174 pub fn truncate(self) -> Vec3 {
175 use crate::swizzles::Vec4Swizzles;
176 self.xyz()
177 }
178
179 /// Computes the dot product of `self` and `rhs`.
180 #[inline]
181 #[must_use]
dot(self, rhs: Self) -> f32182 pub fn dot(self, rhs: Self) -> f32 {
183 (self.x * rhs.x) + (self.y * rhs.y) + (self.z * rhs.z) + (self.w * rhs.w)
184 }
185
186 /// Returns a vector where every component is the dot product of `self` and `rhs`.
187 #[inline]
188 #[must_use]
dot_into_vec(self, rhs: Self) -> Self189 pub fn dot_into_vec(self, rhs: Self) -> Self {
190 Self::splat(self.dot(rhs))
191 }
192
193 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
194 ///
195 /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
196 #[inline]
197 #[must_use]
min(self, rhs: Self) -> Self198 pub fn min(self, rhs: Self) -> Self {
199 Self {
200 x: self.x.min(rhs.x),
201 y: self.y.min(rhs.y),
202 z: self.z.min(rhs.z),
203 w: self.w.min(rhs.w),
204 }
205 }
206
207 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
208 ///
209 /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
210 #[inline]
211 #[must_use]
max(self, rhs: Self) -> Self212 pub fn max(self, rhs: Self) -> Self {
213 Self {
214 x: self.x.max(rhs.x),
215 y: self.y.max(rhs.y),
216 z: self.z.max(rhs.z),
217 w: self.w.max(rhs.w),
218 }
219 }
220
221 /// Component-wise clamping of values, similar to [`f32::clamp`].
222 ///
223 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
224 ///
225 /// # Panics
226 ///
227 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
228 #[inline]
229 #[must_use]
clamp(self, min: Self, max: Self) -> Self230 pub fn clamp(self, min: Self, max: Self) -> Self {
231 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
232 self.max(min).min(max)
233 }
234
235 /// Returns the horizontal minimum of `self`.
236 ///
237 /// In other words this computes `min(x, y, ..)`.
238 #[inline]
239 #[must_use]
min_element(self) -> f32240 pub fn min_element(self) -> f32 {
241 self.x.min(self.y.min(self.z.min(self.w)))
242 }
243
244 /// Returns the horizontal maximum of `self`.
245 ///
246 /// In other words this computes `max(x, y, ..)`.
247 #[inline]
248 #[must_use]
max_element(self) -> f32249 pub fn max_element(self) -> f32 {
250 self.x.max(self.y.max(self.z.max(self.w)))
251 }
252
253 /// Returns a vector mask containing the result of a `==` comparison for each element of
254 /// `self` and `rhs`.
255 ///
256 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
257 /// elements.
258 #[inline]
259 #[must_use]
cmpeq(self, rhs: Self) -> BVec4A260 pub fn cmpeq(self, rhs: Self) -> BVec4A {
261 BVec4A::new(
262 self.x.eq(&rhs.x),
263 self.y.eq(&rhs.y),
264 self.z.eq(&rhs.z),
265 self.w.eq(&rhs.w),
266 )
267 }
268
269 /// Returns a vector mask containing the result of a `!=` comparison for each element of
270 /// `self` and `rhs`.
271 ///
272 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
273 /// elements.
274 #[inline]
275 #[must_use]
cmpne(self, rhs: Self) -> BVec4A276 pub fn cmpne(self, rhs: Self) -> BVec4A {
277 BVec4A::new(
278 self.x.ne(&rhs.x),
279 self.y.ne(&rhs.y),
280 self.z.ne(&rhs.z),
281 self.w.ne(&rhs.w),
282 )
283 }
284
285 /// Returns a vector mask containing the result of a `>=` comparison for each element of
286 /// `self` and `rhs`.
287 ///
288 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
289 /// elements.
290 #[inline]
291 #[must_use]
cmpge(self, rhs: Self) -> BVec4A292 pub fn cmpge(self, rhs: Self) -> BVec4A {
293 BVec4A::new(
294 self.x.ge(&rhs.x),
295 self.y.ge(&rhs.y),
296 self.z.ge(&rhs.z),
297 self.w.ge(&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]
307 #[must_use]
cmpgt(self, rhs: Self) -> BVec4A308 pub fn cmpgt(self, rhs: Self) -> BVec4A {
309 BVec4A::new(
310 self.x.gt(&rhs.x),
311 self.y.gt(&rhs.y),
312 self.z.gt(&rhs.z),
313 self.w.gt(&rhs.w),
314 )
315 }
316
317 /// Returns a vector mask containing the result of a `<=` comparison for each element of
318 /// `self` and `rhs`.
319 ///
320 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
321 /// elements.
322 #[inline]
323 #[must_use]
cmple(self, rhs: Self) -> BVec4A324 pub fn cmple(self, rhs: Self) -> BVec4A {
325 BVec4A::new(
326 self.x.le(&rhs.x),
327 self.y.le(&rhs.y),
328 self.z.le(&rhs.z),
329 self.w.le(&rhs.w),
330 )
331 }
332
333 /// Returns a vector mask containing the result of a `<` comparison for each element of
334 /// `self` and `rhs`.
335 ///
336 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
337 /// elements.
338 #[inline]
339 #[must_use]
cmplt(self, rhs: Self) -> BVec4A340 pub fn cmplt(self, rhs: Self) -> BVec4A {
341 BVec4A::new(
342 self.x.lt(&rhs.x),
343 self.y.lt(&rhs.y),
344 self.z.lt(&rhs.z),
345 self.w.lt(&rhs.w),
346 )
347 }
348
349 /// Returns a vector containing the absolute value of each element of `self`.
350 #[inline]
351 #[must_use]
abs(self) -> Self352 pub fn abs(self) -> Self {
353 Self {
354 x: math::abs(self.x),
355 y: math::abs(self.y),
356 z: math::abs(self.z),
357 w: math::abs(self.w),
358 }
359 }
360
361 /// Returns a vector with elements representing the sign of `self`.
362 ///
363 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
364 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
365 /// - `NAN` if the number is `NAN`
366 #[inline]
367 #[must_use]
signum(self) -> Self368 pub fn signum(self) -> Self {
369 Self {
370 x: math::signum(self.x),
371 y: math::signum(self.y),
372 z: math::signum(self.z),
373 w: math::signum(self.w),
374 }
375 }
376
377 /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
378 #[inline]
379 #[must_use]
copysign(self, rhs: Self) -> Self380 pub fn copysign(self, rhs: Self) -> Self {
381 Self {
382 x: math::copysign(self.x, rhs.x),
383 y: math::copysign(self.y, rhs.y),
384 z: math::copysign(self.z, rhs.z),
385 w: math::copysign(self.w, rhs.w),
386 }
387 }
388
389 /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
390 ///
391 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
392 /// into the first lowest bit, element `y` into the second, etc.
393 #[inline]
394 #[must_use]
is_negative_bitmask(self) -> u32395 pub fn is_negative_bitmask(self) -> u32 {
396 (self.x.is_sign_negative() as u32)
397 | (self.y.is_sign_negative() as u32) << 1
398 | (self.z.is_sign_negative() as u32) << 2
399 | (self.w.is_sign_negative() as u32) << 3
400 }
401
402 /// Returns `true` if, and only if, all elements are finite. If any element is either
403 /// `NaN`, positive or negative infinity, this will return `false`.
404 #[inline]
405 #[must_use]
is_finite(self) -> bool406 pub fn is_finite(self) -> bool {
407 self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
408 }
409
410 /// Returns `true` if any elements are `NaN`.
411 #[inline]
412 #[must_use]
is_nan(self) -> bool413 pub fn is_nan(self) -> bool {
414 self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
415 }
416
417 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
418 ///
419 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
420 #[inline]
421 #[must_use]
is_nan_mask(self) -> BVec4A422 pub fn is_nan_mask(self) -> BVec4A {
423 BVec4A::new(
424 self.x.is_nan(),
425 self.y.is_nan(),
426 self.z.is_nan(),
427 self.w.is_nan(),
428 )
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 math::sqrt(self.dot(self))
437 }
438
439 /// Computes the squared length of `self`.
440 ///
441 /// This is faster than `length()` as it avoids a square root operation.
442 #[doc(alias = "magnitude2")]
443 #[inline]
444 #[must_use]
length_squared(self) -> f32445 pub fn length_squared(self) -> f32 {
446 self.dot(self)
447 }
448
449 /// Computes `1.0 / length()`.
450 ///
451 /// For valid results, `self` must _not_ be of length zero.
452 #[inline]
453 #[must_use]
length_recip(self) -> f32454 pub fn length_recip(self) -> f32 {
455 self.length().recip()
456 }
457
458 /// Computes the Euclidean distance between two points in space.
459 #[inline]
460 #[must_use]
distance(self, rhs: Self) -> f32461 pub fn distance(self, rhs: Self) -> f32 {
462 (self - rhs).length()
463 }
464
465 /// Compute the squared euclidean distance between two points in space.
466 #[inline]
467 #[must_use]
distance_squared(self, rhs: Self) -> f32468 pub fn distance_squared(self, rhs: Self) -> f32 {
469 (self - rhs).length_squared()
470 }
471
472 /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
473 #[inline]
474 #[must_use]
div_euclid(self, rhs: Self) -> Self475 pub fn div_euclid(self, rhs: Self) -> Self {
476 Self::new(
477 math::div_euclid(self.x, rhs.x),
478 math::div_euclid(self.y, rhs.y),
479 math::div_euclid(self.z, rhs.z),
480 math::div_euclid(self.w, rhs.w),
481 )
482 }
483
484 /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
485 ///
486 /// [Euclidean division]: f32::rem_euclid
487 #[inline]
488 #[must_use]
rem_euclid(self, rhs: Self) -> Self489 pub fn rem_euclid(self, rhs: Self) -> Self {
490 Self::new(
491 math::rem_euclid(self.x, rhs.x),
492 math::rem_euclid(self.y, rhs.y),
493 math::rem_euclid(self.z, rhs.z),
494 math::rem_euclid(self.w, rhs.w),
495 )
496 }
497
498 /// Returns `self` normalized to length 1.0.
499 ///
500 /// For valid results, `self` must _not_ be 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 `self` is zero length when `glam_assert` is enabled.
507 #[inline]
508 #[must_use]
normalize(self) -> Self509 pub fn normalize(self) -> Self {
510 #[allow(clippy::let_and_return)]
511 let normalized = self.mul(self.length_recip());
512 glam_assert!(normalized.is_finite());
513 normalized
514 }
515
516 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
517 ///
518 /// In particular, if the input is zero (or very close to zero), or non-finite,
519 /// the result of this operation will be `None`.
520 ///
521 /// See also [`Self::normalize_or_zero()`].
522 #[inline]
523 #[must_use]
try_normalize(self) -> Option<Self>524 pub fn try_normalize(self) -> Option<Self> {
525 let rcp = self.length_recip();
526 if rcp.is_finite() && rcp > 0.0 {
527 Some(self * rcp)
528 } else {
529 None
530 }
531 }
532
533 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
534 ///
535 /// In particular, if the input is zero (or very close to zero), or non-finite,
536 /// the result of this operation will be zero.
537 ///
538 /// See also [`Self::try_normalize()`].
539 #[inline]
540 #[must_use]
normalize_or_zero(self) -> Self541 pub fn normalize_or_zero(self) -> Self {
542 let rcp = self.length_recip();
543 if rcp.is_finite() && rcp > 0.0 {
544 self * rcp
545 } else {
546 Self::ZERO
547 }
548 }
549
550 /// Returns whether `self` is length `1.0` or not.
551 ///
552 /// Uses a precision threshold of `1e-6`.
553 #[inline]
554 #[must_use]
is_normalized(self) -> bool555 pub fn is_normalized(self) -> bool {
556 // TODO: do something with epsilon
557 math::abs(self.length_squared() - 1.0) <= 1e-4
558 }
559
560 /// Returns the vector projection of `self` onto `rhs`.
561 ///
562 /// `rhs` must be of non-zero length.
563 ///
564 /// # Panics
565 ///
566 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
567 #[inline]
568 #[must_use]
project_onto(self, rhs: Self) -> Self569 pub fn project_onto(self, rhs: Self) -> Self {
570 let other_len_sq_rcp = rhs.dot(rhs).recip();
571 glam_assert!(other_len_sq_rcp.is_finite());
572 rhs * self.dot(rhs) * other_len_sq_rcp
573 }
574
575 /// Returns the vector rejection of `self` from `rhs`.
576 ///
577 /// The vector rejection is the vector perpendicular to the projection of `self` onto
578 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
579 ///
580 /// `rhs` must be of non-zero length.
581 ///
582 /// # Panics
583 ///
584 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
585 #[inline]
586 #[must_use]
reject_from(self, rhs: Self) -> Self587 pub fn reject_from(self, rhs: Self) -> Self {
588 self - self.project_onto(rhs)
589 }
590
591 /// Returns the vector projection of `self` onto `rhs`.
592 ///
593 /// `rhs` must be normalized.
594 ///
595 /// # Panics
596 ///
597 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
598 #[inline]
599 #[must_use]
project_onto_normalized(self, rhs: Self) -> Self600 pub fn project_onto_normalized(self, rhs: Self) -> Self {
601 glam_assert!(rhs.is_normalized());
602 rhs * self.dot(rhs)
603 }
604
605 /// Returns the vector rejection of `self` from `rhs`.
606 ///
607 /// The vector rejection is the vector perpendicular to the projection of `self` onto
608 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
609 ///
610 /// `rhs` must be normalized.
611 ///
612 /// # Panics
613 ///
614 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
615 #[inline]
616 #[must_use]
reject_from_normalized(self, rhs: Self) -> Self617 pub fn reject_from_normalized(self, rhs: Self) -> Self {
618 self - self.project_onto_normalized(rhs)
619 }
620
621 /// Returns a vector containing the nearest integer to a number for each element of `self`.
622 /// Round half-way cases away from 0.0.
623 #[inline]
624 #[must_use]
round(self) -> Self625 pub fn round(self) -> Self {
626 Self {
627 x: math::round(self.x),
628 y: math::round(self.y),
629 z: math::round(self.z),
630 w: math::round(self.w),
631 }
632 }
633
634 /// Returns a vector containing the largest integer less than or equal to a number for each
635 /// element of `self`.
636 #[inline]
637 #[must_use]
floor(self) -> Self638 pub fn floor(self) -> Self {
639 Self {
640 x: math::floor(self.x),
641 y: math::floor(self.y),
642 z: math::floor(self.z),
643 w: math::floor(self.w),
644 }
645 }
646
647 /// Returns a vector containing the smallest integer greater than or equal to a number for
648 /// each element of `self`.
649 #[inline]
650 #[must_use]
ceil(self) -> Self651 pub fn ceil(self) -> Self {
652 Self {
653 x: math::ceil(self.x),
654 y: math::ceil(self.y),
655 z: math::ceil(self.z),
656 w: math::ceil(self.w),
657 }
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 {
666 x: math::trunc(self.x),
667 y: math::trunc(self.y),
668 z: math::trunc(self.z),
669 w: math::trunc(self.w),
670 }
671 }
672
673 /// Returns a vector containing the fractional part of the vector, e.g. `self -
674 /// self.floor()`.
675 ///
676 /// Note that this is fast but not precise for large numbers.
677 #[inline]
678 #[must_use]
fract(self) -> Self679 pub fn fract(self) -> Self {
680 self - self.floor()
681 }
682
683 /// Returns a vector containing `e^self` (the exponential function) for each element of
684 /// `self`.
685 #[inline]
686 #[must_use]
exp(self) -> Self687 pub fn exp(self) -> Self {
688 Self::new(
689 math::exp(self.x),
690 math::exp(self.y),
691 math::exp(self.z),
692 math::exp(self.w),
693 )
694 }
695
696 /// Returns a vector containing each element of `self` raised to the power of `n`.
697 #[inline]
698 #[must_use]
powf(self, n: f32) -> Self699 pub fn powf(self, n: f32) -> Self {
700 Self::new(
701 math::powf(self.x, n),
702 math::powf(self.y, n),
703 math::powf(self.z, n),
704 math::powf(self.w, n),
705 )
706 }
707
708 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
709 #[inline]
710 #[must_use]
recip(self) -> Self711 pub fn recip(self) -> Self {
712 Self {
713 x: 1.0 / self.x,
714 y: 1.0 / self.y,
715 z: 1.0 / self.z,
716 w: 1.0 / self.w,
717 }
718 }
719
720 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
721 ///
722 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
723 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
724 /// extrapolated.
725 #[doc(alias = "mix")]
726 #[inline]
727 #[must_use]
lerp(self, rhs: Self, s: f32) -> Self728 pub fn lerp(self, rhs: Self, s: f32) -> Self {
729 self + ((rhs - self) * s)
730 }
731
732 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
733 /// less than or equal to `max_abs_diff`.
734 ///
735 /// This can be used to compare if two vectors contain similar elements. It works best when
736 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
737 /// the values being compared against.
738 ///
739 /// For more see
740 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
741 #[inline]
742 #[must_use]
abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool743 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
744 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
745 }
746
747 /// Returns a vector with a length no less than `min` and no more than `max`
748 ///
749 /// # Panics
750 ///
751 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
752 #[inline]
753 #[must_use]
clamp_length(self, min: f32, max: f32) -> Self754 pub fn clamp_length(self, min: f32, max: f32) -> Self {
755 glam_assert!(min <= max);
756 let length_sq = self.length_squared();
757 if length_sq < min * min {
758 min * (self / math::sqrt(length_sq))
759 } else if length_sq > max * max {
760 max * (self / math::sqrt(length_sq))
761 } else {
762 self
763 }
764 }
765
766 /// Returns a vector with a length no more than `max`
767 #[inline]
768 #[must_use]
clamp_length_max(self, max: f32) -> Self769 pub fn clamp_length_max(self, max: f32) -> Self {
770 let length_sq = self.length_squared();
771 if length_sq > max * max {
772 max * (self / math::sqrt(length_sq))
773 } else {
774 self
775 }
776 }
777
778 /// Returns a vector with a length no less than `min`
779 #[inline]
780 #[must_use]
clamp_length_min(self, min: f32) -> Self781 pub fn clamp_length_min(self, min: f32) -> Self {
782 let length_sq = self.length_squared();
783 if length_sq < min * min {
784 min * (self / math::sqrt(length_sq))
785 } else {
786 self
787 }
788 }
789
790 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
791 /// error, yielding a more accurate result than an unfused multiply-add.
792 ///
793 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
794 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
795 /// and will be heavily dependant on designing algorithms with specific target hardware in
796 /// mind.
797 #[inline]
798 #[must_use]
mul_add(self, a: Self, b: Self) -> Self799 pub fn mul_add(self, a: Self, b: Self) -> Self {
800 Self::new(
801 math::mul_add(self.x, a.x, b.x),
802 math::mul_add(self.y, a.y, b.y),
803 math::mul_add(self.z, a.z, b.z),
804 math::mul_add(self.w, a.w, b.w),
805 )
806 }
807
808 /// Casts all elements of `self` to `f64`.
809 #[inline]
810 #[must_use]
as_dvec4(&self) -> crate::DVec4811 pub fn as_dvec4(&self) -> crate::DVec4 {
812 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
813 }
814
815 /// Casts all elements of `self` to `i16`.
816 #[inline]
817 #[must_use]
as_i16vec4(&self) -> crate::I16Vec4818 pub fn as_i16vec4(&self) -> crate::I16Vec4 {
819 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
820 }
821
822 /// Casts all elements of `self` to `u16`.
823 #[inline]
824 #[must_use]
as_u16vec4(&self) -> crate::U16Vec4825 pub fn as_u16vec4(&self) -> crate::U16Vec4 {
826 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
827 }
828
829 /// Casts all elements of `self` to `i32`.
830 #[inline]
831 #[must_use]
as_ivec4(&self) -> crate::IVec4832 pub fn as_ivec4(&self) -> crate::IVec4 {
833 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
834 }
835
836 /// Casts all elements of `self` to `u32`.
837 #[inline]
838 #[must_use]
as_uvec4(&self) -> crate::UVec4839 pub fn as_uvec4(&self) -> crate::UVec4 {
840 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
841 }
842
843 /// Casts all elements of `self` to `i64`.
844 #[inline]
845 #[must_use]
as_i64vec4(&self) -> crate::I64Vec4846 pub fn as_i64vec4(&self) -> crate::I64Vec4 {
847 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
848 }
849
850 /// Casts all elements of `self` to `u64`.
851 #[inline]
852 #[must_use]
as_u64vec4(&self) -> crate::U64Vec4853 pub fn as_u64vec4(&self) -> crate::U64Vec4 {
854 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
855 }
856 }
857
858 impl Default for Vec4 {
859 #[inline(always)]
default() -> Self860 fn default() -> Self {
861 Self::ZERO
862 }
863 }
864
865 impl Div<Vec4> for Vec4 {
866 type Output = Self;
867 #[inline]
div(self, rhs: Self) -> Self868 fn div(self, rhs: Self) -> Self {
869 Self {
870 x: self.x.div(rhs.x),
871 y: self.y.div(rhs.y),
872 z: self.z.div(rhs.z),
873 w: self.w.div(rhs.w),
874 }
875 }
876 }
877
878 impl DivAssign<Vec4> for Vec4 {
879 #[inline]
div_assign(&mut self, rhs: Self)880 fn div_assign(&mut self, rhs: Self) {
881 self.x.div_assign(rhs.x);
882 self.y.div_assign(rhs.y);
883 self.z.div_assign(rhs.z);
884 self.w.div_assign(rhs.w);
885 }
886 }
887
888 impl Div<f32> for Vec4 {
889 type Output = Self;
890 #[inline]
div(self, rhs: f32) -> Self891 fn div(self, rhs: f32) -> Self {
892 Self {
893 x: self.x.div(rhs),
894 y: self.y.div(rhs),
895 z: self.z.div(rhs),
896 w: self.w.div(rhs),
897 }
898 }
899 }
900
901 impl DivAssign<f32> for Vec4 {
902 #[inline]
div_assign(&mut self, rhs: f32)903 fn div_assign(&mut self, rhs: f32) {
904 self.x.div_assign(rhs);
905 self.y.div_assign(rhs);
906 self.z.div_assign(rhs);
907 self.w.div_assign(rhs);
908 }
909 }
910
911 impl Div<Vec4> for f32 {
912 type Output = Vec4;
913 #[inline]
div(self, rhs: Vec4) -> Vec4914 fn div(self, rhs: Vec4) -> Vec4 {
915 Vec4 {
916 x: self.div(rhs.x),
917 y: self.div(rhs.y),
918 z: self.div(rhs.z),
919 w: self.div(rhs.w),
920 }
921 }
922 }
923
924 impl Mul<Vec4> for Vec4 {
925 type Output = Self;
926 #[inline]
mul(self, rhs: Self) -> Self927 fn mul(self, rhs: Self) -> Self {
928 Self {
929 x: self.x.mul(rhs.x),
930 y: self.y.mul(rhs.y),
931 z: self.z.mul(rhs.z),
932 w: self.w.mul(rhs.w),
933 }
934 }
935 }
936
937 impl MulAssign<Vec4> for Vec4 {
938 #[inline]
mul_assign(&mut self, rhs: Self)939 fn mul_assign(&mut self, rhs: Self) {
940 self.x.mul_assign(rhs.x);
941 self.y.mul_assign(rhs.y);
942 self.z.mul_assign(rhs.z);
943 self.w.mul_assign(rhs.w);
944 }
945 }
946
947 impl Mul<f32> for Vec4 {
948 type Output = Self;
949 #[inline]
mul(self, rhs: f32) -> Self950 fn mul(self, rhs: f32) -> Self {
951 Self {
952 x: self.x.mul(rhs),
953 y: self.y.mul(rhs),
954 z: self.z.mul(rhs),
955 w: self.w.mul(rhs),
956 }
957 }
958 }
959
960 impl MulAssign<f32> for Vec4 {
961 #[inline]
mul_assign(&mut self, rhs: f32)962 fn mul_assign(&mut self, rhs: f32) {
963 self.x.mul_assign(rhs);
964 self.y.mul_assign(rhs);
965 self.z.mul_assign(rhs);
966 self.w.mul_assign(rhs);
967 }
968 }
969
970 impl Mul<Vec4> for f32 {
971 type Output = Vec4;
972 #[inline]
mul(self, rhs: Vec4) -> Vec4973 fn mul(self, rhs: Vec4) -> Vec4 {
974 Vec4 {
975 x: self.mul(rhs.x),
976 y: self.mul(rhs.y),
977 z: self.mul(rhs.z),
978 w: self.mul(rhs.w),
979 }
980 }
981 }
982
983 impl Add<Vec4> for Vec4 {
984 type Output = Self;
985 #[inline]
add(self, rhs: Self) -> Self986 fn add(self, rhs: Self) -> Self {
987 Self {
988 x: self.x.add(rhs.x),
989 y: self.y.add(rhs.y),
990 z: self.z.add(rhs.z),
991 w: self.w.add(rhs.w),
992 }
993 }
994 }
995
996 impl AddAssign<Vec4> for Vec4 {
997 #[inline]
add_assign(&mut self, rhs: Self)998 fn add_assign(&mut self, rhs: Self) {
999 self.x.add_assign(rhs.x);
1000 self.y.add_assign(rhs.y);
1001 self.z.add_assign(rhs.z);
1002 self.w.add_assign(rhs.w);
1003 }
1004 }
1005
1006 impl Add<f32> for Vec4 {
1007 type Output = Self;
1008 #[inline]
add(self, rhs: f32) -> Self1009 fn add(self, rhs: f32) -> Self {
1010 Self {
1011 x: self.x.add(rhs),
1012 y: self.y.add(rhs),
1013 z: self.z.add(rhs),
1014 w: self.w.add(rhs),
1015 }
1016 }
1017 }
1018
1019 impl AddAssign<f32> for Vec4 {
1020 #[inline]
add_assign(&mut self, rhs: f32)1021 fn add_assign(&mut self, rhs: f32) {
1022 self.x.add_assign(rhs);
1023 self.y.add_assign(rhs);
1024 self.z.add_assign(rhs);
1025 self.w.add_assign(rhs);
1026 }
1027 }
1028
1029 impl Add<Vec4> for f32 {
1030 type Output = Vec4;
1031 #[inline]
add(self, rhs: Vec4) -> Vec41032 fn add(self, rhs: Vec4) -> Vec4 {
1033 Vec4 {
1034 x: self.add(rhs.x),
1035 y: self.add(rhs.y),
1036 z: self.add(rhs.z),
1037 w: self.add(rhs.w),
1038 }
1039 }
1040 }
1041
1042 impl Sub<Vec4> for Vec4 {
1043 type Output = Self;
1044 #[inline]
sub(self, rhs: Self) -> Self1045 fn sub(self, rhs: Self) -> Self {
1046 Self {
1047 x: self.x.sub(rhs.x),
1048 y: self.y.sub(rhs.y),
1049 z: self.z.sub(rhs.z),
1050 w: self.w.sub(rhs.w),
1051 }
1052 }
1053 }
1054
1055 impl SubAssign<Vec4> for Vec4 {
1056 #[inline]
sub_assign(&mut self, rhs: Vec4)1057 fn sub_assign(&mut self, rhs: Vec4) {
1058 self.x.sub_assign(rhs.x);
1059 self.y.sub_assign(rhs.y);
1060 self.z.sub_assign(rhs.z);
1061 self.w.sub_assign(rhs.w);
1062 }
1063 }
1064
1065 impl Sub<f32> for Vec4 {
1066 type Output = Self;
1067 #[inline]
sub(self, rhs: f32) -> Self1068 fn sub(self, rhs: f32) -> Self {
1069 Self {
1070 x: self.x.sub(rhs),
1071 y: self.y.sub(rhs),
1072 z: self.z.sub(rhs),
1073 w: self.w.sub(rhs),
1074 }
1075 }
1076 }
1077
1078 impl SubAssign<f32> for Vec4 {
1079 #[inline]
sub_assign(&mut self, rhs: f32)1080 fn sub_assign(&mut self, rhs: f32) {
1081 self.x.sub_assign(rhs);
1082 self.y.sub_assign(rhs);
1083 self.z.sub_assign(rhs);
1084 self.w.sub_assign(rhs);
1085 }
1086 }
1087
1088 impl Sub<Vec4> for f32 {
1089 type Output = Vec4;
1090 #[inline]
sub(self, rhs: Vec4) -> Vec41091 fn sub(self, rhs: Vec4) -> Vec4 {
1092 Vec4 {
1093 x: self.sub(rhs.x),
1094 y: self.sub(rhs.y),
1095 z: self.sub(rhs.z),
1096 w: self.sub(rhs.w),
1097 }
1098 }
1099 }
1100
1101 impl Rem<Vec4> for Vec4 {
1102 type Output = Self;
1103 #[inline]
rem(self, rhs: Self) -> Self1104 fn rem(self, rhs: Self) -> Self {
1105 Self {
1106 x: self.x.rem(rhs.x),
1107 y: self.y.rem(rhs.y),
1108 z: self.z.rem(rhs.z),
1109 w: self.w.rem(rhs.w),
1110 }
1111 }
1112 }
1113
1114 impl RemAssign<Vec4> for Vec4 {
1115 #[inline]
rem_assign(&mut self, rhs: Self)1116 fn rem_assign(&mut self, rhs: Self) {
1117 self.x.rem_assign(rhs.x);
1118 self.y.rem_assign(rhs.y);
1119 self.z.rem_assign(rhs.z);
1120 self.w.rem_assign(rhs.w);
1121 }
1122 }
1123
1124 impl Rem<f32> for Vec4 {
1125 type Output = Self;
1126 #[inline]
rem(self, rhs: f32) -> Self1127 fn rem(self, rhs: f32) -> Self {
1128 Self {
1129 x: self.x.rem(rhs),
1130 y: self.y.rem(rhs),
1131 z: self.z.rem(rhs),
1132 w: self.w.rem(rhs),
1133 }
1134 }
1135 }
1136
1137 impl RemAssign<f32> for Vec4 {
1138 #[inline]
rem_assign(&mut self, rhs: f32)1139 fn rem_assign(&mut self, rhs: f32) {
1140 self.x.rem_assign(rhs);
1141 self.y.rem_assign(rhs);
1142 self.z.rem_assign(rhs);
1143 self.w.rem_assign(rhs);
1144 }
1145 }
1146
1147 impl Rem<Vec4> for f32 {
1148 type Output = Vec4;
1149 #[inline]
rem(self, rhs: Vec4) -> Vec41150 fn rem(self, rhs: Vec4) -> Vec4 {
1151 Vec4 {
1152 x: self.rem(rhs.x),
1153 y: self.rem(rhs.y),
1154 z: self.rem(rhs.z),
1155 w: self.rem(rhs.w),
1156 }
1157 }
1158 }
1159
1160 #[cfg(not(target_arch = "spirv"))]
1161 impl AsRef<[f32; 4]> for Vec4 {
1162 #[inline]
as_ref(&self) -> &[f32; 4]1163 fn as_ref(&self) -> &[f32; 4] {
1164 unsafe { &*(self as *const Vec4 as *const [f32; 4]) }
1165 }
1166 }
1167
1168 #[cfg(not(target_arch = "spirv"))]
1169 impl AsMut<[f32; 4]> for Vec4 {
1170 #[inline]
as_mut(&mut self) -> &mut [f32; 4]1171 fn as_mut(&mut self) -> &mut [f32; 4] {
1172 unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) }
1173 }
1174 }
1175
1176 impl Sum for Vec4 {
1177 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = Self>,1178 fn sum<I>(iter: I) -> Self
1179 where
1180 I: Iterator<Item = Self>,
1181 {
1182 iter.fold(Self::ZERO, Self::add)
1183 }
1184 }
1185
1186 impl<'a> Sum<&'a Self> for Vec4 {
1187 #[inline]
sum<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1188 fn sum<I>(iter: I) -> Self
1189 where
1190 I: Iterator<Item = &'a Self>,
1191 {
1192 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1193 }
1194 }
1195
1196 impl Product for Vec4 {
1197 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = Self>,1198 fn product<I>(iter: I) -> Self
1199 where
1200 I: Iterator<Item = Self>,
1201 {
1202 iter.fold(Self::ONE, Self::mul)
1203 }
1204 }
1205
1206 impl<'a> Product<&'a Self> for Vec4 {
1207 #[inline]
product<I>(iter: I) -> Self where I: Iterator<Item = &'a Self>,1208 fn product<I>(iter: I) -> Self
1209 where
1210 I: Iterator<Item = &'a Self>,
1211 {
1212 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1213 }
1214 }
1215
1216 impl Neg for Vec4 {
1217 type Output = Self;
1218 #[inline]
neg(self) -> Self1219 fn neg(self) -> Self {
1220 Self {
1221 x: self.x.neg(),
1222 y: self.y.neg(),
1223 z: self.z.neg(),
1224 w: self.w.neg(),
1225 }
1226 }
1227 }
1228
1229 impl Index<usize> for Vec4 {
1230 type Output = f32;
1231 #[inline]
index(&self, index: usize) -> &Self::Output1232 fn index(&self, index: usize) -> &Self::Output {
1233 match index {
1234 0 => &self.x,
1235 1 => &self.y,
1236 2 => &self.z,
1237 3 => &self.w,
1238 _ => panic!("index out of bounds"),
1239 }
1240 }
1241 }
1242
1243 impl IndexMut<usize> for Vec4 {
1244 #[inline]
index_mut(&mut self, index: usize) -> &mut Self::Output1245 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1246 match index {
1247 0 => &mut self.x,
1248 1 => &mut self.y,
1249 2 => &mut self.z,
1250 3 => &mut self.w,
1251 _ => panic!("index out of bounds"),
1252 }
1253 }
1254 }
1255
1256 #[cfg(not(target_arch = "spirv"))]
1257 impl fmt::Display for Vec4 {
fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result1258 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1259 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1260 }
1261 }
1262
1263 #[cfg(not(target_arch = "spirv"))]
1264 impl fmt::Debug for Vec4 {
fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result1265 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1266 fmt.debug_tuple(stringify!(Vec4))
1267 .field(&self.x)
1268 .field(&self.y)
1269 .field(&self.z)
1270 .field(&self.w)
1271 .finish()
1272 }
1273 }
1274
1275 impl From<[f32; 4]> for Vec4 {
1276 #[inline]
from(a: [f32; 4]) -> Self1277 fn from(a: [f32; 4]) -> Self {
1278 Self::new(a[0], a[1], a[2], a[3])
1279 }
1280 }
1281
1282 impl From<Vec4> for [f32; 4] {
1283 #[inline]
from(v: Vec4) -> Self1284 fn from(v: Vec4) -> Self {
1285 [v.x, v.y, v.z, v.w]
1286 }
1287 }
1288
1289 impl From<(f32, f32, f32, f32)> for Vec4 {
1290 #[inline]
from(t: (f32, f32, f32, f32)) -> Self1291 fn from(t: (f32, f32, f32, f32)) -> Self {
1292 Self::new(t.0, t.1, t.2, t.3)
1293 }
1294 }
1295
1296 impl From<Vec4> for (f32, f32, f32, f32) {
1297 #[inline]
from(v: Vec4) -> Self1298 fn from(v: Vec4) -> Self {
1299 (v.x, v.y, v.z, v.w)
1300 }
1301 }
1302
1303 impl From<(Vec3A, f32)> for Vec4 {
1304 #[inline]
from((v, w): (Vec3A, f32)) -> Self1305 fn from((v, w): (Vec3A, f32)) -> Self {
1306 v.extend(w)
1307 }
1308 }
1309
1310 impl From<(f32, Vec3A)> for Vec4 {
1311 #[inline]
from((x, v): (f32, Vec3A)) -> Self1312 fn from((x, v): (f32, Vec3A)) -> Self {
1313 Self::new(x, v.x, v.y, v.z)
1314 }
1315 }
1316
1317 impl From<(Vec3, f32)> for Vec4 {
1318 #[inline]
from((v, w): (Vec3, f32)) -> Self1319 fn from((v, w): (Vec3, f32)) -> Self {
1320 Self::new(v.x, v.y, v.z, w)
1321 }
1322 }
1323
1324 impl From<(f32, Vec3)> for Vec4 {
1325 #[inline]
from((x, v): (f32, Vec3)) -> Self1326 fn from((x, v): (f32, Vec3)) -> Self {
1327 Self::new(x, v.x, v.y, v.z)
1328 }
1329 }
1330
1331 impl From<(Vec2, f32, f32)> for Vec4 {
1332 #[inline]
from((v, z, w): (Vec2, f32, f32)) -> Self1333 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1334 Self::new(v.x, v.y, z, w)
1335 }
1336 }
1337
1338 impl From<(Vec2, Vec2)> for Vec4 {
1339 #[inline]
from((v, u): (Vec2, Vec2)) -> Self1340 fn from((v, u): (Vec2, Vec2)) -> Self {
1341 Self::new(v.x, v.y, u.x, u.y)
1342 }
1343 }
1344