Debian Codegraph @

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