ViSP  2.7.0
vpPoseVirtualVisualServoing.cpp
1 /****************************************************************************
2  *
3  * $Id: vpPoseVirtualVisualServoing.cpp 4056 2013-01-05 13:04:42Z fspindle $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2013 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  *
34  * Description:
35  * Pose computation.
36  *
37  * Authors:
38  * Eric Marchand
39  *
40  *****************************************************************************/
41 
47 #include <visp/vpPose.h>
48 #include <visp/vpPoint.h>
49 #include <visp/vpFeatureBuilder.h>
50 #include <visp/vpFeaturePoint.h>
51 #include <visp/vpExponentialMap.h>
52 #include <visp/vpRobust.h>
53 
66 void
68 {
69  try
70  {
71 
72  double residu_1 = 1e8 ;
73  double r =1e8-1;
74 
75  // we stop the minimization when the error is bellow 1e-8
76 
77  int iter = 0 ;
78 
79  unsigned int nb = listP.size() ;
80  vpMatrix L(2*nb,6) ;
81  vpColVector err(2*nb) ;
82  vpColVector sd(2*nb),s(2*nb) ;
83  vpColVector v ;
84 
85  vpPoint P;
86  std::list<vpPoint> lP ;
87 
88  // create sd
89  unsigned int k =0 ;
90  for (std::list<vpPoint>::const_iterator it = listP.begin(); it != listP.end(); ++it)
91  {
92  P = *it;
93  sd[2*k] = P.get_x() ;
94  sd[2*k+1] = P.get_y() ;
95  lP.push_back(P);
96  k ++;
97  }
98 
99  while((int)((residu_1 - r)*1e12) !=0)
100  {
101  residu_1 = r ;
102 
103  // Compute the interaction matrix and the error
104  unsigned int k =0 ;
105  for (std::list<vpPoint>::const_iterator it = lP.begin(); it != lP.end(); ++it)
106  {
107  P = *it;
108  // forward projection of the 3D model for a given pose
109  // change frame coordinates
110  // perspective projection
111  P.track(cMo) ;
112 
113  double x = s[2*k] = P.get_x(); /* point projected from cMo */
114  double y = s[2*k+1] = P.get_y();
115  double Z = P.get_Z() ;
116  L[2*k][0] = -1/Z ;
117  L[2*k][1] = 0 ;
118  L[2*k][2] = x/Z ;
119  L[2*k][3] = x*y ;
120  L[2*k][4] = -(1+x*x) ;
121  L[2*k][5] = y ;
122 
123  L[2*k+1][0] = 0 ;
124  L[2*k+1][1] = -1/Z ;
125  L[2*k+1][2] = y/Z ;
126  L[2*k+1][3] = 1+y*y ;
127  L[2*k+1][4] = -x*y ;
128  L[2*k+1][5] = -x ;
129 
130  k+=1 ;
131  }
132  err = s - sd ;
133 
134  // compute the residual
135  r = err.sumSquare() ;
136 
137  // compute the pseudo inverse of the interaction matrix
138  vpMatrix Lp ;
139  L.pseudoInverse(Lp,1e-16) ;
140 
141  // compute the VVS control law
142  v = -lambda*Lp*err ;
143 
144  //std::cout << "r=" << r <<std::endl ;
145  // update the pose
146 
147  cMo = vpExponentialMap::direct(v).inverse()*cMo ; ;
148  if (iter++>vvsIterMax) break ;
149  }
150 
151  if(computeCovariance)
152  covarianceMatrix = vpMatrix::computeCovarianceMatrix(L,v,-lambda*err);
153  }
154 
155  catch(...)
156  {
157  vpERROR_TRACE(" ") ;
158  throw ;
159  }
160 }
161 
173 void
175 {
176  try{
177 
178  double residu_1 = 1e8 ;
179  double r =1e8-1;
180 
181  // we stop the minimization when the error is bellow 1e-8
182  vpMatrix W ;
183  vpRobust robust(2*listP.size()) ;
184  robust.setThreshold(0.0000) ;
185  vpColVector w,res ;
186 
187  unsigned int nb = listP.size() ;
188  vpMatrix L(2*nb,6) ;
189  vpColVector error(2*nb) ;
190  vpColVector sd(2*nb),s(2*nb) ;
191  vpColVector v ;
192 
193  listP.front() ;
194  vpPoint P;
195  std::list<vpPoint> lP ;
196 
197  // create sd
198  unsigned int k =0 ;
199  for (std::list<vpPoint>::const_iterator it = listP.begin(); it != listP.end(); ++it)
200  {
201  P = *it;
202  sd[2*k] = P.get_x() ;
203  sd[2*k+1] = P.get_y() ;
204  lP.push_back(P) ;
205  k ++;
206  }
207  int iter = 0 ;
208  res.resize(s.getRows()/2) ;
209  w.resize(s.getRows()/2) ;
210  W.resize(s.getRows(), s.getRows()) ;
211  w =1 ;
212 
213  while((int)((residu_1 - r)*1e12) !=0)
214  {
215  residu_1 = r ;
216 
217  // Compute the interaction matrix and the error
218  vpPoint P ;
219  k =0 ;
220  for (std::list<vpPoint>::const_iterator it = lP.begin(); it != lP.end(); ++it)
221  {
222  P = *it;
223  // forward projection of the 3D model for a given pose
224  // change frame coordinates
225  // perspective projection
226  P.track(cMo) ;
227 
228  double x = s[2*k] = P.get_x(); // point projected from cMo
229  double y = s[2*k+1] = P.get_y();
230  double Z = P.get_Z() ;
231  L[2*k][0] = -1/Z ;
232  L[2*k][1] = 0 ;
233  L[2*k][2] = x/Z ;
234  L[2*k][3] = x*y ;
235  L[2*k][4] = -(1+x*x) ;
236  L[2*k][5] = y ;
237 
238  L[2*k+1][0] = 0 ;
239  L[2*k+1][1] = -1/Z ;
240  L[2*k+1][2] = y/Z ;
241  L[2*k+1][3] = 1+y*y ;
242  L[2*k+1][4] = -x*y ;
243  L[2*k+1][5] = -x ;
244 
245  k ++;
246 
247  }
248  error = s - sd ;
249 
250  // compute the residual
251  r = error.sumSquare() ;
252 
253  for(unsigned int k=0 ; k <error.getRows()/2 ; k++)
254  {
255  res[k] = vpMath::sqr(error[2*k]) + vpMath::sqr(error[2*k+1]) ;
256  }
257  robust.setIteration(0);
258  robust.MEstimator(vpRobust::TUKEY, res, w);
259 
260  // compute the pseudo inverse of the interaction matrix
261  for (unsigned int k=0 ; k < error.getRows()/2 ; k++)
262  {
263  W[2*k][2*k] = w[k] ;
264  W[2*k+1][2*k+1] = w[k] ;
265  }
266  // compute the pseudo inverse of the interaction matrix
267  vpMatrix Lp ;
268  (W*L).pseudoInverse(Lp,1e-6) ;
269 
270  // compute the VVS control law
271  v = -lambda*Lp*W*error ;
272 
273  cMo = vpExponentialMap::direct(v).inverse()*cMo ; ;
274  if (iter++>vvsIterMax) break ;
275  }
276 
277  if(computeCovariance)
278  covarianceMatrix = vpMatrix::computeCovarianceMatrix(L,v,-lambda*error, W*W); // Remark: W*W = W*W.t() since the matrix is diagonale, but using W*W is more efficient.
279  }
280  catch(...)
281  {
282  vpERROR_TRACE(" ") ;
283  throw ;
284  }
285 
286 }
287 
288 
Definition of the vpMatrix class.
Definition: vpMatrix.h:96
void resize(const unsigned int nrows, const unsigned int ncols, const bool nullify=true)
Definition: vpMatrix.cpp:174
void poseVirtualVSrobust(vpHomogeneousMatrix &cMo)
compute the pose using a robust virtual visual servoing approach
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
#define vpERROR_TRACE
Definition: vpDebug.h:379
void track(const vpHomogeneousMatrix &cMo)
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.h:138
double sumSquare() const
return sum of the Aij^2 (for all i, for all j)
Definition: vpMatrix.cpp:758
std::list< vpPoint > listP
array of point (use here class vpPoint)
Definition: vpPose.h:97
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b)
void poseVirtualVS(vpHomogeneousMatrix &cMo)
compute the pose using virtual visual servoing approach
Class that defines what is a point.
Definition: vpPoint.h:65
double lambda
parameters use for the virtual visual servoing approach
Definition: vpPose.h:102
static double sqr(double x)
Definition: vpMath.h:106
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.h:136
double get_Z() const
Get the point Z coordinate in the camera frame.
Definition: vpPoint.h:122
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
vpHomogeneousMatrix inverse() const
static vpHomogeneousMatrix direct(const vpColVector &v)
Contains an M-Estimator and various influence function.
Definition: vpRobust.h:63
vpMatrix pseudoInverse(double svThreshold=1e-6) const
Compute the pseudo inverse of the matrix using the SVD.
Definition: vpMatrix.cpp:1810
void setThreshold(const double noise_threshold)
Definition: vpRobust.h:128
unsigned int getRows() const
Return the number of rows of the matrix.
Definition: vpMatrix.h:157
void resize(const unsigned int i, const bool flagNullify=true)
Definition: vpColVector.h:94