use crate::{DMat2, DMat3, DVec2};
use core::ops::{Deref, DerefMut, Mul};
#[derive(Copy, Clone)]
#[repr(C)]
pub struct DAffine2 {
pub matrix2: DMat2,
pub translation: DVec2,
}
impl DAffine2 {
pub const ZERO: Self = Self {
matrix2: DMat2::ZERO,
translation: DVec2::ZERO,
};
pub const IDENTITY: Self = Self {
matrix2: DMat2::IDENTITY,
translation: DVec2::ZERO,
};
pub const NAN: Self = Self {
matrix2: DMat2::NAN,
translation: DVec2::NAN,
};
#[inline(always)]
pub const fn from_cols(x_axis: DVec2, y_axis: DVec2, z_axis: DVec2) -> Self {
Self {
matrix2: DMat2::from_cols(x_axis, y_axis),
translation: z_axis,
}
}
#[inline]
pub fn from_cols_array(m: &[f64; 6]) -> Self {
Self {
matrix2: DMat2::from_cols_slice(&m[0..4]),
translation: DVec2::from_slice(&m[4..6]),
}
}
#[inline]
pub fn to_cols_array(&self) -> [f64; 6] {
let x = &self.matrix2.x_axis;
let y = &self.matrix2.y_axis;
let z = &self.translation;
[x.x, x.y, y.x, y.y, z.x, z.y]
}
#[inline]
pub fn from_cols_array_2d(m: &[[f64; 2]; 3]) -> Self {
Self {
matrix2: DMat2::from_cols(m[0].into(), m[1].into()),
translation: m[2].into(),
}
}
#[inline]
pub fn to_cols_array_2d(&self) -> [[f64; 2]; 3] {
[
self.matrix2.x_axis.into(),
self.matrix2.y_axis.into(),
self.translation.into(),
]
}
#[inline]
pub fn from_cols_slice(slice: &[f64]) -> Self {
Self {
matrix2: DMat2::from_cols_slice(&slice[0..4]),
translation: DVec2::from_slice(&slice[4..6]),
}
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f64]) {
self.matrix2.write_cols_to_slice(&mut slice[0..4]);
self.translation.write_to_slice(&mut slice[4..6]);
}
#[inline]
pub fn from_scale(scale: DVec2) -> Self {
Self {
matrix2: DMat2::from_diagonal(scale),
translation: DVec2::ZERO,
}
}
#[inline]
pub fn from_angle(angle: f64) -> Self {
Self {
matrix2: DMat2::from_angle(angle),
translation: DVec2::ZERO,
}
}
#[inline]
pub fn from_translation(translation: DVec2) -> Self {
Self {
matrix2: DMat2::IDENTITY,
translation,
}
}
#[inline]
pub fn from_mat2(matrix2: DMat2) -> Self {
Self {
matrix2,
translation: DVec2::ZERO,
}
}
#[inline]
pub fn from_mat2_translation(matrix2: DMat2, translation: DVec2) -> Self {
Self {
matrix2,
translation,
}
}
#[inline]
pub fn from_scale_angle_translation(scale: DVec2, angle: f64, translation: DVec2) -> Self {
let rotation = DMat2::from_angle(angle);
Self {
matrix2: DMat2::from_cols(rotation.x_axis * scale.x, rotation.y_axis * scale.y),
translation,
}
}
#[inline]
pub fn from_angle_translation(angle: f64, translation: DVec2) -> Self {
Self {
matrix2: DMat2::from_angle(angle),
translation,
}
}
#[inline]
pub fn from_mat3(m: DMat3) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: DMat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}
#[inline]
pub fn transform_point2(&self, rhs: DVec2) -> DVec2 {
self.matrix2 * rhs + self.translation
}
#[inline]
pub fn transform_vector2(&self, rhs: DVec2) -> DVec2 {
self.matrix2 * rhs
}
#[inline]
pub fn is_finite(&self) -> bool {
self.matrix2.is_finite() && self.translation.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool {
self.matrix2.is_nan() || self.translation.is_nan()
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> bool {
self.matrix2.abs_diff_eq(rhs.matrix2, max_abs_diff)
&& self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
}
#[must_use]
#[inline]
pub fn inverse(&self) -> Self {
let matrix2 = self.matrix2.inverse();
let translation = -(matrix2 * self.translation);
Self {
matrix2,
translation,
}
}
}
impl Default for DAffine2 {
#[inline(always)]
fn default() -> Self {
Self::IDENTITY
}
}
impl Deref for DAffine2 {
type Target = crate::deref::Cols3<DVec2>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}
impl DerefMut for DAffine2 {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}
impl PartialEq for DAffine2 {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.matrix2.eq(&rhs.matrix2) && self.translation.eq(&rhs.translation)
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Debug for DAffine2 {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(DAffine2))
.field("matrix2", &self.matrix2)
.field("translation", &self.translation)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for DAffine2 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[{}, {}, {}]",
self.matrix2.x_axis, self.matrix2.y_axis, self.translation
)
}
}
impl<'a> core::iter::Product<&'a Self> for DAffine2 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl Mul for DAffine2 {
type Output = DAffine2;
#[inline]
fn mul(self, rhs: DAffine2) -> Self::Output {
Self {
matrix2: self.matrix2 * rhs.matrix2,
translation: self.matrix2 * rhs.translation + self.translation,
}
}
}
impl From<DAffine2> for DMat3 {
#[inline]
fn from(m: DAffine2) -> DMat3 {
Self::from_cols(
m.matrix2.x_axis.extend(0.0),
m.matrix2.y_axis.extend(0.0),
m.translation.extend(1.0),
)
}
}
impl Mul<DMat3> for DAffine2 {
type Output = DMat3;
#[inline]
fn mul(self, rhs: DMat3) -> Self::Output {
DMat3::from(self) * rhs
}
}
impl Mul<DAffine2> for DMat3 {
type Output = DMat3;
#[inline]
fn mul(self, rhs: DAffine2) -> Self::Output {
self * DMat3::from(rhs)
}
}