Debian Codegraph @

debian/missing-sources/glam-0.24.2/src/f32/scalar/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, Mul, MulAssign, Neg, Sub, SubAssign};
8
9 /// Creates a 2x2 matrix from two column vectors.
10 #[inline(always)]
11 pub const fn mat2(x_axis: Vec2, y_axis: Vec2) -> Mat2 {
12 Mat2::from_cols(x_axis, y_axis)
13 }
14
15 /// A 2x2 column major matrix.
16 #[derive(Clone, Copy)]
17 #[cfg_attr(
18 not(any(feature = "scalar-math", target_arch = "spirv")),
19 repr(align(16))
20 )]
21 #[cfg_attr(feature = "cuda", repr(align(8)))]
22 #[repr(C)]
23 pub struct Mat2 {
24 pub x_axis: Vec2,
25 pub y_axis: Vec2,
26 }
27
28 impl Mat2 {
29 /// A 2x2 matrix with all elements set to `0.0`.
30 pub const ZERO: Self = Self::from_cols(Vec2::ZERO, Vec2::ZERO);
31
32 /// A 2x2 identity matrix, where all diagonal elements are `1`, and all off-diagonal elements are `0`.
33 pub const IDENTITY: Self = Self::from_cols(Vec2::X, Vec2::Y);
34
35 /// All NAN:s.
36 pub const NAN: Self = Self::from_cols(Vec2::NAN, Vec2::NAN);
37
38 #[allow(clippy::too_many_arguments)]
39 #[inline(always)]
40 const fn new(m00: f32, m01: f32, m10: f32, m11: f32) -> Self {
41 Self {
42 x_axis: Vec2::new(m00, m01),
43 y_axis: Vec2::new(m10, m11),
44 }
45 }
46
47 /// Creates a 2x2 matrix from two column vectors.
48 #[inline(always)]
49 pub const fn from_cols(x_axis: Vec2, y_axis: Vec2) -> Self {
50 Self { x_axis, y_axis }
51 }
52
53 /// Creates a 2x2 matrix from a `[f32; 4]` array stored in column major order.
54 /// If your data is stored in row major you will need to `transpose` the returned
55 /// matrix.
56 #[inline]
57 pub const fn from_cols_array(m: &[f32; 4]) -> Self {
58 Self::new(m[0], m[1], m[2], m[3])
59 }
60
61 /// Creates a `[f32; 4]` array storing data in column major order.
62 /// If you require data in row major order `transpose` the matrix first.
63 #[inline]
64 pub const fn to_cols_array(&self) -> [f32; 4] {
65 [self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
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 pub const fn from_cols_array_2d(m: &[[f32; 2]; 2]) -> Self {
73 Self::from_cols(Vec2::from_array(m[0]), Vec2::from_array(m[1]))
74 }
75
76 /// Creates a `[[f32; 2]; 2]` 2D array storing data in column major order.
77 /// If you require data in row major order `transpose` the matrix first.
78 #[inline]
79 pub const fn to_cols_array_2d(&self) -> [[f32; 2]; 2] {
80 [self.x_axis.to_array(), self.y_axis.to_array()]
81 }
82
83 /// Creates a 2x2 matrix with its diagonal set to `diagonal` and all other entries set to 0.
84 #[doc(alias = "scale")]
85 #[inline]
86 pub const fn from_diagonal(diagonal: Vec2) -> Self {
87 Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
88 }
89
90 /// Creates a 2x2 matrix containing the combining non-uniform `scale` and rotation of
91 /// `angle` (in radians).
92 #[inline]
93 pub fn from_scale_angle(scale: Vec2, angle: f32) -> Self {
94 let (sin, cos) = math::sin_cos(angle);
95 Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
96 }
97
98 /// Creates a 2x2 matrix containing a rotation of `angle` (in radians).
99 #[inline]
100 pub fn from_angle(angle: f32) -> Self {
101 let (sin, cos) = math::sin_cos(angle);
102 Self::new(cos, sin, -sin, cos)
103 }
104
105 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
106 #[inline]
107 pub fn from_mat3(m: Mat3) -> Self {
108 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
109 }
110
111 /// Creates a 2x2 matrix from a 3x3 matrix, discarding the 2nd row and column.
112 #[inline]
113 pub fn from_mat3a(m: Mat3A) -> Self {
114 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
115 }
116
117 /// Creates a 2x2 matrix from the first 4 values in `slice`.
118 ///
119 /// # Panics
120 ///
121 /// Panics if `slice` is less than 4 elements long.
122 #[inline]
123 pub const fn from_cols_slice(slice: &[f32]) -> Self {
124 Self::new(slice[0], slice[1], slice[2], slice[3])
125 }
126
127 /// Writes the columns of `self` to the first 4 elements in `slice`.
128 ///
129 /// # Panics
130 ///
131 /// Panics if `slice` is less than 4 elements long.
132 #[inline]
133 pub fn write_cols_to_slice(self, slice: &mut [f32]) {
134 slice[0] = self.x_axis.x;
135 slice[1] = self.x_axis.y;
136 slice[2] = self.y_axis.x;
137 slice[3] = self.y_axis.y;
138 }
139
140 /// Returns the matrix column for the given `index`.
141 ///
142 /// # Panics
143 ///
144 /// Panics if `index` is greater than 1.
145 #[inline]
146 pub fn col(&self, index: usize) -> Vec2 {
147 match index {
148 0 => self.x_axis,
149 1 => self.y_axis,
150 _ => panic!("index out of bounds"),
151 }
152 }
153
154 /// Returns a mutable reference to the matrix column for the given `index`.
155 ///
156 /// # Panics
157 ///
158 /// Panics if `index` is greater than 1.
159 #[inline]
160 pub fn col_mut(&mut self, index: usize) -> &mut Vec2 {
161 match index {
162 0 => &mut self.x_axis,
163 1 => &mut self.y_axis,
164 _ => panic!("index out of bounds"),
165 }
166 }
167
168 /// Returns the matrix row for the given `index`.
169 ///
170 /// # Panics
171 ///
172 /// Panics if `index` is greater than 1.
173 #[inline]
174 pub fn row(&self, index: usize) -> Vec2 {
175 match index {
176 0 => Vec2::new(self.x_axis.x, self.y_axis.x),
177 1 => Vec2::new(self.x_axis.y, self.y_axis.y),
178 _ => panic!("index out of bounds"),
179 }
180 }
181
182 /// Returns `true` if, and only if, all elements are finite.
183 /// If any element is either `NaN`, positive or negative infinity, this will return `false`.
184 #[inline]
185 pub fn is_finite(&self) -> bool {
186 self.x_axis.is_finite() && self.y_axis.is_finite()
187 }
188
189 /// Returns `true` if any elements are `NaN`.
190 #[inline]
191 pub fn is_nan(&self) -> bool {
192 self.x_axis.is_nan() || self.y_axis.is_nan()
193 }
194
195 /// Returns the transpose of `self`.
196 #[must_use]
197 #[inline]
198 pub fn transpose(&self) -> Self {
199 Self {
200 x_axis: Vec2::new(self.x_axis.x, self.y_axis.x),
201 y_axis: Vec2::new(self.x_axis.y, self.y_axis.y),
202 }
203 }
204
205 /// Returns the determinant of `self`.
206 #[inline]
207 pub fn determinant(&self) -> f32 {
208 self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
209 }
210
211 /// Returns the inverse of `self`.
212 ///
213 /// If the matrix is not invertible the returned matrix will be invalid.
214 ///
215 /// # Panics
216 ///
217 /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled.
218 #[must_use]
219 #[inline]
220 pub fn inverse(&self) -> Self {
221 let inv_det = {
222 let det = self.determinant();
223 glam_assert!(det != 0.0);
224 det.recip()
225 };
226 Self::new(
227 self.y_axis.y * inv_det,
228 self.x_axis.y * -inv_det,
229 self.y_axis.x * -inv_det,
230 self.x_axis.x * inv_det,
231 )
232 }
233
234 /// Transforms a 2D vector.
235 #[inline]
236 pub fn mul_vec2(&self, rhs: Vec2) -> Vec2 {
237 #[allow(clippy::suspicious_operation_groupings)]
238 Vec2::new(
239 (self.x_axis.x * rhs.x) + (self.y_axis.x * rhs.y),
240 (self.x_axis.y * rhs.x) + (self.y_axis.y * rhs.y),
241 )
242 }
243
244 /// Multiplies two 2x2 matrices.
245 #[inline]
246 pub fn mul_mat2(&self, rhs: &Self) -> Self {
247 Self::from_cols(self.mul(rhs.x_axis), self.mul(rhs.y_axis))
248 }
249
250 /// Adds two 2x2 matrices.
251 #[inline]
252 pub fn add_mat2(&self, rhs: &Self) -> Self {
253 Self::from_cols(self.x_axis.add(rhs.x_axis), self.y_axis.add(rhs.y_axis))
254 }
255
256 /// Subtracts two 2x2 matrices.
257 #[inline]
258 pub fn sub_mat2(&self, rhs: &Self) -> Self {
259 Self::from_cols(self.x_axis.sub(rhs.x_axis), self.y_axis.sub(rhs.y_axis))
260 }
261
262 /// Multiplies a 2x2 matrix by a scalar.
263 #[inline]
264 pub fn mul_scalar(&self, rhs: f32) -> Self {
265 Self::from_cols(self.x_axis.mul(rhs), self.y_axis.mul(rhs))
266 }
267
268 /// Returns true if the absolute difference of all elements between `self` and `rhs`
269 /// is less than or equal to `max_abs_diff`.
270 ///
271 /// This can be used to compare if two matrices contain similar elements. It works best
272 /// when comparing with a known value. The `max_abs_diff` that should be used used
273 /// depends on the values being compared against.
274 ///
275 /// For more see
276 /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/).
277 #[inline]
278 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
279 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
280 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
281 }
282
283 #[inline]
284 pub fn as_dmat2(&self) -> DMat2 {
285 DMat2::from_cols(self.x_axis.as_dvec2(), self.y_axis.as_dvec2())
286 }
287 }
288
289 impl Default for Mat2 {
290 #[inline]
291 fn default() -> Self {
292 Self::IDENTITY
293 }
294 }
295
296 impl Add<Mat2> for Mat2 {
297 type Output = Self;
298 #[inline]
299 fn add(self, rhs: Self) -> Self::Output {
300 self.add_mat2(&rhs)
301 }
302 }
303
304 impl AddAssign<Mat2> for Mat2 {
305 #[inline]
306 fn add_assign(&mut self, rhs: Self) {
307 *self = self.add_mat2(&rhs);
308 }
309 }
310
311 impl Sub<Mat2> for Mat2 {
312 type Output = Self;
313 #[inline]
314 fn sub(self, rhs: Self) -> Self::Output {
315 self.sub_mat2(&rhs)
316 }
317 }
318
319 impl SubAssign<Mat2> for Mat2 {
320 #[inline]
321 fn sub_assign(&mut self, rhs: Self) {
322 *self = self.sub_mat2(&rhs);
323 }
324 }
325
326 impl Neg for Mat2 {
327 type Output = Self;
328 #[inline]
329 fn neg(self) -> Self::Output {
330 Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
331 }
332 }
333
334 impl Mul<Mat2> for Mat2 {
335 type Output = Self;
336 #[inline]
337 fn mul(self, rhs: Self) -> Self::Output {
338 self.mul_mat2(&rhs)
339 }
340 }
341
342 impl MulAssign<Mat2> for Mat2 {
343 #[inline]
344 fn mul_assign(&mut self, rhs: Self) {
345 *self = self.mul_mat2(&rhs);
346 }
347 }
348
349 impl Mul<Vec2> for Mat2 {
350 type Output = Vec2;
351 #[inline]
352 fn mul(self, rhs: Vec2) -> Self::Output {
353 self.mul_vec2(rhs)
354 }
355 }
356
357 impl Mul<Mat2> for f32 {
358 type Output = Mat2;
359 #[inline]
360 fn mul(self, rhs: Mat2) -> Self::Output {
361 rhs.mul_scalar(self)
362 }
363 }
364
365 impl Mul<f32> for Mat2 {
366 type Output = Self;
367 #[inline]
368 fn mul(self, rhs: f32) -> Self::Output {
369 self.mul_scalar(rhs)
370 }
371 }
372
373 impl MulAssign<f32> for Mat2 {
374 #[inline]
375 fn mul_assign(&mut self, rhs: f32) {
376 *self = self.mul_scalar(rhs);
377 }
378 }
379
380 impl Sum<Self> for Mat2 {
381 fn sum<I>(iter: I) -> Self
382 where
383 I: Iterator<Item = Self>,
384 {
385 iter.fold(Self::ZERO, Self::add)
386 }
387 }
388
389 impl<'a> Sum<&'a Self> for Mat2 {
390 fn sum<I>(iter: I) -> Self
391 where
392 I: Iterator<Item = &'a Self>,
393 {
394 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
395 }
396 }
397
398 impl Product for Mat2 {
399 fn product<I>(iter: I) -> Self
400 where
401 I: Iterator<Item = Self>,
402 {
403 iter.fold(Self::IDENTITY, Self::mul)
404 }
405 }
406
407 impl<'a> Product<&'a Self> for Mat2 {
408 fn product<I>(iter: I) -> Self
409 where
410 I: Iterator<Item = &'a Self>,
411 {
412 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
413 }
414 }
415
416 impl PartialEq for Mat2 {
417 #[inline]
418 fn eq(&self, rhs: &Self) -> bool {
419 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
420 }
421 }
422
423 #[cfg(not(target_arch = "spirv"))]
424 impl AsRef<[f32; 4]> for Mat2 {
425 #[inline]
426 fn as_ref(&self) -> &[f32; 4] {
427 unsafe { &*(self as *const Self as *const [f32; 4]) }
428 }
429 }
430
431 #[cfg(not(target_arch = "spirv"))]
432 impl AsMut<[f32; 4]> for Mat2 {
433 #[inline]
434 fn as_mut(&mut self) -> &mut [f32; 4] {
435 unsafe { &mut *(self as *mut Self as *mut [f32; 4]) }
436 }
437 }
438
439 #[cfg(not(target_arch = "spirv"))]
440 impl fmt::Debug for Mat2 {
441 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
442 fmt.debug_struct(stringify!(Mat2))
443 .field("x_axis", &self.x_axis)
444 .field("y_axis", &self.y_axis)
445 .finish()
446 }
447 }
448
449 #[cfg(not(target_arch = "spirv"))]
450 impl fmt::Display for Mat2 {
451 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
452 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
453 }
454 }