use crate::{Mat3, Mat3A, Mat4, Quat, Vec3, Vec3A};
use core::ops::{Deref, DerefMut, Mul};
#[derive(Copy, Clone)]
#[repr(C)]
pub struct Affine3A {
pub matrix3: Mat3A,
pub translation: Vec3A,
}
impl Affine3A {
pub const ZERO: Self = Self {
matrix3: Mat3A::ZERO,
translation: Vec3A::ZERO,
};
pub const IDENTITY: Self = Self {
matrix3: Mat3A::IDENTITY,
translation: Vec3A::ZERO,
};
pub const NAN: Self = Self {
matrix3: Mat3A::NAN,
translation: Vec3A::NAN,
};
#[inline(always)]
pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A, w_axis: Vec3A) -> Self {
Self {
matrix3: Mat3A::from_cols(x_axis, y_axis, z_axis),
translation: w_axis,
}
}
#[inline]
pub fn from_cols_array(m: &[f32; 12]) -> Self {
Self {
matrix3: Mat3A::from_cols_slice(&m[0..9]),
translation: Vec3A::from_slice(&m[9..12]),
}
}
#[inline]
pub fn to_cols_array(&self) -> [f32; 12] {
let x = &self.matrix3.x_axis;
let y = &self.matrix3.y_axis;
let z = &self.matrix3.z_axis;
let w = &self.translation;
[x.x, x.y, x.z, y.x, y.y, y.z, z.x, z.y, z.z, w.x, w.y, w.z]
}
#[inline]
pub fn from_cols_array_2d(m: &[[f32; 3]; 4]) -> Self {
Self {
matrix3: Mat3A::from_cols(m[0].into(), m[1].into(), m[2].into()),
translation: m[3].into(),
}
}
#[inline]
pub fn to_cols_array_2d(&self) -> [[f32; 3]; 4] {
[
self.matrix3.x_axis.into(),
self.matrix3.y_axis.into(),
self.matrix3.z_axis.into(),
self.translation.into(),
]
}
#[inline]
pub fn from_cols_slice(slice: &[f32]) -> Self {
Self {
matrix3: Mat3A::from_cols_slice(&slice[0..9]),
translation: Vec3A::from_slice(&slice[9..12]),
}
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f32]) {
self.matrix3.write_cols_to_slice(&mut slice[0..9]);
self.translation.write_to_slice(&mut slice[9..12]);
}
#[inline]
pub fn from_scale(scale: Vec3) -> Self {
Self {
matrix3: Mat3A::from_diagonal(scale),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_quat(rotation: Quat) -> Self {
Self {
matrix3: Mat3A::from_quat(rotation),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
Self {
matrix3: Mat3A::from_axis_angle(axis, angle),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_rotation_x(angle: f32) -> Self {
Self {
matrix3: Mat3A::from_rotation_x(angle),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_rotation_y(angle: f32) -> Self {
Self {
matrix3: Mat3A::from_rotation_y(angle),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_rotation_z(angle: f32) -> Self {
Self {
matrix3: Mat3A::from_rotation_z(angle),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_translation(translation: Vec3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
matrix3: Mat3A::IDENTITY,
translation: translation.into(),
}
}
#[inline]
pub fn from_mat3(mat3: Mat3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
matrix3: mat3.into(),
translation: Vec3A::ZERO,
}
}
#[inline]
pub fn from_mat3_translation(mat3: Mat3, translation: Vec3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
matrix3: mat3.into(),
translation: translation.into(),
}
}
#[inline]
pub fn from_scale_rotation_translation(scale: Vec3, rotation: Quat, translation: Vec3) -> Self {
let rotation = Mat3A::from_quat(rotation);
#[allow(clippy::useless_conversion)]
Self {
matrix3: Mat3A::from_cols(
rotation.x_axis * scale.x,
rotation.y_axis * scale.y,
rotation.z_axis * scale.z,
),
translation: translation.into(),
}
}
#[inline]
pub fn from_rotation_translation(rotation: Quat, translation: Vec3) -> Self {
#[allow(clippy::useless_conversion)]
Self {
matrix3: Mat3A::from_quat(rotation),
translation: translation.into(),
}
}
#[inline]
pub fn from_mat4(m: Mat4) -> Self {
Self {
matrix3: Mat3A::from_cols(
Vec3A::from_vec4(m.x_axis),
Vec3A::from_vec4(m.y_axis),
Vec3A::from_vec4(m.z_axis),
),
translation: Vec3A::from_vec4(m.w_axis),
}
}
#[inline]
pub fn to_scale_rotation_translation(&self) -> (Vec3, Quat, Vec3) {
#[cfg(feature = "libm")]
#[allow(unused_imports)]
use num_traits::Float;
let det = self.matrix3.determinant();
glam_assert!(det != 0.0);
let scale = Vec3::new(
self.matrix3.x_axis.length() * det.signum(),
self.matrix3.y_axis.length(),
self.matrix3.z_axis.length(),
);
glam_assert!(scale.cmpne(Vec3::ZERO).all());
let inv_scale = scale.recip();
#[allow(clippy::useless_conversion)]
let rotation = Quat::from_mat3(&Mat3::from_cols(
(self.matrix3.x_axis * inv_scale.x).into(),
(self.matrix3.y_axis * inv_scale.y).into(),
(self.matrix3.z_axis * inv_scale.z).into(),
));
#[allow(clippy::useless_conversion)]
(scale, rotation, self.translation.into())
}
#[inline]
pub fn look_to_lh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
Self::look_to_rh(eye, -dir, up)
}
#[inline]
pub fn look_to_rh(eye: Vec3, dir: Vec3, up: Vec3) -> Self {
let f = dir.normalize();
let s = f.cross(up).normalize();
let u = s.cross(f);
Self {
matrix3: Mat3A::from_cols(
Vec3A::new(s.x, u.x, -f.x),
Vec3A::new(s.y, u.y, -f.y),
Vec3A::new(s.z, u.z, -f.z),
),
translation: Vec3A::new(-eye.dot(s), -eye.dot(u), eye.dot(f)),
}
}
#[inline]
pub fn look_at_lh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_lh(eye, center - eye, up)
}
#[inline]
pub fn look_at_rh(eye: Vec3, center: Vec3, up: Vec3) -> Self {
glam_assert!(up.is_normalized());
Self::look_to_rh(eye, center - eye, up)
}
#[inline]
pub fn transform_point3(&self, rhs: Vec3) -> Vec3 {
#[allow(clippy::useless_conversion)]
((self.matrix3.x_axis * rhs.x)
+ (self.matrix3.y_axis * rhs.y)
+ (self.matrix3.z_axis * rhs.z)
+ self.translation)
.into()
}
#[inline]
pub fn transform_vector3(&self, rhs: Vec3) -> Vec3 {
#[allow(clippy::useless_conversion)]
((self.matrix3.x_axis * rhs.x)
+ (self.matrix3.y_axis * rhs.y)
+ (self.matrix3.z_axis * rhs.z))
.into()
}
#[inline]
pub fn transform_point3a(&self, rhs: Vec3A) -> Vec3A {
self.matrix3 * rhs + self.translation
}
#[inline]
pub fn transform_vector3a(&self, rhs: Vec3A) -> Vec3A {
self.matrix3 * rhs
}
#[inline]
pub fn is_finite(&self) -> bool {
self.matrix3.is_finite() && self.translation.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool {
self.matrix3.is_nan() || self.translation.is_nan()
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.matrix3.abs_diff_eq(rhs.matrix3, max_abs_diff)
&& self.translation.abs_diff_eq(rhs.translation, max_abs_diff)
}
#[must_use]
#[inline]
pub fn inverse(&self) -> Self {
let matrix3 = self.matrix3.inverse();
let translation = -(matrix3 * self.translation);
Self {
matrix3,
translation,
}
}
}
impl Default for Affine3A {
#[inline(always)]
fn default() -> Self {
Self::IDENTITY
}
}
impl Deref for Affine3A {
type Target = crate::deref::Cols4<Vec3A>;
#[inline(always)]
fn deref(&self) -> &Self::Target {
unsafe { &*(self as *const Self as *const Self::Target) }
}
}
impl DerefMut for Affine3A {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut *(self as *mut Self as *mut Self::Target) }
}
}
impl PartialEq for Affine3A {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.matrix3.eq(&rhs.matrix3) && self.translation.eq(&rhs.translation)
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Debug for Affine3A {
fn fmt(&self, fmt: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
fmt.debug_struct(stringify!(Affine3A))
.field("matrix3", &self.matrix3)
.field("translation", &self.translation)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl core::fmt::Display for Affine3A {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(
f,
"[{}, {}, {}, {}]",
self.matrix3.x_axis, self.matrix3.y_axis, self.matrix3.z_axis, self.translation
)
}
}
impl<'a> core::iter::Product<&'a Self> for Affine3A {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| a * b)
}
}
impl Mul for Affine3A {
type Output = Affine3A;
#[inline]
fn mul(self, rhs: Affine3A) -> Self::Output {
Self {
matrix3: self.matrix3 * rhs.matrix3,
translation: self.matrix3 * rhs.translation + self.translation,
}
}
}
impl From<Affine3A> for Mat4 {
#[inline]
fn from(m: Affine3A) -> Mat4 {
Mat4::from_cols(
m.matrix3.x_axis.extend(0.0),
m.matrix3.y_axis.extend(0.0),
m.matrix3.z_axis.extend(0.0),
m.translation.extend(1.0),
)
}
}
impl Mul<Mat4> for Affine3A {
type Output = Mat4;
#[inline]
fn mul(self, rhs: Mat4) -> Self::Output {
Mat4::from(self) * rhs
}
}
impl Mul<Affine3A> for Mat4 {
type Output = Mat4;
#[inline]
fn mul(self, rhs: Affine3A) -> Self::Output {
self * Mat4::from(rhs)
}
}