dl
dl_matrix.h
浏览该文件的文档.
1
20#pragma once
21
22#include <array>
23#include "dl_type.h"
24#include "dl_math.h"
25#include "base/dl_string.h"
26
27namespace dl
28{
29
30template<typename T, std::size_t M, std::size_t N>
31class Matrix
32{
33public:
34 using Row = std::array<T, N>;//一行的长度为N列
35 using Col = std::array<T, M>;//一列的长度为M行
36 static constexpr size_t MN = M * N;
37 Matrix() {}
38
48 Matrix(const std::array<Row, M>& mul_fow)
49 {
50 for (size_t j = 0; j < N; ++j)
51 {
52 for (size_t i = 0; i < M; ++i)
53 {
54 _col[j][i] = mul_fow[i][j];
55 }
56 }
57 }
58
61 Matrix(const T* p)
62 {
63 for (size_t i = 0; i < MN; ++i)
64 _d[i] = *(p + i);
65 }
66
70 Col& operator[](size_t j)
71 {
72 assert(j < N);
73 return _col[j];
74 }
75
78 const Col& operator[](size_t j) const
79 {
80 assert(j < N);
81 return _col[j];
82 }
83
89 {
90 for (size_t k = 0; k < MN; ++k)
91 _d[k] += b._d[k];
92 }
93
97 Matrix& operator*=(const T& v)
98 {
99 for (size_t k = 0; k < MN; ++k)
100 _d[k] *= v;
101 return *this;
102 }
103
111 {
112 static_assert(M == N);
113 for (size_t i = 0; i < M; ++i)
114 {//计算一行后再覆盖(避免提前覆盖旧值)
115 std::array<T, N> row{};
116 for (size_t j = 0; j < N; ++j)
117 {
118 for (size_t k = 0; k < N; ++k)
119 row[j] += _col[k][i] * b[j][k];
120 }
121 for (size_t j = 0; j < N; ++j)
122 _col[j][i] = row[j];
123 }
124 return *this;
125 }
126
130// Float GetDet()
131// {
132// static_dnd_assert(M == N);
133// if (M == 1)
134// return _d[0];
135// Float ret = 0;
136// //生成代数余子式 共M个,单个大小为M-1
137// Matrix<T, M - 1, N - 1> m;//假装它是行列式
138// for (size_t k = 0; k < M; ++k)
139// {//以第一列生成
140// for (size_t j = 0; j < M; ++j)
141// for (size_t j = 0; j < M; ++j)
142// }
143//
145// return *this;
146// }
150 //Matrix GetInverse()
151 //{
152 // //TODO
153 // static_dnd_assert(M == N);
154 // return *this;
155 //}
156
160 void Identity()
161 {
162 static_assert(M == N);
163 for (size_t j = 0; j < N; ++j)
164 {
165 for (size_t i = 0; i < M; ++i)
166 {
167 if (i == j)
168 _col[j][i] = 1_f;
169 else
170 _col[j][i] = 0;
171 }
172 }
173 }
174
178 {
179 static_assert(M == N);
180 for (size_t j = 0; j < N; ++j)
181 {
182 for (size_t i = 0; i < j; ++i)
183 {
184 swap(_col[j][i], _col[i][j]);
185 }
186 }
187 }
188
189 void Dump()
190 {
191 log_("{:=^32}", fmt::format("Matrix: {}x{}", M, N));
192
193 //所有元素
195 for (size_t i = 0; i < M; ++i)
196 {
197 for (size_t j = 0; j < N; ++j)
198 {
199 str_wh[i][j] = fmt::format("{}", _col[j][i]);
200 }
201 }
202 //合并为M行
203 std::array<std::string, M> str_fow = String::FormatString2(str_wh);
204 for (size_t i = 0; i < M; ++i)
205 log(str_fow[i]);
206
207 std::string str = "Memory: ";
208 for (size_t k = 0; k < MN; ++k)
209 {
210 str += fmt::format("{} ", _d[k]);
211 }
212 log(str);
213 }
214
215 const T& At(size_t i) const
216 {
217 return _d[i];
218 }
219 T& At(size_t i)
220 {
221 return _d[i];
222 }
223private:
224 union
225 {
226 Col _col[N];//共N列
228 };
229};
230
232
233
235template<typename T, size_t M, size_t N>
237{
238 for (size_t k = 0; k < Matrix<T, M, N>::MN; ++k)
239 {
240 if (a.At(k) != b.At(k))
241 return false;
242 }
243 return true;
244}
245
250template<typename T, size_t M, size_t N, size_t P>
252{
253 Matrix<T, M, N> ret;
254 for (size_t j = 0; j < N; ++j)
255 {//按列写入
256 for (size_t i = 0; i < M; ++i)
257 {
258 ret[j][i] = 0;
259 for (size_t k = 0; k < P; ++k)
260 ret[j][i] += a[k][i] * b[j][k];
261 }
262 }
263 return ret;
264}
265
271template<typename T, size_t M, size_t N>
272std::array<T, M> operator*(const Matrix<T, M, N>& a, const std::array<T, N>& v)
273{
274 std::array<T, M> ret{};
275 for (size_t i = 0; i < M; ++i)
276 {
277 for (size_t j = 0; j < N; ++j)
278 ret[i] += a[j][i] * v[j];
279 }
280 return ret;
281}
282
283namespace Mat
284{
289{
290 Matrix4 m;
291 m.Identity();
292 return m;
293}
294
299
304
309
314
315
320
324Matrix4 CreateLookTo(const Position3& eye, const Position3& dir, const Position3& up);
325
329Matrix4 CreatePerspective(Float fovy, Float aspect, Float z_near, Float z_far);
333Matrix4 CreateOrthogonal(Float left, Float top, Float right, Float bottom, Float z_near, Float z_far);
334
341
345void Test();
346
347}
348
349
350}
351
352//复制使用
353
354//Matrix4 m;
355//m[0][0] = 1; m[1][0] = 0; m[2][0] = 0; m[3][0] = t[0];
356//m[0][1] = 0; m[1][1] = 1; m[2][1] = 0; m[3][1] = t[1];
357//m[0][2] = 0; m[1][2] = 0; m[2][2] = 1; m[3][2] = t[2];
358//m[0][3] = 0; m[1][3] = 0; m[2][3] = 0; m[3][3] = 1;
359
Matrix & operator*=(const Matrix &b)
矩阵 乘 矩阵
const Col & operator[](size_t j) const
返回第j列
Matrix(const T *p)
传入地址构造,注意矩阵存储方式
Matrix & operator+=(const Matrix &b)
矩阵 加 矩阵
T & At(size_t i)
Col & operator[](size_t j)
返回第j列
Matrix & operator*=(const T &v)
矩阵 乘 值
std::array< T, N > Row
const T & At(size_t i) const
std::array< T, M > Col
Matrix(const std::array< Row, M > &mul_fow)
初始化
static constexpr size_t MN
void Identity()
获得行列式的值,Determinant(A)
void Transpose()
转置(M不等于N时还需另外实现)
#define log_(...)
数学相关的通用算法
字符串相关
通用类型
Matrix4 CreateRotation(const Position3 &axis, Float angle)
创建旋转矩阵
void Test()
验证正确性
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::array< Float, 3 > Position3
std::array< Float, 4 > Position4
float Float
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)
判断相等
Matrix< Float, 4, 4 > Matrix4
void log(std::string_view str, const std::source_location location=std::source_location::current())
以当前设置输出