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
|
#pragma once
#include <sstream>
#include <cstddef>
#include <iostream>
#include "Rotation.hpp"
template <size_t R, size_t C, typename T = float>
struct Matrix {
public:
Matrix<R, C, T>() : elements{} {};
Matrix<R, C, T>(T scalar) {
std::fill(elements, elements + R * C, scalar);
};
template<typename ...Args>
Matrix<R, C, T>(Args... args): elements{ args... } {};
Matrix<R, C, T>(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 c = angles.vector.map([](auto a) { return cos(a); });
auto s = angles.vector.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) {
return { &elements[index * C] };
}
Vector<R, T> col(size_t index) {
Vector<R, T> result{};
for (int i = 0; i < R; i++) {
result[i] = this->operator()(index, i);
}
return result;
}
Matrix<R, C, T> operator+(Matrix<R, C, T> other) {
Matrix<R, C, T> result{};
for (int i = 0; i < R * C; i++) {
result.elements[i] = elements[i] + other.elements[i];
}
return result;
}
Matrix<R, C, T> operator*(float scalar) {
Matrix<R, C, T> 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*(Matrix<C, N, T> other) {
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*(Vector<R, T> vector) {
Matrix<R, 1, T> matrix(vector.elements);
matrix = this->operator*(matrix);
return { matrix.elements };
}
auto& operator()(size_t x, 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];
};
|