Debian Codegraph @

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