Debian Codegraph @

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