use crate::{f64::math, swizzles::*, DMat3, DVec2, Mat2};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[inline(always)]
#[must_use]
pub const fn dmat2(x_axis: DVec2, y_axis: DVec2) -> DMat2 {
DMat2::from_cols(x_axis, y_axis)
}
#[derive(Clone, Copy)]
#[cfg_attr(feature = "cuda", repr(align(16)))]
#[repr(C)]
pub struct DMat2 {
pub x_axis: DVec2,
pub y_axis: DVec2,
}
impl DMat2 {
pub const ZERO: Self = Self::from_cols(DVec2::ZERO, DVec2::ZERO);
pub const IDENTITY: Self = Self::from_cols(DVec2::X, DVec2::Y);
pub const NAN: Self = Self::from_cols(DVec2::NAN, DVec2::NAN);
#[allow(clippy::too_many_arguments)]
#[inline(always)]
#[must_use]
const fn new(m00: f64, m01: f64, m10: f64, m11: f64) -> Self {
Self {
x_axis: DVec2::new(m00, m01),
y_axis: DVec2::new(m10, m11),
}
}
#[inline(always)]
#[must_use]
pub const fn from_cols(x_axis: DVec2, y_axis: DVec2) -> Self {
Self { x_axis, y_axis }
}
#[inline]
#[must_use]
pub const fn from_cols_array(m: &[f64; 4]) -> Self {
Self::new(m[0], m[1], m[2], m[3])
}
#[inline]
#[must_use]
pub const fn to_cols_array(&self) -> [f64; 4] {
[self.x_axis.x, self.x_axis.y, self.y_axis.x, self.y_axis.y]
}
#[inline]
#[must_use]
pub const fn from_cols_array_2d(m: &[[f64; 2]; 2]) -> Self {
Self::from_cols(DVec2::from_array(m[0]), DVec2::from_array(m[1]))
}
#[inline]
#[must_use]
pub const fn to_cols_array_2d(&self) -> [[f64; 2]; 2] {
[self.x_axis.to_array(), self.y_axis.to_array()]
}
#[doc(alias = "scale")]
#[inline]
#[must_use]
pub const fn from_diagonal(diagonal: DVec2) -> Self {
Self::new(diagonal.x, 0.0, 0.0, diagonal.y)
}
#[inline]
#[must_use]
pub fn from_scale_angle(scale: DVec2, angle: f64) -> Self {
let (sin, cos) = math::sin_cos(angle);
Self::new(cos * scale.x, sin * scale.x, -sin * scale.y, cos * scale.y)
}
#[inline]
#[must_use]
pub fn from_angle(angle: f64) -> Self {
let (sin, cos) = math::sin_cos(angle);
Self::new(cos, sin, -sin, cos)
}
#[inline]
#[must_use]
pub fn from_mat3(m: DMat3) -> Self {
Self::from_cols(m.x_axis.xy(), m.y_axis.xy())
}
#[inline]
#[must_use]
pub fn from_mat3_minor(m: DMat3, i: usize, j: usize) -> Self {
match (i, j) {
(0, 0) => Self::from_cols(m.y_axis.yz(), m.z_axis.yz()),
(0, 1) => Self::from_cols(m.y_axis.xz(), m.z_axis.xz()),
(0, 2) => Self::from_cols(m.y_axis.xy(), m.z_axis.xy()),
(1, 0) => Self::from_cols(m.x_axis.yz(), m.z_axis.yz()),
(1, 1) => Self::from_cols(m.x_axis.xz(), m.z_axis.xz()),
(1, 2) => Self::from_cols(m.x_axis.xy(), m.z_axis.xy()),
(2, 0) => Self::from_cols(m.x_axis.yz(), m.y_axis.yz()),
(2, 1) => Self::from_cols(m.x_axis.xz(), m.y_axis.xz()),
(2, 2) => Self::from_cols(m.x_axis.xy(), m.y_axis.xy()),
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub const fn from_cols_slice(slice: &[f64]) -> Self {
Self::new(slice[0], slice[1], slice[2], slice[3])
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f64]) {
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]
#[must_use]
pub fn col(&self, index: usize) -> DVec2 {
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 DVec2 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub fn row(&self, index: usize) -> DVec2 {
match index {
0 => DVec2::new(self.x_axis.x, self.y_axis.x),
1 => DVec2::new(self.x_axis.y, self.y_axis.y),
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub fn is_finite(&self) -> bool {
self.x_axis.is_finite() && self.y_axis.is_finite()
}
#[inline]
#[must_use]
pub fn is_nan(&self) -> bool {
self.x_axis.is_nan() || self.y_axis.is_nan()
}
#[inline]
#[must_use]
pub fn transpose(&self) -> Self {
Self {
x_axis: DVec2::new(self.x_axis.x, self.y_axis.x),
y_axis: DVec2::new(self.x_axis.y, self.y_axis.y),
}
}
#[inline]
#[must_use]
pub fn determinant(&self) -> f64 {
self.x_axis.x * self.y_axis.y - self.x_axis.y * self.y_axis.x
}
#[inline]
#[must_use]
pub fn inverse(&self) -> Self {
let inv_det = {
let det = self.determinant();
glam_assert!(det != 0.0);
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]
#[must_use]
pub fn mul_vec2(&self, rhs: DVec2) -> DVec2 {
#[allow(clippy::suspicious_operation_groupings)]
DVec2::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]
#[must_use]
pub fn mul_mat2(&self, rhs: &Self) -> Self {
Self::from_cols(self.mul(rhs.x_axis), self.mul(rhs.y_axis))
}
#[inline]
#[must_use]
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]
#[must_use]
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]
#[must_use]
pub fn mul_scalar(&self, rhs: f64) -> Self {
Self::from_cols(self.x_axis.mul(rhs), self.y_axis.mul(rhs))
}
#[inline]
#[must_use]
pub fn div_scalar(&self, rhs: f64) -> Self {
let rhs = DVec2::splat(rhs);
Self::from_cols(self.x_axis.div(rhs), self.y_axis.div(rhs))
}
#[inline]
#[must_use]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
&& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
}
#[inline]
#[must_use]
pub fn abs(&self) -> Self {
Self::from_cols(self.x_axis.abs(), self.y_axis.abs())
}
#[inline]
pub fn as_mat2(&self) -> Mat2 {
Mat2::from_cols(self.x_axis.as_vec2(), self.y_axis.as_vec2())
}
}
impl Default for DMat2 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl Add<DMat2> for DMat2 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
self.add_mat2(&rhs)
}
}
impl AddAssign<DMat2> for DMat2 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = self.add_mat2(&rhs);
}
}
impl Sub<DMat2> for DMat2 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
self.sub_mat2(&rhs)
}
}
impl SubAssign<DMat2> for DMat2 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = self.sub_mat2(&rhs);
}
}
impl Neg for DMat2 {
type Output = Self;
#[inline]
fn neg(self) -> Self::Output {
Self::from_cols(self.x_axis.neg(), self.y_axis.neg())
}
}
impl Mul<DMat2> for DMat2 {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
self.mul_mat2(&rhs)
}
}
impl MulAssign<DMat2> for DMat2 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = self.mul_mat2(&rhs);
}
}
impl Mul<DVec2> for DMat2 {
type Output = DVec2;
#[inline]
fn mul(self, rhs: DVec2) -> Self::Output {
self.mul_vec2(rhs)
}
}
impl Mul<DMat2> for f64 {
type Output = DMat2;
#[inline]
fn mul(self, rhs: DMat2) -> Self::Output {
rhs.mul_scalar(self)
}
}
impl Mul<f64> for DMat2 {
type Output = Self;
#[inline]
fn mul(self, rhs: f64) -> Self::Output {
self.mul_scalar(rhs)
}
}
impl MulAssign<f64> for DMat2 {
#[inline]
fn mul_assign(&mut self, rhs: f64) {
*self = self.mul_scalar(rhs);
}
}
impl Div<DMat2> for f64 {
type Output = DMat2;
#[inline]
fn div(self, rhs: DMat2) -> Self::Output {
rhs.div_scalar(self)
}
}
impl Div<f64> for DMat2 {
type Output = Self;
#[inline]
fn div(self, rhs: f64) -> Self::Output {
self.div_scalar(rhs)
}
}
impl DivAssign<f64> for DMat2 {
#[inline]
fn div_assign(&mut self, rhs: f64) {
*self = self.div_scalar(rhs);
}
}
impl Sum<Self> for DMat2 {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::ZERO, Self::add)
}
}
impl<'a> Sum<&'a Self> for DMat2 {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
}
}
impl Product for DMat2 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::IDENTITY, Self::mul)
}
}
impl<'a> Product<&'a Self> for DMat2 {
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 DMat2 {
#[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<[f64; 4]> for DMat2 {
#[inline]
fn as_ref(&self) -> &[f64; 4] {
unsafe { &*(self as *const Self as *const [f64; 4]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsMut<[f64; 4]> for DMat2 {
#[inline]
fn as_mut(&mut self) -> &mut [f64; 4] {
unsafe { &mut *(self as *mut Self as *mut [f64; 4]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for DMat2 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(stringify!(DMat2))
.field("x_axis", &self.x_axis)
.field("y_axis", &self.y_axis)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for DMat2 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(p) = f.precision() {
write!(f, "[{:.*}, {:.*}]", p, self.x_axis, p, self.y_axis)
} else {
write!(f, "[{}, {}]", self.x_axis, self.y_axis)
}
}
}