Debian Codegraph @

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