-
Notifications
You must be signed in to change notification settings - Fork 0
/
blMatrix2dOperations.hpp
332 lines (251 loc) · 11 KB
/
blMatrix2dOperations.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
#ifndef BL_MATRIX2DOPERATIONS_HPP
#define BL_MATRIX2DOPERATIONS_HPP
///-------------------------------------------------------------------
///
///
///
/// PURPOSE: Useful functions for handling 2x2 matrices
///
/// AUTHOR: Vincenzo Barbato
///
/// 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 "blMatrix2d.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 blMatrix2d<blNumberType>& matrix)
{
// trace is the sum
// of the diagonal
// components
return (matrix[0][0] + matrix[1][1]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to calculate the determinant
// of a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blNumberType det(const blMatrix2d<blNumberType>& matrix)
{
return (matrix[0][0]*matrix[1][1] - matrix[1][0]*matrix[0][1]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to transpose a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix2d<blNumberType> transpose(const blMatrix2d<blNumberType>& matrix)
{
return blMatrix2d<blNumberType>(matrix[0][0],matrix[1][0],
matrix[0][1],matrix[1][1]);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to return an identity matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix2d<blNumberType> eye2d(const blNumberType& diagonalValue = blNumberType(1))
{
return blMatrix2d<blNumberType>(diagonalValue,0,
0,diagonalValue);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to calculate the inverse of a matrix
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix2d<blNumberType> inv(const blMatrix2d<blNumberType>& matrix)
{
return blMatrix2d<blNumberType>(matrix[1][1],-matrix[0][1],-matrix[1][0],matrix[0][0]) / det(matrix);
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to divide two matrices which
// means multiplying m1 by inv(m2)
//-------------------------------------------------------------------
template<typename blNumberType>
inline blMatrix2d<blNumberType> operator/(const blMatrix2d<blNumberType>& m1,
const blMatrix2d<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 blMatrix2d<blNumberType> operator*(const blNumberType& scalar,
const blMatrix2d<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 blMatrix2d<blNumberType>& matrix)
{
inv(matrix)*scalar;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Functions used to multiply points with matrices
//-------------------------------------------------------------------
template<typename blNumberType>
inline blPoint2d<blNumberType> operator*(const blPoint2d<blNumberType>& point1,
const blMatrix2d<blNumberType>& matrix2)
{
// C[i][j] = Sum(A[i][k]*B[k][j])
return blPoint2d<blNumberType>(point1.x()*matrix2[0][0] + point1.y()*matrix2[1][0],
point1.x()*matrix2[0][1] + point1.y()*matrix2[1][1]);
}
template<typename blNumberType>
inline blPoint2d<blNumberType> operator*(const blMatrix2d<blNumberType>& matrix1,
const blPoint2d<blNumberType>& point2)
{
// C[i][j] = Sum(A[i][k]*B[k][j])
return blPoint2d<blNumberType>(matrix1[0][0]*point2.x() + matrix1[0][1]*point2.y(),
matrix1[1][0]*point2.x() + matrix1[1][1]*point2.y());
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// output a matrix2d
// to an output stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::ostream& operator<<(std::ostream& os,
const blMatrix2d<blNumberType>& matrix2d)
{
// We simply output the
// components with a
// space separating them
os << matrix2d[0][0] << " " << matrix2d[0][1] << " ";
os << matrix2d[1][0] << " " << matrix2d[1][1];
return os;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// input a matrix2d
// from an input stream
//-------------------------------------------------------------------
template<typename blNumberType>
inline std::istream& operator>>(std::istream& is,
blMatrix2d<blNumberType>& matrix2d)
{
if(!(is >> matrix2d[0][0]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix2d[0][1]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix2d[1][0]))
{
// Error -- Cound not read
// the value
return is;
}
if(!(is >> matrix2d[1][1]))
{
// Error -- Cound not read
// the value
return is;
}
return is;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// Function used to
// convert a generic
// string to a matrix2d
//-------------------------------------------------------------------
template<typename blStringIteratorType,
typename blCharacterType,
typename blNumberType>
inline blStringIteratorType convertStringToNumber(const blStringIteratorType& beginIter,
const blStringIteratorType& endIter,
const blCharacterType& decimalPointDelimiter,
blMathAPI::blMatrix2d<blNumberType>& convertedNumber,
const int& numberOfTimesToRepeatTheSearchIfBeginIterEqualsEndIter)
{
// We expect the
// following format:
// M = m00 m01
// m10 m11
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 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);
return newStringPosition;
}
//-------------------------------------------------------------------
//-------------------------------------------------------------------
// End of the blMathAPI namespace
}
//-------------------------------------------------------------------
#endif // BL_MATRIX2DOPERATIONS_HPP