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