Debian Codegraph @

debian/missing-sources/glam-0.16.0/src/vec.rs

raw symbols SCIP
1 // Adds common vector methods to an impl.
2 // The methods here should be supported for all types of $t and all sizes of vector.
3 macro_rules! impl_vecn_common_methods {
4 ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $vectrait:ident) => {
5 /// Creates a vector with all elements set to `v`.
6 #[inline(always)]
7 pub fn splat(v: $t) -> Self {
8 Self($inner::splat(v))
9 }
10
11 /// Creates a vector from the elements in `if_true` and `if_false`, selecting which to use
12 /// for each element of `self`.
13 ///
14 /// A true element in the mask uses the corresponding element from `if_true`, and false
15 /// uses the element from `if_false`.
16 #[inline(always)]
17 pub fn select(mask: $mask, if_true: $vecn, if_false: $vecn) -> $vecn {
18 Self($inner::select(mask.0, if_true.0, if_false.0))
19 }
20
21 /// Computes the dot product of `self` and `other`.
22 #[inline(always)]
23 pub fn dot(self, other: Self) -> $t {
24 $vectrait::dot(self.0, other.0)
25 }
26
27 /// Returns a vector containing the mininum values for each element of `self` and `other`.
28 ///
29 /// In other words this computes `[self.x.max(other.x), self.y.max(other.y), ..]`.
30 #[inline(always)]
31 pub fn min(self, other: Self) -> Self {
32 Self(self.0.min(other.0))
33 }
34
35 /// Returns a vector containing the maximum values for each element of `self` and `other`.
36 ///
37 /// In other words this computes `[self.x.max(other.x), self.y.max(other.y), ..]`.
38 #[inline(always)]
39 pub fn max(self, other: Self) -> Self {
40 Self(self.0.max(other.0))
41 }
42
43 /// Component-wise clamping of values, similar to [`f32::clamp`].
44 ///
45 /// Each element in `min` must be less-or-equal to the corresponing element in `max`.
46 ///
47 /// If the `glam-assert` feature is enabled, the function will panic if the contract is not
48 /// met, otherwise the behavior is undefined.
49 #[inline(always)]
50 pub fn clamp(self, min: Self, max: Self) -> Self {
51 Self($vectrait::clamp(self.0, min.0, max.0))
52 }
53
54 /// Returns the horizontal minimum of `self`.
55 ///
56 /// In other words this computes `min(x, y, ..)`.
57 #[inline(always)]
58 pub fn min_element(self) -> $t {
59 $vectrait::min_element(self.0)
60 }
61
62 /// Returns the horizontal maximum of `self`.
63 ///
64 /// In other words this computes `max(x, y, ..)`.
65 #[inline(always)]
66 pub fn max_element(self) -> $t {
67 $vectrait::max_element(self.0)
68 }
69
70 /// Returns a vector mask containing the result of a `==` comparison for each element of
71 /// `self` and `other`.
72 ///
73 /// In other words, this computes `[self.x == other.x, self.y == other.y, ..]` for all
74 /// elements.
75 #[inline(always)]
76 pub fn cmpeq(self, other: Self) -> $mask {
77 $mask(self.0.cmpeq(other.0))
78 }
79
80 /// Returns a vector mask containing the result of a `!=` comparison for each element of
81 /// `self` and `other`.
82 ///
83 /// In other words this computes `[self.x != other.x, self.y != other.y, ..]` for all
84 /// elements.
85 #[inline(always)]
86 pub fn cmpne(self, other: Self) -> $mask {
87 $mask(self.0.cmpne(other.0))
88 }
89
90 /// Returns a vector mask containing the result of a `>=` comparison for each element of
91 /// `self` and `other`.
92 ///
93 /// In other words this computes `[self.x >= other.x, self.y >= other.y, ..]` for all
94 /// elements.
95 #[inline(always)]
96 pub fn cmpge(self, other: Self) -> $mask {
97 $mask(self.0.cmpge(other.0))
98 }
99
100 /// Returns a vector mask containing the result of a `>` comparison for each element of
101 /// `self` and `other`.
102 ///
103 /// In other words this computes `[self.x > other.x, self.y > other.y, ..]` for all
104 /// elements.
105 #[inline(always)]
106 pub fn cmpgt(self, other: Self) -> $mask {
107 $mask(self.0.cmpgt(other.0))
108 }
109
110 /// Returns a vector mask containing the result of a `<=` comparison for each element of
111 /// `self` and `other`.
112 ///
113 /// In other words this computes `[self.x <= other.x, self.y <= other.y, ..]` for all
114 /// elements.
115 #[inline(always)]
116 pub fn cmple(self, other: Self) -> $mask {
117 $mask(self.0.cmple(other.0))
118 }
119
120 /// Returns a vector mask containing the result of a `<` comparison for each element of
121 /// `self` and `other`.
122 ///
123 /// In other words this computes `[self.x < other.x, self.y < other.y, ..]` for all
124 /// elements.
125 #[inline(always)]
126 pub fn cmplt(self, other: Self) -> $mask {
127 $mask(self.0.cmplt(other.0))
128 }
129
130 #[deprecated(since = "0.15.2", note = "Please use `from_slice()` instead")]
131 #[inline(always)]
132 pub fn from_slice_unaligned(slice: &[$t]) -> Self {
133 Self::from_slice(slice)
134 }
135
136 /// Creates a vector from the first N values in `slice`.
137 ///
138 /// # Panics
139 ///
140 /// Panics if `slice` is less than N elements long.
141 #[inline(always)]
142 pub fn from_slice(slice: &[$t]) -> Self {
143 Self($vectrait::from_slice_unaligned(slice))
144 }
145
146 #[deprecated(since = "0.15.2", note = "Please use `write_to_slice()` instead")]
147 #[inline(always)]
148 pub fn write_to_slice_unaligned(self, slice: &mut [$t]) {
149 self.write_to_slice(slice)
150 }
151
152 /// Writes the elements of `self` to the first N elements in `slice`.
153 ///
154 /// # Panics
155 ///
156 /// Panics if `slice` is less than N elements long.
157 #[inline(always)]
158 pub fn write_to_slice(self, slice: &mut [$t]) {
159 $vectrait::write_to_slice_unaligned(self.0, slice)
160 }
161
162 /// Per element multiplication/addition of the three inputs: b + (self * a)
163 #[inline(always)]
164 #[allow(dead_code)]
165 pub(crate) fn mul_add(self, a: Self, b: Self) -> Self {
166 Self(self.0.mul_add(a.0, b.0))
167 }
168 };
169 }
170
171 // Adds signed type vector methods to an impl.
172 // The methods here should be supported for signed types of $t and all sizes of vector.
173 macro_rules! impl_vecn_signed_methods {
174 ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $sgntrait:ident) => {
175 // impl_vecn_common_methods!($t, $vecn, $mask, $inner, $vectrait);
176
177 /// Returns a vector containing the absolute value of each element of `self`.
178 #[inline(always)]
179 pub fn abs(self) -> Self {
180 Self($sgntrait::abs(self.0))
181 }
182
183 /// Returns a vector with elements representing the sign of `self`.
184 ///
185 /// - `1.0` if the number is positive, `+0.0` or `INFINITY`
186 /// - `-1.0` if the number is negative, `-0.0` or `NEG_INFINITY`
187 /// - `NAN` if the number is `NAN`
188 #[inline(always)]
189 pub fn signum(self) -> Self {
190 Self($sgntrait::signum(self.0))
191 }
192 };
193 }
194
195 // Adds float type vector methods to an impl.
196 // The methods here should be supported for float types of $t and all sizes of vector.
197 macro_rules! impl_vecn_float_methods {
198 ($t:ty, $vecn:ident, $mask:ident, $inner:ident, $flttrait:ident) => {
199 // impl_vecn_signed_methods!($t, $vecn, $mask, $inner, $sgntrait, $vectrait);
200
201 /// Returns `true` if, and only if, all elements are finite. If any element is either
202 /// `NaN`, positive or negative infinity, this will return `false`.
203 #[inline(always)]
204 pub fn is_finite(self) -> bool {
205 $flttrait::is_finite(self.0)
206 }
207
208 /// Returns `true` if any elements are `NaN`.
209 #[inline(always)]
210 pub fn is_nan(self) -> bool {
211 $flttrait::is_nan(self.0)
212 }
213
214 /// Performs `is_nan` on each element of self, returning a vector mask of the results.
215 ///
216 /// In other words, this computes `[x.is_nan(), y.is_nan(), z.is_nan(), w.is_nan()]`.
217 #[inline(always)]
218 pub fn is_nan_mask(self) -> $mask {
219 $mask($flttrait::is_nan_mask(self.0))
220 }
221
222 /// Computes the length of `self`.
223 #[cfg_attr(docsrs, doc(alias = "magnitude"))]
224 #[inline(always)]
225 pub fn length(self) -> $t {
226 $flttrait::length(self.0)
227 }
228
229 /// Computes the squared length of `self`.
230 ///
231 /// This is faster than `length()` as it avoids a square root operation.
232 #[cfg_attr(docsrs, doc(alias = "magnitude2"))]
233 #[inline(always)]
234 pub fn length_squared(self) -> $t {
235 $flttrait::length_squared(self.0)
236 }
237
238 /// Computes `1.0 / length()`.
239 ///
240 /// For valid results, `self` must _not_ be of length zero.
241 #[inline(always)]
242 pub fn length_recip(self) -> $t {
243 $flttrait::length_recip(self.0)
244 }
245
246 /// Computes the Euclidean distance between two points in space.
247 #[inline]
248 pub fn distance(self, other: Self) -> $t {
249 (self - other).length()
250 }
251
252 /// Compute the squared euclidean distance between two points in space.
253 #[inline]
254 pub fn distance_squared(self, other: Self) -> $t {
255 (self - other).length_squared()
256 }
257
258 /// Returns `self` normalized to length 1.0.
259 ///
260 /// For valid results, `self` must _not_ be of length zero, nor very close to zero.
261 ///
262 /// See also [`Self::try_normalize`] and [`Self::normalize_or_zero`].
263 #[must_use]
264 #[inline(always)]
265 pub fn normalize(self) -> Self {
266 Self($flttrait::normalize(self.0))
267 }
268
269 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
270 ///
271 /// In particular, if the input is zero (or very close to zero), or non-finite,
272 /// the result of this operation will be `None`.
273 ///
274 /// See also [`Self::normalize_or_zero`].
275 #[must_use]
276 #[inline]
277 pub fn try_normalize(self) -> Option<Self> {
278 let rcp = self.length_recip();
279 if rcp.is_finite() && rcp > 0.0 {
280 Some(self * rcp)
281 } else {
282 None
283 }
284 }
285
286 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
287 ///
288 /// In particular, if the input is zero (or very close to zero), or non-finite,
289 /// the result of this operation will be zero.
290 ///
291 /// See also [`Self::try_normalize`].
292 #[must_use]
293 #[inline]
294 pub fn normalize_or_zero(self) -> Self {
295 let rcp = self.length_recip();
296 if rcp.is_finite() && rcp > 0.0 {
297 self * rcp
298 } else {
299 Self::ZERO
300 }
301 }
302
303 /// Returns whether `self` is length `1.0` or not.
304 ///
305 /// Uses a precision threshold of `1e-6`.
306 #[inline(always)]
307 pub fn is_normalized(self) -> bool {
308 $flttrait::is_normalized(self.0)
309 }
310
311 /// Returns a vector containing the nearest integer to a number for each element of `self`.
312 /// Round half-way cases away from 0.0.
313 #[inline(always)]
314 pub fn round(self) -> Self {
315 Self($flttrait::round(self.0))
316 }
317
318 /// Returns a vector containing the largest integer less than or equal to a number for each
319 /// element of `self`.
320 #[inline(always)]
321 pub fn floor(self) -> Self {
322 Self($flttrait::floor(self.0))
323 }
324
325 /// Returns a vector containing the smallest integer greater than or equal to a number for
326 /// each element of `self`.
327 #[inline(always)]
328 pub fn ceil(self) -> Self {
329 Self($flttrait::ceil(self.0))
330 }
331
332 /// Returns a vector containing the fractional part of the vector, e.g. `self -
333 /// self.floor()`.
334 ///
335 /// Note that this is fast but not precise for large numbers.
336 #[inline(always)]
337 pub fn fract(self) -> Self {
338 self - self.floor()
339 }
340
341 /// Returns a vector containing `e^self` (the exponential function) for each element of
342 /// `self`.
343 #[inline(always)]
344 pub fn exp(self) -> Self {
345 Self($flttrait::exp(self.0))
346 }
347
348 /// Returns a vector containing each element of `self` raised to the power of `n`.
349 #[inline(always)]
350 pub fn powf(self, n: $t) -> Self {
351 Self($flttrait::powf(self.0, n))
352 }
353
354 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
355 #[inline(always)]
356 pub fn recip(self) -> Self {
357 Self($flttrait::recip(self.0))
358 }
359
360 /// Performs a linear interpolation between `self` and `other` based on the value `s`.
361 ///
362 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
363 /// will be equal to `other`.
364 #[cfg_attr(docsrs, doc(alias = "mix"))]
365 #[inline]
366 pub fn lerp(self, other: Self, s: $t) -> Self {
367 self + ((other - self) * s)
368 }
369
370 /// Returns true if the absolute difference of all elements between `self` and `other` is
371 /// less than or equal to `max_abs_diff`.
372 ///
373 /// This can be used to compare if two vectors contain similar elements. It works best when
374 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
375 /// the values being compared against.
376 ///
377 /// For more see
378 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
379 #[inline(always)]
380 pub fn abs_diff_eq(self, other: Self, max_abs_diff: $t) -> bool {
381 $flttrait::abs_diff_eq(self.0, other.0, max_abs_diff)
382 }
383
384 /// Returns a vector with a length no less than `min` and no more than `max`
385 #[inline]
386 pub fn clamp_length(self, min: $t, max: $t) -> Self {
387 let length_sq = self.length_squared();
388 if length_sq < min * min {
389 self * (length_sq.sqrt().recip() * min)
390 } else if length_sq > max * max {
391 self * (length_sq.sqrt().recip() * max)
392 } else {
393 self
394 }
395 }
396
397 /// Returns a vector with a length no more than `max`
398 pub fn clamp_length_max(self, max: $t) -> Self {
399 let length_sq = self.length_squared();
400 if length_sq > max * max {
401 self * (length_sq.sqrt().recip() * max)
402 } else {
403 self
404 }
405 }
406
407 /// Returns a vector with a length no less than `min`
408 pub fn clamp_length_min(self, min: $t) -> Self {
409 let length_sq = self.length_squared();
410 if self.length_squared() < min * min {
411 self * (length_sq.sqrt().recip() * min)
412 } else {
413 self
414 }
415 }
416 };
417 }
418
419 // Adds common vector trait implementations.
420 // The traits here should be supported for all types of $t and all sizes of vector.
421 macro_rules! impl_vecn_common_traits {
422 ($t:ty, $size:literal, $vecn:ident, $inner:ident, $trait:ident) => {
423 impl Default for $vecn {
424 #[inline(always)]
425 fn default() -> Self {
426 Self($inner::ZERO)
427 }
428 }
429
430 impl PartialEq for $vecn {
431 #[inline(always)]
432 fn eq(&self, other: &Self) -> bool {
433 self.cmpeq(*other).all()
434 }
435 }
436
437 impl From<$vecn> for $inner {
438 #[inline(always)]
439 fn from(t: $vecn) -> Self {
440 t.0
441 }
442 }
443
444 impl From<$inner> for $vecn {
445 #[inline(always)]
446 fn from(t: $inner) -> Self {
447 Self(t)
448 }
449 }
450
451 impl Div<$vecn> for $vecn {
452 type Output = Self;
453 #[inline(always)]
454 fn div(self, other: $vecn) -> Self {
455 Self(self.0.div(other.0))
456 }
457 }
458
459 impl DivAssign<$vecn> for $vecn {
460 #[inline(always)]
461 fn div_assign(&mut self, other: $vecn) {
462 self.0 = self.0.div(other.0)
463 }
464 }
465
466 impl Div<$t> for $vecn {
467 type Output = Self;
468 #[inline(always)]
469 fn div(self, other: $t) -> Self {
470 Self(self.0.div_scalar(other))
471 }
472 }
473
474 impl DivAssign<$t> for $vecn {
475 #[inline(always)]
476 fn div_assign(&mut self, other: $t) {
477 self.0 = self.0.div_scalar(other)
478 }
479 }
480
481 impl Div<$vecn> for $t {
482 type Output = $vecn;
483 #[inline(always)]
484 fn div(self, other: $vecn) -> $vecn {
485 $vecn($inner::splat(self).div(other.0))
486 }
487 }
488
489 impl Mul<$vecn> for $vecn {
490 type Output = Self;
491 #[inline(always)]
492 fn mul(self, other: $vecn) -> Self {
493 Self(self.0.mul(other.0))
494 }
495 }
496
497 impl MulAssign<$vecn> for $vecn {
498 #[inline(always)]
499 fn mul_assign(&mut self, other: $vecn) {
500 self.0 = self.0.mul(other.0)
501 }
502 }
503
504 impl Mul<$t> for $vecn {
505 type Output = Self;
506 #[inline(always)]
507 fn mul(self, other: $t) -> Self {
508 Self(self.0.mul_scalar(other))
509 }
510 }
511
512 impl MulAssign<$t> for $vecn {
513 #[inline(always)]
514 fn mul_assign(&mut self, other: $t) {
515 self.0 = self.0.mul_scalar(other)
516 }
517 }
518
519 impl Mul<$vecn> for $t {
520 type Output = $vecn;
521 #[inline(always)]
522 fn mul(self, other: $vecn) -> $vecn {
523 $vecn($inner::splat(self).mul(other.0))
524 }
525 }
526
527 impl Add for $vecn {
528 type Output = Self;
529 #[inline(always)]
530 fn add(self, other: Self) -> Self {
531 Self(self.0.add(other.0))
532 }
533 }
534
535 impl AddAssign for $vecn {
536 #[inline(always)]
537 fn add_assign(&mut self, other: Self) {
538 self.0 = self.0.add(other.0)
539 }
540 }
541
542 impl Sub for $vecn {
543 type Output = Self;
544 #[inline(always)]
545 fn sub(self, other: $vecn) -> Self {
546 Self(self.0.sub(other.0))
547 }
548 }
549
550 impl SubAssign for $vecn {
551 #[inline(always)]
552 fn sub_assign(&mut self, other: $vecn) {
553 self.0 = self.0.sub(other.0)
554 }
555 }
556
557 impl AsRef<[$t; $size]> for $vecn {
558 #[inline(always)]
559 fn as_ref(&self) -> &[$t; $size] {
560 unsafe { &*(self as *const $vecn as *const [$t; $size]) }
561 }
562 }
563
564 impl From<[$t; $size]> for $vecn {
565 #[inline(always)]
566 fn from(a: [$t; $size]) -> Self {
567 Self($trait::from_array(a))
568 }
569 }
570
571 impl AsMut<[$t; $size]> for $vecn {
572 #[inline(always)]
573 fn as_mut(&mut self) -> &mut [$t; $size] {
574 unsafe { &mut *(self as *mut $vecn as *mut [$t; $size]) }
575 }
576 }
577
578 impl From<$vecn> for [$t; $size] {
579 #[inline(always)]
580 fn from(v: $vecn) -> Self {
581 v.into_array()
582 }
583 }
584
585 impl Index<usize> for $vecn {
586 type Output = $t;
587 #[inline(always)]
588 fn index(&self, index: usize) -> &Self::Output {
589 &self.as_ref()[index]
590 }
591 }
592
593 impl IndexMut<usize> for $vecn {
594 #[inline(always)]
595 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
596 &mut self.as_mut()[index]
597 }
598 }
599
600 #[cfg(feature = "std")]
601 impl<'a> Sum<&'a Self> for $vecn {
602 #[inline]
603 fn sum<I>(iter: I) -> Self
604 where
605 I: Iterator<Item = &'a Self>,
606 {
607 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
608 }
609 }
610
611 #[cfg(feature = "std")]
612 impl<'a> Product<&'a Self> for $vecn {
613 #[inline]
614 fn product<I>(iter: I) -> Self
615 where
616 I: Iterator<Item = &'a Self>,
617 {
618 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
619 }
620 }
621 };
622 }
623
624 macro_rules! impl_vecn_eq_hash_traits {
625 ($t:ty, $size:literal, $vecn:ident) => {
626 impl Eq for $vecn {}
627
628 impl core::hash::Hash for $vecn {
629 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
630 let inner: &[$t; $size] = self.as_ref();
631 inner.hash(state);
632 }
633 }
634 };
635 }
636
637 // Adds signed vector trait implementations.
638 // The traits here should be supported for signed types of $t and all sizes of vector.
639 macro_rules! impl_vecn_signed_traits {
640 ($t:ty, $size:literal, $vecn:ident, $inner:ident, $sgntrait:ident) => {
641 impl Neg for $vecn {
642 type Output = Self;
643 #[inline(always)]
644 fn neg(self) -> Self {
645 Self(self.0.neg())
646 }
647 }
648 };
649 }
650
651 macro_rules! impl_vecn_as_f32 {
652 ($ret:ident, $($e:ident),+) => {
653 /// Casts all elements of `self` to `f32`.
654 #[inline(always)]
655 pub fn as_f32(&self) -> $ret {
656 $ret::new($(self.$e as f32),+)
657 }
658 };
659 }
660
661 macro_rules! impl_vecn_as_f64 {
662 ($ret:ident, $($e:ident),+) => {
663 /// Casts all elements of `self` to `f64`.
664 #[inline(always)]
665 pub fn as_f64(&self) -> $ret {
666 $ret::new($(self.$e as f64),+)
667 }
668 };
669 }
670
671 macro_rules! impl_vecn_as_i32 {
672 ($ret:ident, $($e:ident),+) => {
673 /// Casts all elements of `self` to `i32`.
674 #[inline(always)]
675 pub fn as_i32(&self) -> $ret {
676 $ret::new($(self.$e as i32),+)
677 }
678 };
679 }
680
681 macro_rules! impl_vecn_as_u32 {
682 ($ret:ident, $($e:ident),+) => {
683 /// Casts all elements of `self` to `u32`.
684 #[inline(always)]
685 pub fn as_u32(&self) -> $ret {
686 $ret::new($(self.$e as u32),+)
687 }
688 };
689 }