-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cc
294 lines (270 loc) · 7.24 KB
/
matrix.cc
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
#include <math.h>
#include <stdio.h>
#include <GL/gl.h>
#include "matrix.hh"
matrix::matrix() {}
matrix::matrix(const matrix& m)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
array[i][j] = m.array[i][j];
}
matrix matrix::operator= (const matrix& m)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
array[i][j] = m.array[i][j];
return *this;
}
matrix matrix::operator+ (const matrix& param) const
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
// fourth row must remain invarient for homogeneous coordinates to work
ret.array[i][j] = array[i][j] + (j < 3 ? param.array[i][j] : 0);
return ret;
}
matrix matrix::operator+= (const matrix& param)
{
// fourth row must remain invarient for homogeneous coordinates to work
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
array[i][j] += param.array[i][j];
return *this;
}
matrix matrix::operator- (const matrix& param) const
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
// fourth row must remain invarient for homogeneous coordinates to work
ret.array[i][j] = array[i][j] - (j < 3 ? param.array[i][j] : 0);
return ret;
}
matrix matrix::operator-= (const matrix& param)
{
// fourth row must remain invarient for homogeneous coordinates to work
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
array[i][j] -= param.array[i][j];
return *this;
}
matrix matrix::operator* (const matrix& param) const
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
{
ret.array[i][j] = 0;
for(int k = 0; k < 4; k++)
ret.array[i][j] += array[i][k] * param.array[k][j];
}
return ret;
}
matrix matrix::operator*= (const matrix& param)
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
{
ret.array[i][j] = 0;
for(int k = 0; k < 4; k++)
ret.array[i][j] += array[i][k] * param.array[k][j];
}
return *this = ret;
}
matrix matrix::operator* (double param) const
{
matrix ret;
for(int i = 0; i < 3; i++)
for(int j = 0; j < 4; j++)
ret.array[i][j] = param * array[i][j];
// don't scale the fourth row, or we'll mess up homogeneous coordinates
for(int j = 0; j < 4; j++)
ret.array[3][j] = array[3][j];
return ret;
}
matrix matrix::operator*= (double param)
{
for(int i = 0; i < 4; i++)
for(int j = 0; j < 3; j++)
array[i][j] = param * array[i][j];
// don't scale the fourth row, or we'll mess up homogeneous coordinates
for(int j = 0; j < 4; j++)
array[3][j] = array[3][j];
return *this;
}
matrix matrix::inverse() const
{
matrix ret = matrix::identity();
double dot = 1.0 / (array[0][0] * array[0][0] + array[1][0] * array[1][0]
+ array[2][0] * array[2][0]);
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
// transpose the rotation and invert the scaling
ret.array[i][j] = dot * array[j][i];
// reflect the translation, and convert to original basis
ret.array[i][3] -= dot * array[j][i] * array[j][3];
}
return ret;
}
matrix matrix::invert()
{
*this = this->inverse();
return *this;
}
point matrix::operator* (const point& param) const
{
vector ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
ret.array[i] += array[i][j] * param.array[j];
return ret;
}
void matrix::load() const
{
double elems [16];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
elems[4 * i + j] = array[j][i];
glLoadMatrixd(elems);
}
void matrix::mult() const
{
double elems [16];
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
elems[4 * i + j] = array[j][i];
glMultMatrixd(elems);
}
matrix matrix::cross(const point& param)
{
matrix omega;
if(!param.is_vector())
printf("Warning: cross called on non-vector\n");
omega.array[0][0] = 0.0;
omega.array[0][1] = -param.get_Z();
omega.array[0][2] = param.get_Y();
omega.array[0][3] = 0.0;
omega.array[1][0] = param.get_Z();
omega.array[1][1] = 0.0;
omega.array[1][2] = -param.get_X();
omega.array[1][3] = 0.0;
omega.array[2][0] = -param.get_Y();
omega.array[2][1] = param.get_X();
omega.array[2][2] = 0.0;
omega.array[2][3] = 0.0;
omega.array[3][0] = 0.0;
omega.array[3][1] = 0.0;
omega.array[3][2] = 0.0;
omega.array[3][3] = 1.0;
return omega;
}
matrix matrix::identity()
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
ret.array[i][j] = i == j ? 1.0 : 0.0;
return ret;
}
matrix matrix::translate(double tx, double ty, double tz)
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 3; j++)
ret.array[i][j] = i == j ? 1.0 : 0.0;
ret.array[0][3] = tx;
ret.array[1][3] = ty;
ret.array[2][3] = tz;
ret.array[3][3] = 1.0;
return ret;
}
matrix matrix::rotate(double theta, double vx, double vy, double vz)
{
vector v(vx, vy, vz);
matrix omega = cross(v.normalize());
matrix ret = matrix::identity() + omega * sin(theta)
+ omega * omega * (1 - cos(theta));
return ret;
}
matrix matrix::scale(double sx, double sy, double sz)
{
matrix ret;
for(int i = 0; i < 4; i++)
for(int j = 0; j < 4; j++)
ret.array[i][j] = 0.0;
ret.array[0][0] = sx;
ret.array[1][1] = sy;
ret.array[2][2] = sz;
ret.array[3][3] = 1.0;
return ret;
}
matrix matrix::look_at(point origin, point focus, point up)
{
int i;
matrix ret = identity();
// force points to the correct type
if(!origin.array[3]) origin.array[3] = 1;
if(!focus.array[3]) focus.array[3] = 1;
if(up.array[3] != 0 && up.array[3] != 1)
{
up /= up.array[3];
up.array[3] = 0;
}
// handle origin/focus degeneracy
if(!(origin - focus).norm())
{
focus = origin - vector(0,0,1);
printf("Warning: matrix::look_at(): origin and focus are same point\n");
}
// calculate basis vectors
point z = (origin - focus).normalize(),
y = (up - z * (up * z)).normalize();
// pick a default if up in direction of origin - focus
if(!y.norm())
{
printf("Warning: matrix::look_at(): degeneracy of axes\n");
y = (vector(0,1,0) - z * (vector(0,1,0) * z)).normalize();
if(!y.norm()) y = (vector(0,0,1) - z * (vector(0,0,1) * z)).normalize();
}
point x = cross(y) * z;
// build matrix as rotation
for(i = 0; i < 3; i++)
{
ret.array[0][i] = x.array[i];
ret.array[1][i] = y.array[i];
ret.array[2][i] = z.array[i];
}
// add translation component
point shift = -(ret * origin);
for(i = 0; i < 3; i++)
ret.array[i][3] = shift.array[i];
return ret;
}
// perspective transformation, based on Watt Handout
// Assume the view plane is centered at the origin and scaled to the
// output window
matrix matrix::persp(double near, double far, double image)
{
matrix ret = identity();
ret.array[0][0] = ret.array[1][1] = image;
ret.array[2][2] = (near + far) / (near - far);
ret.array[2][3] = 2 * near * far / (near - far);
ret.array[3][2] = -1;
ret.array[3][3] = 0;
return ret;
}
// inverse perspective transformation, based on Watt Handout
matrix matrix::inv_persp(double near, double far, double image)
{
matrix ret = identity();
ret.array[0][0] = ret.array[1][1] = 1.0 / image;
ret.array[2][2] = 0;
ret.array[2][3] = -1;
ret.array[3][2] = (near - far) / (2 * near * far);
ret.array[3][3] = (near + far) / (2 * near * far);
return ret;
}