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