-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix4.cpp
More file actions
404 lines (345 loc) · 12 KB
/
Matrix4.cpp
File metadata and controls
404 lines (345 loc) · 12 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
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//////////////////////////////////////////////////////////////////////
//
// University of Leeds
// COMP 5812M Foundations of Modelling & Rendering
// User Interface for Coursework
//
// September, 2020
//
// ------------------------
// Matrix4.h
// ------------------------
//
// A minimal class for a homogeneous 4x4 matrix
//
// Note: the emphasis here is on clarity, not efficiency
// A number of the routines could be implemented more
// efficiently but aren't
//
///////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include "Matrix4.h"
#include <math.h>
// constructor - default to the zero matrix
Matrix4::Matrix4()
{ // default constructor
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
coordinates[row][col] = 0.0;
} // default constructor
// constructor from a Matrix3
Matrix4::Matrix4(Matrix3 &other)
{ // constructor from Matrix3
// zero it out first
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
coordinates[row][col] = 0.0;
// now copy entries in
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
coordinates[row][col] = other[row][col];
// and set the homogeneous entry to 1
coordinates[3][3] = 1.0;
} // constructor from Matrix3
// equality operator
bool Matrix4::operator ==(const Matrix4 &other) const
{ // operator ==()
// loop through, testing for mismatches
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
if (coordinates[row][col] != other.coordinates[row][col])
return false;
// if no mismatches, matrices are the same
return true;
} // operator ==()
// indexing - retrieves the beginning of a line
// array indexing will then retrieve an element
float * Matrix4::operator [](const int rowIndex)
{ // operator *()
// return the corresponding row
return coordinates[rowIndex];
} // operator *()
// similar routine for const pointers
const float * Matrix4::operator [](const int rowIndex) const
{ // operator *()
// return the corresponding row
return coordinates[rowIndex];
} // operator *()
// scalar operations
// multiplication operator (no division operator)
Matrix4 Matrix4::operator *(float factor) const
{ // operator *()
// start with a zero matrix
Matrix4 returnMatrix;
// multiply by the factor
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
returnMatrix.coordinates[row][col] = coordinates[row][col] * factor;
// and return it
return returnMatrix;
} // operator *()
// vector operations on homogeneous coordinates
// multiplication is the only operator we use
Homogeneous4 Matrix4::operator *(const Homogeneous4 &vector) const
{ // operator *()
// get a zero-initialised vector
Homogeneous4 productVector;
// now loop, adding products
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
productVector[row] += coordinates[row][col] * vector[col];
// return the result
return productVector;
} // operator *()
// and on Cartesian coordinates
Cartesian3 Matrix4::operator *(const Cartesian3 &vector) const
{ // cartesian multiplication
// convert to Homogeneous coords and multiply
Homogeneous4 productVector = (*this) * Homogeneous4(vector);
// then divide back through
return productVector.Point();
} // cartesian multiplication
// matrix operations
// addition operator
Matrix4 Matrix4::operator +(const Matrix4 &other) const
{ // operator +()
// start with a zero matrix
Matrix4 sumMatrix;
// now loop, adding products
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
sumMatrix.coordinates[row][col] = coordinates[row][col] + other.coordinates[row][col];
// return the result
return sumMatrix;
} // operator +()
// subtraction operator
Matrix4 Matrix4::operator -(const Matrix4 &other) const
{ // operator -()
// start with a zero matrix
Matrix4 differenceMatrix;
// now loop, adding products
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
differenceMatrix.coordinates[row][col] = coordinates[row][col] + other.coordinates[row][col];
// return the result
return differenceMatrix;
} // operator -()
// multiplication operator
Matrix4 Matrix4::operator *(const Matrix4 &other) const
{ // operator *()
// start with a zero matrix
Matrix4 productMatrix;
// now loop, adding products
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
for (int entry = 0; entry < 4; entry++)
productMatrix.coordinates[row][col] += coordinates[row][entry] * other.coordinates[entry][col];
// return the result
return productMatrix;
} // operator *()
// matrix transpose
Matrix4 Matrix4::transpose() const
{ // transpose()
// start with a zero matrix
Matrix4 transposeMatrix;
// now loop, adding products
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
transposeMatrix.coordinates[row][col] = coordinates[col][row];
// return the result
return transposeMatrix;
} // transpose()
// returns a column-major array of 16 values
// for use with OpenGL
columnMajorMatrix Matrix4::columnMajor() const
{ // columnMajor()
// start off with an unitialised array
columnMajorMatrix returnArray;
// loop to fill in
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
returnArray.coordinates[4 * col + row] = coordinates[row][col];
// now return the array
return returnArray;
} // columnMajor()
// routine that returns a row vector as a Homogeneous4
Homogeneous4 Matrix4::row(int rowNum)
{ // row()
// temporary variable
Homogeneous4 returnValue;
// loop to copy
for (int column = 0; column < 4; column++)
returnValue[column] = (*this)[rowNum][column];
// and return it
return returnValue;
} // row()
// and similar for a column
Homogeneous4 Matrix4::column(int colNum)
{ // column()
// temporary variable
Homogeneous4 returnValue;
// loop to copy
for (int row = 0; row < 4; row++)
returnValue[row] = (*this)[row][colNum];
// and return it
return returnValue;
} // column()
// static member functions that create specific matrices
// the zero matrix
Matrix4 Matrix4::Zero()
{ // Zero()
// create a temporary matrix - constructor will automatically zero it
Matrix4 returnMatrix;
// so we just return it
return returnMatrix;
} // Zero()
// the identity matrix
Matrix4 Matrix4::Identity()
{ // Identity()
// create a temporary matrix - constructor will automatically zero it
Matrix4 returnMatrix;
// fill in the diagonal with 1's
for (int row = 0; row < 4; row++)
returnMatrix.coordinates[row][row] = 1.0;
// return it
return returnMatrix;
} // Identity()
Matrix4 Matrix4::Translate(const Cartesian3 &vector)
{ // Translation()
// create a temporary matrix and set to identity
Matrix4 returnMatrix = Identity();
// put the translation in the w column
for (int entry = 0; entry < 3; entry++)
returnMatrix.coordinates[entry][3] = vector[entry];
// return it
return returnMatrix;
} // Translation()
Matrix4 Matrix4::RotateX(float degrees)
{ // RotateX()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix4 returnMatrix = Identity();
// now set the four coefficients affected
returnMatrix.coordinates[1][1] = cos(theta);
returnMatrix.coordinates[1][2] = sin(theta);
returnMatrix.coordinates[2][1] = -sin(theta);
returnMatrix.coordinates[2][2] = cos(theta);
// return it
return returnMatrix;
} // RotateX()
Matrix4 Matrix4::RotateY(float degrees)
{ // RotateY()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix4 returnMatrix = Identity();
// now set the four coefficients affected
returnMatrix.coordinates[0][0] = cos(theta);
returnMatrix.coordinates[0][2] = -sin(theta);
returnMatrix.coordinates[2][0] = sin(theta);
returnMatrix.coordinates[2][2] = cos(theta);
// return it
return returnMatrix;
} // RotateY()
Matrix4 Matrix4::RotateZ(float degrees)
{ // RotateZ()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix4 returnMatrix = Identity();
// now set the four coefficients affected
returnMatrix.coordinates[0][0] = cos(theta);
returnMatrix.coordinates[0][1] = sin(theta);
returnMatrix.coordinates[1][0] = -sin(theta);
returnMatrix.coordinates[1][1] = cos(theta);
// return it
return returnMatrix;
} // RotateZ()
Matrix4 Matrix4::GetRotation(const Cartesian3& vector1, const Cartesian3& vector2)
{
Cartesian3 c = vector1.cross(vector2).unit();
float cos = vector1.unit().dot(vector2.unit());
float sin = sqrt(1 - pow(cos, 2));
Matrix4 rot = Matrix4::Identity();
rot.coordinates[0][0] = cos + (1 - cos) * pow(c.x, 2);
rot.coordinates[0][1] = (1 - cos) * c.x * c.y - sin * c.z;
rot.coordinates[0][2] = (1 - cos) * c.x * c.z + sin * c.y;
rot.coordinates[1][0] = (1 - cos) * c.y * c.x + sin * c.z;
rot.coordinates[1][1] = cos + (1 - cos) * pow(c.y, 2);
rot.coordinates[1][2] = (1 - cos) * c.y * c.z - sin * c.x;
rot.coordinates[2][0] = (1 - cos) * c.z * c.x - sin * c.y;
rot.coordinates[2][1] = (1 - cos) * c.z * c.y + sin * c.x;
rot.coordinates[2][2] = cos + (1 - cos) * pow(c.z, 2);
return rot;
}
// routines to retrieve the rotation and translation component
// NOTE: NO ERROR CHECKING: assumes only pure rotation plus translation
Matrix4 Matrix4::GetRotationMatrix()
{ // GetRotationMatrix()
// start with a duplicate copy
Matrix4 returnMatrix = *this;
// and set the final row and column's entries to 0 (except [3][3]
returnMatrix.coordinates[0][3] = 0.0;
returnMatrix.coordinates[1][3] = 0.0;
returnMatrix.coordinates[2][3] = 0.0;
returnMatrix.coordinates[3][0] = 0.0;
returnMatrix.coordinates[3][1] = 0.0;
returnMatrix.coordinates[3][2] = 0.0;
// and return it
return returnMatrix;
} // GetRotationMatrix()
Cartesian3 Matrix4::GetTranslationVector()
{ // GetTranslationVector()
// exploit existing routines - it's just column 3 turned into a vector
return column(3).Vector();
} // GetTranslationVector()
// routine to retrieve a Matrix3x3
Matrix3 Matrix4::GetMatrix3()
{ // GetMatrix3()
Matrix3 returnMatrix;
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
returnMatrix[row][col] = coordinates[row][col];
return returnMatrix;
} // GetMatrix3()
// scalar operations
// additional scalar multiplication operator
Matrix4 operator *(float factor, const Matrix4 &matrix)
{ // operator *()
// since this is commutative, call the other version
return matrix * factor;
} // operator *()
// routine to transpose a matrix
Matrix4 Matrix4::Transpose()
{ // Transpose()
// the copy to return
Matrix4 transposed;
// loop to copy
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
transposed[col][row] = coordinates[row][col];
// and return it
return transposed;
} // Transpose()
// stream input
std::istream & operator >> (std::istream &inStream, Matrix4 &matrix)
{ // operator >>()
// just loop, reading them in
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
inStream >> matrix.coordinates[row][col];
// and return the stream
return inStream;
} // operator >>()
// stream output
std::ostream & operator << (std::ostream &outStream, const Matrix4 &matrix)
{ // operator <<()
// just loop, reading them in
for (int row = 0; row < 4; row++)
for (int col = 0; col < 4; col++)
outStream << std::setw(12) << std::setprecision(5) << std::fixed << matrix.coordinates[row][col] << ((col == 3) ? "\n" : " ");
// and return the stream
return outStream;
} // operator <<()