Debian Codegraph @

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