Debian Codegraph @

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