Basic Image AlgorithmS Library  2.8.0
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
CovMatrix3x3.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 "CovMatrix3x3.hh"
27 
28 using namespace BIAS;
29 using namespace std;
30 
31 
33  : Matrix3x3<COVMATRIX3X3_TYPE>() {
34  _IsDecomposed = false;
35 }
36 
38 
40 {
41  (*this) = mat;
42 }
43 
46 {
47  if (!_IsDecomposed){
48  if (_Decompose()!=0)
49  return -1;
50  } else {
51 #ifdef BIAS_DEBUG
52  if (memcmp( (void*)ConsistencyBackup_.GetData(),
53  (void*)GetData(), 9*sizeof(COVMATRIX3X3_TYPE))) {
54  BIASERR("Corrupted Data in CovMatrix. Maybe you forgot to invalidate."
55  <<"Consistency backup="<<ConsistencyBackup_<<" Current Matrix="
56  <<(*this));
57  BIASABORT;
58  }
59 #endif
60  }
61  S = _S;
62  VT = _VT;
63 
64  return 0;
65 }
66 
68 {
69  if (!_IsDecomposed){
70  if (_Decompose()!=0)
71  return -1;
72  } else {
73 #ifdef BIAS_DEBUG
74  if (memcmp( (void*)ConsistencyBackup_.GetData(),
75  (void*)GetData(), 9*sizeof(COVMATRIX3X3_TYPE))) {
76  BIASERR("Corrupted Data in CovMatrix. Maybe you forgot to invalidate.");
77  BIASABORT;
78  }
79 #endif
80  }
81 
82  S = _S;
83 
84  return 0;
85 }
86 
88 {
89  if (_svd.Compute((Matrix<COVMATRIX3X3_TYPE>)(*this))!=0){
90  return -1;
91  }
95 #ifdef BIAS_DEBUG
97 #endif
98  _IsDecomposed = true;
99 
100  return 0;
101 }
102 
103 
105 {
107  _S = mat._S;
108  _U = mat._U;
109  _VT = mat._VT;
110 
111 #ifdef BIAS_DEBUG
113 #endif
115 
116  //_svd=mat._svd;
117  return (*this);
118 }
119 
120 
121 /** Computes the bounding box x=[xmin,xmax] , y=[ymin,ymax] in image as
122  determined from the projected covariance matrix covmat. */
125  double scale)
126 {
127  int result=0;
128 
129  Vector3<double> S(0,0,0), TT(0,0,0);
131  HomgPoint3D M;
132  HomgPoint2D m, point2d;
133  if (GetPCA(S,VT)!=0){
134  result = -1;
135  }
136 
137  x.Set(DBL_MAX,-DBL_MAX); y.Set(DBL_MAX,-DBL_MAX);
138  for (int i=0;i<3; i++)
139  for (double sig=-1; sig <= 1.1; sig+=2){
140  // compute point on uncertainty ellipsoid
141  // HomgPoint3D T(VT.GetRow(i)); T.Homogenize();
142  TT.Set(VT.GetRow(i));
143  TT *= (sig*scale*S[i]);
144  point3D.Homogenize();
145  TT[0] +=point3D[0];
146  TT[1] +=point3D[1];
147  TT[2] +=point3D[2];
148  M.Set(TT[0], TT[1], TT[2]);
149  // project into image
150  m=P*M;
151  m.Homogenize();
152  // update bounding box params
153  if (m[0]<x[0]) x[0]=m[0];
154  if (m[0]>x[1]) x[1]=m[0];
155  if (m[1]<y[0]) y[0]=m[1];
156  if (m[1]>y[1]) y[1]=m[1];
157  }
158 
159  //// check if the projected point3D is in the box
160  //// otherwise one or more 3DPoints of the ellipsoid are behind the camera
161  point2d=P*point3D;
162  point2d.Homogenize();
163  if (x[1]<point2d[0]) {
164  x[0]=x[1]; x[1]=DBL_MAX;
165  }
166  if (x[0]>point2d[0]) {
167  x[1]=x[0]; x[0]=-DBL_MAX;
168  }
169 
170  if (y[1]<point2d[1]) {
171  y[0]=y[1]; y[1]=DBL_MAX;
172  }
173  if (y[0]>point2d[1]) {
174  y[1]=y[0]; y[0]=-DBL_MAX;
175  }
176 
177  return result;
178 }
179 
180 /** Computes the bounding box x=[xmin,xmax] , y=[ymin,ymax] in image as
181  determined from the projected covariance matrix covmat.
182  A min Search Area can be specified with minSearchArea(which is the
183  radius of the uncertainty sphere).
184 
185 */
188  double scale, double minSearchArea)
189 {
190  int result=0;
191 
192  Vector3<double> S, TT;
194  HomgPoint3D M;
195  HomgPoint2D m, point2d;
196  if (GetPCA(S,VT)!=0){
197  result = -1;
198  }
199  point3D.Homogenize();
200  x.Set(DBL_MAX,-DBL_MAX); y.Set(DBL_MAX,-DBL_MAX);
201  for (int i=0;i<3; i++)
202  for (double sig=-1; sig <= 1.1; sig+=2){
203  // compute point on uncertainty ellipsoid
204  // HomgPoint3D T(VT.GetRow(i)); T.Homogenize();
205  TT.SetZero();
206  TT[i]=1.0;
207  // at first calculate minimum area with scaled unit sphere
208  TT *= sig*minSearchArea;
209  TT[0] +=point3D[0];
210  TT[1] +=point3D[1];
211  TT[2] +=point3D[2];
212  M.Set(TT[0], TT[1], TT[2]);
213  //// project into image
214  m=P*M;
215  m.Homogenize();
216  // update bounding box params
217  if (m[0]<x[0]) x[0]=m[0];
218  if (m[0]>x[1]) x[1]=m[0];
219  if (m[1]<y[0]) y[0]=m[1];
220  if (m[1]>y[1]) y[1]=m[1];
221 
222  //// second: project real cov Matrix
223  TT.Set(VT.GetRow(i));
224  TT *= (sig*scale*S[i]);
225  TT[0] +=point3D[0];
226  TT[1] +=point3D[1];
227  TT[2] +=point3D[2];
228  M.Set(TT[0], TT[1], TT[2]);
229  // project into image
230  m=P*M;
231  m.Homogenize();
232  // update bounding box params
233  if (m[0]<x[0]) x[0]=m[0];
234  if (m[0]>x[1]) x[1]=m[0];
235  if (m[1]<y[0]) y[0]=m[1];
236  if (m[1]>y[1]) y[1]=m[1];
237  }
238 
239  //// check if the projected point3D is in the box
240  //// otherwise one or more 3DPoints of the ellipsoid are behind the camera
241  point2d=P*point3D;
242  point2d.Homogenize();
243  if (x[1]<point2d[0]) {
244  x[0]=x[1]; x[1]=DBL_MAX;
245  }
246  if (x[0]>point2d[0]) {
247  x[1]=x[0]; x[0]=-DBL_MAX;
248  }
249 
250  if (y[1]<point2d[1]) {
251  y[0]=y[1]; y[1]=DBL_MAX;
252  }
253  if (y[0]>point2d[1]) {
254  y[1]=y[0]; y[0]=-DBL_MAX;
255  }
256 
257  return result;
258 }
void GetRow(const unsigned int row, Vector3< T > &r) const
extract one row (&#39;Zeile&#39;) from ths matrix (for convenience)
Definition: Matrix3x3.cpp:287
void Set(const T *pv)
copy the array of vectorsize beginning at *T to this-&gt;data_
Definition: Vector3.hh:532
CovMatrix3x3 & operator=(const CovMatrix3x3 &mat)
class HomgPoint2D describes a point with 2 degrees of freedom in projective coordinates.
Definition: HomgPoint2D.hh:67
int GetPCA(Vector3< COVMATRIX3X3_TYPE > &S, Matrix3x3< COVMATRIX3X3_TYPE > &VT)
void Set(const HOMGPOINT3D_TYPE &x, const HOMGPOINT3D_TYPE &y, const HOMGPOINT3D_TYPE &z)
set elementwise with given 3 euclidean scalar values.
Definition: HomgPoint3D.hh:321
HomgPoint2D & Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint2D.hh:215
void SetZero()
set all values to 0
Definition: Vector3.hh:559
void Homogenize()
homogenize class data member elements to W==1 by divison by W
Definition: HomgPoint3D.hh:308
int Compute(const Matrix< double > &M, double ZeroThreshold=DEFAULT_DOUBLE_ZERO_THRESHOLD)
set a new matrix and compute its decomposition.
Definition: SVD.cpp:102
int GetS(Vector3< COVMATRIX3X3_TYPE > &S)
void Set(const T &scalar)
set all elements to a scalar value
Definition: Vector2.hh:190
const Matrix< double > & GetVT() const
return VT (=transposed(V))
Definition: SVD.hh:177
int GetSearchArea(PMatrixBase &P, HomgPoint3D &point3D, Vector2< double > &x, Vector2< double > &y, double scale)
Computes the bounding box x=[xmin,xmax] , y=[ymin,ymax] in image as determined from the projected cov...
class for 3x3 covariance matrices
Definition: CovMatrix3x3.hh:50
const Vector< double > & GetS() const
return S which is a vector of the singular values of A in descending order.
Definition: SVD.hh:167
Matrix3x3< COVMATRIX3X3_TYPE > _VT
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
Matrix3x3< COVMATRIX3X3_TYPE > ConsistencyBackup_
saves matrix fields to make consistency checks possible later
Vector3< COVMATRIX3X3_TYPE > _S
matrix class with arbitrary size, indexing is row major.
const Matrix< double > & GetU() const
return U U is a m x m orthogonal matrix
Definition: SVD.hh:187
Matrix3x3< T > & operator=(const Matrix3x3< T > &mat)
assignment operator
Definition: Matrix3x3.cpp:704
bool _IsDecomposed
flag indicating whether the stored PCA is still valid
Matrix3x3< COVMATRIX3X3_TYPE > _U