Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMatrixBase.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 
26 #ifndef _BIAS_PMatrix_hh_
27 #define _BIAS_PMatrix_hh_
28 
29 #include "bias_config.h"
30 
31 #include <Base/Debug/Debug.hh>
32 #include <Base/Math/Matrix3x4.hh>
33 #include <Base/Math/Vector4.hh>
34 #include <Base/Math/Vector3.hh>
35 #include <Base/Math/Vector.hh>
36 #include <Base/Math/Matrix3x3.hh>
37 #include <Base/Math/Matrix4x4.hh>
38 #include "HomgLine2D.hh"
39 #include "HomgPoint3D.hh"
40  //#include "HomgLine3D.hh"
41 
42 // dimension of P matrix:
43 #define P_MATRIX_ROWS 3
44 #define P_MATRIX_COLS 4
45 
46 // element type:
47 #define PMATRIX_TYPE double
48 
49 #define PMATRIX_EPSILON 1e-14
50 namespace BIAS {
51 
52  class BIASGeometryBase_EXPORT RMatrixBase;
53 
54 /** @class PMatrixBase
55  @ingroup g_geometry
56  @brief describes a projective 3D -> 2D mapping in homogenous coordinates
57 
58  Matrix P is a 3x4 matrix (3 rows {Zeilen}, 4 columns{Spalten})
59  describing a mapping between a 3D projective point in space and the
60  corresponding projective 2D point (in an image plane).
61 
62  With R' = transpose of R, K = Kamera calibration matrix and
63  C = Kamera center is:
64  P = (K R' | -K R' C)
65  This means that R is the rotation from camera to world coo, and
66  R' = R.Transpose() the rotation from world to camera coo.
67 
68  Only implementation very specific to PMatrix
69  (and not valid for general matrices, e.g. like decomposition)
70  should be implemented here.
71  @author Jan Woetzel
72  @status alpha (02/26/2002) **/
73 
74  class BIASGeometryBase_EXPORT PMatrixBase : public Matrix3x4<PMATRIX_TYPE> {
75 
76  public:
77 
78  /** default constructor **/
79  inline PMatrixBase() : Matrix3x4<PMATRIX_TYPE>()
80  {}
81 
82  /** constructor setting identity or zero
83  @brief author koeser **/
84  explicit inline PMatrixBase(const MatrixInitType& i)
85  : Matrix3x4<PMATRIX_TYPE>(i)
86  {}
87 
88  explicit inline PMatrixBase(const std::string & s)
89  : Matrix3x4<PMATRIX_TYPE>(s)
90  {}
91 
92  /** @author Jan Woetzel **/
93  inline PMatrixBase(const PMatrixBase & A) : Matrix3x4<PMATRIX_TYPE>(A)
94  {}
95 
96  /** @author Jan Woetzel **/
97  inline PMatrixBase(const Matrix3x4<PMATRIX_TYPE> & Amat_ )
98  : Matrix3x4<PMATRIX_TYPE>(Amat_)
99  {}
100 
101  /** @author Jan Woetzel
102  not fine, should no be here, better cast stepwise between direct
103  father and child **/
104  explicit inline PMatrixBase(const Matrix<PMATRIX_TYPE>& Amat);
105 
106  /** Take R, K, C and compose PMatrixBase
107  @author Jan-Friso Evers-Senne
108  @date 2002-08-20 */
109  inline
111  const Matrix3x3<PMATRIX_TYPE> &R,
112  const Vector3<PMATRIX_TYPE> &C);
113 
114  /** assume K=Identity and compose P
115  @author woelk 08/2004 */
116  inline PMatrixBase(const Matrix3x3<PMATRIX_TYPE> &R,
117  const Vector3<PMATRIX_TYPE> &C);
118 
119  /** destructor **/
120  virtual inline ~PMatrixBase()
121  {}
122 
123  /** setting a new size different from 3x4 is not allowed for the fixed
124  size P-Matrix overloads the general newsize from basec class
125  @author Jan Woetzel
126  @status alpha (02/25/2002) **/
127  PMatrixBase &newsize(int rows, int cols);
128 
129  inline PMatrixBase& operator=(const PMatrixBase &mat);
130 
131  /** Convert the PMatrixBase to 'identity' matrix, overriding the Matrix3x4
132  method:
133  1 0 0 0
134  0 1 0 0
135  0 0 1 0
136  @author Ingo Thomsen
137  @date 04/11/2002
138  @status tested **/
139  inline void set_identity();
140 
141  /** Sets P-Matrix from a vector which contains the elements of P
142  row wise.*/
143  void SetFromVector(const Vector<PMATRIX_TYPE>& vals);
144 
145  /** composes this from K, R and C
146  using P = [ K R' | -K R' C ]
147  with R' = transpose(R)
148  @author Felix Woelk */
149  void Compose(const Matrix3x3<PMATRIX_TYPE> &K,
150  const Matrix3x3<PMATRIX_TYPE> &R,
151  const Vector3<PMATRIX_TYPE> &C);
152 
153  void Compose(const Matrix3x3<PMATRIX_TYPE> &K,
154  const Matrix3x3<PMATRIX_TYPE> &R,
155  const HomgPoint3D &C);
156 
157  /// calibrated case, uses K=Identity
158  void Compose(const Matrix3x3<PMATRIX_TYPE> &R,
159  const Vector3<PMATRIX_TYPE> &C);
160 
161  /// calibrated case, uses K=Identity
162  void Compose(const Matrix3x3<PMATRIX_TYPE> &R,
163  const HomgPoint3D &C);
164 
165  /// description of supported parametrization types
166  enum E_ParametrizationType { AXIS_TIMES_ANGLE, AXIS_AND_ANGLE,
167  EULER, QUATERNION };
168 
169  /** Compose a projection matrix using a parametrization. The supported
170  parametrization vector always starts with the camera center followed
171  by the camera orientation. Currently Euler angles,
172  axis (norm 1) and angle, axis times angle and quaternion are supported
173  as orientation parametrization.
174  @author woelk 02/2006 */
175  void Compose(const Matrix3x3<PMATRIX_TYPE> &K,
176  const Vector<double>& parametrization,
177  const enum E_ParametrizationType param_type);
178 
179  /** Back projects image point x to 3D space and returns ray on which the 3D
180  space point lies.
181  @note Inverted matrix is computed on each call.
182  @author jmf */
183  HomgPoint3D Backproject(const HomgPoint2D& x, double mu=1);
184 
185  /** Load a Daimler .bog fiel and compose a P matrix from it.
186  @author Jan Woetzel 03/2005 */
187  int LoadBOG(const std::string & filename);
188 
189  protected:
190  /// @author woelk 03/2006
191  RMatrixBase Parametrization2R_(const Vector<double>& parametrization,
192  const enum E_ParametrizationType param_type)const;
193 
194  }; // class PMatrixBase
195 
196  ////////////////////////////////////////////////////////////////////////
197  // implementation
198  ////////////////////////////////////////////////////////////////////////
199 
200  inline
202  const Matrix3x3<PMATRIX_TYPE> &R,
203  const Vector3<PMATRIX_TYPE> &C)
204  {
205  Compose(K, R, C);
206  }
207 
209  const Vector3<PMATRIX_TYPE> &C)
210  { Compose(R, C); }
211 
213  register PMATRIX_TYPE* d = GetData();
214  d[0] = d[5] = d[10] = 1;
215  d[1] = d[2] = d[3] = d[4] = d[6] = d[7] = d[8] = d[9] = d[11] = 0;
216  }
217 
219  {
220  if ((Amat.num_rows()==3) && (Amat.num_cols()==4)) {
221  //(is slow, better use memcopy or even better 'grandpa'-constructor.
222  for (int index=0; index<12; index++ ) {
223  this->GetData()[index] = Amat.GetData()[index];
224  };
225  } else {
226  BIASERR(" wrong size in constructor! Amat is a "<<Amat.num_rows()
227  <<" x "<<Amat.num_cols()<<" matrix but must be 3x4."
228  <<std::endl );
229  exit(1);
230  };
231  }
232 
235  return *this;
236  }
237 
238 } // end namespace
239 
240 #endif // _BIAS_PMatrixBase_hh_
PMatrixBase & operator=(const PMatrixBase &mat)
Definition: PMatrixBase.hh:233
MatrixInitType
can be passed to matrix constructors to init the matrix with the most often used values ...
Definition: Matrix.hh:59
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
Subscript num_cols() const
Definition: cmat.h:320
class for column vectors with arbitrary size
PMatrixBase(const Matrix3x4< PMATRIX_TYPE > &Amat_)
Definition: PMatrixBase.hh:97
PMatrixBase()
default constructor
Definition: PMatrixBase.hh:79
E_ParametrizationType
description of supported parametrization types
Definition: PMatrixBase.hh:166
Matrix< T > & operator=(const TNT::Matrix< T > &mat)
assignment operators calling corresponding operator from base class if appropriate ...
Definition: Matrix.hh:1101
PMatrixBase(const MatrixInitType &i)
constructor setting identity or zero
Definition: PMatrixBase.hh:84
virtual ~PMatrixBase()
destructor
Definition: PMatrixBase.hh:120
PMatrixBase(const PMatrixBase &A)
Definition: PMatrixBase.hh:93
PMATRIX_TYPE * 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 HomgPoint3D describes a point with 3 degrees of freedom in projective coordinates.
Definition: HomgPoint3D.hh:61
describes a projective 3D -&gt; 2D mapping in homogenous coordinates
Definition: PMatrixBase.hh:74
void set_identity()
Convert the PMatrixBase to &#39;identity&#39; matrix, overriding the Matrix3x4 method: 1 0 0 0 0 1 0 0 0 0 1 ...
Definition: PMatrixBase.hh:212
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
void Compose(const Matrix3x3< PMATRIX_TYPE > &K, const Matrix3x3< PMATRIX_TYPE > &R, const Vector3< PMATRIX_TYPE > &C)
composes this from K, R and C using P = [ K R&#39; | -K R&#39; C ] with R&#39; = transpose(R) ...
Definition: PMatrixBase.cpp:34
Subscript num_rows() const
Definition: cmat.h:319
is a &#39;fixed size&#39; rectangular matrix of dim.
Definition: Matrix3x3.hh:39
PMatrixBase(const std::string &s)
Definition: PMatrixBase.hh:88