use super::*;
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(C)]
pub struct Mat2 {
pub(crate) x_axis: Vec2,
pub(crate) y_axis: Vec2,
}
unsafe impl Zeroable for Mat2 {}
unsafe impl Pod for Mat2 {}
impl core::fmt::Debug for Mat2 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("Mat2 { x_axis: (")?;
let [x, y]: [f32; 2] = cast(self.x_axis);
core::fmt::Debug::fmt(&x, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&y, f)?;
f.write_str("), y_axis: (")?;
let [x, y]: [f32; 2] = cast(self.y_axis);
core::fmt::Debug::fmt(&x, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&y, f)?;
f.write_str(") }")
}
}
impl core::fmt::Display for Mat2 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1]: [f32; 2] = cast(self.x_axis);
let [y0, y1]: [f32; 2] = cast(self.y_axis);
f.write_str("| ")?;
core::fmt::Display::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&y0, f)?;
f.write_str(" |\n| ")?;
core::fmt::Display::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&y1, f)?;
f.write_str(" |")
}
}
impl core::fmt::LowerExp for Mat2 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1]: [f32; 2] = cast(self.x_axis);
let [y0, y1]: [f32; 2] = cast(self.y_axis);
f.write_str("| ")?;
core::fmt::LowerExp::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&y0, f)?;
f.write_str(" |\n| ")?;
core::fmt::LowerExp::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&y1, f)?;
f.write_str(" |")
}
}
impl core::fmt::UpperExp for Mat2 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1]: [f32; 2] = cast(self.x_axis);
let [y0, y1]: [f32; 2] = cast(self.y_axis);
f.write_str("| ")?;
core::fmt::UpperExp::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&y0, f)?;
f.write_str(" |\n| ")?;
core::fmt::UpperExp::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&y1, f)?;
f.write_str(" |")
}
}
impl Index<usize> for Mat2 {
type Output = Vec2;
#[inline(always)]
fn index(&self, index: usize) -> &Vec2 {
match index {
0 => &self.x_axis,
1 => &self.y_axis,
otherwise => panic!("Mat2 index out of bounds: {}", otherwise),
}
}
}
impl IndexMut<usize> for Mat2 {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Vec2 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
otherwise => panic!("Mat2 index out of bounds: {}", otherwise),
}
}
}
impl AsRef<[Vec2; 2]> for Mat2 {
#[inline(always)]
fn as_ref(&self) -> &[Vec2; 2] {
cast_ref(self)
}
}
impl AsMut<[Vec2; 2]> for Mat2 {
#[inline(always)]
fn as_mut(&mut self) -> &mut [Vec2; 2] {
cast_mut(self)
}
}
impl From<[f32; 4]> for Mat2 {
#[inline]
fn from([xx, xy, yx, yy]: [f32; 4]) -> Self {
Self {
x_axis: Vec2::from([xx, xy]),
y_axis: Vec2::from([yx, yy]),
}
}
}
impl From<Mat2> for [f32; 4] {
#[inline]
fn from(Mat2 { x_axis, y_axis }: Mat2) -> Self {
let [e0, e1]: [f32; 2] = x_axis.into();
let [e2, e3]: [f32; 2] = y_axis.into();
[e0, e1, e2, e3]
}
}
#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix2<f32>> for Mat2 {
#[inline]
fn from(mint::ColumnMatrix2 { x, y }: mint::ColumnMatrix2<f32>) -> Self {
let x_axis: Vec2 = x.into();
let y_axis: Vec2 = y.into();
Self { x_axis, y_axis }
}
}
#[cfg(feature = "mint")]
impl From<Mat2> for mint::ColumnMatrix2<f32> {
#[inline]
fn from(Mat2 { x_axis, y_axis }: Mat2) -> Self {
let x: mint::Vector2<f32> = x_axis.into();
let y: mint::Vector2<f32> = y_axis.into();
Self { x, y }
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Mat2 {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
<[f32; 4]>::from(*self).serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Mat2 {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self::from(<[f32; 4]>::deserialize(deserializer)?))
}
}
impl Add for Mat2 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Self {
x_axis: self.x_axis + rhs.x_axis,
y_axis: self.y_axis + rhs.y_axis,
}
}
}
impl Add<f32> for Mat2 {
type Output = Self;
#[inline]
fn add(self, rhs: f32) -> Self {
self + Self::splat(rhs)
}
}
impl Add<Mat2> for f32 {
type Output = Mat2;
#[inline]
fn add(self, rhs: Mat2) -> Mat2 {
Mat2::splat(self) + rhs
}
}
impl AddAssign for Mat2 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl AddAssign<f32> for Mat2 {
#[inline]
fn add_assign(&mut self, rhs: f32) {
*self = *self + Self::splat(rhs)
}
}
impl Sub for Mat2 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self {
x_axis: self.x_axis - rhs.x_axis,
y_axis: self.y_axis - rhs.y_axis,
}
}
}
impl Sub<f32> for Mat2 {
type Output = Self;
#[inline]
fn sub(self, rhs: f32) -> Self {
self - Self::splat(rhs)
}
}
impl Sub<Mat2> for f32 {
type Output = Mat2;
#[inline]
fn sub(self, rhs: Mat2) -> Mat2 {
Mat2::splat(self) - rhs
}
}
impl SubAssign for Mat2 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs
}
}
impl SubAssign<f32> for Mat2 {
#[inline]
fn sub_assign(&mut self, rhs: f32) {
*self = *self - Self::splat(rhs)
}
}
impl Neg for Mat2 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self {
x_axis: -self.x_axis,
y_axis: -self.y_axis,
}
}
}
impl Mul<f32> for Mat2 {
type Output = Self;
#[inline]
fn mul(self, rhs: f32) -> Self {
Self {
x_axis: self.x_axis * rhs,
y_axis: self.y_axis * rhs,
}
}
}
impl Mul<Mat2> for f32 {
type Output = Mat2;
#[inline]
fn mul(self, rhs: Mat2) -> Mat2 {
rhs * self
}
}
impl MulAssign<f32> for Mat2 {
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl Mul<Vec2> for Mat2 {
type Output = Vec2;
#[inline]
fn mul(self, v2: Vec2) -> Vec2 {
Vec2::new(
Vec2::new(self.x_axis.x, self.y_axis.x).dot(v2),
Vec2::new(self.x_axis.y, self.y_axis.y).dot(v2),
)
}
}
impl Mul for Mat2 {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
Self {
x_axis: self * rhs.x_axis,
y_axis: self * rhs.y_axis,
}
}
}
impl MulAssign for Mat2 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
impl Mat2 {
#[inline(always)]
pub fn x_axis(self) -> Vec2 {
self.x_axis
}
#[inline(always)]
pub fn y_axis(self) -> Vec2 {
self.y_axis
}
#[inline(always)]
pub fn x_axis_mut(&mut self) -> &mut Vec2 {
&mut self.x_axis
}
#[inline(always)]
pub fn y_axis_mut(&mut self) -> &mut Vec2 {
&mut self.y_axis
}
}
impl Mat2 {
#[inline(always)]
pub fn new(x_axis: Vec2, y_axis: Vec2) -> Self {
Self { x_axis, y_axis }
}
#[inline]
pub fn splat(v: f32) -> Self {
Self {
x_axis: Vec2::splat(v),
y_axis: Vec2::splat(v),
}
}
#[inline]
pub fn diagonal(x: f32, y: f32) -> Self {
let x_axis = Vec2::new(x, 0.0);
let y_axis = Vec2::new(0.0, y);
Self { x_axis, y_axis }
}
#[inline]
pub fn to_mat3(self, z: f32) -> Mat3 {
let x_axis = self.x_axis.to_vec3(0.0);
let y_axis = self.y_axis.to_vec3(0.0);
let z_axis = Vec3::new(0.0, 0.0, z);
Mat3 {
x_axis,
y_axis,
z_axis,
}
}
#[inline]
pub fn determinant(self) -> f32 {
let a = self.x_axis.x();
let b = self.y_axis.x();
let c = self.x_axis.y();
let d = self.y_axis.y();
a * d - b * c
}
#[inline]
pub fn transpose(self) -> Self {
Mat2::new(
Vec2::new(self.x_axis.x(), self.y_axis.x()),
Vec2::new(self.x_axis.y(), self.y_axis.y()),
)
}
#[inline]
pub fn inverse(self) -> Option<Self> {
let det = self.determinant();
if det != 0.0 {
Some(
Mat2::new(
Vec2::new(self.y_axis.y, -self.x_axis.y),
Vec2::new(-self.y_axis.x, self.x_axis.x),
) * (1.0 / det),
)
} else {
None
}
}
}