Visual Servoing Platform  version 3.6.1 under development (2024-04-26)
vpPoseVirtualVisualServoing.cpp
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Pose computation.
32  */
33 
39 #include <visp3/core/vpConfig.h>
40 #include <visp3/core/vpExponentialMap.h>
41 #include <visp3/core/vpPoint.h>
42 #include <visp3/core/vpRobust.h>
43 #include <visp3/vision/vpPose.h>
44 
46 {
47  try {
48 
49  double residu_1 = 1e8;
50  double r = 1e8 - 1;
51 
52  // we stop the minimization when the error is bellow 1e-8
53 
54  int iter = 0;
55 
56  unsigned int nb = static_cast<unsigned int>(listP.size());
57  vpMatrix L(2 * nb, 6);
58  vpColVector err(2 * nb);
59  vpColVector sd(2 * nb), s(2 * nb);
60  vpColVector v;
61 
62  vpPoint P;
63  std::list<vpPoint> lP;
64 
65  // create sd
66  unsigned int k = 0;
67  std::list<vpPoint>::const_iterator listp_end = listP.end();
68  for (std::list<vpPoint>::const_iterator it = listP.begin(); it != listp_end; ++it) {
69  P = *it;
70  sd[2 * k] = P.get_x();
71  sd[(2 * k) + 1] = P.get_y();
72  lP.push_back(P);
73  ++k;
74  }
75 
76  vpHomogeneousMatrix cMoPrev = cMo;
77  // --comment: while((int)((residu_1 - r)*1e12) !=0)
78  // --comment: while(std::fabs((residu_1 - r)*1e12) >
79  // --comment: std::numeric_limits < double > :: epsilon())
80  while (std::fabs(residu_1 - r) > vvsEpsilon) {
81  residu_1 = r;
82 
83  // Compute the interaction matrix and the error
84  k = 0;
85  std::list<vpPoint>::const_iterator lp_end = lP.end();
86  for (std::list<vpPoint>::const_iterator it = lP.begin(); it != lp_end; ++it) {
87  P = *it;
88  // forward projection of the 3D model for a given pose
89  // change frame coordinates
90  // perspective projection
91  P.track(cMo);
92 
93  double x = s[2 * k] = P.get_x(); /* point projected from cMo */
94  double y = s[(2 * k) + 1] = P.get_y();
95  double Z = P.get_Z();
96  L[2 * k][0] = -1 / Z;
97  L[2 * k][1] = 0;
98  L[2 * k][2] = x / Z;
99  L[2 * k][3] = x * y;
100  L[2 * k][4] = -(1 + (x * x));
101  L[2 * k][5] = y;
102 
103  L[(2 * k) + 1][0] = 0;
104  L[(2 * k) + 1][1] = -1 / Z;
105  L[(2 * k) + 1][2] = y / Z;
106  L[(2 * k) + 1][3] = 1 + (y * y);
107  L[(2 * k) + 1][4] = -x * y;
108  L[(2 * k) + 1][5] = -x;
109 
110  k += 1;
111  }
112  err = s - sd;
113 
114  // compute the residual
115  r = err.sumSquare();
116 
117  // compute the pseudo inverse of the interaction matrix
118  vpMatrix Lp;
119  L.pseudoInverse(Lp, 1e-16);
120 
121  // compute the VVS control law
122  v = -m_lambda * Lp * err;
123 
124  // update the pose
125 
126  cMoPrev = cMo;
127  cMo = vpExponentialMap::direct(v).inverse() * cMo;
128 
129  if (iter> vvsIterMax) {
130  break;
131  }
132  else {
133  ++iter;
134  }
135  }
136 
137  if (computeCovariance) {
138  covarianceMatrix = vpMatrix::computeCovarianceMatrixVVS(cMoPrev, err, L);
139  }
140  }
141 
142  catch (...) {
143  vpERROR_TRACE(" ");
144  throw;
145  }
146 }
147 
149 {
150  try {
151 
152  double residu_1 = 1e8;
153  double r = 1e8 - 1;
154 
155  // we stop the minimization when the error is bellow 1e-8
156  vpMatrix W;
157  vpRobust robust;
158  robust.setMinMedianAbsoluteDeviation(0.00001);
159  vpColVector w, res;
160 
161  unsigned int nb = static_cast<unsigned int>(listP.size());
162  vpMatrix L(2 * nb, 6);
163  vpColVector error(2 * nb);
164  vpColVector sd(2 * nb), s(2 * nb);
165  vpColVector v;
166 
167  vpPoint P;
168  std::list<vpPoint> lP;
169 
170  // create sd
171  unsigned int k_ = 0;
172  std::list<vpPoint>::const_iterator listp_end = listP.end();
173  for (std::list<vpPoint>::const_iterator it = listP.begin(); it != listp_end; ++it) {
174  P = *it;
175  sd[2 * k_] = P.get_x();
176  sd[(2 * k_) + 1] = P.get_y();
177  lP.push_back(P);
178  ++k_;
179  }
180  int iter = 0;
181  res.resize(s.getRows() / 2);
182  w.resize(s.getRows() / 2);
183  W.resize(s.getRows(), s.getRows());
184  w = 1;
185 
186  // --comment: while (residu_1 - r) times 1e12 diff 0
187  while (std::fabs((residu_1 - r) * 1e12) > std::numeric_limits<double>::epsilon()) {
188  residu_1 = r;
189 
190  // Compute the interaction matrix and the error
191  k_ = 0;
192  std::list<vpPoint>::const_iterator lp_end = lP.end();
193  for (std::list<vpPoint>::const_iterator it = lP.begin(); it != lp_end; ++it) {
194  P = *it;
195  // forward projection of the 3D model for a given pose
196  // change frame coordinates
197  // perspective projection
198  P.track(cMo);
199 
200  double x = s[2 * k_] = P.get_x(); // point projected from cMo
201  double y = s[(2 * k_) + 1] = P.get_y();
202  double Z = P.get_Z();
203  L[2 * k_][0] = -1 / Z;
204  L[2 * k_][1] = 0;
205  L[2 * k_][2] = x / Z;
206  L[2 * k_][3] = x * y;
207  L[2 * k_][4] = -(1 + (x * x));
208  L[2 * k_][5] = y;
209 
210  L[(2 * k_) + 1][0] = 0;
211  L[(2 * k_) + 1][1] = -1 / Z;
212  L[(2 * k_) + 1][2] = y / Z;
213  L[(2 * k_) + 1][3] = 1 + (y * y);
214  L[(2 * k_) + 1][4] = -x * y;
215  L[(2 * k_) + 1][5] = -x;
216 
217  ++k_;
218  }
219  error = s - sd;
220 
221  // compute the residual
222  r = error.sumSquare();
223 
224  unsigned int v_error_rows = error.getRows();
225  for (unsigned int k = 0; k < (v_error_rows / 2); ++k) {
226  res[k] = vpMath::sqr(error[2 * k]) + vpMath::sqr(error[(2 * k) + 1]);
227  }
228  robust.MEstimator(vpRobust::TUKEY, res, w);
229 
230  // compute the pseudo inverse of the interaction matrix
231  unsigned int error_rows = error.getRows();
232  for (unsigned int k = 0; k < (error_rows / 2); ++k) {
233  W[2 * k][2 * k] = w[k];
234  W[(2 * k) + 1][(2 * k) + 1] = w[k];
235  }
236  // compute the pseudo inverse of the interaction matrix
237  vpMatrix Lp;
238  (W * L).pseudoInverse(Lp, 1e-6);
239 
240  // compute the VVS control law
241  v = -m_lambda * Lp * W * error;
242 
243  cMo = vpExponentialMap::direct(v).inverse() * cMo;
244  if (iter > vvsIterMax) {
245  break;
246  }
247  else {
248  ++iter;
249  }
250  }
251 
252  if (computeCovariance) {
253  covarianceMatrix =
254  vpMatrix::computeCovarianceMatrix(L, v, -m_lambda * error, W * W); // Remark: W*W = W*W.t() since the
255  // matrix is diagonale, but using W*W
256  // is more efficient.
257  }
258  }
259  catch (...) {
260  vpERROR_TRACE(" ");
261  throw;
262  }
263 }
264 
265 // Check if std:c++17 or higher
266 #if ((__cplusplus >= 201703L) || (defined(_MSVC_LANG) && (_MSVC_LANG >= 201703L)))
267 std::optional<vpHomogeneousMatrix> vpPose::poseVirtualVSWithDepth(const std::vector<vpPoint> &points, const vpHomogeneousMatrix &cMo)
268 {
269  auto residu_1 { 1e8 }, r { 1e8 - 1 };
270  const auto lambda { 0.9 }, vvsEpsilon { 1e-8 };
271  const unsigned int vvsIterMax { 200 };
272 
273  const unsigned int nb = static_cast<unsigned int>(points.size());
274  vpMatrix L(3 * nb, 6);
275  vpColVector err(3 * nb);
276  vpColVector sd(3 * nb), s(3 * nb);
277 
278  // create sd
279  auto v_points_size = points.size();
280  for (auto i = 0u; i < v_points_size; ++i) {
281  sd[3 * i] = points[i].get_x();
282  sd[(3 * i) + 1] = points[i].get_y();
283  sd[(3 * i) + 2] = points[i].get_Z();
284  }
285 
286  auto cMoPrev = cMo;
287  auto iter = 0u;
288  while (std::fabs(residu_1 - r) > vvsEpsilon) {
289  residu_1 = r;
290 
291  // Compute the interaction matrix and the error
292  auto points_size = points.size();
293  for (auto i = 0u; i < points_size; ++i) {
294  // forward projection of the 3D model for a given pose
295  // change frame coordinates
296  // perspective projection
297  vpColVector cP, p;
298  points.at(i).changeFrame(cMo, cP);
299  points.at(i).projection(cP, p);
300 
301  const auto x = s[3 * i] = p[0];
302  const auto y = s[(3 * i) + 1] = p[1];
303  const auto Z = s[(3 * i) + 2] = cP[2];
304  L[3 * i][0] = -1 / Z;
305  L[3 * i][1] = 0;
306  L[3 * i][2] = x / Z;
307  L[3 * i][3] = x * y;
308  L[3 * i][4] = -(1 + vpMath::sqr(x));
309  L[3 * i][5] = y;
310 
311  L[(3 * i) + 1][0] = 0;
312  L[(3 * i) + 1][1] = -1 / Z;
313  L[(3 * i) + 1][2] = y / Z;
314  L[(3 * i) + 1][3] = 1 + vpMath::sqr(y);
315  L[(3 * i) + 1][4] = -x * y;
316  L[(3 * i) + 1][5] = -x;
317 
318  L[(3 * i) + 2][0] = 0;
319  L[(3 * i) + 2][1] = 0;
320  L[(3 * i) + 2][2] = -1;
321  L[(3 * i) + 2][3] = -y * Z;
322  L[(3 * i) + 2][4] = x * Z;
323  L[(3 * i) + 2][5] = -0;
324  }
325  err = s - sd;
326 
327  // compute the residual
328  r = err.sumSquare();
329 
330  // compute the pseudo inverse of the interaction matrix
331  vpMatrix Lp;
332  L.pseudoInverse(Lp, 1e-16);
333 
334  // compute the VVS control law
335  const auto v = -lambda * Lp * err;
336 
337  // update the pose
338  cMoPrev = vpExponentialMap::direct(v).inverse() * cMoPrev;
339 
340  if (iter > vvsIterMax) {
341  return std::nullopt;
342  }
343  else {
344  ++iter;
345  }
346  }
347  return cMoPrev;
348 }
349 
350 #endif
void resize(unsigned int nrows, unsigned int ncols, bool flagNullify=true, bool recopy_=true)
Definition: vpArray2D.h:352
unsigned int size() const
Return the number of elements of the 2D array.
Definition: vpArray2D.h:339
unsigned int getRows() const
Definition: vpArray2D.h:337
Implementation of column vector and the associated operations.
Definition: vpColVector.h:163
double sumSquare() const
void resize(unsigned int i, bool flagNullify=true)
Definition: vpColVector.h:1056
static vpHomogeneousMatrix direct(const vpColVector &v)
void track(const vpHomogeneousMatrix &cMo)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpHomogeneousMatrix inverse() const
static double sqr(double x)
Definition: vpMath.h:201
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:146
static vpMatrix computeCovarianceMatrix(const vpMatrix &A, const vpColVector &x, const vpColVector &b)
static vpMatrix computeCovarianceMatrixVVS(const vpHomogeneousMatrix &cMo, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W)
Class that defines a 3D point in the object frame and allows forward projection of a 3D point in the ...
Definition: vpPoint.h:77
double get_y() const
Get the point y coordinate in the image plane.
Definition: vpPoint.cpp:465
double get_x() const
Get the point x coordinate in the image plane.
Definition: vpPoint.cpp:463
double get_Z() const
Get the point cZ coordinate in the camera frame.
Definition: vpPoint.cpp:449
double m_lambda
Parameters use for the virtual visual servoing approach.
Definition: vpPose.h:765
void poseVirtualVS(vpHomogeneousMatrix &cMo)
std::list< vpPoint > listP
Array of point (use here class vpPoint)
Definition: vpPose.h:115
void poseVirtualVSrobust(vpHomogeneousMatrix &cMo)
Contains an M-estimator and various influence function.
Definition: vpRobust.h:83
@ TUKEY
Tukey influence function.
Definition: vpRobust.h:88
void MEstimator(const vpRobustEstimatorType method, const vpColVector &residues, vpColVector &weights)
Definition: vpRobust.cpp:136
void setMinMedianAbsoluteDeviation(double mad_min)
Definition: vpRobust.h:156
#define vpERROR_TRACE
Definition: vpDebug.h:382