Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Matrix4x4.hh
1 /*
2 This file is part of the BIAS library (Basic ImageAlgorithmS).
3 
4 Copyright (C) 2003-2009 (see file CONTACT for details)
5 Multimediale Systeme der Informationsverarbeitung
6 Institut fuer Informatik
7 Christian-Albrechts-Universitaet Kiel
8 
9 
10 BIAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
14 
15 BIAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU Lesser General Public License for more details.
19 
20 You should have received a copy of the GNU Lesser General Public License
21 along with BIAS; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 */
24 
25 #ifndef _BIAS_Matrix4x4_hh_
26 #define _BIAS_Matrix4x4_hh_
27 #include "bias_config.h"
28 
29 #include <Base/Debug/Error.hh>
30 
31 #include "Matrix.hh"
32 #include "Matrix3x3.hh"
33 #include "Vector3.hh"
34 #include "Vector4.hh"
35 
36 
37 namespace BIAS {
38 
39  /**
40  @class Matrix4x4
41  @ingroup g_math
42  @brief is a 'fixed size' quadratic matrix of dim. 4 x 4 which is templated
43  over the element-type
44 
45  @author Jan Woetzel, (04/17/2002)
46  **/
47 
48  // forward declaration to avoid mutual inclusion
49  //template <class T> class BIASMathBase_EXPORT Matrix3x3;
50  //template <class T> class BIASMathBase_EXPORT Vector4;
51  //template <class T> class BIASMathBase_EXPORT Vector3;
52 
53  template <class T=double>
54  class BIASMathBase_EXPORT Matrix4x4 : public Matrix<T>
55  {
56  public:
57 
58  /// constructors
59  /// call the corresponding constructors of the base class if appropriate
60  ///
61 
62  /** default constructor
63  **/
64  inline Matrix4x4<T>() : Matrix<T>(4, 4) {};
65 
66  // DEPRECATED due to instantiation problem JW
67  //explicit Matrix4x4<T>(const char *s) : Matrix<T>(4, 4, s) {};
68  /// JW
69  explicit Matrix4x4<T>(const std::string & s);
70 
71 
72  /** Set the matrix values JW */
74  const T a0, const T a1, const T a2, const T a3,
75  const T a4, const T a5, const T a6, const T a7,
76  const T a8, const T a9, const T a10, const T a11,
77  const T a12, const T a13, const T a14, const T a15 );
78 
79  /** @brief init matrix with identity or zero
80  @author koeser */
81  explicit Matrix4x4<T>(const MatrixInitType& i) : Matrix<T>(4, 4, T(0)) {
82  switch (i) {
83  case MatrixZero:return;
84  case MatrixIdentity:{
85  this->v_[0] = this->v_[5] = this->v_[10] = this->v_[15] = T(1);
86  return;
87  }
88  default:
89  BIASERR("undefined MatrixInitType");
90  BIASABORT;
91  }
92  }
93 
94 
95  Matrix4x4<T>(const Matrix4x4<T> & A) : Matrix<T>(A) {};
96 
97  Matrix4x4<T>(const Matrix<T>& arg);
98 
99  /** cast constructor from TNT::Matrix */
100  Matrix4x4<T>(const TNT::Matrix<T>& A);
101 
102  /** Set as Rotation and Translation for similarity transforms
103  @author grest, Dec. 2004
104  */
105  Matrix4x4<T>(const Matrix3x3<T> & R,
106  const Vector3<T> &trans);
107 
108 
109  /** destructor
110  @status untested (04/17/2002)
111  **/
112  virtual ~Matrix4x4<T>();
113 
114 
115  /** matrix - vector multiplicate this matrix with Vector4,
116  storing the result in destvec, calculates:
117  destvec = (this Matrix) * argvec
118  @author Jan Woetzel
119  @status untested (04/17/2002)
120  **/
121  inline void Mult( const Vector4<T> &argvec, Vector4<T> &destvec) const {
122  // direct access to the matrix values using fixed indicies:
123  // 0 1 2 3 0
124  // 4 5 6 7 * 1
125  // 8 9 10 11 2
126  // 12 13 14 15 3
127 
128  // pointer to the beginning of the data array of this matrix
129  const register T *matP = this->GetData();
130 
131  // first row:
132  destvec[0] =
133  matP[0] * argvec[0]
134  +matP[1] * argvec[1]
135  +matP[2] * argvec[2]
136  +matP[3] * argvec[3];
137  // second row:
138  destvec[1] =
139  matP[4] * argvec[0]
140  +matP[5] * argvec[1]
141  +matP[6] * argvec[2]
142  +matP[7] * argvec[3];
143  // third row:
144  destvec[2] =
145  matP[8] * argvec[0]
146  +matP[9] * argvec[1]
147  +matP[10] * argvec[2]
148  +matP[11] * argvec[3];
149  // fourth row:
150  destvec[3] =
151  matP[12] * argvec[0]
152  +matP[13] * argvec[1]
153  +matP[14] * argvec[2]
154  +matP[15] * argvec[3];
155  };
156 
157 
158 
159 
160  /** calculate the determinant recursively using sub matrices of size 3x3
161  @author koeser 10/2003
162  @status experimental, slow implementation, not yet heavily tested
163  */
164  inline T det() const {
165  T d = 0;
166  Matrix3x3<T> SubMatrix;
167  for (register unsigned int sub=0; sub<4; sub++) {
168  // first column is used,
169  // set up the 3x3 submatrices from the right matrix part
170  // this is done 4 times (loop counter="sub")
171  for (register unsigned int i=0; i<3; i++) {
172  // construct the sub matrix under inspection,
173  // skip the first column
174  SubMatrix[0][i] = (*this)[(sub+1)%4][i+1];
175  SubMatrix[1][i] = (*this)[(sub+2)%4][i+1];
176  SubMatrix[2][i] = (*this)[(sub+3)%4][i+1];
177  }
178  // add value of subdeterminant to overall sum
179  d += SubMatrix.GetDeterminant() * (*this)[sub][0];
180  }
181  return d;
182  }
183 
184  /** matrix-matrix multiplication with other Matrix4x4,
185  storing the result in destmat
186  using 'manual inlining'
187  @author Jan Woetzel
188  @status untested (04/17/2002)
189  **/
190  inline void Mult(const Matrix4x4<T> & argmat, Matrix4x4<T> & destmat)const{
191  //
192  // direct access using fixed indicies with matrix order:
193  // (this matrix) * arg
194  //
195  // 0 1 2 3 0 1 2 3
196  // 4 5 6 7 * 4 5 6 7
197  // 8 9 10 11 8 9 10 11
198  // 12 13 14 15 12 13 14 15
199 
200  // pointer to the destination matrix's data array
201  register T *destP = destmat.GetData();
202  // pointer to the argument (other) matrix's data array
203  const register T *argP = argmat.GetData();
204  // pointer to this matrix's data array
205  const register T *matP = this->GetData();
206 
207  // first row:
208  destP[0] =
209  matP[0]*argP[0] + matP[1]*argP[4] + matP[2]*argP[8] + matP[3]*argP[12];
210  destP[1] =
211  matP[0]*argP[1] + matP[1]*argP[5] + matP[2]*argP[9] + matP[3]*argP[13];
212  destP[2] =
213  matP[0]*argP[2] + matP[1]*argP[6] + matP[2]*argP[10] + matP[3]*argP[14];
214  destP[3] =
215  matP[0]*argP[3] + matP[1]*argP[7] + matP[2]*argP[11] + matP[3]*argP[15];
216  // second row:
217  destP[4] =
218  matP[4]*argP[0] + matP[5]*argP[4] + matP[6]*argP[8] + matP[7]*argP[12];
219  destP[5] =
220  matP[4]*argP[1] + matP[5]*argP[5] + matP[6]*argP[9] + matP[7]*argP[13];
221  destP[6] =
222  matP[4]*argP[2] + matP[5]*argP[6] + matP[6]*argP[10] + matP[7]*argP[14];
223  destP[7] =
224  matP[4]*argP[3] + matP[5]*argP[7] + matP[6]*argP[11] + matP[7]*argP[15];
225  // third row:
226  destP[8] =
227  matP[8]*argP[0] + matP[9]*argP[4] + matP[10]*argP[8] + matP[11]*argP[12];
228  destP[9] =
229  matP[8]*argP[1] + matP[9]*argP[5] + matP[10]*argP[9] + matP[11]*argP[13];
230  destP[10] =
231  matP[8]*argP[2] + matP[9]*argP[6] + matP[10]*argP[10] + matP[11]*argP[14];
232  destP[11] =
233  matP[8]*argP[3] + matP[9]*argP[7] + matP[10]*argP[11] + matP[11]*argP[15];
234  // fourth row:
235  destP[12] =
236  matP[12]*argP[0] + matP[13]*argP[4] + matP[14]*argP[8] + matP[15]*argP[12];
237  destP[13] =
238  matP[12]*argP[1] + matP[13]*argP[5] + matP[14]*argP[9] + matP[15]*argP[13];
239  destP[14] =
240  matP[12]*argP[2] + matP[13]*argP[6] + matP[14]*argP[10] + matP[15]*argP[14];
241  destP[15] =
242  matP[12]*argP[3] + matP[13]*argP[7] + matP[14]*argP[11] + matP[15]*argP[15];
243  }
244 
245 
246  /** assignment operators
247  calling corresponding operator from base class "TNT::Matrix" if appropriate
248  @author Jan Woetzel
249  @status untested (02/25/2002)
250  **/
251  Matrix4x4 & operator = (const Matrix<T> &mat);
252 
253 
254  /** set the elements of this matrix to the identity matrix
255  (posisbly overriding the inherited method)
256  @author , Jan Woetzel
257  @date 04/17/2002
258  @status untested
259  **/
260  inline void SetIdentity() {
261  register T* d = this->GetData(); // pointer to this matrix's data array
262  d[0] = 1;
263  d[1] = 0;
264  d[2] = 0;
265  d[3] = 0;
266  d[4] = 0;
267  d[5] = 1;
268  d[6] = 0;
269  d[7] = 0;
270  d[8] = 0;
271  d[9] = 0;
272  d[10] = 1;
273  d[11] = 0;
274  d[12] = 0;
275  d[13] = 0;
276  d[14] = 0;
277  d[15] = 1;
278  }
279 
280  /// stl conform interface
281  inline void clear() {
282  this->SetZero();
283  };
284 
285 
286  /** just neccessary to avoid resizing of this 'fixed size' matrix because it is derived from the resizable Matrix
287  Should be removed if Matrix4x4 becomes a base class.
288  (04/17/2002) Jan Woetzel
289  **/
290  Matrix4x4 & newsize(int rows, int cols);
291 
292 
293  /** tranpose this matrix "in place"
294  example:
295  0 1 2 3 --> 0 4 8 12
296  4 5 6 7 --> 1 5 9 13
297  8 9 10 11 --> 2 6 10 14
298  12 13 14 15 --> 3 7 11 15
299  @author Jan-Friso Evers-Senne
300  @status untested (07/08/2002)
301  **/
302  inline void TransposeIP() {
303  register T *dataP = this->GetData(); // pointer to the data array
304  register T tmp;
305 #define swap(a,b,tmp) tmp=a;a=b;b=tmp;
306  swap(dataP[1],dataP[4],tmp);
307  swap(dataP[2],dataP[8],tmp);
308  swap(dataP[12],dataP[3],tmp);
309  swap(dataP[13],dataP[7],tmp);
310  swap(dataP[14],dataP[11],tmp);
311  swap(dataP[9],dataP[6],tmp);
312 #undef swap
313  }
314 
315  inline Matrix4x4 Transpose() const {
316  Matrix4x4 tmp = (*this);
317  tmp.TransposeIP();
318  return tmp;
319  }
320 
321 
322 
323  }; // class Matrix4x4
324 } // namespace BIAS
325 
326 #include "Operators.hh"
327 
328 #endif
void TransposeIP()
tranpose this matrix &quot;in place&quot; example: 0 1 2 3 –&gt; 0 4 8 12 4 5 6 7 –&gt; 1 5 9 13 8 9 10 11 –&gt; 2 6 10...
Definition: Matrix4x4.hh:302
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
void Mult(const Matrix4x4< T > &argmat, Matrix4x4< T > &destmat) const
matrix-matrix multiplication with other Matrix4x4, storing the result in destmat using &#39;manual inli...
Definition: Matrix4x4.hh:190
T det() const
calculate the determinant recursively using sub matrices of size 3x3
Definition: Matrix4x4.hh:164
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
void Mult(const Vector4< T > &argvec, Vector4< T > &destvec) const
matrix - vector multiplicate this matrix with Vector4, storing the result in destvec, calculates: destvec = (this Matrix) * argvec
Definition: Matrix4x4.hh:121
Matrix4x4 Transpose() const
Definition: Matrix4x4.hh:315
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
matrix class with arbitrary size, indexing is row major.
void SetIdentity()
set the elements of this matrix to the identity matrix (posisbly overriding the inherited method) ...
Definition: Matrix4x4.hh:260
T GetDeterminant() const
returns the Determinant |A| of this
Definition: Matrix3x3.cpp:347
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
void clear()
stl conform interface
Definition: Matrix4x4.hh:281