Debian Codegraph @

debian/missing-sources/glam-0.23.0/src/f32/coresimd/vec3a.rs

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