Debian Codegraph @

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