Debian Codegraph @

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