Debian Codegraph @

debian/missing-sources/glam-0.21.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::{wasm32::*, BVec3A, Vec2, Vec3, Vec4};
4
5 #[cfg(not(target_arch = "spirv"))]
6 use core::fmt;
7 use core::iter::{Product, Sum};
8 use core::{f32, ops::*};
9
10 use core::arch::wasm32::*;
11
12 #[cfg(feature = "libm")]
13 #[allow(unused_imports)]
14 use num_traits::Float;
15
16 union UnionCast {
17 a: [f32; 4],
18 v: Vec3A,
19 }
20
21 /// Creates a 3-dimensional vector.
22 #[inline(always)]
23 pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
24 Vec3A::new(x, y, z)
25 }
26
27 /// A 3-dimensional vector with SIMD support.
28 ///
29 /// This type is 16 byte aligned. A SIMD vector type is used for storage on supported platforms for
30 /// better performance than the `Vec3` type.
31 ///
32 /// It is possible to convert between `Vec3` and `Vec3A` types using `From` trait implementations.
33 #[derive(Clone, Copy)]
34 #[repr(transparent)]
35 pub struct Vec3A(pub(crate) v128);
36
37 impl Vec3A {
38 /// All zeroes.
39 pub const ZERO: Self = Self::splat(0.0);
40
41 /// All ones.
42 pub const ONE: Self = Self::splat(1.0);
43
44 /// All negative ones.
45 pub const NEG_ONE: Self = Self::splat(-1.0);
46
47 /// All NAN.
48 pub const NAN: Self = Self::splat(f32::NAN);
49
50 /// A unit-length vector pointing along the positive X axis.
51 pub const X: Self = Self::new(1.0, 0.0, 0.0);
52
53 /// A unit-length vector pointing along the positive Y axis.
54 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
55
56 /// A unit-length vector pointing along the positive Z axis.
57 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
58
59 /// A unit-length vector pointing along the negative X axis.
60 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
61
62 /// A unit-length vector pointing along the negative Y axis.
63 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
64
65 /// A unit-length vector pointing along the negative Z axis.
66 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
67
68 /// The unit axes.
69 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
70
71 /// Creates a new vector.
72 #[inline(always)]
73 pub const fn new(x: f32, y: f32, z: f32) -> Self {
74 Self(f32x4(x, y, z, z))
75 }
76
77 /// Creates a vector with all elements set to `v`.
78 #[inline]
79 pub const fn splat(v: f32) -> Self {
80 unsafe { UnionCast { a: [v; 4] }.v }
81 }
82
83 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
84 /// for each element of `self`.
85 ///
86 /// A true element in the mask uses the corresponding element from `if_true`, and false
87 /// uses the element from `if_false`.
88 #[inline]
89 pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
90 Self(v128_bitselect(if_true.0, if_false.0, mask.0))
91 }
92
93 /// Creates a new vector from an array.
94 #[inline]
95 pub const fn from_array(a: [f32; 3]) -> Self {
96 Self::new(a[0], a[1], a[2])
97 }
98
99 /// `[x, y, z]`
100 #[inline]
101 pub const fn to_array(&self) -> [f32; 3] {
102 unsafe { *(self as *const Vec3A as *const [f32; 3]) }
103 }
104
105 /// Creates a vector from the first 3 values in `slice`.
106 ///
107 /// # Panics
108 ///
109 /// Panics if `slice` is less than 3 elements long.
110 #[inline]
111 pub const fn from_slice(slice: &[f32]) -> Self {
112 Self::new(slice[0], slice[1], slice[2])
113 }
114
115 /// Writes the elements of `self` to the first 3 elements in `slice`.
116 ///
117 /// # Panics
118 ///
119 /// Panics if `slice` is less than 3 elements long.
120 #[inline]
121 pub fn write_to_slice(self, slice: &mut [f32]) {
122 slice[0] = self.x;
123 slice[1] = self.y;
124 slice[2] = self.z;
125 }
126
127 /// Internal method for creating a 3D vector from a 4D vector, discarding `w`.
128 #[allow(dead_code)]
129 #[inline]
130 pub(crate) fn from_vec4(v: Vec4) -> Self {
131 Self(v.0)
132 }
133
134 /// Creates a 4D vector from `self` and the given `w` value.
135 #[inline]
136 pub fn extend(self, w: f32) -> Vec4 {
137 Vec4::new(self.x, self.y, self.z, w)
138 }
139
140 /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
141 ///
142 /// Truncation may also be performed by using `self.xy()` or `Vec2::from()`.
143 #[inline]
144 pub fn truncate(self) -> Vec2 {
145 use crate::swizzles::Vec3Swizzles;
146 self.xy()
147 }
148
149 /// Computes the dot product of `self` and `rhs`.
150 #[inline]
151 pub fn dot(self, rhs: Self) -> f32 {
152 dot3(self.0, rhs.0)
153 }
154
155 /// Computes the cross product of `self` and `rhs`.
156 #[inline]
157 pub fn cross(self, rhs: Self) -> Self {
158 let lhszxy = i32x4_shuffle::<2, 0, 1, 1>(self.0, self.0);
159 let rhszxy = i32x4_shuffle::<2, 0, 1, 1>(rhs.0, rhs.0);
160 let lhszxy_rhs = f32x4_mul(lhszxy, rhs.0);
161 let rhszxy_lhs = f32x4_mul(rhszxy, self.0);
162 let sub = f32x4_sub(lhszxy_rhs, rhszxy_lhs);
163 Self(i32x4_shuffle::<2, 0, 1, 1>(sub, sub))
164 }
165
166 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
167 ///
168 /// In other words this computes `[self.x.min(rhs.x), self.y.min(rhs.y), ..]`.
169 #[inline]
170 pub fn min(self, rhs: Self) -> Self {
171 Self(f32x4_pmin(self.0, rhs.0))
172 }
173
174 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
175 ///
176 /// In other words this computes `[self.x.max(rhs.x), self.y.max(rhs.y), ..]`.
177 #[inline]
178 pub fn max(self, rhs: Self) -> Self {
179 Self(f32x4_pmax(self.0, rhs.0))
180 }
181
182 /// Component-wise clamping of values, similar to [`f32::clamp`].
183 ///
184 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
185 ///
186 /// # Panics
187 ///
188 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
189 #[inline]
190 pub fn clamp(self, min: Self, max: Self) -> Self {
191 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
192 self.max(min).min(max)
193 }
194
195 /// Returns the horizontal minimum of `self`.
196 ///
197 /// In other words this computes `min(x, y, ..)`.
198 #[inline]
199 pub fn min_element(self) -> f32 {
200 let v = self.0;
201 let v = f32x4_pmin(v, i32x4_shuffle::<2, 2, 1, 1>(v, v));
202 let v = f32x4_pmin(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
203 f32x4_extract_lane::<0>(v)
204 }
205
206 /// Returns the horizontal maximum of `self`.
207 ///
208 /// In other words this computes `max(x, y, ..)`.
209 #[inline]
210 pub fn max_element(self) -> f32 {
211 let v = self.0;
212 let v = f32x4_pmax(v, i32x4_shuffle::<2, 2, 0, 0>(v, v));
213 let v = f32x4_pmax(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
214 f32x4_extract_lane::<0>(v)
215 }
216
217 /// Returns a vector mask containing the result of a `==` comparison for each element of
218 /// `self` and `rhs`.
219 ///
220 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
221 /// elements.
222 #[inline]
223 pub fn cmpeq(self, rhs: Self) -> BVec3A {
224 BVec3A(f32x4_eq(self.0, rhs.0))
225 }
226
227 /// Returns a vector mask containing the result of a `!=` comparison for each element of
228 /// `self` and `rhs`.
229 ///
230 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
231 /// elements.
232 #[inline]
233 pub fn cmpne(self, rhs: Self) -> BVec3A {
234 BVec3A(f32x4_ne(self.0, rhs.0))
235 }
236
237 /// Returns a vector mask containing the result of a `>=` comparison for each element of
238 /// `self` and `rhs`.
239 ///
240 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
241 /// elements.
242 #[inline]
243 pub fn cmpge(self, rhs: Self) -> BVec3A {
244 BVec3A(f32x4_ge(self.0, rhs.0))
245 }
246
247 /// Returns a vector mask containing the result of a `>` comparison for each element of
248 /// `self` and `rhs`.
249 ///
250 /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
251 /// elements.
252 #[inline]
253 pub fn cmpgt(self, rhs: Self) -> BVec3A {
254 BVec3A(f32x4_gt(self.0, rhs.0))
255 }
256
257 /// Returns a vector mask containing the result of a `<=` comparison for each element of
258 /// `self` and `rhs`.
259 ///
260 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
261 /// elements.
262 #[inline]
263 pub fn cmple(self, rhs: Self) -> BVec3A {
264 BVec3A(f32x4_le(self.0, rhs.0))
265 }
266
267 /// Returns a vector mask containing the result of a `<` comparison for each element of
268 /// `self` and `rhs`.
269 ///
270 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
271 /// elements.
272 #[inline]
273 pub fn cmplt(self, rhs: Self) -> BVec3A {
274 BVec3A(f32x4_lt(self.0, rhs.0))
275 }
276
277 /// Returns a vector containing the absolute value of each element of `self`.
278 #[inline]
279 pub fn abs(self) -> Self {
280 Self(f32x4_abs(self.0))
281 }
282
283 /// Returns a vector with elements representing the sign of `self`.
284 ///
285 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
286 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
287 /// - `NAN` if the number is `NAN`
288 #[inline]
289 pub fn signum(self) -> Self {
290 let mask = self.cmpge(Self::ZERO);
291 let result = Self::select(mask, Self::ONE, Self::NEG_ONE);
292 let mask = self.is_nan_mask();
293 Self::select(mask, self, result)
294 }
295
296 /// Returns `true` if, and only if, all elements are finite. If any element is either
297 /// `NaN`, positive or negative infinity, this will return `false`.
298 #[inline]
299 pub fn is_finite(self) -> bool {
300 self.x.is_finite() && self.y.is_finite() && self.z.is_finite()
301 }
302
303 /// Returns `true` if any elements are `NaN`.
304 #[inline]
305 pub fn is_nan(self) -> bool {
306 self.is_nan_mask().any()
307 }
308
309 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
310 ///
311 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
312 #[inline]
313 pub fn is_nan_mask(self) -> BVec3A {
314 BVec3A(f32x4_ne(self.0, self.0))
315 }
316
317 /// Computes the length of `self`.
318 #[doc(alias = "magnitude")]
319 #[inline]
320 pub fn length(self) -> f32 {
321 let dot = dot3_in_x(self.0, self.0);
322 f32x4_extract_lane::<0>(f32x4_sqrt(dot))
323 }
324
325 /// Computes the squared length of `self`.
326 ///
327 /// This is faster than `length()` as it avoids a square root operation.
328 #[doc(alias = "magnitude2")]
329 #[inline]
330 pub fn length_squared(self) -> f32 {
331 self.dot(self)
332 }
333
334 /// Computes `1.0 / length()`.
335 ///
336 /// For valid results, `self` must _not_ be of length zero.
337 #[inline]
338 pub fn length_recip(self) -> f32 {
339 let dot = dot3_in_x(self.0, self.0);
340 f32x4_extract_lane::<0>(f32x4_div(Self::ONE.0, f32x4_sqrt(dot)))
341 }
342
343 /// Computes the Euclidean distance between two points in space.
344 #[inline]
345 pub fn distance(self, rhs: Self) -> f32 {
346 (self - rhs).length()
347 }
348
349 /// Compute the squared euclidean distance between two points in space.
350 #[inline]
351 pub fn distance_squared(self, rhs: Self) -> f32 {
352 (self - rhs).length_squared()
353 }
354
355 /// Returns `self` normalized to length 1.0.
356 ///
357 /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
358 ///
359 /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`].
360 ///
361 /// Panics
362 ///
363 /// Will panic if `self` is zero length when `glam_assert` is enabled.
364 #[must_use]
365 #[inline]
366 pub fn normalize(self) -> Self {
367 let length = f32x4_sqrt(dot3_into_v128(self.0, self.0));
368 #[allow(clippy::let_and_return)]
369 let normalized = Self(f32x4_div(self.0, length));
370 glam_assert!(normalized.is_finite());
371 normalized
372 }
373
374 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
375 ///
376 /// In particular, if the input is zero (or very close to zero), or non-finite,
377 /// the result of this operation will be `None`.
378 ///
379 /// See also [`Self::normalize_or_zero`].
380 #[must_use]
381 #[inline]
382 pub fn try_normalize(self) -> Option<Self> {
383 let rcp = self.length_recip();
384 if rcp.is_finite() && rcp > 0.0 {
385 Some(self * rcp)
386 } else {
387 None
388 }
389 }
390
391 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
392 ///
393 /// In particular, if the input is zero (or very close to zero), or non-finite,
394 /// the result of this operation will be zero.
395 ///
396 /// See also [`Self::try_normalize`].
397 #[must_use]
398 #[inline]
399 pub fn normalize_or_zero(self) -> Self {
400 let rcp = self.length_recip();
401 if rcp.is_finite() && rcp > 0.0 {
402 self * rcp
403 } else {
404 Self::ZERO
405 }
406 }
407
408 /// Returns whether `self` is length `1.0` or not.
409 ///
410 /// Uses a precision threshold of `1e-6`.
411 #[inline]
412 pub fn is_normalized(self) -> bool {
413 // TODO: do something with epsilon
414 (self.length_squared() - 1.0).abs() <= 1e-4
415 }
416
417 /// Returns the vector projection of `self` onto `rhs`.
418 ///
419 /// `rhs` must be of non-zero length.
420 ///
421 /// # Panics
422 ///
423 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
424 #[must_use]
425 #[inline]
426 pub fn project_onto(self, rhs: Self) -> Self {
427 let other_len_sq_rcp = rhs.dot(rhs).recip();
428 glam_assert!(other_len_sq_rcp.is_finite());
429 rhs * self.dot(rhs) * other_len_sq_rcp
430 }
431
432 /// Returns the vector rejection of `self` from `rhs`.
433 ///
434 /// The vector rejection is the vector perpendicular to the projection of `self` onto
435 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
436 ///
437 /// `rhs` must be of non-zero length.
438 ///
439 /// # Panics
440 ///
441 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
442 #[must_use]
443 #[inline]
444 pub fn reject_from(self, rhs: Self) -> Self {
445 self - self.project_onto(rhs)
446 }
447
448 /// Returns the vector projection of `self` onto `rhs`.
449 ///
450 /// `rhs` must be normalized.
451 ///
452 /// # Panics
453 ///
454 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
455 #[must_use]
456 #[inline]
457 pub fn project_onto_normalized(self, rhs: Self) -> Self {
458 glam_assert!(rhs.is_normalized());
459 rhs * self.dot(rhs)
460 }
461
462 /// Returns the vector rejection of `self` from `rhs`.
463 ///
464 /// The vector rejection is the vector perpendicular to the projection of `self` onto
465 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
466 ///
467 /// `rhs` must be normalized.
468 ///
469 /// # Panics
470 ///
471 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
472 #[must_use]
473 #[inline]
474 pub fn reject_from_normalized(self, rhs: Self) -> Self {
475 self - self.project_onto_normalized(rhs)
476 }
477
478 /// Returns a vector containing the nearest integer to a number for each element of `self`.
479 /// Round half-way cases away from 0.0.
480 #[inline]
481 pub fn round(self) -> Self {
482 Self(f32x4_nearest(self.0))
483 }
484
485 /// Returns a vector containing the largest integer less than or equal to a number for each
486 /// element of `self`.
487 #[inline]
488 pub fn floor(self) -> Self {
489 Self(f32x4_floor(self.0))
490 }
491
492 /// Returns a vector containing the smallest integer greater than or equal to a number for
493 /// each element of `self`.
494 #[inline]
495 pub fn ceil(self) -> Self {
496 Self(f32x4_ceil(self.0))
497 }
498
499 /// Returns a vector containing the fractional part of the vector, e.g. `self -
500 /// self.floor()`.
501 ///
502 /// Note that this is fast but not precise for large numbers.
503 #[inline]
504 pub fn fract(self) -> Self {
505 self - self.floor()
506 }
507
508 /// Returns a vector containing `e^self` (the exponential function) for each element of
509 /// `self`.
510 #[inline]
511 pub fn exp(self) -> Self {
512 Self::new(self.x.exp(), self.y.exp(), self.z.exp())
513 }
514
515 /// Returns a vector containing each element of `self` raised to the power of `n`.
516 #[inline]
517 pub fn powf(self, n: f32) -> Self {
518 Self::new(self.x.powf(n), self.y.powf(n), self.z.powf(n))
519 }
520
521 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
522 #[inline]
523 pub fn recip(self) -> Self {
524 Self(f32x4_div(Self::ONE.0, self.0))
525 }
526
527 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
528 ///
529 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
530 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
531 /// extrapolated.
532 #[doc(alias = "mix")]
533 #[inline]
534 pub fn lerp(self, rhs: Self, s: f32) -> Self {
535 self + ((rhs - self) * s)
536 }
537
538 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
539 /// less than or equal to `max_abs_diff`.
540 ///
541 /// This can be used to compare if two vectors contain similar elements. It works best when
542 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
543 /// the values being compared against.
544 ///
545 /// For more see
546 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
547 #[inline]
548 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
549 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
550 }
551
552 /// Returns a vector with a length no less than `min` and no more than `max`
553 ///
554 /// # Panics
555 ///
556 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
557 #[inline]
558 pub fn clamp_length(self, min: f32, max: f32) -> Self {
559 glam_assert!(min <= max);
560 let length_sq = self.length_squared();
561 if length_sq < min * min {
562 self * (length_sq.sqrt().recip() * min)
563 } else if length_sq > max * max {
564 self * (length_sq.sqrt().recip() * max)
565 } else {
566 self
567 }
568 }
569
570 /// Returns a vector with a length no more than `max`
571 pub fn clamp_length_max(self, max: f32) -> Self {
572 let length_sq = self.length_squared();
573 if length_sq > max * max {
574 self * (length_sq.sqrt().recip() * max)
575 } else {
576 self
577 }
578 }
579
580 /// Returns a vector with a length no less than `min`
581 pub fn clamp_length_min(self, min: f32) -> Self {
582 let length_sq = self.length_squared();
583 if length_sq < min * min {
584 self * (length_sq.sqrt().recip() * min)
585 } else {
586 self
587 }
588 }
589
590 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
591 /// error, yielding a more accurate result than an unfused multiply-add.
592 ///
593 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
594 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
595 /// and will be heavily dependant on designing algorithms with specific target hardware in
596 /// mind.
597 #[inline]
598 pub fn mul_add(self, a: Self, b: Self) -> Self {
599 Self::new(
600 self.x.mul_add(a.x, b.x),
601 self.y.mul_add(a.y, b.y),
602 self.z.mul_add(a.z, b.z),
603 )
604 }
605
606 /// Returns the angle (in radians) between two vectors.
607 ///
608 /// The input vectors do not need to be unit length however they must be non-zero.
609 #[inline]
610 pub fn angle_between(self, rhs: Self) -> f32 {
611 use crate::FloatEx;
612 self.dot(rhs)
613 .div(self.length_squared().mul(rhs.length_squared()).sqrt())
614 .acos_approx()
615 }
616
617 /// Returns some vector that is orthogonal to the given one.
618 ///
619 /// The input vector must be finite and non-zero.
620 ///
621 /// The output vector is not necessarily unit-length.
622 /// For that use [`Self::any_orthonormal_vector`] instead.
623 #[inline]
624 pub fn any_orthogonal_vector(&self) -> Self {
625 // This can probably be optimized
626 if self.x.abs() > self.y.abs() {
627 Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
628 } else {
629 Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
630 }
631 }
632
633 /// Returns any unit-length vector that is orthogonal to the given one.
634 /// The input vector must be finite and non-zero.
635 ///
636 /// # Panics
637 ///
638 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
639 #[inline]
640 pub fn any_orthonormal_vector(&self) -> Self {
641 glam_assert!(self.is_normalized());
642 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
643 #[cfg(feature = "std")]
644 let sign = (1.0_f32).copysign(self.z);
645 #[cfg(not(feature = "std"))]
646 let sign = self.z.signum();
647 let a = -1.0 / (sign + self.z);
648 let b = self.x * self.y * a;
649 Self::new(b, sign + self.y * self.y * a, -self.y)
650 }
651
652 /// Given a unit-length vector return two other vectors that together form an orthonormal
653 /// basis. That is, all three vectors are orthogonal to each other and are normalized.
654 ///
655 /// # Panics
656 ///
657 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
658 #[inline]
659 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
660 glam_assert!(self.is_normalized());
661 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
662 #[cfg(feature = "std")]
663 let sign = (1.0_f32).copysign(self.z);
664 #[cfg(not(feature = "std"))]
665 let sign = self.z.signum();
666 let a = -1.0 / (sign + self.z);
667 let b = self.x * self.y * a;
668 (
669 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
670 Self::new(b, sign + self.y * self.y * a, -self.y),
671 )
672 }
673
674 /// Casts all elements of `self` to `f64`.
675 #[inline]
676 pub fn as_dvec3(&self) -> crate::DVec3 {
677 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
678 }
679
680 /// Casts all elements of `self` to `i32`.
681 #[inline]
682 pub fn as_ivec3(&self) -> crate::IVec3 {
683 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
684 }
685
686 /// Casts all elements of `self` to `u32`.
687 #[inline]
688 pub fn as_uvec3(&self) -> crate::UVec3 {
689 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
690 }
691 }
692
693 impl Default for Vec3A {
694 #[inline(always)]
695 fn default() -> Self {
696 Self::ZERO
697 }
698 }
699
700 impl PartialEq for Vec3A {
701 #[inline]
702 fn eq(&self, rhs: &Self) -> bool {
703 self.cmpeq(*rhs).all()
704 }
705 }
706
707 impl Div<Vec3A> for Vec3A {
708 type Output = Self;
709 #[inline]
710 fn div(self, rhs: Self) -> Self {
711 Self(f32x4_div(self.0, rhs.0))
712 }
713 }
714
715 impl DivAssign<Vec3A> for Vec3A {
716 #[inline]
717 fn div_assign(&mut self, rhs: Self) {
718 self.0 = f32x4_div(self.0, rhs.0);
719 }
720 }
721
722 impl Div<f32> for Vec3A {
723 type Output = Self;
724 #[inline]
725 fn div(self, rhs: f32) -> Self {
726 Self(f32x4_div(self.0, f32x4_splat(rhs)))
727 }
728 }
729
730 impl DivAssign<f32> for Vec3A {
731 #[inline]
732 fn div_assign(&mut self, rhs: f32) {
733 self.0 = f32x4_div(self.0, f32x4_splat(rhs))
734 }
735 }
736
737 impl Div<Vec3A> for f32 {
738 type Output = Vec3A;
739 #[inline]
740 fn div(self, rhs: Vec3A) -> Vec3A {
741 Vec3A(f32x4_div(f32x4_splat(self), rhs.0))
742 }
743 }
744
745 impl Mul<Vec3A> for Vec3A {
746 type Output = Self;
747 #[inline]
748 fn mul(self, rhs: Self) -> Self {
749 Self(f32x4_mul(self.0, rhs.0))
750 }
751 }
752
753 impl MulAssign<Vec3A> for Vec3A {
754 #[inline]
755 fn mul_assign(&mut self, rhs: Self) {
756 self.0 = f32x4_mul(self.0, rhs.0);
757 }
758 }
759
760 impl Mul<f32> for Vec3A {
761 type Output = Self;
762 #[inline]
763 fn mul(self, rhs: f32) -> Self {
764 Self(f32x4_mul(self.0, f32x4_splat(rhs)))
765 }
766 }
767
768 impl MulAssign<f32> for Vec3A {
769 #[inline]
770 fn mul_assign(&mut self, rhs: f32) {
771 self.0 = f32x4_mul(self.0, f32x4_splat(rhs))
772 }
773 }
774
775 impl Mul<Vec3A> for f32 {
776 type Output = Vec3A;
777 #[inline]
778 fn mul(self, rhs: Vec3A) -> Vec3A {
779 Vec3A(f32x4_mul(f32x4_splat(self), rhs.0))
780 }
781 }
782
783 impl Add<Vec3A> for Vec3A {
784 type Output = Self;
785 #[inline]
786 fn add(self, rhs: Self) -> Self {
787 Self(f32x4_add(self.0, rhs.0))
788 }
789 }
790
791 impl AddAssign<Vec3A> for Vec3A {
792 #[inline]
793 fn add_assign(&mut self, rhs: Self) {
794 self.0 = f32x4_add(self.0, rhs.0);
795 }
796 }
797
798 impl Add<f32> for Vec3A {
799 type Output = Self;
800 #[inline]
801 fn add(self, rhs: f32) -> Self {
802 Self(f32x4_add(self.0, f32x4_splat(rhs)))
803 }
804 }
805
806 impl AddAssign<f32> for Vec3A {
807 #[inline]
808 fn add_assign(&mut self, rhs: f32) {
809 self.0 = f32x4_add(self.0, f32x4_splat(rhs));
810 }
811 }
812
813 impl Add<Vec3A> for f32 {
814 type Output = Vec3A;
815 #[inline]
816 fn add(self, rhs: Vec3A) -> Vec3A {
817 Vec3A(f32x4_add(f32x4_splat(self), rhs.0))
818 }
819 }
820
821 impl Sub<Vec3A> for Vec3A {
822 type Output = Self;
823 #[inline]
824 fn sub(self, rhs: Self) -> Self {
825 Self(f32x4_sub(self.0, rhs.0))
826 }
827 }
828
829 impl SubAssign<Vec3A> for Vec3A {
830 #[inline]
831 fn sub_assign(&mut self, rhs: Vec3A) {
832 self.0 = f32x4_sub(self.0, rhs.0);
833 }
834 }
835
836 impl Sub<f32> for Vec3A {
837 type Output = Self;
838 #[inline]
839 fn sub(self, rhs: f32) -> Self {
840 Self(f32x4_sub(self.0, f32x4_splat(rhs)))
841 }
842 }
843
844 impl SubAssign<f32> for Vec3A {
845 #[inline]
846 fn sub_assign(&mut self, rhs: f32) {
847 self.0 = f32x4_sub(self.0, f32x4_splat(rhs))
848 }
849 }
850
851 impl Sub<Vec3A> for f32 {
852 type Output = Vec3A;
853 #[inline]
854 fn sub(self, rhs: Vec3A) -> Vec3A {
855 Vec3A(f32x4_sub(f32x4_splat(self), rhs.0))
856 }
857 }
858
859 impl Rem<Vec3A> for Vec3A {
860 type Output = Self;
861 #[inline]
862 fn rem(self, rhs: Self) -> Self {
863 let n = f32x4_floor(f32x4_div(self.0, rhs.0));
864 Self(f32x4_sub(self.0, f32x4_mul(n, rhs.0)))
865 }
866 }
867
868 impl RemAssign<Vec3A> for Vec3A {
869 #[inline]
870 fn rem_assign(&mut self, rhs: Self) {
871 *self = self.rem(rhs);
872 }
873 }
874
875 impl Rem<f32> for Vec3A {
876 type Output = Self;
877 #[inline]
878 fn rem(self, rhs: f32) -> Self {
879 self.rem(Self::splat(rhs))
880 }
881 }
882
883 impl RemAssign<f32> for Vec3A {
884 #[inline]
885 fn rem_assign(&mut self, rhs: f32) {
886 *self = self.rem(Self::splat(rhs));
887 }
888 }
889
890 impl Rem<Vec3A> for f32 {
891 type Output = Vec3A;
892 #[inline]
893 fn rem(self, rhs: Vec3A) -> Vec3A {
894 Vec3A::splat(self).rem(rhs)
895 }
896 }
897
898 #[cfg(not(target_arch = "spirv"))]
899 impl AsRef<[f32; 3]> for Vec3A {
900 #[inline]
901 fn as_ref(&self) -> &[f32; 3] {
902 unsafe { &*(self as *const Vec3A as *const [f32; 3]) }
903 }
904 }
905
906 #[cfg(not(target_arch = "spirv"))]
907 impl AsMut<[f32; 3]> for Vec3A {
908 #[inline]
909 fn as_mut(&mut self) -> &mut [f32; 3] {
910 unsafe { &mut *(self as *mut Vec3A as *mut [f32; 3]) }
911 }
912 }
913
914 impl<'a> Sum<&'a Self> for Vec3A {
915 #[inline]
916 fn sum<I>(iter: I) -> Self
917 where
918 I: Iterator<Item = &'a Self>,
919 {
920 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
921 }
922 }
923
924 impl<'a> Product<&'a Self> for Vec3A {
925 #[inline]
926 fn product<I>(iter: I) -> Self
927 where
928 I: Iterator<Item = &'a Self>,
929 {
930 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
931 }
932 }
933
934 impl Neg for Vec3A {
935 type Output = Self;
936 #[inline]
937 fn neg(self) -> Self {
938 Self(f32x4_neg(self.0))
939 }
940 }
941
942 impl Index<usize> for Vec3A {
943 type Output = f32;
944 #[inline]
945 fn index(&self, index: usize) -> &Self::Output {
946 match index {
947 0 => &self.x,
948 1 => &self.y,
949 2 => &self.z,
950 _ => panic!("index out of bounds"),
951 }
952 }
953 }
954
955 impl IndexMut<usize> for Vec3A {
956 #[inline]
957 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
958 match index {
959 0 => &mut self.x,
960 1 => &mut self.y,
961 2 => &mut self.z,
962 _ => panic!("index out of bounds"),
963 }
964 }
965 }
966
967 #[cfg(not(target_arch = "spirv"))]
968 impl fmt::Display for Vec3A {
969 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
970 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
971 }
972 }
973
974 #[cfg(not(target_arch = "spirv"))]
975 impl fmt::Debug for Vec3A {
976 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
977 fmt.debug_tuple(stringify!(Vec3A))
978 .field(&self.x)
979 .field(&self.y)
980 .field(&self.z)
981 .finish()
982 }
983 }
984
985 impl From<Vec3A> for v128 {
986 #[inline]
987 fn from(t: Vec3A) -> Self {
988 t.0
989 }
990 }
991
992 impl From<v128> for Vec3A {
993 #[inline]
994 fn from(t: v128) -> Self {
995 Self(t)
996 }
997 }
998
999 impl From<[f32; 3]> for Vec3A {
1000 #[inline]
1001 fn from(a: [f32; 3]) -> Self {
1002 Self::new(a[0], a[1], a[2])
1003 }
1004 }
1005
1006 impl From<Vec3A> for [f32; 3] {
1007 #[inline]
1008 fn from(v: Vec3A) -> Self {
1009 unsafe { *(&v.0 as *const v128 as *const Self) }
1010 }
1011 }
1012
1013 impl From<(f32, f32, f32)> for Vec3A {
1014 #[inline]
1015 fn from(t: (f32, f32, f32)) -> Self {
1016 Self::new(t.0, t.1, t.2)
1017 }
1018 }
1019
1020 impl From<Vec3A> for (f32, f32, f32) {
1021 #[inline]
1022 fn from(v: Vec3A) -> Self {
1023 unsafe { *(&v.0 as *const v128 as *const Self) }
1024 }
1025 }
1026
1027 impl From<Vec3> for Vec3A {
1028 #[inline]
1029 fn from(v: Vec3) -> Self {
1030 Self::new(v.x, v.y, v.z)
1031 }
1032 }
1033
1034 impl From<Vec4> for Vec3A {
1035 /// Creates a `Vec3A` from the `x`, `y` and `z` elements of `self` discarding `w`.
1036 ///
1037 /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
1038 #[inline]
1039 fn from(v: Vec4) -> Self {
1040 Self(v.0)
1041 }
1042 }
1043
1044 impl From<Vec3A> for Vec3 {
1045 #[inline]
1046 fn from(v: Vec3A) -> Self {
1047 unsafe { *(&v.0 as *const v128 as *const Self) }
1048 }
1049 }
1050
1051 impl From<(Vec2, f32)> for Vec3A {
1052 #[inline]
1053 fn from((v, z): (Vec2, f32)) -> Self {
1054 Self::new(v.x, v.y, z)
1055 }
1056 }
1057
1058 impl Deref for Vec3A {
1059 type Target = crate::deref::Vec3<f32>;
1060 #[inline]
1061 fn deref(&self) -> &Self::Target {
1062 unsafe { &*(self as *const Self).cast() }
1063 }
1064 }
1065
1066 impl DerefMut for Vec3A {
1067 #[inline]
1068 fn deref_mut(&mut self) -> &mut Self::Target {
1069 unsafe { &mut *(self as *mut Self).cast() }
1070 }
1071 }