Debian Codegraph @

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

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