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