Debian Codegraph @

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

raw
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)]
16 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)]
80 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]
86 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]
104 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]
115 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]
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]
131 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]
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]
154 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]
161 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]
167 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]
175 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]
188 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]
205 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]
214 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]
222 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]
232 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]
247 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]
262 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]
277 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]
292 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]
307 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]
318 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]
333 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 bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
343 ///
344 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
345 /// into the first lowest bit, element `y` into the second, etc.
346 #[inline]
347 pub fn is_negative_bitmask(self) -> u32 {
348 (self.x.is_sign_negative() as u32)
349 | (self.y.is_sign_negative() as u32) << 1
350 | (self.z.is_sign_negative() as u32) << 2
351 | (self.w.is_sign_negative() as u32) << 3
352 }
353
354 /// Returns `true` if, and only if, all elements are finite. If any element is either
355 /// `NaN`, positive or negative infinity, this will return `false`.
356 #[inline]
357 pub fn is_finite(self) -> bool {
358 self.x.is_finite() && self.y.is_finite() && self.z.is_finite() && self.w.is_finite()
359 }
360
361 /// Returns `true` if any elements are `NaN`.
362 #[inline]
363 pub fn is_nan(self) -> bool {
364 self.x.is_nan() || self.y.is_nan() || self.z.is_nan() || self.w.is_nan()
365 }
366
367 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
368 ///
369 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
370 #[inline]
371 pub fn is_nan_mask(self) -> BVec4 {
372 BVec4::new(
373 self.x.is_nan(),
374 self.y.is_nan(),
375 self.z.is_nan(),
376 self.w.is_nan(),
377 )
378 }
379
380 /// Computes the length of `self`.
381 #[doc(alias = "magnitude")]
382 #[inline]
383 pub fn length(self) -> f32 {
384 self.dot(self).sqrt()
385 }
386
387 /// Computes the squared length of `self`.
388 ///
389 /// This is faster than `length()` as it avoids a square root operation.
390 #[doc(alias = "magnitude2")]
391 #[inline]
392 pub fn length_squared(self) -> f32 {
393 self.dot(self)
394 }
395
396 /// Computes `1.0 / length()`.
397 ///
398 /// For valid results, `self` must _not_ be of length zero.
399 #[inline]
400 pub fn length_recip(self) -> f32 {
401 self.length().recip()
402 }
403
404 /// Computes the Euclidean distance between two points in space.
405 #[inline]
406 pub fn distance(self, rhs: Self) -> f32 {
407 (self - rhs).length()
408 }
409
410 /// Compute the squared euclidean distance between two points in space.
411 #[inline]
412 pub fn distance_squared(self, rhs: Self) -> f32 {
413 (self - rhs).length_squared()
414 }
415
416 /// Returns `self` normalized to length 1.0.
417 ///
418 /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
419 ///
420 /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`].
421 ///
422 /// Panics
423 ///
424 /// Will panic if `self` is zero length when `glam_assert` is enabled.
425 #[must_use]
426 #[inline]
427 pub fn normalize(self) -> Self {
428 #[allow(clippy::let_and_return)]
429 let normalized = self.mul(self.length_recip());
430 glam_assert!(normalized.is_finite());
431 normalized
432 }
433
434 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
435 ///
436 /// In particular, if the input is zero (or very close to zero), or non-finite,
437 /// the result of this operation will be `None`.
438 ///
439 /// See also [`Self::normalize_or_zero`].
440 #[must_use]
441 #[inline]
442 pub fn try_normalize(self) -> Option<Self> {
443 let rcp = self.length_recip();
444 if rcp.is_finite() && rcp > 0.0 {
445 Some(self * rcp)
446 } else {
447 None
448 }
449 }
450
451 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
452 ///
453 /// In particular, if the input is zero (or very close to zero), or non-finite,
454 /// the result of this operation will be zero.
455 ///
456 /// See also [`Self::try_normalize`].
457 #[must_use]
458 #[inline]
459 pub fn normalize_or_zero(self) -> Self {
460 let rcp = self.length_recip();
461 if rcp.is_finite() && rcp > 0.0 {
462 self * rcp
463 } else {
464 Self::ZERO
465 }
466 }
467
468 /// Returns whether `self` is length `1.0` or not.
469 ///
470 /// Uses a precision threshold of `1e-6`.
471 #[inline]
472 pub fn is_normalized(self) -> bool {
473 // TODO: do something with epsilon
474 (self.length_squared() - 1.0).abs() <= 1e-4
475 }
476
477 /// Returns the vector projection of `self` onto `rhs`.
478 ///
479 /// `rhs` must be of non-zero length.
480 ///
481 /// # Panics
482 ///
483 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
484 #[must_use]
485 #[inline]
486 pub fn project_onto(self, rhs: Self) -> Self {
487 let other_len_sq_rcp = rhs.dot(rhs).recip();
488 glam_assert!(other_len_sq_rcp.is_finite());
489 rhs * self.dot(rhs) * other_len_sq_rcp
490 }
491
492 /// Returns the vector rejection of `self` from `rhs`.
493 ///
494 /// The vector rejection is the vector perpendicular to the projection of `self` onto
495 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
496 ///
497 /// `rhs` must be of non-zero length.
498 ///
499 /// # Panics
500 ///
501 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
502 #[must_use]
503 #[inline]
504 pub fn reject_from(self, rhs: Self) -> Self {
505 self - self.project_onto(rhs)
506 }
507
508 /// Returns the vector projection of `self` onto `rhs`.
509 ///
510 /// `rhs` must be normalized.
511 ///
512 /// # Panics
513 ///
514 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
515 #[must_use]
516 #[inline]
517 pub fn project_onto_normalized(self, rhs: Self) -> Self {
518 glam_assert!(rhs.is_normalized());
519 rhs * self.dot(rhs)
520 }
521
522 /// Returns the vector rejection of `self` from `rhs`.
523 ///
524 /// The vector rejection is the vector perpendicular to the projection of `self` onto
525 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
526 ///
527 /// `rhs` must be normalized.
528 ///
529 /// # Panics
530 ///
531 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
532 #[must_use]
533 #[inline]
534 pub fn reject_from_normalized(self, rhs: Self) -> Self {
535 self - self.project_onto_normalized(rhs)
536 }
537
538 /// Returns a vector containing the nearest integer to a number for each element of `self`.
539 /// Round half-way cases away from 0.0.
540 #[inline]
541 pub fn round(self) -> Self {
542 Self {
543 x: self.x.round(),
544 y: self.y.round(),
545 z: self.z.round(),
546 w: self.w.round(),
547 }
548 }
549
550 /// Returns a vector containing the largest integer less than or equal to a number for each
551 /// element of `self`.
552 #[inline]
553 pub fn floor(self) -> Self {
554 Self {
555 x: self.x.floor(),
556 y: self.y.floor(),
557 z: self.z.floor(),
558 w: self.w.floor(),
559 }
560 }
561
562 /// Returns a vector containing the smallest integer greater than or equal to a number for
563 /// each element of `self`.
564 #[inline]
565 pub fn ceil(self) -> Self {
566 Self {
567 x: self.x.ceil(),
568 y: self.y.ceil(),
569 z: self.z.ceil(),
570 w: self.w.ceil(),
571 }
572 }
573
574 /// Returns a vector containing the fractional part of the vector, e.g. `self -
575 /// self.floor()`.
576 ///
577 /// Note that this is fast but not precise for large numbers.
578 #[inline]
579 pub fn fract(self) -> Self {
580 self - self.floor()
581 }
582
583 /// Returns a vector containing `e^self` (the exponential function) for each element of
584 /// `self`.
585 #[inline]
586 pub fn exp(self) -> Self {
587 Self::new(self.x.exp(), self.y.exp(), self.z.exp(), self.w.exp())
588 }
589
590 /// Returns a vector containing each element of `self` raised to the power of `n`.
591 #[inline]
592 pub fn powf(self, n: f32) -> Self {
593 Self::new(
594 self.x.powf(n),
595 self.y.powf(n),
596 self.z.powf(n),
597 self.w.powf(n),
598 )
599 }
600
601 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
602 #[inline]
603 pub fn recip(self) -> Self {
604 Self {
605 x: self.x.recip(),
606 y: self.y.recip(),
607 z: self.z.recip(),
608 w: self.w.recip(),
609 }
610 }
611
612 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
613 ///
614 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
615 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
616 /// extrapolated.
617 #[doc(alias = "mix")]
618 #[inline]
619 pub fn lerp(self, rhs: Self, s: f32) -> Self {
620 self + ((rhs - self) * s)
621 }
622
623 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
624 /// less than or equal to `max_abs_diff`.
625 ///
626 /// This can be used to compare if two vectors contain similar elements. It works best when
627 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
628 /// the values being compared against.
629 ///
630 /// For more see
631 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
632 #[inline]
633 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
634 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
635 }
636
637 /// Returns a vector with a length no less than `min` and no more than `max`
638 ///
639 /// # Panics
640 ///
641 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
642 #[inline]
643 pub fn clamp_length(self, min: f32, max: f32) -> Self {
644 glam_assert!(min <= max);
645 let length_sq = self.length_squared();
646 if length_sq < min * min {
647 self * (length_sq.sqrt().recip() * min)
648 } else if length_sq > max * max {
649 self * (length_sq.sqrt().recip() * max)
650 } else {
651 self
652 }
653 }
654
655 /// Returns a vector with a length no more than `max`
656 pub fn clamp_length_max(self, max: f32) -> Self {
657 let length_sq = self.length_squared();
658 if length_sq > max * max {
659 self * (length_sq.sqrt().recip() * max)
660 } else {
661 self
662 }
663 }
664
665 /// Returns a vector with a length no less than `min`
666 pub fn clamp_length_min(self, min: f32) -> Self {
667 let length_sq = self.length_squared();
668 if length_sq < min * min {
669 self * (length_sq.sqrt().recip() * min)
670 } else {
671 self
672 }
673 }
674
675 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
676 /// error, yielding a more accurate result than an unfused multiply-add.
677 ///
678 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
679 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
680 /// and will be heavily dependant on designing algorithms with specific target hardware in
681 /// mind.
682 #[inline]
683 pub fn mul_add(self, a: Self, b: Self) -> Self {
684 Self::new(
685 self.x.mul_add(a.x, b.x),
686 self.y.mul_add(a.y, b.y),
687 self.z.mul_add(a.z, b.z),
688 self.w.mul_add(a.w, b.w),
689 )
690 }
691
692 /// Casts all elements of `self` to `f64`.
693 #[inline]
694 pub fn as_dvec4(&self) -> crate::DVec4 {
695 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
696 }
697
698 /// Casts all elements of `self` to `i32`.
699 #[inline]
700 pub fn as_ivec4(&self) -> crate::IVec4 {
701 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
702 }
703
704 /// Casts all elements of `self` to `u32`.
705 #[inline]
706 pub fn as_uvec4(&self) -> crate::UVec4 {
707 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
708 }
709 }
710
711 impl Default for Vec4 {
712 #[inline(always)]
713 fn default() -> Self {
714 Self::ZERO
715 }
716 }
717
718 impl Div<Vec4> for Vec4 {
719 type Output = Self;
720 #[inline]
721 fn div(self, rhs: Self) -> Self {
722 Self {
723 x: self.x.div(rhs.x),
724 y: self.y.div(rhs.y),
725 z: self.z.div(rhs.z),
726 w: self.w.div(rhs.w),
727 }
728 }
729 }
730
731 impl DivAssign<Vec4> for Vec4 {
732 #[inline]
733 fn div_assign(&mut self, rhs: Self) {
734 self.x.div_assign(rhs.x);
735 self.y.div_assign(rhs.y);
736 self.z.div_assign(rhs.z);
737 self.w.div_assign(rhs.w);
738 }
739 }
740
741 impl Div<f32> for Vec4 {
742 type Output = Self;
743 #[inline]
744 fn div(self, rhs: f32) -> Self {
745 Self {
746 x: self.x.div(rhs),
747 y: self.y.div(rhs),
748 z: self.z.div(rhs),
749 w: self.w.div(rhs),
750 }
751 }
752 }
753
754 impl DivAssign<f32> for Vec4 {
755 #[inline]
756 fn div_assign(&mut self, rhs: f32) {
757 self.x.div_assign(rhs);
758 self.y.div_assign(rhs);
759 self.z.div_assign(rhs);
760 self.w.div_assign(rhs);
761 }
762 }
763
764 impl Div<Vec4> for f32 {
765 type Output = Vec4;
766 #[inline]
767 fn div(self, rhs: Vec4) -> Vec4 {
768 Vec4 {
769 x: self.div(rhs.x),
770 y: self.div(rhs.y),
771 z: self.div(rhs.z),
772 w: self.div(rhs.w),
773 }
774 }
775 }
776
777 impl Mul<Vec4> for Vec4 {
778 type Output = Self;
779 #[inline]
780 fn mul(self, rhs: Self) -> Self {
781 Self {
782 x: self.x.mul(rhs.x),
783 y: self.y.mul(rhs.y),
784 z: self.z.mul(rhs.z),
785 w: self.w.mul(rhs.w),
786 }
787 }
788 }
789
790 impl MulAssign<Vec4> for Vec4 {
791 #[inline]
792 fn mul_assign(&mut self, rhs: Self) {
793 self.x.mul_assign(rhs.x);
794 self.y.mul_assign(rhs.y);
795 self.z.mul_assign(rhs.z);
796 self.w.mul_assign(rhs.w);
797 }
798 }
799
800 impl Mul<f32> for Vec4 {
801 type Output = Self;
802 #[inline]
803 fn mul(self, rhs: f32) -> Self {
804 Self {
805 x: self.x.mul(rhs),
806 y: self.y.mul(rhs),
807 z: self.z.mul(rhs),
808 w: self.w.mul(rhs),
809 }
810 }
811 }
812
813 impl MulAssign<f32> for Vec4 {
814 #[inline]
815 fn mul_assign(&mut self, rhs: f32) {
816 self.x.mul_assign(rhs);
817 self.y.mul_assign(rhs);
818 self.z.mul_assign(rhs);
819 self.w.mul_assign(rhs);
820 }
821 }
822
823 impl Mul<Vec4> for f32 {
824 type Output = Vec4;
825 #[inline]
826 fn mul(self, rhs: Vec4) -> Vec4 {
827 Vec4 {
828 x: self.mul(rhs.x),
829 y: self.mul(rhs.y),
830 z: self.mul(rhs.z),
831 w: self.mul(rhs.w),
832 }
833 }
834 }
835
836 impl Add<Vec4> for Vec4 {
837 type Output = Self;
838 #[inline]
839 fn add(self, rhs: Self) -> Self {
840 Self {
841 x: self.x.add(rhs.x),
842 y: self.y.add(rhs.y),
843 z: self.z.add(rhs.z),
844 w: self.w.add(rhs.w),
845 }
846 }
847 }
848
849 impl AddAssign<Vec4> for Vec4 {
850 #[inline]
851 fn add_assign(&mut self, rhs: Self) {
852 self.x.add_assign(rhs.x);
853 self.y.add_assign(rhs.y);
854 self.z.add_assign(rhs.z);
855 self.w.add_assign(rhs.w);
856 }
857 }
858
859 impl Add<f32> for Vec4 {
860 type Output = Self;
861 #[inline]
862 fn add(self, rhs: f32) -> Self {
863 Self {
864 x: self.x.add(rhs),
865 y: self.y.add(rhs),
866 z: self.z.add(rhs),
867 w: self.w.add(rhs),
868 }
869 }
870 }
871
872 impl AddAssign<f32> for Vec4 {
873 #[inline]
874 fn add_assign(&mut self, rhs: f32) {
875 self.x.add_assign(rhs);
876 self.y.add_assign(rhs);
877 self.z.add_assign(rhs);
878 self.w.add_assign(rhs);
879 }
880 }
881
882 impl Add<Vec4> for f32 {
883 type Output = Vec4;
884 #[inline]
885 fn add(self, rhs: Vec4) -> Vec4 {
886 Vec4 {
887 x: self.add(rhs.x),
888 y: self.add(rhs.y),
889 z: self.add(rhs.z),
890 w: self.add(rhs.w),
891 }
892 }
893 }
894
895 impl Sub<Vec4> for Vec4 {
896 type Output = Self;
897 #[inline]
898 fn sub(self, rhs: Self) -> Self {
899 Self {
900 x: self.x.sub(rhs.x),
901 y: self.y.sub(rhs.y),
902 z: self.z.sub(rhs.z),
903 w: self.w.sub(rhs.w),
904 }
905 }
906 }
907
908 impl SubAssign<Vec4> for Vec4 {
909 #[inline]
910 fn sub_assign(&mut self, rhs: Vec4) {
911 self.x.sub_assign(rhs.x);
912 self.y.sub_assign(rhs.y);
913 self.z.sub_assign(rhs.z);
914 self.w.sub_assign(rhs.w);
915 }
916 }
917
918 impl Sub<f32> for Vec4 {
919 type Output = Self;
920 #[inline]
921 fn sub(self, rhs: f32) -> Self {
922 Self {
923 x: self.x.sub(rhs),
924 y: self.y.sub(rhs),
925 z: self.z.sub(rhs),
926 w: self.w.sub(rhs),
927 }
928 }
929 }
930
931 impl SubAssign<f32> for Vec4 {
932 #[inline]
933 fn sub_assign(&mut self, rhs: f32) {
934 self.x.sub_assign(rhs);
935 self.y.sub_assign(rhs);
936 self.z.sub_assign(rhs);
937 self.w.sub_assign(rhs);
938 }
939 }
940
941 impl Sub<Vec4> for f32 {
942 type Output = Vec4;
943 #[inline]
944 fn sub(self, rhs: Vec4) -> Vec4 {
945 Vec4 {
946 x: self.sub(rhs.x),
947 y: self.sub(rhs.y),
948 z: self.sub(rhs.z),
949 w: self.sub(rhs.w),
950 }
951 }
952 }
953
954 impl Rem<Vec4> for Vec4 {
955 type Output = Self;
956 #[inline]
957 fn rem(self, rhs: Self) -> Self {
958 Self {
959 x: self.x.rem(rhs.x),
960 y: self.y.rem(rhs.y),
961 z: self.z.rem(rhs.z),
962 w: self.w.rem(rhs.w),
963 }
964 }
965 }
966
967 impl RemAssign<Vec4> for Vec4 {
968 #[inline]
969 fn rem_assign(&mut self, rhs: Self) {
970 self.x.rem_assign(rhs.x);
971 self.y.rem_assign(rhs.y);
972 self.z.rem_assign(rhs.z);
973 self.w.rem_assign(rhs.w);
974 }
975 }
976
977 impl Rem<f32> for Vec4 {
978 type Output = Self;
979 #[inline]
980 fn rem(self, rhs: f32) -> Self {
981 Self {
982 x: self.x.rem(rhs),
983 y: self.y.rem(rhs),
984 z: self.z.rem(rhs),
985 w: self.w.rem(rhs),
986 }
987 }
988 }
989
990 impl RemAssign<f32> for Vec4 {
991 #[inline]
992 fn rem_assign(&mut self, rhs: f32) {
993 self.x.rem_assign(rhs);
994 self.y.rem_assign(rhs);
995 self.z.rem_assign(rhs);
996 self.w.rem_assign(rhs);
997 }
998 }
999
1000 impl Rem<Vec4> for f32 {
1001 type Output = Vec4;
1002 #[inline]
1003 fn rem(self, rhs: Vec4) -> Vec4 {
1004 Vec4 {
1005 x: self.rem(rhs.x),
1006 y: self.rem(rhs.y),
1007 z: self.rem(rhs.z),
1008 w: self.rem(rhs.w),
1009 }
1010 }
1011 }
1012
1013 #[cfg(not(target_arch = "spirv"))]
1014 impl AsRef<[f32; 4]> for Vec4 {
1015 #[inline]
1016 fn as_ref(&self) -> &[f32; 4] {
1017 unsafe { &*(self as *const Vec4 as *const [f32; 4]) }
1018 }
1019 }
1020
1021 #[cfg(not(target_arch = "spirv"))]
1022 impl AsMut<[f32; 4]> for Vec4 {
1023 #[inline]
1024 fn as_mut(&mut self) -> &mut [f32; 4] {
1025 unsafe { &mut *(self as *mut Vec4 as *mut [f32; 4]) }
1026 }
1027 }
1028
1029 impl Sum for Vec4 {
1030 #[inline]
1031 fn sum<I>(iter: I) -> Self
1032 where
1033 I: Iterator<Item = Self>,
1034 {
1035 iter.fold(Self::ZERO, Self::add)
1036 }
1037 }
1038
1039 impl<'a> Sum<&'a Self> for Vec4 {
1040 #[inline]
1041 fn sum<I>(iter: I) -> Self
1042 where
1043 I: Iterator<Item = &'a Self>,
1044 {
1045 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1046 }
1047 }
1048
1049 impl Product for Vec4 {
1050 #[inline]
1051 fn product<I>(iter: I) -> Self
1052 where
1053 I: Iterator<Item = Self>,
1054 {
1055 iter.fold(Self::ONE, Self::mul)
1056 }
1057 }
1058
1059 impl<'a> Product<&'a Self> for Vec4 {
1060 #[inline]
1061 fn product<I>(iter: I) -> Self
1062 where
1063 I: Iterator<Item = &'a Self>,
1064 {
1065 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1066 }
1067 }
1068
1069 impl Neg for Vec4 {
1070 type Output = Self;
1071 #[inline]
1072 fn neg(self) -> Self {
1073 Self {
1074 x: self.x.neg(),
1075 y: self.y.neg(),
1076 z: self.z.neg(),
1077 w: self.w.neg(),
1078 }
1079 }
1080 }
1081
1082 impl Index<usize> for Vec4 {
1083 type Output = f32;
1084 #[inline]
1085 fn index(&self, index: usize) -> &Self::Output {
1086 match index {
1087 0 => &self.x,
1088 1 => &self.y,
1089 2 => &self.z,
1090 3 => &self.w,
1091 _ => panic!("index out of bounds"),
1092 }
1093 }
1094 }
1095
1096 impl IndexMut<usize> for Vec4 {
1097 #[inline]
1098 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1099 match index {
1100 0 => &mut self.x,
1101 1 => &mut self.y,
1102 2 => &mut self.z,
1103 3 => &mut self.w,
1104 _ => panic!("index out of bounds"),
1105 }
1106 }
1107 }
1108
1109 #[cfg(not(target_arch = "spirv"))]
1110 impl fmt::Display for Vec4 {
1111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1112 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1113 }
1114 }
1115
1116 #[cfg(not(target_arch = "spirv"))]
1117 impl fmt::Debug for Vec4 {
1118 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1119 fmt.debug_tuple(stringify!(Vec4))
1120 .field(&self.x)
1121 .field(&self.y)
1122 .field(&self.z)
1123 .field(&self.w)
1124 .finish()
1125 }
1126 }
1127
1128 impl From<[f32; 4]> for Vec4 {
1129 #[inline]
1130 fn from(a: [f32; 4]) -> Self {
1131 Self::new(a[0], a[1], a[2], a[3])
1132 }
1133 }
1134
1135 impl From<Vec4> for [f32; 4] {
1136 #[inline]
1137 fn from(v: Vec4) -> Self {
1138 [v.x, v.y, v.z, v.w]
1139 }
1140 }
1141
1142 impl From<(f32, f32, f32, f32)> for Vec4 {
1143 #[inline]
1144 fn from(t: (f32, f32, f32, f32)) -> Self {
1145 Self::new(t.0, t.1, t.2, t.3)
1146 }
1147 }
1148
1149 impl From<Vec4> for (f32, f32, f32, f32) {
1150 #[inline]
1151 fn from(v: Vec4) -> Self {
1152 (v.x, v.y, v.z, v.w)
1153 }
1154 }
1155
1156 impl From<(Vec3A, f32)> for Vec4 {
1157 #[inline]
1158 fn from((v, w): (Vec3A, f32)) -> Self {
1159 v.extend(w)
1160 }
1161 }
1162
1163 impl From<(f32, Vec3A)> for Vec4 {
1164 #[inline]
1165 fn from((x, v): (f32, Vec3A)) -> Self {
1166 Self::new(x, v.x, v.y, v.z)
1167 }
1168 }
1169
1170 impl From<(Vec3, f32)> for Vec4 {
1171 #[inline]
1172 fn from((v, w): (Vec3, f32)) -> Self {
1173 Self::new(v.x, v.y, v.z, w)
1174 }
1175 }
1176
1177 impl From<(f32, Vec3)> for Vec4 {
1178 #[inline]
1179 fn from((x, v): (f32, Vec3)) -> Self {
1180 Self::new(x, v.x, v.y, v.z)
1181 }
1182 }
1183
1184 impl From<(Vec2, f32, f32)> for Vec4 {
1185 #[inline]
1186 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1187 Self::new(v.x, v.y, z, w)
1188 }
1189 }
1190
1191 impl From<(Vec2, Vec2)> for Vec4 {
1192 #[inline]
1193 fn from((v, u): (Vec2, Vec2)) -> Self {
1194 Self::new(v.x, v.y, u.x, u.y)
1195 }
1196 }