Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Matrix3x4.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 
26 #include "Matrix3x4.hh"
27 // STD
28 #include <iostream>
29 #include <fstream>
30 #include <sstream>
31 #include <string>
32 
33 
34 using namespace BIAS;
35 using namespace std;
36 
37 namespace BIAS {
38 
39 template <class T>
41 
42 
43 template<class T>
45  const T a0, const T a1, const T a2, const T a3,
46  const T a4, const T a5, const T a6, const T a7,
47  const T a8, const T a9, const T a10, const T a11 )
48  : Matrix<T>(3, 4)
49 {
50  this->GetData()[0]=a0; this->GetData()[1]=a1; this->GetData()[ 2]= a2; this->GetData()[ 3]= a3;
51  this->GetData()[4]=a4; this->GetData()[5]=a5; this->GetData()[ 6]= a6; this->GetData()[ 7]= a7;
52  this->GetData()[8]=a8; this->GetData()[9]=a9; this->GetData()[10]=a10; this->GetData()[11]=a11;
53 }
54 
55 
56 template<class T>
57 Matrix3x4<T>::Matrix3x4(const std::string & s)
58 : Matrix<T>(3, 4, s)
59 {}
60 
61 
62 template<class T>
64  if ((mat.num_rows() == 3) && (mat.num_cols() == 4)) {
65  Matrix<T>::operator= (mat); // call the operator of the base class
66  return *this;
67  } else {
68  BIASERR("cannot assign with operator= , num_rows=" << mat.num_rows() << " num_cols= " << mat.num_rows()
69  << " are'nt appropriate for a 3x4 Matrix3x4<T>.");
70 #if DEBUG
71  BIASABORT;
72 #endif
73  };
74  return *this;
75 }
76 
77 template
78 <class T>
79 Matrix3x4<T> & Matrix3x4<T>::newsize(int rows, int cols) {
80  if ((rows != 3) || (cols != 4)) {
81  // do nothing
82  // print error message if the new size should not be a 3x4 matrix
83  BIASERR("The size of a Matrix3x4<T> has to be 3 rows, 4 cols. newsize can't create a " <<rows<< " x " <<cols<< " Matrix3x4 !");
84 #if DEBUG
85  BIASABORT;
86 #endif
87  return *this;
88 
89  } else {
90  BIASASSERT( rows==3 ); // 3x4 matrix
91  BIASASSERT( cols==4 );
92  // call base class function (usually unnecessary except for first construction)
93  Matrix<T>::newsize(3, 4);
94  return *this;
95  }
96 }
97 
98 
99 // JW
100 template <class T>
101 int
102 Matrix3x4<T>::LoadBan(const std::string & filename, const unsigned int & Time){
103  ifstream fs( filename.c_str() );
104  if (!fs) {
105  return -1; // error, could not open file.
106  } else {
107  // parse and read in
108  const unsigned int nMax=1024;
109  // character array for getline
110  char caLine[nMax];
111  // std string for find
112  string strLine;
113  bool found=false;
114  string::size_type pos=string::npos;
115  // pattern to find
116  stringstream patternSS;
117  patternSS<<"AddCameraKey { Time "<<setw(1)<<int(Time)<<" Camera [ ";
118  string pattern=patternSS.str();
119  while (!fs.eof() && (fs) &&(!found)){
120  fs.getline(&caLine[0], nMax);
121  strLine = caLine;
122  pos = strLine.find(pattern);
123  if (pos != string::npos){
124  // found pattern
125  found=true;
126  pos+=pattern.size();
127  string sub;
128  sub = strLine.substr(pos, strLine.size()-pos );
129  // helper to stream string into numeric variable
130  istringstream iss( sub );
131 
132  // read numerics
133  for (unsigned int row=0; row<3; row++) {
134  for (unsigned int col=0; col<4; col++) {
135  iss >> (*this)[row][col];
136  }
137  }
138  return 0; // OK.
139  };
140  };
141  fs.close();
142  };
143  return -2; // not found.
144 }
145 
146 
147 template <class T>
148 int
149 Matrix3x4<T>::LoadPARTXT(const std::string & filenameParTxt,
150  const unsigned int & index,
151  std::string & correspImgFilename,
152  int & nTotal)
153 {
154  ifstream fs;
155  fs.open(filenameParTxt.c_str() );
156  if (!fs){
157  BIASERR("could not open "<<filenameParTxt);
158  return(-1);
159  }
160  unsigned int uNumOfMat=0;
161  // read number of matrices in file
162  fs >> uNumOfMat;
163 
164  nTotal=uNumOfMat;
165  if(index>=uNumOfMat)
166  {
167  BIASERR("index greater then number of matrices");
168  return(-1);
169  }
170 
171  unsigned int uMc=0;
172  bool found=false;
173 
174  std::string tempImgFilename;
175  Matrix3x4<T> tempMat;
176  Matrix3x3<T> tempK;
177  Matrix3x3<T> tempR;
178  Vector3<T> tempC;
179 
180  unsigned int uRow=0;
181  unsigned int uCol=0;
182 
183  while (!found &&fs && !fs.eof())
184  {
185  fs>>tempImgFilename;
186  if(fs.eof())
187  {
188  BIASERR("Unexpected end of file");
189  return(-1);
190  }
191  // read k-matrix
192  for(uRow=0;uRow<3;uRow++)
193  for(uCol=0;uCol<3;uCol++)
194  {
195  fs>>tempK[uRow][uCol];
196  if(fs.eof())
197  {
198  BIASERR("Unexpected end of file");
199  return(-1);
200  }
201  }
202 
203  // read r-matrix
204  for(uRow=0;uRow<3;uRow++)
205  for(uCol=0;uCol<3;uCol++)
206  {
207  fs>>tempR[uRow][uCol];
208  if(fs.eof())
209  {
210  BIASERR("Unexpected end of file");
211  return(-1);
212  }
213  }
214 
215  // read t-vector
216  for(uRow=0;uRow<3;uRow++)
217  {
218  if(fs.eof())
219  {
220  BIASERR("Unexpected end of file");
221  return(-1);
222  }
223  fs>>tempC[uRow];
224  }
225  if(uMc==index)
226  {
227  found=true;
228  }
229  uMc++;
230  }
231  // The Seitz' projection matrix representation is given by K*[R t]
232  Matrix3x3<T> mKr=tempK*tempR;
233  //save K*R in P
234  for(uRow=0;uRow<3;uRow++)
235  for(uCol=0;uCol<3;uCol++)
236  {
237  tempMat[uRow][uCol]=mKr[uRow][uCol];
238  tempMat[uRow][uCol]=mKr[uRow][uCol];
239  tempMat[uRow][uCol]=mKr[uRow][uCol];
240  }
241  //save K*t in P
242  Vector3<T> vKt = tempK * tempC;
243  tempMat[0][3]=vKt[0];
244  tempMat[1][3]=vKt[1];
245  tempMat[2][3]=vKt[2];
246 
247  //everything went fine!
248  //save the results.
249  (*this)=tempMat;
250  correspImgFilename=tempImgFilename;
251 
252  return(0); // OK
253 }
254 
255 
256 } // end namespace BIAS
257 
258 #define INST(type) \
259 template class BIASMathBase_EXPORT BIAS::Matrix3x4<type>;\
260 
261 INST(unsigned char)
262 INST(char)
263 INST(float)
264 INST(short)
265 INST(unsigned short)
266 INST(long int)
267 INST(int)
268 INST(unsigned int)
269 INST(double)
Subscript num_cols() const
Definition: cmat.h:320
Matrix< T > & newsize(Subscript M, Subscript N)
Definition: cmat.h:269
int LoadBan(const std::string &filename, const unsigned int &Time=0)
Loads a PMatrix from .ban file given by AddCameraKey and Time index.
Definition: Matrix3x4.cpp:102
Matrix< T > & operator=(const TNT::Matrix< T > &mat)
assignment operators calling corresponding operator from base class if appropriate ...
Definition: Matrix.hh:1101
Matrix3x4 & newsize(int rows, int cols)
just neccessary to avoid resizing of this &#39;fixed size&#39; matrix because it is derived from the resizabl...
Definition: Matrix3x4.cpp:79
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
Matrix3x4()
default constructor
Definition: Matrix3x4.hh:58
int LoadPARTXT(const std::string &filenameParTxt, const unsigned int &index, std::string &correspImgFilename, int &nTotal)
Load a PMatrix from a _par.txt (S.
Definition: Matrix3x4.cpp:149
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
matrix class with arbitrary size, indexing is row major.
virtual ~Matrix3x4()
destructor untested (04/17/2002)
Definition: Matrix3x4.cpp:40
Subscript num_rows() const
Definition: cmat.h:319
is a &#39;fixed size&#39; rectangular matrix of dim.
Definition: Matrix3x3.hh:39
Matrix3x4 & operator=(const Matrix< T > &mat)
assignment operator calling corresponding operator from base class if appropriate.
Definition: Matrix3x4.cpp:63