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