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, 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]
290 #[must_use]
291 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]
297 pub fn as_mat2(&self) -> Mat2 {
298 Mat2::from_cols(self.x_axis.as_vec2(), self.y_axis.as_vec2())
299 }
300}
301
302impl Default for DMat2 {
303 #[inline]
304 fn default() -> Self {
305 Self::IDENTITY
306 }
307}
308
309impl Add<DMat2> for DMat2 {
310 type Output = Self;
311 #[inline]
312 fn add(self, rhs: Self) -> Self::Output {
313 self.add_mat2(&rhs)
314 }
315}
316
317impl AddAssign<DMat2> for DMat2 {
318 #[inline]
319 fn add_assign(&mut self, rhs: Self) {
320 *self = self.add_mat2(&rhs);
321 }
322}
323
324impl Sub<DMat2> for DMat2 {
325 type Output = Self;
326 #[inline]
327 fn sub(self, rhs: Self) -> Self::Output {
328 self.sub_mat2(&rhs)
329 }
330}
331
332impl SubAssign<DMat2> for DMat2 {
333 #[inline]
334 fn sub_assign(&mut self, rhs: Self) {
335 *self = self.sub_mat2(&rhs);
336 }
337}
338
339impl Neg for DMat2 {
340 type Output = Self;
341 #[inline]
342 fn neg(self) -> Self::Output {
343 Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
344 }
345}
346
347impl Mul<DMat2> for DMat2 {
348 type Output = Self;
349 #[inline]
350 fn mul(self, rhs: Self) -> Self::Output {
351 self.mul_mat2(&rhs)
352 }
353}
354
355impl MulAssign<DMat2> for DMat2 {
356 #[inline]
357 fn mul_assign(&mut self, rhs: Self) {
358 *self = self.mul_mat2(&rhs);
359 }
360}
361
362impl Mul<DVec2> for DMat2 {
363 type Output = DVec2;
364 #[inline]
365 fn mul(self, rhs: DVec2) -> Self::Output {
366 self.mul_vec2(rhs)
367 }
368}
369
370impl Mul<DMat2> for f64 {
371 type Output = DMat2;
372 #[inline]
373 fn mul(self, rhs: DMat2) -> Self::Output {
374 rhs.mul_scalar(self)
375 }
376}
377
378impl Mul<f64> for DMat2 {
379 type Output = Self;
380 #[inline]
381 fn mul(self, rhs: f64) -> Self::Output {
382 self.mul_scalar(rhs)
383 }
384}
385
386impl MulAssign<f64> for DMat2 {
387 #[inline]
388 fn mul_assign(&mut self, rhs: f64) {
389 *self = self.mul_scalar(rhs);
390 }
391}
392
393impl Sum<Self> for DMat2 {
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
402impl<'a> Sum<&'a Self> for DMat2 {
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
411impl Product for DMat2 {
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
420impl<'a> Product<&'a Self> for DMat2 {
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
429impl PartialEq for DMat2 {
430 #[inline]
431 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"))]
437impl AsRef<[f64; 4]> for DMat2 {
438 #[inline]
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"))]
445impl AsMut<[f64; 4]> for DMat2 {
446 #[inline]
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"))]
453impl fmt::Debug for DMat2 {
454 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"))]
463impl fmt::Display for DMat2 {
464 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
465 write!(f, "[{}, {}]", self.x_axis, self.y_axis)
466 }
467}