Debian Codegraph @

debian/missing-sources/glam-0.22.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 bitmask with the lowest 3 bits set to the sign bits from the elements of `self`.
296 ///
297 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
298 /// into the first lowest bit, element `y` into the second, etc.
299 #[inline]
300 pub fn is_negative_bitmask(self) -> u32 {
301 (self.0.is_sign_negative().to_bitmask() & 0x7) as u32
302 }
303
304 /// Returns `true` if, and only if, all elements are finite. If any element is either
305 /// `NaN`, positive or negative infinity, this will return `false`.
306 #[inline]
307 pub fn is_finite(self) -> bool {
308 f32x4::is_finite(self.0)
309 .bitor(mask32x4::from_array([false, false, false, true]))
310 .all()
311 }
312
313 /// Returns `true` if any elements are `NaN`.
314 #[inline]
315 pub fn is_nan(self) -> bool {
316 self.is_nan_mask().any()
317 }
318
319 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
320 ///
321 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
322 #[inline]
323 pub fn is_nan_mask(self) -> BVec3A {
324 BVec3A(f32x4::is_nan(self.0))
325 }
326
327 /// Computes the length of `self`.
328 #[doc(alias = "magnitude")]
329 #[inline]
330 pub fn length(self) -> f32 {
331 let dot = dot3_in_x(self.0, self.0);
332 dot.sqrt()[0]
333 }
334
335 /// Computes the squared length of `self`.
336 ///
337 /// This is faster than `length()` as it avoids a square root operation.
338 #[doc(alias = "magnitude2")]
339 #[inline]
340 pub fn length_squared(self) -> f32 {
341 self.dot(self)
342 }
343
344 /// Computes `1.0 / length()`.
345 ///
346 /// For valid results, `self` must _not_ be of length zero.
347 #[inline]
348 pub fn length_recip(self) -> f32 {
349 let dot = dot3_in_x(self.0, self.0);
350 dot.sqrt().recip()[0]
351 }
352
353 /// Computes the Euclidean distance between two points in space.
354 #[inline]
355 pub fn distance(self, rhs: Self) -> f32 {
356 (self - rhs).length()
357 }
358
359 /// Compute the squared euclidean distance between two points in space.
360 #[inline]
361 pub fn distance_squared(self, rhs: Self) -> f32 {
362 (self - rhs).length_squared()
363 }
364
365 /// Returns `self` normalized to length 1.0.
366 ///
367 /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
368 ///
369 /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`].
370 ///
371 /// Panics
372 ///
373 /// Will panic if `self` is zero length when `glam_assert` is enabled.
374 #[must_use]
375 #[inline]
376 pub fn normalize(self) -> Self {
377 let length = dot3_into_f32x4(self.0, self.0).sqrt();
378 #[allow(clippy::let_and_return)]
379 let normalized = Self(self.0 / length);
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(self.0.round())
493 }
494
495 /// Returns a vector containing the largest integer less than or equal to a number for each
496 /// element of `self`.
497 #[inline]
498 pub fn floor(self) -> Self {
499 Self(self.0.floor())
500 }
501
502 /// Returns a vector containing the smallest integer greater than or equal to a number for
503 /// each element of `self`.
504 #[inline]
505 pub fn ceil(self) -> Self {
506 Self(self.0.ceil())
507 }
508
509 /// Returns a vector containing the fractional part of the vector, e.g. `self -
510 /// self.floor()`.
511 ///
512 /// Note that this is fast but not precise for large numbers.
513 #[inline]
514 pub fn fract(self) -> Self {
515 self - self.floor()
516 }
517
518 /// Returns a vector containing `e^self` (the exponential function) for each element of
519 /// `self`.
520 #[inline]
521 pub fn exp(self) -> Self {
522 Self::new(self.x.exp(), self.y.exp(), self.z.exp())
523 }
524
525 /// Returns a vector containing each element of `self` raised to the power of `n`.
526 #[inline]
527 pub fn powf(self, n: f32) -> Self {
528 Self::new(self.x.powf(n), self.y.powf(n), self.z.powf(n))
529 }
530
531 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
532 #[inline]
533 pub fn recip(self) -> Self {
534 Self(self.0.recip())
535 }
536
537 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
538 ///
539 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
540 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
541 /// extrapolated.
542 #[doc(alias = "mix")]
543 #[inline]
544 pub fn lerp(self, rhs: Self, s: f32) -> Self {
545 self + ((rhs - self) * s)
546 }
547
548 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
549 /// less than or equal to `max_abs_diff`.
550 ///
551 /// This can be used to compare if two vectors contain similar elements. It works best when
552 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
553 /// the values being compared against.
554 ///
555 /// For more see
556 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
557 #[inline]
558 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
559 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
560 }
561
562 /// Returns a vector with a length no less than `min` and no more than `max`
563 ///
564 /// # Panics
565 ///
566 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
567 #[inline]
568 pub fn clamp_length(self, min: f32, max: f32) -> Self {
569 glam_assert!(min <= max);
570 let length_sq = self.length_squared();
571 if length_sq < min * min {
572 self * (length_sq.sqrt().recip() * min)
573 } else if length_sq > max * max {
574 self * (length_sq.sqrt().recip() * max)
575 } else {
576 self
577 }
578 }
579
580 /// Returns a vector with a length no more than `max`
581 pub fn clamp_length_max(self, max: f32) -> Self {
582 let length_sq = self.length_squared();
583 if length_sq > max * max {
584 self * (length_sq.sqrt().recip() * max)
585 } else {
586 self
587 }
588 }
589
590 /// Returns a vector with a length no less than `min`
591 pub fn clamp_length_min(self, min: f32) -> Self {
592 let length_sq = self.length_squared();
593 if length_sq < min * min {
594 self * (length_sq.sqrt().recip() * min)
595 } else {
596 self
597 }
598 }
599
600 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
601 /// error, yielding a more accurate result than an unfused multiply-add.
602 ///
603 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
604 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
605 /// and will be heavily dependant on designing algorithms with specific target hardware in
606 /// mind.
607 #[inline]
608 pub fn mul_add(self, a: Self, b: Self) -> Self {
609 Self(self.0.mul_add(a.0, b.0))
610 }
611
612 /// Returns the angle (in radians) between two vectors.
613 ///
614 /// The input vectors do not need to be unit length however they must be non-zero.
615 #[inline]
616 pub fn angle_between(self, rhs: Self) -> f32 {
617 use crate::FloatEx;
618 self.dot(rhs)
619 .div(self.length_squared().mul(rhs.length_squared()).sqrt())
620 .acos_approx()
621 }
622
623 /// Returns some vector that is orthogonal to the given one.
624 ///
625 /// The input vector must be finite and non-zero.
626 ///
627 /// The output vector is not necessarily unit-length.
628 /// For that use [`Self::any_orthonormal_vector`] instead.
629 #[inline]
630 pub fn any_orthogonal_vector(&self) -> Self {
631 // This can probably be optimized
632 if self.x.abs() > self.y.abs() {
633 Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
634 } else {
635 Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
636 }
637 }
638
639 /// Returns any unit-length vector that is orthogonal to the given one.
640 /// The input vector must be finite and non-zero.
641 ///
642 /// # Panics
643 ///
644 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
645 #[inline]
646 pub fn any_orthonormal_vector(&self) -> Self {
647 glam_assert!(self.is_normalized());
648 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
649 #[cfg(feature = "std")]
650 let sign = (1.0_f32).copysign(self.z);
651 #[cfg(not(feature = "std"))]
652 let sign = self.z.signum();
653 let a = -1.0 / (sign + self.z);
654 let b = self.x * self.y * a;
655 Self::new(b, sign + self.y * self.y * a, -self.y)
656 }
657
658 /// Given a unit-length vector return two other vectors that together form an orthonormal
659 /// basis. That is, all three vectors are orthogonal to each other and are normalized.
660 ///
661 /// # Panics
662 ///
663 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
664 #[inline]
665 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
666 glam_assert!(self.is_normalized());
667 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
668 #[cfg(feature = "std")]
669 let sign = (1.0_f32).copysign(self.z);
670 #[cfg(not(feature = "std"))]
671 let sign = self.z.signum();
672 let a = -1.0 / (sign + self.z);
673 let b = self.x * self.y * a;
674 (
675 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
676 Self::new(b, sign + self.y * self.y * a, -self.y),
677 )
678 }
679
680 /// Casts all elements of `self` to `f64`.
681 #[inline]
682 pub fn as_dvec3(&self) -> crate::DVec3 {
683 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
684 }
685
686 /// Casts all elements of `self` to `i32`.
687 #[inline]
688 pub fn as_ivec3(&self) -> crate::IVec3 {
689 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
690 }
691
692 /// Casts all elements of `self` to `u32`.
693 #[inline]
694 pub fn as_uvec3(&self) -> crate::UVec3 {
695 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
696 }
697 }
698
699 impl Default for Vec3A {
700 #[inline(always)]
701 fn default() -> Self {
702 Self::ZERO
703 }
704 }
705
706 impl PartialEq for Vec3A {
707 #[inline]
708 fn eq(&self, rhs: &Self) -> bool {
709 self.cmpeq(*rhs).all()
710 }
711 }
712
713 impl Div<Vec3A> for Vec3A {
714 type Output = Self;
715 #[inline]
716 fn div(self, rhs: Self) -> Self {
717 Self(self.0 / rhs.0)
718 }
719 }
720
721 impl DivAssign<Vec3A> for Vec3A {
722 #[inline]
723 fn div_assign(&mut self, rhs: Self) {
724 self.0 /= rhs.0;
725 }
726 }
727
728 impl Div<f32> for Vec3A {
729 type Output = Self;
730 #[inline]
731 fn div(self, rhs: f32) -> Self {
732 Self(self.0 / f32x4::splat(rhs))
733 }
734 }
735
736 impl DivAssign<f32> for Vec3A {
737 #[inline]
738 fn div_assign(&mut self, rhs: f32) {
739 self.0 /= f32x4::splat(rhs);
740 }
741 }
742
743 impl Div<Vec3A> for f32 {
744 type Output = Vec3A;
745 #[inline]
746 fn div(self, rhs: Vec3A) -> Vec3A {
747 Vec3A(f32x4::splat(self) / rhs.0)
748 }
749 }
750
751 impl Mul<Vec3A> for Vec3A {
752 type Output = Self;
753 #[inline]
754 fn mul(self, rhs: Self) -> Self {
755 Self(self.0 * rhs.0)
756 }
757 }
758
759 impl MulAssign<Vec3A> for Vec3A {
760 #[inline]
761 fn mul_assign(&mut self, rhs: Self) {
762 self.0 *= rhs.0;
763 }
764 }
765
766 impl Mul<f32> for Vec3A {
767 type Output = Self;
768 #[inline]
769 fn mul(self, rhs: f32) -> Self {
770 Self(self.0 * f32x4::splat(rhs))
771 }
772 }
773
774 impl MulAssign<f32> for Vec3A {
775 #[inline]
776 fn mul_assign(&mut self, rhs: f32) {
777 self.0 *= f32x4::splat(rhs);
778 }
779 }
780
781 impl Mul<Vec3A> for f32 {
782 type Output = Vec3A;
783 #[inline]
784 fn mul(self, rhs: Vec3A) -> Vec3A {
785 Vec3A(f32x4::splat(self) * rhs.0)
786 }
787 }
788
789 impl Add<Vec3A> for Vec3A {
790 type Output = Self;
791 #[inline]
792 fn add(self, rhs: Self) -> Self {
793 Self(self.0 + rhs.0)
794 }
795 }
796
797 impl AddAssign<Vec3A> for Vec3A {
798 #[inline]
799 fn add_assign(&mut self, rhs: Self) {
800 self.0 += rhs.0;
801 }
802 }
803
804 impl Add<f32> for Vec3A {
805 type Output = Self;
806 #[inline]
807 fn add(self, rhs: f32) -> Self {
808 Self(self.0 + f32x4::splat(rhs))
809 }
810 }
811
812 impl AddAssign<f32> for Vec3A {
813 #[inline]
814 fn add_assign(&mut self, rhs: f32) {
815 self.0 += f32x4::splat(rhs);
816 }
817 }
818
819 impl Add<Vec3A> for f32 {
820 type Output = Vec3A;
821 #[inline]
822 fn add(self, rhs: Vec3A) -> Vec3A {
823 Vec3A(f32x4::splat(self) + rhs.0)
824 }
825 }
826
827 impl Sub<Vec3A> for Vec3A {
828 type Output = Self;
829 #[inline]
830 fn sub(self, rhs: Self) -> Self {
831 Self(self.0 - rhs.0)
832 }
833 }
834
835 impl SubAssign<Vec3A> for Vec3A {
836 #[inline]
837 fn sub_assign(&mut self, rhs: Vec3A) {
838 self.0 -= rhs.0;
839 }
840 }
841
842 impl Sub<f32> for Vec3A {
843 type Output = Self;
844 #[inline]
845 fn sub(self, rhs: f32) -> Self {
846 Self(self.0 - f32x4::splat(rhs))
847 }
848 }
849
850 impl SubAssign<f32> for Vec3A {
851 #[inline]
852 fn sub_assign(&mut self, rhs: f32) {
853 self.0 -= f32x4::splat(rhs);
854 }
855 }
856
857 impl Sub<Vec3A> for f32 {
858 type Output = Vec3A;
859 #[inline]
860 fn sub(self, rhs: Vec3A) -> Vec3A {
861 Vec3A(f32x4::splat(self) - rhs.0)
862 }
863 }
864
865 impl Rem<Vec3A> for Vec3A {
866 type Output = Self;
867 #[inline]
868 fn rem(self, rhs: Self) -> Self {
869 Self(self.0 % rhs.0)
870 }
871 }
872
873 impl RemAssign<Vec3A> for Vec3A {
874 #[inline]
875 fn rem_assign(&mut self, rhs: Self) {
876 self.0 %= rhs.0;
877 }
878 }
879
880 impl Rem<f32> for Vec3A {
881 type Output = Self;
882 #[inline]
883 fn rem(self, rhs: f32) -> Self {
884 self.rem(Self::splat(rhs))
885 }
886 }
887
888 impl RemAssign<f32> for Vec3A {
889 #[inline]
890 fn rem_assign(&mut self, rhs: f32) {
891 self.0 %= f32x4::splat(rhs);
892 }
893 }
894
895 impl Rem<Vec3A> for f32 {
896 type Output = Vec3A;
897 #[inline]
898 fn rem(self, rhs: Vec3A) -> Vec3A {
899 Vec3A::splat(self).rem(rhs)
900 }
901 }
902
903 #[cfg(not(target_arch = "spirv"))]
904 impl AsRef<[f32; 3]> for Vec3A {
905 #[inline]
906 fn as_ref(&self) -> &[f32; 3] {
907 unsafe { &*(self as *const Vec3A as *const [f32; 3]) }
908 }
909 }
910
911 #[cfg(not(target_arch = "spirv"))]
912 impl AsMut<[f32; 3]> for Vec3A {
913 #[inline]
914 fn as_mut(&mut self) -> &mut [f32; 3] {
915 unsafe { &mut *(self as *mut Vec3A as *mut [f32; 3]) }
916 }
917 }
918
919 impl Sum for Vec3A {
920 #[inline]
921 fn sum<I>(iter: I) -> Self
922 where
923 I: Iterator<Item = Self>,
924 {
925 iter.fold(Self::ZERO, Self::add)
926 }
927 }
928
929 impl<'a> Sum<&'a Self> for Vec3A {
930 #[inline]
931 fn sum<I>(iter: I) -> Self
932 where
933 I: Iterator<Item = &'a Self>,
934 {
935 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
936 }
937 }
938
939 impl Product for Vec3A {
940 #[inline]
941 fn product<I>(iter: I) -> Self
942 where
943 I: Iterator<Item = Self>,
944 {
945 iter.fold(Self::ONE, Self::mul)
946 }
947 }
948
949 impl<'a> Product<&'a Self> for Vec3A {
950 #[inline]
951 fn product<I>(iter: I) -> Self
952 where
953 I: Iterator<Item = &'a Self>,
954 {
955 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
956 }
957 }
958
959 impl Neg for Vec3A {
960 type Output = Self;
961 #[inline]
962 fn neg(self) -> Self {
963 Self(-self.0)
964 }
965 }
966
967 impl Index<usize> for Vec3A {
968 type Output = f32;
969 #[inline]
970 fn index(&self, index: usize) -> &Self::Output {
971 &self.0[index]
972 }
973 }
974
975 impl IndexMut<usize> for Vec3A {
976 #[inline]
977 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
978 &mut self.0[index]
979 }
980 }
981
982 #[cfg(not(target_arch = "spirv"))]
983 impl fmt::Display for Vec3A {
984 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
985 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
986 }
987 }
988
989 #[cfg(not(target_arch = "spirv"))]
990 impl fmt::Debug for Vec3A {
991 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
992 fmt.debug_tuple(stringify!(Vec3A))
993 .field(&self.x)
994 .field(&self.y)
995 .field(&self.z)
996 .finish()
997 }
998 }
999
1000 impl From<Vec3A> for f32x4 {
1001 #[inline]
1002 fn from(t: Vec3A) -> Self {
1003 t.0
1004 }
1005 }
1006
1007 impl From<f32x4> for Vec3A {
1008 #[inline]
1009 fn from(t: f32x4) -> Self {
1010 Self(t)
1011 }
1012 }
1013
1014 impl From<[f32; 3]> for Vec3A {
1015 #[inline]
1016 fn from(a: [f32; 3]) -> Self {
1017 Self::new(a[0], a[1], a[2])
1018 }
1019 }
1020
1021 impl From<Vec3A> for [f32; 3] {
1022 #[inline]
1023 fn from(v: Vec3A) -> Self {
1024 unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1025 }
1026 }
1027
1028 impl From<(f32, f32, f32)> for Vec3A {
1029 #[inline]
1030 fn from(t: (f32, f32, f32)) -> Self {
1031 Self::new(t.0, t.1, t.2)
1032 }
1033 }
1034
1035 impl From<Vec3A> for (f32, f32, f32) {
1036 #[inline]
1037 fn from(v: Vec3A) -> Self {
1038 unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1039 }
1040 }
1041
1042 impl From<Vec3> for Vec3A {
1043 #[inline]
1044 fn from(v: Vec3) -> Self {
1045 Self::new(v.x, v.y, v.z)
1046 }
1047 }
1048
1049 impl From<Vec4> for Vec3A {
1050 /// Creates a `Vec3A` from the `x`, `y` and `z` elements of `self` discarding `w`.
1051 ///
1052 /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
1053 #[inline]
1054 fn from(v: Vec4) -> Self {
1055 Self(v.0)
1056 }
1057 }
1058
1059 impl From<Vec3A> for Vec3 {
1060 #[inline]
1061 fn from(v: Vec3A) -> Self {
1062 unsafe { *(v.0.to_array().as_ptr() as *const Self) }
1063 }
1064 }
1065
1066 impl From<(Vec2, f32)> for Vec3A {
1067 #[inline]
1068 fn from((v, z): (Vec2, f32)) -> Self {
1069 Self::new(v.x, v.y, z)
1070 }
1071 }
1072
1073 impl Deref for Vec3A {
1074 type Target = crate::deref::Vec3<f32>;
1075 #[inline]
1076 fn deref(&self) -> &Self::Target {
1077 unsafe { &*(self as *const Self).cast() }
1078 }
1079 }
1080
1081 impl DerefMut for Vec3A {
1082 #[inline]
1083 fn deref_mut(&mut self) -> &mut Self::Target {
1084 unsafe { &mut *(self as *mut Self).cast() }
1085 }
1086 }