-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix3.cpp
More file actions
322 lines (270 loc) · 9.74 KB
/
Matrix3.cpp
File metadata and controls
322 lines (270 loc) · 9.74 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
//////////////////////////////////////////////////////////////////////
//
// University of Leeds
// COMP 5823M Animation & Simulation
//
// September, 2020
//
// ------------------------
// Matrix3.h
// ------------------------
//
// A minimal class for a 3x3 Cartesian matrix
//
///////////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include "Matrix3.h"
#include <math.h>
// constructor - default to the zero matrix
Matrix3::Matrix3()
{ // default constructor
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
coordinates[row][col] = 0.0;
} // default constructor
// equality operator
bool Matrix3::operator ==(const Matrix3 &other) const
{ // operator ==()
// loop through, testing for mismatches
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; 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 * Matrix3::operator [](const int rowIndex)
{ // operator *()
// return the corresponding row
return coordinates[rowIndex];
} // operator *()
// similar routine for const pointers
const float * Matrix3::operator [](const int rowIndex) const
{ // operator *()
// return the corresponding row
return coordinates[rowIndex];
} // operator *()
// scalar operations
// multiplication operator (no division operator)
Matrix3 Matrix3::operator *(float factor) const
{ // operator *()
// start with a zero matrix
Matrix3 returnMatrix;
// multiply by the factor
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
returnMatrix.coordinates[row][col] = coordinates[row][col] * factor;
// and return it
return returnMatrix;
} // operator *()
// vector operations on Cartesian coordinates
Cartesian3 Matrix3::operator *(const Cartesian3 &vector) const
{ // cartesian multiplication
// get a zero-initialised vector
Cartesian3 productVector;
// now loop, adding products
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
productVector[row] += coordinates[row][col] * vector[col];
// return the result
return productVector;
} // cartesian multiplication
// matrix operations
// addition operator
Matrix3 Matrix3::operator +(const Matrix3 &other) const
{ // operator +()
// start with a zero matrix
Matrix3 sumMatrix;
// now loop, adding products
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
sumMatrix.coordinates[row][col] = coordinates[row][col] + other.coordinates[row][col];
// return the result
return sumMatrix;
} // operator +()
// subtraction operator
Matrix3 Matrix3::operator -(const Matrix3 &other) const
{ // operator -()
// start with a zero matrix
Matrix3 differenceMatrix;
// now loop, adding products
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
differenceMatrix.coordinates[row][col] = coordinates[row][col] + other.coordinates[row][col];
// return the result
return differenceMatrix;
} // operator -()
// multiplication operator
Matrix3 Matrix3::operator *(const Matrix3 &other) const
{ // operator *()
// start with a zero matrix
Matrix3 productMatrix;
// now loop, adding products
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
for (int entry = 0; entry < 3; entry++)
productMatrix.coordinates[row][col] += coordinates[row][entry] * other.coordinates[entry][col];
// return the result
return productMatrix;
} // operator *()
// matrix transpose
Matrix3 Matrix3::transpose() const
{ // transpose()
// start with a zero matrix
Matrix3 transposeMatrix;
// now loop, adding products
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
transposeMatrix.coordinates[row][col] = coordinates[col][row];
// return the result
return transposeMatrix;
} // transpose()
// routine that returns a row vector
Cartesian3 Matrix3::row(int rowNum)
{ // row()
// temporary variable
Cartesian3 returnValue;
// loop to copy
for (int column = 0; column < 3; column++)
returnValue[column] = (*this)[rowNum][column];
// and return it
return returnValue;
} // row()
// and similar for a column
Cartesian3 Matrix3::column(int colNum)
{ // column()
// temporary variable
Cartesian3 returnValue;
// loop to copy
for (int row = 0; row < 3; row++)
returnValue[row] = (*this)[row][colNum];
// and return it
return returnValue;
} // column()
// static member functions that create specific matrices
// the zero matrix
Matrix3 Matrix3::Zero()
{ // Zero()
// create a temporary matrix - constructor will automatically zero it
Matrix3 returnMatrix;
// so we just return it
return returnMatrix;
} // Zero()
// the identity matrix
Matrix3 Matrix3::Identity()
{ // Identity()
// create a temporary matrix - constructor will automatically zero it
Matrix3 returnMatrix;
// fill in the diagonal with 1's
for (int row = 0; row < 3; row++)
returnMatrix.coordinates[row][row] = 1.0;
// return it
return returnMatrix;
} // Identity()
Matrix3 Matrix3::RotateX(float degrees)
{ // RotateX()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix3 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()
Matrix3 Matrix3::RotateY(float degrees)
{ // RotateY()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix3 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()
Matrix3 Matrix3::RotateZ(float degrees)
{ // RotateZ()
// convert angle from degrees to radians
float theta = DEG2RAD(degrees);
// create a temporary matrix and set to identity
Matrix3 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()
// scalar operations
// additional scalar multiplication operator
Matrix3 operator *(float factor, const Matrix3 &matrix)
{ // operator *()
// since this is commutative, call the other version
return matrix * factor;
} // operator *()
// routine to transpose a matrix
Matrix3 Matrix3::Transpose()
{ // Transpose()
// the copy to return
Matrix3 transposed;
// loop to copy
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
transposed[col][row] = coordinates[row][col];
// and return it
return transposed;
} // Transpose()
// stream input
std::istream & operator >> (std::istream &inStream, Matrix3 &matrix)
{ // operator >>()
// just loop, reading them in
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
inStream >> matrix.coordinates[row][col];
// and return the stream
return inStream;
} // operator >>()
// stream output
std::ostream & operator << (std::ostream &outStream, const Matrix3 &matrix)
{ // operator <<()
// just loop, reading them in
for (int row = 0; row < 3; row++)
for (int col = 0; col < 3; col++)
outStream << std::setw(12) << std::setprecision(5) << std::fixed << matrix.coordinates[row][col] << ((col == 2) ? "\n" : " ");
// and return the stream
return outStream;
} // operator <<()
Matrix3 Matrix3::Inverse()
{ // Inverse()
// create an inverse matrix to fill in
Matrix3 coMatrix;
// fill in the individual entries with cofactors
coMatrix[0][0] = coordinates[1][1] * coordinates[2][2] - coordinates[1][2] * coordinates[2][1];
coMatrix[0][1] = coordinates[1][2] * coordinates[2][0] - coordinates[1][0] * coordinates[2][2];
coMatrix[0][2] = coordinates[1][0] * coordinates[2][1] - coordinates[1][1] * coordinates[2][0];
coMatrix[1][0] = coordinates[2][1] * coordinates[1][0] - coordinates[2][0] * coordinates[1][1];
coMatrix[1][1] = coordinates[2][2] * coordinates[0][0] - coordinates[2][0] * coordinates[0][2];
coMatrix[1][2] = coordinates[2][0] * coordinates[0][1] - coordinates[2][1] * coordinates[0][0];
coMatrix[2][0] = coordinates[0][1] * coordinates[1][2] - coordinates[0][2] * coordinates[1][1];
coMatrix[2][1] = coordinates[0][2] * coordinates[1][0] - coordinates[0][0] * coordinates[1][2];
coMatrix[2][2] = coordinates[0][0] * coordinates[1][1] - coordinates[0][1] * coordinates[1][0];
// we can also use these entries to compute the determinant, which is just a row or column-wise sum of the signed cofactors
float det = coordinates[0][0] * coMatrix[0][0] + coordinates[0][1] * coMatrix[0][1] + coordinates[0][2] * coMatrix[0][2];
// if the determinant is zero, return a zero matrix
if (det == 0)
return Zero();
// otherwise transpose the comatrix and divide by the determinant
else
return (1.0 / det) * coMatrix.Transpose();
} // Inverse()