Debian Codegraph @

debian/missing-sources/glam/src/f32/coresimd/vec4.rs

raw
1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{coresimd::*, f32::math, BVec4, BVec4A, Vec2, Vec3, Vec3A};
4
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::{f32, ops::*};
8
9 use core::simd::{cmp::SimdPartialEq, cmp::SimdPartialOrd, num::SimdFloat, *};
10 use std::simd::StdFloat;
11
12 /// Creates a 4-dimensional vector.
13 #[inline(always)]
14 #[must_use]
15 pub const fn vec4(x: f32, y: f32, z: f32, w: f32) -> Vec4 {
16 Vec4::new(x, y, z, w)
17 }
18
19 /// A 4-dimensional vector.
20 ///
21 /// SIMD vector types are used for storage on supported platforms.
22 ///
23 /// This type is 16 byte aligned.
24 #[derive(Clone, Copy)]
25 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
26 #[repr(transparent)]
27 pub struct Vec4(pub(crate) f32x4);
28
29 impl Vec4 {
30 /// All zeroes.
31 pub const ZERO: Self = Self::splat(0.0);
32
33 /// All ones.
34 pub const ONE: Self = Self::splat(1.0);
35
36 /// All negative ones.
37 pub const NEG_ONE: Self = Self::splat(-1.0);
38
39 /// All `f32::MIN`.
40 pub const MIN: Self = Self::splat(f32::MIN);
41
42 /// All `f32::MAX`.
43 pub const MAX: Self = Self::splat(f32::MAX);
44
45 /// All `f32::NAN`.
46 pub const NAN: Self = Self::splat(f32::NAN);
47
48 /// All `f32::INFINITY`.
49 pub const INFINITY: Self = Self::splat(f32::INFINITY);
50
51 /// All `f32::NEG_INFINITY`.
52 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
53
54 /// A unit vector pointing along the positive X axis.
55 pub const X: Self = Self::new(1.0, 0.0, 0.0, 0.0);
56
57 /// A unit vector pointing along the positive Y axis.
58 pub const Y: Self = Self::new(0.0, 1.0, 0.0, 0.0);
59
60 /// A unit vector pointing along the positive Z axis.
61 pub const Z: Self = Self::new(0.0, 0.0, 1.0, 0.0);
62
63 /// A unit vector pointing along the positive W axis.
64 pub const W: Self = Self::new(0.0, 0.0, 0.0, 1.0);
65
66 /// A unit vector pointing along the negative X axis.
67 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0, 0.0);
68
69 /// A unit vector pointing along the negative Y axis.
70 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0, 0.0);
71
72 /// A unit vector pointing along the negative Z axis.
73 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0, 0.0);
74
75 /// A unit vector pointing along the negative W axis.
76 pub const NEG_W: Self = Self::new(0.0, 0.0, 0.0, -1.0);
77
78 /// The unit axes.
79 pub const AXES: [Self; 4] = [Self::X, Self::Y, Self::Z, Self::W];
80
81 /// Vec4 uses Rust Portable SIMD
82 pub const USES_CORE_SIMD: bool = true;
83 /// Vec4 uses Arm NEON
84 pub const USES_NEON: bool = false;
85 /// Vec4 uses scalar math
86 pub const USES_SCALAR_MATH: bool = false;
87 /// Vec4 uses Intel SSE2
88 pub const USES_SSE2: bool = false;
89 /// Vec4 uses WebAssembly 128-bit SIMD
90 pub const USES_WASM32_SIMD: bool = false;
91
92 /// Creates a new vector.
93 #[inline(always)]
94 #[must_use]
95 pub const fn new(x: f32, y: f32, z: f32, w: f32) -> Self {
96 Self(f32x4::from_array([x, y, z, w]))
97 }
98
99 /// Creates a vector with all elements set to `v`.
100 #[inline]
101 #[must_use]
102 pub const fn splat(v: f32) -> Self {
103 Self(Simd::from_array([v; 4]))
104 }
105
106 /// Returns a vector containing each element of `self` modified by a mapping function `f`.
107 #[inline]
108 #[must_use]
109 pub fn map<F>(self, f: F) -> Self
110 where
111 F: Fn(f32) -> f32,
112 {
113 Self::new(f(self.x), f(self.y), f(self.z), f(self.w))
114 }
115
116 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
117 /// for each element of `self`.
118 ///
119 /// A true element in the mask uses the corresponding element from `if_true`, and false
120 /// uses the element from `if_false`.
121 #[inline]
122 #[must_use]
123 pub fn select(mask: BVec4A, if_true: Self, if_false: Self) -> Self {
124 Self(mask.0.select(if_true.0, if_false.0))
125 }
126
127 /// Creates a new vector from an array.
128 #[inline]
129 #[must_use]
130 pub const fn from_array(a: [f32; 4]) -> Self {
131 Self::new(a[0], a[1], a[2], a[3])
132 }
133
134 /// Converts `self` to `[x, y, z, w]`
135 #[inline]
136 #[must_use]
137 pub const fn to_array(&self) -> [f32; 4] {
138 unsafe { *(self as *const Self as *const [f32; 4]) }
139 }
140
141 /// Creates a vector from the first 4 values in `slice`.
142 ///
143 /// # Panics
144 ///
145 /// Panics if `slice` is less than 4 elements long.
146 #[inline]
147 #[must_use]
148 pub const fn from_slice(slice: &[f32]) -> Self {
149 assert!(slice.len() >= 4);
150 Self::new(slice[0], slice[1], slice[2], slice[3])
151 }
152
153 /// Writes the elements of `self` to the first 4 elements in `slice`.
154 ///
155 /// # Panics
156 ///
157 /// Panics if `slice` is less than 4 elements long.
158 #[inline]
159 pub fn write_to_slice(self, slice: &mut [f32]) {
160 slice[..4].copy_from_slice(&self.to_array());
161 }
162
163 /// Creates a 3D vector from the `x`, `y` and `z` elements of `self`, discarding `w`.
164 ///
165 /// Truncation to [`Vec3`] may also be performed by using [`self.xyz()`][crate::swizzles::Vec4Swizzles::xyz()].
166 ///
167 /// To truncate to [`Vec3A`] use [`Vec3A::from_vec4()`].
168 #[inline]
169 #[must_use]
170 pub fn truncate(self) -> Vec3 {
171 use crate::swizzles::Vec4Swizzles;
172 self.xyz()
173 }
174
175 /// Projects a homogeneous coordinate to 3D space by performing perspective divide.
176 ///
177 /// To project to [`Vec3A`] use [`Vec3A::from_homogeneous()`].
178 ///
179 /// # Panics
180 ///
181 /// Will panic if `self.w` is `0` when `glam_assert` is enabled.
182 #[inline]
183 #[must_use]
184 pub fn project(self) -> Vec3 {
185 Vec3::from_homogeneous(self)
186 }
187
188 /// Creates a 4D vector from `self` with the given value of `x`.
189 #[inline]
190 #[must_use]
191 pub fn with_x(mut self, x: f32) -> Self {
192 self.x = x;
193 self
194 }
195
196 /// Creates a 4D vector from `self` with the given value of `y`.
197 #[inline]
198 #[must_use]
199 pub fn with_y(mut self, y: f32) -> Self {
200 self.y = y;
201 self
202 }
203
204 /// Creates a 4D vector from `self` with the given value of `z`.
205 #[inline]
206 #[must_use]
207 pub fn with_z(mut self, z: f32) -> Self {
208 self.z = z;
209 self
210 }
211
212 /// Creates a 4D vector from `self` with the given value of `w`.
213 #[inline]
214 #[must_use]
215 pub fn with_w(mut self, w: f32) -> Self {
216 self.w = w;
217 self
218 }
219
220 /// Computes the dot product of `self` and `rhs`.
221 #[inline]
222 #[must_use]
223 pub fn dot(self, rhs: Self) -> f32 {
224 dot4(self.0, rhs.0)
225 }
226
227 /// Returns a vector where every component is the dot product of `self` and `rhs`.
228 #[inline]
229 #[must_use]
230 pub fn dot_into_vec(self, rhs: Self) -> Self {
231 Self(dot4_into_f32x4(self.0, rhs.0))
232 }
233
234 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
235 ///
236 /// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`.
237 ///
238 /// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on
239 /// different SIMD architectures.
240 #[inline]
241 #[must_use]
242 pub fn min(self, rhs: Self) -> Self {
243 Self(self.0.simd_lt(rhs.0).select(self.0, rhs.0))
244 }
245
246 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
247 ///
248 /// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`.
249 ///
250 /// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on
251 /// different SIMD architectures.
252 #[inline]
253 #[must_use]
254 pub fn max(self, rhs: Self) -> Self {
255 Self(self.0.simd_gt(rhs.0).select(self.0, rhs.0))
256 }
257
258 /// Component-wise clamping of values, similar to [`f32::clamp`].
259 ///
260 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
261 ///
262 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
263 /// different SIMD architectures.
264 ///
265 /// # Panics
266 ///
267 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
268 #[inline]
269 #[must_use]
270 pub fn clamp(self, min: Self, max: Self) -> Self {
271 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
272 self.max(min).min(max)
273 }
274
275 /// Returns the horizontal minimum of `self`.
276 ///
277 /// In other words this computes `min(x, y, ..)`.
278 ///
279 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
280 /// different SIMD architectures.
281 #[inline]
282 #[must_use]
283 pub fn min_element(self) -> f32 {
284 let min = |a: f32x4, b: f32x4| a.simd_lt(b).select(a, b);
285 let v = self.0;
286 let v = min(v, simd_swizzle!(v, [2, 3, 0, 0]));
287 let v = min(v, simd_swizzle!(v, [1, 0, 0, 0]));
288 v[0]
289 }
290
291 /// Returns the horizontal maximum of `self`.
292 ///
293 /// In other words this computes `max(x, y, ..)`.
294 ///
295 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
296 /// different SIMD architectures.
297 #[inline]
298 #[must_use]
299 pub fn max_element(self) -> f32 {
300 let max = |a: f32x4, b: f32x4| a.simd_gt(b).select(a, b);
301 let v = self.0;
302 let v = max(v, simd_swizzle!(v, [2, 3, 0, 0]));
303 let v = max(v, simd_swizzle!(v, [1, 0, 0, 0]));
304 v[0]
305 }
306
307 /// Returns the index of the first minimum element of `self`.
308 #[doc(alias = "argmin")]
309 #[inline]
310 #[must_use]
311 pub fn min_position(self) -> usize {
312 let mut min = self.x;
313 let mut index = 0;
314 if self.y < min {
315 min = self.y;
316 index = 1;
317 }
318 if self.z < min {
319 min = self.z;
320 index = 2;
321 }
322 if self.w < min {
323 index = 3;
324 }
325 index
326 }
327
328 /// Returns the index of the first maximum element of `self`.
329 #[doc(alias = "argmax")]
330 #[inline]
331 #[must_use]
332 pub fn max_position(self) -> usize {
333 let mut max = self.x;
334 let mut index = 0;
335 if self.y > max {
336 max = self.y;
337 index = 1;
338 }
339 if self.z > max {
340 max = self.z;
341 index = 2;
342 }
343 if self.w > max {
344 index = 3;
345 }
346 index
347 }
348
349 /// Returns the sum of all elements of `self`.
350 ///
351 /// In other words, this computes `self.x + self.y + ..`.
352 #[inline]
353 #[must_use]
354 pub fn element_sum(self) -> f32 {
355 self.0.reduce_sum()
356 }
357
358 /// Returns the product of all elements of `self`.
359 ///
360 /// In other words, this computes `self.x * self.y * ..`.
361 #[inline]
362 #[must_use]
363 pub fn element_product(self) -> f32 {
364 self.0.reduce_product()
365 }
366
367 /// Returns a vector mask containing the result of a `==` comparison for each element of
368 /// `self` and `rhs`.
369 ///
370 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
371 /// elements.
372 #[inline]
373 #[must_use]
374 pub fn cmpeq(self, rhs: Self) -> BVec4A {
375 BVec4A(f32x4::simd_eq(self.0, rhs.0))
376 }
377
378 /// Returns a vector mask containing the result of a `!=` comparison for each element of
379 /// `self` and `rhs`.
380 ///
381 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
382 /// elements.
383 #[inline]
384 #[must_use]
385 pub fn cmpne(self, rhs: Self) -> BVec4A {
386 BVec4A(f32x4::simd_ne(self.0, rhs.0))
387 }
388
389 /// Returns a vector mask containing the result of a `>=` comparison for each element of
390 /// `self` and `rhs`.
391 ///
392 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
393 /// elements.
394 #[inline]
395 #[must_use]
396 pub fn cmpge(self, rhs: Self) -> BVec4A {
397 BVec4A(f32x4::simd_ge(self.0, rhs.0))
398 }
399
400 /// Returns a vector mask containing the result of a `>` comparison for each element of
401 /// `self` and `rhs`.
402 ///
403 /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
404 /// elements.
405 #[inline]
406 #[must_use]
407 pub fn cmpgt(self, rhs: Self) -> BVec4A {
408 BVec4A(f32x4::simd_gt(self.0, rhs.0))
409 }
410
411 /// Returns a vector mask containing the result of a `<=` comparison for each element of
412 /// `self` and `rhs`.
413 ///
414 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
415 /// elements.
416 #[inline]
417 #[must_use]
418 pub fn cmple(self, rhs: Self) -> BVec4A {
419 BVec4A(f32x4::simd_le(self.0, rhs.0))
420 }
421
422 /// Returns a vector mask containing the result of a `<` comparison for each element of
423 /// `self` and `rhs`.
424 ///
425 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
426 /// elements.
427 #[inline]
428 #[must_use]
429 pub fn cmplt(self, rhs: Self) -> BVec4A {
430 BVec4A(f32x4::simd_lt(self.0, rhs.0))
431 }
432
433 /// Returns a vector containing the absolute value of each element of `self`.
434 #[inline]
435 #[must_use]
436 pub fn abs(self) -> Self {
437 Self(self.0.abs())
438 }
439
440 /// Returns a vector with elements representing the sign of `self`.
441 ///
442 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
443 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
444 /// - `NAN` if the number is `NAN`
445 #[inline]
446 #[must_use]
447 pub fn signum(self) -> Self {
448 Self(self.0.signum())
449 }
450
451 /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
452 #[inline]
453 #[must_use]
454 pub fn copysign(self, rhs: Self) -> Self {
455 Self(self.0.copysign(rhs.0))
456 }
457
458 /// Returns a bitmask with the lowest 4 bits set to the sign bits from the elements of `self`.
459 ///
460 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
461 /// into the first lowest bit, element `y` into the second, etc.
462 ///
463 /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
464 /// bit and negative infinity.
465 #[inline]
466 #[must_use]
467 pub fn is_negative_bitmask(self) -> u32 {
468 self.0.is_sign_negative().to_bitmask() as u32
469 }
470
471 /// Returns `true` if, and only if, all elements are finite. If any element is either
472 /// `NaN`, positive or negative infinity, this will return `false`.
473 #[inline]
474 #[must_use]
475 pub fn is_finite(self) -> bool {
476 self.is_finite_mask().all()
477 }
478
479 /// Performs `is_finite` on each element of self, returning a vector mask of the results.
480 ///
481 /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
482 #[inline]
483 #[must_use]
484 pub fn is_finite_mask(self) -> BVec4A {
485 BVec4A(f32x4::is_finite(self.0))
486 }
487
488 /// Returns `true` if any elements are `NaN`.
489 #[inline]
490 #[must_use]
491 pub fn is_nan(self) -> bool {
492 self.is_nan_mask().any()
493 }
494
495 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
496 ///
497 /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
498 #[inline]
499 #[must_use]
500 pub fn is_nan_mask(self) -> BVec4A {
501 BVec4A(f32x4::is_nan(self.0))
502 }
503
504 /// Computes the length of `self`.
505 #[doc(alias = "magnitude")]
506 #[inline]
507 #[must_use]
508 pub fn length(self) -> f32 {
509 let dot = dot4_in_x(self.0, self.0);
510 dot.sqrt()[0]
511 }
512
513 /// Computes the squared length of `self`.
514 ///
515 /// This is faster than `length()` as it avoids a square root operation.
516 #[doc(alias = "magnitude2")]
517 #[inline]
518 #[must_use]
519 pub fn length_squared(self) -> f32 {
520 self.dot(self)
521 }
522
523 /// Computes `1.0 / length()`.
524 ///
525 /// For valid results, `self` must _not_ be of length zero.
526 #[inline]
527 #[must_use]
528 pub fn length_recip(self) -> f32 {
529 let dot = dot4_in_x(self.0, self.0);
530 dot.sqrt().recip()[0]
531 }
532
533 /// Computes the Euclidean distance between two points in space.
534 #[inline]
535 #[must_use]
536 pub fn distance(self, rhs: Self) -> f32 {
537 (self - rhs).length()
538 }
539
540 /// Compute the squared euclidean distance between two points in space.
541 #[inline]
542 #[must_use]
543 pub fn distance_squared(self, rhs: Self) -> f32 {
544 (self - rhs).length_squared()
545 }
546
547 /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
548 #[inline]
549 #[must_use]
550 pub fn div_euclid(self, rhs: Self) -> Self {
551 Self::new(
552 math::div_euclid(self.x, rhs.x),
553 math::div_euclid(self.y, rhs.y),
554 math::div_euclid(self.z, rhs.z),
555 math::div_euclid(self.w, rhs.w),
556 )
557 }
558
559 /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
560 ///
561 /// [Euclidean division]: f32::rem_euclid
562 #[inline]
563 #[must_use]
564 pub fn rem_euclid(self, rhs: Self) -> Self {
565 Self::new(
566 math::rem_euclid(self.x, rhs.x),
567 math::rem_euclid(self.y, rhs.y),
568 math::rem_euclid(self.z, rhs.z),
569 math::rem_euclid(self.w, rhs.w),
570 )
571 }
572
573 /// Returns `self` normalized to length 1.0.
574 ///
575 /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
576 ///
577 /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
578 ///
579 /// # Panics
580 ///
581 /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
582 #[inline]
583 #[must_use]
584 pub fn normalize(self) -> Self {
585 let length = dot4_into_f32x4(self.0, self.0).sqrt();
586 #[allow(clippy::let_and_return)]
587 let normalized = Self(self.0 / length);
588 glam_assert!(normalized.is_finite());
589 normalized
590 }
591
592 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
593 ///
594 /// In particular, if the input is zero (or very close to zero), or non-finite,
595 /// the result of this operation will be `None`.
596 ///
597 /// See also [`Self::normalize_or_zero()`].
598 #[inline]
599 #[must_use]
600 pub fn try_normalize(self) -> Option<Self> {
601 let rcp = self.length_recip();
602 if rcp.is_finite() && rcp > 0.0 {
603 Some(self * rcp)
604 } else {
605 None
606 }
607 }
608
609 /// Returns `self` normalized to length 1.0 if possible, else returns a
610 /// fallback value.
611 ///
612 /// In particular, if the input is zero (or very close to zero), or non-finite,
613 /// the result of this operation will be the fallback value.
614 ///
615 /// See also [`Self::try_normalize()`].
616 #[inline]
617 #[must_use]
618 pub fn normalize_or(self, fallback: Self) -> Self {
619 let rcp = self.length_recip();
620 if rcp.is_finite() && rcp > 0.0 {
621 self * rcp
622 } else {
623 fallback
624 }
625 }
626
627 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
628 ///
629 /// In particular, if the input is zero (or very close to zero), or non-finite,
630 /// the result of this operation will be zero.
631 ///
632 /// See also [`Self::try_normalize()`].
633 #[inline]
634 #[must_use]
635 pub fn normalize_or_zero(self) -> Self {
636 self.normalize_or(Self::ZERO)
637 }
638
639 /// Returns `self` normalized to length 1.0 and the length of `self`.
640 ///
641 /// If `self` is zero length then `(Self::X, 0.0)` is returned.
642 #[inline]
643 #[must_use]
644 pub fn normalize_and_length(self) -> (Self, f32) {
645 let length = self.length();
646 let rcp = 1.0 / length;
647 if rcp.is_finite() && rcp > 0.0 {
648 (self * rcp, length)
649 } else {
650 (Self::X, 0.0)
651 }
652 }
653
654 /// Returns whether `self` is length `1.0` or not.
655 ///
656 /// Uses a precision threshold of approximately `1e-4`.
657 #[inline]
658 #[must_use]
659 pub fn is_normalized(self) -> bool {
660 math::abs(self.length_squared() - 1.0) <= 2e-4
661 }
662
663 /// Returns the vector projection of `self` onto `rhs`.
664 ///
665 /// `rhs` must be of non-zero length.
666 ///
667 /// # Panics
668 ///
669 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
670 #[inline]
671 #[must_use]
672 pub fn project_onto(self, rhs: Self) -> Self {
673 let other_len_sq_rcp = rhs.dot(rhs).recip();
674 glam_assert!(other_len_sq_rcp.is_finite());
675 rhs * self.dot(rhs) * other_len_sq_rcp
676 }
677
678 /// Returns the vector rejection of `self` from `rhs`.
679 ///
680 /// The vector rejection is the vector perpendicular to the projection of `self` onto
681 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
682 ///
683 /// `rhs` must be of non-zero length.
684 ///
685 /// # Panics
686 ///
687 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
688 #[doc(alias("plane"))]
689 #[inline]
690 #[must_use]
691 pub fn reject_from(self, rhs: Self) -> Self {
692 self - self.project_onto(rhs)
693 }
694
695 /// Returns the vector projection of `self` onto `rhs`.
696 ///
697 /// `rhs` must be normalized.
698 ///
699 /// # Panics
700 ///
701 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
702 #[inline]
703 #[must_use]
704 pub fn project_onto_normalized(self, rhs: Self) -> Self {
705 glam_assert!(rhs.is_normalized());
706 rhs * self.dot(rhs)
707 }
708
709 /// Returns the vector rejection of `self` from `rhs`.
710 ///
711 /// The vector rejection is the vector perpendicular to the projection of `self` onto
712 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
713 ///
714 /// `rhs` must be normalized.
715 ///
716 /// # Panics
717 ///
718 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
719 #[doc(alias("plane"))]
720 #[inline]
721 #[must_use]
722 pub fn reject_from_normalized(self, rhs: Self) -> Self {
723 self - self.project_onto_normalized(rhs)
724 }
725
726 /// Returns a vector containing the nearest integer to a number for each element of `self`.
727 /// Round half-way cases away from 0.0.
728 #[inline]
729 #[must_use]
730 pub fn round(self) -> Self {
731 Self(self.0.round())
732 }
733
734 /// Returns a vector containing the largest integer less than or equal to a number for each
735 /// element of `self`.
736 #[inline]
737 #[must_use]
738 pub fn floor(self) -> Self {
739 Self(self.0.floor())
740 }
741
742 /// Returns a vector containing the smallest integer greater than or equal to a number for
743 /// each element of `self`.
744 #[inline]
745 #[must_use]
746 pub fn ceil(self) -> Self {
747 Self(self.0.ceil())
748 }
749
750 /// Returns a vector containing the integer part each element of `self`. This means numbers are
751 /// always truncated towards zero.
752 #[inline]
753 #[must_use]
754 pub fn trunc(self) -> Self {
755 Self(self.0.trunc())
756 }
757
758 /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
759 ///
760 /// Note that this differs from the GLSL implementation of `fract` which returns
761 /// `self - self.floor()`.
762 ///
763 /// Note that this is fast but not precise for large numbers.
764 #[inline]
765 #[must_use]
766 pub fn fract(self) -> Self {
767 self - self.trunc()
768 }
769
770 /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
771 ///
772 /// Note that this differs from the Rust implementation of `fract` which returns
773 /// `self - self.trunc()`.
774 ///
775 /// Note that this is fast but not precise for large numbers.
776 #[inline]
777 #[must_use]
778 pub fn fract_gl(self) -> Self {
779 self - self.floor()
780 }
781
782 /// Returns a vector containing `e^self` (the exponential function) for each element of
783 /// `self`.
784 #[inline]
785 #[must_use]
786 pub fn exp(self) -> Self {
787 Self::new(
788 math::exp(self.x),
789 math::exp(self.y),
790 math::exp(self.z),
791 math::exp(self.w),
792 )
793 }
794
795 /// Returns a vector containing `2^self` for each element of `self`.
796 #[inline]
797 #[must_use]
798 pub fn exp2(self) -> Self {
799 Self::new(
800 math::exp2(self.x),
801 math::exp2(self.y),
802 math::exp2(self.z),
803 math::exp2(self.w),
804 )
805 }
806
807 /// Returns a vector containing the natural logarithm for each element of `self`.
808 /// This returns NaN when the element is negative and negative infinity when the element is zero.
809 #[inline]
810 #[must_use]
811 pub fn ln(self) -> Self {
812 Self::new(
813 math::ln(self.x),
814 math::ln(self.y),
815 math::ln(self.z),
816 math::ln(self.w),
817 )
818 }
819
820 /// Returns a vector containing the base 2 logarithm for each element of `self`.
821 /// This returns NaN when the element is negative and negative infinity when the element is zero.
822 #[inline]
823 #[must_use]
824 pub fn log2(self) -> Self {
825 Self::new(
826 math::log2(self.x),
827 math::log2(self.y),
828 math::log2(self.z),
829 math::log2(self.w),
830 )
831 }
832
833 /// Returns a vector containing each element of `self` raised to the power of `n`.
834 #[inline]
835 #[must_use]
836 pub fn powf(self, n: f32) -> Self {
837 Self::new(
838 math::powf(self.x, n),
839 math::powf(self.y, n),
840 math::powf(self.z, n),
841 math::powf(self.w, n),
842 )
843 }
844
845 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
846 #[inline]
847 #[must_use]
848 pub fn recip(self) -> Self {
849 Self(self.0.recip())
850 }
851
852 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
853 ///
854 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
855 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
856 /// extrapolated.
857 #[doc(alias = "mix")]
858 #[inline]
859 #[must_use]
860 pub fn lerp(self, rhs: Self, s: f32) -> Self {
861 self * (1.0 - s) + rhs * s
862 }
863
864 /// Moves towards `rhs` based on the value `d`.
865 ///
866 /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
867 /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
868 #[inline]
869 #[must_use]
870 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
871 let a = rhs - *self;
872 let len = a.length();
873 if len <= d || len <= 1e-4 {
874 return rhs;
875 }
876 *self + a / len * d
877 }
878
879 /// Calculates the midpoint between `self` and `rhs`.
880 ///
881 /// The midpoint is the average of, or halfway point between, two vectors.
882 /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
883 /// while being slightly cheaper to compute.
884 #[inline]
885 pub fn midpoint(self, rhs: Self) -> Self {
886 (self + rhs) * 0.5
887 }
888
889 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
890 /// less than or equal to `max_abs_diff`.
891 ///
892 /// This can be used to compare if two vectors contain similar elements. It works best when
893 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
894 /// the values being compared against.
895 ///
896 /// For more see
897 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
898 #[inline]
899 #[must_use]
900 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
901 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
902 }
903
904 /// Returns a vector with a length no less than `min` and no more than `max`.
905 ///
906 /// # Panics
907 ///
908 /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
909 #[inline]
910 #[must_use]
911 pub fn clamp_length(self, min: f32, max: f32) -> Self {
912 glam_assert!(0.0 <= min);
913 glam_assert!(min <= max);
914 let length_sq = self.length_squared();
915 if length_sq < min * min {
916 min * (self / math::sqrt(length_sq))
917 } else if length_sq > max * max {
918 max * (self / math::sqrt(length_sq))
919 } else {
920 self
921 }
922 }
923
924 /// Returns a vector with a length no more than `max`.
925 ///
926 /// # Panics
927 ///
928 /// Will panic if `max` is negative when `glam_assert` is enabled.
929 #[inline]
930 #[must_use]
931 pub fn clamp_length_max(self, max: f32) -> Self {
932 glam_assert!(0.0 <= max);
933 let length_sq = self.length_squared();
934 if length_sq > max * max {
935 max * (self / math::sqrt(length_sq))
936 } else {
937 self
938 }
939 }
940
941 /// Returns a vector with a length no less than `min`.
942 ///
943 /// # Panics
944 ///
945 /// Will panic if `min` is negative when `glam_assert` is enabled.
946 #[inline]
947 #[must_use]
948 pub fn clamp_length_min(self, min: f32) -> Self {
949 glam_assert!(0.0 <= min);
950 let length_sq = self.length_squared();
951 if length_sq < min * min {
952 min * (self / math::sqrt(length_sq))
953 } else {
954 self
955 }
956 }
957
958 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
959 /// error, yielding a more accurate result than an unfused multiply-add.
960 ///
961 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
962 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
963 /// and will be heavily dependant on designing algorithms with specific target hardware in
964 /// mind.
965 #[inline]
966 #[must_use]
967 pub fn mul_add(self, a: Self, b: Self) -> Self {
968 Self(self.0.mul_add(a.0, b.0))
969 }
970
971 /// Returns the reflection vector for a given incident vector `self` and surface normal
972 /// `normal`.
973 ///
974 /// `normal` must be normalized.
975 ///
976 /// # Panics
977 ///
978 /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
979 #[inline]
980 #[must_use]
981 pub fn reflect(self, normal: Self) -> Self {
982 glam_assert!(normal.is_normalized());
983 self - 2.0 * self.dot(normal) * normal
984 }
985
986 /// Returns the refraction direction for a given incident vector `self`, surface normal
987 /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
988 /// a zero vector will be returned.
989 ///
990 /// `self` and `normal` must be normalized.
991 ///
992 /// # Panics
993 ///
994 /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
995 #[inline]
996 #[must_use]
997 pub fn refract(self, normal: Self, eta: f32) -> Self {
998 glam_assert!(self.is_normalized());
999 glam_assert!(normal.is_normalized());
1000 let n_dot_i = normal.dot(self);
1001 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1002 if k >= 0.0 {
1003 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1004 } else {
1005 Self::ZERO
1006 }
1007 }
1008
1009 /// Casts all elements of `self` to `f64`.
1010 #[inline]
1011 #[must_use]
1012 pub fn as_dvec4(&self) -> crate::DVec4 {
1013 crate::DVec4::new(self.x as f64, self.y as f64, self.z as f64, self.w as f64)
1014 }
1015
1016 /// Casts all elements of `self` to `i8`.
1017 #[inline]
1018 #[must_use]
1019 pub fn as_i8vec4(&self) -> crate::I8Vec4 {
1020 crate::I8Vec4::new(self.x as i8, self.y as i8, self.z as i8, self.w as i8)
1021 }
1022
1023 /// Casts all elements of `self` to `u8`.
1024 #[inline]
1025 #[must_use]
1026 pub fn as_u8vec4(&self) -> crate::U8Vec4 {
1027 crate::U8Vec4::new(self.x as u8, self.y as u8, self.z as u8, self.w as u8)
1028 }
1029
1030 /// Casts all elements of `self` to `i16`.
1031 #[inline]
1032 #[must_use]
1033 pub fn as_i16vec4(&self) -> crate::I16Vec4 {
1034 crate::I16Vec4::new(self.x as i16, self.y as i16, self.z as i16, self.w as i16)
1035 }
1036
1037 /// Casts all elements of `self` to `u16`.
1038 #[inline]
1039 #[must_use]
1040 pub fn as_u16vec4(&self) -> crate::U16Vec4 {
1041 crate::U16Vec4::new(self.x as u16, self.y as u16, self.z as u16, self.w as u16)
1042 }
1043
1044 /// Casts all elements of `self` to `i32`.
1045 #[inline]
1046 #[must_use]
1047 pub fn as_ivec4(&self) -> crate::IVec4 {
1048 crate::IVec4::new(self.x as i32, self.y as i32, self.z as i32, self.w as i32)
1049 }
1050
1051 /// Casts all elements of `self` to `u32`.
1052 #[inline]
1053 #[must_use]
1054 pub fn as_uvec4(&self) -> crate::UVec4 {
1055 crate::UVec4::new(self.x as u32, self.y as u32, self.z as u32, self.w as u32)
1056 }
1057
1058 /// Casts all elements of `self` to `i64`.
1059 #[inline]
1060 #[must_use]
1061 pub fn as_i64vec4(&self) -> crate::I64Vec4 {
1062 crate::I64Vec4::new(self.x as i64, self.y as i64, self.z as i64, self.w as i64)
1063 }
1064
1065 /// Casts all elements of `self` to `u64`.
1066 #[inline]
1067 #[must_use]
1068 pub fn as_u64vec4(&self) -> crate::U64Vec4 {
1069 crate::U64Vec4::new(self.x as u64, self.y as u64, self.z as u64, self.w as u64)
1070 }
1071
1072 /// Casts all elements of `self` to `usize`.
1073 #[inline]
1074 #[must_use]
1075 pub fn as_usizevec4(&self) -> crate::USizeVec4 {
1076 crate::USizeVec4::new(
1077 self.x as usize,
1078 self.y as usize,
1079 self.z as usize,
1080 self.w as usize,
1081 )
1082 }
1083 }
1084
1085 impl Default for Vec4 {
1086 #[inline(always)]
1087 fn default() -> Self {
1088 Self::ZERO
1089 }
1090 }
1091
1092 impl PartialEq for Vec4 {
1093 #[inline]
1094 fn eq(&self, rhs: &Self) -> bool {
1095 self.cmpeq(*rhs).all()
1096 }
1097 }
1098
1099 impl Div for Vec4 {
1100 type Output = Self;
1101 #[inline]
1102 fn div(self, rhs: Self) -> Self {
1103 Self(self.0 / rhs.0)
1104 }
1105 }
1106
1107 impl Div<&Self> for Vec4 {
1108 type Output = Self;
1109 #[inline]
1110 fn div(self, rhs: &Self) -> Self {
1111 self.div(*rhs)
1112 }
1113 }
1114
1115 impl Div<&Vec4> for &Vec4 {
1116 type Output = Vec4;
1117 #[inline]
1118 fn div(self, rhs: &Vec4) -> Vec4 {
1119 (*self).div(*rhs)
1120 }
1121 }
1122
1123 impl Div<Vec4> for &Vec4 {
1124 type Output = Vec4;
1125 #[inline]
1126 fn div(self, rhs: Vec4) -> Vec4 {
1127 (*self).div(rhs)
1128 }
1129 }
1130
1131 impl DivAssign for Vec4 {
1132 #[inline]
1133 fn div_assign(&mut self, rhs: Self) {
1134 self.0 /= rhs.0;
1135 }
1136 }
1137
1138 impl DivAssign<&Self> for Vec4 {
1139 #[inline]
1140 fn div_assign(&mut self, rhs: &Self) {
1141 self.div_assign(*rhs);
1142 }
1143 }
1144
1145 impl Div<f32> for Vec4 {
1146 type Output = Self;
1147 #[inline]
1148 fn div(self, rhs: f32) -> Self {
1149 Self(self.0 / f32x4::splat(rhs))
1150 }
1151 }
1152
1153 impl Div<&f32> for Vec4 {
1154 type Output = Self;
1155 #[inline]
1156 fn div(self, rhs: &f32) -> Self {
1157 self.div(*rhs)
1158 }
1159 }
1160
1161 impl Div<&f32> for &Vec4 {
1162 type Output = Vec4;
1163 #[inline]
1164 fn div(self, rhs: &f32) -> Vec4 {
1165 (*self).div(*rhs)
1166 }
1167 }
1168
1169 impl Div<f32> for &Vec4 {
1170 type Output = Vec4;
1171 #[inline]
1172 fn div(self, rhs: f32) -> Vec4 {
1173 (*self).div(rhs)
1174 }
1175 }
1176
1177 impl DivAssign<f32> for Vec4 {
1178 #[inline]
1179 fn div_assign(&mut self, rhs: f32) {
1180 self.0 /= f32x4::splat(rhs);
1181 }
1182 }
1183
1184 impl DivAssign<&f32> for Vec4 {
1185 #[inline]
1186 fn div_assign(&mut self, rhs: &f32) {
1187 self.div_assign(*rhs);
1188 }
1189 }
1190
1191 impl Div<Vec4> for f32 {
1192 type Output = Vec4;
1193 #[inline]
1194 fn div(self, rhs: Vec4) -> Vec4 {
1195 Vec4(f32x4::splat(self) / rhs.0)
1196 }
1197 }
1198
1199 impl Div<&Vec4> for f32 {
1200 type Output = Vec4;
1201 #[inline]
1202 fn div(self, rhs: &Vec4) -> Vec4 {
1203 self.div(*rhs)
1204 }
1205 }
1206
1207 impl Div<&Vec4> for &f32 {
1208 type Output = Vec4;
1209 #[inline]
1210 fn div(self, rhs: &Vec4) -> Vec4 {
1211 (*self).div(*rhs)
1212 }
1213 }
1214
1215 impl Div<Vec4> for &f32 {
1216 type Output = Vec4;
1217 #[inline]
1218 fn div(self, rhs: Vec4) -> Vec4 {
1219 (*self).div(rhs)
1220 }
1221 }
1222
1223 impl Mul for Vec4 {
1224 type Output = Self;
1225 #[inline]
1226 fn mul(self, rhs: Self) -> Self {
1227 Self(self.0 * rhs.0)
1228 }
1229 }
1230
1231 impl Mul<&Self> for Vec4 {
1232 type Output = Self;
1233 #[inline]
1234 fn mul(self, rhs: &Self) -> Self {
1235 self.mul(*rhs)
1236 }
1237 }
1238
1239 impl Mul<&Vec4> for &Vec4 {
1240 type Output = Vec4;
1241 #[inline]
1242 fn mul(self, rhs: &Vec4) -> Vec4 {
1243 (*self).mul(*rhs)
1244 }
1245 }
1246
1247 impl Mul<Vec4> for &Vec4 {
1248 type Output = Vec4;
1249 #[inline]
1250 fn mul(self, rhs: Vec4) -> Vec4 {
1251 (*self).mul(rhs)
1252 }
1253 }
1254
1255 impl MulAssign for Vec4 {
1256 #[inline]
1257 fn mul_assign(&mut self, rhs: Self) {
1258 self.0 *= rhs.0;
1259 }
1260 }
1261
1262 impl MulAssign<&Self> for Vec4 {
1263 #[inline]
1264 fn mul_assign(&mut self, rhs: &Self) {
1265 self.mul_assign(*rhs);
1266 }
1267 }
1268
1269 impl Mul<f32> for Vec4 {
1270 type Output = Self;
1271 #[inline]
1272 fn mul(self, rhs: f32) -> Self {
1273 Self(self.0 * f32x4::splat(rhs))
1274 }
1275 }
1276
1277 impl Mul<&f32> for Vec4 {
1278 type Output = Self;
1279 #[inline]
1280 fn mul(self, rhs: &f32) -> Self {
1281 self.mul(*rhs)
1282 }
1283 }
1284
1285 impl Mul<&f32> for &Vec4 {
1286 type Output = Vec4;
1287 #[inline]
1288 fn mul(self, rhs: &f32) -> Vec4 {
1289 (*self).mul(*rhs)
1290 }
1291 }
1292
1293 impl Mul<f32> for &Vec4 {
1294 type Output = Vec4;
1295 #[inline]
1296 fn mul(self, rhs: f32) -> Vec4 {
1297 (*self).mul(rhs)
1298 }
1299 }
1300
1301 impl MulAssign<f32> for Vec4 {
1302 #[inline]
1303 fn mul_assign(&mut self, rhs: f32) {
1304 self.0 *= f32x4::splat(rhs);
1305 }
1306 }
1307
1308 impl MulAssign<&f32> for Vec4 {
1309 #[inline]
1310 fn mul_assign(&mut self, rhs: &f32) {
1311 self.mul_assign(*rhs);
1312 }
1313 }
1314
1315 impl Mul<Vec4> for f32 {
1316 type Output = Vec4;
1317 #[inline]
1318 fn mul(self, rhs: Vec4) -> Vec4 {
1319 Vec4(f32x4::splat(self) * rhs.0)
1320 }
1321 }
1322
1323 impl Mul<&Vec4> for f32 {
1324 type Output = Vec4;
1325 #[inline]
1326 fn mul(self, rhs: &Vec4) -> Vec4 {
1327 self.mul(*rhs)
1328 }
1329 }
1330
1331 impl Mul<&Vec4> for &f32 {
1332 type Output = Vec4;
1333 #[inline]
1334 fn mul(self, rhs: &Vec4) -> Vec4 {
1335 (*self).mul(*rhs)
1336 }
1337 }
1338
1339 impl Mul<Vec4> for &f32 {
1340 type Output = Vec4;
1341 #[inline]
1342 fn mul(self, rhs: Vec4) -> Vec4 {
1343 (*self).mul(rhs)
1344 }
1345 }
1346
1347 impl Add for Vec4 {
1348 type Output = Self;
1349 #[inline]
1350 fn add(self, rhs: Self) -> Self {
1351 Self(self.0 + rhs.0)
1352 }
1353 }
1354
1355 impl Add<&Self> for Vec4 {
1356 type Output = Self;
1357 #[inline]
1358 fn add(self, rhs: &Self) -> Self {
1359 self.add(*rhs)
1360 }
1361 }
1362
1363 impl Add<&Vec4> for &Vec4 {
1364 type Output = Vec4;
1365 #[inline]
1366 fn add(self, rhs: &Vec4) -> Vec4 {
1367 (*self).add(*rhs)
1368 }
1369 }
1370
1371 impl Add<Vec4> for &Vec4 {
1372 type Output = Vec4;
1373 #[inline]
1374 fn add(self, rhs: Vec4) -> Vec4 {
1375 (*self).add(rhs)
1376 }
1377 }
1378
1379 impl AddAssign for Vec4 {
1380 #[inline]
1381 fn add_assign(&mut self, rhs: Self) {
1382 self.0 += rhs.0;
1383 }
1384 }
1385
1386 impl AddAssign<&Self> for Vec4 {
1387 #[inline]
1388 fn add_assign(&mut self, rhs: &Self) {
1389 self.add_assign(*rhs);
1390 }
1391 }
1392
1393 impl Add<f32> for Vec4 {
1394 type Output = Self;
1395 #[inline]
1396 fn add(self, rhs: f32) -> Self {
1397 Self(self.0 + f32x4::splat(rhs))
1398 }
1399 }
1400
1401 impl Add<&f32> for Vec4 {
1402 type Output = Self;
1403 #[inline]
1404 fn add(self, rhs: &f32) -> Self {
1405 self.add(*rhs)
1406 }
1407 }
1408
1409 impl Add<&f32> for &Vec4 {
1410 type Output = Vec4;
1411 #[inline]
1412 fn add(self, rhs: &f32) -> Vec4 {
1413 (*self).add(*rhs)
1414 }
1415 }
1416
1417 impl Add<f32> for &Vec4 {
1418 type Output = Vec4;
1419 #[inline]
1420 fn add(self, rhs: f32) -> Vec4 {
1421 (*self).add(rhs)
1422 }
1423 }
1424
1425 impl AddAssign<f32> for Vec4 {
1426 #[inline]
1427 fn add_assign(&mut self, rhs: f32) {
1428 self.0 += f32x4::splat(rhs);
1429 }
1430 }
1431
1432 impl AddAssign<&f32> for Vec4 {
1433 #[inline]
1434 fn add_assign(&mut self, rhs: &f32) {
1435 self.add_assign(*rhs);
1436 }
1437 }
1438
1439 impl Add<Vec4> for f32 {
1440 type Output = Vec4;
1441 #[inline]
1442 fn add(self, rhs: Vec4) -> Vec4 {
1443 Vec4(f32x4::splat(self) + rhs.0)
1444 }
1445 }
1446
1447 impl Add<&Vec4> for f32 {
1448 type Output = Vec4;
1449 #[inline]
1450 fn add(self, rhs: &Vec4) -> Vec4 {
1451 self.add(*rhs)
1452 }
1453 }
1454
1455 impl Add<&Vec4> for &f32 {
1456 type Output = Vec4;
1457 #[inline]
1458 fn add(self, rhs: &Vec4) -> Vec4 {
1459 (*self).add(*rhs)
1460 }
1461 }
1462
1463 impl Add<Vec4> for &f32 {
1464 type Output = Vec4;
1465 #[inline]
1466 fn add(self, rhs: Vec4) -> Vec4 {
1467 (*self).add(rhs)
1468 }
1469 }
1470
1471 impl Sub for Vec4 {
1472 type Output = Self;
1473 #[inline]
1474 fn sub(self, rhs: Self) -> Self {
1475 Self(self.0 - rhs.0)
1476 }
1477 }
1478
1479 impl Sub<&Self> for Vec4 {
1480 type Output = Self;
1481 #[inline]
1482 fn sub(self, rhs: &Self) -> Self {
1483 self.sub(*rhs)
1484 }
1485 }
1486
1487 impl Sub<&Vec4> for &Vec4 {
1488 type Output = Vec4;
1489 #[inline]
1490 fn sub(self, rhs: &Vec4) -> Vec4 {
1491 (*self).sub(*rhs)
1492 }
1493 }
1494
1495 impl Sub<Vec4> for &Vec4 {
1496 type Output = Vec4;
1497 #[inline]
1498 fn sub(self, rhs: Vec4) -> Vec4 {
1499 (*self).sub(rhs)
1500 }
1501 }
1502
1503 impl SubAssign for Vec4 {
1504 #[inline]
1505 fn sub_assign(&mut self, rhs: Self) {
1506 self.0 -= rhs.0;
1507 }
1508 }
1509
1510 impl SubAssign<&Self> for Vec4 {
1511 #[inline]
1512 fn sub_assign(&mut self, rhs: &Self) {
1513 self.sub_assign(*rhs);
1514 }
1515 }
1516
1517 impl Sub<f32> for Vec4 {
1518 type Output = Self;
1519 #[inline]
1520 fn sub(self, rhs: f32) -> Self {
1521 Self(self.0 - f32x4::splat(rhs))
1522 }
1523 }
1524
1525 impl Sub<&f32> for Vec4 {
1526 type Output = Self;
1527 #[inline]
1528 fn sub(self, rhs: &f32) -> Self {
1529 self.sub(*rhs)
1530 }
1531 }
1532
1533 impl Sub<&f32> for &Vec4 {
1534 type Output = Vec4;
1535 #[inline]
1536 fn sub(self, rhs: &f32) -> Vec4 {
1537 (*self).sub(*rhs)
1538 }
1539 }
1540
1541 impl Sub<f32> for &Vec4 {
1542 type Output = Vec4;
1543 #[inline]
1544 fn sub(self, rhs: f32) -> Vec4 {
1545 (*self).sub(rhs)
1546 }
1547 }
1548
1549 impl SubAssign<f32> for Vec4 {
1550 #[inline]
1551 fn sub_assign(&mut self, rhs: f32) {
1552 self.0 -= f32x4::splat(rhs);
1553 }
1554 }
1555
1556 impl SubAssign<&f32> for Vec4 {
1557 #[inline]
1558 fn sub_assign(&mut self, rhs: &f32) {
1559 self.sub_assign(*rhs);
1560 }
1561 }
1562
1563 impl Sub<Vec4> for f32 {
1564 type Output = Vec4;
1565 #[inline]
1566 fn sub(self, rhs: Vec4) -> Vec4 {
1567 Vec4(f32x4::splat(self) - rhs.0)
1568 }
1569 }
1570
1571 impl Sub<&Vec4> for f32 {
1572 type Output = Vec4;
1573 #[inline]
1574 fn sub(self, rhs: &Vec4) -> Vec4 {
1575 self.sub(*rhs)
1576 }
1577 }
1578
1579 impl Sub<&Vec4> for &f32 {
1580 type Output = Vec4;
1581 #[inline]
1582 fn sub(self, rhs: &Vec4) -> Vec4 {
1583 (*self).sub(*rhs)
1584 }
1585 }
1586
1587 impl Sub<Vec4> for &f32 {
1588 type Output = Vec4;
1589 #[inline]
1590 fn sub(self, rhs: Vec4) -> Vec4 {
1591 (*self).sub(rhs)
1592 }
1593 }
1594
1595 impl Rem for Vec4 {
1596 type Output = Self;
1597 #[inline]
1598 fn rem(self, rhs: Self) -> Self {
1599 Self(self.0 % rhs.0)
1600 }
1601 }
1602
1603 impl Rem<&Self> for Vec4 {
1604 type Output = Self;
1605 #[inline]
1606 fn rem(self, rhs: &Self) -> Self {
1607 self.rem(*rhs)
1608 }
1609 }
1610
1611 impl Rem<&Vec4> for &Vec4 {
1612 type Output = Vec4;
1613 #[inline]
1614 fn rem(self, rhs: &Vec4) -> Vec4 {
1615 (*self).rem(*rhs)
1616 }
1617 }
1618
1619 impl Rem<Vec4> for &Vec4 {
1620 type Output = Vec4;
1621 #[inline]
1622 fn rem(self, rhs: Vec4) -> Vec4 {
1623 (*self).rem(rhs)
1624 }
1625 }
1626
1627 impl RemAssign for Vec4 {
1628 #[inline]
1629 fn rem_assign(&mut self, rhs: Self) {
1630 self.0 %= rhs.0;
1631 }
1632 }
1633
1634 impl RemAssign<&Self> for Vec4 {
1635 #[inline]
1636 fn rem_assign(&mut self, rhs: &Self) {
1637 self.rem_assign(*rhs);
1638 }
1639 }
1640
1641 impl Rem<f32> for Vec4 {
1642 type Output = Self;
1643 #[inline]
1644 fn rem(self, rhs: f32) -> Self {
1645 self.rem(Self::splat(rhs))
1646 }
1647 }
1648
1649 impl Rem<&f32> for Vec4 {
1650 type Output = Self;
1651 #[inline]
1652 fn rem(self, rhs: &f32) -> Self {
1653 self.rem(*rhs)
1654 }
1655 }
1656
1657 impl Rem<&f32> for &Vec4 {
1658 type Output = Vec4;
1659 #[inline]
1660 fn rem(self, rhs: &f32) -> Vec4 {
1661 (*self).rem(*rhs)
1662 }
1663 }
1664
1665 impl Rem<f32> for &Vec4 {
1666 type Output = Vec4;
1667 #[inline]
1668 fn rem(self, rhs: f32) -> Vec4 {
1669 (*self).rem(rhs)
1670 }
1671 }
1672
1673 impl RemAssign<f32> for Vec4 {
1674 #[inline]
1675 fn rem_assign(&mut self, rhs: f32) {
1676 self.0 %= f32x4::splat(rhs);
1677 }
1678 }
1679
1680 impl RemAssign<&f32> for Vec4 {
1681 #[inline]
1682 fn rem_assign(&mut self, rhs: &f32) {
1683 self.rem_assign(*rhs);
1684 }
1685 }
1686
1687 impl Rem<Vec4> for f32 {
1688 type Output = Vec4;
1689 #[inline]
1690 fn rem(self, rhs: Vec4) -> Vec4 {
1691 Vec4::splat(self).rem(rhs)
1692 }
1693 }
1694
1695 impl Rem<&Vec4> for f32 {
1696 type Output = Vec4;
1697 #[inline]
1698 fn rem(self, rhs: &Vec4) -> Vec4 {
1699 self.rem(*rhs)
1700 }
1701 }
1702
1703 impl Rem<&Vec4> for &f32 {
1704 type Output = Vec4;
1705 #[inline]
1706 fn rem(self, rhs: &Vec4) -> Vec4 {
1707 (*self).rem(*rhs)
1708 }
1709 }
1710
1711 impl Rem<Vec4> for &f32 {
1712 type Output = Vec4;
1713 #[inline]
1714 fn rem(self, rhs: Vec4) -> Vec4 {
1715 (*self).rem(rhs)
1716 }
1717 }
1718
1719 impl AsRef<[f32; 4]> for Vec4 {
1720 #[inline]
1721 fn as_ref(&self) -> &[f32; 4] {
1722 unsafe { &*(self as *const Self as *const [f32; 4]) }
1723 }
1724 }
1725
1726 impl AsMut<[f32; 4]> for Vec4 {
1727 #[inline]
1728 fn as_mut(&mut self) -> &mut [f32; 4] {
1729 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
1730 }
1731 }
1732
1733 impl Sum for Vec4 {
1734 #[inline]
1735 fn sum<I>(iter: I) -> Self
1736 where
1737 I: Iterator<Item = Self>,
1738 {
1739 iter.fold(Self::ZERO, Self::add)
1740 }
1741 }
1742
1743 impl<'a> Sum<&'a Self> for Vec4 {
1744 #[inline]
1745 fn sum<I>(iter: I) -> Self
1746 where
1747 I: Iterator<Item = &'a Self>,
1748 {
1749 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1750 }
1751 }
1752
1753 impl Product for Vec4 {
1754 #[inline]
1755 fn product<I>(iter: I) -> Self
1756 where
1757 I: Iterator<Item = Self>,
1758 {
1759 iter.fold(Self::ONE, Self::mul)
1760 }
1761 }
1762
1763 impl<'a> Product<&'a Self> for Vec4 {
1764 #[inline]
1765 fn product<I>(iter: I) -> Self
1766 where
1767 I: Iterator<Item = &'a Self>,
1768 {
1769 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1770 }
1771 }
1772
1773 impl Neg for Vec4 {
1774 type Output = Self;
1775 #[inline]
1776 fn neg(self) -> Self {
1777 Self(-self.0)
1778 }
1779 }
1780
1781 impl Neg for &Vec4 {
1782 type Output = Vec4;
1783 #[inline]
1784 fn neg(self) -> Vec4 {
1785 (*self).neg()
1786 }
1787 }
1788
1789 impl Index<usize> for Vec4 {
1790 type Output = f32;
1791 #[inline]
1792 fn index(&self, index: usize) -> &Self::Output {
1793 &self.0[index]
1794 }
1795 }
1796
1797 impl IndexMut<usize> for Vec4 {
1798 #[inline]
1799 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1800 &mut self.0[index]
1801 }
1802 }
1803
1804 impl fmt::Display for Vec4 {
1805 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1806 if let Some(p) = f.precision() {
1807 write!(
1808 f,
1809 "[{:.*}, {:.*}, {:.*}, {:.*}]",
1810 p, self.x, p, self.y, p, self.z, p, self.w
1811 )
1812 } else {
1813 write!(f, "[{}, {}, {}, {}]", self.x, self.y, self.z, self.w)
1814 }
1815 }
1816 }
1817
1818 impl fmt::Debug for Vec4 {
1819 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
1820 fmt.debug_tuple(stringify!(Vec4))
1821 .field(&self.x)
1822 .field(&self.y)
1823 .field(&self.z)
1824 .field(&self.w)
1825 .finish()
1826 }
1827 }
1828
1829 impl From<Vec4> for f32x4 {
1830 #[inline(always)]
1831 fn from(t: Vec4) -> Self {
1832 t.0
1833 }
1834 }
1835
1836 impl From<f32x4> for Vec4 {
1837 #[inline(always)]
1838 fn from(t: f32x4) -> Self {
1839 Self(t)
1840 }
1841 }
1842
1843 impl From<[f32; 4]> for Vec4 {
1844 #[inline]
1845 fn from(a: [f32; 4]) -> Self {
1846 Self(f32x4::from_array(a))
1847 }
1848 }
1849
1850 impl From<Vec4> for [f32; 4] {
1851 #[inline]
1852 fn from(v: Vec4) -> Self {
1853 v.0.to_array()
1854 }
1855 }
1856
1857 impl From<(f32, f32, f32, f32)> for Vec4 {
1858 #[inline]
1859 fn from(t: (f32, f32, f32, f32)) -> Self {
1860 Self::new(t.0, t.1, t.2, t.3)
1861 }
1862 }
1863
1864 impl From<Vec4> for (f32, f32, f32, f32) {
1865 #[inline]
1866 fn from(v: Vec4) -> Self {
1867 (v.x, v.y, v.z, v.w)
1868 }
1869 }
1870
1871 impl From<(Vec3A, f32)> for Vec4 {
1872 #[inline]
1873 fn from((v, w): (Vec3A, f32)) -> Self {
1874 v.extend(w)
1875 }
1876 }
1877
1878 impl From<(f32, Vec3A)> for Vec4 {
1879 #[inline]
1880 fn from((x, v): (f32, Vec3A)) -> Self {
1881 Self::new(x, v.x, v.y, v.z)
1882 }
1883 }
1884
1885 impl From<(Vec3, f32)> for Vec4 {
1886 #[inline]
1887 fn from((v, w): (Vec3, f32)) -> Self {
1888 Self::new(v.x, v.y, v.z, w)
1889 }
1890 }
1891
1892 impl From<(f32, Vec3)> for Vec4 {
1893 #[inline]
1894 fn from((x, v): (f32, Vec3)) -> Self {
1895 Self::new(x, v.x, v.y, v.z)
1896 }
1897 }
1898
1899 impl From<(Vec2, f32, f32)> for Vec4 {
1900 #[inline]
1901 fn from((v, z, w): (Vec2, f32, f32)) -> Self {
1902 Self::new(v.x, v.y, z, w)
1903 }
1904 }
1905
1906 impl From<(Vec2, Vec2)> for Vec4 {
1907 #[inline]
1908 fn from((v, u): (Vec2, Vec2)) -> Self {
1909 Self::new(v.x, v.y, u.x, u.y)
1910 }
1911 }
1912
1913 impl Deref for Vec4 {
1914 type Target = crate::deref::Vec4<f32>;
1915 #[inline]
1916 fn deref(&self) -> &Self::Target {
1917 unsafe { &*(self as *const Self).cast() }
1918 }
1919 }
1920
1921 impl DerefMut for Vec4 {
1922 #[inline]
1923 fn deref_mut(&mut self) -> &mut Self::Target {
1924 unsafe { &mut *(self as *mut Self).cast() }
1925 }
1926 }
1927
1928 impl From<BVec4> for Vec4 {
1929 #[inline]
1930 fn from(v: BVec4) -> Self {
1931 Self::new(
1932 f32::from(v.x),
1933 f32::from(v.y),
1934 f32::from(v.z),
1935 f32::from(v.w),
1936 )
1937 }
1938 }
1939
1940 #[cfg(not(feature = "scalar-math"))]
1941 impl From<BVec4A> for Vec4 {
1942 #[inline]
1943 fn from(v: BVec4A) -> Self {
1944 let bool_array: [bool; 4] = v.into();
1945 Self::new(
1946 f32::from(bool_array[0]),
1947 f32::from(bool_array[1]),
1948 f32::from(bool_array[2]),
1949 f32::from(bool_array[3]),
1950 )
1951 }
1952 }