| 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, AddAssign, Deref, DerefMut, Mul, MulAssign, Sub, SubAssign}; |
| 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, $mat3: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 a 3x3 matrix, discarding the 2nd row and column. |
| 91 | #[inline(always)] |
| 92 | pub fn from_mat3(m: $mat3) -> Self { |
| 93 | Self::from_cols(m.x_axis.into(), m.y_axis.into()) |
| 94 | } |
| 95 | |
| 96 | /// Creates a 2x2 matrix from the first 4 values in `slice`. |
| 97 | /// |
| 98 | /// # Panics |
| 99 | /// |
| 100 | /// Panics if `slice` is less than 4 elements long. |
| 101 | #[inline(always)] |
| 102 | pub fn from_cols_slice(slice: &[$t]) -> Self { |
| 103 | Self(Matrix2x2::from_cols_slice(slice)) |
| 104 | } |
| 105 | |
| 106 | /// Writes the columns of `self` to the first 4 elements in `slice`. |
| 107 | /// |
| 108 | /// # Panics |
| 109 | /// |
| 110 | /// Panics if `slice` is less than 4 elements long. |
| 111 | #[inline(always)] |
| 112 | pub fn write_cols_to_slice(self, slice: &mut [$t]) { |
| 113 | Matrix2x2::write_cols_to_slice(&self.0, slice) |
| 114 | } |
| 115 | |
| 116 | /// Returns the matrix column for the given `index`. |
| 117 | /// |
| 118 | /// # Panics |
| 119 | /// |
| 120 | /// Panics if `index` is greater than 1. |
| 121 | #[inline] |
| 122 | pub fn col(&self, index: usize) -> $vec2 { |
| 123 | match index { |
| 124 | 0 => self.x_axis, |
| 125 | 1 => self.y_axis, |
| 126 | _ => panic!("index out of bounds"), |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | /// Returns a mutable reference to 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_mut(&mut self, index: usize) -> &mut $vec2 { |
| 137 | match index { |
| 138 | 0 => &mut self.x_axis, |
| 139 | 1 => &mut self.y_axis, |
| 140 | _ => panic!("index out of bounds"), |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /// Returns the matrix row for the given `index`. |
| 145 | /// |
| 146 | /// # Panics |
| 147 | /// |
| 148 | /// Panics if `index` is greater than 1. |
| 149 | #[inline] |
| 150 | pub fn row(&self, index: usize) -> $vec2 { |
| 151 | match index { |
| 152 | 0 => $vec2::new(self.x_axis.x, self.y_axis.x), |
| 153 | 1 => $vec2::new(self.x_axis.y, self.y_axis.y), |
| 154 | _ => panic!("index out of bounds"), |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | /// Returns `true` if, and only if, all elements are finite. |
| 159 | /// If any element is either `NaN`, positive or negative infinity, this will return `false`. |
| 160 | #[inline] |
| 161 | pub fn is_finite(&self) -> bool { |
| 162 | // TODO |
| 163 | self.x_axis.is_finite() && self.y_axis.is_finite() |
| 164 | } |
| 165 | |
| 166 | /// Returns `true` if any elements are `NaN`. |
| 167 | #[inline] |
| 168 | pub fn is_nan(&self) -> bool { |
| 169 | self.x_axis.is_nan() || self.y_axis.is_nan() |
| 170 | } |
| 171 | |
| 172 | /// Returns the transpose of `self`. |
| 173 | #[must_use] |
| 174 | #[inline(always)] |
| 175 | pub fn transpose(&self) -> Self { |
| 176 | Self(self.0.transpose()) |
| 177 | } |
| 178 | |
| 179 | /// Returns the determinant of `self`. |
| 180 | #[inline(always)] |
| 181 | pub fn determinant(&self) -> $t { |
| 182 | self.0.determinant() |
| 183 | } |
| 184 | |
| 185 | /// Returns the inverse of `self`. |
| 186 | /// |
| 187 | /// If the matrix is not invertible the returned matrix will be invalid. |
| 188 | /// |
| 189 | /// # Panics |
| 190 | /// |
| 191 | /// Will panic if the determinant of `self` is zero when `glam_assert` is enabled. |
| 192 | #[must_use] |
| 193 | #[inline(always)] |
| 194 | pub fn inverse(&self) -> Self { |
| 195 | Self(self.0.inverse()) |
| 196 | } |
| 197 | |
| 198 | /// Transforms a 2D vector. |
| 199 | #[inline(always)] |
| 200 | pub fn mul_vec2(&self, other: $vec2) -> $vec2 { |
| 201 | $vec2(self.0.mul_vector(other.0)) |
| 202 | } |
| 203 | |
| 204 | /// Multiplies two 2x2 matrices. |
| 205 | #[inline(always)] |
| 206 | pub fn mul_mat2(&self, other: &Self) -> Self { |
| 207 | Self(self.0.mul_matrix(&other.0)) |
| 208 | } |
| 209 | |
| 210 | /// Adds two 2x2 matrices. |
| 211 | #[inline(always)] |
| 212 | pub fn add_mat2(&self, other: &Self) -> Self { |
| 213 | Self(self.0.add_matrix(&other.0)) |
| 214 | } |
| 215 | |
| 216 | /// Subtracts two 2x2 matrices. |
| 217 | #[inline(always)] |
| 218 | pub fn sub_mat2(&self, other: &Self) -> Self { |
| 219 | Self(self.0.sub_matrix(&other.0)) |
| 220 | } |
| 221 | |
| 222 | /// Multiplies a 2x2 matrix by a scalar. |
| 223 | #[inline(always)] |
| 224 | pub fn mul_scalar(&self, other: $t) -> Self { |
| 225 | Self(self.0.mul_scalar(other)) |
| 226 | } |
| 227 | |
| 228 | /// Returns true if the absolute difference of all elements between `self` and `other` |
| 229 | /// is less than or equal to `max_abs_diff`. |
| 230 | /// |
| 231 | /// This can be used to compare if two matrices contain similar elements. It works best |
| 232 | /// when comparing with a known value. The `max_abs_diff` that should be used used |
| 233 | /// depends on the values being compared against. |
| 234 | /// |
| 235 | /// For more see |
| 236 | /// [comparing floating point numbers](https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). |
| 237 | #[inline(always)] |
| 238 | pub fn abs_diff_eq(&self, other: &Self, max_abs_diff: $t) -> bool { |
| 239 | self.0.abs_diff_eq(&other.0, max_abs_diff) |
| 240 | } |
| 241 | }; |
| 242 | } |
| 243 | |
| 244 | macro_rules! impl_mat2_traits { |
| 245 | ($t:ty, $new:ident, $mat2:ident, $vec2:ident) => { |
| 246 | /// Creates a 2x2 matrix from two column vectors. |
| 247 | #[inline(always)] |
| 248 | pub fn $new(x_axis: $vec2, y_axis: $vec2) -> $mat2 { |
| 249 | $mat2::from_cols(x_axis, y_axis) |
| 250 | } |
| 251 | |
| 252 | impl_matn_common_traits!($t, $mat2, $vec2); |
| 253 | |
| 254 | impl PartialEq for $mat2 { |
| 255 | #[inline] |
| 256 | fn eq(&self, other: &Self) -> bool { |
| 257 | self.x_axis.eq(&other.x_axis) && self.y_axis.eq(&other.y_axis) |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | #[cfg(not(target_arch = "spriv"))] |
| 262 | impl AsRef<[$t; 4]> for $mat2 { |
| 263 | #[inline(always)] |
| 264 | fn as_ref(&self) -> &[$t; 4] { |
| 265 | unsafe { &*(self as *const Self as *const [$t; 4]) } |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | #[cfg(not(target_arch = "spriv"))] |
| 270 | impl AsMut<[$t; 4]> for $mat2 { |
| 271 | #[inline(always)] |
| 272 | fn as_mut(&mut self) -> &mut [$t; 4] { |
| 273 | unsafe { &mut *(self as *mut Self as *mut [$t; 4]) } |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | impl Deref for $mat2 { |
| 278 | type Target = Columns2<$vec2>; |
| 279 | #[inline(always)] |
| 280 | fn deref(&self) -> &Self::Target { |
| 281 | unsafe { &*(self as *const Self as *const Self::Target) } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | impl DerefMut for $mat2 { |
| 286 | #[inline(always)] |
| 287 | fn deref_mut(&mut self) -> &mut Self::Target { |
| 288 | unsafe { &mut *(self as *mut Self as *mut Self::Target) } |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | #[cfg(not(target_arch = "spirv"))] |
| 293 | impl fmt::Debug for $mat2 { |
| 294 | fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { |
| 295 | fmt.debug_struct(stringify!($mat2)) |
| 296 | .field("x_axis", &self.x_axis) |
| 297 | .field("y_axis", &self.y_axis) |
| 298 | .finish() |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | #[cfg(not(target_arch = "spirv"))] |
| 303 | impl fmt::Display for $mat2 { |
| 304 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 305 | write!(f, "[{}, {}]", self.x_axis, self.y_axis) |
| 306 | } |
| 307 | } |
| 308 | }; |
| 309 | } |
| 310 | |
| 311 | #[cfg(all(target_feature = "sse2", not(feature = "scalar-math")))] |
| 312 | type InnerF32 = __m128; |
| 313 | |
| 314 | #[cfg(any(not(target_feature = "sse2"), feature = "scalar-math"))] |
| 315 | type InnerF32 = crate::core::storage::Columns2<XY<f32>>; |
| 316 | |
| 317 | /// A 2x2 column major matrix. |
| 318 | #[derive(Clone, Copy)] |
| 319 | #[cfg_attr( |
| 320 | not(any(feature = "scalar-math", target_arch = "spriv")), |
| 321 | repr(align(16)) |
| 322 | )] |
| 323 | #[cfg_attr(any(feature = "scalar-math", target_arch = "spriv"), repr(transparent))] |
| 324 | pub struct Mat2(pub(crate) InnerF32); |
| 325 | |
| 326 | impl Mat2 { |
| 327 | impl_mat2_methods!(f32, Vec2, Mat3, InnerF32); |
| 328 | |
| 329 | #[inline(always)] |
| 330 | pub fn as_f64(&self) -> DMat2 { |
| 331 | DMat2::from_cols(self.x_axis.as_f64(), self.y_axis.as_f64()) |
| 332 | } |
| 333 | } |
| 334 | impl_mat2_traits!(f32, mat2, Mat2, Vec2); |
| 335 | |
| 336 | type InnerF64 = crate::core::storage::Columns2<XY<f64>>; |
| 337 | |
| 338 | /// A 2x2 column major matrix. |
| 339 | #[derive(Clone, Copy)] |
| 340 | #[repr(transparent)] |
| 341 | pub struct DMat2(pub(crate) InnerF64); |
| 342 | |
| 343 | impl DMat2 { |
| 344 | impl_mat2_methods!(f64, DVec2, DMat3, InnerF64); |
| 345 | |
| 346 | #[inline(always)] |
| 347 | pub fn as_f32(&self) -> Mat2 { |
| 348 | Mat2::from_cols(self.x_axis.as_f32(), self.y_axis.as_f32()) |
| 349 | } |
| 350 | } |
| 351 | impl_mat2_traits!(f64, dmat2, DMat2, DVec2); |
| 352 | |
| 353 | #[cfg(any(feature = "scalar-math", target_arch = "spriv"))] |
| 354 | mod const_test_mat2 { |
| 355 | const_assert_eq!( |
| 356 | core::mem::align_of::<f32>(), |
| 357 | core::mem::align_of::<super::Mat2>() |
| 358 | ); |
| 359 | const_assert_eq!(16, core::mem::size_of::<super::Mat2>()); |
| 360 | } |
| 361 | |
| 362 | #[cfg(not(any(feature = "scalar-math", target_arch = "spriv")))] |
| 363 | mod const_test_mat2 { |
| 364 | const_assert_eq!(16, core::mem::align_of::<super::Mat2>()); |
| 365 | const_assert_eq!(16, core::mem::size_of::<super::Mat2>()); |
| 366 | } |
| 367 | |
| 368 | mod const_test_dmat2 { |
| 369 | const_assert_eq!( |
| 370 | core::mem::align_of::<f64>(), |
| 371 | core::mem::align_of::<super::DMat2>() |
| 372 | ); |
| 373 | const_assert_eq!(32, core::mem::size_of::<super::DMat2>()); |
| 374 | } |