Debian Codegraph @

debian/missing-sources/glam-0.15.2/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 #[inline(always)]
264 pub fn normalize(self) -> Self {
265 Self($flttrait::normalize(self.0))
266 }
267
268 /// Returns `self` normalized to length 1.0 if possible, else returns `None`.
269 ///
270 /// In particular, if the input is zero (or very close to zero), or non-finite,
271 /// the result of this operation will be `None`.
272 ///
273 /// See also [`Self::normalize_or_zero`].
274 #[inline]
275 pub fn try_normalize(self) -> Option<Self> {
276 let rcp = self.length_recip();
277 if rcp.is_finite() && rcp > 0.0 {
278 Some(self * rcp)
279 } else {
280 None
281 }
282 }
283
284 /// Returns `self` normalized to length 1.0 if possible, else returns zero.
285 ///
286 /// In particular, if the input is zero (or very close to zero), or non-finite,
287 /// the result of this operation will be zero.
288 ///
289 /// See also [`Self::try_normalize`].
290 #[inline]
291 pub fn normalize_or_zero(self) -> Self {
292 let rcp = self.length_recip();
293 if rcp.is_finite() && rcp > 0.0 {
294 self * rcp
295 } else {
296 Self::ZERO
297 }
298 }
299
300 /// Returns whether `self` is length `1.0` or not.
301 ///
302 /// Uses a precision threshold of `1e-6`.
303 #[inline(always)]
304 pub fn is_normalized(self) -> bool {
305 $flttrait::is_normalized(self.0)
306 }
307
308 /// Returns a vector containing the nearest integer to a number for each element of `self`.
309 /// Round half-way cases away from 0.0.
310 #[inline(always)]
311 pub fn round(self) -> Self {
312 Self($flttrait::round(self.0))
313 }
314
315 /// Returns a vector containing the largest integer less than or equal to a number for each
316 /// element of `self`.
317 #[inline(always)]
318 pub fn floor(self) -> Self {
319 Self($flttrait::floor(self.0))
320 }
321
322 /// Returns a vector containing the smallest integer greater than or equal to a number for
323 /// each element of `self`.
324 #[inline(always)]
325 pub fn ceil(self) -> Self {
326 Self($flttrait::ceil(self.0))
327 }
328
329 /// Returns a vector containing `e^self` (the exponential function) for each element of
330 /// `self`.
331 #[inline(always)]
332 pub fn exp(self) -> Self {
333 Self($flttrait::exp(self.0))
334 }
335
336 /// Returns a vector containing each element of `self` raised to the power of `n`.
337 #[inline(always)]
338 pub fn powf(self, n: $t) -> Self {
339 Self($flttrait::powf(self.0, n))
340 }
341
342 /// Returns a vector containing the reciprocal `1.0/n` of each element of `self`.
343 #[inline(always)]
344 pub fn recip(self) -> Self {
345 Self($flttrait::recip(self.0))
346 }
347
348 /// Performs a linear interpolation between `self` and `other` based on the value `s`.
349 ///
350 /// When `s` is `0.0`, the result will be equal to `self`. When `s` is `1.0`, the result
351 /// will be equal to `other`.
352 #[cfg_attr(docsrs, doc(alias = "mix"))]
353 #[inline]
354 pub fn lerp(self, other: Self, s: $t) -> Self {
355 self + ((other - self) * s)
356 }
357
358 /// Returns true if the absolute difference of all elements between `self` and `other` is
359 /// less than or equal to `max_abs_diff`.
360 ///
361 /// This can be used to compare if two vectors contain similar elements. It works best when
362 /// comparing with a known value. The `max_abs_diff` that should be used used depends on
363 /// the values being compared against.
364 ///
365 /// For more see
366 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
367 #[inline(always)]
368 pub fn abs_diff_eq(self, other: Self, max_abs_diff: $t) -> bool {
369 $flttrait::abs_diff_eq(self.0, other.0, max_abs_diff)
370 }
371
372 /// Returns a vector with a length no less than `min` and no more than `max`
373 #[inline]
374 pub fn clamp_length(self, min: $t, max: $t) -> Self {
375 let length_sq = self.length_squared();
376 if length_sq < min * min {
377 self * (length_sq.sqrt().recip() * min)
378 } else if length_sq > max * max {
379 self * (length_sq.sqrt().recip() * max)
380 } else {
381 self
382 }
383 }
384
385 /// Returns a vector with a length no more than `max`
386 pub fn clamp_length_max(self, max: $t) -> Self {
387 let length_sq = self.length_squared();
388 if length_sq > max * max {
389 self * (length_sq.sqrt().recip() * max)
390 } else {
391 self
392 }
393 }
394
395 /// Returns a vector with a length no less than `min`
396 pub fn clamp_length_min(self, min: $t) -> Self {
397 let length_sq = self.length_squared();
398 if self.length_squared() < min * min {
399 self * (length_sq.sqrt().recip() * min)
400 } else {
401 self
402 }
403 }
404 };
405 }
406
407 // Adds common vector trait implementations.
408 // The traits here should be supported for all types of $t and all sizes of vector.
409 macro_rules! impl_vecn_common_traits {
410 ($t:ty, $size:literal, $vecn:ident, $inner:ident, $trait:ident) => {
411 impl Default for $vecn {
412 #[inline(always)]
413 fn default() -> Self {
414 Self($inner::ZERO)
415 }
416 }
417
418 impl PartialEq for $vecn {
419 #[inline(always)]
420 fn eq(&self, other: &Self) -> bool {
421 self.cmpeq(*other).all()
422 }
423 }
424
425 impl From<$vecn> for $inner {
426 #[inline(always)]
427 fn from(t: $vecn) -> Self {
428 t.0
429 }
430 }
431
432 impl From<$inner> for $vecn {
433 #[inline(always)]
434 fn from(t: $inner) -> Self {
435 Self(t)
436 }
437 }
438
439 impl Div<$vecn> for $vecn {
440 type Output = Self;
441 #[inline(always)]
442 fn div(self, other: $vecn) -> Self {
443 Self(self.0.div(other.0))
444 }
445 }
446
447 impl DivAssign<$vecn> for $vecn {
448 #[inline(always)]
449 fn div_assign(&mut self, other: $vecn) {
450 self.0 = self.0.div(other.0)
451 }
452 }
453
454 impl Div<$t> for $vecn {
455 type Output = Self;
456 #[inline(always)]
457 fn div(self, other: $t) -> Self {
458 Self(self.0.div_scalar(other))
459 }
460 }
461
462 impl DivAssign<$t> for $vecn {
463 #[inline(always)]
464 fn div_assign(&mut self, other: $t) {
465 self.0 = self.0.div_scalar(other)
466 }
467 }
468
469 impl Div<$vecn> for $t {
470 type Output = $vecn;
471 #[inline(always)]
472 fn div(self, other: $vecn) -> $vecn {
473 $vecn($inner::splat(self).div(other.0))
474 }
475 }
476
477 impl Mul<$vecn> for $vecn {
478 type Output = Self;
479 #[inline(always)]
480 fn mul(self, other: $vecn) -> Self {
481 Self(self.0.mul(other.0))
482 }
483 }
484
485 impl MulAssign<$vecn> for $vecn {
486 #[inline(always)]
487 fn mul_assign(&mut self, other: $vecn) {
488 self.0 = self.0.mul(other.0)
489 }
490 }
491
492 impl Mul<$t> for $vecn {
493 type Output = Self;
494 #[inline(always)]
495 fn mul(self, other: $t) -> Self {
496 Self(self.0.mul_scalar(other))
497 }
498 }
499
500 impl MulAssign<$t> for $vecn {
501 #[inline(always)]
502 fn mul_assign(&mut self, other: $t) {
503 self.0 = self.0.mul_scalar(other)
504 }
505 }
506
507 impl Mul<$vecn> for $t {
508 type Output = $vecn;
509 #[inline(always)]
510 fn mul(self, other: $vecn) -> $vecn {
511 $vecn($inner::splat(self).mul(other.0))
512 }
513 }
514
515 impl Add for $vecn {
516 type Output = Self;
517 #[inline(always)]
518 fn add(self, other: Self) -> Self {
519 Self(self.0.add(other.0))
520 }
521 }
522
523 impl AddAssign for $vecn {
524 #[inline(always)]
525 fn add_assign(&mut self, other: Self) {
526 self.0 = self.0.add(other.0)
527 }
528 }
529
530 impl Sub for $vecn {
531 type Output = Self;
532 #[inline(always)]
533 fn sub(self, other: $vecn) -> Self {
534 Self(self.0.sub(other.0))
535 }
536 }
537
538 impl SubAssign for $vecn {
539 #[inline(always)]
540 fn sub_assign(&mut self, other: $vecn) {
541 self.0 = self.0.sub(other.0)
542 }
543 }
544
545 impl AsRef<[$t; $size]> for $vecn {
546 #[inline(always)]
547 fn as_ref(&self) -> &[$t; $size] {
548 unsafe { &*(self as *const $vecn as *const [$t; $size]) }
549 }
550 }
551
552 impl From<[$t; $size]> for $vecn {
553 #[inline(always)]
554 fn from(a: [$t; $size]) -> Self {
555 Self($trait::from_array(a))
556 }
557 }
558
559 impl AsMut<[$t; $size]> for $vecn {
560 #[inline(always)]
561 fn as_mut(&mut self) -> &mut [$t; $size] {
562 unsafe { &mut *(self as *mut $vecn as *mut [$t; $size]) }
563 }
564 }
565
566 impl From<$vecn> for [$t; $size] {
567 #[inline(always)]
568 fn from(v: $vecn) -> Self {
569 v.into_array()
570 }
571 }
572
573 impl Index<usize> for $vecn {
574 type Output = $t;
575 #[inline(always)]
576 fn index(&self, index: usize) -> &Self::Output {
577 &self.as_ref()[index]
578 }
579 }
580
581 impl IndexMut<usize> for $vecn {
582 #[inline(always)]
583 fn index_mut(&mut self, index: usize) -> &mut Self::Output {
584 &mut self.as_mut()[index]
585 }
586 }
587
588 #[cfg(feature = "std")]
589 impl<'a> Sum<&'a Self> for $vecn {
590 #[inline]
591 fn sum<I>(iter: I) -> Self
592 where
593 I: Iterator<Item = &'a Self>,
594 {
595 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
596 }
597 }
598
599 #[cfg(feature = "std")]
600 impl<'a> Product<&'a Self> for $vecn {
601 #[inline]
602 fn product<I>(iter: I) -> Self
603 where
604 I: Iterator<Item = &'a Self>,
605 {
606 iter.fold(Self::ONE, |a, &b| Self::mul(a, b))
607 }
608 }
609 };
610 }
611
612 macro_rules! impl_vecn_eq_hash_traits {
613 ($t:ty, $size:literal, $vecn:ident) => {
614 impl Eq for $vecn {}
615
616 impl core::hash::Hash for $vecn {
617 fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
618 let inner: &[$t; $size] = self.as_ref();
619 inner.hash(state);
620 }
621 }
622 };
623 }
624
625 // Adds signed vector trait implementations.
626 // The traits here should be supported for signed types of $t and all sizes of vector.
627 macro_rules! impl_vecn_signed_traits {
628 ($t:ty, $size:literal, $vecn:ident, $inner:ident, $sgntrait:ident) => {
629 impl Neg for $vecn {
630 type Output = Self;
631 #[inline(always)]
632 fn neg(self) -> Self {
633 Self(self.0.neg())
634 }
635 }
636 };
637 }
638
639 macro_rules! impl_vecn_as_f32 {
640 ($ret:ident, $($e:ident),+) => {
641 /// Casts all elements of `self` to `f32`.
642 #[inline(always)]
643 pub fn as_f32(&self) -> $ret {
644 $ret::new($(self.$e as f32),+)
645 }
646 };
647 }
648
649 macro_rules! impl_vecn_as_f64 {
650 ($ret:ident, $($e:ident),+) => {
651 /// Casts all elements of `self` to `f64`.
652 #[inline(always)]
653 pub fn as_f64(&self) -> $ret {
654 $ret::new($(self.$e as f64),+)
655 }
656 };
657 }
658
659 macro_rules! impl_vecn_as_i32 {
660 ($ret:ident, $($e:ident),+) => {
661 /// Casts all elements of `self` to `i32`.
662 #[inline(always)]
663 pub fn as_i32(&self) -> $ret {
664 $ret::new($(self.$e as i32),+)
665 }
666 };
667 }
668
669 macro_rules! impl_vecn_as_u32 {
670 ($ret:ident, $($e:ident),+) => {
671 /// Casts all elements of `self` to `u32`.
672 #[inline(always)]
673 pub fn as_u32(&self) -> $ret {
674 $ret::new($(self.$e as u32),+)
675 }
676 };
677 }