-
Notifications
You must be signed in to change notification settings - Fork 0
/
mat.h
271 lines (250 loc) · 7.26 KB
/
mat.h
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
// Copyright © 2018-2020 Artyom Tokarev. All rights reserved.
// Licensed under the MIT License.
#pragma once
#include "vec.h"
namespace spt {
using default_value_type = double;
template <std::size_t N, typename ValueType = default_value_type>
struct mat;
template <std::size_t N> using mati = mat<N, std::int64_t>;
template <std::size_t N> using matf = mat<N, float>;
template <std::size_t N> using matd = mat<N, double>;
template <typename ValueType>
using mat2 = mat<2, ValueType>;
using mat2i = mati<2>;
using mat2f = matf<2>;
using mat2d = matd<2>;
template <typename ValueType>
using mat3 = mat<3, ValueType>;
using mat3i = mati<3>;
using mat3f = matf<3>;
using mat3d = matd<3>;
template <typename ValueType>
using mat4 = mat<4, ValueType>;
using mat4i = mati<4>;
using mat4f = matf<4>;
using mat4d = matd<4>;
template <typename ValueType>
struct mat<3, ValueType> {
static constexpr std::size_t n = 3;
using value_type = ValueType;
using line_type = spt::vec<3, value_type>;
std::array<line_type, 3> x;
static mat identity() {
return {
line_type({
static_cast<value_type>(1),
static_cast<value_type>(0),
static_cast<value_type>(0) }),
line_type({
static_cast<value_type>(0),
static_cast<value_type>(1),
static_cast<value_type>(0) }),
line_type({
static_cast<value_type>(0),
static_cast<value_type>(0),
static_cast<value_type>(1) }) };
}
mat& transpose() const {
std::swap(x[0][1], x[1][0]);
std::swap(x[0][2], x[2][0]);
std::swap(x[1][2], x[2][1]);
return *this;
}
mat transposed() const {
return {
x[0][0], x[1][0], x[2][0],
x[0][1], x[1][1], x[2][1],
x[0][2], x[1][2], x[2][2] };
}
mat inversed() const {
auto a00 = x[0][0], a01 = x[0][1], a02 = x[0][2];
auto a10 = x[1][0], a11 = x[1][1], a12 = x[1][2];
auto a20 = x[2][0], a21 = x[2][1], a22 = x[2][2];
auto b01 = a22 * a11 - a12 * a21;
auto b11 = -a22 * a10 + a12 * a20;
auto b21 = a21 * a10 - a11 * a20;
auto det = a00 * b01 + a01 * b11 + a02 * b21;
return mat{ b01, (-a22 * a01 + a02 * a21), (a12 * a01 - a02 * a11),
b11, (a22 * a00 - a02 * a20), (-a12 * a00 + a02 * a10),
b21, (-a21 * a00 + a01 * a20), (a11 * a00 - a01 * a10) } / det;
}
mat& operator=(const mat& right) {
x = right.x;
return *this;
}
mat operator-() const {
return { -x[0], -x[1], -x[2] };
}
mat operator+(const mat& right) const {
return {
x[0] + right.x[0],
x[1] + right.x[1],
x[2] + right.x[2] };
}
mat operator-(const mat& right) const {
return {
x[0] - right.x[0],
x[1] - right.x[1],
x[2] - right.x[2] };
}
mat operator*(value_type scalar) const {
return {
x[0] * scalar,
x[1] * scalar,
x[2] * scalar };
}
mat operator/(value_type scalar) const {
return {
x[0] / scalar,
x[1] / scalar,
x[2] / scalar };
}
mat& operator+=(const mat& right) {
x[0] += right.x[0];
x[1] += right.x[1];
x[2] += right.x[2];
return *this;
}
mat& operator-=(const mat& right) {
x[0] -= right.x[0];
x[1] -= right.x[1];
x[2] -= right.x[2];
return *this;
}
mat& operator*=(value_type scalar) {
x[0] *= scalar;
x[1] *= scalar;
x[2] *= scalar;
return *this;
}
mat& operator/=(value_type scalar) {
x[0] /= scalar;
x[1] /= scalar;
x[2] /= scalar;
return *this;
}
line_type& operator[](std::size_t i) {
return x[i];
}
const line_type& operator[](std::size_t i) const {
return x[i];
}
mat() {}
mat(const mat& other) {
x = other.x;
}
mat(const std::array<line_type, 3>& x)
: x{ x } {}
mat(const std::array<value_type, 9>& x) {
this->x[0] = line_type({ x[0], x[1], x[2] });
this->x[1] = line_type({ x[3], x[4], x[5] });
this->x[2] = line_type({ x[6], x[7], x[8] });
}
mat(const line_type& x0, const line_type& x1, const line_type& x2) {
x[0] = x0;
x[1] = x1;
x[2] = x2;
}
mat(value_type v0, value_type v1, value_type v2,
value_type v3, value_type v4, value_type v5,
value_type v6, value_type v7, value_type v8)
: x{ line_type({ v0, v1, v2 }), line_type({ v3, v4, v5 }), line_type({ v6, v7, v8 }) } {}
};
template <typename ValueType>
struct mat<2, ValueType> {
static constexpr std::size_t n = 2;
using value_type = ValueType;
using line_type = spt::vec<2, value_type>;
std::array<line_type, 2> x;
static mat identity() {
return {
line_type({ static_cast<value_type>(1), static_cast<value_type>(0) }),
line_type({ static_cast<value_type>(0), static_cast<value_type>(1) }) };
}
mat& transpose() const {
std::swap(x[0][1], x[1][0]);
return *this;
}
mat transposed() const {
return {
x[0][0], x[1][0],
x[0][1], x[1][1] };
}
mat inversed() const {
value_type det = x[0][0] * x[1][1] - x[0][1] * x[1][0];
return mat{ x[1][1], -x[0][1],
-x[1][0], x[0][0] } / det;
}
mat& operator=(const mat& right) {
x = right.x;
return *this;
}
mat operator-() const {
return { -x[0], -x[1] };
}
mat operator+(const mat& right) const {
return {
x[0] + right.x[0],
x[1] + right.x[1] };
}
mat operator-(const mat& right) const {
return {
x[0] - right.x[0],
x[1] - right.x[1] };
}
mat operator*(value_type scalar) const {
return {
x[0] * scalar,
x[1] * scalar };
}
mat operator/(value_type scalar) const {
return {
x[0] / scalar,
x[1] / scalar };
}
mat& operator+=(const mat& right) {
x[0] += right.x[0];
x[1] += right.x[1];
return *this;
}
mat& operator-=(const mat& right) {
x[0] -= right.x[0];
x[1] -= right.x[1];
return *this;
}
mat& operator*=(value_type scalar) {
x[0] *= scalar;
x[1] *= scalar;
return *this;
}
mat& operator/=(value_type scalar) {
x[0] /= scalar;
x[1] /= scalar;
return *this;
}
line_type& operator[](std::size_t i) {
return x[i];
}
const line_type& operator[](std::size_t i) const {
return x[i];
}
mat() {}
mat(const mat& other) {
x = other.x;
}
mat(const std::array<line_type, 2>& x)
: x{x} {}
mat(const std::array<value_type, 4>& x) {
this->x[0] = line_type({ x[0], x[1] });
this->x[1] = line_type({ x[2], x[3] });
}
mat(const line_type& x0, const line_type& x1) {
x[0] = x0;
x[1] = x1;
}
mat(value_type v0, value_type v1,
value_type v2, value_type v3)
: x{ line_type({ v0, v1 }), line_type({ v2, v3 }) } {}
};
} // namespace spt