Debian Codegraph @

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

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