-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
380 lines (362 loc) · 9.89 KB
/
Matrix.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/* A matrix class used for math.
* Similar to Vector<N>, this is just an std::array of vectors with the needed functionality.
*/
export module math:Matrix;
import :forward;
import :Vector;
import <array>;
import <iostream>;
import <type_traits>;
import <cstddef>;
import <cmath>;
export namespace math
{
template<std::size_t M, std::size_t N>
class Matrix
{
// Vector<N> is treated as a row vector here.
std::array<Vector<N>, M> rows;
public:
constexpr Matrix() = default;
explicit constexpr Matrix(const float entry)
{
rows.fill(Vector<N>(entry));
}
constexpr Matrix(const std::array<Vector<N>, M>& rows) : rows(rows) {}
constexpr Matrix(const std::array<std::array<float, N>, M>& rows)
{
for (std::size_t i = 0; i < M; i++)
{
this->rows[i] = rows[i];
}
}
constexpr Matrix(const std::array<float, M * N>& entries)
{
for (std::size_t i = 0; i < M; i++)
{
for (std::size_t j = 0; j < N; j++)
{
rows[i][j] = entries[i * N + j];
}
}
}
template<typename... T, std::enable_if_t<
std::conjunction_v<std::is_same<T, Vector<N>>...> && sizeof...(T) == M
>* = nullptr>
constexpr Matrix(const T&... rows) : rows({rows...}) {}
template<typename... T, std::enable_if_t<
std::conjunction_v<std::is_same<T, std::array<float, N>>...> && sizeof...(T) == M
>* = nullptr>
constexpr Matrix(const T&... rows) :
Matrix(std::array<std::array<float, N>, M>({rows...})) {}
template<typename... T, std::enable_if_t<
std::conjunction_v<std::is_same<T, float>...> && sizeof...(T) == M * N
>* = nullptr>
constexpr Matrix(const T... entries) : Matrix(std::array<float, M * N>({entries...})) {}
constexpr Vector<N>& operator[](const std::size_t i)
{
return rows[i];
}
constexpr const Vector<N>& operator[](const std::size_t i) const
{
return rows[i];
}
constexpr Vector<M> getColumn(const std::size_t i) const
{
Vector<M> column;
for (std::size_t j = 0; j < M; j++)
{
column[j] = rows[j][i];
}
return column;
}
constexpr Matrix<M, N>& operator+=(const Matrix<M, N>& rhs)
{
for (std::size_t i = 0; i < M; i++)
{
rows[i] += rhs[i];
}
return *this;
}
constexpr Matrix<M, N>& operator-=(const Matrix<M, N>& rhs)
{
for (std::size_t i = 0; i < M; i++)
{
rows[i] -= rhs[i];
}
return *this;
}
constexpr Matrix<M, N>& operator*= (const float rhs)
{
for (Vector<N>& row : rows)
{
row *= rhs;
}
return *this;
}
constexpr Matrix<M, N>& operator/= (const float rhs)
{
for (Vector<N>& row : rows)
{
row /= rhs;
}
return *this;
}
constexpr Matrix<N, M> transpose() const
{
Matrix<N, M> transpose;
for (std::size_t i = 0; i < N; i++)
{
transpose[i] = getColumn(i);
}
return transpose;
}
template<std::size_t D = N, std::enable_if_t<D == 1 && M == D>* = nullptr>
constexpr float determinant() const
{
return rows[0][0];
}
template<std::size_t D = N, std::enable_if_t<D == 2 && M == D>* = nullptr>
constexpr float determinant() const
{
return rows[0][0] * rows[1][1] - rows[0][1] * rows[1][0];
}
template<std::size_t D = N, std::enable_if_t<D == 3 && M == D>* = nullptr>
constexpr float determinant() const
{
return rows[0][0] * (rows[1][1] * rows[2][2] - rows[1][2] * rows[2][1]) +
rows[0][1] * (rows[1][2] * rows[2][0] - rows[1][0] * rows[2][2]) +
rows[0][2] * (rows[1][0] * rows[2][1] - rows[1][1] * rows[2][0]);
}
// TODO determinant in the general case
template<std::size_t D = N, std::enable_if_t<D >= 2 && M == D>* = nullptr>
constexpr float cofactor(const std::size_t row, const std::size_t col) const
{
Matrix<N - 1, N - 1> submatrix;
for (std::size_t i = 0; i < N - 1; i++)
{
for (std::size_t j = 0; j < N - 1; j++)
{
submatrix[i][j] = rows[i + static_cast<std::size_t>(i >= row)]
[j + static_cast<std::size_t>(j >= col)];
}
}
return static_cast<bool>((row + col) % 2) ?
-submatrix.determinant() : submatrix.determinant();
}
template<std::size_t D = N, std::enable_if_t<D >= 2 && M == D>* = nullptr>
constexpr Matrix<N, N> comatrix() const
{
Matrix<N, N> comatrix;
for (std::size_t i = 0; i < N; i++)
{
for (std::size_t j = 0; j < N; j++)
{
comatrix[i][j] = cofactor(i, j);
}
}
return comatrix;
}
template<std::size_t D = N, std::enable_if_t<D >= 2 && M == D>* = nullptr>
constexpr Matrix<N, N> adjugate() const
{
return comatrix().transpose();
}
template<std::size_t D = N, std::enable_if_t<D == 1 && M == D>* = nullptr>
constexpr Matrix<1, 1> inverse() const
{
return Matrix<1, 1>(1.0f / determinant());
}
template<std::size_t D = N, std::enable_if_t<D == 2 && M == D>* = nullptr>
constexpr Mat2 inverse() const
{
return Mat2(rows[1][1], -rows[0][1], -rows[1][0], rows[0][0]) / determinant();
}
template<std::size_t D = N, std::enable_if_t<D == 3 && M == D>* = nullptr>
constexpr Mat3 inverse() const
{
// https://stackoverflow.com/questions/983999/simple-3x3-matrix-inverse-code-c
return Mat3(
rows[1][1] * rows[2][2] - rows[2][1] * rows[1][2],
rows[0][2] * rows[2][1] - rows[0][1] * rows[2][2],
rows[0][1] * rows[1][2] - rows[0][2] * rows[1][1],
rows[1][2] * rows[2][0] - rows[1][0] * rows[2][2],
rows[0][0] * rows[2][2] - rows[0][2] * rows[2][0],
rows[1][0] * rows[0][2] - rows[0][0] * rows[1][2],
rows[1][0] * rows[2][1] - rows[2][0] * rows[1][1],
rows[2][0] * rows[0][1] - rows[0][0] * rows[2][1],
rows[0][0] * rows[1][1] - rows[1][0] * rows[0][1]
) / determinant();
} // TODO inverse in the general case
template<std::size_t D = N, std::enable_if_t<D >= 1 && M == D>* = nullptr>
constexpr Matrix<N, N>& invert()
{
*this = inverse();
return *this;
}
};
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator+(Matrix<M, N> lhs, const Matrix<M, N>& rhs)
{
lhs += rhs;
return lhs;
}
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator-(Matrix<M, N> lhs, const Matrix<M, N>& rhs)
{
lhs -= rhs;
return lhs;
}
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator-(Matrix<M, N> rhs)
{
for (std::size_t i = 0; i < M; i++)
{
rhs[i] = -rhs[i];
}
return rhs;
}
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator*(Matrix<M, N> lhs, const float rhs)
{
lhs *= rhs;
return lhs;
}
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator*(const float lhs, const Matrix<M, N>& rhs)
{
return rhs * lhs;
}
template<std::size_t M, std::size_t N>
constexpr Matrix<M, N> operator/(Matrix<M, N> lhs, const float rhs)
{
lhs /= rhs;
return lhs;
}
template<std::size_t M, std::size_t N, std::size_t O>
constexpr Matrix<M, O> operator*(const Matrix<M, N>& lhs, const Matrix<N, O>& rhs)
{
Matrix<M, O> product;
for (std::size_t i = 0; i < M; i++)
{
for (std::size_t j = 0; j < O; j++)
{
product[i][j] = lhs[i].dot(rhs.getColumn(j));
}
}
return product;
}
template<std::size_t M, std::size_t N>
constexpr Vector<M> operator*(const Matrix<M, N>& lhs, const Vector<N>& rhs)
{
return Vector<M>(lhs * Matrix<1, N>(rhs).transpose());
}
template<std::size_t M, std::size_t N>
constexpr Vector<N> operator*(const Vector<M>& lhs, const Matrix<M, N>& rhs)
{
return Vector<N>(Matrix<1, M>(lhs) * rhs);
}
template<std::size_t M, std::size_t N>
constexpr bool operator==(const Matrix<M, N>& lhs, const Matrix<M, N>& rhs)
{
for (std::size_t i = 0; i < M; i++)
{
if (lhs[i] != rhs[i])
{
return false;
}
}
return true;
}
template<std::size_t M, std::size_t N>
constexpr bool operator!=(const Matrix<M, N>& lhs, const Matrix<M, N>& rhs)
{
return !(lhs == rhs);
}
template<std::size_t M, std::size_t N>
std::ostream& operator<<(std::ostream& lhs, const Matrix<M, N>& rhs)
{
lhs << '[';
for (std::size_t i = 0; i + 1 < M; i++)
{
lhs << rhs[i] << ", ";
}
if (M)
{
lhs << rhs[M - 1];
}
lhs << ']';
return lhs;
}
template<std::size_t M, std::size_t N>
std::istream& operator>>(std::istream& lhs, Matrix<M, N>& rhs)
{
for (std::size_t i = 0; i < M; i++)
{
lhs >> rhs[i];
}
return lhs;
}
// Prefer to use I<N>.
template<std::size_t N>
constexpr Matrix<N, N> identity()
{
Matrix<N, N> identity = Matrix<N, N>(0.0f);
for (std::size_t i = 0; i < N; i++)
{
identity[i][i] = 1.0f;
}
return identity;
}
template<std::size_t N>
constexpr Matrix<N, N> I = identity<N>();
constexpr Mat2 I2 = I<2>;
constexpr Mat3 I3 = I<3>;
constexpr Mat4 I4 = I<4>;
// Rotation matrices about the 3D axes. Because there is a need to rotate by an arbitrary
// angle, Rx<A>, Ry<A>, and Rz<A> are not necessarily preferred.
constexpr Mat3 rotationX(const float theta)
{
return {
1.0f, 0.0f, 0.0f,
0.0f, std::cos(theta), -std::sin(theta),
0.0f, std::sin(theta), std::cos(theta)
};
}
template<float A>
constexpr Mat3 Rx = rotationX(A);
constexpr Mat3 rotationY(const float theta)
{
return {
std::cos(theta), 0.0f, std::sin(theta),
0.0f, 1.0f, 0.0f,
-std::sin(theta), 0.0f, std::cos(theta)
};
}
template<float A>
constexpr Mat3 Ry = rotationY(A);
constexpr Mat3 rotationZ(const float theta)
{
return {
std::cos(theta), -std::sin(theta), 0.0f,
std::sin(theta), std::cos(theta), 0.0f,
0.0f, 0.0f, 1.0f
};
}
template<float A>
constexpr Mat3 Rz = rotationZ(A);
// Construct the remaining axes from a single z-axis. The axis is assumed to be normalized.
inline Mat3 axesZ(const Vec3& axis) // TODO constexpr when MSVC becomes compliant with C++23
{
// https://backend.orbit.dtu.dk/ws/portalfiles/portal/126824972/onb_frisvad_jgt2012_v2.pdf
// https://graphics.pixar.com/library/OrthonormalB/paper.pdf
const float sign = std::copysign(1.0f, axis.z());
const float a = -1.0f / (sign + axis.z());
const float b = axis.x() * axis.y() * a;
return {
Vec3(1.0f + sign * axis.x() * axis.x() * a, sign * b, -sign * axis.x()),
Vec3(b, sign + axis.y() * axis.y() * a, -axis.y()),
axis
};
}
}