Debian Codegraph @

debian/missing-sources/glam-0.27.0/src/f32/wasm32/mat2.rs

raw
1 // Generated from mat.rs.tera template. Edit the template, not the generated file.
2
3 use crate::{f32::math, swizzles::*, DMat2, Mat3, Mat3A, Vec2};
4 #[cfg(not(target_arch = "spirv"))]
5 use core::fmt;
6 use core::iter::{Product, Sum};
7 use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9 use core::arch::wasm32::*;
10
11 /// Creates a 2x2 matrix from two column vectors.
12 #[inline(always)]
13 #[must_use]
14 pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
15 Mat2::from_cols(x_axis, y_axis)
16 }
17
18 /// A 2x2 column major matrix.
19 ///
20 /// SIMD vector types are used for storage on supported platforms.
21 ///
22 /// This type is 16 byte aligned.
23 #[derive(Clone, Copy)]
24 #[repr(transparent)]
25 pub struct Mat2(pub(crate) v128);
26
27 impl Mat2 {
28 /// A 2x2 matrix with all elements set to `0.0`.
29 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
30
31 /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
32 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
33
34 /// All NAN:s.
35 pub const NAN: Self = Self::from_cols(Vec2::NAN, Vec2::NAN);
36
37 #[allow(clippy::too_many_arguments)]
38 #[inline(always)]
39 #[must_use]
40 const fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
41 Self(f32x4(m00, m01, m10, m11))
42 }
43
44 /// Creates a 2x2 matrix from two column vectors.
45 #[inline(always)]
46 #[must_use]
47 pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
48 Self(f32x4(x_axis.x, x_axis.y, y_axis.x, y_axis.y))
49 }
50
51 /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
52 /// If your data is stored in row major you will need to `transpose` the returned
53 /// matrix.
54 #[inline]
55 #[must_use]
56 pub const fn from_cols_array(m: &[f32; 4]) -> Self {
57 Self::new(m[0], m[1], m[2], m[3])
58 }
59
60 /// Creates a `[f32; 4]` array storing data in column major order.
61 /// If you require data in row major order `transpose` the matrix first.
62 #[inline]
63 #[must_use]
64 pub const fn to_cols_array(&self) -> [f32; 4] {
65 unsafe { *(self as *const Self as *const [f32; 4]) }
66 }
67
68 /// Creates a 2x2 matrix from a `[[f32; 2]; 2]` 2D array stored in column major order.
69 /// If your data is in row major order you will need to `transpose` the returned
70 /// matrix.
71 #[inline]
72 #[must_use]
73 pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
74 Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
75 }
76
77 /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
78 /// If you require data in row major order `transpose` the matrix first.
79 #[inline]
80 #[must_use]
81 pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
82 unsafe { *(self as *const Self as *const [[f32; 2]; 2]) }
83 }
84
85 /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
86 #[doc(alias = "scale")]
87 #[inline]
88 #[must_use]
89 pub const fn from_diagonal(diagonal: Vec2) -> Self {
90 Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
91 }
92
93 /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
94 /// `angle` (in radians).
95 #[inline]
96 #[must_use]
97 pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
98 let (sin, cos) = math::sin_cos(angle);
99 Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
100 }
101
102 /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
103 #[inline]
104 #[must_use]
105 pub fn from_angle(angle: f32) -> Self {
106 let (sin, cos) = math::sin_cos(angle);
107 Self::new(cos, sin, -sin, cos)
108 }
109
110 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
111 #[inline]
112 #[must_use]
113 pub fn from_mat3(m: Mat3) -> Self {
114 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
115 }
116
117 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
118 #[inline]
119 #[must_use]
120 pub fn from_mat3a(m: Mat3A) -> Self {
121 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
122 }
123
124 /// Creates a 2x2 matrix from the first 4 values in `slice`.
125 ///
126 /// # Panics
127 ///
128 /// Panics if `slice` is less than 4 elements long.
129 #[inline]
130 #[must_use]
131 pub const fn from_cols_slice(slice: &[f32]) -> Self {
132 Self::new(slice[0], slice[1], slice[2], slice[3])
133 }
134
135 /// Writes the columns of `self` to the first 4 elements in `slice`.
136 ///
137 /// # Panics
138 ///
139 /// Panics if `slice` is less than 4 elements long.
140 #[inline]
141 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
142 slice[0] = self.x_axis.x;
143 slice[1] = self.x_axis.y;
144 slice[2] = self.y_axis.x;
145 slice[3] = self.y_axis.y;
146 }
147
148 /// Returns the matrix column for the given `index`.
149 ///
150 /// # Panics
151 ///
152 /// Panics if `index` is greater than 1.
153 #[inline]
154 #[must_use]
155 pub fn col(&self, index: usize) -> Vec2 {
156 match index {
157 0 => self.x_axis,
158 1 => self.y_axis,
159 _ => panic!("index out of bounds"),
160 }
161 }
162
163 /// Returns a mutable reference to the matrix column for the given `index`.
164 ///
165 /// # Panics
166 ///
167 /// Panics if `index` is greater than 1.
168 #[inline]
169 pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
170 match index {
171 0 => &mut self.x_axis,
172 1 => &mut self.y_axis,
173 _ => panic!("index out of bounds"),
174 }
175 }
176
177 /// Returns the matrix row for the given `index`.
178 ///
179 /// # Panics
180 ///
181 /// Panics if `index` is greater than 1.
182 #[inline]
183 #[must_use]
184 pub fn row(&self, index: usize) -> Vec2 {
185 match index {
186 0 => Vec2::new(self.x_axis.x, self.y_axis.x),
187 1 => Vec2::new(self.x_axis.y, self.y_axis.y),
188 _ => panic!("index out of bounds"),
189 }
190 }
191
192 /// Returns `true` if, and only if, all elements are finite.
193 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
194 #[inline]
195 #[must_use]
196 pub fn is_finite(&self) -> bool {
197 self.x_axis.is_finite() && self.y_axis.is_finite()
198 }
199
200 /// Returns `true` if any elements are `NaN`.
201 #[inline]
202 #[must_use]
203 pub fn is_nan(&self) -> bool {
204 self.x_axis.is_nan() || self.y_axis.is_nan()
205 }
206
207 /// Returns the transpose of `self`.
208 #[inline]
209 #[must_use]
210 pub fn transpose(&self) -> Self {
211 Self(i32x4_shuffle::<0, 2, 5, 7>(self.0, self.0))
212 }
213
214 /// Returns the determinant of `self`.
215 #[inline]
216 #[must_use]
217 pub fn determinant(&self) -> f32 {
218 let abcd = self.0;
219 let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd);
220 let prod = f32x4_mul(abcd, dcba);
221 let det = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod));
222 f32x4_extract_lane::<0>(det)
223 }
224
225 /// Returns the inverse of `self`.
226 ///
227 /// If the matrix is not invertible the returned matrix will be invalid.
228 ///
229 /// # Panics
230 ///
231 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
232 #[inline]
233 #[must_use]
234 pub fn inverse(&self) -> Self {
235 const SIGN: v128 = crate::wasm32::v128_from_f32x4([1.0, -1.0, -1.0, 1.0]);
236 let abcd = self.0;
237 let dcba = i32x4_shuffle::<3, 2, 5, 4>(abcd, abcd);
238 let prod = f32x4_mul(abcd, dcba);
239 let sub = f32x4_sub(prod, i32x4_shuffle::<1, 1, 5, 5>(prod, prod));
240 let det = i32x4_shuffle::<0, 0, 4, 4>(sub, sub);
241 let tmp = f32x4_div(SIGN, det);
242 glam_assert!(Mat2(tmp).is_finite());
243 let dbca = i32x4_shuffle::<3, 1, 6, 4>(abcd, abcd);
244 Self(f32x4_mul(dbca, tmp))
245 }
246
247 /// Transforms a 2D vector.
248 #[inline]
249 #[must_use]
250 pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
251 use core::mem::MaybeUninit;
252 let abcd = self.0;
253 let xxyy = f32x4(rhs.x, rhs.x, rhs.y, rhs.y);
254 let axbxcydy = f32x4_mul(abcd, xxyy);
255 let cydyaxbx = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy, axbxcydy);
256 let result = f32x4_add(axbxcydy, cydyaxbx);
257 let mut out: MaybeUninit<v128> = MaybeUninit::uninit();
258 unsafe {
259 v128_store(out.as_mut_ptr(), result);
260 *(&out.assume_init() as *const v128 as *const Vec2)
261 }
262 }
263
264 /// Multiplies two 2x2 matrices.
265 #[inline]
266 #[must_use]
267 pub fn mul_mat2(&self, rhs: &Self) -> Self {
268 let abcd = self.0;
269 let rhs = rhs.0;
270 let xxyy0 = i32x4_shuffle::<0, 0, 5, 5>(rhs, rhs);
271 let xxyy1 = i32x4_shuffle::<2, 2, 7, 7>(rhs, rhs);
272 let axbxcydy0 = f32x4_mul(abcd, xxyy0);
273 let axbxcydy1 = f32x4_mul(abcd, xxyy1);
274 let cydyaxbx0 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy0, axbxcydy0);
275 let cydyaxbx1 = i32x4_shuffle::<2, 3, 4, 5>(axbxcydy1, axbxcydy1);
276 let result0 = f32x4_add(axbxcydy0, cydyaxbx0);
277 let result1 = f32x4_add(axbxcydy1, cydyaxbx1);
278 Self(i32x4_shuffle::<0, 1, 4, 5>(result0, result1))
279 }
280
281 /// Adds two 2x2 matrices.
282 #[inline]
283 #[must_use]
284 pub fn add_mat2(&self, rhs: &Self) -> Self {
285 Self(f32x4_add(self.0, rhs.0))
286 }
287
288 /// Subtracts two 2x2 matrices.
289 #[inline]
290 #[must_use]
291 pub fn sub_mat2(&self, rhs: &Self) -> Self {
292 Self(f32x4_sub(self.0, rhs.0))
293 }
294
295 /// Multiplies a 2x2 matrix by a scalar.
296 #[inline]
297 #[must_use]
298 pub fn mul_scalar(&self, rhs: f32) -> Self {
299 Self(f32x4_mul(self.0, f32x4_splat(rhs)))
300 }
301
302 /// Divides a 2x2 matrix by a scalar.
303 #[inline]
304 #[must_use]
305 pub fn div_scalar(&self, rhs: f32) -> Self {
306 Self(f32x4_div(self.0, f32x4_splat(rhs)))
307 }
308
309 /// Returns true if the absolute difference of all elements between `self` and `rhs`
310 /// is less than or equal to `max_abs_diff`.
311 ///
312 /// This can be used to compare if two matrices contain similar elements. It works best
313 /// when comparing with a known value. The `max_abs_diff` that should be used used
314 /// depends on the values being compared against.
315 ///
316 /// For more see
317 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
318 #[inline]
319 #[must_use]
320 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
321 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
322 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
323 }
324
325 /// Takes the absolute value of each element in `self`
326 #[inline]
327 #[must_use]
328 pub fn abs(&self) -> Self {
329 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
330 }
331
332 #[inline]
333 pub fn as_dmat2(&self) -> DMat2 {
334 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
335 }
336 }
337
338 impl Default for Mat2 {
339 #[inline]
340 fn default() -> Self {
341 Self::IDENTITY
342 }
343 }
344
345 impl Add<Mat2> for Mat2 {
346 type Output = Self;
347 #[inline]
348 fn add(self, rhs: Self) -> Self::Output {
349 self.add_mat2(&rhs)
350 }
351 }
352
353 impl AddAssign<Mat2> for Mat2 {
354 #[inline]
355 fn add_assign(&mut self, rhs: Self) {
356 *self = self.add_mat2(&rhs);
357 }
358 }
359
360 impl Sub<Mat2> for Mat2 {
361 type Output = Self;
362 #[inline]
363 fn sub(self, rhs: Self) -> Self::Output {
364 self.sub_mat2(&rhs)
365 }
366 }
367
368 impl SubAssign<Mat2> for Mat2 {
369 #[inline]
370 fn sub_assign(&mut self, rhs: Self) {
371 *self = self.sub_mat2(&rhs);
372 }
373 }
374
375 impl Neg for Mat2 {
376 type Output = Self;
377 #[inline]
378 fn neg(self) -> Self::Output {
379 Self(f32x4_neg(self.0))
380 }
381 }
382
383 impl Mul<Mat2> for Mat2 {
384 type Output = Self;
385 #[inline]
386 fn mul(self, rhs: Self) -> Self::Output {
387 self.mul_mat2(&rhs)
388 }
389 }
390
391 impl MulAssign<Mat2> for Mat2 {
392 #[inline]
393 fn mul_assign(&mut self, rhs: Self) {
394 *self = self.mul_mat2(&rhs);
395 }
396 }
397
398 impl Mul<Vec2> for Mat2 {
399 type Output = Vec2;
400 #[inline]
401 fn mul(self, rhs: Vec2) -> Self::Output {
402 self.mul_vec2(rhs)
403 }
404 }
405
406 impl Mul<Mat2> for f32 {
407 type Output = Mat2;
408 #[inline]
409 fn mul(self, rhs: Mat2) -> Self::Output {
410 rhs.mul_scalar(self)
411 }
412 }
413
414 impl Mul<f32> for Mat2 {
415 type Output = Self;
416 #[inline]
417 fn mul(self, rhs: f32) -> Self::Output {
418 self.mul_scalar(rhs)
419 }
420 }
421
422 impl MulAssign<f32> for Mat2 {
423 #[inline]
424 fn mul_assign(&mut self, rhs: f32) {
425 *self = self.mul_scalar(rhs);
426 }
427 }
428
429 impl Div<Mat2> for f32 {
430 type Output = Mat2;
431 #[inline]
432 fn div(self, rhs: Mat2) -> Self::Output {
433 rhs.div_scalar(self)
434 }
435 }
436
437 impl Div<f32> for Mat2 {
438 type Output = Self;
439 #[inline]
440 fn div(self, rhs: f32) -> Self::Output {
441 self.div_scalar(rhs)
442 }
443 }
444
445 impl DivAssign<f32> for Mat2 {
446 #[inline]
447 fn div_assign(&mut self, rhs: f32) {
448 *self = self.div_scalar(rhs);
449 }
450 }
451
452 impl Sum<Self> for Mat2 {
453 fn sum<I>(iter: I) -> Self
454 where
455 I: Iterator<Item = Self>,
456 {
457 iter.fold(Self::ZERO, Self::add)
458 }
459 }
460
461 impl<'a> Sum<&'a Self> for Mat2 {
462 fn sum<I>(iter: I) -> Self
463 where
464 I: Iterator<Item = &'a Self>,
465 {
466 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
467 }
468 }
469
470 impl Product for Mat2 {
471 fn product<I>(iter: I) -> Self
472 where
473 I: Iterator<Item = Self>,
474 {
475 iter.fold(Self::IDENTITY, Self::mul)
476 }
477 }
478
479 impl<'a> Product<&'a Self> for Mat2 {
480 fn product<I>(iter: I) -> Self
481 where
482 I: Iterator<Item = &'a Self>,
483 {
484 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
485 }
486 }
487
488 impl PartialEq for Mat2 {
489 #[inline]
490 fn eq(&self, rhs: &Self) -> bool {
491 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
492 }
493 }
494
495 #[cfg(not(target_arch = "spirv"))]
496 impl AsRef<[f32; 4]> for Mat2 {
497 #[inline]
498 fn as_ref(&self) -> &[f32; 4] {
499 unsafe { &*(self as *const Self as *const [f32; 4]) }
500 }
501 }
502
503 #[cfg(not(target_arch = "spirv"))]
504 impl AsMut<[f32; 4]> for Mat2 {
505 #[inline]
506 fn as_mut(&mut self) -> &mut [f32; 4] {
507 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
508 }
509 }
510
511 impl core::ops::Deref for Mat2 {
512 type Target = crate::deref::Cols2<Vec2>;
513 #[inline]
514 fn deref(&self) -> &Self::Target {
515 unsafe { &*(self as *const Self as *const Self::Target) }
516 }
517 }
518
519 impl core::ops::DerefMut for Mat2 {
520 #[inline]
521 fn deref_mut(&mut self) -> &mut Self::Target {
522 unsafe { &mut *(self as *mut Self as *mut Self::Target) }
523 }
524 }
525
526 #[cfg(not(target_arch = "spirv"))]
527 impl fmt::Debug for Mat2 {
528 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
529 fmt.debug_struct(stringify!(Mat2))
530 .field("x_axis", &self.x_axis)
531 .field("y_axis", &self.y_axis)
532 .finish()
533 }
534 }
535
536 #[cfg(not(target_arch = "spirv"))]
537 impl fmt::Display for Mat2 {
538 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
539 if let Some(p) = f.precision() {
540 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
541 } else {
542 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
543 }
544 }
545 }