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
|
#pragma once
#include <sstream>
#include <cstddef>
#include "Rotation.hpp"
#include "Trig.hpp"
template <size_t R, size_t C, typename T = float>
struct Matrix {
Matrix() : elements{} {}
explicit Matrix(const T scalar) {
std::fill(elements, elements + R * C, scalar);
}
template<typename ...Args, std::enable_if_t<sizeof...(Args) == R * C, int> = 0>
Matrix(Args... args): elements{ args... } {}
explicit Matrix(const T values[R * C]) {
std::copy(values, values + R * C, elements);
}
static Matrix<R, R, T> identity() {
Matrix<R, R, T> result{};
for (int i = 0; i < R; i++) {
result(i, i) = 1;
}
return result;
}
static Matrix<4, 4> transformation(Vector<3> position) {
return {
1.0f, 0.0f, 0.0f, position.x(),
0.0f, 1.0f, 0.0f, position.y(),
0.0f, 0.0f, 1.0f, position.z(),
0.0f, 0.0f, 0.0f, 1.0f
};
}
static Matrix<4, 4> rotation(Rotation angles) {
auto radians = angles.vector.map([](auto a) { return Math::radians(a); });
auto c = radians.map([](auto a) { return cos(a); });
auto s = radians.map([](auto a) { return sin(a); });
Matrix<4, 4> rotation_x{
1.0f, 0.0f, 0.0f, 0.0f,
0.0f, c.x(), -s.x(), 0.0f,
0.0f, s.x(), c.x(), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
Matrix<4, 4> rotation_y{
c.y(), 0.0f, s.y(), 0.0f,
0.0f, 1.0f, 0.0f, 0.0f,
-s.y(), 0.0f, c.y(), 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
Matrix<4, 4> rotation_z{
c.z(), -s.z(), 0.0f, 0.0f,
s.z(), c.z(), 0.0f, 0.0f,
0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f,
};
return rotation_x * rotation_y * rotation_z;
}
Vector<C, T> row(size_t index) const {
return Vector<C, T>{ &elements[index * C] };
}
Vector<R, T> col(size_t index) const {
Vector<R, T> result{};
for (int i = 0; i < R; i++) {
result[i] = this->operator()(index, i);
}
return result;
}
Matrix transpose() const {
Matrix result{};
for (int y = 0; y < R; y++) {
for (int x = 0; x < C; x++) {
result(x, y) = this->operator()(y, x);
}
}
return result;
}
Matrix operator+(const Matrix other) const {
Matrix result{};
for (int i = 0; i < R * C; i++) {
result.elements[i] = elements[i] + other.elements[i];
}
return result;
}
Matrix operator*(float scalar) {
Matrix result{};
for (int i = 0; i < R * C; i++) {
result.elements[i] = elements[i] * scalar;
}
return result;
}
template<size_t N>
Matrix<R, N, T> operator*(const Matrix<C, N, T> other) const {
Matrix<R, N, T> result{};
for (int y = 0; y < R; y++) {
for (int x = 0; x < N; x++) {
auto r = row(y);
auto c = other.col(x);
auto dot = r * c;
result(x, y) = dot;
}
}
return result;
}
Vector<R, T> operator*(const Vector<R, T> vector) const {
Matrix<R, 1, T> matrix(vector.elements);
matrix = this->operator*(matrix);
return Vector<R, T>{ matrix.elements };
}
const T& operator()(const size_t x, const size_t y) const {
return elements[y * C + x];
}
T& operator()(const size_t x, const size_t y) {
return elements[y * C + x];
}
std::string string() {
std::stringstream str{};
for (int x = 0; x < R; x++) {
for (int y = 0; y < C; y++) {
str << this->operator()(x, y) << " ";
}
str << "\n";
}
return str.str();
}
T elements[R * C];
};
|