Debian Codegraph @

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

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