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 
45 
46 //=============================================================================
47 //
48 // CLASS Matrix4x4T
49 //
50 //=============================================================================
51 
52 
53 #ifndef ACG_MATRIX4X4_HH
54 #define ACG_MATRIX4X4_HH
55 
56 
57 //== INCLUDES =================================================================
58 
59 #include "VectorT.hh"
60 #include <cmath>
61 #include "../Config/ACGDefines.hh"
62 #include <iostream>
63 #include <algorithm>
64 #include <functional>
65 
66 
67 //== NAMESPACES ==============================================================
68 
69 
70 namespace ACG {
71 
72 
73 //== MACROS / HELPERS =========================================================
74 
75 template<typename Scalar> inline bool checkEpsilon(Scalar x) {
76  return fabs(x) < (1e-6);
77 }
78 
79 template<> inline bool checkEpsilon(float x) {
80  return fabs(x) < (1e-4);
81 }
82 
83 
84 
85 //== CLASS DEFINITION =========================================================
86 
87 
91 template <class Scalar>
93 {
94 public:
95 
98 
100  template <class OtherScalar>
101  inline Matrix4x4T(const Matrix4x4T<OtherScalar>& _rhs) {
102  operator=(_rhs);
103  }
104 
107  inline Matrix4x4T(const Scalar _array[16]) {
108  std::copy(_array,_array+16, mat_);
109  }
110 
113 
114 
116  template<typename otherScalar>
118  // produces warning C4244 on msvc (implicit cast double to float)
119 // std::copy(_rhs.data(),_rhs.data()+16,mat_);
120  for (int i = 0; i < 16; ++i)
121  mat_[i] = Scalar(_rhs.data()[i]);
122 
123  return *this;
124  }
125 
126 
127 
129  inline Scalar& operator()(unsigned int row, unsigned int col) {
130  return mat_[(row)+((col)<<2)];
131  }
132 
134  inline const Scalar& operator()(unsigned int row, unsigned int col) const {
135  return mat_[(row)+((col)<<2)];
136  }
137 
138 
140  inline bool operator== (const Matrix4x4T<Scalar>& _rhs) const {
141  int i;
142  const Scalar *a = mat_;
143  const Scalar *b = _rhs.mat_;
144  for(i=0;i< 16;i++,a++,b++)
145  if(! checkEpsilon( *a - *b ))
146  return false;
147  return true;
148  }
149 
151  inline bool operator!= (const Matrix4x4T<Scalar>& _rhs) const {
152  return !( operator==(_rhs) );
153  }
154 
155 
158  std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::plus<Scalar>());
159  return _rhs;
160  }
161 
164  std::transform(mat_,mat_+16, _rhs.mat_, _rhs.mat_, std::minus<Scalar>());
165  return _rhs;
166  }
167 
169  Matrix4x4T operator*(const Matrix4x4T<Scalar>& inst) const;
170 
172  Matrix4x4T operator*(const Scalar& scalar);
173 
174 
176  inline Matrix4x4T& operator+= ( const Matrix4x4T<Scalar>& _rhs) {
177  std::transform(mat_,mat_+16,_rhs.mat_, mat_, std::plus<Scalar>());
178  return *this;
179  }
180 
182  inline Matrix4x4T& operator-= ( const Matrix4x4T<Scalar>& _rhs) {
183  std::transform(mat_,mat_+16, _rhs.mat_, mat_, std::minus<Scalar>());
184  return *this;
185  }
186 
189 
191  Matrix4x4T& leftMult(const Matrix4x4T<Scalar>& _rhs);
192 
193 
195  template <typename T>
196  inline VectorT<T,4> operator*(const VectorT<T,4>& _v) const;
197 
199  template <typename T>
200  inline VectorT<T,3> transform_point(const VectorT<T,3>& _v) const;
201 
203  template <typename T>
204  inline VectorT<T,3> transform_vector(const VectorT<T,3>& _v) const;
205 
207  inline void clear();
208 
210  inline void identity();
211 
212 
214  inline bool is_identity() const {
215  int i;
216  const Scalar *a = mat_;
217  Scalar b = 0.0;
218  for(i=0;i< 16;i++,a++,b++) {
219  if ( ( i == 0) || ( i == 5 ) || ( i == 10 ) || ( i == 15 ) )
220  b = 1.0;
221  else
222  b = 0.0;
223  if(! checkEpsilon( *a - b ))
224  return false;
225  }
226  return true;
227  }
228 
229 
231  inline void transpose();
232 
233 
235  bool invert();
236 
237  Scalar determinant() const {
238  return mat_[12] * mat_[9] * mat_[6] * mat_[3] - mat_[8] * mat_[13] * mat_[6] * mat_[3] -
239  mat_[12] * mat_[5] * mat_[10] * mat_[3] + mat_[4] * mat_[13] * mat_[10] * mat_[3] +
240  mat_[8] * mat_[5] * mat_[14] * mat_[3] - mat_[4] * mat_[9] * mat_[14] * mat_[3] -
241  mat_[12] * mat_[9] * mat_[2] * mat_[7] + mat_[8] * mat_[13] * mat_[2] * mat_[7] +
242  mat_[12] * mat_[1] * mat_[10] * mat_[7] - mat_[0] * mat_[13] * mat_[10] * mat_[7] -
243  mat_[8] * mat_[1] * mat_[14] * mat_[7] + mat_[0] * mat_[9] * mat_[14] * mat_[7] +
244  mat_[12] * mat_[5] * mat_[2] * mat_[11] - mat_[4] * mat_[13] * mat_[2] * mat_[11] -
245  mat_[12] * mat_[1] * mat_[6] * mat_[11] + mat_[0] * mat_[13] * mat_[6] * mat_[11] +
246  mat_[4] * mat_[1] * mat_[14] * mat_[11] - mat_[0] * mat_[5] * mat_[14] * mat_[11] -
247  mat_[8] * mat_[5] * mat_[2] * mat_[15] + mat_[4] * mat_[9] * mat_[2] * mat_[15] +
248  mat_[8] * mat_[1] * mat_[6] * mat_[15] - mat_[0] * mat_[9] * mat_[6] * mat_[15] -
249  mat_[4] * mat_[1] * mat_[10] * mat_[15] + mat_[0] * mat_[5] * mat_[10] * mat_[15];
250  }
251 
252 
256  inline const Scalar* get_raw_data() const { return mat_; }
257  inline const Scalar* raw() const { return mat_; }
258  inline const Scalar* data() const { return mat_; }
259 
260 protected:
261 
262  Scalar mat_[16];
263 };
264 
265 
270 
271 
272 
273 
274 //== IO to/from streams =======================================================
275 
276 
278 template<typename Scalar>
279 inline std::ostream&
280 operator<<(std::ostream& os, const Matrix4x4T<Scalar>& m)
281 {
282  for(int i=0; i<4; i++)
283  {
284  for(int j=0; j<4; j++)
285  os << m(i,j) << " ";
286  os << "\n";
287  }
288  return os;
289 }
290 
291 
293 template<typename Scalar>
294 inline std::istream&
295 operator>>(std::istream& is, Matrix4x4T<Scalar>& m)
296 {
297  for(int i=0; i<4; i++)
298  for(int j=0; j<4; j++)
299  is >> m(i,j);
300  return is;
301 }
302 
303 //=============================================================================
304 } // namespace ACG
305 //=============================================================================
306 #if defined(INCLUDE_TEMPLATES) && !defined(ACG_MATRIX4X4_C)
307 #define ACG_MATRIX4X4_TEMPLATES
308 #include "Matrix4x4T_impl.hh"
309 #endif
310 //=============================================================================
311 #endif // ACG_MATRIX4X4_HH defined
312 //=============================================================================
313 
Matrix4x4T< float > Matrix4x4f
typedef
Definition: Matrix4x4T.hh:267
std::istream & operator>>(std::istream &is, Matrix4x4T< Scalar > &m)
read the space-separated components of a vector from a stream */
Definition: Matrix4x4T.hh:295
Matrix4x4T operator-(Matrix4x4T< Scalar > _rhs) const
self - _rhs
Definition: Matrix4x4T.hh:163
void clear()
sets all elements to zero
~Matrix4x4T()
destructor
Definition: Matrix4x4T.hh:112
Matrix4x4T & operator+=(const Matrix4x4T< Scalar > &_rhs)
self += _rhs
Definition: Matrix4x4T.hh:176
Namespace providing different geometric functions concerning angles.
void transpose()
transpose matrix
bool is_identity() const
check if the matrix is the identity ( up to an epsilon )
Definition: Matrix4x4T.hh:214
Matrix4x4T< double > Matrix4x4d
typedef
Definition: Matrix4x4T.hh:269
Matrix4x4T operator+(Matrix4x4T< Scalar > _rhs) const
self + _rhs
Definition: Matrix4x4T.hh:157
Matrix4x4T(const Scalar _array[16])
Definition: Matrix4x4T.hh:107
void identity()
setup an identity matrix
bool operator!=(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices
Definition: Matrix4x4T.hh:151
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)
Matrix4x4T()
constructor: uninitialized values
Definition: Matrix4x4T.hh:97
bool operator==(const Matrix4x4T< Scalar > &_rhs) const
compare two matrices (up to some epsilon)
Definition: Matrix4x4T.hh:140
bool invert()
matrix inversion (returns true on success)
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)
const Scalar & operator()(unsigned int row, unsigned int col) const
access operator (read only)
Definition: Matrix4x4T.hh:134
Matrix4x4T & operator-=(const Matrix4x4T< Scalar > &_rhs)
self -= _rhs
Definition: Matrix4x4T.hh:182
Matrix4x4T & operator*=(const Matrix4x4T< Scalar > &_rhs)
self *= _rhs
Scalar & operator()(unsigned int row, unsigned int col)
access operator (read and write)
Definition: Matrix4x4T.hh:129
Matrix4x4T< Scalar > & operator=(const Matrix4x4T< otherScalar > &_rhs)
assignment from other matrix type
Definition: Matrix4x4T.hh:117
Matrix4x4T operator*(const Matrix4x4T< Scalar > &inst) const
self * _rhs
Matrix4x4T & leftMult(const Matrix4x4T< Scalar > &_rhs)
multiply from left: self = _rhs * self
const Scalar * get_raw_data() const
Definition: Matrix4x4T.hh:256
Matrix4x4T(const Matrix4x4T< OtherScalar > &_rhs)
construct from other matrix type
Definition: Matrix4x4T.hh:101