-
Notifications
You must be signed in to change notification settings - Fork 0
/
tmatrix4x4.h
383 lines (336 loc) · 13.8 KB
/
tmatrix4x4.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
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
381
382
383
#ifndef TMATRIX4X4_H
#define TMATRIX4X4_H
#include "tvector3d.h"
#include "tvector4d.h"
class TMatrix4x4 {
public:
// Constructores
inline TMatrix4x4();
inline TMatrix4x4(float m11, float m12, float m13, float m14, float m21,
float m22, float m23, float m24, float m31, float m32,
float m33, float m34, float m41, float m42, float m43,
float m44);
inline explicit TMatrix4x4(const float *values);
inline TMatrix4x4(const float *values, int cols, int rows);
// Operadores
inline const float &operator()(int column, int row) const;
inline float &operator()(int row, int column);
inline TMatrix4x4 &operator+=(const TMatrix4x4 &other);
inline TMatrix4x4 &operator-=(const TMatrix4x4 &other);
inline TMatrix4x4 &operator*=(const TMatrix4x4 &other);
inline TMatrix4x4 &operator*=(float m);
inline TMatrix4x4 &operator/=(float m);
inline bool operator==(const TMatrix4x4 &matrix) const;
inline bool operator!=(const TMatrix4x4 &matrix) const;
friend TMatrix4x4 operator+(const TMatrix4x4 &m1, const TMatrix4x4 &m2);
friend TMatrix4x4 operator-(const TMatrix4x4 &m1, const TMatrix4x4 &m2);
friend TMatrix4x4 operator*(const TMatrix4x4 &m1, const TMatrix4x4 &m2);
friend TVector3D operator*(const TVector3D &vector, const TMatrix4x4 &matrix);
friend TVector3D operator*(const TMatrix4x4 &matrix, const TVector3D &vector);
friend TVector4D operator*(const TVector4D &vector, const TMatrix4x4 &matrix);
friend TVector4D operator*(const TMatrix4x4 &matrix, const TVector4D &vector);
friend TPoint operator*(const TPoint &point, const TMatrix4x4 &matrix);
friend TMatrix4x4 operator-(const TMatrix4x4 &matrix);
friend TPoint operator*(const TMatrix4x4 &matrix, const TPoint &point);
friend TMatrix4x4 operator*(float m, const TMatrix4x4 &matrix);
friend TMatrix4x4 operator*(const TMatrix4x4 &matrix, float m);
friend TMatrix4x4 operator/(const TMatrix4x4 &matrix, float m);
// Métodos
bool isAffine() const;
bool isIdentity() const;
void setToIdentity();
void fill(float value);
double determinant() const;
TMatrix4x4 inverted(bool *invertible = nullptr) const;
TMatrix4x4 transposed() const;
TVector4D column(int index) const;
void setColumn(int index, const TVector4D &value);
TVector4D row(int index) const;
void setRow(int index, const TVector4D &value);
// Escalar
void scale(const TVector3D &vector);
void scale(float x, float y);
void scale(float x, float y, float z);
void scale(float m);
// Trasladar
void translate(const TVector3D &vector);
void translate(float x, float y);
void translate(float x, float y, float z);
// Rotar
void rotateX(float angle, const TVector3D &vector);
void rotateY(float angle, const TVector3D &vector);
void rotateZ(float angle, const TVector3D &vector);
void viewport(const TPoint &p1, const TPoint &p2);
inline float *data() { return *m; };
inline const float *data() const { return *m; }
inline const float *constData() const { return *m; }
private:
float m[4][4];
explicit TMatrix4x4(int) {}
friend class TVector3D;
friend class TVector4D;
};
inline TMatrix4x4::TMatrix4x4(){};
inline TMatrix4x4::TMatrix4x4(float m11, float m12, float m13, float m14,
float m21, float m22, float m23, float m24,
float m31, float m32, float m33, float m34,
float m41, float m42, float m43, float m44) {
m[0][0] = m11;
m[0][1] = m21;
m[0][2] = m31;
m[0][3] = m41;
m[1][0] = m12;
m[1][1] = m22;
m[1][2] = m32;
m[1][3] = m42;
m[2][0] = m13;
m[2][1] = m23;
m[2][2] = m33;
m[2][3] = m43;
m[3][0] = m14;
m[3][1] = m24;
m[3][2] = m34;
m[3][3] = m44;
}
inline const float &TMatrix4x4::operator()(int column, int row) const {
if (row >= 0 && row < 4 && column >= 0 && column < 4)
return m[column][row];
}
inline float &TMatrix4x4::operator()(int column, int row) {
if (row >= 0 && row < 4 && column >= 0 && column < 4)
return m[column][row];
}
inline TMatrix4x4 &TMatrix4x4::operator+=(const TMatrix4x4 &other) {
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m[j][i] += other.m[j][i];
return *this;
}
inline TMatrix4x4 &TMatrix4x4::operator-=(const TMatrix4x4 &other) {
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m[j][i] -= other.m[j][i];
return *this;
}
inline TMatrix4x4 &TMatrix4x4::operator*=(const TMatrix4x4 &other) {
float m0, m1, m2;
m0 = m[0][0] * other.m[0][0] + m[1][0] * other.m[0][1] +
m[2][0] * other.m[0][2] + m[3][0] * other.m[0][3];
m1 = m[0][0] * other.m[1][0] + m[1][0] * other.m[1][1] +
m[2][0] * other.m[1][2] + m[3][0] * other.m[1][3];
m2 = m[0][0] * other.m[2][0] + m[1][0] * other.m[2][1] +
m[2][0] * other.m[2][2] + m[3][0] * other.m[2][3];
m[3][0] = m[0][0] * other.m[3][0] + m[1][0] * other.m[3][1] +
m[2][0] * other.m[3][2] + m[3][0] * other.m[3][3];
m[0][0] = m0;
m[1][0] = m1;
m[2][0] = m2;
m0 = m[0][1] * other.m[0][0] + m[1][1] * other.m[0][1] +
m[2][1] * other.m[0][2] + m[3][1] * other.m[0][3];
m1 = m[0][1] * other.m[1][0] + m[1][1] * other.m[1][1] +
m[2][1] * other.m[1][2] + m[3][1] * other.m[1][3];
m2 = m[0][1] * other.m[2][0] + m[1][1] * other.m[2][1] +
m[2][1] * other.m[2][2] + m[3][1] * other.m[2][3];
m[3][1] = m[0][1] * other.m[3][0] + m[1][1] * other.m[3][1] +
m[2][1] * other.m[3][2] + m[3][1] * other.m[3][3];
m[0][1] = m0;
m[1][1] = m1;
m[2][1] = m2;
m0 = m[0][2] * other.m[0][0] + m[1][2] * other.m[0][1] +
m[2][2] * other.m[0][2] + m[3][2] * other.m[0][3];
m1 = m[0][2] * other.m[1][0] + m[1][2] * other.m[1][1] +
m[2][2] * other.m[1][2] + m[3][2] * other.m[1][3];
m2 = m[0][2] * other.m[2][0] + m[1][2] * other.m[2][1] +
m[2][2] * other.m[2][2] + m[3][2] * other.m[2][3];
m[3][2] = m[0][2] * other.m[3][0] + m[1][2] * other.m[3][1] +
m[2][2] * other.m[3][2] + m[3][2] * other.m[3][3];
m[0][2] = m0;
m[1][2] = m1;
m[2][2] = m2;
m0 = m[0][3] * other.m[0][0] + m[1][3] * other.m[0][1] +
m[2][3] * other.m[0][2] + m[3][3] * other.m[0][3];
m1 = m[0][3] * other.m[1][0] + m[1][3] * other.m[1][1] +
m[2][3] * other.m[1][2] + m[3][3] * other.m[1][3];
m2 = m[0][3] * other.m[2][0] + m[1][3] * other.m[2][1] +
m[2][3] * other.m[2][2] + m[3][3] * other.m[2][3];
m[3][3] = m[0][3] * other.m[3][0] + m[1][3] * other.m[3][1] +
m[2][3] * other.m[3][2] + m[3][3] * other.m[3][3];
m[0][3] = m0;
m[1][3] = m1;
m[2][3] = m2;
return *this;
}
inline TMatrix4x4 &TMatrix4x4::operator*=(float value) {
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m[j][i] *= value;
return *this;
}
inline bool TMatrix4x4::operator==(const TMatrix4x4 &other) const {
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
if (m[j][i] != other.m[j][i])
return false;
return true;
}
inline bool TMatrix4x4::operator!=(const TMatrix4x4 &other) const {
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
if (m[j][i] == other.m[j][i])
return false;
return true;
}
inline TMatrix4x4 operator+(const TMatrix4x4 &m1, const TMatrix4x4 &m2) {
TMatrix4x4 m(1);
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m.m[j][i] = m1.m[j][i] + m2.m[j][i];
return m;
}
inline TMatrix4x4 operator-(const TMatrix4x4 &m1, const TMatrix4x4 &m2) {
TMatrix4x4 m(1);
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m.m[j][i] = m1.m[j][i] - m2.m[j][i];
return m;
}
inline TMatrix4x4 operator*(const TMatrix4x4 &m1, const TMatrix4x4 &m2) {
TMatrix4x4 m(1);
m.m[0][0] = m1.m[0][0] * m2.m[0][0] + m1.m[1][0] * m2.m[0][1] +
m1.m[2][0] * m2.m[0][2] + m1.m[3][0] * m2.m[0][3];
m.m[0][1] = m1.m[0][1] * m2.m[0][0] + m1.m[1][1] * m2.m[0][1] +
m1.m[2][1] * m2.m[0][2] + m1.m[3][1] * m2.m[0][3];
m.m[0][2] = m1.m[0][2] * m2.m[0][0] + m1.m[1][2] * m2.m[0][1] +
m1.m[2][2] * m2.m[0][2] + m1.m[3][2] * m2.m[0][3];
m.m[0][3] = m1.m[0][3] * m2.m[0][0] + m1.m[1][3] * m2.m[0][1] +
m1.m[2][3] * m2.m[0][2] + m1.m[3][3] * m2.m[0][3];
m.m[1][0] = m1.m[0][0] * m2.m[1][0] + m1.m[1][0] * m2.m[1][1] +
m1.m[2][0] * m2.m[1][2] + m1.m[3][0] * m2.m[1][3];
m.m[1][1] = m1.m[0][1] * m2.m[1][0] + m1.m[1][1] * m2.m[1][1] +
m1.m[2][1] * m2.m[1][2] + m1.m[3][1] * m2.m[1][3];
m.m[1][2] = m1.m[0][2] * m2.m[1][0] + m1.m[1][2] * m2.m[1][1] +
m1.m[2][2] * m2.m[1][2] + m1.m[3][2] * m2.m[1][3];
m.m[1][3] = m1.m[0][3] * m2.m[1][0] + m1.m[1][3] * m2.m[1][1] +
m1.m[2][3] * m2.m[1][2] + m1.m[3][3] * m2.m[1][3];
m.m[2][0] = m1.m[0][0] * m2.m[2][0] + m1.m[1][0] * m2.m[2][1] +
m1.m[2][0] * m2.m[2][2] + m1.m[3][0] * m2.m[2][3];
m.m[2][1] = m1.m[0][1] * m2.m[2][0] + m1.m[1][1] * m2.m[2][1] +
m1.m[2][1] * m2.m[2][2] + m1.m[3][1] * m2.m[2][3];
m.m[2][2] = m1.m[0][2] * m2.m[2][0] + m1.m[1][2] * m2.m[2][1] +
m1.m[2][2] * m2.m[2][2] + m1.m[3][2] * m2.m[2][3];
m.m[2][3] = m1.m[0][3] * m2.m[2][0] + m1.m[1][3] * m2.m[2][1] +
m1.m[2][3] * m2.m[2][2] + m1.m[3][3] * m2.m[2][3];
m.m[3][0] = m1.m[0][0] * m2.m[3][0] + m1.m[1][0] * m2.m[3][1] +
m1.m[2][0] * m2.m[3][2] + m1.m[3][0] * m2.m[3][3];
m.m[3][1] = m1.m[0][1] * m2.m[3][0] + m1.m[1][1] * m2.m[3][1] +
m1.m[2][1] * m2.m[3][2] + m1.m[3][1] * m2.m[3][3];
m.m[3][2] = m1.m[0][2] * m2.m[3][0] + m1.m[1][2] * m2.m[3][1] +
m1.m[2][2] * m2.m[3][2] + m1.m[3][2] * m2.m[3][3];
m.m[3][3] = m1.m[0][3] * m2.m[3][0] + m1.m[1][3] * m2.m[3][1] +
m1.m[2][3] * m2.m[3][2] + m1.m[3][3] * m2.m[3][3];
return m;
}
inline TVector3D operator*(const TVector3D &vector, const TMatrix4x4 &matrix) {
float x, y, z, w;
x = vector.x * matrix.m[0][0] + vector.y * matrix.m[0][1] +
vector.z * matrix.m[0][2] + matrix.m[0][3];
y = vector.x * matrix.m[1][0] + vector.y * matrix.m[1][1] +
vector.z * matrix.m[1][2] + matrix.m[1][3];
z = vector.x * matrix.m[2][0] + vector.y * matrix.m[2][1] +
vector.z * matrix.m[2][2] + matrix.m[2][3];
w = vector.x * matrix.m[3][0] + vector.y * matrix.m[3][1] +
vector.z * matrix.m[3][2] + matrix.m[3][3];
if (w == 1.0f)
return TVector3D(x, y, z);
else
return TVector3D(x / w, y / w, z / w);
}
inline TVector3D operator*(const TMatrix4x4 &matrix, const TVector3D &vector) {
float x, y, z, w;
x = vector.x * matrix.m[0][0] + vector.y * matrix.m[1][0] +
vector.z * matrix.m[2][0] + matrix.m[3][0];
y = vector.x * matrix.m[0][1] + vector.y * matrix.m[1][1] +
vector.z * matrix.m[2][1] + matrix.m[3][1];
z = vector.z * matrix.m[0][2] + vector.y * matrix.m[1][2] +
vector.z * matrix.m[2][2] + matrix.m[3][2];
w = vector.x * matrix.m[0][3] + vector.y * matrix.m[1][3] +
vector.z * matrix.m[2][3] + matrix.m[3][3];
if (w == 1.0f)
return TVector3D(x, y, z);
else
return TVector3D(x / w, y / w, z / w);
}
inline TVector4D operator*(const TVector4D &vector, const TMatrix4x4 &matrix) {
float x, y, z, w;
x = vector.x * matrix.m[0][0] + vector.y * matrix.m[0][1] +
vector.z * matrix.m[0][2] + vector.w * matrix.m[0][3];
y = vector.x * matrix.m[1][0] + vector.y * matrix.m[1][1] +
vector.z * matrix.m[1][2] + vector.w * matrix.m[1][3];
z = vector.x * matrix.m[2][0] + vector.y * matrix.m[2][1] +
vector.z * matrix.m[2][2] + vector.w * matrix.m[2][3];
w = vector.x * matrix.m[3][0] + vector.y * matrix.m[3][1] +
vector.z * matrix.m[3][2] + vector.w * matrix.m[3][3];
return TVector4D(x, y, z, w);
}
inline TVector4D operator*(const TMatrix4x4 &matrix, const TVector4D &vector) {
float x, y, z, w;
x = vector.x * matrix.m[0][0] + vector.y * matrix.m[1][0] +
vector.z * matrix.m[2][0] + vector.w * matrix.m[3][0];
y = vector.x * matrix.m[0][1] + vector.y * matrix.m[1][1] +
vector.z * matrix.m[2][1] + vector.w * matrix.m[3][1];
z = vector.x * matrix.m[0][2] + vector.y * matrix.m[1][2] +
vector.z * matrix.m[2][2] + vector.w * matrix.m[3][2];
w = vector.x * matrix.m[0][3] + vector.y * matrix.m[1][3] +
vector.z * matrix.m[2][3] + vector.w * matrix.m[3][3];
return TVector4D(x, y, z, w);
}
inline TPoint operator*(const TPoint &point, const TMatrix4x4 &matrix) {
float xin, yin;
float x, y, w;
xin = point.x;
yin = point.y;
x = xin * matrix.m[0][0] + yin * matrix.m[0][1] + matrix.m[0][3];
y = xin * matrix.m[1][0] + yin * matrix.m[1][1] + matrix.m[1][3];
w = xin * matrix.m[3][0] + yin * matrix.m[3][1] + matrix.m[3][3];
if (w == 1.0f)
return TPoint(static_cast<int>(round(x)), static_cast<int>(round(y)));
else
return TPoint(static_cast<int>(round(x / w)),
static_cast<int>(round(y / w)));
}
inline TPoint operator*(const TMatrix4x4 &matrix, const TPoint &point) {
float xin, yin;
float x, y, w;
xin = point.x;
yin = point.y;
x = xin * matrix.m[0][0] + yin * matrix.m[1][0] + matrix.m[3][0];
y = xin * matrix.m[0][1] + yin * matrix.m[1][1] + matrix.m[3][1];
w = xin * matrix.m[0][3] + yin * matrix.m[1][3] + matrix.m[3][3];
if (w == 1.0f)
return TPoint(static_cast<int>(round(x)), static_cast<int>(round(y)));
else
return TPoint(static_cast<int>(round(x / w)),
static_cast<int>(round(y / w)));
}
inline TMatrix4x4 operator-(const TMatrix4x4 &matrix) {
TMatrix4x4 m(1);
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m.m[j][i] = -matrix.m[j][i];
return m;
}
inline TMatrix4x4 operator*(float factor, const TMatrix4x4 &matrix) {
TMatrix4x4 m(1);
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m.m[j][i] = matrix.m[j][i] * factor;
return m;
}
inline TMatrix4x4 operator*(const TMatrix4x4 &matrix, float factor) {
TMatrix4x4 m(1);
for (auto j = 0; j < 4; j++)
for (auto i = 0; i < 4; i++)
m.m[j][i] = matrix.m[j][i] * factor;
return m;
}
#endif // TMATRIX4X4_H