1use crate::{f64::math, swizzles::*, DMat3, DVec2, Mat2};
4#[cfg(not(target_arch = "spirv"))]
5use core::fmt;
6use core::iter::{Product, Sum};
7use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
8
9#[inline(always)]
11#[must_use]
12pub const fn dmat2(x_axis: DVec2, y_axis: DVec2) -> DMat2 {
13 DMat2::from_cols(x_axis, y_axis)
14}
15
16#[derive(Clone, Copy)]
18#[cfg_attr(feature = "cuda", repr(align(16)))]
19#[repr(C)]
20pub struct DMat2 {
21 pub x_axis: DVec2,
22 pub y_axis: DVec2,
23}
24
25impl DMat2 {
26 pub const ZERO: Self = Self::from_cols(DVec2::ZERO, DVec2::ZERO);
28
29 pub const IDENTITY: Self = Self::from_cols(DVec2::X, DVec2::Y);
31
32 pub const NAN: Self = Self::from_cols(DVec2::NAN, DVec2::NAN);
34
35 #[allow(clippy::too_many_arguments)]
36 #[inline(always)]
37 #[must_use]
38 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 #[inline(always)]
47 #[must_use]
48 pub const fn from_cols(x_axis: DVec2, y_axis: DVec2) -> Self {
49 Self { x_axis, y_axis }
50 }
51
52 #[inline]
56 #[must_use]
57 pub const fn from_cols_array(m: &[f64; 4]) -> Self {
58 Self::new(m[0], m[1], m[2], m[3])
59 }
60
61 #[inline]
64 #[must_use]
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 #[inline]
73 #[must_use]
74 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 #[inline]
81 #[must_use]
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 #[doc(alias = "scale")]
88 #[inline]
89 #[must_use]
90 pub const fn from_diagonal(diagonal: DVec2) -> Self {
91 Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
92 }
93
94 #[inline]
97 #[must_use]
98 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 #[inline]
105 #[must_use]
106 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 #[inline]
113 #[must_use]
114 pub fn from_mat3(m: DMat3) -> Self {
115 Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
116 }
117
118 #[inline]
124 #[must_use]
125 pub const fn from_cols_slice(slice: &[f64]) -> Self {
126 Self::new(slice[0], slice[1], slice[2], slice[3])
127 }
128
129 #[inline]
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 #[inline]
148 #[must_use]
149 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 #[inline]
163 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 #[inline]
177 #[must_use]
178 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 #[inline]
189 #[must_use]
190 pub fn is_finite(&self) -> bool {
191 self.x_axis.is_finite() && self.y_axis.is_finite()
192 }
193
194 #[inline]
196 #[must_use]
197 pub fn is_nan(&self) -> bool {
198 self.x_axis.is_nan() || self.y_axis.is_nan()
199 }
200
201 #[inline]
203 #[must_use]
204 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 #[inline]
213 #[must_use]
214 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 #[inline]
226 #[must_use]
227 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 #[inline]
243 #[must_use]
244 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 #[inline]
254 #[must_use]
255 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 #[inline]
261 #[must_use]
262 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 #[inline]
268 #[must_use]
269 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 #[inline]
275 #[must_use]
276 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 #[inline]
282 #[must_use]
283 pub fn div_scalar(&self, rhs: f64) -> Self {
284 let rhs = DVec2::splat(rhs);
285 Self::from_cols(self.x_axis.div(rhs), self.y_axis.div(rhs))
286 }
287
288 #[inline]
298 #[must_use]
299 pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
300 self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
301 && self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
302 }
303
304 #[inline]
306 #[must_use]
307 pub fn abs(&self) -> Self {
308 Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
309 }
310
311 #[inline]
312 pub fn as_mat2(&self) -> Mat2 {
313 Mat2::from_cols(self.x_axis.as_vec2(), self.y_axis.as_vec2())
314 }
315}
316
317impl Default for DMat2 {
318 #[inline]
319 fn default() -> Self {
320 Self::IDENTITY
321 }
322}
323
324impl Add<DMat2> for DMat2 {
325 type Output = Self;
326 #[inline]
327 fn add(self, rhs: Self) -> Self::Output {
328 self.add_mat2(&rhs)
329 }
330}
331
332impl AddAssign<DMat2> for DMat2 {
333 #[inline]
334 fn add_assign(&mut self, rhs: Self) {
335 *self = self.add_mat2(&rhs);
336 }
337}
338
339impl Sub<DMat2> for DMat2 {
340 type Output = Self;
341 #[inline]
342 fn sub(self, rhs: Self) -> Self::Output {
343 self.sub_mat2(&rhs)
344 }
345}
346
347impl SubAssign<DMat2> for DMat2 {
348 #[inline]
349 fn sub_assign(&mut self, rhs: Self) {
350 *self = self.sub_mat2(&rhs);
351 }
352}
353
354impl Neg for DMat2 {
355 type Output = Self;
356 #[inline]
357 fn neg(self) -> Self::Output {
358 Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
359 }
360}
361
362impl Mul<DMat2> for DMat2 {
363 type Output = Self;
364 #[inline]
365 fn mul(self, rhs: Self) -> Self::Output {
366 self.mul_mat2(&rhs)
367 }
368}
369
370impl MulAssign<DMat2> for DMat2 {
371 #[inline]
372 fn mul_assign(&mut self, rhs: Self) {
373 *self = self.mul_mat2(&rhs);
374 }
375}
376
377impl Mul<DVec2> for DMat2 {
378 type Output = DVec2;
379 #[inline]
380 fn mul(self, rhs: DVec2) -> Self::Output {
381 self.mul_vec2(rhs)
382 }
383}
384
385impl Mul<DMat2> for f64 {
386 type Output = DMat2;
387 #[inline]
388 fn mul(self, rhs: DMat2) -> Self::Output {
389 rhs.mul_scalar(self)
390 }
391}
392
393impl Mul<f64> for DMat2 {
394 type Output = Self;
395 #[inline]
396 fn mul(self, rhs: f64) -> Self::Output {
397 self.mul_scalar(rhs)
398 }
399}
400
401impl MulAssign<f64> for DMat2 {
402 #[inline]
403 fn mul_assign(&mut self, rhs: f64) {
404 *self = self.mul_scalar(rhs);
405 }
406}
407
408impl Div<DMat2> for f64 {
409 type Output = DMat2;
410 #[inline]
411 fn div(self, rhs: DMat2) -> Self::Output {
412 rhs.div_scalar(self)
413 }
414}
415
416impl Div<f64> for DMat2 {
417 type Output = Self;
418 #[inline]
419 fn div(self, rhs: f64) -> Self::Output {
420 self.div_scalar(rhs)
421 }
422}
423
424impl DivAssign<f64> for DMat2 {
425 #[inline]
426 fn div_assign(&mut self, rhs: f64) {
427 *self = self.div_scalar(rhs);
428 }
429}
430
431impl Sum<Self> for DMat2 {
432 fn sum<I>(iter: I) -> Self
433 where
434 I: Iterator<Item = Self>,
435 {
436 iter.fold(Self::ZERO, Self::add)
437 }
438}
439
440impl<'a> Sum<&'a Self> for DMat2 {
441 fn sum<I>(iter: I) -> Self
442 where
443 I: Iterator<Item = &'a Self>,
444 {
445 iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
446 }
447}
448
449impl Product for DMat2 {
450 fn product<I>(iter: I) -> Self
451 where
452 I: Iterator<Item = Self>,
453 {
454 iter.fold(Self::IDENTITY, Self::mul)
455 }
456}
457
458impl<'a> Product<&'a Self> for DMat2 {
459 fn product<I>(iter: I) -> Self
460 where
461 I: Iterator<Item = &'a Self>,
462 {
463 iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
464 }
465}
466
467impl PartialEq for DMat2 {
468 #[inline]
469 fn eq(&self, rhs: &Self) -> bool {
470 self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
471 }
472}
473
474#[cfg(not(target_arch = "spirv"))]
475impl AsRef<[f64; 4]> for DMat2 {
476 #[inline]
477 fn as_ref(&self) -> &[f64; 4] {
478 unsafe { &*(self as *const Self as *const [f64; 4]) }
479 }
480}
481
482#[cfg(not(target_arch = "spirv"))]
483impl AsMut<[f64; 4]> for DMat2 {
484 #[inline]
485 fn as_mut(&mut self) -> &mut [f64; 4] {
486 unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
487 }
488}
489
490#[cfg(not(target_arch = "spirv"))]
491impl fmt::Debug for DMat2 {
492 fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
493 fmt.debug_struct(stringify!(DMat2))
494 .field("x_axis", &self.x_axis)
495 .field("y_axis", &self.y_axis)
496 .finish()
497 }
498}
499
500#[cfg(not(target_arch = "spirv"))]
501impl fmt::Display for DMat2 {
502 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
503 if let Some(p) = f.precision() {
504 write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
505 } else {
506 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
507 }
508 }
509}