Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Matrix2x2.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 _Matrix2x2_hh_
26 #define _Matrix2x2_hh_
27 #include "bias_config.h"
28 
29 #include <Base/Debug/Error.hh>
30  //#include <Base/Debug/Debug.hh>
31 
32 #include "Matrix.hh"
33 
34 
35 namespace BIAS {
36 
37 /**
38  @class Matrix2x2
39  @ingroup g_math
40  @brief is a 'fixed size' quadratic matrix of dim.
41  2 x 2 which is templated over the elemnt-type.
42 **/
43 
44  // forward declaration to avoid nested includes
45  template <class T> class BIASMathBase_EXPORT Vector2;
46 
47  template<class T=double>
48  class BIASMathBase_EXPORT Matrix2x2 : public Matrix<T>
49  {
50  public:
51  inline Matrix2x2<T>() : Matrix<T>(2, 2) {};
52 
53  explicit Matrix2x2<T>(const MatrixInitType& i) : Matrix<T>(2, 2, T(0)) {
54  switch (i) {
55  case MatrixZero:return;
56  case MatrixIdentity:{
57  this->v_[0] = this->v_[3] = T(1); return;
58  }
59  default:
60  BIASERR("undefined MatrixInitType");
61  BIASABORT;
62  }
63  }
64 
65  // DEPRECATED due to instantiation problem JW
66  //explicit Matrix2x2<T>(const char *s) : Matrix<T>(2, 2, s) {};
67  explicit Matrix2x2<T>(const std::string & s);
68 
69 
70  /** @author Jan Woetzel
71  @status untested (04/17/2002) **/
72  Matrix2x2<T>(const Matrix2x2<T> & A) : Matrix<T>(A) {};
73 
74  /** cast constructor from TNT::Matrix */
75  Matrix2x2<T>(const TNT::Matrix<T>& A);
76 
77  /** Set the matrix to | a0 a1 |
78  | a2 a3 | */
80  const T a0, const T a1,
81  const T a2, const T a3);
82 
83  virtual ~Matrix2x2<T>();
84 
85  /** matrix - vector multiplicate this matrix with Vector2,
86  storing the result in destvec
87  calculates:
88  destvec = (this Matrix) * argvec
89  **/
90  inline void Mult(const Vector2<T> &argvec, Vector2<T> &destvec) const;
91 
92  /** matrix - vector multiplicate this matrix with Vector2,
93  storing the result in destvec
94  calculates:
95  destvec^T = argvec^T * (this Matrix)
96  **/
97  inline void MultLeft(const Vector2<T> &argvec, Vector2<T> &destvec) const;
98 
99  /** @brief transposed argvec multipied from left, wrapper function */
100  Vector2<T> MultLeft(const Vector2<T> &argvec) const {
101  Vector2<T> ret; MultLeft(argvec, ret); return ret;
102  }
103 
104 
105  /** matrix-matrix multiplication with other Matrix2x2,
106  storing the result in destmat
107  calculates:
108  destmat = (this mat) * argmat
109  **/
110  inline void Mult(const Matrix2x2<T> & argmat, Matrix2x2<T> & destmat)const;
111 
112  /** Multiplication in place with argmat */
113  inline void MultIP(const Matrix2x2<T> & argmat);
114 
115  /** assignment operator **/
116  Matrix2x2<T>& operator=(const Matrix2x2<T> &mat);
117 
118  /** returns transposed matrix*/
119  inline Matrix2x2<T> Transpose() const;
120 
121  /** set this as transposed arg */
122  inline void Transpose(const Matrix2x2<T>& arg);
123 
124  /** transpose matrix in place */
125  inline void TransposeIP();
126 
127  /** set the elements of this matrix to the identity matrix **/
128  inline void SetIdentity();
129 
130  /** set the elements of this matrix to zero **/
131  inline void SetZero();
132 
133  /// stl conform interface
134  inline void clear() {
135  SetZero();
136  };
137 
138 
139  /** calculate the determinante */
140  inline T det() const;
141 
142  /** just neccessary to avoid resizing of this 'fixed size' matrix because
143  it is derived from the resizable Matrix
144  should be removed when Matrix2x2 becomes a base class.
145  (04/17/2002) Felix Woelk **/
146  Matrix2x2 & newsize(int rows, int cols);
147 
148  /** analyticaly inverts matrix
149  @author woelk 12 2002 */
150  int Invert(Matrix2x2<T>& result) const;
151 
152  inline Matrix2x2<T> Invert() const
153  { Matrix2x2<T> res; Invert(res); return res; }
154 
155  /** Eigenvalue decomposition. The eigenvalues and eigenvectors of the
156  matrix are returned in value1/2 vector1/2.
157  @return number of linear independent eigenvectors = rank of matrix
158  @author woelk 06/2005 */
159  int EigenvalueDecomposition(T& value1, Vector2<T>& vector1,
160  T& value2, Vector2<T>& vector2) const;
161  }; // class
162 
163  template <class T>
164  inline void Matrix2x2<T>::Transpose(const Matrix2x2<T>& arg)
165  {
166  register T *dataP = this->GetData();
167  register const T *argP = arg.GetData();
168  dataP[0] = argP[0]; dataP[3] = argP[3];
169  dataP[1] = argP[2]; dataP[2] = argP[1];
170  }
171 
172  template <class T>
174  {
175  Matrix2x2 rs;
176  rs.Transpose(*this);
177  return rs;
178  }
179 
180  template <class T>
182  {
183  T tmp = this->v_[1];
184  this->v_[1] = this->v_[2];
185  this->v_[2] = tmp;
186  }
187 
188  template <class T>
189  inline void Matrix2x2<T>::Mult(const Matrix2x2<T> & argmat,
190  Matrix2x2<T> & destmat) const
191  {
192  register T *destP = destmat.GetData();
193  const register T *argP = argmat.GetData();
194  const register T *matP = this->GetData();
195 
196  destP[0] =
197  matP[0] * argP[0] + matP[1] * argP[2];
198  destP[1] =
199  matP[0] * argP[1] + matP[1] * argP[3];
200 
201  destP[2] =
202  matP[2] * argP[0] + matP[3] * argP[2];
203  destP[3] =
204  matP[2] * argP[1] + matP[3] * argP[3];
205 
206  }
207 
208  template <class T>
209  inline void Matrix2x2<T>::MultIP(const Matrix2x2<T> & argmat)
210  {
211  const register T *argP = argmat.GetData();
212  register T tmp = this->v_[0];
213 
214  this->v_[0] =
215  tmp * argP[0] + this->v_[1] * argP[2];
216  this->v_[1] =
217  tmp * argP[1] + this->v_[1] * argP[3];
218 
219  tmp = this->v_[2];
220  this->v_[2] =
221  this->v_[2] * argP[0] + this->v_[3] * argP[2];
222  this->v_[3] =
223  tmp * argP[1] + this->v_[3] * argP[3];
224 
225  }
226 
227  template <class T>
228  inline void Matrix2x2<T>::Mult(const Vector2<T> &argvec,
229  Vector2<T> &destvec) const
230  {
231  const register T *matP = this->GetData();
232  destvec[0] = argvec[0] * matP[0]
233  + argvec[1] * matP[1];
234  destvec[1] = argvec[0] * matP[2]
235  + argvec[1] * matP[3];
236  }
237 
238  template <class T>
239  inline void Matrix2x2<T>::MultLeft(const Vector2<T> &argvec,
240  Vector2<T> &destvec) const
241  {
242  const register T *matP = this->GetData();
243  destvec[0] = argvec[0] * matP[0]
244  + argvec[1] * matP[2];
245  destvec[1] = argvec[0] * matP[1]
246  + argvec[1] * matP[3];
247  }
248 
249 
250  template <class T>
252  register T* d = this->GetData(); // pointer to this matrix's data array
253  d[0] = d[3] = 1;
254  d[1] = d[2] = 0;
255  }
256 
257  template <class T>
258  inline void Matrix2x2<T>::SetZero() {
259  register T* d = this->GetData(); // pointer to this matrix's data array
260  d[0]=d[1]=d[2]=d[3] =0;
261  }
262 
263 
264 
265  template <class T>
266  inline T Matrix2x2<T>::det() const
267  {
268  register const T *dataP = this->GetData();
269  return (dataP[0] * dataP[3] - dataP[1] * dataP[2]);
270  }
271 
272 
273 } // end of namespace BIAS
274 
275 #include "Operators.hh"
276 
277 #endif // _Matrix2x2_hh_
void SetIdentity()
set the elements of this matrix to the identity matrix
Definition: Matrix2x2.hh:251
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
T det() const
calculate the determinante
Definition: Matrix2x2.hh:266
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix2x2.hh:48
void TransposeIP()
transpose matrix in place
Definition: Matrix2x2.hh:181
void Mult(const Vector2< T > &argvec, Vector2< T > &destvec) const
matrix - vector multiplicate this matrix with Vector2, storing the result in destvec calculates: dest...
Definition: Matrix2x2.hh:228
Matrix2x2< T > Transpose() const
returns transposed matrix
Definition: Matrix2x2.hh:173
class Vector2 contains a Vector of dim.
Definition: Vector2.hh:79
Matrix2x2< T > Invert() const
Definition: Matrix2x2.hh:152
void clear()
stl conform interface
Definition: Matrix2x2.hh:134
void MultLeft(const Vector2< T > &argvec, Vector2< T > &destvec) const
matrix - vector multiplicate this matrix with Vector2, storing the result in destvec calculates: dest...
Definition: Matrix2x2.hh:239
void MultIP(const Matrix2x2< T > &argmat)
Multiplication in place with argmat.
Definition: Matrix2x2.hh:209
T * GetData()
get the pointer to the data array of the matrix (for faster direct memeory access) ...
Definition: Matrix.hh:185
matrix class with arbitrary size, indexing is row major.
Vector2< T > MultLeft(const Vector2< T > &argvec) const
transposed argvec multipied from left, wrapper function
Definition: Matrix2x2.hh:100
void SetZero()
set the elements of this matrix to zero
Definition: Matrix2x2.hh:258