-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix4.h
299 lines (251 loc) · 7.08 KB
/
matrix4.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
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
#ifndef MATRIX4_H
#define MATRIX4_H
#include <cassert>
#include <cmath>
#include "cvec.h"
// Forward declaration of Matrix4 and transpose since those are used below
class Matrix4;
Matrix4 transpose(const Matrix4& m);
// A 4x4 Matrix.
// To get the element at ith row and jth column, use a(i,j)
class Matrix4 {
double d_[16]; // layout is row-major
public:
double &operator () (const int row, const int col) {
return d_[(row << 2) + col];
}
const double &operator () (const int row, const int col) const {
return d_[(row << 2) + col];
}
double& operator [] (const int i) {
return d_[i];
}
const double& operator [] (const int i) const {
return d_[i];
}
Matrix4() {
for (int i = 0; i < 16; ++i) {
d_[i] = 0;
}
for (int i = 0; i < 4; ++i) {
(*this)(i,i) = 1;
}
}
Matrix4(const double a) {
for (int i = 0; i < 16; ++i) {
d_[i] = a;
}
}
template <class T>
Matrix4& readFromColumnMajorMatrix(const T m[]) {
for (int i = 0; i < 16; ++i) {
d_[i] = m[i];
}
return *this = transpose(*this);
}
template <class T>
void writeToColumnMajorMatrix(T m[]) const {
Matrix4 t = transpose(*this);
for (int i = 0; i < 16; ++i) {
m[i] = T(t.d_[i]);
}
}
Matrix4& operator += (const Matrix4& m) {
for (int i = 0; i < 16; ++i) {
d_[i] += m.d_[i];
}
return *this;
}
Matrix4& operator -= (const Matrix4& m) {
for (int i = 0; i < 16; ++i) {
d_[i] -= m.d_[i];
}
return *this;
}
Matrix4& operator *= (const double a) {
for (int i = 0; i < 16; ++i) {
d_[i] *= a;
}
return *this;
}
Matrix4& operator *= (const Matrix4& a) {
return *this = *this * a;
}
Matrix4 operator + (const Matrix4& a) const {
return Matrix4(*this) += a;
}
Matrix4 operator - (const Matrix4& a) const {
return Matrix4(*this) -= a;
}
Matrix4 operator * (const double a) const {
return Matrix4(*this) *= a;
}
Cvec4 operator * (const Cvec4& v) const {
Cvec4 r(0);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
r[i] += (*this)(i,j) * v(j);
}
}
return r;
}
Matrix4 operator * (const Matrix4& m) const {
Matrix4 r(0);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
for (int k = 0; k < 4; ++k) {
r(i,k) += (*this)(i,j) * m(j,k);
}
}
}
return r;
}
static Matrix4 makeXRotation(const double ang) {
return makeXRotation(std::cos(ang * CS175_PI/180), std::sin(ang * CS175_PI/180));
}
static Matrix4 makeYRotation(const double ang) {
return makeYRotation(std::cos(ang * CS175_PI/180), std::sin(ang * CS175_PI/180));
}
static Matrix4 makeZRotation(const double ang) {
return makeZRotation(std::cos(ang * CS175_PI/180), std::sin(ang * CS175_PI/180));
}
static Matrix4 makeXRotation(const double c, const double s) {
Matrix4 r;
r(1,1) = r(2,2) = c;
r(1,2) = -s;
r(2,1) = s;
return r;
}
static Matrix4 makeYRotation(const double c, const double s) {
Matrix4 r;
r(0,0) = r(2,2) = c;
r(0,2) = s;
r(2,0) = -s;
return r;
}
static Matrix4 makeZRotation(const double c, const double s) {
Matrix4 r;
r(0,0) = r(1,1) = c;
r(0,1) = -s;
r(1,0) = s;
return r;
}
static Matrix4 makeTranslation(const Cvec3& t) {
Matrix4 r;
for (int i = 0; i < 3; ++i) {
r(i,3) = t[i];
}
return r;
}
static Matrix4 makeScale(const Cvec3& s) {
Matrix4 r;
for (int i = 0; i < 3; ++i) {
r(i,i) = s[i];
}
return r;
}
static Matrix4 makeProjection(
const double top, const double bottom,
const double left, const double right,
const double nearClip, const double farClip) {
Matrix4 r(0);
// 1st row
if (std::abs(right - left) > CS175_EPS) {
r(0,0) = -2.0 * nearClip / (right - left);
r(0,2) = (right+left) / (right - left);
}
// 2nd row
if (std::abs(top - bottom) > CS175_EPS) {
r(1,1) = -2.0 * nearClip / (top - bottom);
r(1,2) = (top + bottom) / (top - bottom);
}
// 3rd row
if (std::abs(farClip - nearClip) > CS175_EPS) {
r(2,2) = (farClip+nearClip) / (farClip - nearClip);
r(2,3) = -2.0 * farClip * nearClip / (farClip - nearClip);
}
r(3,2) = -1.0;
return r;
}
static Matrix4 makeProjection(const double fovy, const double aspectRatio, const double zNear, const double zFar) {
Matrix4 r(0);
const double ang = fovy * 0.5 * CS175_PI/180;
const double f = std::abs(std::sin(ang)) < CS175_EPS ? 0 : 1/std::tan(ang);
if (std::abs(aspectRatio) > CS175_EPS)
r(0,0) = f/aspectRatio; // 1st row
r(1,1) = f; // 2nd row
if (std::abs(zFar - zNear) > CS175_EPS) { // 3rd row
r(2,2) = (zFar+zNear) / (zFar - zNear);
r(2,3) = -2.0 * zFar * zNear / (zFar - zNear);
}
r(3,2) = -1.0; // 4th row
return r;
}
};
inline bool isAffine(const Matrix4& m) {
return std::abs(m[15]-1) + std::abs(m[14]) + std::abs(m[13]) + std::abs(m[12]) < CS175_EPS;
}
inline double norm2(const Matrix4& m) {
double r = 0;
for (int i = 0; i < 16; ++i) {
r += m[i]*m[i];
}
return r;
}
// computes inverse of affine matrix. assumes last row is [0,0,0,1]
inline Matrix4 inv(const Matrix4& m) {
Matrix4 r; // default constructor initializes it to identity
assert(isAffine(m));
double det = m(0,0)*(m(1,1)*m(2,2) - m(1,2)*m(2,1)) +
m(0,1)*(m(1,2)*m(2,0) - m(1,0)*m(2,2)) +
m(0,2)*(m(1,0)*m(2,1) - m(1,1)*m(2,0));
// check non-singular matrix
assert(std::abs(det) > CS175_EPS3);
// "rotation part"
r(0,0) = (m(1,1) * m(2,2) - m(1,2) * m(2,1)) / det;
r(1,0) = -(m(1,0) * m(2,2) - m(1,2) * m(2,0)) / det;
r(2,0) = (m(1,0) * m(2,1) - m(1,1) * m(2,0)) / det;
r(0,1) = -(m(0,1) * m(2,2) - m(0,2) * m(2,1)) / det;
r(1,1) = (m(0,0) * m(2,2) - m(0,2) * m(2,0)) / det;
r(2,1) = -(m(0,0) * m(2,1) - m(0,1) * m(2,0)) / det;
r(0,2) = (m(0,1) * m(1,2) - m(0,2) * m(1,1)) / det;
r(1,2) = -(m(0,0) * m(1,2) - m(0,2) * m(1,0)) / det;
r(2,2) = (m(0,0) * m(1,1) - m(0,1) * m(1,0)) / det;
// "translation part" - multiply the translation (on the left) by the inverse linear part
r(0,3) = -(m(0,3) * r(0,0) + m(1,3) * r(0,1) + m(2,3) * r(0,2));
r(1,3) = -(m(0,3) * r(1,0) + m(1,3) * r(1,1) + m(2,3) * r(1,2));
r(2,3) = -(m(0,3) * r(2,0) + m(1,3) * r(2,1) + m(2,3) * r(2,2));
assert(isAffine(r) && norm2(Matrix4() - m*r) < CS175_EPS2);
return r;
}
inline Matrix4 transpose(const Matrix4& m) {
Matrix4 r(0);
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
r(i,j) = m(j,i);
}
}
return r;
}
inline Matrix4 normalMatrix(const Matrix4& m) {
Matrix4 invm = inv(m);
invm(0, 3) = invm(1, 3) = invm(2, 3) = 0;
return transpose(invm);
}
inline Matrix4 transFact(const Matrix4& m) {
Matrix4 r;
for (int i = 0; i < 3; i++) {
r(i,3) = m(i,3);
}
return r;
}
inline Matrix4 linFact(const Matrix4& m) {
Matrix4 r;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
r(i,j) = m(i,j) ;
}
}
return r;
}
#endif