30template<
typename T, std::
size_t M, std::
size_t N>
34 using Row = std::array<T, N>;
35 using Col = std::array<T, M>;
36 static constexpr size_t MN =
M *
N;
48 Matrix(
const std::array<Row, M>& mul_fow)
50 for (
size_t j = 0; j <
N; ++j)
52 for (
size_t i = 0; i <
M; ++i)
54 _col[j][i] = mul_fow[i][j];
63 for (
size_t i = 0; i <
MN; ++i)
90 for (
size_t k = 0; k <
MN; ++k)
99 for (
size_t k = 0; k <
MN; ++k)
112 static_assert(
M ==
N);
113 for (
size_t i = 0; i <
M; ++i)
115 std::array<T, N> row{};
116 for (
size_t j = 0; j <
N; ++j)
118 for (
size_t k = 0; k <
N; ++k)
119 row[j] +=
_col[k][i] * b[j][k];
121 for (
size_t j = 0; j <
N; ++j)
162 static_assert(
M ==
N);
163 for (
size_t j = 0; j <
N; ++j)
165 for (
size_t i = 0; i <
M; ++i)
179 static_assert(
M ==
N);
180 for (
size_t j = 0; j <
N; ++j)
182 for (
size_t i = 0; i < j; ++i)
191 log_(
"{:=^32}", fmt::format(
"Matrix: {}x{}",
M,
N));
194 std::array<std::array<std::string, N>,
M> str_wh;
195 for (
size_t i = 0; i <
M; ++i)
197 for (
size_t j = 0; j <
N; ++j)
199 str_wh[i][j] = fmt::format(
"{}",
_col[j][i]);
204 for (
size_t i = 0; i <
M; ++i)
207 std::string str =
"Memory: ";
208 for (
size_t k = 0; k <
MN; ++k)
210 str += fmt::format(
"{} ",
_d[k]);
215 const T&
At(
size_t i)
const
231template<
typename T,
size_t M,
size_t N>
234 for (
size_t k = 0; k < Matrix<T, M, N>::MN; ++k)
236 if (a.
At(k) != b.
At(k))
246template<
typename T,
size_t M,
size_t N,
size_t P>
250 for (
size_t j = 0; j <
N; ++j)
252 for (
size_t i = 0; i <
M; ++i)
255 for (
size_t k = 0; k <
P; ++k)
256 ret[j][i] += a[k][i] * b[j][k];
267template<
typename T,
size_t M,
size_t N>
270 std::array<T, M> ret{};
271 for (
size_t i = 0; i <
M; ++i)
273 for (
size_t j = 0; j <
N; ++j)
274 ret[i] += a[j][i] * v[j];
Matrix & operator*=(const Matrix &b)
矩阵 乘 矩阵
const Col & operator[](size_t j) const
返回第j列
Matrix(const T *p)
传入地址构造,注意矩阵存储方式
Matrix & operator+=(const Matrix &b)
矩阵 加 矩阵
Col & operator[](size_t j)
返回第j列
Matrix & operator*=(const T &v)
矩阵 乘 值
const T & At(size_t i) const
Matrix(const std::array< Row, M > &mul_fow)
初始化
static constexpr size_t MN
void Identity()
获得行列式的值,Determinant(A)
void Transpose()
转置(M不等于N时还需另外实现)
Matrix4 CreateRotation(const Position3 &axis, Float angle)
创建旋转矩阵
Matrix4 CreatePerspective(Float fovy, Float aspect, Float z_near, Float z_far)
创建透视投影矩阵 [0, 1]
Matrix4 CreateTranslation(const Position3 &t)
创建平移矩阵
void ApplyTranslation(Matrix4 &m, const Position3 &t)
平移矩阵
Matrix4 GetInverse(const Matrix4 &m)
获得逆矩阵
Matrix4 CreateOrthogonal(Float left, Float top, Float right, Float bottom, Float z_near, Float z_far)
创建正交投影矩阵 [0, 1]
Matrix4 CreateLookTo(const Position3 &eye, const Position3 &dir, const Position3 &up)
创建LookTo矩阵
Matrix4 CreateScaling(const Position3 &s)
创建缩放矩阵
Matrix4 CreateIdentity()
创建平移矩阵
std::string to_string(const SpriteID &id)
constexpr std::array< T, N > operator*(const std::array< T, N > &a, const std::array< T, N > &b)
bool operator==(const Matrix< T, M, N > &a, const Matrix< T, M, N > &b)
判断相等
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出
std::array< Float, 3 > Position3
std::array< Float, 4 > Position4