Debian Codegraph @

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