Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Matrix3x3.hh
1 /* This file is part of the BIAS library (Basic ImageAlgorithmS).
2 
3  Copyright (C) 2003-2009 (see file CONTACT for details)
4  Multimediale Systeme der Informationsverarbeitung
5  Institut fuer Informatik
6  Christian-Albrechts-Universitaet Kiel
7 
8 
9  BIAS is free software; you can redistribute it and/or modify
10  it under the terms of the GNU Lesser General Public License as published by
11  the Free Software Foundation; either version 2.1 of the License, or
12  (at your option) any later version.
13 
14  BIAS is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public License
20  along with BIAS; if not, write to the Free Software
21  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
22 
23 #ifndef _Matrix3x3_hh_
24 #define _Matrix3x3_hh_
25 #include "bias_config.h"
26 
27 #include <Base/Debug/Error.hh>
28 
29 #include <Base/Math/Vector3.hh>
30 
31 #include <iostream>
32 #include <string>
33 #include <limits>
34 
35 namespace BIAS {
36 
37  template <class T> class Matrix;
38  template <class T> class Matrix3x3;
39  template <class T> class Matrix3x4;
40 
41  template <class T>
42  std::ostream& operator<<(std::ostream& os, const Matrix3x3<T>& mat);
43 
44  template <class T>
45  std::istream& operator>>(std::istream& is, Matrix3x3<T>& mat);
46 
47  /** @class Matrix3x3
48  @ingroup g_math
49  @brief is a 'fixed size' quadratic matrix of dim. 3 x 3 which is
50  templated over the element-type.
51 
52  complete rewrite of class with fixed internal memory, to avoid
53  calling of allocator new[]
54 
55  @author woelk 11/2007 (c) www.vision-n.de */
56  template <class T=double>
57  class BIASMathBase_EXPORT Matrix3x3
58  {
59  public:
60  /** default constructor */
61  inline Matrix3x3<T>() {}
62 
63  //explicit Matrix3x3<T>(const std::string & s);
64 
65  /// constructor from 9 values
66  Matrix3x3<T>(const T a0, const T a1, const T a2,
67  const T a3, const T a4, const T a5,
68  const T a6, const T a7, const T a8);
69 
70  /** @brief init matrix with identity or zero
71  @author koeser */
72  explicit Matrix3x3<T>(const MatrixInitType& i);
73 
74  /** copy constructor **/
75  Matrix3x3<T>(const Matrix3x3<T>& A);
76 
77  /** cast constructor from Matrix */
78  Matrix3x3<T>(const Matrix<T>& A);
79 
80  /** cast constructor from TNT::Matrix */
81  Matrix3x3<T>(const TNT::Matrix<T>& A);
82 
83  /** destructor */
84  virtual ~Matrix3x3<T>();
85 
86  /** assignment operator
87  @author woelk 11/2007 (c) www.vision-n.de **/
88  Matrix3x3<T>& operator=(const Matrix3x3<T>& mat);
89 
90  /// @author woelk 11/2007 (c) www.vision-n.de
91  inline T* operator[](const unsigned row)
92  { return Data_+3*row; }
93 
94  /// @author woelk 11/2007 (c) www.vision-n.de
95  inline const T* operator[](const unsigned row) const
96  { return Data_+3*row; }
97 
98  /// @author woelk 11/2007 (c) www.vision-n.de
99  inline T* GetData()
100  { return Data_; }
101 
102  /// @author woelk 11/2007 (c) www.vision-n.de
103  inline const T* GetData() const
104  { return Data_; }
105 
106  /// @author woelk 11/2007 (c) www.vision-n.de
107  inline unsigned GetNumElements() const
108  { return 9u; }
109 
110  /// @author woelk 11/2007 (c) www.vision-n.de
111  inline void SetZero();
112 
113  /** set the elements of this matrix to the identity matrix
114  (possibly overriding the inherited method)
115  @author Ingo Thomsen, Jan Woetzel
116  @date 04/17/2002
117  @status untested **/
118  inline void SetIdentity();
119 
120  /** matrix - vector multiplicate this matrix with Vector3,
121  storing the result in destvec
122  calculates:
123  destvec = (this Matrix) * argvec
124  @author Ingo Thomsen, Jan Woetzel
125  @status untested (04/17/2002)**/
126  inline void Mult(const Vector3<T> &argvec, Vector3<T> &destvec) const;
127 
128  /** multiplies matrix from left with transposed argvec, resulting
129  in transposed destvec
130  @author woelk 05/2003 */
131  inline
132  void TransposedMult(const Vector3<T> &argvec, Vector3<T> &destvec) const;
133 
134  /** matrix-matrix multiplication with other Matrix3x3,
135  storing the result in destmat
136  calculates:
137  destmat = (this mat) * argmat
138  @author Ingo Thomsen, Jan Woetzel
139  @status untested (04/17/2002)**/
140  inline void Mult(const Matrix3x3<T> & argmat,Matrix3x3<T> & destmat) const;
141 
142  /// woelk 11/2007 (c) www.vision-n.de
143  inline Matrix3x3<T>& operator*=(const Matrix3x3<T> & arg)
144  { Matrix3x3<T> tmp(*this); tmp.Mult(arg, *this); return *this; }
145 
146  /** @author woelk 06 2003 */
147  void Mult(const Matrix3x4<T> & argmat,Matrix3x4<T> & destmat) const;
148 
149  /** scalar-matrix multiplication */
150  inline void Scale(const T &scalar, Matrix3x3<T> & destmat) const;
151 
152  /// woelk 11/2007 (c) www.vision-n.de
153  Matrix3x3<T>& operator*=(const T& arg);
154 
155  /// @author woelk 11/2007 (c) www.vision-n.de
156  Matrix3x3<T>& operator/=(const T& arg);
157 
158  /// @author woelk 11/2007 (c) www.vision-n.de
159  Matrix3x3<T>& operator+=(const Matrix3x3<T>& arg);
160 
161  /// @author woelk 11/2007 (c) www.vision-n.de
162  Matrix3x3<T>& operator-=(const Matrix3x3<T>& arg);
163 
164  /** tranpose this matrix "in place"
165  example:
166  0 1 2 --> 0 3 6
167  3 4 5 --> 1 4 7
168  6 7 8 --> 2 5 8 */
169  inline void TransposeIP();
170 
171  /** returns transposed matrix
172  tested 12.06.2002
173  @author Felix Woelk */
174  Matrix3x3<T> Transpose() const;
175 
176  /** sets this as transposed arg, fw */
177  inline void Transpose(const Matrix3x3<T>& arg);
178 
179  /** sets the diagonalelements of this 3x3 Matrix
180  rowwise with the values of the 9 (x1) vector
181  @author Ingo Thomsen, Jan Woetzel **/
182  void SetFromVector(const TNT::Vector<T> & vec);
183 
184  /** set this matrix from 3 vectors each representating a column*/
185  void SetFromColumnVectors(const BIAS::Vector3<T> & v0,
186  const BIAS::Vector3<T> & v1,
187  const BIAS::Vector3<T> & v2);
188 
189  /** set this matrix from 3 vectors, each representating a row */
190  void SetFromRowVectors(const BIAS::Vector3<T> & v0,
191  const BIAS::Vector3<T> & v1,
192  const BIAS::Vector3<T> & v2);
193 
194  /** extract one row ('Zeile') from ths matrix (for convenience) */
195  void GetRow(const unsigned int row, Vector3<T>& r) const;
196  inline Vector3<T> GetRow(const unsigned int row) const
197  { BIAS::Vector3<T> m; GetRow(row, m); return m; }
198 
199  /** extract one column ('Spalte') from this matrix (for convenience) */
200  void GetColumn(const unsigned int col, Vector3<T>& r) const;
201 
202  BIAS::Vector3<T> GetColumn(const unsigned int col) const
203  { BIAS::Vector3<T> m; GetColumn(col, m); return m; }
204 
205  /// @author woelk 11/2007 (c) www.vision-n.de
206  void SetColumn(const unsigned int col, const Vector3<T>& c);
207 
208  /// @author woelk 11/2007 (c) www.vision-n.de
209  void SetRow(const unsigned int row, const Vector3<T>& r);
210 
211  /** Sets matrix from vector as cross product matrix of this vector. */
212  void SetAsCrossProductMatrix(const Vector3<T> &vec);
213 
214  /** Sets matrix from vector as cross product matrix of a vector.
215  with components (x, y, z) */
216  void SetAsCrossProductMatrix(const T &x, const T &y, const T &z);
217 
218  /** In place matrix conversion.
219  Returns -1 if determinant is zero, 0 on success.
220  @author woelk 07/2005 */
221  int InvertIP();
222 
223  /** Matrix inversion: inverts this and stores resulty in argument inv.
224  Returns -1 if determinant is zero, 0 on success.
225  @author woelk 07/2005 */
226  int GetInverse(Matrix3x3<T>& inv) const;
227 
228  /** return the trace of the matrix
229  @author woelk 04/2006 */
230  inline T Trace() const
231  { const register T *p = this->GetData(); return p[0] + p[4] + p[8]; }
232 
233  /** returns the Determinant |A| of this
234  @author grest */
235  T GetDeterminant() const;
236 
237  /** return biggest and smallest entry
238  @author woelk 08/2004 */
239  void GetMaxMin(T& max, T&min) const;
240 
241  /// @author woelk 11/2007 (c) www.vision-n.de
242  void GetAbsMaxMin(T& max, T&min) const;
243 
244  /// @author woelk 11/2007 (c) www.vision-n.de
245  T GetMax() const;
246 
247  /// @author woelk 11/2007 (c) www.vision-n.de
248  T GetMin() const;
249 
250  inline T NormL1() const;
251 
252  /// @author woelk 11/2007 (c) www.vision-n.de
253  inline double NormFrobenius() const;
254 
255  inline double NormL2() const
256  { return NormFrobenius(); }
257 
258  /** divide this by biggest absolute entry,
259  returns biggest entry
260  @author woelk 11/2007 (c) www.vision-n.de */
261  T Normalize();
262 
263  /// @author woelk 11/2007 (c) www.vision-n.de
264  void MakeSymmetric();
265 
266  /// @author woelk 11/2007 (c) www.vision-n.de
267  bool IsZero(const T eps = std::numeric_limits<T>::epsilon()) const;
268 
269  /// @author woelk 12/2007 (c) www.vision-n.de
270  bool IsIdentity(const T eps = std::numeric_limits<T>::epsilon()) const;
271 
272  bool operator==(const Matrix3x3<T>& arg) const;
273 
274  bool operator!=(const Matrix3x3<T>& arg) const
275  { return !operator==(arg); }
276 
277  bool Load(const std::string& fname);
278 
279  bool Save(const std::string& fname) const;
280 
281  protected:
282  T Data_[9];
283 
284  }; // class Matrix3x3 --------------------------------------
285 
286 
287  ////////////////////////////////////////////////////////////////
288  // inline implementations
289  ////////////////////////////////////////////////////////////////
290 
291 
292  template <class T>
293  void Matrix3x3<T>::
295  {
296  memset((void *)Data_, 0, 9*sizeof(T));
297  }
298 
299 
300  template <class T>
301  void Matrix3x3<T>::
302  Mult(const Vector3<T> &argvec, Vector3<T> &destvec) const
303  {
304  // direct access to the matrix values using fixed indicies:
305  // 0 1 2 0
306  // 3 4 5 * 1
307  // 6 7 8 2
308  const register T *matP = this->GetData();
309  destvec[0] = argvec[0] * matP[0]
310  + argvec[1] * matP[1]
311  + argvec[2] * matP[2];
312  destvec[1] = argvec[0] * matP[3]
313  + argvec[1] * matP[4]
314  + argvec[2] * matP[5];
315  destvec[2] = argvec[0] * matP[6]
316  + argvec[1] * matP[7]
317  + argvec[2] * matP[8];
318  }
319 
320 
321  template <class T>
322  void Matrix3x3<T>::
323  TransposedMult(const Vector3<T> &argvec, Vector3<T> &destvec) const
324  {
325  // direct access to the matrix values using fixed indicies:
326  // 0 1 2 0
327  // 3 4 5 * 1
328  // 6 7 8 2
329  const register T *matP = this->GetData();
330  destvec[0] = argvec[0] * matP[0]
331  + argvec[1] * matP[3]
332  + argvec[2] * matP[6];
333  destvec[1] = argvec[0] * matP[1]
334  + argvec[1] * matP[4]
335  + argvec[2] * matP[7];
336  destvec[2] = argvec[0] * matP[2]
337  + argvec[1] * matP[5]
338  + argvec[2] * matP[8];
339  }
340 
341 
342  template <class T>
343  void Matrix3x3<T>::
344  Mult(const Matrix3x3<T> &argmat, Matrix3x3<T> &destmat) const
345  {
346  //
347  // direct access using fixed indicies with matrix order:
348  // 0 1 2
349  // 3 4 5
350  // 6 7 8
351  register T *destP = destmat.GetData();
352  const register T *argP = argmat.GetData();
353  const register T *matP = this->GetData();
354  // does not work for in place multiplication
355  BIASASSERT(destP!=argP);
356  BIASASSERT(destP!=matP);
357 
358  destP[0] = matP[0] * argP[0] + matP[1] * argP[3] + matP[2] * argP[6];
359  destP[1] = matP[0] * argP[1] + matP[1] * argP[4] + matP[2] * argP[7];
360  destP[2] = matP[0] * argP[2] + matP[1] * argP[5] + matP[2] * argP[8];
361 
362  destP[3] = matP[3] * argP[0] + matP[4] * argP[3] + matP[5] * argP[6];
363  destP[4] = matP[3] * argP[1] + matP[4] * argP[4] + matP[5] * argP[7];
364  destP[5] = matP[3] * argP[2] + matP[4] * argP[5] + matP[5] * argP[8];
365 
366  destP[6] = matP[6] * argP[0] + matP[7] * argP[3] + matP[8] * argP[6];
367  destP[7] = matP[6] * argP[1] + matP[7] * argP[4] + matP[8] * argP[7];
368  destP[8] = matP[6] * argP[2] + matP[7] * argP[5] + matP[8] * argP[8];
369  }
370 
371 
372  template <class T>
373  void Matrix3x3<T>::
374  Scale(const T &scalar, Matrix3x3<T> &destmat) const
375  {
376  register T *dest = destmat.GetData();
377  const register T *src = this->GetData();
378  for (int i=0; i<9; i++)
379  dest[i] = scalar * src[i];
380  }
381 
382 
383  template <class T>
384  void Matrix3x3<T>::
386  {
387  // swap the 1,2,5 with the 3,6,7
388  // 0 1 2
389  // 3 4 5
390  // 6 7 8
391  register T *dataP = this->GetData(); // pointer to the data array
392  register T tmp;
393  // swap 1,3:
394  tmp = dataP[1];
395  dataP[1] = dataP[3];
396  dataP[3] = tmp;
397  // swap 2,6:
398  tmp = dataP[2];
399  dataP[2] = dataP[6];
400  dataP[6] = tmp;
401  // swap 5,7:
402  tmp = dataP[5];
403  dataP[5] = dataP[7];
404  dataP[7] = tmp;
405  }
406 
407 
408  template <class T>
409  void Matrix3x3<T>::
411  {
412  register const T *argp = arg.GetData();
413  this->Data_[0] = argp[0];
414  this->Data_[4] = argp[4];
415  this->Data_[8] = argp[8];
416 
417  this->Data_[1] = argp[3];
418  this->Data_[2] = argp[6];
419  this->Data_[3] = argp[1];
420 
421  this->Data_[5] = argp[7];
422  this->Data_[6] = argp[2];
423  this->Data_[7] = argp[5];
424  }
425 
426 
427  template <class T>
428  void Matrix3x3<T>::
430  {
431  register T* d = this->GetData(); // pointer to this matrix's data array
432  d[0] = 1;
433  d[1] = 0;
434  d[2] = 0;
435  d[3] = 0;
436  d[4] = 1;
437  d[5] = 0;
438  d[6] = 0;
439  d[7] = 0;
440  d[8] = 1;
441  }
442 
443 
444  template<class T>
445  double Matrix3x3<T>::
447  {
448  double result = 0;
449  for (int i=0; i<9; i++){
450  result += (double)(Data_[i]) * (double)(Data_[i]);
451  }
452  return sqrt(result);
453  }
454 
455  template<class T>
457  NormL1() const
458  {
459  T result = 0;
460  for (int i=0; i<9; i++){
461  result += (Data_[i]>0)?(Data_[i]):(-(Data_[i]));
462  }
463  return result;
464  }
465 
466 
467 
468  /////////////////////////////////////////////////////////////////////////
469  // non member functions
470  /////////////////////////////////////////////////////////////////////////
471 
472  /** @relates Matrix3x3 */
473  template<class T>
474  Matrix3x3<T> operator/(const Matrix3x3<T>& mat, const T& arg)
475  { Matrix3x3<T> res(mat); res/=arg; return res; }
476 
477  /** @relates Matrix3x3 */
478  template<class T>
479  Matrix3x3<T> operator*(const Matrix3x3<T>& mat, const T& arg)
480  { Matrix3x3<T> res(mat); res*=arg; return res; }
481 
482  /** @relates Matrix3x3 */
483  template<class T>
484  Matrix3x3<T> operator*(const T& arg, const Matrix3x3<T>& mat)
485  { Matrix3x3<T> res(mat); res*=arg; return res; }
486 
487 
488 } // end of namespace BIAS
489 
490 
491 #include "Operators.hh"
492 
493 #endif
494 
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
unsigned GetNumElements() const
Definition: Matrix3x3.hh:107
void TransposeIP()
tranpose this matrix &quot;in place&quot; example: 0 1 2 –&gt; 0 3 6 3 4 5 –&gt; 1 4 7 6 7 8 –&gt; 2 5 8 ...
Definition: Matrix3x3.hh:385
const T * operator[](const unsigned row) const
Definition: Matrix3x3.hh:95
void TransposedMult(const Vector3< T > &argvec, Vector3< T > &destvec) const
multiplies matrix from left with transposed argvec, resulting in transposed destvec ...
Definition: Matrix3x3.hh:323
T * operator[](const unsigned row)
Definition: Matrix3x3.hh:91
const T * GetData() const
Definition: Matrix3x3.hh:103
T NormL1() const
Definition: Matrix3x3.hh:457
BIASMathBase_EXPORT Vector2< T > & operator/=(Vector2< T > &vec, const Vector2< T > &argvec)
elementwise division
Definition: Vector2.hh:846
T Trace() const
return the trace of the matrix
Definition: Matrix3x3.hh:230
Matrix3x3< T > operator*(const T &arg, const Matrix3x3< T > &mat)
Definition: Matrix3x3.hh:484
Vector2< T > & operator-=(Vector2< T > &vec, const Vector2< T > &argvec)
sub operator for two Vectors
Definition: Vector2.hh:839
class BIASMathBase_EXPORT Matrix
Definition: Operators.hh:37
Matrix3x3< T > operator*(const Matrix3x3< T > &mat, const T &arg)
Definition: Matrix3x3.hh:479
double NormFrobenius() const
Definition: Matrix3x3.hh:446
void Mult(const Vector3< T > &argvec, Vector3< T > &destvec) const
matrix - vector multiplicate this matrix with Vector3, storing the result in destvec calculates: dest...
Definition: Matrix3x3.hh:302
BIASMathBase_EXPORT Vector2< T > & operator+=(Vector2< T > &vec, const Vector2< T > &argvec)
add operator for two Vectors
Definition: Vector2.hh:830
Vector2< T > & operator*=(Vector2< T > &vec, const T &scalar)
Multiplication operator with scalar argument.
Definition: Vector2.hh:873
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
BIAS::Vector3< T > GetColumn(const unsigned int col) const
Definition: Matrix3x3.hh:202
Matrix3x3< T > & operator*=(const Matrix3x3< T > &arg)
woelk 11/2007 (c) www.vision-n.de
Definition: Matrix3x3.hh:143
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
class BIASMathBase_EXPORT Matrix3x3
matrix class with arbitrary size, indexing is row major.
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
double NormL2() const
Definition: Matrix3x3.hh:255
bool operator!=(const Matrix3x3< T > &arg) const
Definition: Matrix3x3.hh:274
void Scale(const T &scalar, Matrix3x3< T > &destmat) const
scalar-matrix multiplication
Definition: Matrix3x3.hh:374
Matrix3x3< T > operator/(const Matrix3x3< T > &mat, const T &arg)
Definition: Matrix3x3.hh:474
is a &#39;fixed size&#39; rectangular matrix of dim.
Definition: Matrix3x3.hh:39
Vector3< T > GetRow(const unsigned int row) const
Definition: Matrix3x3.hh:196
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429