Visual Servoing Platform  version 3.1.0
vpPose.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Pose computation.
33  *
34  * Authors:
35  * Eric Marchand
36  * Francois Chaumette
37  * Aurelien Yol
38  *
39  *****************************************************************************/
40 
49 #ifndef vpPOSE_HH
50 #define vpPOSE_HH
51 
52 #include <visp3/core/vpHomogeneousMatrix.h>
53 #include <visp3/core/vpPoint.h>
54 #include <visp3/core/vpRGBa.h>
55 #include <visp3/vision/vpHomography.h>
56 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
57 #include <visp3/core/vpList.h>
58 #endif
59 #include <visp3/core/vpThread.h>
60 
61 #include <list>
62 #include <math.h>
63 #include <vector>
64 
79 class VISP_EXPORT vpPose
80 {
81 public:
83  typedef enum {
85  DEMENTHON,
87  LOWE,
91  LAGRANGE_LOWE,
93  DEMENTHON_LOWE,
95  VIRTUAL_VS,
97  DEMENTHON_VIRTUAL_VS,
99  LAGRANGE_VIRTUAL_VS
102 
104  PREFILTER_DUPLICATE_POINTS = 0x1,
105  PREFILTER_ALMOST_DUPLICATE_POINTS = 0x2,
108  PREFILTER_DEGENERATE_POINTS = 0x4,
111  CHECK_DEGENERATE_POINTS = 0x8
112  };
113 
114  unsigned int npt;
115  std::list<vpPoint> listP;
116 
117  double residual;
118 
119 protected:
120  double lambda;
121 
122 private:
124  int vvsIterMax;
126  std::vector<vpPoint> c3d;
128  bool computeCovariance;
130  vpMatrix covarianceMatrix;
133  unsigned int ransacNbInlierConsensus;
135  int ransacMaxTrials;
137  std::vector<vpPoint> ransacInliers;
139  std::vector<unsigned int> ransacInlierIndex;
141  double ransacThreshold;
144  double distanceToPlaneForCoplanarityTest;
146  int ransacFlags;
149  std::vector<vpPoint> listOfPoints;
151  bool useParallelRansac;
153  int nbParallelRansacThreads;
156  double vvsEpsilon;
157 
158  // For parallel RANSAC
159  class RansacFunctor
160  {
161  public:
162  RansacFunctor(const vpHomogeneousMatrix &cMo_, const unsigned int ransacNbInlierConsensus_,
163  const int ransacMaxTrials_, const double ransacThreshold_, const unsigned int initial_seed_,
164  const bool checkDegeneratePoints_, const std::vector<vpPoint> &listOfUniquePoints_,
165  bool (*func_)(vpHomogeneousMatrix *))
166  : m_best_consensus(), m_checkDegeneratePoints(checkDegeneratePoints_), m_cMo(cMo_), m_foundSolution(false),
167  m_func(func_), m_initial_seed(initial_seed_), m_listOfUniquePoints(listOfUniquePoints_), m_nbInliers(0),
168  m_ransacMaxTrials(ransacMaxTrials_), m_ransacNbInlierConsensus(ransacNbInlierConsensus_),
169  m_ransacThreshold(ransacThreshold_)
170  {
171  }
172 
173  RansacFunctor()
174  : m_best_consensus(), m_checkDegeneratePoints(false), m_cMo(), m_foundSolution(false), m_func(NULL),
175  m_initial_seed(0), m_listOfUniquePoints(), m_nbInliers(0), m_ransacMaxTrials(), m_ransacNbInlierConsensus(),
176  m_ransacThreshold()
177  {
178  }
179 
180  void operator()() { m_foundSolution = poseRansacImpl(); }
181 
182  // Access the return value.
183  bool getResult() const { return m_foundSolution; }
184 
185  std::vector<unsigned int> getBestConsensus() const { return m_best_consensus; }
186 
187  vpHomogeneousMatrix getEstimatedPose() const { return m_cMo; }
188 
189  unsigned int getNbInliers() const { return m_nbInliers; }
190 
191  private:
192  std::vector<unsigned int> m_best_consensus;
193  bool m_checkDegeneratePoints;
194  vpHomogeneousMatrix m_cMo;
195  bool m_foundSolution;
196  bool (*m_func)(vpHomogeneousMatrix *);
197  unsigned int m_initial_seed;
198  std::vector<vpPoint> m_listOfUniquePoints;
199  unsigned int m_nbInliers;
200  int m_ransacMaxTrials;
201  unsigned int m_ransacNbInlierConsensus;
202  double m_ransacThreshold;
203 
204  bool poseRansacImpl();
205  };
206 
207 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
208  static vpThread::Return poseRansacImplThread(vpThread::Args arg);
209 #endif
210 
211 protected:
212  double computeResidualDementhon(const vpHomogeneousMatrix &cMo);
213 
214  // method used in poseDementhonPlan()
215  int calculArbreDementhon(vpMatrix &b, vpColVector &U, vpHomogeneousMatrix &cMo);
216 
217 public:
218  vpPose();
219  virtual ~vpPose();
220  void addPoint(const vpPoint &P);
221  void addPoints(const std::vector<vpPoint> &lP);
222  void clearPoint();
223 
224  bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool (*func)(vpHomogeneousMatrix *) = NULL);
225  double computeResidual(const vpHomogeneousMatrix &cMo) const;
226  bool coplanar(int &coplanar_plane_type);
227  void displayModel(vpImage<unsigned char> &I, vpCameraParameters &cam, vpColor col = vpColor::none);
228  void displayModel(vpImage<vpRGBa> &I, vpCameraParameters &cam, vpColor col = vpColor::none);
229  void init();
230  void poseDementhonPlan(vpHomogeneousMatrix &cMo);
231  void poseDementhonNonPlan(vpHomogeneousMatrix &cMo);
232  void poseLagrangePlan(vpHomogeneousMatrix &cMo, const int coplanar_plane_type = 0);
233  void poseLagrangeNonPlan(vpHomogeneousMatrix &cMo);
234  void poseLowe(vpHomogeneousMatrix &cMo);
235  bool poseRansac(vpHomogeneousMatrix &cMo, bool (*func)(vpHomogeneousMatrix *) = NULL);
236  void poseVirtualVSrobust(vpHomogeneousMatrix &cMo);
237  void poseVirtualVS(vpHomogeneousMatrix &cMo);
238  void printPoint();
239  void setDistanceToPlaneForCoplanarityTest(double d);
240  void setLambda(double a) { lambda = a; }
241  void setVvsEpsilon(const double eps)
242  {
243  if (eps >= 0) {
244  vvsEpsilon = eps;
245  } else {
246  throw vpException(vpException::badValue, "Epsilon value must be >= 0.");
247  }
248  }
249  void setVvsIterMax(int nb) { vvsIterMax = nb; }
250 
251  void setRansacNbInliersToReachConsensus(const unsigned int &nbC) { ransacNbInlierConsensus = nbC; }
252  void setRansacThreshold(const double &t)
253  {
254  // Test whether or not t is > 0
255  if (t > std::numeric_limits<double>::epsilon()) {
256  ransacThreshold = t;
257  } else {
258  throw vpException(vpException::badValue, "The Ransac threshold must be positive as we deal with distance.");
259  }
260  }
261  void setRansacMaxTrials(const int &rM) { ransacMaxTrials = rM; }
262  unsigned int getRansacNbInliers() const { return (unsigned int)ransacInliers.size(); }
263  std::vector<unsigned int> getRansacInlierIndex() const { return ransacInlierIndex; }
264  std::vector<vpPoint> getRansacInliers() const { return ransacInliers; }
265 
272  void setCovarianceComputation(const bool &flag) { computeCovariance = flag; }
273 
284  {
285  if (!computeCovariance)
286  vpTRACE("Warning : The covariance matrix has not been computed. See "
287  "setCovarianceComputation() to do it.");
288 
289  return covarianceMatrix;
290  }
291 
299  inline void setRansacFilterFlags(const int flags) { ransacFlags = flags; }
300 
306  inline int getNbParallelRansacThreads() const { return nbParallelRansacThreads; }
307 
315  inline void setNbParallelRansacThreads(const int nb) { nbParallelRansacThreads = nb; }
316 
322  inline bool getUseParallelRansac() const { return useParallelRansac; }
323 
329  inline void setUseParallelRansac(const bool use) { useParallelRansac = use; }
330 
336  std::vector<vpPoint> getPoints() const
337  {
338  std::vector<vpPoint> vectorOfPoints(listP.begin(), listP.end());
339  return vectorOfPoints;
340  }
341 
342  static void display(vpImage<unsigned char> &I, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, double size,
343  vpColor col = vpColor::none);
344  static void display(vpImage<vpRGBa> &I, vpHomogeneousMatrix &cMo, vpCameraParameters &cam, double size,
345  vpColor col = vpColor::none);
346  static double poseFromRectangle(vpPoint &p1, vpPoint &p2, vpPoint &p3, vpPoint &p4, double lx,
348 
349  static int computeRansacIterations(double probability, double epsilon, const int sampleSize = 4,
350  int maxIterations = 2000);
351 
352  static void findMatch(std::vector<vpPoint> &p2D, std::vector<vpPoint> &p3D,
353  const unsigned int &numberOfInlierToReachAConsensus, const double &threshold,
354  unsigned int &ninliers, std::vector<vpPoint> &listInliers, vpHomogeneousMatrix &cMo,
355  const int &maxNbTrials = 10000);
356 };
357 
358 #endif
359 
360 /*
361  * Local variables:
362  * c-basic-offset: 2
363  * End:
364  */
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:104
double residual
Residual in meter.
Definition: vpPose.h:117
std::vector< unsigned int > getRansacInlierIndex() const
Definition: vpPose.h:263
std::vector< vpPoint > getPoints() const
Definition: vpPose.h:336
void * Return
Definition: vpThread.h:41
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class to define colors available for display functionnalities.
Definition: vpColor.h:120
bool getUseParallelRansac() const
Definition: vpPose.h:322
static const vpColor none
Definition: vpColor.h:192
error that can be emited by ViSP classes.
Definition: vpException.h:71
void setRansacThreshold(const double &t)
Definition: vpPose.h:252
std::list< vpPoint > listP
Array of point (use here class vpPoint)
Definition: vpPose.h:115
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:264
Class that defines what is a point.
Definition: vpPoint.h:58
double lambda
parameters use for the virtual visual servoing approach
Definition: vpPose.h:120
vpMatrix getCovarianceMatrix() const
Definition: vpPose.h:283
int getNbParallelRansacThreads() const
Definition: vpPose.h:306
FILTERING_RANSAC_FLAGS
Definition: vpPose.h:103
#define vpTRACE
Definition: vpDebug.h:416
void * Args
Definition: vpThread.h:40
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:79
void setNbParallelRansacThreads(const int nb)
Definition: vpPose.h:315
Generic class defining intrinsic camera parameters.
void setVvsIterMax(int nb)
Definition: vpPose.h:249
unsigned int npt
Number of point used in pose computation.
Definition: vpPose.h:114
void setRansacMaxTrials(const int &rM)
Definition: vpPose.h:261
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:251
void setLambda(double a)
Definition: vpPose.h:240
void setUseParallelRansac(const bool use)
Definition: vpPose.h:329
unsigned int getRansacNbInliers() const
Definition: vpPose.h:262
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setRansacFilterFlags(const int flags)
Definition: vpPose.h:299
vpPoseMethodType
Methods that could be used to estimate the pose from points.
Definition: vpPose.h:83
void setVvsEpsilon(const double eps)
Definition: vpPose.h:241
void setCovarianceComputation(const bool &flag)
Definition: vpPose.h:272