Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
PMatrixBase.cpp
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 #include "PMatrixBase.hh"
26 #include "RMatrixBase.hh"
27 #include <Base/Debug/Exception.hh>
28 #include <Base/Geometry/Quaternion.hh>
29 #include <Base/Math/BogIO.hh>
30 
31 using namespace BIAS;
32 using namespace std;
33 
35  const Matrix3x3<PMATRIX_TYPE> &R,
36  const Vector3<PMATRIX_TYPE> &C)
37 {
38  Matrix3x3<double> Rt, KRt;
39  Vector3<double> LastCol;
40 
41  Rt = R;
42  Rt.TransposeIP();
43  KRt=K*Rt;
44  // last column of P is: -R'C (docu; JW 11/2002)
45  KRt.Mult(C, LastCol);
46  LastCol.MultIP(-1.0);
47 
48  // fill the Matrix RtRtC = [R' | -R'C] (docu; JW 11/2002)
49  for (int row=0; row<3; row++) {
50  (*this)[row][0] = KRt[row][0];
51  (*this)[row][1] = KRt[row][1];
52  (*this)[row][2] = KRt[row][2];
53  (*this)[row][3] = LastCol[row];
54  }
55 }
56 
58  const Matrix3x3<PMATRIX_TYPE> &R,
59  const HomgPoint3D &C)
60 {
62  if (!C.IsAtInfinity()){
63  cnew.Set(C[0]/C[3], C[1]/C[3], C[2]/C[3]);
64  Compose(K, R, cnew);
65  } else {
66  BIASERR("cannot compose camera at infinity");
67  }
68 }
69 
71  const Vector3<PMATRIX_TYPE> &C)
72 {
74  K.SetIdentity();
75  Compose(K, R, C);
76 }
77 
79  const HomgPoint3D &C)
80 {
82  K.SetIdentity();
83  Compose(K, R, C);
84 }
85 
87  const Vector<double>& src,
88  const enum E_ParametrizationType param_type)
89 {
90  RMatrixBase R = Parametrization2R_(src, param_type);
91 
92  Vector3<double> C(src[0], src[1], src[2]);
93  Compose(K, R, C);
94 }
95 
98  const enum E_ParametrizationType param_type) const
99 {
100  RMatrixBase R;
101  switch (param_type){
102  case AXIS_TIMES_ANGLE:
103  {
104  if (src.Size()!=6){
105  BEXCEPTION("invalid src vector size "<<src.Size());
106  }
107  Vector3<double> axis_angle;
108  axis_angle.Set(src[3], src[4], src[5]);
109  R.SetFromAxisAngle(axis_angle);
110  }
111  break;
112  case AXIS_AND_ANGLE:
113  {
114  if (src.Size()!=7){
115  BEXCEPTION("invalid src vector size "<<src.Size());
116  }
117  Vector3<double> axis;
118  axis.Set(src[3], src[4], src[5]);
119  R.Set(axis, src[6]);
120  }
121  break;
122  case EULER:
123  if (src.Size()!=6){
124  BEXCEPTION("invalid src vector size "<<src.Size());
125  }
126  R.SetXYZ(src[3], src[4], src[5]);
127  break;
128  case QUATERNION:
129  {
130  if (src.Size()!=7){
131  BEXCEPTION("invalid src vector size "<<src.Size());
132  }
133  Quaternion<double> q(src[3], src[4], src[5], src[6]);
134  R.SetFromQuaternion(q);
135  }
136  break;
137  default:
138  BIASERR("unknown enum type "<<param_type);
139  BEXCEPTION("unkown enum type "<<param_type);
140  break;
141  }
142  return R;
143 }
144 
145 PMatrixBase &
146 PMatrixBase::newsize(int rows, int cols)
147 {
148  BIASERR("The size of a P-Matrix has to be 3x4. newsize can't create a "
149  << rows<<" x "<< cols << " PMatrixBase. aborting.");
150  exit(-1);
151  //return *this;
152 }
153 
154 /** Sets P-Matrix from a vector which contains the elements of P
155  row wise.*/
157 {
158  PMATRIX_TYPE *data = vals.GetData();
159  for (unsigned int i = 0 ; i < P_MATRIX_ROWS; i++)
160  for (unsigned int j = 0 ; j < P_MATRIX_COLS; j++)
161  (*this)[i][j]=*(data++);
162 }
163 
165 {
166  // prepare data
167  HomgPoint2D xx=x;
169  HomgPoint2D p4;
170 
171  for (int i=0;i<3;i++){
172  p4[i]=(*this)[i][3];
173  for (int j=0;j<3;j++)
174  M[i][j]=(*this)[i][j];
175  }
176 
177  // invert M
178  if (M.InvertIP() != 0) {
179  BIASERR("error: unable to invert M "<<M<<endl)
180  BIASABORT
181  }
182 
183  // perform transformation
184  xx*=mu;
185  xx-=p4;
186  xx = M*xx;
187 
188  // return result
189  return HomgPoint3D(xx[0], xx[1], xx[2], 1.0);
190 }
191 
192 int PMatrixBase::
193 LoadBOG(const std::string & filename)
194 {
198  int result=0;
199  if (0!=LoadBogK(filename, K)) result += -1;
200  if (0!=LoadBogRC(filename, R)) result += -2;
201  if (0!=C.LoadBogTC(filename)) result += -4;
202  Compose(K, R, C);
203  return result;
204 }
int LoadBogTC(const std::string &filename)
reads the TC part of a BOG file which is used by Daimler Chrysler for storing Camera center position...
Definition: Vector3.cpp:161
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
void SetXYZ(ROTATION_MATRIX_TYPE PhiX, ROTATION_MATRIX_TYPE PhiY, ROTATION_MATRIX_TYPE PhiZ)
Set Euler angles (in rad) in order XYZ.
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
int LoadBogK(const std::string &filename, Matrix3x3< T > &mat)
Definition: BogIO.hh:91
int SetFromQuaternion(const Quaternion< ROTATION_MATRIX_TYPE > &q)
Set rotation matrix from a quaternion.
unsigned int Size() const
length of the vector
Definition: Vector.hh:143
E_ParametrizationType
description of supported parametrization types
Definition: PMatrixBase.hh:166
void Set(const Vector3< ROTATION_MATRIX_TYPE > &w, const ROTATION_MATRIX_TYPE phi)
Set from rotation axis w and angle phi (in rad)
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
RMatrixBase Parametrization2R_(const Vector< double > &parametrization, const enum E_ParametrizationType param_type) const
Definition: PMatrixBase.cpp:97
void SetFromVector(const Vector< PMATRIX_TYPE > &vals)
Sets P-Matrix from a vector which contains the elements of P row wise.
bool IsAtInfinity() const
Definition: HomgPoint3D.hh:398
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
int LoadBOG(const std::string &filename)
Load a Daimler .bog fiel and compose a P matrix from it.
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
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
void SetFromAxisAngle(Vector3< ROTATION_MATRIX_TYPE > w)
Set from rotation axis * angle (modified Rodrigues vector)
T * GetData() const
get the pointer to the data array of the vector (for faster direct memory access) ...
Definition: Vector.hh:219
HomgPoint3D Backproject(const HomgPoint2D &x, double mu=1)
Back projects image point x to 3D space and returns ray on which the 3D space point lies...
void MultIP(const T &scalar)
Definition: Vector3.hh:327
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
int InvertIP()
In place matrix conversion.
Definition: Matrix3x3.cpp:420
int LoadBogRC(const std::string &filename, Matrix3x3< T > &mat)
Definition: BogIO.hh:17
PMatrixBase & newsize(int rows, int cols)
setting a new size different from 3x4 is not allowed for the fixed size P-Matrix overloads the genera...
class BIASGeometryBase_EXPORT HomgPoint3D
void SetIdentity()
set the elements of this matrix to the identity matrix (possibly overriding the inherited method) ...
Definition: Matrix3x3.hh:429