-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix4.h
More file actions
210 lines (177 loc) · 5.5 KB
/
Matrix4.h
File metadata and controls
210 lines (177 loc) · 5.5 KB
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
#include "Vector4.h";
#include "Vector3.h"
class Matrix4 {
public:
// use 4 vector 4s to represent a 4x4 matrix
Vector4 m_matrix[4];
Matrix4() {
m_matrix[0] = Vector4(1, 0, 0, 0);
m_matrix[1] = Vector4(0, 1, 0, 0);
m_matrix[2] = Vector4(0, 0, 1, 0);
m_matrix[3] = Vector4(0, 0, 0, 1);
}
Matrix4(const Matrix4& copy) {
m_matrix[0] = copy.m_matrix[0];
m_matrix[1] = copy.m_matrix[1];
m_matrix[2] = copy.m_matrix[2];
m_matrix[3] = copy.m_matrix[3];
}
Matrix4(Vector4 v0, Vector4 v1, Vector4 v2, Vector4 v3) {
m_matrix[0] = v0;
m_matrix[1] = v1;
m_matrix[2] = v2;
m_matrix[3] = v3;
}
Matrix4(Vector3 v0, Vector3 v1, Vector3 v2) {
m_matrix[0] = Vector4(v0.getX(), v0.getY(), v0.getZ(), 0);
m_matrix[1] = Vector4(v1.getX(), v1.getY(), v1.getZ(), 0);
m_matrix[2] = Vector4(v2.getX(), v2.getY(), v2.getZ(), 0);
m_matrix[3] = Vector4(0, 0, 0, 1);
}
Vector4 getRow(int i) const {
return m_matrix[i];
}
Vector4 getColumn(int i) const {
if (i < 0 || i > 3) {
// return error
return Vector4();
}
switch (i)
{
case 0:
return Vector4(m_matrix[0].getX(), m_matrix[1].getX(), m_matrix[2].getX(), m_matrix[3].getX());
case 1:
return Vector4(m_matrix[0].getY(), m_matrix[1].getY(), m_matrix[2].getY(), m_matrix[3].getY());
case 2:
return Vector4(m_matrix[0].getZ(), m_matrix[1].getZ(), m_matrix[2].getZ(), m_matrix[3].getZ());
case 3:
return Vector4(m_matrix[0].getW(), m_matrix[1].getW(), m_matrix[2].getW(), m_matrix[3].getW());
default:
break;
}
}
void setColumn(int i, Vector4 v) {
if (i < 0 || i > 3) {
// return error
return;
}
switch (i)
{
case 0:
m_matrix[0].set(0, v.getX());
m_matrix[1].set(0, v.getY());
m_matrix[2].set(0, v.getZ());
m_matrix[3].set(0, v.getW());
break;
case 1:
m_matrix[0].set(1, v.getX());
m_matrix[1].set(1, v.getY());
m_matrix[2].set(1, v.getZ());
m_matrix[3].set(1, v.getW());
break;
case 2:
m_matrix[0].set(2, v.getX());
m_matrix[1].set(2, v.getY());
m_matrix[2].set(2, v.getZ());
m_matrix[3].set(2, v.getW());
break;
case 3:
m_matrix[0].set(3, v.getX());
m_matrix[1].set(3, v.getY());
m_matrix[2].set(3, v.getZ());
m_matrix[3].set(3, v.getW());
break;
default:
break;
}
}
Matrix4 operator * (const Matrix4& mat) const {
Matrix4 result;
// Loop through rows of the first matrix
for (int i = 0; i < 4; i++) {
// Loop through columns of the second matrix
Vector4 v = Vector4(0, 0, 0, 0);
for (int j = 0; j < 4; j++) {
// Calculate the dot product of the ith row of this matrix and the jth column of the mat
float cij = m_matrix[i].dotProduct(mat.getColumn(j));
v.set(j, cij);
};
result.m_matrix[i] = v;
}
return result;
}
Vector4 operator * (const Vector4& vec) const {
Vector4 result;
// Loop through rows of the matrix
for (int i = 0; i < 4; i++) {
// Compute dot product of the ith row of the matrix and the vector
result.set(i, m_matrix[i].dotProduct(vec));
}
return result;
}
Matrix4 removeTranslation() {
Matrix4 result = *this;
result.setColumn(3, Vector4(0, 0, 0, 1));
return result;
}
Matrix4 removeScaling() {
//Remove any scaling(normalize columns)
Matrix4 result = *this;
Vector4 v0 = result.getColumn(0);
Vector4 v1 = result.getColumn(1);
Vector4 v2 = result.getColumn(2);
//Vector4 v3 = result.getColumn(3);
v0 = v0.normalize();
v1 = v1.normalize();
v2 = v2.normalize();
//v3 = v3.normalize();
result.setColumn(0, v0);
result.setColumn(1, v1);
result.setColumn(2, v2);
//result.setColumn(3, v3);
return result;
}
Vector4 multVec4 (const Vector4& vec) const {
Vector4 result;
// Loop through rows of the matrix
for (int i = 0; i < 4; i++) {
// Compute dot product of the ith row of the matrix and the vector
result.set(i, m_matrix[i].dotProduct(vec));
}
return result;
}
static Matrix4 convertFromGzMat(GzMatrix mat) {
// convert GzMatrix to Matrix4
Vector4 v0(mat[0][0], mat[0][1], mat[0][2], mat[0][3]);
Vector4 v1(mat[1][0], mat[1][1], mat[1][2], mat[1][3]);
Vector4 v2(mat[2][0], mat[2][1], mat[2][2], mat[2][3]);
Vector4 v3(mat[3][0], mat[3][1], mat[3][2], mat[3][3]);
return Matrix4(v0, v1, v2, v3);
}
static void convertToGzMat(const Matrix4& mat, GzMatrix& result) {
// convert Matrix4 to GzMatrix
result[0][0] = mat.getRow(0).getX();
result[0][1] = mat.getRow(0).getY();
result[0][2] = mat.getRow(0).getZ();
result[0][3] = mat.getRow(0).getW();
result[1][0] = mat.getRow(1).getX();
result[1][1] = mat.getRow(1).getY();
result[1][2] = mat.getRow(1).getZ();
result[1][3] = mat.getRow(1).getW();
result[2][0] = mat.getRow(2).getX();
result[2][1] = mat.getRow(2).getY();
result[2][2] = mat.getRow(2).getZ();
result[2][3] = mat.getRow(2).getW();
result[3][0] = mat.getRow(3).getX();
result[3][1] = mat.getRow(3).getY();
result[3][2] = mat.getRow(3).getZ();
result[3][3] = mat.getRow(3).getW();
}
Matrix4 setIdentity() {
// set the matrix to identity matrix
m_matrix[0] = Vector4(1, 0, 0, 0);
m_matrix[1] = Vector4(0, 1, 0, 0);
m_matrix[2] = Vector4(0, 0, 1, 0);
m_matrix[3] = Vector4(0, 0, 0, 1);
}
};