Debian Codegraph @

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

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