Debian Codegraph @

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

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