use super::*;
#[derive(Clone, Copy, Default, PartialEq)]
#[repr(align(16), C)]
pub struct Mat3 {
pub(crate) x_axis: Vec3,
pub(crate) y_axis: Vec3,
pub(crate) z_axis: Vec3,
}
unsafe impl Zeroable for Mat3 {}
unsafe impl Pod for Mat3 {}
impl core::fmt::Debug for Mat3 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
f.write_str("Mat3 { x_axis: (")?;
let [x, y, z, _]: [f32; 4] = cast(self.x_axis);
core::fmt::Debug::fmt(&x, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&y, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&z, f)?;
f.write_str("), y_axis: (")?;
let [x, y, z, _]: [f32; 4] = cast(self.y_axis);
core::fmt::Debug::fmt(&x, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&y, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&z, f)?;
f.write_str("), z_axis: (")?;
let [x, y, z, _]: [f32; 4] = cast(self.z_axis);
core::fmt::Debug::fmt(&x, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&y, f)?;
f.write_str(", ")?;
core::fmt::Debug::fmt(&z, f)?;
f.write_str(") }")
}
}
impl core::fmt::Display for Mat3 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
f.write_str("| ")?;
core::fmt::Display::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&y0, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&z0, f)?;
f.write_str(" |\n| ")?;
core::fmt::Display::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&y1, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&z1, f)?;
f.write_str(" |\n| ")?;
core::fmt::Display::fmt(&x2, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&y2, f)?;
f.write_str(", ")?;
core::fmt::Display::fmt(&z2, f)?;
f.write_str(" |")
}
}
impl core::fmt::LowerExp for Mat3 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
f.write_str("| ")?;
core::fmt::LowerExp::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&y0, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&z0, f)?;
f.write_str(" |\n| ")?;
core::fmt::LowerExp::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&y1, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&z1, f)?;
f.write_str(" |\n| ")?;
core::fmt::LowerExp::fmt(&x2, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&y2, f)?;
f.write_str(", ")?;
core::fmt::LowerExp::fmt(&z2, f)?;
f.write_str(" |")
}
}
impl core::fmt::UpperExp for Mat3 {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
let [x0, x1, x2, _]: [f32; 4] = cast(self.x_axis);
let [y0, y1, y2, _]: [f32; 4] = cast(self.y_axis);
let [z0, z1, z2, _]: [f32; 4] = cast(self.z_axis);
f.write_str("| ")?;
core::fmt::UpperExp::fmt(&x0, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&y0, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&z0, f)?;
f.write_str(" |\n| ")?;
core::fmt::UpperExp::fmt(&x1, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&y1, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&z1, f)?;
f.write_str(" |\n| ")?;
core::fmt::UpperExp::fmt(&x2, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&y2, f)?;
f.write_str(", ")?;
core::fmt::UpperExp::fmt(&z2, f)?;
f.write_str(" |")
}
}
impl Index<usize> for Mat3 {
type Output = Vec3;
#[inline(always)]
fn index(&self, index: usize) -> &Vec3 {
match index {
0 => &self.x_axis,
1 => &self.y_axis,
2 => &self.z_axis,
otherwise => panic!("Mat3 index out of bounds: {}", otherwise),
}
}
}
impl IndexMut<usize> for Mat3 {
#[inline(always)]
fn index_mut(&mut self, index: usize) -> &mut Vec3 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
2 => &mut self.z_axis,
otherwise => panic!("Mat3 index out of bounds: {}", otherwise),
}
}
}
impl AsRef<[Vec3; 3]> for Mat3 {
#[inline(always)]
fn as_ref(&self) -> &[Vec3; 3] {
cast_ref(self)
}
}
impl AsMut<[Vec3; 3]> for Mat3 {
#[inline(always)]
fn as_mut(&mut self) -> &mut [Vec3; 3] {
cast_mut(self)
}
}
impl From<[f32; 9]> for Mat3 {
#[inline]
fn from([xx, xy, xz, yx, yy, yz, zx, zy, zz]: [f32; 9]) -> Self {
Self {
x_axis: Vec3::from([xx, xy, xz]),
y_axis: Vec3::from([yx, yy, yz]),
z_axis: Vec3::from([zx, zy, zz]),
}
}
}
impl From<Mat3> for [f32; 9] {
#[inline]
fn from(
Mat3 {
x_axis,
y_axis,
z_axis,
}: Mat3,
) -> Self {
let [e0, e1, e2]: [f32; 3] = x_axis.into();
let [e3, e4, e5]: [f32; 3] = y_axis.into();
let [e6, e7, e8]: [f32; 3] = z_axis.into();
[e0, e1, e2, e3, e4, e5, e6, e7, e8]
}
}
#[cfg(feature = "mint")]
impl From<mint::ColumnMatrix3<f32>> for Mat3 {
#[inline]
fn from(mint::ColumnMatrix3 { x, y, z }: mint::ColumnMatrix3<f32>) -> Self {
let x_axis: Vec3 = x.into();
let y_axis: Vec3 = y.into();
let z_axis: Vec3 = z.into();
Self {
x_axis,
y_axis,
z_axis,
}
}
}
#[cfg(feature = "mint")]
impl From<Mat3> for mint::ColumnMatrix3<f32> {
#[inline]
fn from(
Mat3 {
x_axis,
y_axis,
z_axis,
}: Mat3,
) -> Self {
let x: mint::Vector3<f32> = x_axis.into();
let y: mint::Vector3<f32> = y_axis.into();
let z: mint::Vector3<f32> = z_axis.into();
Self { x, y, z }
}
}
#[cfg(feature = "serde")]
impl serde::Serialize for Mat3 {
#[inline]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
<[f32; 9]>::from(*self).serialize(serializer)
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Mat3 {
#[inline]
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
Ok(Self::from(<[f32; 9]>::deserialize(deserializer)?))
}
}
impl Add for Mat3 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self {
Self {
x_axis: self.x_axis + rhs.x_axis,
y_axis: self.y_axis + rhs.y_axis,
z_axis: self.z_axis + rhs.z_axis,
}
}
}
impl Add<f32> for Mat3 {
type Output = Self;
#[inline]
fn add(self, rhs: f32) -> Self {
self + Self::splat(rhs)
}
}
impl Add<Mat3> for f32 {
type Output = Mat3;
#[inline]
fn add(self, rhs: Mat3) -> Mat3 {
Mat3::splat(self) + rhs
}
}
impl AddAssign for Mat3 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = *self + rhs
}
}
impl AddAssign<f32> for Mat3 {
#[inline]
fn add_assign(&mut self, rhs: f32) {
*self = *self + Self::splat(rhs)
}
}
impl Sub for Mat3 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self {
Self {
x_axis: self.x_axis - rhs.x_axis,
y_axis: self.y_axis - rhs.y_axis,
z_axis: self.z_axis - rhs.z_axis,
}
}
}
impl Sub<f32> for Mat3 {
type Output = Self;
#[inline]
fn sub(self, rhs: f32) -> Self {
self - Self::splat(rhs)
}
}
impl Sub<Mat3> for f32 {
type Output = Mat3;
#[inline]
fn sub(self, rhs: Mat3) -> Mat3 {
Mat3::splat(self) - rhs
}
}
impl SubAssign for Mat3 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = *self - rhs
}
}
impl SubAssign<f32> for Mat3 {
#[inline]
fn sub_assign(&mut self, rhs: f32) {
*self = *self - Self::splat(rhs)
}
}
impl Neg for Mat3 {
type Output = Self;
#[inline]
fn neg(self) -> Self {
Self {
x_axis: -self.x_axis,
y_axis: -self.y_axis,
z_axis: -self.z_axis,
}
}
}
impl Mul<f32> for Mat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: f32) -> Self {
Self {
x_axis: self.x_axis * rhs,
y_axis: self.y_axis * rhs,
z_axis: self.z_axis * rhs,
}
}
}
impl Mul<Mat3> for f32 {
type Output = Mat3;
#[inline]
fn mul(self, rhs: Mat3) -> Mat3 {
rhs * self
}
}
impl MulAssign<f32> for Mat3 {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
*self = *self * rhs;
}
}
impl Mul<Vec3> for Mat3 {
type Output = Vec3;
#[inline]
fn mul(self, v3: Vec3) -> Vec3 {
Vec3::new(
Vec3::new(self.x_axis.x(), self.y_axis.x(), self.z_axis.x()).dot(v3),
Vec3::new(self.x_axis.y(), self.y_axis.y(), self.z_axis.y()).dot(v3),
Vec3::new(self.x_axis.z(), self.y_axis.z(), self.z_axis.z()).dot(v3),
)
}
}
impl Mul for Mat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self {
Self {
x_axis: self * rhs.x_axis,
y_axis: self * rhs.y_axis,
z_axis: self * rhs.z_axis,
}
}
}
impl MulAssign for Mat3 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = *self * rhs;
}
}
impl Mat3 {
#[inline(always)]
pub fn x_axis(self) -> Vec3 {
self.x_axis
}
#[inline(always)]
pub fn y_axis(self) -> Vec3 {
self.y_axis
}
#[inline(always)]
pub fn z_axis(self) -> Vec3 {
self.z_axis
}
#[inline(always)]
pub fn x_axis_mut(&mut self) -> &mut Vec3 {
&mut self.x_axis
}
#[inline(always)]
pub fn y_axis_mut(&mut self) -> &mut Vec3 {
&mut self.y_axis
}
#[inline(always)]
pub fn z_axis_mut(&mut self) -> &mut Vec3 {
&mut self.z_axis
}
}
impl Mat3 {
#[inline(always)]
pub fn new(x_axis: Vec3, y_axis: Vec3, z_axis: Vec3) -> Self {
Self {
x_axis,
y_axis,
z_axis,
}
}
#[inline]
pub fn splat(v: f32) -> Self {
Self {
x_axis: Vec3::splat(v),
y_axis: Vec3::splat(v),
z_axis: Vec3::splat(v),
}
}
#[inline]
pub fn diagonal(x: f32, y: f32, z: f32) -> Self {
let x_axis = Vec3::new(x, 0.0, 0.0);
let y_axis = Vec3::new(0.0, y, 0.0);
let z_axis = Vec3::new(0.0, 0.0, z);
Self {
x_axis,
y_axis,
z_axis,
}
}
#[inline]
pub fn to_mat2(self) -> Mat2 {
let x_axis = self.x_axis.to_vec2();
let y_axis = self.y_axis.to_vec2();
Mat2 { x_axis, y_axis }
}
#[inline]
pub fn to_mat4(self, w: f32) -> Mat4 {
let x_axis = self.x_axis.to_vec4(0.0);
let y_axis = self.y_axis.to_vec4(0.0);
let z_axis = self.z_axis.to_vec4(0.0);
let w_axis = Vec4::new(0.0, 0.0, 0.0, w);
Mat4 {
x_axis,
y_axis,
z_axis,
w_axis,
}
}
#[inline]
pub fn determinant(self) -> f32 {
let a = self.x_axis.x();
let b = self.y_axis.x();
let c = self.z_axis.x();
let d = self.x_axis.y();
let e = self.y_axis.y();
let f = self.z_axis.y();
let g = self.x_axis.z();
let h = self.y_axis.z();
let i = self.z_axis.z();
a * (e * i - f * h) - b * (d * i - f * g) + c * (d * h - e * g)
}
#[inline]
#[allow(unused_mut)]
pub fn transpose(mut self) -> Self {
if_sse! {{
let mut dummy = Vec4::default();
transpose4(
&mut self.x_axis.v4.sse,
&mut self.y_axis.v4.sse,
&mut self.z_axis.v4.sse,
&mut dummy.sse
);
self
} else {
let a = self.x_axis.x();
let b = self.y_axis.x();
let c = self.z_axis.x();
let d = self.x_axis.y();
let e = self.y_axis.y();
let f = self.z_axis.y();
let g = self.x_axis.z();
let h = self.y_axis.z();
let i = self.z_axis.z();
let x_axis = Vec3::new(a, b, c);
let y_axis = Vec3::new(d, e, f);
let z_axis = Vec3::new(g, h, i);
Self {
x_axis,
y_axis,
z_axis,
}
}}
}
#[inline]
pub fn inverse(self) -> Option<Self> {
let self_det = self.determinant();
if self_det != 0.0 {
let a = self.x_axis.x();
let b = self.y_axis.x();
let c = self.z_axis.x();
let d = self.x_axis.y();
let e = self.y_axis.y();
let f = self.z_axis.y();
let g = self.x_axis.z();
let h = self.y_axis.z();
let i = self.z_axis.z();
let a_det = Mat2::from([e, h, f, i]).determinant();
let b_det = Mat2::from([d, g, f, i]).determinant();
let c_det = Mat2::from([d, g, e, h]).determinant();
let d_det = Mat2::from([b, h, c, i]).determinant();
let e_det = Mat2::from([a, g, c, i]).determinant();
let f_det = Mat2::from([a, g, b, h]).determinant();
let g_det = Mat2::from([b, e, c, f]).determinant();
let h_det = Mat2::from([a, d, c, f]).determinant();
let i_det = Mat2::from([a, d, b, e]).determinant();
let b_det = -b_det;
let d_det = -d_det;
let f_det = -f_det;
let h_det = -h_det;
let adjugate = Mat3::from([
a_det, b_det, c_det, d_det, e_det, f_det, g_det, h_det, i_det,
]);
Some(adjugate * (1.0 / self_det))
} else {
None
}
}
}