Debian Codegraph @

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