use crate::{
nums::{bool32x4, f32x4, num_traits::*},
swizzles::*,
Mat2, Mat3x4, UnitVec2x4, Vec2x4,
};
use auto_ops_det::{impl_op_ex, impl_op_ex_commutative};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{self, Add, Mul, Neg, Sub};
#[inline]
pub const fn mat2x4(x_axis: Vec2x4, y_axis: Vec2x4) -> Mat2x4 {
Mat2x4::from_cols(x_axis, y_axis)
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Mat2x4 {
pub x_axis: Vec2x4,
pub y_axis: Vec2x4,
}
impl Mat2x4 {
pub const ZERO: Self = Self::from_cols(Vec2x4::ZERO, Vec2x4::ZERO);
pub const IDENTITY: Self = Self::from_cols(Vec2x4::X, Vec2x4::Y);
pub const NAN: Self = Self::from_cols(Vec2x4::NAN, Vec2x4::NAN);
#[allow(clippy::too_many_arguments)]
#[inline]
const fn new(m00: f32x4, m01: f32x4, m10: f32x4, m11: f32x4) -> Self {
Self {
x_axis: Vec2x4::new(m00, m01),
y_axis: Vec2x4::new(m10, m11),
}
}
#[inline]
pub fn lane_select(cond: bool32x4, if_true: Self, if_false: Self) -> Self {
Self {
x_axis: Vec2x4::lane_select(cond, if_true.x_axis, if_false.x_axis),
y_axis: Vec2x4::lane_select(cond, if_true.y_axis, if_false.y_axis),
}
}
#[inline]
pub fn splat_soa(m: Mat2) -> Self {
Self {
x_axis: Vec2x4::splat_soa(m.x_axis),
y_axis: Vec2x4::splat_soa(m.y_axis),
}
}
#[inline]
pub fn extract_lane(&self, i: usize) -> Mat2 {
Mat2::from_cols(self.x_axis.extract_lane(i), self.y_axis.extract_lane(i))
}
#[inline]
pub fn replace_lane(&mut self, i: usize, m: Mat2) {
self.x_axis.replace_lane(i, m.x_axis);
self.y_axis.replace_lane(i, m.y_axis);
}
#[inline]
pub fn split_soa(self) -> [Mat2; 4] {
[
self.extract_lane(0),
self.extract_lane(1),
self.extract_lane(2),
self.extract_lane(3),
]
}
#[inline]
pub fn compose_soa(a: &[Mat2; 4]) -> Mat2x4 {
let mut v = Self::default();
v.replace_lane(0, a[0]);
v.replace_lane(1, a[1]);
v.replace_lane(2, a[2]);
v.replace_lane(3, a[3]);
v
}
#[inline]
pub const fn from_cols(x_axis: Vec2x4, y_axis: Vec2x4) -> Self {
Self { x_axis, y_axis }
}
#[inline]
pub const fn from_cols_array(m: &[f32x4; 4]) -> Self {
Self::new(m[0], m[1], m[2], m[3])
}
#[inline]
pub fn to_cols_array(&self) -> [f32x4; 4] {
[self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
}
#[inline]
pub const fn from_cols_array_2d(m: &[[f32x4; 2]; 2]) -> Self {
Self::from_cols(Vec2x4::from_array(m[0]), Vec2x4::from_array(m[1]))
}
#[inline]
pub fn to_cols_array_2d(&self) -> [[f32x4; 2]; 2] {
[self.x_axis.to_array(), self.y_axis.to_array()]
}
#[doc(alias = "scale")]
#[inline]
pub fn from_diagonal(diagonal: Vec2x4) -> Self {
Self::new(diagonal.x, f32x4::ZERO, f32x4::ZERO, diagonal.y)
}
#[inline]
pub fn from_scale_angle(scale: Vec2x4, angle: f32x4) -> Self {
let (sin, cos) = angle.sin_cosf();
Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
}
#[inline]
pub fn from_angle(angle: f32x4) -> Self {
let (sin, cos) = angle.sin_cosf();
Self::new(cos, sin, -sin, cos)
}
#[inline]
pub fn from_mat3(m: Mat3x4) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}
#[inline]
pub const fn from_cols_slice(slice: &[f32x4]) -> Self {
Self::new(slice[0], slice[1], slice[2], slice[3])
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f32x4]) {
slice[0] = self.x_axis.x;
slice[1] = self.x_axis.y;
slice[2] = self.y_axis.x;
slice[3] = self.y_axis.y;
}
#[inline]
pub fn col(&self, index: usize) -> Vec2x4 {
match index {
0 => self.x_axis,
1 => self.y_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn col_mut(&mut self, index: usize) -> &mut Vec2x4 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn row(&self, index: usize) -> Vec2x4 {
match index {
0 => Vec2x4::new(self.x_axis.x, self.y_axis.x),
1 => Vec2x4::new(self.x_axis.y, self.y_axis.y),
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn is_finite(&self) -> bool32x4 {
self.x_axis.is_finite() & self.y_axis.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool32x4 {
self.x_axis.is_nan() | self.y_axis.is_nan()
}
#[must_use]
#[inline]
pub fn transpose(&self) -> Self {
Self {
x_axis: Vec2x4::new(self.x_axis.x, self.y_axis.x),
y_axis: Vec2x4::new(self.x_axis.y, self.y_axis.y),
}
}
#[inline]
pub fn determinant(&self) -> f32x4 {
self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
}
#[must_use]
#[inline]
pub fn inverse(&self) -> Self {
let inv_det = {
let det = self.determinant();
glam_assert!(det.ne(f32x4::ZERO));
det.recip()
};
Self::new(
self.y_axis.y * inv_det,
self.x_axis.y * -inv_det,
self.y_axis.x * -inv_det,
self.x_axis.x * inv_det,
)
}
#[inline]
pub fn mul_vec2(&self, rhs: Vec2x4) -> Vec2x4 {
#[allow(clippy::suspicious_operation_groupings)]
Vec2x4::new(
(self.x_axis.x * rhs.x) + (self.y_axis.x * rhs.y),
(self.x_axis.y * rhs.x) + (self.y_axis.y * rhs.y),
)
}
#[inline]
pub fn mul_mat2(&self, rhs: &Self) -> Self {
Self::from_cols(self.mul(rhs.x_axis), self.mul(rhs.y_axis))
}
#[inline]
pub fn add_mat2(&self, rhs: &Self) -> Self {
Self::from_cols(self.x_axis.add(rhs.x_axis), self.y_axis.add(rhs.y_axis))
}
#[inline]
pub fn sub_mat2(&self, rhs: &Self) -> Self {
Self::from_cols(self.x_axis.sub(rhs.x_axis), self.y_axis.sub(rhs.y_axis))
}
#[inline]
pub fn mul_scalar(&self, rhs: f32x4) -> Self {
Self::from_cols(self.x_axis.mul(rhs), self.y_axis.mul(rhs))
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32x4) -> bool32x4 {
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
}
}
impl Default for Mat2x4 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl_op_ex!(+ |a: &Mat2x4, b:&Mat2x4| -> Mat2x4 { a.add_mat2(b) });
impl_op_ex!(+= |a: &mut Mat2x4, b:&Mat2x4| { *a = a.add_mat2(b); });
impl_op_ex!(-|a: &Mat2x4, b: &Mat2x4| -> Mat2x4 { a.sub_mat2(b) });
impl_op_ex!(-= |a: &mut Mat2x4, b:&Mat2x4| { *a = a.sub_mat2(b); });
impl Neg for Mat2x4 {
type Output = Self;
#[inline]
fn neg(self) -> Self::Output {
Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
}
}
impl_op_ex!(*|a: &Mat2x4, b: &Mat2x4| -> Mat2x4 { a.mul_mat2(b) });
impl_op_ex!(*= |a: &mut Mat2x4, b:&Mat2x4| { *a = a.mul_mat2(b); });
impl_op_ex!(*|a: &Mat2x4, b: &Vec2x4| -> Vec2x4 { a.mul_vec2(*b) });
impl_op_ex!(*|a: &Mat2x4, b: &UnitVec2x4| -> Vec2x4 { a * b.as_vec2x4() });
impl_op_ex_commutative!(*|a: &Mat2x4, b: f32x4| -> Mat2x4 { a.mul_scalar(b) });
impl_op_ex!(*= |a: &mut Mat2x4, b:f32x4| { *a = a.mul_scalar(b); });
impl<'a> Sum<&'a Self> for Mat2x4 {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
}
}
impl<'a> Product<&'a Self> for Mat2x4 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
}
}
impl PartialEq for Mat2x4 {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis)
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsRef<[f32x4; 4]> for Mat2x4 {
#[inline]
fn as_ref(&self) -> &[f32x4; 4] {
unsafe { &*(self as *const Self as *const [f32x4; 4]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsMut<[f32x4; 4]> for Mat2x4 {
#[inline]
fn as_mut(&mut self) -> &mut [f32x4; 4] {
unsafe { &mut *(self as *mut Self as *mut [f32x4; 4]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Mat2x4 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(stringify!(Mat2x4))
.field("x_axis", &self.x_axis)
.field("y_axis", &self.y_axis)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat2x4 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
}
}