Debian Codegraph @

debian/missing-sources/glam-0.21.3/src/f32/scalar/mat2.rs

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