Debian Codegraph @

debian/missing-sources/glam-0.24.2/src/f64/dmat2.rs

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