use crate::{Mat2, Mat3, Mat3A, Vec2, Vec3A};
use core::ops::{Deref, DerefMut, Mul, MulAssign};
#[derive(Copy, Clone)]
#[cfg_attr(
all(
feature = "bytemuck",
not(any(feature = "scalar-math", target_arch = "spirv"))
),
derive(bytemuck::AnyBitPattern)
)]
#[cfg_attr(
all(
feature = "bytemuck",
feature = "scalar-math",
not(target_arch = "spirv")
),
derive(bytemuck::Pod, bytemuck::Zeroable)
)]
#[repr(C)]
pub struct Affine2 {
pub matrix2: Mat2,
pub translation: Vec2,
}
impl Affine2 {
pub const ZERO: Self = Self {
matrix2: Mat2::ZERO,
translation: Vec2::ZERO,
};
pub const IDENTITY: Self = Self {
matrix2: Mat2::IDENTITY,
translation: Vec2::ZERO,
};
pub const NAN: Self = Self {
matrix2: Mat2::NAN,
translation: Vec2::NAN,
};
#[inline(always)]
#[must_use]
pub const fn from_cols(x_axis: Vec2, y_axis: Vec2, z_axis: Vec2) -> Self {
Self {
matrix2: Mat2::from_cols(x_axis, y_axis),
translation: z_axis,
}
}
#[inline]
#[must_use]
pub fn from_cols_array(m: &[f32; 6]) -> Self {
Self {
matrix2: Mat2::from_cols_array(&[m[0], m[1], m[2], m[3]]),
translation: Vec2::from_array([m[4], m[5]]),
}
}
#[inline]
#[must_use]
pub fn to_cols_array(&self) -> [f32; 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]
#[must_use]
pub fn from_cols_array_2d(m: &[[f32; 2]; 3]) -> Self {
Self {
matrix2: Mat2::from_cols(m[0].into(), m[1].into()),
translation: m[2].into(),
}
}
#[inline]
#[must_use]
pub fn to_cols_array_2d(&self) -> [[f32; 2]; 3] {
[
self.matrix2.x_axis.into(),
self.matrix2.y_axis.into(),
self.translation.into(),
]
}
#[inline]
#[must_use]
pub fn from_cols_slice(slice: &[f32]) -> Self {
Self {
matrix2: Mat2::from_cols_slice(&slice[0..4]),
translation: Vec2::from_slice(&slice[4..6]),
}
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f32]) {
self.matrix2.write_cols_to_slice(&mut slice[0..4]);
self.translation.write_to_slice(&mut slice[4..6]);
}
#[inline]
#[must_use]
pub fn from_scale(scale: Vec2) -> Self {
Self {
matrix2: Mat2::from_diagonal(scale),
translation: Vec2::ZERO,
}
}
#[inline]
#[must_use]
pub fn from_angle(angle: f32) -> Self {
Self {
matrix2: Mat2::from_angle(angle),
translation: Vec2::ZERO,
}
}
#[inline]
#[must_use]
pub fn from_translation(translation: Vec2) -> Self {
Self {
matrix2: Mat2::IDENTITY,
translation,
}
}
#[inline]
#[must_use]
pub fn from_mat2(matrix2: Mat2) -> Self {
Self {
matrix2,
translation: Vec2::ZERO,
}
}
#[inline]
#[must_use]
pub fn from_mat2_translation(matrix2: Mat2, translation: Vec2) -> Self {
Self {
matrix2,
translation,
}
}
#[inline]
#[must_use]
pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
let rotation = Mat2::from_angle(angle);
Self {
matrix2: Mat2::from_cols(rotation.x_axis * scale.x, rotation.y_axis * scale.y),
translation,
}
}
#[inline]
#[must_use]
pub fn from_angle_translation(angle: f32, translation: Vec2) -> Self {
Self {
matrix2: Mat2::from_angle(angle),
translation,
}
}
#[inline]
#[must_use]
pub fn from_mat3(m: Mat3) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}
#[inline]
#[must_use]
pub fn from_mat3a(m: Mat3A) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: Mat2::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}
#[inline]
#[must_use]
pub fn to_scale_angle_translation(self) -> (Vec2, f32, Vec2) {
use crate::f32::math;
let det = self.matrix2.determinant();
glam_assert!(det != 0.0);
let scale = Vec2::new(
self.matrix2.x_axis.length() * math::signum(det),
self.matrix2.y_axis.length(),
);
glam_assert!(scale.cmpne(Vec2::ZERO).all());
let angle = math::atan2(-self.matrix2.y_axis.x, self.matrix2.y_axis.y);
(scale, angle, self.translation)
}
#[inline]
#[must_use]
pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
self.matrix2 * rhs + self.translation
}
#[inline]
pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
self.matrix2 * rhs
}
#[inline]
#[must_use]
pub fn is_finite(&self) -> bool {
self.matrix2.is_finite() && self.translation.is_finite()
}
#[inline]
#[must_use]
pub fn is_nan(&self) -> bool {
self.matrix2.is_nan() || self.translation.is_nan()
}
#[inline]
#[must_use]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.matrix2.abs_diff_eq(rhs.matrix2, max_abs_diff)
&& self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
}
#[inline]
#[must_use]
pub fn inverse(&self) -> Self {
let matrix2 = self.matrix2.inverse();
let translation = -(matrix2 * self.translation);
Self {
matrix2,
translation,
}
}
#[inline]
#[must_use]
pub fn as_daffine2(&self) -> crate::DAffine2 {
crate::DAffine2::from_mat2_translation(self.matrix2.as_dmat2(), self.translation.as_dvec2())
}
}
impl Default for Affine2 {
#[inline(always)]
fn default() -> Self {
Self::IDENTITY
}
}
impl Deref for Affine2 {
type Target = crate::deref::Cols3<Vec2>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}
impl DerefMut for Affine2 {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}
impl PartialEq for Affine2 {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.matrix2.eq(&rhs.matrix2) && self.translation.eq(&rhs.translation)
}
}
impl core::fmt::Debug for Affine2 {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(Affine2))
.field("matrix2", &self.matrix2)
.field("translation", &self.translation)
.finish()
}
}
impl core::fmt::Display for Affine2 {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.matrix2.x_axis, p, self.matrix2.y_axis, p, self.translation
)
} else {
write!(
f,
"[{}, {}, {}]",
self.matrix2.x_axis, self.matrix2.y_axis, self.translation
)
}
}
}
impl<'a> core::iter::Product<&'a Self> for Affine2 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl Mul for Affine2 {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
Self {
matrix2: self.matrix2 * rhs.matrix2,
translation: self.matrix2 * rhs.translation + self.translation,
}
}
}
impl Mul<&Self> for Affine2 {
type Output = Self;
#[inline]
fn mul(self, rhs: &Self) -> Self {
self.mul(*rhs)
}
}
impl Mul<&Affine2> for &Affine2 {
type Output = Affine2;
#[inline]
fn mul(self, rhs: &Affine2) -> Affine2 {
(*self).mul(*rhs)
}
}
impl Mul<Affine2> for &Affine2 {
type Output = Affine2;
#[inline]
fn mul(self, rhs: Affine2) -> Affine2 {
(*self).mul(rhs)
}
}
impl MulAssign for Affine2 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = self.mul(rhs);
}
}
impl MulAssign<&Self> for Affine2 {
#[inline]
fn mul_assign(&mut self, rhs: &Self) {
self.mul_assign(*rhs);
}
}
impl From<Affine2> for Mat3 {
#[inline]
fn from(m: Affine2) -> Self {
Self::from_cols(
m.matrix2.x_axis.extend(0.0),
m.matrix2.y_axis.extend(0.0),
m.translation.extend(1.0),
)
}
}
impl Mul<Mat3> for Affine2 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: Mat3) -> Self::Output {
Mat3::from(self) * rhs
}
}
impl Mul<&Mat3> for Affine2 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: &Mat3) -> Mat3 {
self.mul(*rhs)
}
}
impl Mul<&Mat3> for &Affine2 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: &Mat3) -> Mat3 {
(*self).mul(*rhs)
}
}
impl Mul<Mat3> for &Affine2 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: Mat3) -> Mat3 {
(*self).mul(rhs)
}
}
impl Mul<Affine2> for Mat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: Affine2) -> Self {
self * Self::from(rhs)
}
}
impl Mul<&Affine2> for Mat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: &Affine2) -> Self {
self.mul(*rhs)
}
}
impl Mul<&Affine2> for &Mat3 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: &Affine2) -> Mat3 {
(*self).mul(*rhs)
}
}
impl Mul<Affine2> for &Mat3 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: Affine2) -> Mat3 {
(*self).mul(rhs)
}
}
impl MulAssign<Affine2> for Mat3 {
#[inline]
fn mul_assign(&mut self, rhs: Affine2) {
*self = self.mul(rhs);
}
}
impl MulAssign<&Affine2> for Mat3 {
#[inline]
fn mul_assign(&mut self, rhs: &Affine2) {
self.mul_assign(*rhs);
}
}
impl Mul<Mat3A> for Affine2 {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: Mat3A) -> Self::Output {
Mat3A::from(self) * rhs
}
}
impl Mul<&Mat3A> for Affine2 {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: &Mat3A) -> Mat3A {
self.mul(*rhs)
}
}
impl Mul<&Mat3A> for &Affine2 {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: &Mat3A) -> Mat3A {
(*self).mul(*rhs)
}
}
impl Mul<Mat3A> for &Affine2 {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: Mat3A) -> Mat3A {
(*self).mul(rhs)
}
}
impl Mul<Affine2> for Mat3A {
type Output = Self;
#[inline]
fn mul(self, rhs: Affine2) -> Self {
self * Self::from(rhs)
}
}
impl Mul<&Affine2> for Mat3A {
type Output = Self;
#[inline]
fn mul(self, rhs: &Affine2) -> Self {
self.mul(*rhs)
}
}
impl Mul<&Affine2> for &Mat3A {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: &Affine2) -> Mat3A {
(*self).mul(*rhs)
}
}
impl Mul<Affine2> for &Mat3A {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: Affine2) -> Mat3A {
(*self).mul(rhs)
}
}
impl MulAssign<Affine2> for Mat3A {
#[inline]
fn mul_assign(&mut self, rhs: Affine2) {
*self = self.mul(rhs);
}
}
impl MulAssign<&Affine2> for Mat3A {
#[inline]
fn mul_assign(&mut self, rhs: &Affine2) {
self.mul_assign(*rhs);
}
}
impl From<Affine2> for Mat3A {
#[inline]
fn from(m: Affine2) -> Self {
Self::from_cols(
Vec3A::from((m.matrix2.x_axis, 0.0)),
Vec3A::from((m.matrix2.y_axis, 0.0)),
Vec3A::from((m.translation, 1.0)),
)
}
}