Debian Codegraph @

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