use crate::{swizzles::*, DMat3, EulerRot, Mat2, Mat3, Mat4, Quat, Vec2, Vec3, Vec3A};
#[cfg(not(target_arch = "spirv"))]
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign};
use core::arch::wasm32::*;
#[cfg(feature = "libm")]
#[allow(unused_imports)]
use num_traits::Float;
#[inline(always)]
pub const fn mat3a(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Mat3A {
Mat3A::from_cols(x_axis, y_axis, z_axis)
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct Mat3A {
pub x_axis: Vec3A,
pub y_axis: Vec3A,
pub z_axis: Vec3A,
}
impl Mat3A {
pub const ZERO: Self = Self::from_cols(Vec3A::ZERO, Vec3A::ZERO, Vec3A::ZERO);
pub const IDENTITY: Self = Self::from_cols(Vec3A::X, Vec3A::Y, Vec3A::Z);
pub const NAN: Self = Self::from_cols(Vec3A::NAN, Vec3A::NAN, Vec3A::NAN);
#[allow(clippy::too_many_arguments)]
#[inline(always)]
const fn new(
m00: f32,
m01: f32,
m02: f32,
m10: f32,
m11: f32,
m12: f32,
m20: f32,
m21: f32,
m22: f32,
) -> Self {
Self {
x_axis: Vec3A::new(m00, m01, m02),
y_axis: Vec3A::new(m10, m11, m12),
z_axis: Vec3A::new(m20, m21, m22),
}
}
#[inline(always)]
pub const fn from_cols(x_axis: Vec3A, y_axis: Vec3A, z_axis: Vec3A) -> Self {
Self {
x_axis,
y_axis,
z_axis,
}
}
#[inline]
pub const fn from_cols_array(m: &[f32; 9]) -> Self {
Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
}
#[inline]
pub const fn to_cols_array(&self) -> [f32; 9] {
let [x_axis_x, x_axis_y, x_axis_z] = self.x_axis.to_array();
let [y_axis_x, y_axis_y, y_axis_z] = self.y_axis.to_array();
let [z_axis_x, z_axis_y, z_axis_z] = self.z_axis.to_array();
[
x_axis_x, x_axis_y, x_axis_z, y_axis_x, y_axis_y, y_axis_z, z_axis_x, z_axis_y,
z_axis_z,
]
}
#[inline]
pub const fn from_cols_array_2d(m: &[[f32; 3]; 3]) -> Self {
Self::from_cols(
Vec3A::from_array(m[0]),
Vec3A::from_array(m[1]),
Vec3A::from_array(m[2]),
)
}
#[inline]
pub const fn to_cols_array_2d(&self) -> [[f32; 3]; 3] {
[
self.x_axis.to_array(),
self.y_axis.to_array(),
self.z_axis.to_array(),
]
}
#[doc(alias = "scale")]
#[inline]
pub const fn from_diagonal(diagonal: Vec3) -> Self {
Self::new(
diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
)
}
pub fn from_mat4(m: Mat4) -> Self {
Self::from_cols(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
}
#[inline]
pub fn from_quat(rotation: Quat) -> Self {
glam_assert!(rotation.is_normalized());
let x2 = rotation.x + rotation.x;
let y2 = rotation.y + rotation.y;
let z2 = rotation.z + rotation.z;
let xx = rotation.x * x2;
let xy = rotation.x * y2;
let xz = rotation.x * z2;
let yy = rotation.y * y2;
let yz = rotation.y * z2;
let zz = rotation.z * z2;
let wx = rotation.w * x2;
let wy = rotation.w * y2;
let wz = rotation.w * z2;
Self::from_cols(
Vec3A::new(1.0 - (yy + zz), xy + wz, xz - wy),
Vec3A::new(xy - wz, 1.0 - (xx + zz), yz + wx),
Vec3A::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
)
}
#[inline]
pub fn from_axis_angle(axis: Vec3, angle: f32) -> Self {
glam_assert!(axis.is_normalized());
let (sin, cos) = angle.sin_cos();
let (xsin, ysin, zsin) = axis.mul(sin).into();
let (x, y, z) = axis.into();
let (x2, y2, z2) = axis.mul(axis).into();
let omc = 1.0 - cos;
let xyomc = x * y * omc;
let xzomc = x * z * omc;
let yzomc = y * z * omc;
Self::from_cols(
Vec3A::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
Vec3A::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
Vec3A::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
)
}
#[inline]
pub fn from_euler(order: EulerRot, a: f32, b: f32, c: f32) -> Self {
let quat = Quat::from_euler(order, a, b, c);
Self::from_quat(quat)
}
#[inline]
pub fn from_rotation_x(angle: f32) -> Self {
let (sina, cosa) = angle.sin_cos();
Self::from_cols(
Vec3A::X,
Vec3A::new(0.0, cosa, sina),
Vec3A::new(0.0, -sina, cosa),
)
}
#[inline]
pub fn from_rotation_y(angle: f32) -> Self {
let (sina, cosa) = angle.sin_cos();
Self::from_cols(
Vec3A::new(cosa, 0.0, -sina),
Vec3A::Y,
Vec3A::new(sina, 0.0, cosa),
)
}
#[inline]
pub fn from_rotation_z(angle: f32) -> Self {
let (sina, cosa) = angle.sin_cos();
Self::from_cols(
Vec3A::new(cosa, sina, 0.0),
Vec3A::new(-sina, cosa, 0.0),
Vec3A::Z,
)
}
#[inline]
pub fn from_translation(translation: Vec2) -> Self {
Self::from_cols(
Vec3A::X,
Vec3A::Y,
Vec3A::new(translation.x, translation.y, 1.0),
)
}
#[inline]
pub fn from_angle(angle: f32) -> Self {
let (sin, cos) = angle.sin_cos();
Self::from_cols(
Vec3A::new(cos, sin, 0.0),
Vec3A::new(-sin, cos, 0.0),
Vec3A::Z,
)
}
#[inline]
pub fn from_scale_angle_translation(scale: Vec2, angle: f32, translation: Vec2) -> Self {
let (sin, cos) = angle.sin_cos();
Self::from_cols(
Vec3A::new(cos * scale.x, sin * scale.x, 0.0),
Vec3A::new(-sin * scale.y, cos * scale.y, 0.0),
Vec3A::new(translation.x, translation.y, 1.0),
)
}
#[inline]
pub fn from_scale(scale: Vec2) -> Self {
glam_assert!(scale.cmpne(Vec2::ZERO).any());
Self::from_cols(
Vec3A::new(scale.x, 0.0, 0.0),
Vec3A::new(0.0, scale.y, 0.0),
Vec3A::Z,
)
}
#[inline]
pub fn from_mat2(m: Mat2) -> Self {
Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), Vec3A::Z)
}
#[inline]
pub const fn from_cols_slice(slice: &[f32]) -> Self {
Self::new(
slice[0], slice[1], slice[2], slice[3], slice[4], slice[5], slice[6], slice[7],
slice[8],
)
}
#[inline]
pub fn write_cols_to_slice(self, slice: &mut [f32]) {
slice[0] = self.x_axis.x;
slice[1] = self.x_axis.y;
slice[2] = self.x_axis.z;
slice[3] = self.y_axis.x;
slice[4] = self.y_axis.y;
slice[5] = self.y_axis.z;
slice[6] = self.z_axis.x;
slice[7] = self.z_axis.y;
slice[8] = self.z_axis.z;
}
#[inline]
pub fn col(&self, index: usize) -> Vec3A {
match index {
0 => self.x_axis,
1 => self.y_axis,
2 => self.z_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn col_mut(&mut self, index: usize) -> &mut Vec3A {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
2 => &mut self.z_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn row(&self, index: usize) -> Vec3A {
match index {
0 => Vec3A::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
1 => Vec3A::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
2 => Vec3A::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
_ => panic!("index out of bounds"),
}
}
#[inline]
pub fn is_finite(&self) -> bool {
self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
}
#[inline]
pub fn is_nan(&self) -> bool {
self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
}
#[must_use]
#[inline]
pub fn transpose(&self) -> Self {
let tmp0 = i32x4_shuffle::<0, 1, 4, 5>(self.x_axis.0, self.y_axis.0);
let tmp1 = i32x4_shuffle::<2, 3, 6, 7>(self.x_axis.0, self.y_axis.0);
Self {
x_axis: Vec3A(i32x4_shuffle::<0, 2, 4, 4>(tmp0, self.z_axis.0)),
y_axis: Vec3A(i32x4_shuffle::<1, 3, 5, 5>(tmp0, self.z_axis.0)),
z_axis: Vec3A(i32x4_shuffle::<0, 2, 6, 6>(tmp1, self.z_axis.0)),
}
}
#[inline]
pub fn determinant(&self) -> f32 {
self.z_axis.dot(self.x_axis.cross(self.y_axis))
}
#[must_use]
#[inline]
pub fn inverse(&self) -> Self {
let tmp0 = self.y_axis.cross(self.z_axis);
let tmp1 = self.z_axis.cross(self.x_axis);
let tmp2 = self.x_axis.cross(self.y_axis);
let det = self.z_axis.dot(tmp2);
glam_assert!(det != 0.0);
let inv_det = Vec3A::splat(det.recip());
Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
}
#[inline]
pub fn transform_point2(&self, rhs: Vec2) -> Vec2 {
glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
}
#[inline]
pub fn transform_vector2(&self, rhs: Vec2) -> Vec2 {
glam_assert!(self.row(2).abs_diff_eq(Vec3A::Z, 1e-6));
Mat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
}
#[inline]
pub fn mul_vec3(&self, rhs: Vec3) -> Vec3 {
self.mul_vec3a(rhs.into()).into()
}
#[inline]
pub fn mul_vec3a(&self, rhs: Vec3A) -> Vec3A {
let mut res = self.x_axis.mul(rhs.xxx());
res = res.add(self.y_axis.mul(rhs.yyy()));
res = res.add(self.z_axis.mul(rhs.zzz()));
res
}
#[inline]
pub fn mul_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
self.mul(rhs.x_axis),
self.mul(rhs.y_axis),
self.mul(rhs.z_axis),
)
}
#[inline]
pub fn add_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
self.x_axis.add(rhs.x_axis),
self.y_axis.add(rhs.y_axis),
self.z_axis.add(rhs.z_axis),
)
}
#[inline]
pub fn sub_mat3(&self, rhs: &Self) -> Self {
Self::from_cols(
self.x_axis.sub(rhs.x_axis),
self.y_axis.sub(rhs.y_axis),
self.z_axis.sub(rhs.z_axis),
)
}
#[inline]
pub fn mul_scalar(&self, rhs: f32) -> Self {
Self::from_cols(
self.x_axis.mul(rhs),
self.y_axis.mul(rhs),
self.z_axis.mul(rhs),
)
}
#[inline]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f32) -> bool {
self.x_axis.abs_diff_eq(rhs.x_axis, max_abs_diff)
&& self.y_axis.abs_diff_eq(rhs.y_axis, max_abs_diff)
&& self.z_axis.abs_diff_eq(rhs.z_axis, max_abs_diff)
}
#[inline]
pub fn as_dmat3(&self) -> DMat3 {
DMat3::from_cols(
self.x_axis.as_dvec3(),
self.y_axis.as_dvec3(),
self.z_axis.as_dvec3(),
)
}
}
impl Default for Mat3A {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl Add<Mat3A> for Mat3A {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
self.add_mat3(&rhs)
}
}
impl AddAssign<Mat3A> for Mat3A {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = self.add_mat3(&rhs);
}
}
impl Sub<Mat3A> for Mat3A {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
self.sub_mat3(&rhs)
}
}
impl SubAssign<Mat3A> for Mat3A {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = self.sub_mat3(&rhs);
}
}
impl Neg for Mat3A {
type Output = Self;
#[inline]
fn neg(self) -> Self::Output {
Self::from_cols(self.x_axis.neg(), self.y_axis.neg(), self.z_axis.neg())
}
}
impl Mul<Mat3A> for Mat3A {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
self.mul_mat3(&rhs)
}
}
impl MulAssign<Mat3A> for Mat3A {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = self.mul_mat3(&rhs);
}
}
impl Mul<Vec3A> for Mat3A {
type Output = Vec3A;
#[inline]
fn mul(self, rhs: Vec3A) -> Self::Output {
self.mul_vec3a(rhs)
}
}
impl Mul<Mat3A> for f32 {
type Output = Mat3A;
#[inline]
fn mul(self, rhs: Mat3A) -> Self::Output {
rhs.mul_scalar(self)
}
}
impl Mul<f32> for Mat3A {
type Output = Self;
#[inline]
fn mul(self, rhs: f32) -> Self::Output {
self.mul_scalar(rhs)
}
}
impl MulAssign<f32> for Mat3A {
#[inline]
fn mul_assign(&mut self, rhs: f32) {
*self = self.mul_scalar(rhs);
}
}
impl Mul<Vec3> for Mat3A {
type Output = Vec3;
#[inline]
fn mul(self, rhs: Vec3) -> Vec3 {
self.mul_vec3a(rhs.into()).into()
}
}
impl From<Mat3> for Mat3A {
#[inline]
fn from(m: Mat3) -> Self {
Self {
x_axis: m.x_axis.into(),
y_axis: m.y_axis.into(),
z_axis: m.z_axis.into(),
}
}
}
impl Sum<Self> for Mat3A {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::ZERO, Self::add)
}
}
impl<'a> Sum<&'a Self> for Mat3A {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::ZERO, |a, &b| Self::add(a, b))
}
}
impl Product for Mat3A {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::IDENTITY, Self::mul)
}
}
impl<'a> Product<&'a Self> for Mat3A {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = &'a Self>,
{
iter.fold(Self::IDENTITY, |a, &b| Self::mul(a, b))
}
}
impl PartialEq for Mat3A {
#[inline]
fn eq(&self, rhs: &Self) -> bool {
self.x_axis.eq(&rhs.x_axis) && self.y_axis.eq(&rhs.y_axis) && self.z_axis.eq(&rhs.z_axis)
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Debug for Mat3A {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(stringify!(Mat3A))
.field("x_axis", &self.x_axis)
.field("y_axis", &self.y_axis)
.field("z_axis", &self.z_axis)
.finish()
}
}
#[cfg(not(target_arch = "spirv"))]
impl fmt::Display for Mat3A {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
}
}