-
Notifications
You must be signed in to change notification settings - Fork 0
/
blMatrix3dOperations.hpp
472 lines (363 loc) · 16.1 KB
/
blMatrix3dOperations.hpp
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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
#ifndef BL_MATRIX3DOPERATIONS_HPP
#define BL_MATRIX3DOPERATIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: Useful functions for handling 3x3 matrices
///
/// AUTHOR: Vincenzo Barbato
/// navyenzo@gmail.com
///
/// NOTE: All things in this library are defined within the
/// blMathAPI namespace
///
/// LISENSE: MIT-LICENCE
/// http://www.opensource.org/licenses/mit-license.php
///
///
///
///-------------------------------------------------------------------
//-------------------------------------------------------------------
// Includes needed for this file
//-------------------------------------------------------------------
#include "blMatrix3d.hpp"
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// NOTE: This class is defined within the blMathAPI namespace
//-------------------------------------------------------------------
namespace blMathAPI
{
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to calculate the
// trace of the matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType trace(const blMatrix3d<blNumberType>& matrix)
{
// trace is the sum of
// the diagonal components
return (matrix[0][0] + matrix[1][1] + matrix[2][2]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to calculate the determinant
// of a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType det(const blMatrix3d<blNumberType>& matrix)
{
// Initialize the determinant
blNumberType result = 0;
// Calculate the determinant
result += matrix[0][0]*(matrix[1][1]*matrix[2][2] - matrix[2][1]*matrix[1][2]);
result -= matrix[0][1]*(matrix[1][0]*matrix[2][2] - matrix[2][0]*matrix[1][2]);
result += matrix[0][2]*(matrix[1][0]*matrix[2][1] - matrix[2][0]*matrix[1][1]);
// Return the determinant
return result;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to transpose a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix3d<blNumberType> transpose(const blMatrix3d<blNumberType>& matrix)
{
return blMatrix3d<blNumberType>(matrix[0][0],matrix[1][0],matrix[2][0],
matrix[0][1],matrix[1][1],matrix[2][1],
matrix[0][2],matrix[1][2],matrix[2][2]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to return an identity matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix3d<blNumberType> eye3d(const blNumberType& diagonalValue = blNumberType(1))
{
return blMatrix3d<blNumberType>(diagonalValue,0,0,
0,diagonalValue,0,
0,0,diagonalValue);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to calculate the inverse of a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix3d<blNumberType> inv(const blMatrix3d<blNumberType>& matrix)
{
// First: Calculate the determinant
// of the matrix
blNumberType D = det(matrix);
// Second: transpose the matrix
blMatrix3d<blNumberType> Mt = transpose(matrix);
// Third: Calculate the matrix
// of minors
blMatrix3d<blNumberType> Minors;
Minors[0][0] = Mt[1][1]*Mt[2][2] - Mt[2][1]*Mt[1][2];
Minors[0][1] = Mt[1][0]*Mt[2][2] - Mt[2][0]*Mt[1][2];
Minors[0][2] = Mt[1][0]*Mt[2][1] - Mt[2][0]*Mt[1][1];
Minors[1][0] = Mt[0][1]*Mt[2][2] - Mt[2][1]*Mt[0][2];
Minors[1][1] = Mt[0][0]*Mt[2][2] - Mt[2][0]*Mt[0][2];
Minors[1][2] = Mt[0][0]*Mt[2][1] - Mt[2][0]*Mt[0][1];
Minors[2][0] = Mt[0][1]*Mt[1][2] - Mt[1][1]*Mt[0][2];
Minors[2][1] = Mt[0][0]*Mt[1][2] - Mt[1][0]*Mt[0][2];
Minors[2][2] = Mt[0][0]*Mt[1][1] - Mt[1][0]*Mt[0][1];
// Fourth: Calculate the
// adjoint matrix
Minors[0][1] *= blNumberType(-1);
Minors[1][0] *= blNumberType(-1);
Minors[1][2] *= blNumberType(-1);
Minors[2][1] *= blNumberType(-1);
// Fifth: Calculate the
// inverse
return Minors/D;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to divide two matrices which
// means multiplying m1 by inv(m2)
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix3d<blNumberType> operator/(const blMatrix3d<blNumberType>& m1,
const blMatrix3d<blNumberType>& m2)
{
return m1*inv(m2);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to multiply a scalar by a matrix where
// the scalar comes before the matrix in the equation
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix3d<blNumberType> operator*(const blNumberType& scalar,
const blMatrix3d<blNumberType>& matrix)
{
return matrix*scalar;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to divide a scalar by a matrix
// which means multiply a scalar by the inverse
// of that matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix2d<blNumberType> operator/(const blNumberType& scalar,
const blMatrix3d<blNumberType>& matrix)
{
inv(matrix)*scalar;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to multiply vectors with matrices
//-------------------------------------------------------------------
template<typename blNumberType>
inline blVector3d<blNumberType> operator*(const blVector3d<blNumberType>& vector1,
const blMatrix3d<blNumberType>& matrix2)
{
// C[i][j] = Sum(A[i][k]*B[k][j])
return blVector3d<blNumberType>(vector1.x()*matrix2[0][0] + vector1.y()*matrix2[1][0] + vector1.z()*matrix2[2][0],
vector1.x()*matrix2[0][1] + vector1.y()*matrix2[1][1] + vector1.z()*matrix2[2][1],
vector1.x()*matrix2[0][2] + vector1.y()*matrix2[1][2] + vector1.z()*matrix2[2][2]);
}
template<typename blNumberType>
inline blVector3d<blNumberType> operator*(const blMatrix3d<blNumberType>& matrix1,
const blVector3d<blNumberType>& vector2)
{
// C[i][j] = Sum(A[i][k]*B[k][j])
return blVector3d<blNumberType>(matrix1[0][0]*vector2.x() + matrix1[0][1]*vector2.y() + matrix1[0][2]*vector2.z(),
matrix1[1][0]*vector2.x() + matrix1[1][1]*vector2.y() + matrix1[1][2]*vector2.z(),
matrix1[2][0]*vector2.x() + matrix1[2][1]*vector2.y() + matrix1[2][2]*vector2.z());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// output a matrix3d
// to an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::ostream& operator<<(std::ostream& os,
const blMatrix3d<blNumberType>& matrix3d)
{
// We simply output the
// components with a
// space separating them
os << matrix3d[0][0] << " " << matrix3d[0][1] << " " << matrix3d[0][2] << " ";
os << matrix3d[1][0] << " " << matrix3d[1][1] << " " << matrix3d[1][2] << " ";
os << matrix3d[2][0] << " " << matrix3d[2][1] << " " << matrix3d[2][2];
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// input a matrix3d
// from an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::istream& operator>>(std::istream& is,
blMatrix3d<blNumberType>& matrix3d)
{
if(!(is >> matrix3d[0][0]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[0][1]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[0][2]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[1][0]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[1][1]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[1][2]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[2][0]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[2][1]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix3d[2][2]))
{
// Error -- Cound not read
// the value
return is;
}
return is;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// convert a generic
// string to a matrix3d
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType convertStringToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& decimalPointDelimiter,
blMathAPI::blMatrix3d<blNumberType>& convertedNumber,
const int& numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter)
{
// We expect the
// following format:
// M = m00 m01 m02
// m10 m11 m12
// m20 m21 m22
blStringIteratorType newStringPosition;
// First we try to
// get the m00 value
newStringPosition = convertStringToNumber(beginIter,
endIter,
decimalPointDelimiter,
convertedNumber[0][0],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m01 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[0][1],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m02 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[0][2],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m10 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[1][0],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m11 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[1][1],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m12 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[1][2],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m20 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[2][0],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m21 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[2][1],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
if(newStringPosition != endIter)
++newStringPosition;
// Then we try to
// get the m22 value
newStringPosition = convertStringToNumber(newStringPosition,
endIter,
decimalPointDelimiter,
convertedNumber[2][2],
numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter);
return newStringPosition;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_MATRIX3DOPERATIONS_HPP