mt
 All Classes Files Functions Enumerations Groups Pages
matrix3x3.h
Go to the documentation of this file.
1 /***************************************************************************
2  * Copyright (C) 2006 by Adolfo Rodriguez *
3  * adolfo.rodriguez@upc.edu *
4  * *
5  * This program is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation; either version 2 of the License, or *
8  * (at your option) any later version. *
9  * *
10  * This program is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13  * GNU General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU General Public License *
16  * along with this program; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19  ***************************************************************************/
20 
22 
23 // HEADER GUARD
24 #ifndef MT_MATRIX3X3_H
25 #define MT_MATRIX3X3_H
26 
27 // C++ STANDARD HEADERS
28 #include <iostream>
29 
30 // PROJECT HEADERS
31 #include <mt/util/assert/assert_template.h>
32 
33 // MT LIBRARY HEADERS
34 #include <mt/scalar.h>
35 #include <mt/unit3.h>
36 #include <mt/vector3.h>
37 
38 
40 
41 namespace mt
42 {
43 
44 
46 
49 
55 
56 class Matrix3x3
57 {
58 public:
59 
60 // LIFECYCLE
61 
63  Matrix3x3();
64 
66  Matrix3x3(const Scalar& xx, const Scalar& xy, const Scalar& xz,
67  const Scalar& yx, const Scalar& yy, const Scalar& yz,
68  const Scalar& zx, const Scalar& zy, const Scalar& zz);
69 
70  // Compiler generated copy constructor is being used
71 
72  // Compiler generated destructor is being used
73 
74 
75 // OPERATORS
76 
77  // Compiler generated assignment operator is being used
78 
81  Vector3& operator[](size_t n);
82 
85  const Vector3& operator[](size_t n) const;
86 
88  Matrix3x3& operator*=(const Matrix3x3& m);
89 
90 // OPERATIONS
91 
93  Scalar tdot( size_t c,
94  const Vector3& v) const;
95 
97  Scalar determinant() const;
98 
100  Matrix3x3 adjoint() const;
101 
103  Matrix3x3 transpose() const;
104 
106  Matrix3x3 inverse() const;
107 
109  Matrix3x3 transposeTimes(const Matrix3x3& m) const;
110 
112  Matrix3x3 timesTranspose(const Matrix3x3& m) const;
113 
115  Matrix3x3& scale(const Vector3& v);
116 
117 
118 // ACCESS
119 
122  Vector3& at(size_t n);
123 
126  const Vector3& at(size_t n) const;
127 
129  Vector3 getScaling() const;
130 
132  void setValue(const Scalar& xx, const Scalar& xy, const Scalar& xz,
133  const Scalar& yx, const Scalar& yy, const Scalar& yz,
134  const Scalar& zx, const Scalar& zy, const Scalar& zz);
135 
137  void setIdentity();
138 
139 
140 private:
141 
142 // MEMBERS
143 
144 Vector3 m_el[3];
145 
146 
147 // METHODS
148 
159 Scalar cofac(size_t r1, size_t c1,
160  size_t r2, size_t c2) const;
161 
162 };
163 
165 
166 // OPERATORS
167 
169 Vector3 operator*(const Matrix3x3& m,
170  const Vector3& v);
171 
173 Vector3 operator*(const Vector3& v,
174  const Matrix3x3& m);
175 
177 Matrix3x3 operator*(const Matrix3x3& m1,
178  const Matrix3x3& m2);
179 
180 std::ostream& operator<<(std::ostream& os,
181  const Matrix3x3& m);
182 
183 
184 // FUNCTIONS
185 
187 Matrix3x3 abs(const Matrix3x3& m);
188 
190 Matrix3x3 scale(const Matrix3x3 m,
191  const Vector3& v);
192 
194 Scalar determinant(const Matrix3x3& m);
195 
197 Matrix3x3 adjoint(const Matrix3x3& m);
198 
200 Matrix3x3 transpose(const Matrix3x3& m);
201 
203 Matrix3x3 inverse(const Matrix3x3& m);
204 
206 Matrix3x3 transposeTimes(const Matrix3x3& m1,
207  const Matrix3x3& m2);
208 
210 Matrix3x3 timesTranspose(const Matrix3x3& m1,
211  const Matrix3x3& m2);
212 
213 
215 
216 // LIFECYCLE
217 
219 
220 
221 inline
222 Matrix3x3::Matrix3x3(const Scalar& xx, const Scalar& xy, const Scalar& xz,
223  const Scalar& yx, const Scalar& yy, const Scalar& yz,
224  const Scalar& zx, const Scalar& zy, const Scalar& zz)
225 {
226  setValue(xx, xy, xz,
227  yx, yy, yz,
228  zx, zy, zz);
229 }
230 
231 
232 // OPERATORS
233 
235 {
236  return m_el[n];
237 }
238 
239 
240 inline const Vector3& Matrix3x3::operator[](size_t n) const
241 {
242  return m_el[n];
243 }
244 
245 
247 {
248  setValue(m.tdot(0, m_el[0]), m.tdot(1, m_el[0]), m.tdot(2, m_el[0]),
249  m.tdot(0, m_el[1]), m.tdot(1, m_el[1]), m.tdot(2, m_el[1]),
250  m.tdot(0, m_el[2]), m.tdot(1, m_el[2]), m.tdot(2, m_el[2]));
251  return *this;
252 }
253 
254 
255 // OPERATIONS
256 
257 inline Scalar Matrix3x3::tdot( size_t c,
258  const Vector3& v) const
259 {
260  return (m_el[0][c] * v[0] + m_el[1][c] * v[1] + m_el[2][c] * v[2]);
261 }
262 
263 
264 inline Scalar Matrix3x3::determinant() const
265 {
266  return triple((*this)[0], (*this)[1], (*this)[2]);
267 }
268 
269 
271 {
272  return Matrix3x3(cofac(1, 1, 2, 2), cofac(0, 2, 2, 1), cofac(0, 1, 1, 2),
273  cofac(1, 2, 2, 0), cofac(0, 0, 2, 2), cofac(0, 2, 1, 0),
274  cofac(1, 0, 2, 1), cofac(0, 1, 2, 0), cofac(0, 0, 1, 1));
275 }
276 
277 
279 {
280  return Matrix3x3(m_el[0][0], m_el[1][0], m_el[2][0],
281  m_el[0][1], m_el[1][1], m_el[2][1],
282  m_el[0][2], m_el[1][2], m_el[2][2]);
283 }
284 
285 
287 {
288  // Matrix cofactors
289  const Vector3 co(cofac(1, 1, 2, 2),
290  cofac(1, 2, 2, 0),
291  cofac(1, 0, 2, 1));
292 
293  // Matrix determiant
294  const Scalar det((*this)[0].dot(co));
295 
296  // Inverse computation
297  const Scalar s = Scalar(1.0) / det;
298  return Matrix3x3(co[0] * s, cofac(0, 2, 2, 1) * s, cofac(0, 1, 1, 2) * s,
299  co[1] * s, cofac(0, 0, 2, 2) * s, cofac(0, 2, 1, 0) * s,
300  co[2] * s, cofac(0, 1, 2, 0) * s, cofac(0, 0, 1, 1) * s);
301 }
302 
303 
305 {
306  return Matrix3x3(
307  m_el[0][0] * m[0][0] + m_el[1][0] * m[1][0] + m_el[2][0] * m[2][0],
308  m_el[0][0] * m[0][1] + m_el[1][0] * m[1][1] + m_el[2][0] * m[2][1],
309  m_el[0][0] * m[0][2] + m_el[1][0] * m[1][2] + m_el[2][0] * m[2][2],
310  m_el[0][1] * m[0][0] + m_el[1][1] * m[1][0] + m_el[2][1] * m[2][0],
311  m_el[0][1] * m[0][1] + m_el[1][1] * m[1][1] + m_el[2][1] * m[2][1],
312  m_el[0][1] * m[0][2] + m_el[1][1] * m[1][2] + m_el[2][1] * m[2][2],
313  m_el[0][2] * m[0][0] + m_el[1][2] * m[1][0] + m_el[2][2] * m[2][0],
314  m_el[0][2] * m[0][1] + m_el[1][2] * m[1][1] + m_el[2][2] * m[2][1],
315  m_el[0][2] * m[0][2] + m_el[1][2] * m[1][2] + m_el[2][2] * m[2][2]);
316 }
317 
318 
320 {
321  return Matrix3x3(m_el[0].dot(m[0]), m_el[0].dot(m[1]), m_el[0].dot(m[2]),
322  m_el[1].dot(m[0]), m_el[1].dot(m[1]), m_el[1].dot(m[2]),
323  m_el[2].dot(m[0]), m_el[2].dot(m[1]), m_el[2].dot(m[2]));
324 }
325 
326 
328 {
329  setValue(m_el[0][0] * v[0], m_el[0][1] * v[1], m_el[0][2] * v[2],
330  m_el[1][0] * v[0], m_el[1][1] * v[1], m_el[1][2] * v[2],
331  m_el[2][0] * v[0], m_el[2][1] * v[1], m_el[2][2] * v[2]);
332  return *this;
333 }
334 
335 
336 // ACCESS
337 
338 inline Vector3& Matrix3x3::at(size_t n)
339 {
340  util::Assert(n < 3, std::range_error("Index out of range"));
341  return m_el[n];
342 }
343 
344 
345 inline const Vector3& Matrix3x3::at(size_t n) const
346 {
347  util::Assert(n < 3, std::range_error("Index out of range"));
348  return m_el[n];
349 }
350 
352 {
353  return Vector3
354  (m_el[0][0] * m_el[0][0] + m_el[1][0] * m_el[1][0] + m_el[2][0] * m_el[2][0],
355  m_el[0][1] * m_el[0][1] + m_el[1][1] * m_el[1][1] + m_el[2][1] * m_el[2][1],
356  m_el[0][2] * m_el[0][2] + m_el[1][2] * m_el[1][2] + m_el[2][2] * m_el[2][2]);
357 }
358 
359 
360 inline
361 void Matrix3x3::setValue(const Scalar& xx, const Scalar& xy, const Scalar& xz,
362  const Scalar& yx, const Scalar& yy, const Scalar& yz,
363  const Scalar& zx, const Scalar& zy, const Scalar& zz)
364 {
365  m_el[0][0] = xx;
366  m_el[0][1] = xy;
367  m_el[0][2] = xz;
368  m_el[1][0] = yx;
369  m_el[1][1] = yy;
370  m_el[1][2] = yz;
371  m_el[2][0] = zx;
372  m_el[2][1] = zy;
373  m_el[2][2] = zz;
374 }
375 
376 
378 {
379  setValue(Scalar(1.0), Scalar(0.0), Scalar(0.0),
380  Scalar(0.0), Scalar(1.0), Scalar(0.0),
381  Scalar(0.0), Scalar(0.0), Scalar(1.0));
382 }
383 
384 
385 // PRIVATE METHODS
386 
387 inline Scalar Matrix3x3::cofac(size_t r1, size_t c1,
388  size_t r2, size_t c2) const
389 {
390  return (m_el[r1][c1] * m_el[r2][c2] - m_el[r1][c2] * m_el[r2][c1]);
391 }
392 
393 
395 
396 // OPERATORS
397 
398 inline Vector3 operator*(const Matrix3x3& m,
399  const Vector3& v)
400 {
401  return Vector3(m[0].dot(v),
402  m[1].dot(v),
403  m[2].dot(v));
404 }
405 
406 
407 inline Vector3 operator*(const Vector3& v,
408  const Matrix3x3& m)
409 {
410  return Vector3(m.tdot(0, v),
411  m.tdot(1, v),
412  m.tdot(2, v));
413 }
414 
415 
416 inline Matrix3x3 operator*(const Matrix3x3& m1,
417  const Matrix3x3& m2)
418 {
419  Matrix3x3 m(m1);
420  return m *= m2;
421 }
422 
423 
424 inline std::ostream& operator<<(std::ostream& os,
425  const Matrix3x3& m)
426 {
427  os << m[0] << '\n'
428  << m[1] << '\n'
429  << m[2];
430  return os;
431 }
432 
433 
434 // FUNCTIONS
435 
436 inline Matrix3x3 abs(const Matrix3x3& m)
437 {
438  return Matrix3x3(abs(m[0][0]), abs(m[0][1]), abs(m[0][2]),
439  abs(m[1][0]), abs(m[1][1]), abs(m[1][2]),
440  abs(m[2][0]), abs(m[2][1]), abs(m[2][2]));
441 }
442 
443 
444 inline Matrix3x3 scale(const Matrix3x3 m,
445  const Vector3& v)
446 {
447  Matrix3x3 m1(m);
448  return m1.scale(v);
449 }
450 
451 
452 inline Scalar determinant(const Matrix3x3& m)
453 {
454  return m.determinant();
455 }
456 
457 
458 inline Matrix3x3 adjoint(const Matrix3x3& m)
459 {
460  return m.adjoint();
461 }
462 
463 
464 inline Matrix3x3 transpose(const Matrix3x3& m)
465 {
466  return m.transpose();
467 }
468 
469 
470 inline Matrix3x3 inverse(const Matrix3x3& m)
471 {
472  return m.inverse();
473 }
474 
475 
476 inline Matrix3x3 transposeTimes(const Matrix3x3& m1,
477  const Matrix3x3& m2)
478 {
479  return m1.transposeTimes(m2);
480 }
481 
482 
483 inline Matrix3x3 timesTranspose(const Matrix3x3& m1,
484  const Matrix3x3& m2)
485 {
486  return m1.timesTranspose(m2);
487 }
488 
489 
490 } // mt
491 
492 #endif // MT_MATRIX3X3_H