Debian Codegraph @

debian/missing-sources/glam-0.16.0/src/mat2.rs

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