Debian Codegraph @

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

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