Developer Documentation
Matrix4x4T.hh
1 /*===========================================================================*\
2  * *
3  * OpenFlipper *
4  * Copyright (c) 2001-2015, RWTH-Aachen University *
5  * Department of Computer Graphics and Multimedia *
6  * All rights reserved. *
7  * www.openflipper.org *
8  * *
9  *---------------------------------------------------------------------------*
10  * This file is part of OpenFlipper. *
11  *---------------------------------------------------------------------------*
12  * *
13  * Redistribution and use in source and binary forms, with or without *
14  * modification, are permitted provided that the following conditions *
15  * are met: *
16  * *
17  * 1. Redistributions of source code must retain the above copyright notice, *
18  * this list of conditions and the following disclaimer. *
19  * *
20  * 2. Redistributions in binary form must reproduce the above copyright *
21  * notice, this list of conditions and the following disclaimer in the *
22  * documentation and/or other materials provided with the distribution. *
23  * *
24  * 3. Neither the name of the copyright holder nor the names of its *
25  * contributors may be used to endorse or promote products derived from *
26  * this software without specific prior written permission. *
27  * *
28  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS *
29  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *
30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A *
31  * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *
32  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, *
33  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, *
34  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR *
35  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF *
36  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING *
37  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS *
38  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. *
39  * *
40 \*===========================================================================*/
41 
42 /*===========================================================================*\
43  * *
44  * $Revision$ *
45  * $Author$ *
46  * $Date$ *
47  * *
48 \*===========================================================================*/
49 
50 
51 
52 //=============================================================================
53 //
54 // CLASS Matrix4x4T
55 //
56 //=============================================================================
57 
58 
59 #ifndef ACG_MATRIX4X4_HH
60 #define ACG_MATRIX4X4_HH
61 
62 
63 //== INCLUDES =================================================================
64 
65 #include "VectorT.hh"
66 #include <cmath>
67 #include "../Config/ACGDefines.hh"
68 #include <iostream>
69 #include <algorithm>
70 #include <functional>
71 
72 
73 //== NAMESPACES ==============================================================
74 
75 
76 namespace ACG {
77 
78 
79 //== MACROS / HELPERS =========================================================
80 
81 template<typename Scalar> inline bool checkEpsilon(Scalar x) {
82  return fabs(x) < (1e-6);
83 }
84 
85 template<> inline bool checkEpsilon(float x) {
86  return fabs(x) < (1e-4);
87 }
88 
89 
90 
91 //== CLASS DEFINITION =========================================================
92 
93 
97 template <class Scalar>
99 {
100 public:
101 
104 
106  template <class OtherScalar>
107  inline Matrix4x4T(const Matrix4x4T<OtherScalar>& _rhs) {
108  operator=(_rhs);
109  }
110 
113  inline Matrix4x4T(const Scalar _array[16]) {
114  std::copy(_array,_array+16, mat_);
115  }
116 
119 
120 
122  template<typename otherScalar>
124  // produces warning C4244 on msvc (implicit cast double to float)
125 // std::copy(_rhs.data(),_rhs.data()+16,mat_);
126  for (int i = 0; i < 16; ++i)
127  mat_[i] = Scalar(_rhs.data()[i]);
128 
129  return *this;
130  }
131 
132 
133 
135  inline Scalar& operator()(unsigned int row, unsigned int col) {
136  return mat_[(row)+((col)<<2)];
137  }
138 
140  inline const Scalar& operator()(unsigned int row, unsigned int col) const {
141  return mat_[(row)+((col)<<2)];
142  }
143 
144 
146  inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
147  int i;
148  const Scalar *a = mat_;
149  const Scalar *b = _rhs.mat_;
150  for(i=0;i< 16;i++,a++,b++)
151  if(! checkEpsilon( *a - *b ))
152  return false;
153  return true;
154  }
155 
157  inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
158  return !( operator==(_rhs) );
159  }
160 
161 
164  std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::plus<Scalar>());
165  return _rhs;
166  }
167 
170  std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::minus<Scalar>());
171  return _rhs;
172  }
173 
175  Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
176 
178  Matrix4x4T operator*(const Scalar& scalar);
179 
180 
182  inline Matrix4x4T& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
183  std::transform(mat_,mat_+16,_rhs.mat_, mat_, std::plus<Scalar>());
184  return *this;
185  }
186 
188  inline Matrix4x4T& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
189  std::transform(mat_,mat_+16, _rhs.mat_, mat_, std::minus<Scalar>());
190  return *this;
191  }
192 
195 
197  Matrix4x4T& leftMult(const Matrix4x4T<Scalar>& _rhs);
198 
199 
201  template <typename T>
202  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
203 
205  template <typename T>
206  inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
207 
209  template <typename T>
210  inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
211 
213  inline void clear();
214 
216  inline void identity();
217 
218 
220  inline bool is_identity() const {
221  int i;
222  const Scalar *a = mat_;
223  Scalar b = 0.0;
224  for(i=0;i< 16;i++,a++,b++) {
225  if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
226  b = 1.0;
227  else
228  b = 0.0;
229  if(! checkEpsilon( *a - b ))
230  return false;
231  }
232  return true;
233  }
234 
235 
237  inline void transpose();
238 
239 
241  bool invert();
242 
243  Scalar determinant() const {
244  return mat_[12] * mat_[9] * mat_[6] * mat_[3] - mat_[8] * mat_[13] * mat_[6] * mat_[3] -
245  mat_[12] * mat_[5] * mat_[10] * mat_[3] + mat_[4] * mat_[13] * mat_[10] * mat_[3] +
246  mat_[8] * mat_[5] * mat_[14] * mat_[3] - mat_[4] * mat_[9] * mat_[14] * mat_[3] -
247  mat_[12] * mat_[9] * mat_[2] * mat_[7] + mat_[8] * mat_[13] * mat_[2] * mat_[7] +
248  mat_[12] * mat_[1] * mat_[10] * mat_[7] - mat_[0] * mat_[13] * mat_[10] * mat_[7] -
249  mat_[8] * mat_[1] * mat_[14] * mat_[7] + mat_[0] * mat_[9] * mat_[14] * mat_[7] +
250  mat_[12] * mat_[5] * mat_[2] * mat_[11] - mat_[4] * mat_[13] * mat_[2] * mat_[11] -
251  mat_[12] * mat_[1] * mat_[6] * mat_[11] + mat_[0] * mat_[13] * mat_[6] * mat_[11] +
252  mat_[4] * mat_[1] * mat_[14] * mat_[11] - mat_[0] * mat_[5] * mat_[14] * mat_[11] -
253  mat_[8] * mat_[5] * mat_[2] * mat_[15] + mat_[4] * mat_[9] * mat_[2] * mat_[15] +
254  mat_[8] * mat_[1] * mat_[6] * mat_[15] - mat_[0] * mat_[9] * mat_[6] * mat_[15] -
255  mat_[4] * mat_[1] * mat_[10] * mat_[15] + mat_[0] * mat_[5] * mat_[10] * mat_[15];
256  }
257 
258 
262  inline const Scalar* get_raw_data() const { return mat_; }
263  inline const Scalar* raw() const { return mat_; }
264  inline const Scalar* data() const { return mat_; }
265 
266 protected:
267 
268  Scalar mat_[16];
269 };
270 
271 
276 
277 
278 
279 
280 //== IO to/from streams =======================================================
281 
282 
284 template<typename Scalar>
285 inline std::ostream&
286 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
287 {
288  for(int i=0; i<4; i++)
289  {
290  for(int j=0; j<4; j++)
291  os << m(i,j) << " ";
292  os << "\n";
293  }
294  return os;
295 }
296 
297 
299 template<typename Scalar>
300 inline std::istream&
301 operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
302 {
303  for(int i=0; i<4; i++)
304  for(int j=0; j<4; j++)
305  is >> m(i,j);
306  return is;
307 }
308 
309 //=============================================================================
310 } // namespace ACG
311 //=============================================================================
312 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
313 #define ACG_MATRIX4X4_TEMPLATES
314 #include "Matrix4x4T.cc"
315 #endif
316 //=============================================================================
317 #endif // ACG_MATRIX4X4_HH defined
318 //=============================================================================
319 
Matrix4x4T(const Scalar _array[16])
Definition: Matrix4x4T.hh:113
void transpose()
transpose matrix
Definition: Matrix4x4T.cc:272
VectorT< T, 3 > transform_point(const VectorT< T, 3 > &_v) const
transform point (x&#39;,y&#39;,z&#39;,1) = M * (x,y,z,1)
Definition: Matrix4x4T.cc:202
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Definition: Matrix4x4T.cc:115
const Scalar & operator()(unsigned int row, unsigned int col) const
access operator (read only)
Definition: Matrix4x4T.hh:140
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:182
bool is_identity() const
check if the matrix is the identity ( up to an epsilon )
Definition: Matrix4x4T.hh:220
void identity()
setup an identity matrix
Definition: Matrix4x4T.cc:256
std::istream & operator>>(std::istream &is, Matrix4x4T< Scalar > &m)
read the space-separated components of a vector from a stream */
Definition: Matrix4x4T.hh:301
bool operator!=(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices
Definition: Matrix4x4T.hh:157
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
Definition: Matrix4x4T.cc:143
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:188
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:262
Namespace providing different geometric functions concerning angles.
Definition: DBSCANT.cc:51
Matrix4x4T()
constructor: uninitialized values
Definition: Matrix4x4T.hh:103
Matrix4x4T< double > Matrix4x4d
typedef
Definition: Matrix4x4T.hh:275
bool operator==(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices (up to some epsilon)
Definition: Matrix4x4T.hh:146
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:123
bool invert()
matrix inversion (returns true on success)
Definition: Matrix4x4T.cc:297
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Definition: Matrix4x4T.cc:85
Matrix4x4T< float > Matrix4x4f
typedef
Definition: Matrix4x4T.hh:273
Matrix4x4T operator+(Matrix4x4T< Scalar > _rhs) const
self + _rhs
Definition: Matrix4x4T.hh:163
~Matrix4x4T()
destructor
Definition: Matrix4x4T.hh:118
Matrix4x4T(const Matrix4x4T< OtherScalar > &_rhs)
construct from other matrix type
Definition: Matrix4x4T.hh:107
Matrix4x4T operator-(Matrix4x4T< Scalar > _rhs) const
self - _rhs
Definition: Matrix4x4T.hh:169
Scalar & operator()(unsigned int row, unsigned int col)
access operator (read and write)
Definition: Matrix4x4T.hh:135
void clear()
sets all elements to zero
Definition: Matrix4x4T.cc:240
VectorT< T, 3 > transform_vector(const VectorT< T, 3 > &_v) const
transform vector (x&#39;,y&#39;,z&#39;,0) = A * (x,y,z,0)
Definition: Matrix4x4T.cc:225