use crate::{
nums::{bool32x4, f32x4, num_traits::*},
Affine2, Mat2x4, Mat3x4, Point2x4, Vec2x4,
};
use auto_ops_det::{impl_op_ex, impl_op_ex_commutative};
use core::ops::{self, Deref, DerefMut};
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Affine2x4 {
pub matrix2: Mat2x4,
pub translation: Vec2x4,
}
impl Affine2x4 {
pub const ZERO: Self = Self {
matrix2: Mat2x4::ZERO,
translation: Vec2x4::ZERO,
};
pub const IDENTITY: Self = Self {
matrix2: Mat2x4::IDENTITY,
translation: Vec2x4::ZERO,
};
#[inline]
pub fn lane_select(cond: bool32x4, if_true: Self, if_false: Self) -> Self {
Self {
matrix2: Mat2x4::lane_select(cond, if_true.matrix2, if_false.matrix2),
translation: Vec2x4::lane_select(cond, if_true.translation, if_false.translation),
}
}
#[inline]
pub fn splat_soa(m: Affine2) -> Self {
Self {
matrix2: Mat2x4::splat_soa(m.matrix2),
translation: Vec2x4::splat_soa(m.translation),
}
}
#[inline]
pub fn extract_lane(&self, i: usize) -> Affine2 {
Affine2 {
matrix2: self.matrix2.extract_lane(i),
translation: self.translation.extract_lane(i),
}
}
#[inline]
pub fn replace_lane(&mut self, i: usize, m: Affine2) {
self.matrix2.replace_lane(i, m.matrix2);
self.translation.replace_lane(i, m.translation);
}
#[inline]
pub fn split_soa(self) -> [Affine2; 4] {
[
self.extract_lane(0),
self.extract_lane(1),
self.extract_lane(2),
self.extract_lane(3),
]
}
#[inline]
pub fn compose_soa(a: &[Affine2; 4]) -> Affine2x4 {
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
}
pub const NAN: Self = Self {
matrix2: Mat2x4::NAN,
translation: Vec2x4::NAN,
};
#[inline]
pub const fn from_cols(x_axis: Vec2x4, y_axis: Vec2x4, z_axis: Vec2x4) -> Self {
Self {
matrix2: Mat2x4::from_cols(x_axis, y_axis),
translation: z_axis,
}
}
#[inline]
pub fn from_cols_array(m: &[f32x4; 6]) -> Self {
Self {
matrix2: Mat2x4::from_cols_slice(&m[0..4]),
translation: Vec2x4::from_slice(&m[4..6]),
}
}
#[inline]
pub fn to_cols_array(&self) -> [f32x4; 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: &[[f32x4; 2]; 3]) -> Self {
Self {
matrix2: Mat2x4::from_cols(m[0].into(), m[1].into()),
translation: m[2].into(),
}
}
#[inline]
pub fn to_cols_array_2d(&self) -> [[f32x4; 2]; 3] {
[
self.matrix2.x_axis.into(),
self.matrix2.y_axis.into(),
self.translation.into(),
]
}
#[inline]
pub fn from_cols_slice(slice: &[f32x4]) -> Self {
Self {
matrix2: Mat2x4::from_cols_slice(&slice[0..4]),
translation: Vec2x4::from_slice(&slice[4..6]),
}
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f32x4]) {
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: Vec2x4) -> Self {
Self {
matrix2: Mat2x4::from_diagonal(scale),
translation: Vec2x4::ZERO,
}
}
#[inline]
pub fn from_angle(angle: f32x4) -> Self {
Self {
matrix2: Mat2x4::from_angle(angle),
translation: Vec2x4::ZERO,
}
}
#[inline]
pub fn from_translation(translation: Vec2x4) -> Self {
Self {
matrix2: Mat2x4::IDENTITY,
translation,
}
}
#[inline]
pub fn from_mat2(matrix2: Mat2x4) -> Self {
Self {
matrix2,
translation: Vec2x4::ZERO,
}
}
#[inline]
pub fn from_mat2_translation(matrix2: Mat2x4, translation: Vec2x4) -> Self {
Self {
matrix2,
translation,
}
}
#[inline]
pub fn from_scale_angle_translation(scale: Vec2x4, angle: f32x4, translation: Vec2x4) -> Self {
let rotation = Mat2x4::from_angle(angle);
Self {
matrix2: Mat2x4::from_cols(rotation.x_axis * scale.x, rotation.y_axis * scale.y),
translation,
}
}
#[inline]
pub fn from_angle_translation(angle: f32x4, translation: Vec2x4) -> Self {
Self {
matrix2: Mat2x4::from_angle(angle),
translation,
}
}
#[inline]
pub fn from_mat3(m: Mat3x4) -> Self {
use crate::swizzles::Vec3Swizzles;
Self {
matrix2: Mat2x4::from_cols(m.x_axis.xy(), m.y_axis.xy()),
translation: m.z_axis.xy(),
}
}
#[inline]
pub fn transform_point2(&self, rhs: Point2x4) -> Point2x4 {
Point2x4(self.matrix2 * rhs.0 + self.translation)
}
#[inline]
pub fn transform_vector2(&self, rhs: Vec2x4) -> Vec2x4 {
self.matrix2 * rhs
}
#[inline]
pub fn is_finite(&self) -> bool32x4 {
self.matrix2.is_finite() & self.translation.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool32x4 {
self.matrix2.is_nan() | self.translation.is_nan()
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32x4) -> bool32x4 {
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 Affine2x4 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl Deref for Affine2x4 {
type Target = crate::deref::Cols3<Vec2x4>;
#[inline]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}
impl DerefMut for Affine2x4 {
#[inline]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}
impl PartialEq for Affine2x4 {
#[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 Affine2x4 {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(Affine2x4))
.field("matrix2", &self.matrix2)
.field("translation", &self.translation)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for Affine2x4 {
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 Affine2x4 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl_op_ex!(*|a: &Affine2x4, b: &Affine2x4| -> Affine2x4 {
Affine2x4 {
matrix2: a.matrix2 * b.matrix2,
translation: a.matrix2 * b.translation + a.translation,
}
});
impl_op_ex_commutative!(*|a: &Affine2x4, b: f32x4| -> Affine2x4 {
Affine2x4 {
matrix2: a.matrix2 * b,
translation: a.translation * b,
}
});
impl_op_ex!(+ |a: &Affine2x4, b: &Affine2x4| -> Affine2x4 {
Affine2x4 {
matrix2: a.matrix2 + b.matrix2,
translation: a.translation + b.translation,
}
});
impl_op_ex!(-|a: &Affine2x4, b: &Affine2x4| -> Affine2x4 {
Affine2x4 {
matrix2: a.matrix2 - b.matrix2,
translation: a.translation - b.translation,
}
});
impl From<Affine2x4> for Mat3x4 {
#[inline]
fn from(m: Affine2x4) -> Mat3x4 {
Self::from_cols(
m.matrix2.x_axis.extend(f32x4::ZERO),
m.matrix2.y_axis.extend(f32x4::ZERO),
m.translation.extend(f32x4::ONE),
)
}
}
impl From<&Affine2x4> for Mat3x4 {
#[inline]
fn from(m: &Affine2x4) -> Mat3x4 {
Self::from_cols(
m.matrix2.x_axis.extend(f32x4::ZERO),
m.matrix2.y_axis.extend(f32x4::ZERO),
m.translation.extend(f32x4::ONE),
)
}
}
impl_op_ex!(*|a: &Affine2x4, b: &Mat3x4| -> Mat3x4 { Mat3x4::from(a) * b });
impl_op_ex!(*|a: &Mat3x4, b: &Affine2x4| -> Mat3x4 { a * Mat3x4::from(b) });
impl_op_ex!(*|a: &Affine2x4, b: &Vec2x4| -> Vec2x4 { a.matrix2 * b });
impl_op_ex!(*|a: &Affine2x4, b: &Point2x4| -> Point2x4 { a.transform_point2(*b) });