Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Vector3.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 "Vector3.hh"
26 #include "Vector.hh"
27 #include "math.h"
28 #include <Base/Common/W32Compat.hh>
29 
30 // STD
31 #include <iostream>
32 #include <iomanip>
33 #include <fstream>
34 #include <sstream>
35 #include <string>
36 #include <vector>
37 
38 using namespace BIAS;
39 using namespace std;
40 
41 namespace BIAS {
42 
43 
44  // JW 06/2003
45  template < class T >
46  Vector3<T>
48  {
49  Vector3<T> vec=(*this);
50  vec.Normalize();
51  return vec;
52  }
53 
54  template < class T >
57  {
58  /** the jacobian of the function x' = x/|x| can be written as
59  J = 1/|x| ( I - (xx^T)/(x^Tx) ),
60  cf. Foerstner, 2005 "Uncertainty and Projective Geometry" */
61  const T act_length = (T) NormL2(); // euclidean length
62  if (act_length > std::numeric_limits<T>::epsilon() ){
64  Matrix3x3<T> tmp;
65  OuterProduct(*this, tmp);
66  tmp /= (T) (act_length * act_length);
67  J -= tmp;
68  J /= act_length;
69  cov = J * cov * J.Transpose();
70  }
71  return Normalize();
72  }
73 
74 
75  // JW 09/2003 - default: not implemented
76  template < class T >
77  Vector3<T>
79  {
80  BIASERR("CoordSphereToEuclidean not implemented for this template type t - but e.g. for double");
81 #ifdef BIAS_DEBUG
82  BIASABORT; // HACK
83  return Vector3<T>(0,0,0);
84 #else
85  return Vector3<T>(0,0,0);
86 #endif
87  }
88 
89 
90  // JW 09/2003
91  template <>
94  {
95  // symbolic names for clarity:
96  const double r = (*this)[0];
97  const double phi = (*this)[1];
98  const double theta = (*this)[2];
99 
100  if (r==0) return Vector3<double>( 0, 0, 0 ); // singularity nullvector.
101 
102  const double xx( r * sin(theta) * cos(phi) );
103  const double yy( r * (sin(theta) * sin(phi)) );
104  const double zz( r * cos(theta) ); // TODO: plus or minus here -> RHS or LHS?
105 
106  return Vector3<double>( xx, yy, zz );
107  }
108 
109 
110  // JW 09/2003 - default: not implemented
111  template < class T >
114  {
115  BIASERR("CoordEuclideanToSphere not implemented for this template type T"
116  <<" - but e.g. for double");
117 #ifdef BIAS_DEBUG
118  BIASABORT; // HACK
119 #endif
120  return Vector3<T>(0,0,0);
121  }
122 
123 
124  // JW 09/2003
125  template <>
128  {
129  // symbolic names for clarity:
130  const double xx = (*this)[0];
131  const double yy = (*this)[1];
132  const double zz = (*this)[2];
133 
134  const double r( NormL2() );
135 
136  if (r==0) return Vector3<double>(0,0,0); // singularity null vector
137 
138  //const double theta( atan2(yy, xx) );
139  const double phi(M_PI_2- atan2(xx, yy) );
140 
141  // length of projection into XY plane (for phi)
142  const double projLength=sqrt( xx * xx + yy * yy );
143 
144  const double theta(atan2(projLength, zz) );
145 
146  return Vector3<double>(r, phi, theta);
147  }
148 
149 
150  template <class T>
152  {
153  mat[0][0]=data_[0]*v[0]; mat[0][1]= data_[0]*v[1]; mat[0][2]= data_[0]*v[2];
154  mat[1][0]=data_[1]*v[0]; mat[1][1]= data_[1]*v[1]; mat[1][2]= data_[1]*v[2];
155  mat[2][0]=data_[2]*v[0]; mat[2][1]= data_[2]*v[1]; mat[2][2]= data_[2]*v[2];
156  }
157 
158 
159  // JW 03/2005
160  template <class T>
161  int Vector3<T>::LoadBogTC(const std::string & filename)
162  {
163  SetZero();// reset this
164  ifstream fs( filename.c_str() );
165  if (!fs) {
166  BIASERR("could not open file "<<filename);
167  return -1; // error, could not open file.
168  };
169  // parse and read in
170  const unsigned int nMax=1024;
171  char caLine[nMax];
172  // std string for find
173  string strLine;
174  // helper for string to number /atof)
175  istringstream iss;
176  string::size_type pos=string::npos;
177  unsigned int line=0;
178  vector<string> pattern;
179  pattern.clear();
180  pattern.push_back("TC_X ="); // 0
181  pattern.push_back("TC_Y ="); // 1
182  pattern.push_back("TC_Z ="); // 2
183  vector< bool > found;
184  found.resize( pattern.size(), false);
185  BIASASSERT(found.size()>2);
186  while (!fs.eof() && (fs)
187  && (!found[0] || !found[1] || !found[2] ) )
188  {
189  line++;
190  fs.getline(&caLine[0], nMax);
191  strLine = caLine;
192  for (unsigned int i=0; i<pattern.size(); i++){
193  pos = strLine.find(pattern[i]);
194  if (pos != string::npos){
195  found[i]=true;
196  iss.clear();
197  iss.str( // after pattern: start, length
198  strLine.substr(pos+pattern[i].size(), strLine.size()-pos-pattern[i].size())
199  );
200  BIASASSERT(i<3); // dim
201  iss>> (*this)[i];
202  };
203  };
204  };
205  // check if we found all patterns
206  BIASASSERT(found.size()==pattern.size());
207  bool foundAll=true;
208  for (unsigned int j=0; j<found.size(); j++) foundAll=foundAll&&found[j];
209  if (!foundAll) return -99; // missed something
210  return 0; // OK.
211  }
212 
213  // jw
214  template<class T>
215  bool Vector3<T>::Load(const std::string & filename)
216  {
217  std::ifstream fs( filename.c_str() );
218  if (!fs) {
219  return false; // error, could not open file.
220  } else {
221  // read in
222  fs>>(*this);
223  fs.close();
224  };
225  return true;
226  }
227 
228  // jw
229  template<class T>
230  bool Vector3<T>::Save(const std::string & filename) const
231  {
232  std::ofstream fs( filename.c_str() );
233  if (!fs) {
234  return false; // error, could not open file.
235  } else {
236  // write out to disk
237  fs<<(*this);
238  fs.close();
239  };
240  return true;
241  }
242 
243  // specialization
244  template<>
246  {
247  Matrix<unsigned int> result(3,3);
248  result.SetZero();
249  BIASERR("not implemented because minus does not make sense for unsigned types");
250  BIASBREAK;
251  return result;
252  }
253 
254 
255  // impl. moved here for DLL instantiation
256  template <class T>
258  {
259  Matrix<T> result(3,3);
260  result.SetZero();
261  T* pData = result.GetData() + 1;
262  *pData++ = -data_[2];
263  *pData++ = data_[1];
264  *pData++ = data_[2];
265  pData++;
266  *pData++ = -data_[0];
267  *pData++ = -data_[1];
268  *pData = data_[0];
269  return result;
270  }
271 
272 
273  template <class T>
275  {
276  // check if v has correct size:
277  BIASASSERT(v.size() == VECTOR3_SIZE);
278  data_[0] = v[0];
279  data_[1] = v[1];
280  data_[2] = v[2];
281  }
282 
283 
284 
285 } // namespace BIAS
286 
287 
288 #define INST(type) \
289  template class BIASMathBase_EXPORT BIAS::Vector3<type>;
290 
291 INST(unsigned char)
292 INST(char)
293 INST(float)
294 INST(short)
295 INST(unsigned short)
296 INST(long int)
297 INST(int)
298 INST(unsigned int)
299 INST(double)
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
BIAS::Vector3< T > CoordEuclideanToSphere() const
coordinate transform.
Definition: Vector3.cpp:113
class for column vectors with arbitrary size
void OuterProduct(const Vector3< T > &v, Matrix3x3< T > &res) const
outer product, constructs a matrix.
Definition: Vector3.cpp:151
BIAS::Vector3< T > GetNormalized() const
return a normalized vector of this
Definition: Vector3.cpp:47
Matrix< T > GetSkewSymmetricMatrix() const
constructs a skew symmetric 3x3 matrix from (*this), which can be used instead of the cross product ...
Definition: Vector3.cpp:257
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
INST(unsigned char)
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
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
bool Save(const std::string &filename) const
method to save directly to a given filename.
Definition: Vector3.cpp:230
matrix class with arbitrary size, indexing is row major.
BIAS::Vector3< T > CoordSphereToEuclidean() const
coordinate transfrom.
Definition: Vector3.cpp:78
Matrix3x3< T > Transpose() const
returns transposed matrix tested 12.06.2002
Definition: Matrix3x3.cpp:167
bool Load(const std::string &filename)
method to load directly from a given filename.
Definition: Vector3.cpp:215
Vector3< T > & Normalize()
normalize this vector to length 1
Definition: Vector3.hh:663
Subscript size() const
Definition: vec.h:262