Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
EParametrization.cpp
1 #include "EParametrization.hh"
2 
3 #include <Base/Debug/Exception.hh>
4 #include <Base/Common/CompareFloatingPoint.hh>
5 #include <Base/Geometry/HomgPoint2D.hh>
6 #include <Base/Geometry/HomgPoint2DCov.hh>
7 
8 
9 using namespace BIAS;
10 using namespace std;
11 
14  : E_(7, 0.0), Cov_(7, 7, MatrixZero)
15 {
16  E_[6] = 1.0;
17 }
18 
19 
22  const Matrix<EP_TYPE>& covariance)
23 {
24  Set(EQvec, covariance);
25 }
26 
27 
30  const Quaternion<EP_TYPE>& orientation,
31  const Matrix<EP_TYPE>& covariance)
32 {
33  Set(epipole, orientation, covariance);
34 }
35 
36 
38 Set(const Vector3<EP_TYPE>& epipole, const Quaternion<EP_TYPE>& orientation,
39  const Matrix<EP_TYPE>& covariance)
40 {
41  Vector<EP_TYPE> EQvec(7);
42  EQ2E_(epipole, orientation, EQvec);
43  Set(EQvec, covariance);
44 }
45 
47 SetEQ(const Vector3<EP_TYPE>& epipole,
48  const Quaternion<EP_TYPE>& orientation)
49 {
50  Vector<EP_TYPE> EQvec(7);
51  EQ2E_(epipole, orientation, EQvec);
52  E_ = EQvec;
53  Cov_.SetZero();
54 }
55 
56 
58 SetEQ(const Vector<EP_TYPE>& EQvec)
59 {
60  if (EQvec.size() != 7){
61  BEXCEPTION("Invalid vector size (must be 7, " << EQvec.size() << " given)!");
62  }
63  E_ = EQvec;
64  Cov_.SetZero();
65 }
66 
67 
70 {
71  if(Cov.num_rows() != 7 || Cov.num_cols() != 7) {
72  BEXCEPTION("Dimension of given covariance matrix is invalid "
73  "(7x7 matrix needed, " << Cov.num_rows() << "x"
74  << Cov.num_cols() << " matrix given)!\n");
75  }
76  // todo: check if covariance matrix is symmetric and positive definite
77  Cov_ = Cov;
78 }
79 
80 
83  Matrix<EP_TYPE> res = Cov_;
84  return res;
85 }
86 
87 
89 GetEpipole3() const
90 {
93  E2EQ_(E_, e, q);
94  return e;
95 }
96 
97 
100 {
102  E2EQ_(E_, epipole, q);
103 }
104 
105 
108 {
111  E2EQ_(E_, e, q);
112  return q;
113 }
114 
115 
118 {
119  E_[0] = e[0];
120  E_[1] = e[1];
121  E_[2] = e[2];
122  Cov_.SetZero();
123 }
124 
125 
128 {
129  E_[3] = q[0];
130  E_[4] = q[1];
131  E_[5] = q[2];
132  E_[6] = q[3];
133  Cov_.SetZero();
134 }
135 
138 {
139  Vector3<EP_TYPE> epipole;
140  GetEpipole(epipole);
142  RMatrixBase R;
143  R.SetFromQuaternion(Q);
144  E.SetAsCrossProductMatrix(epipole);
145  E = E * R;
146 }
147 
150 {
152 
154 
155  return E;
156 }
157 
158 
160 Load(const std::string& file)
161 {
162  std::ifstream ifs(file.c_str());
163  if(!ifs)
164  return false;
165  ifs>>*this;
166  if(!ifs) {
167  ifs.close();
168  return false;
169  }
170  ifs.close();
171  return true;
172 }
173 
174 
176 Save(const std::string& file) const
177 {
178  std::ofstream ofs(file.c_str());
179  if(!ofs)
180  return false;
181  ofs<<*this;
182  if(!ofs) {
183  ofs.close();
184  return false;
185  }
186  ofs.close();
187  return true;
188 }
189 
190 
193 {
194  Matrix4x4<EP_TYPE> res;
195  for (int r=0; r<4; r++){
196  for (int c=0; c<4; c++){
197  res[r][c] = Cov_[r+3][c+3];
198  }
199  }
200  return res;
201 }
202 
203 
206 {
207  for (int r=0; r<3; r++){
208  for (int c=0; c<3; c++){
209  cov[r][c] = Cov_[r][c];
210  }
211  }
212 }
213 
214 // Matrix3x3<EP_TYPE> EParametrization::
215 // GetEpipoleCov() const
216 // {
217 // Matrix3x3<EP_TYPE> res;
218 // for (int r=0; r<3; r++){
219 // for (int c=0; c<3; c++){
220 // res[r][c] = Cov_[r][c];
221 // }
222 // }
223 // return res;
224 // }
225 
226 
229 {
231  q.Invert();
232  Vector3<EP_TYPE> ep, tmp;
233  GetEpipole(tmp);
234  q.MultVec(tmp, ep);
235  ep *= -1.0;
236  E_[0] = ep[0];
237  E_[1] = ep[1];
238  E_[2] = ep[2];
239  E_[3] = q[0];
240  E_[4] = q[1];
241  E_[5] = q[2];
242  E_[6] = q[3];
243  // the covariance does not need to be modified
244 }
245 
246 
248 Normalize(const bool update_covariance)
249 {
250  BIASASSERT(E_.Size()==7u);
251 
252  Vector4<double> q;
253  double quat_norm = 0.0;
254  for (int i=3; i<7; i++){
255  quat_norm += E_[i] * E_[i];
256  q[i-3] = E_[i];
257  }
258  quat_norm = sqrt(quat_norm);
259  BIASASSERT(!Equal(quat_norm, 0.0));
260  for (int i=3; i<7; i++){
261  E_[i] /= quat_norm;
262  }
263 
264  Vector3<double> e;
265  double ep_norm = 0.0;
266  for (int i=0; i<3; i++){
267  ep_norm += E_[i] * E_[i];
268  e[i] = E_[i];
269  }
270  ep_norm = sqrt(ep_norm);
271  BIASASSERT(!Equal(ep_norm, 0.0));
272  for (int i=0; i<3; i++){
273  E_[i] /= ep_norm;
274  }
275 
276  // Update covariance of normalized parameter vector
277  // by linear approximation of the normalization function
278  // @author esquivel 01/2010
279  // @todo use Unscented Transform instead!
280  if (update_covariance)
281  {
282  // The Jacobian of normalization is given by:
283  // J = [ Je 0
284  // 0 Jq ] with
285  // Je = 1/|e|(I - ee^T/e^Te) and
286  // Jq = 1/|q|(I - qq^T/q^Tq)
287  // (compare Foerstner 2004, "Uncertainty and Projective Geometry")
289  Matrix3x3<double> tmp_e = e.OuterProduct(e);
291  Matrix4x4<double> tmp_q = q.OuterProduct(q);
292  tmp_e /= e.ScalarProduct(e);
293  tmp_q /= q.ScalarProduct(q);
294  Je -= tmp_e;
295  Je /= ep_norm;
296  Jq -= tmp_q;
297  Jq /= quat_norm;
298  Matrix<EP_TYPE> J(7, 7, MatrixZero);
299  for (int i=0; i<3; i++) {
300  for (int j=0; j<3; j++) {
301  J[i][j] = Je[i][j];
302  }
303  }
304  for (int i=0; i<4; i++) {
305  for (int j=0; j<4; j++) {
306  J[i+3][j+3] = Jq[i][j];
307  }
308  }
309 
310  // The covariance transforms as J * cov * J^T
311  Cov_ = J * Cov_ * J.Transpose();
312 
313  //BIASERR("unfinished");
314  //BIASABORT;
315  }
316 }
317 
318 
319 std::ostream& BIAS::
320 operator<<(std::ostream &os, const EParametrization& p)
321 {
322  os<< "epipole= "<< p.GetEpipole3()<< " orientation= "<< p.GetOrientation();
323  os<< " cov= "<< p.GetCovarianceMatrix();
324  return os;
325 }
326 
327 
328 std::istream& BIAS::
329 operator>>(std::istream &is, EParametrization& p)
330 {
331  std::string temp;
332  Vector3<EP_TYPE> epipole;
333  Quaternion<EP_TYPE> orientation;
334  Matrix<EP_TYPE> cov;
335  is>>temp>>epipole>>temp>>orientation>>temp>>cov;
336  p.Set(epipole, orientation, cov);
337  return is;
338 }
339 
void E2EQ_(const Vector< EP_TYPE > &E, Vector3< EP_TYPE > &epipole, Quaternion< EP_TYPE > &orientation) const
void SetEQ(const Vector< EP_TYPE > &EQvec)
Set this from E parametrization.
Subscript num_cols() const
Definition: cmat.h:320
void Invert()
inverts this, by changing the rotation axis by *=-1
Definition: Quaternion.hh:34
Matrix< T > Transpose() const
transpose function, storing data destination matrix
Definition: Matrix.hh:823
Vector3< EP_TYPE > GetEpipole3() const
returns the epipole (first 3 entries) part of the essential matrix parametrization vector ...
void ScalarProduct(const Vector3< T > &argvec, T &result) const
scalar product (=inner product) of two vectors, storing the result in result
Definition: Vector3.hh:603
void OuterProduct(const Vector3< T > &v, Matrix3x3< T > &res) const
outer product, constructs a matrix.
Definition: Vector3.cpp:151
Matrix4x4< EP_TYPE > GetOrientationCov() const
covariance to GetOrientation()
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
Matrix3x3< EP_TYPE > GetEssentialMatrix() const
returns the essential matrix computed from parametrization
int MultVec(const Vector3< QUAT_TYPE > &vec, Vector3< QUAT_TYPE > &res) const
rotates the given Vector qith the quaternion ( q v q* ) the resulting vector is given in res ...
Definition: Quaternion.hh:136
Slim class bundling essential matrix parameterization and associated covariance matrix.
class Vector4 contains a Vector of dim.
Definition: Vector4.hh:65
void SetZero()
Sets all values to zero.
Definition: Matrix.hh:856
void EQ2E_(const Vector3< EP_TYPE > &epipole, const Quaternion< EP_TYPE > &orientation, Vector< EP_TYPE > &E) const
Vector< EP_TYPE > E_
first 3 entries relate to the epipole, the last four to the quaternion orientation ...
void Set(const Vector3< EP_TYPE > &epipole, const Quaternion< EP_TYPE > &orientation, const Matrix< EP_TYPE > &covariance)
Set this from epipole, orientation and covariance.
Matrix< EP_TYPE > Cov_
first 3 columns relate to epipole, the last four columns relate to the orientation ...
void SetOrientation(const Quaternion< EP_TYPE > &q)
set orientation part of vector and zero covariance matrix
bool Save(const std::string &file) const
Quaternion< EP_TYPE > GetOrientation() const
returns the quaternion (last 4 entries) part of the essential matrix parametrization vector ...
void Invert()
inverts the direction,
Matrix< EP_TYPE > GetCovarianceMatrix() const
return whole (with orientation and epipole) covariance matrix
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix.hh:54
std::ostream & operator<<(std::ostream &os, const Array2D< T > &arg)
Definition: Array2D.hh:260
class Vector3 contains a Vector of fixed dim.
Definition: Matrix.hh:53
bool Equal(const T left, const T right, const T eps)
comparison function for floating point values See http://www.boost.org/libs/test/doc/components/test_...
bool Load(const std::string &file)
void SetEpipole(const Vector3< EP_TYPE > &epipole)
set epipole part of vector and zero covariance matrix
Implements a 3D rotation matrix.
Definition: RMatrixBase.hh:44
Subscript num_rows() const
Definition: cmat.h:319
is a &#39;fixed size&#39; quadratic matrix of dim.
Definition: Matrix4x4.hh:54
void SetAsCrossProductMatrix(const Vector3< T > &vec)
Sets matrix from vector as cross product matrix of this vector.
Definition: Matrix3x3.cpp:275
class for rotation with axis and angle
void GetEpipole(Vector3< EP_TYPE > &epipole) const
returns the epipole (first 3 entries) part of the essential matrix parametrization vector ...
void Normalize(const bool update_covariance=true)
void SetCovarianceMatrix(const Matrix< EP_TYPE > &Cov)
Sets covariance matrix from Cov.
Subscript size() const
Definition: vec.h:262
BIASCommon_EXPORT std::istream & operator>>(std::istream &is, BIAS::TimeStamp &ts)
Standard input operator for TimeStamps.
Definition: TimeStamp.cpp:157
void GetEpipoleCov(Matrix3x3< EP_TYPE > &cov) const
covariance to GetEpipole(),