fromsoftware_shared/dl_math/
matrix.rs

1use crate::{F32Vector2, F32Vector3, F32Vector4};
2
3#[repr(C, align(16))]
4#[derive(Debug, Clone, Copy, PartialEq)]
5/// Row-major 4x4 float matrix.
6pub struct F32Matrix4x4(
7    pub F32Vector4,
8    pub F32Vector4,
9    pub F32Vector4,
10    pub F32Vector4,
11);
12
13#[repr(C, align(16))]
14#[derive(Debug, Clone, Copy, PartialEq)]
15/// Column-major 4x3 float matrix.
16pub struct F32Matrix4x3(pub F32Vector4, pub F32Vector4, pub F32Vector4);
17
18#[repr(C, align(16))]
19#[derive(Debug, Clone, Copy, PartialEq)]
20/// Column-major 4x2 float matrix.
21pub struct F32Matrix4x2(
22    pub F32Vector2,
23    pub F32Vector2,
24    pub F32Vector2,
25    pub F32Vector2,
26);
27
28#[repr(C, align(16))]
29#[derive(Debug, Clone, Copy, PartialEq)]
30/// Row-major 3x4 float matrix.
31pub struct F32Matrix3x4(pub F32Vector4, pub F32Vector4, pub F32Vector4);
32
33#[repr(C)]
34#[derive(Debug, Clone, Copy, PartialEq)]
35/// Row-major 3x3 float matrix.
36pub struct F32Matrix3x3(pub F32Vector3, pub F32Vector3, pub F32Vector3);
37
38#[repr(C)]
39#[derive(Debug, Clone, Copy, PartialEq)]
40/// Column-major 3x2 float matrix.
41pub struct F32Matrix3x2(pub F32Vector2, pub F32Vector2, pub F32Vector2);
42
43#[repr(C)]
44#[derive(Debug, Clone, Copy, PartialEq)]
45/// Row-major 2x4 float matrix.
46pub struct F32Matrix2x4(pub F32Vector4, pub F32Vector4);
47
48#[repr(C)]
49#[derive(Debug, Clone, Copy, PartialEq)]
50/// Row-major 2x3 float matrix.
51pub struct F32Matrix2x3(pub F32Vector3, pub F32Vector3);
52
53#[repr(C)]
54#[derive(Debug, Clone, Copy, PartialEq)]
55/// Row-major 2x2 float matrix.
56pub struct F32Matrix2x2(pub F32Vector2, pub F32Vector2);
57
58macro_rules! impl_matrix_new {
59    ($MatrixType:ident, $VectorType:ident, $($param:ident),+) => {
60        impl $MatrixType {
61            /// Construct from an array of row/column vectors.
62            #[inline]
63            pub fn new($($param: $VectorType),+) -> Self {
64                Self($($param),+)
65            }
66        }
67    };
68}
69
70impl_matrix_new!(F32Matrix4x4, F32Vector4, r0, r1, r2, r3);
71impl_matrix_new!(F32Matrix4x3, F32Vector4, c0, c1, c2);
72impl_matrix_new!(F32Matrix4x2, F32Vector2, c0, c1, c2, c3);
73impl_matrix_new!(F32Matrix3x4, F32Vector4, r0, r1, r2);
74impl_matrix_new!(F32Matrix3x3, F32Vector3, r0, r1, r2);
75impl_matrix_new!(F32Matrix3x2, F32Vector2, c0, c1, c2);
76impl_matrix_new!(F32Matrix2x4, F32Vector4, r0, r1);
77impl_matrix_new!(F32Matrix2x3, F32Vector3, c0, c1);
78impl_matrix_new!(F32Matrix2x2, F32Vector2, r0, r1);
79
80impl From<F32Matrix4x4> for glam::Mat4 {
81    #[inline]
82    fn from(F32Matrix4x4(r0, r1, r2, r3): F32Matrix4x4) -> Self {
83        Self::from_cols(r0.into(), r1.into(), r2.into(), r3.into()).transpose()
84    }
85}
86
87impl From<F32Matrix3x3> for glam::Mat3 {
88    #[inline]
89    fn from(F32Matrix3x3(r0, r1, r2): F32Matrix3x3) -> Self {
90        Self::from_cols(r0.into(), r1.into(), r2.into()).transpose()
91    }
92}
93
94impl From<F32Matrix3x3> for glam::Mat3A {
95    #[inline]
96    fn from(F32Matrix3x3(r0, r1, r2): F32Matrix3x3) -> Self {
97        Self::from_cols(r0.into(), r1.into(), r2.into()).transpose()
98    }
99}
100
101impl From<glam::Mat4> for F32Matrix4x4 {
102    #[inline]
103    fn from(m: glam::Mat4) -> Self {
104        let t = m.transpose();
105
106        Self(
107            t.x_axis.into(),
108            t.y_axis.into(),
109            t.z_axis.into(),
110            t.w_axis.into(),
111        )
112    }
113}
114
115impl From<glam::Mat3> for F32Matrix3x3 {
116    #[inline]
117    fn from(m: glam::Mat3) -> Self {
118        let m = m.transpose();
119        Self(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
120    }
121}
122
123impl From<glam::Mat3A> for F32Matrix3x3 {
124    #[inline]
125    fn from(m: glam::Mat3A) -> Self {
126        let m = m.transpose();
127        Self(m.x_axis.into(), m.y_axis.into(), m.z_axis.into())
128    }
129}