IsoEx
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Matrix4x4T.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (C) 2001-2011 by Computer Graphics Group, RWTH Aachen *
5  * www.openflipper.org *
6  * *
7  *---------------------------------------------------------------------------*
8  * This file is part of OpenFlipper. *
9  * *
10  * OpenFlipper is free software: you can redistribute it and/or modify *
11  * it under the terms of the GNU Lesser General Public License as *
12  * published by the Free Software Foundation, either version 3 of *
13  * the License, or (at your option) any later version with the *
14  * following exceptions: *
15  * *
16  * If other files instantiate templates or use macros *
17  * or inline functions from this file, or you compile this file and *
18  * link it with other files to produce an executable, this file does *
19  * not by itself cause the resulting executable to be covered by the *
20  * GNU Lesser General Public License. This exception does not however *
21  * invalidate any other reasons why the executable file might be *
22  * covered by the GNU Lesser General Public License. *
23  * *
24  * OpenFlipper is distributed in the hope that it will be useful, *
25  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
26  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
27  * GNU Lesser General Public License for more details. *
28  * *
29  * You should have received a copy of the GNU LesserGeneral Public *
30  * License along with OpenFlipper. If not, *
31  * see <http://www.gnu.org/licenses/>. *
32  * *
33 \*===========================================================================*/
34 
35 /*===========================================================================*\
36  * *
37  * $Revision: 14430 $ *
38  * $Author: ebke $ *
39  * $Date: 2012-04-23 12:26:49 +0200 (Mo, 23 Apr 2012) $ *
40  * *
41 \*===========================================================================*/
42 
43 
44 
45 //=============================================================================
46 //
47 // CLASS Matrix4x4T
48 //
49 //=============================================================================
50 
51 
52 #ifndef ACG_MATRIX4X4_HH
53 #define ACG_MATRIX4X4_HH
54 
55 
56 //== INCLUDES =================================================================
57 
58 #include "VectorT.hh"
59 #include <math.h>
60 #include <iostream>
61 
62 
63 //== NAMESPACES ==============================================================
64 
65 
66 namespace ACG {
67 
68 
69 //== MACROS / HELPERS =========================================================
70 
71 
72 #define matrixOperator(src, dst, op) { \
73  Scalar *_a = dst;\
74  const Scalar *_b = src;\
75  *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
76  *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
77  *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; \
78  *_a++ op *_b++; *_a++ op *_b++; *_a++ op *_b++; *_a op *_b ; \
79 }
80 
81 #define MAT(m,r,c) ((m)[(r)+((c)<<2)])
82 
83 template<typename Scalar> inline bool checkEpsilon(Scalar x) {
84  return fabs(x) < (1e-6);
85 }
86 
87 template<> inline bool checkEpsilon(float x) {
88  return fabs(x) < (1e-4);
89 }
90 
91 
92 
93 //== CLASS DEFINITION =========================================================
94 
95 
99 template <class Scalar>
101 {
102 public:
103 
106 
108  template <class OtherScalar>
109  inline Matrix4x4T(const Matrix4x4T<OtherScalar> _rhs) {
110  operator=(_rhs);
111  }
112 
115  inline Matrix4x4T(const Scalar _array[16]) {
116  /*
117  * Why aren't we doing a memcpy here?
118  */
119  matrixOperator(_array, mat_, =);
120  }
121 
124 
125 
127  template<typename otherScalar>
129  for (int i=0; i<4; ++i)
130  for (int j=0; j<4; ++j)
131  operator()(i,j) = _rhs(i,j);
132  return *this;
133  }
134 
135 
136 
138  inline Scalar& operator()(unsigned int row, unsigned int col) {
139  return MAT(mat_,row,col);
140  }
141 
143  inline const Scalar& operator()(unsigned int row, unsigned int col) const {
144  return MAT(mat_,row,col);
145  }
146 
147 
149  inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
150  int i;
151  const Scalar *a = mat_;
152  const Scalar *b = _rhs.mat_;
153  for(i=0;i< 16;i++,a++,b++)
154  if(! checkEpsilon( *a - *b ))
155  return false;
156  return true;
157  }
158 
160  inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
161  return !( operator==(_rhs) );
162  }
163 
164 
166  inline Matrix4x4T operator+ (const Matrix4x4T<Scalar>& _rhs) const {
167  Matrix4x4T<Scalar> m(_rhs);
168  matrixOperator(mat_, m.mat_, += );
169  return m;
170  }
171 
173  inline Matrix4x4T operator- (const Matrix4x4T<Scalar>& _rhs) const {
174  Matrix4x4T<Scalar> m(*this);
175  matrixOperator(_rhs.mat_, m.mat_, -= );
176  return m;
177  }
178 
180  Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
181 
183  Matrix4x4T operator*(const Scalar& scalar);
184 
185 
187  inline Matrix4x4T& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
188  matrixOperator(_rhs.mat_, mat_, += );
189  return *this;
190  }
191 
193  inline Matrix4x4T& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
194  matrixOperator(_rhs.mat_, mat_, -= );
195  return *this;
196  }
197 
200 
202  Matrix4x4T& leftMult(const Matrix4x4T<Scalar>& _rhs);
203 
204 
206  template <typename T>
207  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
208 
210  template <typename T>
211  inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
212 
214  template <typename T>
215  inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
216 
218  inline void clear();
219 
221  inline void identity();
222 
223 
225  inline bool is_identity() const {
226  int i;
227  const Scalar *a = mat_;
228  Scalar b = 0.0;
229  for(i=0;i< 16;i++,a++,b++) {
230  if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
231  b = 1.0;
232  else
233  b = 0.0;
234  if(! checkEpsilon( *a - b ))
235  return false;
236  }
237  return true;
238  }
239 
240 
242  inline void transpose();
243 
244 
246  bool invert();
247 
248  Scalar determinant() const {
249  return mat_[12] * mat_[9] * mat_[6] * mat_[3] - mat_[8] * mat_[13] * mat_[6] * mat_[3] -
250  mat_[12] * mat_[5] * mat_[10] * mat_[3] + mat_[4] * mat_[13] * mat_[10] * mat_[3] +
251  mat_[8] * mat_[5] * mat_[14] * mat_[3] - mat_[4] * mat_[9] * mat_[14] * mat_[3] -
252  mat_[12] * mat_[9] * mat_[2] * mat_[7] + mat_[8] * mat_[13] * mat_[2] * mat_[7] +
253  mat_[12] * mat_[1] * mat_[10] * mat_[7] - mat_[0] * mat_[13] * mat_[10] * mat_[7] -
254  mat_[8] * mat_[1] * mat_[14] * mat_[7] + mat_[0] * mat_[9] * mat_[14] * mat_[7] +
255  mat_[12] * mat_[5] * mat_[2] * mat_[11] - mat_[4] * mat_[13] * mat_[2] * mat_[11] -
256  mat_[12] * mat_[1] * mat_[6] * mat_[11] + mat_[0] * mat_[13] * mat_[6] * mat_[11] +
257  mat_[4] * mat_[1] * mat_[14] * mat_[11] - mat_[0] * mat_[5] * mat_[14] * mat_[11] -
258  mat_[8] * mat_[5] * mat_[2] * mat_[15] + mat_[4] * mat_[9] * mat_[2] * mat_[15] +
259  mat_[8] * mat_[1] * mat_[6] * mat_[15] - mat_[0] * mat_[9] * mat_[6] * mat_[15] -
260  mat_[4] * mat_[1] * mat_[10] * mat_[15] + mat_[0] * mat_[5] * mat_[10] * mat_[15];
261  }
262 
263 
267  inline const Scalar* get_raw_data() const { return mat_; }
268  inline const Scalar* raw() const { return mat_; }
269  inline const Scalar* data() const { return mat_; }
270 
271 protected:
272 
273  Scalar mat_[16];
274 };
275 
276 
278 typedef Matrix4x4T<float> Matrix4x4f;
280 typedef Matrix4x4T<double> Matrix4x4d;
281 
282 
283 
284 
285 //== IO to/from streams =======================================================
286 
287 
289 template<typename Scalar>
290 inline std::ostream&
291 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
292 {
293  for(int i=0; i<4; i++)
294  {
295  for(int j=0; j<4; j++)
296  os << m(i,j) << " ";
297  os << "\n";
298  }
299  return os;
300 }
301 
302 
304 template<typename Scalar>
305 inline std::istream&
306 operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
307 {
308  for(int i=0; i<4; i++)
309  for(int j=0; j<4; j++)
310  is >> m(i,j);
311  return is;
312 }
313 
314 
315 //=============================================================================
316 
317 #undef matrixOperator
318 #undef MAT
319 
320 //=============================================================================
321 } // namespace ACG
322 //=============================================================================
323 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
324 #define ACG_MATRIX4X4_TEMPLATES
325 #include "Matrix4x4T.cc"
326 #endif
327 //=============================================================================
328 #endif // ACG_MATRIX4X4_HH defined
329 //=============================================================================
330 
bool operator!=(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices
Definition: Matrix4x4T.hh:160
void clear()
sets all elements to zero
Definition: Matrix4x4T.cc:233
~Matrix4x4T()
destructor
Definition: Matrix4x4T.hh:123
Matrix4x4T(const Matrix4x4T< OtherScalar > _rhs)
construct from other matrix type
Definition: Matrix4x4T.hh:109
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:187
Definition: Matrix4x4T.cc:65
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:78
void transpose()
transpose matrix
Definition: Matrix4x4T.cc:265
Matrix4x4T(const Scalar _array[16])
Definition: Matrix4x4T.hh:115
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x',y',z',0) = A * (x,y,z,0)
Definition: Matrix4x4T.cc:218
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:249
Matrix4x4T()
constructor: uninitialized values
Definition: Matrix4x4T.hh:105
bool invert()
matrix inversion (returns true on success)
Definition: Matrix4x4T.cc:290
Matrix4x4T operator+(const Matrix4x4T< Scalar > &_rhs) const
self + _rhs
Definition: Matrix4x4T.hh:166
bool operator==(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices (up to some epsilon)
Definition: Matrix4x4T.hh:149
Definition: Matrix4x4T.hh:100
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:193
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:108
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x',y',z',1) = M * (x,y,z,1)
Definition: Matrix4x4T.cc:195
bool is_identity() const
check if the matrix is the identity ( up to an epsilon )
Definition: Matrix4x4T.hh:225
Scalar & operator()(unsigned int row, unsigned int col)
access operator (read and write)
Definition: Matrix4x4T.hh:138
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:128
const Scalar & operator()(unsigned int row, unsigned int col) const
access operator (read only)
Definition: Matrix4x4T.hh:143
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:136
Matrix4x4T operator-(const Matrix4x4T< Scalar > &_rhs) const
self - _rhs
Definition: Matrix4x4T.hh:173
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:267