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