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