Debian Codegraph @

debian/missing-sources/glam-0.24.2/src/f32/scalar/vec4.rs

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