Debian Codegraph @

debian/missing-sources/glam/src/f32/wasm32/vec3a.rs

raw
1 // Generated from vec.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{f32::math, wasm32::*, BVec3, BVec3A, FloatExt, Quat, Vec2, Vec3, Vec4};
4
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::{f32, ops::*};
8
9 use core::arch::wasm32::*;
10
11 #[cfg(feature = "zerocopy")]
12 use zerocopy_derive::*;
13
14 /// Creates a 3-dimensional vector.
15 #[inline(always)]
16 #[must_use]
17 pub const fn vec3a(x: f32, y: f32, z: f32) -> Vec3A {
18 Vec3A::new(x, y, z)
19 }
20
21 /// A 3-dimensional vector.
22 ///
23 /// SIMD vector types are used for storage on supported platforms for better
24 /// performance than the [`Vec3`] type.
25 ///
26 /// It is possible to convert between [`Vec3`] and [`Vec3A`] types using [`From`]
27 /// or [`Into`] trait implementations.
28 ///
29 /// This type is 16 byte aligned.
30 #[derive(Clone, Copy)]
31 #[cfg_attr(feature = "bytemuck", derive(bytemuck::Pod, bytemuck::Zeroable))]
32 #[cfg_attr(
33 feature = "zerocopy",
34 derive(FromBytes, Immutable, IntoBytes, KnownLayout)
35 )]
36 #[repr(transparent)]
37 pub struct Vec3A(pub(crate) v128);
38
39 impl Vec3A {
40 /// All zeroes.
41 pub const ZERO: Self = Self::splat(0.0);
42
43 /// All ones.
44 pub const ONE: Self = Self::splat(1.0);
45
46 /// All negative ones.
47 pub const NEG_ONE: Self = Self::splat(-1.0);
48
49 /// All `f32::MIN`.
50 pub const MIN: Self = Self::splat(f32::MIN);
51
52 /// All `f32::MAX`.
53 pub const MAX: Self = Self::splat(f32::MAX);
54
55 /// All `f32::NAN`.
56 pub const NAN: Self = Self::splat(f32::NAN);
57
58 /// All `f32::INFINITY`.
59 pub const INFINITY: Self = Self::splat(f32::INFINITY);
60
61 /// All `f32::NEG_INFINITY`.
62 pub const NEG_INFINITY: Self = Self::splat(f32::NEG_INFINITY);
63
64 /// A unit vector pointing along the positive X axis.
65 pub const X: Self = Self::new(1.0, 0.0, 0.0);
66
67 /// A unit vector pointing along the positive Y axis.
68 pub const Y: Self = Self::new(0.0, 1.0, 0.0);
69
70 /// A unit vector pointing along the positive Z axis.
71 pub const Z: Self = Self::new(0.0, 0.0, 1.0);
72
73 /// A unit vector pointing along the negative X axis.
74 pub const NEG_X: Self = Self::new(-1.0, 0.0, 0.0);
75
76 /// A unit vector pointing along the negative Y axis.
77 pub const NEG_Y: Self = Self::new(0.0, -1.0, 0.0);
78
79 /// A unit vector pointing along the negative Z axis.
80 pub const NEG_Z: Self = Self::new(0.0, 0.0, -1.0);
81
82 /// The unit axes.
83 pub const AXES: [Self; 3] = [Self::X, Self::Y, Self::Z];
84
85 /// Vec3A uses Rust Portable SIMD
86 pub const USES_CORE_SIMD: bool = false;
87 /// Vec3A uses Arm NEON
88 pub const USES_NEON: bool = false;
89 /// Vec3A uses scalar math
90 pub const USES_SCALAR_MATH: bool = false;
91 /// Vec3A uses Intel SSE2
92 pub const USES_SSE2: bool = false;
93 /// Vec3A uses WebAssembly 128-bit SIMD
94 pub const USES_WASM32_SIMD: bool = true;
95
96 /// Creates a new vector.
97 #[inline(always)]
98 #[must_use]
99 pub const fn new(x: f32, y: f32, z: f32) -> Self {
100 Self(f32x4(x, y, z, z))
101 }
102
103 /// Creates a vector with all elements set to `v`.
104 #[inline]
105 #[must_use]
106 pub const fn splat(v: f32) -> Self {
107 Self(f32x4(v, v, v, v))
108 }
109
110 /// Returns a vector containing each element of `self` modified by a mapping function `f`.
111 #[inline]
112 #[must_use]
113 pub fn map<F>(self, f: F) -> Self
114 where
115 F: Fn(f32) -> f32,
116 {
117 Self::new(f(self.x), f(self.y), f(self.z))
118 }
119
120 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
121 /// for each element of `self`.
122 ///
123 /// A true element in the mask uses the corresponding element from `if_true`, and false
124 /// uses the element from `if_false`.
125 #[inline]
126 #[must_use]
127 pub fn select(mask: BVec3A, if_true: Self, if_false: Self) -> Self {
128 Self(v128_bitselect(if_true.0, if_false.0, mask.0))
129 }
130
131 /// Creates a new vector from an array.
132 #[inline]
133 #[must_use]
134 pub const fn from_array(a: [f32; 3]) -> Self {
135 Self::new(a[0], a[1], a[2])
136 }
137
138 /// Converts `self` to `[x, y, z]`
139 #[inline]
140 #[must_use]
141 pub const fn to_array(&self) -> [f32; 3] {
142 unsafe { *(self as *const Self as *const [f32; 3]) }
143 }
144
145 /// Creates a vector from the first 3 values in `slice`.
146 ///
147 /// # Panics
148 ///
149 /// Panics if `slice` is less than 3 elements long.
150 #[inline]
151 #[must_use]
152 pub const fn from_slice(slice: &[f32]) -> Self {
153 assert!(slice.len() >= 3);
154 Self::new(slice[0], slice[1], slice[2])
155 }
156
157 /// Writes the elements of `self` to the first 3 elements in `slice`.
158 ///
159 /// # Panics
160 ///
161 /// Panics if `slice` is less than 3 elements long.
162 #[inline]
163 pub fn write_to_slice(self, slice: &mut [f32]) {
164 slice[..3].copy_from_slice(&self.to_array());
165 }
166
167 /// Creates a [`Vec3A`] from the `x`, `y` and `z` elements of `self` discarding `w`.
168 ///
169 /// On architectures where SIMD is supported such as SSE2 on `x86_64` this conversion is a noop.
170 #[inline]
171 #[must_use]
172 pub fn from_vec4(v: Vec4) -> Self {
173 Self(v.0)
174 }
175
176 /// Creates a 4D vector from `self` and the given `w` value.
177 #[inline]
178 #[must_use]
179 pub fn extend(self, w: f32) -> Vec4 {
180 Vec4::new(self.x, self.y, self.z, w)
181 }
182
183 /// Creates a 2D vector from the `x` and `y` elements of `self`, discarding `z`.
184 ///
185 /// Truncation may also be performed by using [`self.xy()`][crate::swizzles::Vec3Swizzles::xy()].
186 #[inline]
187 #[must_use]
188 pub fn truncate(self) -> Vec2 {
189 use crate::swizzles::Vec3Swizzles;
190 self.xy()
191 }
192
193 /// Projects a homogeneous coordinate to 3D space by performing perspective divide.
194 ///
195 /// # Panics
196 ///
197 /// Will panic if `v.w` is `0` when `glam_assert` is enabled.
198 #[inline]
199 #[must_use]
200 pub fn from_homogeneous(v: Vec4) -> Self {
201 glam_assert!(v.w != 0.0);
202 Self::from_vec4(v) / v.w
203 }
204
205 /// Creates a homogeneous coordinate from `self`, equivalent to `self.extend(1.0)`.
206 #[inline]
207 #[must_use]
208 pub fn to_homogeneous(self) -> Vec4 {
209 self.extend(1.0)
210 }
211
212 // Converts `self` to a `Vec3`.
213 #[inline]
214 #[must_use]
215 pub fn to_vec3(self) -> Vec3 {
216 Vec3::from(self)
217 }
218
219 /// Creates a 3D vector from `self` with the given value of `x`.
220 #[inline]
221 #[must_use]
222 pub fn with_x(mut self, x: f32) -> Self {
223 self.x = x;
224 self
225 }
226
227 /// Creates a 3D vector from `self` with the given value of `y`.
228 #[inline]
229 #[must_use]
230 pub fn with_y(mut self, y: f32) -> Self {
231 self.y = y;
232 self
233 }
234
235 /// Creates a 3D vector from `self` with the given value of `z`.
236 #[inline]
237 #[must_use]
238 pub fn with_z(mut self, z: f32) -> Self {
239 self.z = z;
240 self
241 }
242
243 /// Computes the dot product of `self` and `rhs`.
244 #[inline]
245 #[must_use]
246 pub fn dot(self, rhs: Self) -> f32 {
247 dot3(self.0, rhs.0)
248 }
249
250 /// Returns a vector where every component is the dot product of `self` and `rhs`.
251 #[inline]
252 #[must_use]
253 pub fn dot_into_vec(self, rhs: Self) -> Self {
254 Self(dot3_into_v128(self.0, rhs.0))
255 }
256
257 /// Computes the cross product of `self` and `rhs`.
258 #[inline]
259 #[must_use]
260 pub fn cross(self, rhs: Self) -> Self {
261 let lhszxy = i32x4_shuffle::<2, 0, 1, 1>(self.0, self.0);
262 let rhszxy = i32x4_shuffle::<2, 0, 1, 1>(rhs.0, rhs.0);
263 let lhszxy_rhs = f32x4_mul(lhszxy, rhs.0);
264 let rhszxy_lhs = f32x4_mul(rhszxy, self.0);
265 let sub = f32x4_sub(lhszxy_rhs, rhszxy_lhs);
266 Self(i32x4_shuffle::<2, 0, 1, 1>(sub, sub))
267 }
268
269 /// Returns a vector containing the minimum values for each element of `self` and `rhs`.
270 ///
271 /// In other words this computes `[min(x, rhs.x), min(self.y, rhs.y), ..]`.
272 ///
273 /// NaN propogation does not follow IEEE 754-2008 semantics for minNum and may differ on
274 /// different SIMD architectures.
275 #[inline]
276 #[must_use]
277 pub fn min(self, rhs: Self) -> Self {
278 Self(f32x4_pmin(self.0, rhs.0))
279 }
280
281 /// Returns a vector containing the maximum values for each element of `self` and `rhs`.
282 ///
283 /// In other words this computes `[max(self.x, rhs.x), max(self.y, rhs.y), ..]`.
284 ///
285 /// NaN propogation does not follow IEEE 754-2008 semantics for maxNum and may differ on
286 /// different SIMD architectures.
287 #[inline]
288 #[must_use]
289 pub fn max(self, rhs: Self) -> Self {
290 Self(f32x4_pmax(self.0, rhs.0))
291 }
292
293 /// Component-wise clamping of values, similar to [`f32::clamp`].
294 ///
295 /// Each element in `min` must be less-or-equal to the corresponding element in `max`.
296 ///
297 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
298 /// different SIMD architectures.
299 ///
300 /// # Panics
301 ///
302 /// Will panic if `min` is greater than `max` when `glam_assert` is enabled.
303 #[inline]
304 #[must_use]
305 pub fn clamp(self, min: Self, max: Self) -> Self {
306 glam_assert!(min.cmple(max).all(), "clamp: expected min <= max");
307 self.max(min).min(max)
308 }
309
310 /// Returns the horizontal minimum of `self`.
311 ///
312 /// In other words this computes `min(x, y, ..)`.
313 ///
314 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
315 /// different SIMD architectures.
316 #[inline]
317 #[must_use]
318 pub fn min_element(self) -> f32 {
319 let v = self.0;
320 let v = f32x4_pmin(v, i32x4_shuffle::<2, 2, 1, 1>(v, v));
321 let v = f32x4_pmin(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
322 f32x4_extract_lane::<0>(v)
323 }
324
325 /// Returns the horizontal maximum of `self`.
326 ///
327 /// In other words this computes `max(x, y, ..)`.
328 ///
329 /// NaN propogation does not follow IEEE 754-2008 semantics and may differ on
330 /// different SIMD architectures.
331 #[inline]
332 #[must_use]
333 pub fn max_element(self) -> f32 {
334 let v = self.0;
335 let v = f32x4_pmax(v, i32x4_shuffle::<2, 2, 0, 0>(v, v));
336 let v = f32x4_pmax(v, i32x4_shuffle::<1, 0, 0, 0>(v, v));
337 f32x4_extract_lane::<0>(v)
338 }
339
340 /// Returns the index of the first minimum element of `self`.
341 #[doc(alias = "argmin")]
342 #[inline]
343 #[must_use]
344 pub fn min_position(self) -> usize {
345 let mut min = self.x;
346 let mut index = 0;
347 if self.y < min {
348 min = self.y;
349 index = 1;
350 }
351 if self.z < min {
352 index = 2;
353 }
354 index
355 }
356
357 /// Returns the index of the first maximum element of `self`.
358 #[doc(alias = "argmax")]
359 #[inline]
360 #[must_use]
361 pub fn max_position(self) -> usize {
362 let mut max = self.x;
363 let mut index = 0;
364 if self.y > max {
365 max = self.y;
366 index = 1;
367 }
368 if self.z > max {
369 index = 2;
370 }
371 index
372 }
373
374 /// Returns the sum of all elements of `self`.
375 ///
376 /// In other words, this computes `self.x + self.y + ..`.
377 #[inline]
378 #[must_use]
379 pub fn element_sum(self) -> f32 {
380 let v = self.0;
381 let v = f32x4_add(v, i32x4_shuffle::<1, 0, 4, 0>(v, Self::ZERO.0));
382 let v = f32x4_add(v, i32x4_shuffle::<2, 0, 0, 0>(v, v));
383 f32x4_extract_lane::<0>(v)
384 }
385
386 /// Returns the product of all elements of `self`.
387 ///
388 /// In other words, this computes `self.x * self.y * ..`.
389 #[inline]
390 #[must_use]
391 pub fn element_product(self) -> f32 {
392 let v = self.0;
393 let v = f32x4_mul(v, i32x4_shuffle::<1, 0, 4, 0>(v, Self::ONE.0));
394 let v = f32x4_mul(v, i32x4_shuffle::<2, 0, 0, 0>(v, v));
395 f32x4_extract_lane::<0>(v)
396 }
397
398 /// Returns a vector mask containing the result of a `==` comparison for each element of
399 /// `self` and `rhs`.
400 ///
401 /// In other words, this computes `[self.x == rhs.x, self.y == rhs.y, ..]` for all
402 /// elements.
403 #[inline]
404 #[must_use]
405 pub fn cmpeq(self, rhs: Self) -> BVec3A {
406 BVec3A(f32x4_eq(self.0, rhs.0))
407 }
408
409 /// Returns a vector mask containing the result of a `!=` comparison for each element of
410 /// `self` and `rhs`.
411 ///
412 /// In other words this computes `[self.x != rhs.x, self.y != rhs.y, ..]` for all
413 /// elements.
414 #[inline]
415 #[must_use]
416 pub fn cmpne(self, rhs: Self) -> BVec3A {
417 BVec3A(f32x4_ne(self.0, rhs.0))
418 }
419
420 /// Returns a vector mask containing the result of a `>=` comparison for each element of
421 /// `self` and `rhs`.
422 ///
423 /// In other words this computes `[self.x >= rhs.x, self.y >= rhs.y, ..]` for all
424 /// elements.
425 #[inline]
426 #[must_use]
427 pub fn cmpge(self, rhs: Self) -> BVec3A {
428 BVec3A(f32x4_ge(self.0, rhs.0))
429 }
430
431 /// Returns a vector mask containing the result of a `>` comparison for each element of
432 /// `self` and `rhs`.
433 ///
434 /// In other words this computes `[self.x > rhs.x, self.y > rhs.y, ..]` for all
435 /// elements.
436 #[inline]
437 #[must_use]
438 pub fn cmpgt(self, rhs: Self) -> BVec3A {
439 BVec3A(f32x4_gt(self.0, rhs.0))
440 }
441
442 /// Returns a vector mask containing the result of a `<=` comparison for each element of
443 /// `self` and `rhs`.
444 ///
445 /// In other words this computes `[self.x <= rhs.x, self.y <= rhs.y, ..]` for all
446 /// elements.
447 #[inline]
448 #[must_use]
449 pub fn cmple(self, rhs: Self) -> BVec3A {
450 BVec3A(f32x4_le(self.0, rhs.0))
451 }
452
453 /// Returns a vector mask containing the result of a `<` comparison for each element of
454 /// `self` and `rhs`.
455 ///
456 /// In other words this computes `[self.x < rhs.x, self.y < rhs.y, ..]` for all
457 /// elements.
458 #[inline]
459 #[must_use]
460 pub fn cmplt(self, rhs: Self) -> BVec3A {
461 BVec3A(f32x4_lt(self.0, rhs.0))
462 }
463
464 /// Returns a vector containing the absolute value of each element of `self`.
465 #[inline]
466 #[must_use]
467 pub fn abs(self) -> Self {
468 Self(f32x4_abs(self.0))
469 }
470
471 /// Returns a vector with elements representing the sign of `self`.
472 ///
473 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
474 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
475 /// - `NAN` if the number is `NAN`
476 #[inline]
477 #[must_use]
478 pub fn signum(self) -> Self {
479 let result = Self(v128_or(v128_and(self.0, Self::NEG_ONE.0), Self::ONE.0));
480 let mask = self.is_nan_mask();
481 Self::select(mask, self, result)
482 }
483
484 /// Returns a vector with signs of `rhs` and the magnitudes of `self`.
485 #[inline]
486 #[must_use]
487 pub fn copysign(self, rhs: Self) -> Self {
488 let mask = Self::splat(-0.0);
489 Self(v128_or(
490 v128_and(rhs.0, mask.0),
491 v128_andnot(self.0, mask.0),
492 ))
493 }
494
495 /// Returns a bitmask with the lowest 3 bits set to the sign bits from the elements of `self`.
496 ///
497 /// A negative element results in a `1` bit and a positive element in a `0` bit. Element `x` goes
498 /// into the first lowest bit, element `y` into the second, etc.
499 ///
500 /// An element is negative if it has a negative sign, including -0.0, NaNs with negative sign
501 /// bit and negative infinity.
502 #[inline]
503 #[must_use]
504 pub fn is_negative_bitmask(self) -> u32 {
505 (u32x4_bitmask(self.0) & 0x7) as u32
506 }
507
508 /// Returns `true` if, and only if, all elements are finite. If any element is either
509 /// `NaN`, positive or negative infinity, this will return `false`.
510 #[inline]
511 #[must_use]
512 pub fn is_finite(self) -> bool {
513 self.is_finite_mask().all()
514 }
515
516 /// Performs `is_finite` on each element of self, returning a vector mask of the results.
517 ///
518 /// In other words, this computes `[x.is_finite(), y.is_finite(), ...]`.
519 #[inline]
520 #[must_use]
521 pub fn is_finite_mask(self) -> BVec3A {
522 BVec3A(f32x4_lt(f32x4_abs(self.0), Self::INFINITY.0))
523 }
524
525 /// Returns `true` if any elements are `NaN`.
526 #[inline]
527 #[must_use]
528 pub fn is_nan(self) -> bool {
529 self.is_nan_mask().any()
530 }
531
532 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
533 ///
534 /// In other words, this computes `[x.is_nan(), y.is_nan(), ...]`.
535 #[inline]
536 #[must_use]
537 pub fn is_nan_mask(self) -> BVec3A {
538 BVec3A(f32x4_ne(self.0, self.0))
539 }
540
541 /// Computes the length of `self`.
542 #[doc(alias = "magnitude")]
543 #[inline]
544 #[must_use]
545 pub fn length(self) -> f32 {
546 let dot = dot3_in_x(self.0, self.0);
547 f32x4_extract_lane::<0>(f32x4_sqrt(dot))
548 }
549
550 /// Computes the squared length of `self`.
551 ///
552 /// This is faster than `length()` as it avoids a square root operation.
553 #[doc(alias = "magnitude2")]
554 #[inline]
555 #[must_use]
556 pub fn length_squared(self) -> f32 {
557 self.dot(self)
558 }
559
560 /// Computes `1.0 / length()`.
561 ///
562 /// For valid results, `self` must _not_ be of length zero.
563 #[inline]
564 #[must_use]
565 pub fn length_recip(self) -> f32 {
566 let dot = dot3_in_x(self.0, self.0);
567 f32x4_extract_lane::<0>(f32x4_div(Self::ONE.0, f32x4_sqrt(dot)))
568 }
569
570 /// Computes the Euclidean distance between two points in space.
571 #[inline]
572 #[must_use]
573 pub fn distance(self, rhs: Self) -> f32 {
574 (self - rhs).length()
575 }
576
577 /// Compute the squared euclidean distance between two points in space.
578 #[inline]
579 #[must_use]
580 pub fn distance_squared(self, rhs: Self) -> f32 {
581 (self - rhs).length_squared()
582 }
583
584 /// Returns the element-wise quotient of [Euclidean division] of `self` by `rhs`.
585 #[inline]
586 #[must_use]
587 pub fn div_euclid(self, rhs: Self) -> Self {
588 Self::new(
589 math::div_euclid(self.x, rhs.x),
590 math::div_euclid(self.y, rhs.y),
591 math::div_euclid(self.z, rhs.z),
592 )
593 }
594
595 /// Returns the element-wise remainder of [Euclidean division] of `self` by `rhs`.
596 ///
597 /// [Euclidean division]: f32::rem_euclid
598 #[inline]
599 #[must_use]
600 pub fn rem_euclid(self, rhs: Self) -> Self {
601 Self::new(
602 math::rem_euclid(self.x, rhs.x),
603 math::rem_euclid(self.y, rhs.y),
604 math::rem_euclid(self.z, rhs.z),
605 )
606 }
607
608 /// Returns `self` normalized to length 1.0.
609 ///
610 /// For valid results, `self` must be finite and _not_ of length zero, nor very close to zero.
611 ///
612 /// See also [`Self::try_normalize()`] and [`Self::normalize_or_zero()`].
613 ///
614 /// # Panics
615 ///
616 /// Will panic if the resulting normalized vector is not finite when `glam_assert` is enabled.
617 #[inline]
618 #[must_use]
619 pub fn normalize(self) -> Self {
620 let length = f32x4_sqrt(dot3_into_v128(self.0, self.0));
621 #[allow(clippy::let_and_return)]
622 let normalized = Self(f32x4_div(self.0, length));
623 glam_assert!(normalized.is_finite());
624 normalized
625 }
626
627 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
628 ///
629 /// In particular, if the input is zero (or very close to zero), or non-finite,
630 /// the result of this operation will be `None`.
631 ///
632 /// See also [`Self::normalize_or_zero()`].
633 #[inline]
634 #[must_use]
635 pub fn try_normalize(self) -> Option<Self> {
636 let rcp = self.length_recip();
637 if rcp.is_finite() && rcp > 0.0 {
638 Some(self * rcp)
639 } else {
640 None
641 }
642 }
643
644 /// Returns `self` normalized to length 1.0 if possible, else returns a
645 /// fallback value.
646 ///
647 /// In particular, if the input is zero (or very close to zero), or non-finite,
648 /// the result of this operation will be the fallback value.
649 ///
650 /// See also [`Self::try_normalize()`].
651 #[inline]
652 #[must_use]
653 pub fn normalize_or(self, fallback: Self) -> Self {
654 let rcp = self.length_recip();
655 if rcp.is_finite() && rcp > 0.0 {
656 self * rcp
657 } else {
658 fallback
659 }
660 }
661
662 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
663 ///
664 /// In particular, if the input is zero (or very close to zero), or non-finite,
665 /// the result of this operation will be zero.
666 ///
667 /// See also [`Self::try_normalize()`].
668 #[inline]
669 #[must_use]
670 pub fn normalize_or_zero(self) -> Self {
671 self.normalize_or(Self::ZERO)
672 }
673
674 /// Returns `self` normalized to length 1.0 and the length of `self`.
675 ///
676 /// If `self` is zero length then `(Self::X, 0.0)` is returned.
677 #[inline]
678 #[must_use]
679 pub fn normalize_and_length(self) -> (Self, f32) {
680 let length = self.length();
681 let rcp = 1.0 / length;
682 if rcp.is_finite() && rcp > 0.0 {
683 (self * rcp, length)
684 } else {
685 (Self::X, 0.0)
686 }
687 }
688
689 /// Returns whether `self` is length `1.0` or not.
690 ///
691 /// Uses a precision threshold of approximately `1e-4`.
692 #[inline]
693 #[must_use]
694 pub fn is_normalized(self) -> bool {
695 math::abs(self.length_squared() - 1.0) <= 2e-4
696 }
697
698 /// Returns the vector projection of `self` onto `rhs`.
699 ///
700 /// `rhs` must be of non-zero length.
701 ///
702 /// # Panics
703 ///
704 /// Will panic if `rhs` is zero length when `glam_assert` is enabled.
705 #[inline]
706 #[must_use]
707 pub fn project_onto(self, rhs: Self) -> Self {
708 let other_len_sq_rcp = rhs.dot(rhs).recip();
709 glam_assert!(other_len_sq_rcp.is_finite());
710 rhs * self.dot(rhs) * other_len_sq_rcp
711 }
712
713 /// Returns the vector rejection of `self` from `rhs`.
714 ///
715 /// The vector rejection is the vector perpendicular to the projection of `self` onto
716 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
717 ///
718 /// `rhs` must be of non-zero length.
719 ///
720 /// # Panics
721 ///
722 /// Will panic if `rhs` has a length of zero when `glam_assert` is enabled.
723 #[doc(alias("plane"))]
724 #[inline]
725 #[must_use]
726 pub fn reject_from(self, rhs: Self) -> Self {
727 self - self.project_onto(rhs)
728 }
729
730 /// Returns the vector projection of `self` onto `rhs`.
731 ///
732 /// `rhs` must be normalized.
733 ///
734 /// # Panics
735 ///
736 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
737 #[inline]
738 #[must_use]
739 pub fn project_onto_normalized(self, rhs: Self) -> Self {
740 glam_assert!(rhs.is_normalized());
741 rhs * self.dot(rhs)
742 }
743
744 /// Returns the vector rejection of `self` from `rhs`.
745 ///
746 /// The vector rejection is the vector perpendicular to the projection of `self` onto
747 /// `rhs`, in rhs words the result of `self - self.project_onto(rhs)`.
748 ///
749 /// `rhs` must be normalized.
750 ///
751 /// # Panics
752 ///
753 /// Will panic if `rhs` is not normalized when `glam_assert` is enabled.
754 #[doc(alias("plane"))]
755 #[inline]
756 #[must_use]
757 pub fn reject_from_normalized(self, rhs: Self) -> Self {
758 self - self.project_onto_normalized(rhs)
759 }
760
761 /// Returns a vector containing the nearest integer to a number for each element of `self`.
762 /// Round half-way cases away from 0.0.
763 #[inline]
764 #[must_use]
765 pub fn round(self) -> Self {
766 Self(f32x4_nearest(self.0))
767 }
768
769 /// Returns a vector containing the largest integer less than or equal to a number for each
770 /// element of `self`.
771 #[inline]
772 #[must_use]
773 pub fn floor(self) -> Self {
774 Self(f32x4_floor(self.0))
775 }
776
777 /// Returns a vector containing the smallest integer greater than or equal to a number for
778 /// each element of `self`.
779 #[inline]
780 #[must_use]
781 pub fn ceil(self) -> Self {
782 Self(f32x4_ceil(self.0))
783 }
784
785 /// Returns a vector containing the integer part each element of `self`. This means numbers are
786 /// always truncated towards zero.
787 #[inline]
788 #[must_use]
789 pub fn trunc(self) -> Self {
790 Self(f32x4_trunc(self.0))
791 }
792
793 /// Returns a vector containing the fractional part of the vector as `self - self.trunc()`.
794 ///
795 /// Note that this differs from the GLSL implementation of `fract` which returns
796 /// `self - self.floor()`.
797 ///
798 /// Note that this is fast but not precise for large numbers.
799 #[inline]
800 #[must_use]
801 pub fn fract(self) -> Self {
802 self - self.trunc()
803 }
804
805 /// Returns a vector containing the fractional part of the vector as `self - self.floor()`.
806 ///
807 /// Note that this differs from the Rust implementation of `fract` which returns
808 /// `self - self.trunc()`.
809 ///
810 /// Note that this is fast but not precise for large numbers.
811 #[inline]
812 #[must_use]
813 pub fn fract_gl(self) -> Self {
814 self - self.floor()
815 }
816
817 /// Returns a vector containing `e^self` (the exponential function) for each element of
818 /// `self`.
819 #[inline]
820 #[must_use]
821 pub fn exp(self) -> Self {
822 Self::new(math::exp(self.x), math::exp(self.y), math::exp(self.z))
823 }
824
825 /// Returns a vector containing `2^self` for each element of `self`.
826 #[inline]
827 #[must_use]
828 pub fn exp2(self) -> Self {
829 Self::new(math::exp2(self.x), math::exp2(self.y), math::exp2(self.z))
830 }
831
832 /// Returns a vector containing the natural logarithm for each element of `self`.
833 /// This returns NaN when the element is negative and negative infinity when the element is zero.
834 #[inline]
835 #[must_use]
836 pub fn ln(self) -> Self {
837 Self::new(math::ln(self.x), math::ln(self.y), math::ln(self.z))
838 }
839
840 /// Returns a vector containing the base 2 logarithm for each element of `self`.
841 /// This returns NaN when the element is negative and negative infinity when the element is zero.
842 #[inline]
843 #[must_use]
844 pub fn log2(self) -> Self {
845 Self::new(math::log2(self.x), math::log2(self.y), math::log2(self.z))
846 }
847
848 /// Returns a vector containing each element of `self` raised to the power of `n`.
849 #[inline]
850 #[must_use]
851 pub fn powf(self, n: f32) -> Self {
852 Self::new(
853 math::powf(self.x, n),
854 math::powf(self.y, n),
855 math::powf(self.z, n),
856 )
857 }
858
859 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
860 #[inline]
861 #[must_use]
862 pub fn recip(self) -> Self {
863 Self(f32x4_div(Self::ONE.0, self.0))
864 }
865
866 /// Performs a linear interpolation between `self` and `rhs` based on the value `s`.
867 ///
868 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
869 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
870 /// extrapolated.
871 #[doc(alias = "mix")]
872 #[inline]
873 #[must_use]
874 pub fn lerp(self, rhs: Self, s: f32) -> Self {
875 self * (1.0 - s) + rhs * s
876 }
877
878 /// Moves towards `rhs` based on the value `d`.
879 ///
880 /// When `d` is `0.0`, the result will be equal to `self`. When `d` is equal to
881 /// `self.distance(rhs)`, the result will be equal to `rhs`. Will not go past `rhs`.
882 #[inline]
883 #[must_use]
884 pub fn move_towards(&self, rhs: Self, d: f32) -> Self {
885 let a = rhs - *self;
886 let len = a.length();
887 if len <= d || len <= 1e-4 {
888 return rhs;
889 }
890 *self + a / len * d
891 }
892
893 /// Calculates the midpoint between `self` and `rhs`.
894 ///
895 /// The midpoint is the average of, or halfway point between, two vectors.
896 /// `a.midpoint(b)` should yield the same result as `a.lerp(b, 0.5)`
897 /// while being slightly cheaper to compute.
898 #[inline]
899 pub fn midpoint(self, rhs: Self) -> Self {
900 (self + rhs) * 0.5
901 }
902
903 /// Returns true if the absolute difference of all elements between `self` and `rhs` is
904 /// less than or equal to `max_abs_diff`.
905 ///
906 /// This can be used to compare if two vectors contain similar elements. It works best when
907 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
908 /// the values being compared against.
909 ///
910 /// For more see
911 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
912 #[inline]
913 #[must_use]
914 pub fn abs_diff_eq(self, rhs: Self, max_abs_diff: f32) -> bool {
915 self.sub(rhs).abs().cmple(Self::splat(max_abs_diff)).all()
916 }
917
918 /// Returns a vector with a length no less than `min` and no more than `max`.
919 ///
920 /// # Panics
921 ///
922 /// Will panic if `min` is greater than `max`, or if either `min` or `max` is negative, when `glam_assert` is enabled.
923 #[inline]
924 #[must_use]
925 pub fn clamp_length(self, min: f32, max: f32) -> Self {
926 glam_assert!(0.0 <= min);
927 glam_assert!(min <= max);
928 let length_sq = self.length_squared();
929 if length_sq < min * min {
930 min * (self / math::sqrt(length_sq))
931 } else if length_sq > max * max {
932 max * (self / math::sqrt(length_sq))
933 } else {
934 self
935 }
936 }
937
938 /// Returns a vector with a length no more than `max`.
939 ///
940 /// # Panics
941 ///
942 /// Will panic if `max` is negative when `glam_assert` is enabled.
943 #[inline]
944 #[must_use]
945 pub fn clamp_length_max(self, max: f32) -> Self {
946 glam_assert!(0.0 <= max);
947 let length_sq = self.length_squared();
948 if length_sq > max * max {
949 max * (self / math::sqrt(length_sq))
950 } else {
951 self
952 }
953 }
954
955 /// Returns a vector with a length no less than `min`.
956 ///
957 /// # Panics
958 ///
959 /// Will panic if `min` is negative when `glam_assert` is enabled.
960 #[inline]
961 #[must_use]
962 pub fn clamp_length_min(self, min: f32) -> Self {
963 glam_assert!(0.0 <= min);
964 let length_sq = self.length_squared();
965 if length_sq < min * min {
966 min * (self / math::sqrt(length_sq))
967 } else {
968 self
969 }
970 }
971
972 /// Fused multiply-add. Computes `(self * a) + b` element-wise with only one rounding
973 /// error, yielding a more accurate result than an unfused multiply-add.
974 ///
975 /// Using `mul_add` *may* be more performant than an unfused multiply-add if the target
976 /// architecture has a dedicated fma CPU instruction. However, this is not always true,
977 /// and will be heavily dependant on designing algorithms with specific target hardware in
978 /// mind.
979 #[inline]
980 #[must_use]
981 pub fn mul_add(self, a: Self, b: Self) -> Self {
982 Self::new(
983 math::mul_add(self.x, a.x, b.x),
984 math::mul_add(self.y, a.y, b.y),
985 math::mul_add(self.z, a.z, b.z),
986 )
987 }
988
989 /// Returns the reflection vector for a given incident vector `self` and surface normal
990 /// `normal`.
991 ///
992 /// `normal` must be normalized.
993 ///
994 /// # Panics
995 ///
996 /// Will panic if `normal` is not normalized when `glam_assert` is enabled.
997 #[inline]
998 #[must_use]
999 pub fn reflect(self, normal: Self) -> Self {
1000 glam_assert!(normal.is_normalized());
1001 self - 2.0 * self.dot(normal) * normal
1002 }
1003
1004 /// Returns the refraction direction for a given incident vector `self`, surface normal
1005 /// `normal` and ratio of indices of refraction, `eta`. When total internal reflection occurs,
1006 /// a zero vector will be returned.
1007 ///
1008 /// `self` and `normal` must be normalized.
1009 ///
1010 /// # Panics
1011 ///
1012 /// Will panic if `self` or `normal` is not normalized when `glam_assert` is enabled.
1013 #[inline]
1014 #[must_use]
1015 pub fn refract(self, normal: Self, eta: f32) -> Self {
1016 glam_assert!(self.is_normalized());
1017 glam_assert!(normal.is_normalized());
1018 let n_dot_i = normal.dot(self);
1019 let k = 1.0 - eta * eta * (1.0 - n_dot_i * n_dot_i);
1020 if k >= 0.0 {
1021 eta * self - (eta * n_dot_i + math::sqrt(k)) * normal
1022 } else {
1023 Self::ZERO
1024 }
1025 }
1026
1027 /// Returns the angle (in radians) between two vectors in the range `[0, +Ï€]`.
1028 ///
1029 /// The inputs do not need to be unit vectors however they must be non-zero.
1030 #[inline]
1031 #[must_use]
1032 pub fn angle_between(self, rhs: Self) -> f32 {
1033 math::acos_approx(
1034 self.dot(rhs)
1035 .div(math::sqrt(self.length_squared().mul(rhs.length_squared()))),
1036 )
1037 }
1038
1039 /// Rotates around the x axis by `angle` (in radians).
1040 #[inline]
1041 #[must_use]
1042 pub fn rotate_x(self, angle: f32) -> Self {
1043 let (sina, cosa) = math::sin_cos(angle);
1044 Self::new(
1045 self.x,
1046 self.y * cosa - self.z * sina,
1047 self.y * sina + self.z * cosa,
1048 )
1049 }
1050
1051 /// Rotates around the y axis by `angle` (in radians).
1052 #[inline]
1053 #[must_use]
1054 pub fn rotate_y(self, angle: f32) -> Self {
1055 let (sina, cosa) = math::sin_cos(angle);
1056 Self::new(
1057 self.x * cosa + self.z * sina,
1058 self.y,
1059 self.x * -sina + self.z * cosa,
1060 )
1061 }
1062
1063 /// Rotates around the z axis by `angle` (in radians).
1064 #[inline]
1065 #[must_use]
1066 pub fn rotate_z(self, angle: f32) -> Self {
1067 let (sina, cosa) = math::sin_cos(angle);
1068 Self::new(
1069 self.x * cosa - self.y * sina,
1070 self.x * sina + self.y * cosa,
1071 self.z,
1072 )
1073 }
1074
1075 /// Rotates around `axis` by `angle` (in radians).
1076 ///
1077 /// The axis must be a unit vector.
1078 ///
1079 /// # Panics
1080 ///
1081 /// Will panic if `axis` is not normalized when `glam_assert` is enabled.
1082 #[inline]
1083 #[must_use]
1084 pub fn rotate_axis(self, axis: Self, angle: f32) -> Self {
1085 Quat::from_axis_angle(axis.into(), angle) * self
1086 }
1087
1088 /// Rotates towards `rhs` up to `max_angle` (in radians).
1089 ///
1090 /// When `max_angle` is `0.0`, the result will be equal to `self`. When `max_angle` is equal to
1091 /// `self.angle_between(rhs)`, the result will be parallel to `rhs`. If `max_angle` is negative,
1092 /// rotates towards the exact opposite of `rhs`. Will not go past the target.
1093 #[inline]
1094 #[must_use]
1095 pub fn rotate_towards(self, rhs: Self, max_angle: f32) -> Self {
1096 let angle_between = self.angle_between(rhs);
1097 // When `max_angle < 0`, rotate no further than `PI` radians away
1098 let angle = max_angle.clamp(angle_between - core::f32::consts::PI, angle_between);
1099 let axis = self
1100 .cross(rhs)
1101 .try_normalize()
1102 .unwrap_or_else(|| self.any_orthogonal_vector().normalize());
1103 Quat::from_axis_angle(axis.into(), angle) * self
1104 }
1105
1106 /// Returns some vector that is orthogonal to the given one.
1107 ///
1108 /// The input vector must be finite and non-zero.
1109 ///
1110 /// The output vector is not necessarily unit length. For that use
1111 /// [`Self::any_orthonormal_vector()`] instead.
1112 #[inline]
1113 #[must_use]
1114 pub fn any_orthogonal_vector(&self) -> Self {
1115 // This can probably be optimized
1116 if math::abs(self.x) > math::abs(self.y) {
1117 Self::new(-self.z, 0.0, self.x) // self.cross(Self::Y)
1118 } else {
1119 Self::new(0.0, self.z, -self.y) // self.cross(Self::X)
1120 }
1121 }
1122
1123 /// Returns any unit vector that is orthogonal to the given one.
1124 ///
1125 /// The input vector must be unit length.
1126 ///
1127 /// # Panics
1128 ///
1129 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
1130 #[inline]
1131 #[must_use]
1132 pub fn any_orthonormal_vector(&self) -> Self {
1133 glam_assert!(self.is_normalized());
1134 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
1135 let sign = math::signum(self.z);
1136 let a = -1.0 / (sign + self.z);
1137 let b = self.x * self.y * a;
1138 Self::new(b, sign + self.y * self.y * a, -self.y)
1139 }
1140
1141 /// Given a unit vector return two other vectors that together form an orthonormal
1142 /// basis. That is, all three vectors are orthogonal to each other and are normalized.
1143 ///
1144 /// # Panics
1145 ///
1146 /// Will panic if `self` is not normalized when `glam_assert` is enabled.
1147 #[inline]
1148 #[must_use]
1149 pub fn any_orthonormal_pair(&self) -> (Self, Self) {
1150 glam_assert!(self.is_normalized());
1151 // From https://graphics.pixar.com/library/OrthonormalB/paper.pdf
1152 let sign = math::signum(self.z);
1153 let a = -1.0 / (sign + self.z);
1154 let b = self.x * self.y * a;
1155 (
1156 Self::new(1.0 + sign * self.x * self.x * a, sign * b, -sign * self.x),
1157 Self::new(b, sign + self.y * self.y * a, -self.y),
1158 )
1159 }
1160
1161 /// Performs a spherical linear interpolation between `self` and `rhs` based on the value `s`.
1162 ///
1163 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
1164 /// will be equal to `rhs`. When `s` is outside of range `[0, 1]`, the result is linearly
1165 /// extrapolated.
1166 #[inline]
1167 #[must_use]
1168 pub fn slerp(self, rhs: Self, s: f32) -> Self {
1169 let self_length = self.length();
1170 let rhs_length = rhs.length();
1171 // Cosine of the angle between the vectors [-1, 1], or NaN if either vector has a zero length
1172 let dot = self.dot(rhs) / (self_length * rhs_length);
1173 // If dot is close to 1 or -1, or is NaN the calculations for t1 and t2 break down
1174 if math::abs(dot) < 1.0 - 3e-7 {
1175 // Angle between the vectors [0, +Ï€]
1176 let theta = math::acos_approx(dot);
1177 // Sine of the angle between vectors [0, 1]
1178 let sin_theta = math::sin(theta);
1179 let t1 = math::sin(theta * (1. - s));
1180 let t2 = math::sin(theta * s);
1181
1182 // Interpolate vector lengths
1183 let result_length = self_length.lerp(rhs_length, s);
1184 // Scale the vectors to the target length and interpolate them
1185 return (self * (result_length / self_length) * t1
1186 + rhs * (result_length / rhs_length) * t2)
1187 * sin_theta.recip();
1188 }
1189 if dot < 0.0 {
1190 // Vectors are almost parallel in opposing directions
1191
1192 // Create a rotation from self to rhs along some axis
1193 let axis = self.any_orthogonal_vector().normalize().into();
1194 let rotation = Quat::from_axis_angle(axis, core::f32::consts::PI * s);
1195 // Interpolate vector lengths
1196 let result_length = self_length.lerp(rhs_length, s);
1197 rotation * self * (result_length / self_length)
1198 } else {
1199 // Vectors are almost parallel in the same direction, or dot was NaN
1200 self.lerp(rhs, s)
1201 }
1202 }
1203
1204 /// Casts all elements of `self` to `f64`.
1205 #[inline]
1206 #[must_use]
1207 pub fn as_dvec3(&self) -> crate::DVec3 {
1208 crate::DVec3::new(self.x as f64, self.y as f64, self.z as f64)
1209 }
1210
1211 /// Casts all elements of `self` to `i8`.
1212 #[inline]
1213 #[must_use]
1214 pub fn as_i8vec3(&self) -> crate::I8Vec3 {
1215 crate::I8Vec3::new(self.x as i8, self.y as i8, self.z as i8)
1216 }
1217
1218 /// Casts all elements of `self` to `u8`.
1219 #[inline]
1220 #[must_use]
1221 pub fn as_u8vec3(&self) -> crate::U8Vec3 {
1222 crate::U8Vec3::new(self.x as u8, self.y as u8, self.z as u8)
1223 }
1224
1225 /// Casts all elements of `self` to `i16`.
1226 #[inline]
1227 #[must_use]
1228 pub fn as_i16vec3(&self) -> crate::I16Vec3 {
1229 crate::I16Vec3::new(self.x as i16, self.y as i16, self.z as i16)
1230 }
1231
1232 /// Casts all elements of `self` to `u16`.
1233 #[inline]
1234 #[must_use]
1235 pub fn as_u16vec3(&self) -> crate::U16Vec3 {
1236 crate::U16Vec3::new(self.x as u16, self.y as u16, self.z as u16)
1237 }
1238
1239 /// Casts all elements of `self` to `i32`.
1240 #[inline]
1241 #[must_use]
1242 pub fn as_ivec3(&self) -> crate::IVec3 {
1243 crate::IVec3::new(self.x as i32, self.y as i32, self.z as i32)
1244 }
1245
1246 /// Casts all elements of `self` to `u32`.
1247 #[inline]
1248 #[must_use]
1249 pub fn as_uvec3(&self) -> crate::UVec3 {
1250 crate::UVec3::new(self.x as u32, self.y as u32, self.z as u32)
1251 }
1252
1253 /// Casts all elements of `self` to `i64`.
1254 #[inline]
1255 #[must_use]
1256 pub fn as_i64vec3(&self) -> crate::I64Vec3 {
1257 crate::I64Vec3::new(self.x as i64, self.y as i64, self.z as i64)
1258 }
1259
1260 /// Casts all elements of `self` to `u64`.
1261 #[inline]
1262 #[must_use]
1263 pub fn as_u64vec3(&self) -> crate::U64Vec3 {
1264 crate::U64Vec3::new(self.x as u64, self.y as u64, self.z as u64)
1265 }
1266
1267 /// Casts all elements of `self` to `usize`.
1268 #[inline]
1269 #[must_use]
1270 pub fn as_usizevec3(&self) -> crate::USizeVec3 {
1271 crate::USizeVec3::new(self.x as usize, self.y as usize, self.z as usize)
1272 }
1273 }
1274
1275 impl Default for Vec3A {
1276 #[inline(always)]
1277 fn default() -> Self {
1278 Self::ZERO
1279 }
1280 }
1281
1282 impl PartialEq for Vec3A {
1283 #[inline]
1284 fn eq(&self, rhs: &Self) -> bool {
1285 self.cmpeq(*rhs).all()
1286 }
1287 }
1288
1289 impl Div for Vec3A {
1290 type Output = Self;
1291 #[inline]
1292 fn div(self, rhs: Self) -> Self {
1293 Self(f32x4_div(self.0, rhs.0))
1294 }
1295 }
1296
1297 impl Div<&Self> for Vec3A {
1298 type Output = Self;
1299 #[inline]
1300 fn div(self, rhs: &Self) -> Self {
1301 self.div(*rhs)
1302 }
1303 }
1304
1305 impl Div<&Vec3A> for &Vec3A {
1306 type Output = Vec3A;
1307 #[inline]
1308 fn div(self, rhs: &Vec3A) -> Vec3A {
1309 (*self).div(*rhs)
1310 }
1311 }
1312
1313 impl Div<Vec3A> for &Vec3A {
1314 type Output = Vec3A;
1315 #[inline]
1316 fn div(self, rhs: Vec3A) -> Vec3A {
1317 (*self).div(rhs)
1318 }
1319 }
1320
1321 impl DivAssign for Vec3A {
1322 #[inline]
1323 fn div_assign(&mut self, rhs: Self) {
1324 self.0 = f32x4_div(self.0, rhs.0);
1325 }
1326 }
1327
1328 impl DivAssign<&Self> for Vec3A {
1329 #[inline]
1330 fn div_assign(&mut self, rhs: &Self) {
1331 self.div_assign(*rhs);
1332 }
1333 }
1334
1335 impl Div<f32> for Vec3A {
1336 type Output = Self;
1337 #[inline]
1338 fn div(self, rhs: f32) -> Self {
1339 Self(f32x4_div(self.0, f32x4_splat(rhs)))
1340 }
1341 }
1342
1343 impl Div<&f32> for Vec3A {
1344 type Output = Self;
1345 #[inline]
1346 fn div(self, rhs: &f32) -> Self {
1347 self.div(*rhs)
1348 }
1349 }
1350
1351 impl Div<&f32> for &Vec3A {
1352 type Output = Vec3A;
1353 #[inline]
1354 fn div(self, rhs: &f32) -> Vec3A {
1355 (*self).div(*rhs)
1356 }
1357 }
1358
1359 impl Div<f32> for &Vec3A {
1360 type Output = Vec3A;
1361 #[inline]
1362 fn div(self, rhs: f32) -> Vec3A {
1363 (*self).div(rhs)
1364 }
1365 }
1366
1367 impl DivAssign<f32> for Vec3A {
1368 #[inline]
1369 fn div_assign(&mut self, rhs: f32) {
1370 self.0 = f32x4_div(self.0, f32x4_splat(rhs));
1371 }
1372 }
1373
1374 impl DivAssign<&f32> for Vec3A {
1375 #[inline]
1376 fn div_assign(&mut self, rhs: &f32) {
1377 self.div_assign(*rhs);
1378 }
1379 }
1380
1381 impl Div<Vec3A> for f32 {
1382 type Output = Vec3A;
1383 #[inline]
1384 fn div(self, rhs: Vec3A) -> Vec3A {
1385 Vec3A(f32x4_div(f32x4_splat(self), rhs.0))
1386 }
1387 }
1388
1389 impl Div<&Vec3A> for f32 {
1390 type Output = Vec3A;
1391 #[inline]
1392 fn div(self, rhs: &Vec3A) -> Vec3A {
1393 self.div(*rhs)
1394 }
1395 }
1396
1397 impl Div<&Vec3A> for &f32 {
1398 type Output = Vec3A;
1399 #[inline]
1400 fn div(self, rhs: &Vec3A) -> Vec3A {
1401 (*self).div(*rhs)
1402 }
1403 }
1404
1405 impl Div<Vec3A> for &f32 {
1406 type Output = Vec3A;
1407 #[inline]
1408 fn div(self, rhs: Vec3A) -> Vec3A {
1409 (*self).div(rhs)
1410 }
1411 }
1412
1413 impl Mul for Vec3A {
1414 type Output = Self;
1415 #[inline]
1416 fn mul(self, rhs: Self) -> Self {
1417 Self(f32x4_mul(self.0, rhs.0))
1418 }
1419 }
1420
1421 impl Mul<&Self> for Vec3A {
1422 type Output = Self;
1423 #[inline]
1424 fn mul(self, rhs: &Self) -> Self {
1425 self.mul(*rhs)
1426 }
1427 }
1428
1429 impl Mul<&Vec3A> for &Vec3A {
1430 type Output = Vec3A;
1431 #[inline]
1432 fn mul(self, rhs: &Vec3A) -> Vec3A {
1433 (*self).mul(*rhs)
1434 }
1435 }
1436
1437 impl Mul<Vec3A> for &Vec3A {
1438 type Output = Vec3A;
1439 #[inline]
1440 fn mul(self, rhs: Vec3A) -> Vec3A {
1441 (*self).mul(rhs)
1442 }
1443 }
1444
1445 impl MulAssign for Vec3A {
1446 #[inline]
1447 fn mul_assign(&mut self, rhs: Self) {
1448 self.0 = f32x4_mul(self.0, rhs.0);
1449 }
1450 }
1451
1452 impl MulAssign<&Self> for Vec3A {
1453 #[inline]
1454 fn mul_assign(&mut self, rhs: &Self) {
1455 self.mul_assign(*rhs);
1456 }
1457 }
1458
1459 impl Mul<f32> for Vec3A {
1460 type Output = Self;
1461 #[inline]
1462 fn mul(self, rhs: f32) -> Self {
1463 Self(f32x4_mul(self.0, f32x4_splat(rhs)))
1464 }
1465 }
1466
1467 impl Mul<&f32> for Vec3A {
1468 type Output = Self;
1469 #[inline]
1470 fn mul(self, rhs: &f32) -> Self {
1471 self.mul(*rhs)
1472 }
1473 }
1474
1475 impl Mul<&f32> for &Vec3A {
1476 type Output = Vec3A;
1477 #[inline]
1478 fn mul(self, rhs: &f32) -> Vec3A {
1479 (*self).mul(*rhs)
1480 }
1481 }
1482
1483 impl Mul<f32> for &Vec3A {
1484 type Output = Vec3A;
1485 #[inline]
1486 fn mul(self, rhs: f32) -> Vec3A {
1487 (*self).mul(rhs)
1488 }
1489 }
1490
1491 impl MulAssign<f32> for Vec3A {
1492 #[inline]
1493 fn mul_assign(&mut self, rhs: f32) {
1494 self.0 = f32x4_mul(self.0, f32x4_splat(rhs))
1495 }
1496 }
1497
1498 impl MulAssign<&f32> for Vec3A {
1499 #[inline]
1500 fn mul_assign(&mut self, rhs: &f32) {
1501 self.mul_assign(*rhs);
1502 }
1503 }
1504
1505 impl Mul<Vec3A> for f32 {
1506 type Output = Vec3A;
1507 #[inline]
1508 fn mul(self, rhs: Vec3A) -> Vec3A {
1509 Vec3A(f32x4_mul(f32x4_splat(self), rhs.0))
1510 }
1511 }
1512
1513 impl Mul<&Vec3A> for f32 {
1514 type Output = Vec3A;
1515 #[inline]
1516 fn mul(self, rhs: &Vec3A) -> Vec3A {
1517 self.mul(*rhs)
1518 }
1519 }
1520
1521 impl Mul<&Vec3A> for &f32 {
1522 type Output = Vec3A;
1523 #[inline]
1524 fn mul(self, rhs: &Vec3A) -> Vec3A {
1525 (*self).mul(*rhs)
1526 }
1527 }
1528
1529 impl Mul<Vec3A> for &f32 {
1530 type Output = Vec3A;
1531 #[inline]
1532 fn mul(self, rhs: Vec3A) -> Vec3A {
1533 (*self).mul(rhs)
1534 }
1535 }
1536
1537 impl Add for Vec3A {
1538 type Output = Self;
1539 #[inline]
1540 fn add(self, rhs: Self) -> Self {
1541 Self(f32x4_add(self.0, rhs.0))
1542 }
1543 }
1544
1545 impl Add<&Self> for Vec3A {
1546 type Output = Self;
1547 #[inline]
1548 fn add(self, rhs: &Self) -> Self {
1549 self.add(*rhs)
1550 }
1551 }
1552
1553 impl Add<&Vec3A> for &Vec3A {
1554 type Output = Vec3A;
1555 #[inline]
1556 fn add(self, rhs: &Vec3A) -> Vec3A {
1557 (*self).add(*rhs)
1558 }
1559 }
1560
1561 impl Add<Vec3A> for &Vec3A {
1562 type Output = Vec3A;
1563 #[inline]
1564 fn add(self, rhs: Vec3A) -> Vec3A {
1565 (*self).add(rhs)
1566 }
1567 }
1568
1569 impl AddAssign for Vec3A {
1570 #[inline]
1571 fn add_assign(&mut self, rhs: Self) {
1572 self.0 = f32x4_add(self.0, rhs.0);
1573 }
1574 }
1575
1576 impl AddAssign<&Self> for Vec3A {
1577 #[inline]
1578 fn add_assign(&mut self, rhs: &Self) {
1579 self.add_assign(*rhs);
1580 }
1581 }
1582
1583 impl Add<f32> for Vec3A {
1584 type Output = Self;
1585 #[inline]
1586 fn add(self, rhs: f32) -> Self {
1587 Self(f32x4_add(self.0, f32x4_splat(rhs)))
1588 }
1589 }
1590
1591 impl Add<&f32> for Vec3A {
1592 type Output = Self;
1593 #[inline]
1594 fn add(self, rhs: &f32) -> Self {
1595 self.add(*rhs)
1596 }
1597 }
1598
1599 impl Add<&f32> for &Vec3A {
1600 type Output = Vec3A;
1601 #[inline]
1602 fn add(self, rhs: &f32) -> Vec3A {
1603 (*self).add(*rhs)
1604 }
1605 }
1606
1607 impl Add<f32> for &Vec3A {
1608 type Output = Vec3A;
1609 #[inline]
1610 fn add(self, rhs: f32) -> Vec3A {
1611 (*self).add(rhs)
1612 }
1613 }
1614
1615 impl AddAssign<f32> for Vec3A {
1616 #[inline]
1617 fn add_assign(&mut self, rhs: f32) {
1618 self.0 = f32x4_add(self.0, f32x4_splat(rhs));
1619 }
1620 }
1621
1622 impl AddAssign<&f32> for Vec3A {
1623 #[inline]
1624 fn add_assign(&mut self, rhs: &f32) {
1625 self.add_assign(*rhs);
1626 }
1627 }
1628
1629 impl Add<Vec3A> for f32 {
1630 type Output = Vec3A;
1631 #[inline]
1632 fn add(self, rhs: Vec3A) -> Vec3A {
1633 Vec3A(f32x4_add(f32x4_splat(self), rhs.0))
1634 }
1635 }
1636
1637 impl Add<&Vec3A> for f32 {
1638 type Output = Vec3A;
1639 #[inline]
1640 fn add(self, rhs: &Vec3A) -> Vec3A {
1641 self.add(*rhs)
1642 }
1643 }
1644
1645 impl Add<&Vec3A> for &f32 {
1646 type Output = Vec3A;
1647 #[inline]
1648 fn add(self, rhs: &Vec3A) -> Vec3A {
1649 (*self).add(*rhs)
1650 }
1651 }
1652
1653 impl Add<Vec3A> for &f32 {
1654 type Output = Vec3A;
1655 #[inline]
1656 fn add(self, rhs: Vec3A) -> Vec3A {
1657 (*self).add(rhs)
1658 }
1659 }
1660
1661 impl Sub for Vec3A {
1662 type Output = Self;
1663 #[inline]
1664 fn sub(self, rhs: Self) -> Self {
1665 Self(f32x4_sub(self.0, rhs.0))
1666 }
1667 }
1668
1669 impl Sub<&Self> for Vec3A {
1670 type Output = Self;
1671 #[inline]
1672 fn sub(self, rhs: &Self) -> Self {
1673 self.sub(*rhs)
1674 }
1675 }
1676
1677 impl Sub<&Vec3A> for &Vec3A {
1678 type Output = Vec3A;
1679 #[inline]
1680 fn sub(self, rhs: &Vec3A) -> Vec3A {
1681 (*self).sub(*rhs)
1682 }
1683 }
1684
1685 impl Sub<Vec3A> for &Vec3A {
1686 type Output = Vec3A;
1687 #[inline]
1688 fn sub(self, rhs: Vec3A) -> Vec3A {
1689 (*self).sub(rhs)
1690 }
1691 }
1692
1693 impl SubAssign for Vec3A {
1694 #[inline]
1695 fn sub_assign(&mut self, rhs: Self) {
1696 self.0 = f32x4_sub(self.0, rhs.0);
1697 }
1698 }
1699
1700 impl SubAssign<&Self> for Vec3A {
1701 #[inline]
1702 fn sub_assign(&mut self, rhs: &Self) {
1703 self.sub_assign(*rhs);
1704 }
1705 }
1706
1707 impl Sub<f32> for Vec3A {
1708 type Output = Self;
1709 #[inline]
1710 fn sub(self, rhs: f32) -> Self {
1711 Self(f32x4_sub(self.0, f32x4_splat(rhs)))
1712 }
1713 }
1714
1715 impl Sub<&f32> for Vec3A {
1716 type Output = Self;
1717 #[inline]
1718 fn sub(self, rhs: &f32) -> Self {
1719 self.sub(*rhs)
1720 }
1721 }
1722
1723 impl Sub<&f32> for &Vec3A {
1724 type Output = Vec3A;
1725 #[inline]
1726 fn sub(self, rhs: &f32) -> Vec3A {
1727 (*self).sub(*rhs)
1728 }
1729 }
1730
1731 impl Sub<f32> for &Vec3A {
1732 type Output = Vec3A;
1733 #[inline]
1734 fn sub(self, rhs: f32) -> Vec3A {
1735 (*self).sub(rhs)
1736 }
1737 }
1738
1739 impl SubAssign<f32> for Vec3A {
1740 #[inline]
1741 fn sub_assign(&mut self, rhs: f32) {
1742 self.0 = f32x4_sub(self.0, f32x4_splat(rhs))
1743 }
1744 }
1745
1746 impl SubAssign<&f32> for Vec3A {
1747 #[inline]
1748 fn sub_assign(&mut self, rhs: &f32) {
1749 self.sub_assign(*rhs);
1750 }
1751 }
1752
1753 impl Sub<Vec3A> for f32 {
1754 type Output = Vec3A;
1755 #[inline]
1756 fn sub(self, rhs: Vec3A) -> Vec3A {
1757 Vec3A(f32x4_sub(f32x4_splat(self), rhs.0))
1758 }
1759 }
1760
1761 impl Sub<&Vec3A> for f32 {
1762 type Output = Vec3A;
1763 #[inline]
1764 fn sub(self, rhs: &Vec3A) -> Vec3A {
1765 self.sub(*rhs)
1766 }
1767 }
1768
1769 impl Sub<&Vec3A> for &f32 {
1770 type Output = Vec3A;
1771 #[inline]
1772 fn sub(self, rhs: &Vec3A) -> Vec3A {
1773 (*self).sub(*rhs)
1774 }
1775 }
1776
1777 impl Sub<Vec3A> for &f32 {
1778 type Output = Vec3A;
1779 #[inline]
1780 fn sub(self, rhs: Vec3A) -> Vec3A {
1781 (*self).sub(rhs)
1782 }
1783 }
1784
1785 impl Rem for Vec3A {
1786 type Output = Self;
1787 #[inline]
1788 fn rem(self, rhs: Self) -> Self {
1789 let n = f32x4_floor(f32x4_div(self.0, rhs.0));
1790 Self(f32x4_sub(self.0, f32x4_mul(n, rhs.0)))
1791 }
1792 }
1793
1794 impl Rem<&Self> for Vec3A {
1795 type Output = Self;
1796 #[inline]
1797 fn rem(self, rhs: &Self) -> Self {
1798 self.rem(*rhs)
1799 }
1800 }
1801
1802 impl Rem<&Vec3A> for &Vec3A {
1803 type Output = Vec3A;
1804 #[inline]
1805 fn rem(self, rhs: &Vec3A) -> Vec3A {
1806 (*self).rem(*rhs)
1807 }
1808 }
1809
1810 impl Rem<Vec3A> for &Vec3A {
1811 type Output = Vec3A;
1812 #[inline]
1813 fn rem(self, rhs: Vec3A) -> Vec3A {
1814 (*self).rem(rhs)
1815 }
1816 }
1817
1818 impl RemAssign for Vec3A {
1819 #[inline]
1820 fn rem_assign(&mut self, rhs: Self) {
1821 *self = self.rem(rhs);
1822 }
1823 }
1824
1825 impl RemAssign<&Self> for Vec3A {
1826 #[inline]
1827 fn rem_assign(&mut self, rhs: &Self) {
1828 self.rem_assign(*rhs);
1829 }
1830 }
1831
1832 impl Rem<f32> for Vec3A {
1833 type Output = Self;
1834 #[inline]
1835 fn rem(self, rhs: f32) -> Self {
1836 self.rem(Self::splat(rhs))
1837 }
1838 }
1839
1840 impl Rem<&f32> for Vec3A {
1841 type Output = Self;
1842 #[inline]
1843 fn rem(self, rhs: &f32) -> Self {
1844 self.rem(*rhs)
1845 }
1846 }
1847
1848 impl Rem<&f32> for &Vec3A {
1849 type Output = Vec3A;
1850 #[inline]
1851 fn rem(self, rhs: &f32) -> Vec3A {
1852 (*self).rem(*rhs)
1853 }
1854 }
1855
1856 impl Rem<f32> for &Vec3A {
1857 type Output = Vec3A;
1858 #[inline]
1859 fn rem(self, rhs: f32) -> Vec3A {
1860 (*self).rem(rhs)
1861 }
1862 }
1863
1864 impl RemAssign<f32> for Vec3A {
1865 #[inline]
1866 fn rem_assign(&mut self, rhs: f32) {
1867 *self = self.rem(Self::splat(rhs));
1868 }
1869 }
1870
1871 impl RemAssign<&f32> for Vec3A {
1872 #[inline]
1873 fn rem_assign(&mut self, rhs: &f32) {
1874 self.rem_assign(*rhs);
1875 }
1876 }
1877
1878 impl Rem<Vec3A> for f32 {
1879 type Output = Vec3A;
1880 #[inline]
1881 fn rem(self, rhs: Vec3A) -> Vec3A {
1882 Vec3A::splat(self).rem(rhs)
1883 }
1884 }
1885
1886 impl Rem<&Vec3A> for f32 {
1887 type Output = Vec3A;
1888 #[inline]
1889 fn rem(self, rhs: &Vec3A) -> Vec3A {
1890 self.rem(*rhs)
1891 }
1892 }
1893
1894 impl Rem<&Vec3A> for &f32 {
1895 type Output = Vec3A;
1896 #[inline]
1897 fn rem(self, rhs: &Vec3A) -> Vec3A {
1898 (*self).rem(*rhs)
1899 }
1900 }
1901
1902 impl Rem<Vec3A> for &f32 {
1903 type Output = Vec3A;
1904 #[inline]
1905 fn rem(self, rhs: Vec3A) -> Vec3A {
1906 (*self).rem(rhs)
1907 }
1908 }
1909
1910 impl AsRef<[f32; 3]> for Vec3A {
1911 #[inline]
1912 fn as_ref(&self) -> &[f32; 3] {
1913 unsafe { &*(self as *const Self as *const [f32; 3]) }
1914 }
1915 }
1916
1917 impl AsMut<[f32; 3]> for Vec3A {
1918 #[inline]
1919 fn as_mut(&mut self) -> &mut [f32; 3] {
1920 unsafe { &mut *(self as *mut Self as *mut [f32; 3]) }
1921 }
1922 }
1923
1924 impl Sum for Vec3A {
1925 #[inline]
1926 fn sum<I>(iter: I) -> Self
1927 where
1928 I: Iterator<Item = Self>,
1929 {
1930 iter.fold(Self::ZERO, Self::add)
1931 }
1932 }
1933
1934 impl<'a> Sum<&'a Self> for Vec3A {
1935 #[inline]
1936 fn sum<I>(iter: I) -> Self
1937 where
1938 I: Iterator<Item = &'a Self>,
1939 {
1940 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
1941 }
1942 }
1943
1944 impl Product for Vec3A {
1945 #[inline]
1946 fn product<I>(iter: I) -> Self
1947 where
1948 I: Iterator<Item = Self>,
1949 {
1950 iter.fold(Self::ONE, Self::mul)
1951 }
1952 }
1953
1954 impl<'a> Product<&'a Self> for Vec3A {
1955 #[inline]
1956 fn product<I>(iter: I) -> Self
1957 where
1958 I: Iterator<Item = &'a Self>,
1959 {
1960 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
1961 }
1962 }
1963
1964 impl Neg for Vec3A {
1965 type Output = Self;
1966 #[inline]
1967 fn neg(self) -> Self {
1968 Self(f32x4_neg(self.0))
1969 }
1970 }
1971
1972 impl Neg for &Vec3A {
1973 type Output = Vec3A;
1974 #[inline]
1975 fn neg(self) -> Vec3A {
1976 (*self).neg()
1977 }
1978 }
1979
1980 impl Index<usize> for Vec3A {
1981 type Output = f32;
1982 #[inline]
1983 fn index(&self, index: usize) -> &Self::Output {
1984 match index {
1985 0 => &self.x,
1986 1 => &self.y,
1987 2 => &self.z,
1988 _ => panic!("index out of bounds"),
1989 }
1990 }
1991 }
1992
1993 impl IndexMut<usize> for Vec3A {
1994 #[inline]
1995 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
1996 match index {
1997 0 => &mut self.x,
1998 1 => &mut self.y,
1999 2 => &mut self.z,
2000 _ => panic!("index out of bounds"),
2001 }
2002 }
2003 }
2004
2005 impl fmt::Display for Vec3A {
2006 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2007 if let Some(p) = f.precision() {
2008 write!(f, "[{:.*}, {:.*}, {:.*}]", p, self.x, p, self.y, p, self.z)
2009 } else {
2010 write!(f, "[{}, {}, {}]", self.x, self.y, self.z)
2011 }
2012 }
2013 }
2014
2015 impl fmt::Debug for Vec3A {
2016 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
2017 fmt.debug_tuple(stringify!(Vec3A))
2018 .field(&self.x)
2019 .field(&self.y)
2020 .field(&self.z)
2021 .finish()
2022 }
2023 }
2024
2025 impl From<Vec3A> for v128 {
2026 #[inline(always)]
2027 fn from(t: Vec3A) -> Self {
2028 t.0
2029 }
2030 }
2031
2032 impl From<v128> for Vec3A {
2033 #[inline(always)]
2034 fn from(t: v128) -> Self {
2035 Self(t)
2036 }
2037 }
2038
2039 impl From<[f32; 3]> for Vec3A {
2040 #[inline]
2041 fn from(a: [f32; 3]) -> Self {
2042 Self::new(a[0], a[1], a[2])
2043 }
2044 }
2045
2046 impl From<Vec3A> for [f32; 3] {
2047 #[inline]
2048 fn from(v: Vec3A) -> Self {
2049 unsafe { *(&v.0 as *const v128 as *const Self) }
2050 }
2051 }
2052
2053 impl From<(f32, f32, f32)> for Vec3A {
2054 #[inline]
2055 fn from(t: (f32, f32, f32)) -> Self {
2056 Self::new(t.0, t.1, t.2)
2057 }
2058 }
2059
2060 impl From<Vec3A> for (f32, f32, f32) {
2061 #[inline]
2062 fn from(v: Vec3A) -> Self {
2063 (v.x, v.y, v.z)
2064 }
2065 }
2066
2067 impl From<Vec3> for Vec3A {
2068 #[inline]
2069 fn from(v: Vec3) -> Self {
2070 Self::new(v.x, v.y, v.z)
2071 }
2072 }
2073
2074 impl From<Vec3A> for Vec3 {
2075 #[inline]
2076 fn from(v: Vec3A) -> Self {
2077 unsafe { *(&v.0 as *const v128 as *const Self) }
2078 }
2079 }
2080
2081 impl From<(Vec2, f32)> for Vec3A {
2082 #[inline]
2083 fn from((v, z): (Vec2, f32)) -> Self {
2084 Self::new(v.x, v.y, z)
2085 }
2086 }
2087
2088 impl Deref for Vec3A {
2089 type Target = crate::deref::Vec3<f32>;
2090 #[inline]
2091 fn deref(&self) -> &Self::Target {
2092 unsafe { &*(self as *const Self).cast() }
2093 }
2094 }
2095
2096 impl DerefMut for Vec3A {
2097 #[inline]
2098 fn deref_mut(&mut self) -> &mut Self::Target {
2099 unsafe { &mut *(self as *mut Self).cast() }
2100 }
2101 }
2102
2103 impl From<BVec3> for Vec3A {
2104 #[inline]
2105 fn from(v: BVec3) -> Self {
2106 Self::new(f32::from(v.x), f32::from(v.y), f32::from(v.z))
2107 }
2108 }
2109
2110 impl From<BVec3A> for Vec3A {
2111 #[inline]
2112 fn from(v: BVec3A) -> Self {
2113 let bool_array: [bool; 3] = v.into();
2114 Self::new(
2115 f32::from(bool_array[0]),
2116 f32::from(bool_array[1]),
2117 f32::from(bool_array[2]),
2118 )
2119 }
2120 }