Debian Codegraph @

debian/missing-sources/glam-0.29.3/src/f32/scalar/vec3a.rs

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