use crate::{
euler::{FromEuler, ToEuler},
f64::math,
swizzles::*,
DMat2, DMat4, DQuat, DVec2, DVec3, EulerRot, Mat3,
};
use core::fmt;
use core::iter::{Product, Sum};
use core::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Neg, Sub, SubAssign};
#[inline(always)]
#[must_use]
pub const fn dmat3(x_axis: DVec3, y_axis: DVec3, z_axis: DVec3) -> DMat3 {
DMat3::from_cols(x_axis, y_axis, z_axis)
}
#[derive(Clone, Copy)]
#[repr(C)]
pub struct DMat3 {
pub x_axis: DVec3,
pub y_axis: DVec3,
pub z_axis: DVec3,
}
impl DMat3 {
pub const ZERO: Self = Self::from_cols(DVec3::ZERO, DVec3::ZERO, DVec3::ZERO);
pub const IDENTITY: Self = Self::from_cols(DVec3::X, DVec3::Y, DVec3::Z);
pub const NAN: Self = Self::from_cols(DVec3::NAN, DVec3::NAN, DVec3::NAN);
#[allow(clippy::too_many_arguments)]
#[inline(always)]
#[must_use]
const fn new(
m00: f64,
m01: f64,
m02: f64,
m10: f64,
m11: f64,
m12: f64,
m20: f64,
m21: f64,
m22: f64,
) -> Self {
Self {
x_axis: DVec3::new(m00, m01, m02),
y_axis: DVec3::new(m10, m11, m12),
z_axis: DVec3::new(m20, m21, m22),
}
}
#[inline(always)]
#[must_use]
pub const fn from_cols(x_axis: DVec3, y_axis: DVec3, z_axis: DVec3) -> Self {
Self {
x_axis,
y_axis,
z_axis,
}
}
#[inline]
#[must_use]
pub const fn from_cols_array(m: &[f64; 9]) -> Self {
Self::new(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8])
}
#[inline]
#[must_use]
pub const fn to_cols_array(&self) -> [f64; 9] {
[
self.x_axis.x,
self.x_axis.y,
self.x_axis.z,
self.y_axis.x,
self.y_axis.y,
self.y_axis.z,
self.z_axis.x,
self.z_axis.y,
self.z_axis.z,
]
}
#[inline]
#[must_use]
pub const fn from_cols_array_2d(m: &[[f64; 3]; 3]) -> Self {
Self::from_cols(
DVec3::from_array(m[0]),
DVec3::from_array(m[1]),
DVec3::from_array(m[2]),
)
}
#[inline]
#[must_use]
pub const fn to_cols_array_2d(&self) -> [[f64; 3]; 3] {
[
self.x_axis.to_array(),
self.y_axis.to_array(),
self.z_axis.to_array(),
]
}
#[doc(alias = "scale")]
#[inline]
#[must_use]
pub const fn from_diagonal(diagonal: DVec3) -> Self {
Self::new(
diagonal.x, 0.0, 0.0, 0.0, diagonal.y, 0.0, 0.0, 0.0, diagonal.z,
)
}
#[inline]
#[must_use]
pub fn from_mat4(m: DMat4) -> Self {
Self::from_cols(
DVec3::from_vec4(m.x_axis),
DVec3::from_vec4(m.y_axis),
DVec3::from_vec4(m.z_axis),
)
}
#[inline]
#[must_use]
pub fn from_mat4_minor(m: DMat4, i: usize, j: usize) -> Self {
match (i, j) {
(0, 0) => Self::from_cols(m.y_axis.yzw(), m.z_axis.yzw(), m.w_axis.yzw()),
(0, 1) => Self::from_cols(m.y_axis.xzw(), m.z_axis.xzw(), m.w_axis.xzw()),
(0, 2) => Self::from_cols(m.y_axis.xyw(), m.z_axis.xyw(), m.w_axis.xyw()),
(0, 3) => Self::from_cols(m.y_axis.xyz(), m.z_axis.xyz(), m.w_axis.xyz()),
(1, 0) => Self::from_cols(m.x_axis.yzw(), m.z_axis.yzw(), m.w_axis.yzw()),
(1, 1) => Self::from_cols(m.x_axis.xzw(), m.z_axis.xzw(), m.w_axis.xzw()),
(1, 2) => Self::from_cols(m.x_axis.xyw(), m.z_axis.xyw(), m.w_axis.xyw()),
(1, 3) => Self::from_cols(m.x_axis.xyz(), m.z_axis.xyz(), m.w_axis.xyz()),
(2, 0) => Self::from_cols(m.x_axis.yzw(), m.y_axis.yzw(), m.w_axis.yzw()),
(2, 1) => Self::from_cols(m.x_axis.xzw(), m.y_axis.xzw(), m.w_axis.xzw()),
(2, 2) => Self::from_cols(m.x_axis.xyw(), m.y_axis.xyw(), m.w_axis.xyw()),
(2, 3) => Self::from_cols(m.x_axis.xyz(), m.y_axis.xyz(), m.w_axis.xyz()),
(3, 0) => Self::from_cols(m.x_axis.yzw(), m.y_axis.yzw(), m.z_axis.yzw()),
(3, 1) => Self::from_cols(m.x_axis.xzw(), m.y_axis.xzw(), m.z_axis.xzw()),
(3, 2) => Self::from_cols(m.x_axis.xyw(), m.y_axis.xyw(), m.z_axis.xyw()),
(3, 3) => Self::from_cols(m.x_axis.xyz(), m.y_axis.xyz(), m.z_axis.xyz()),
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub fn from_quat(rotation: DQuat) -> 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(
DVec3::new(1.0 - (yy + zz), xy + wz, xz - wy),
DVec3::new(xy - wz, 1.0 - (xx + zz), yz + wx),
DVec3::new(xz + wy, yz - wx, 1.0 - (xx + yy)),
)
}
#[inline]
#[must_use]
pub fn from_axis_angle(axis: DVec3, angle: f64) -> Self {
glam_assert!(axis.is_normalized());
let (sin, cos) = math::sin_cos(angle);
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(
DVec3::new(x2 * omc + cos, xyomc + zsin, xzomc - ysin),
DVec3::new(xyomc - zsin, y2 * omc + cos, yzomc + xsin),
DVec3::new(xzomc + ysin, yzomc - xsin, z2 * omc + cos),
)
}
#[inline]
#[must_use]
pub fn from_euler(order: EulerRot, a: f64, b: f64, c: f64) -> Self {
Self::from_euler_angles(order, a, b, c)
}
#[inline]
#[must_use]
pub fn to_euler(&self, order: EulerRot) -> (f64, f64, f64) {
glam_assert!(
self.x_axis.is_normalized()
&& self.y_axis.is_normalized()
&& self.z_axis.is_normalized()
);
self.to_euler_angles(order)
}
#[inline]
#[must_use]
pub fn from_rotation_x(angle: f64) -> Self {
let (sina, cosa) = math::sin_cos(angle);
Self::from_cols(
DVec3::X,
DVec3::new(0.0, cosa, sina),
DVec3::new(0.0, -sina, cosa),
)
}
#[inline]
#[must_use]
pub fn from_rotation_y(angle: f64) -> Self {
let (sina, cosa) = math::sin_cos(angle);
Self::from_cols(
DVec3::new(cosa, 0.0, -sina),
DVec3::Y,
DVec3::new(sina, 0.0, cosa),
)
}
#[inline]
#[must_use]
pub fn from_rotation_z(angle: f64) -> Self {
let (sina, cosa) = math::sin_cos(angle);
Self::from_cols(
DVec3::new(cosa, sina, 0.0),
DVec3::new(-sina, cosa, 0.0),
DVec3::Z,
)
}
#[inline]
#[must_use]
pub fn from_translation(translation: DVec2) -> Self {
Self::from_cols(
DVec3::X,
DVec3::Y,
DVec3::new(translation.x, translation.y, 1.0),
)
}
#[inline]
#[must_use]
pub fn from_angle(angle: f64) -> Self {
let (sin, cos) = math::sin_cos(angle);
Self::from_cols(
DVec3::new(cos, sin, 0.0),
DVec3::new(-sin, cos, 0.0),
DVec3::Z,
)
}
#[inline]
#[must_use]
pub fn from_scale_angle_translation(scale: DVec2, angle: f64, translation: DVec2) -> Self {
let (sin, cos) = math::sin_cos(angle);
Self::from_cols(
DVec3::new(cos * scale.x, sin * scale.x, 0.0),
DVec3::new(-sin * scale.y, cos * scale.y, 0.0),
DVec3::new(translation.x, translation.y, 1.0),
)
}
#[inline]
#[must_use]
pub fn from_scale(scale: DVec2) -> Self {
glam_assert!(scale.cmpne(DVec2::ZERO).any());
Self::from_cols(
DVec3::new(scale.x, 0.0, 0.0),
DVec3::new(0.0, scale.y, 0.0),
DVec3::Z,
)
}
#[inline]
pub fn from_mat2(m: DMat2) -> Self {
Self::from_cols((m.x_axis, 0.0).into(), (m.y_axis, 0.0).into(), DVec3::Z)
}
#[inline]
#[must_use]
pub const fn from_cols_slice(slice: &[f64]) -> 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 [f64]) {
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]
#[must_use]
pub fn col(&self, index: usize) -> DVec3 {
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 DVec3 {
match index {
0 => &mut self.x_axis,
1 => &mut self.y_axis,
2 => &mut self.z_axis,
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub fn row(&self, index: usize) -> DVec3 {
match index {
0 => DVec3::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
1 => DVec3::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
2 => DVec3::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
_ => panic!("index out of bounds"),
}
}
#[inline]
#[must_use]
pub fn is_finite(&self) -> bool {
self.x_axis.is_finite() && self.y_axis.is_finite() && self.z_axis.is_finite()
}
#[inline]
#[must_use]
pub fn is_nan(&self) -> bool {
self.x_axis.is_nan() || self.y_axis.is_nan() || self.z_axis.is_nan()
}
#[inline]
#[must_use]
pub fn transpose(&self) -> Self {
Self {
x_axis: DVec3::new(self.x_axis.x, self.y_axis.x, self.z_axis.x),
y_axis: DVec3::new(self.x_axis.y, self.y_axis.y, self.z_axis.y),
z_axis: DVec3::new(self.x_axis.z, self.y_axis.z, self.z_axis.z),
}
}
#[inline]
#[must_use]
pub fn determinant(&self) -> f64 {
self.z_axis.dot(self.x_axis.cross(self.y_axis))
}
#[inline]
#[must_use]
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 = DVec3::splat(det.recip());
Self::from_cols(tmp0.mul(inv_det), tmp1.mul(inv_det), tmp2.mul(inv_det)).transpose()
}
#[inline]
#[must_use]
pub fn transform_point2(&self, rhs: DVec2) -> DVec2 {
glam_assert!(self.row(2).abs_diff_eq(DVec3::Z, 1e-6));
DMat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs + self.z_axis.xy()
}
#[inline]
#[must_use]
pub fn transform_vector2(&self, rhs: DVec2) -> DVec2 {
glam_assert!(self.row(2).abs_diff_eq(DVec3::Z, 1e-6));
DMat2::from_cols(self.x_axis.xy(), self.y_axis.xy()) * rhs
}
#[inline]
#[must_use]
pub fn mul_vec3(&self, rhs: DVec3) -> DVec3 {
let mut res = self.x_axis.mul(rhs.x);
res = res.add(self.y_axis.mul(rhs.y));
res = res.add(self.z_axis.mul(rhs.z));
res
}
#[inline]
#[must_use]
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]
#[must_use]
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]
#[must_use]
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]
#[must_use]
pub fn mul_scalar(&self, rhs: f64) -> Self {
Self::from_cols(
self.x_axis.mul(rhs),
self.y_axis.mul(rhs),
self.z_axis.mul(rhs),
)
}
#[inline]
#[must_use]
pub fn div_scalar(&self, rhs: f64) -> Self {
let rhs = DVec3::splat(rhs);
Self::from_cols(
self.x_axis.div(rhs),
self.y_axis.div(rhs),
self.z_axis.div(rhs),
)
}
#[inline]
#[must_use]
pub fn abs_diff_eq(&self, rhs: Self, max_abs_diff: f64) -> 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]
#[must_use]
pub fn abs(&self) -> Self {
Self::from_cols(self.x_axis.abs(), self.y_axis.abs(), self.z_axis.abs())
}
#[inline]
pub fn as_mat3(&self) -> Mat3 {
Mat3::from_cols(
self.x_axis.as_vec3(),
self.y_axis.as_vec3(),
self.z_axis.as_vec3(),
)
}
}
impl Default for DMat3 {
#[inline]
fn default() -> Self {
Self::IDENTITY
}
}
impl Add<DMat3> for DMat3 {
type Output = Self;
#[inline]
fn add(self, rhs: Self) -> Self::Output {
self.add_mat3(&rhs)
}
}
impl AddAssign<DMat3> for DMat3 {
#[inline]
fn add_assign(&mut self, rhs: Self) {
*self = self.add_mat3(&rhs);
}
}
impl Sub<DMat3> for DMat3 {
type Output = Self;
#[inline]
fn sub(self, rhs: Self) -> Self::Output {
self.sub_mat3(&rhs)
}
}
impl SubAssign<DMat3> for DMat3 {
#[inline]
fn sub_assign(&mut self, rhs: Self) {
*self = self.sub_mat3(&rhs);
}
}
impl Neg for DMat3 {
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<DMat3> for DMat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: Self) -> Self::Output {
self.mul_mat3(&rhs)
}
}
impl MulAssign<DMat3> for DMat3 {
#[inline]
fn mul_assign(&mut self, rhs: Self) {
*self = self.mul_mat3(&rhs);
}
}
impl Mul<DVec3> for DMat3 {
type Output = DVec3;
#[inline]
fn mul(self, rhs: DVec3) -> Self::Output {
self.mul_vec3(rhs)
}
}
impl Mul<DMat3> for f64 {
type Output = DMat3;
#[inline]
fn mul(self, rhs: DMat3) -> Self::Output {
rhs.mul_scalar(self)
}
}
impl Mul<f64> for DMat3 {
type Output = Self;
#[inline]
fn mul(self, rhs: f64) -> Self::Output {
self.mul_scalar(rhs)
}
}
impl MulAssign<f64> for DMat3 {
#[inline]
fn mul_assign(&mut self, rhs: f64) {
*self = self.mul_scalar(rhs);
}
}
impl Div<DMat3> for f64 {
type Output = DMat3;
#[inline]
fn div(self, rhs: DMat3) -> Self::Output {
rhs.div_scalar(self)
}
}
impl Div<f64> for DMat3 {
type Output = Self;
#[inline]
fn div(self, rhs: f64) -> Self::Output {
self.div_scalar(rhs)
}
}
impl DivAssign<f64> for DMat3 {
#[inline]
fn div_assign(&mut self, rhs: f64) {
*self = self.div_scalar(rhs);
}
}
impl Sum<Self> for DMat3 {
fn sum<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::ZERO, Self::add)
}
}
impl<'a> Sum<&'a Self> for DMat3 {
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 DMat3 {
fn product<I>(iter: I) -> Self
where
I: Iterator<Item = Self>,
{
iter.fold(Self::IDENTITY, Self::mul)
}
}
impl<'a> Product<&'a Self> for DMat3 {
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 DMat3 {
#[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 AsRef<[f64; 9]> for DMat3 {
#[inline]
fn as_ref(&self) -> &[f64; 9] {
unsafe { &*(self as *const Self as *const [f64; 9]) }
}
}
#[cfg(not(target_arch = "spirv"))]
impl AsMut<[f64; 9]> for DMat3 {
#[inline]
fn as_mut(&mut self) -> &mut [f64; 9] {
unsafe { &mut *(self as *mut Self as *mut [f64; 9]) }
}
}
impl fmt::Debug for DMat3 {
fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt.debug_struct(stringify!(DMat3))
.field("x_axis", &self.x_axis)
.field("y_axis", &self.y_axis)
.field("z_axis", &self.z_axis)
.finish()
}
}
impl fmt::Display for DMat3 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(p) = f.precision() {
write!(
f,
"[{:.*}, {:.*}, {:.*}]",
p, self.x_axis, p, self.y_axis, p, self.z_axis
)
} else {
write!(f, "[{}, {}, {}]", self.x_axis, self.y_axis, self.z_axis)
}
}
}