Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Francois Chaumette
36  * Aurelien Yol
37  *
38  *****************************************************************************/
39 
48 #ifndef vpPOSE_HH
49 #define vpPOSE_HH
50 
51 #include <visp3/core/vpHomogeneousMatrix.h>
52 #include <visp3/vision/vpHomography.h>
53 #include <visp3/core/vpPoint.h>
54 #include <visp3/core/vpRGBa.h>
55 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
56 # include <visp3/core/vpList.h>
57 #endif
58 #include <visp3/core/vpThread.h>
59 
60 #include <math.h>
61 #include <list>
62 #include <vector>
63 
76 class VISP_EXPORT vpPose
77 {
78 public:
80  typedef enum
81  {
84  LOWE ,
85  RANSAC ,
90  LAGRANGE_VIRTUAL_VS
91  } vpPoseMethodType;
92 
94  PREFILTER_DUPLICATE_POINTS = 0x1,
95  PREFILTER_ALMOST_DUPLICATE_POINTS = 0x2,
96  PREFILTER_DEGENERATE_POINTS = 0x4,
97  CHECK_DEGENERATE_POINTS = 0x8
98  };
99 
100  unsigned int npt ;
101  std::list<vpPoint> listP ;
102 
103  double residual ;
104 
105 protected :
106  double lambda ;
107 
108 private:
109  int vvsIterMax ;
110  std::vector<vpPoint> c3d ;
113  bool computeCovariance;
115  vpMatrix covarianceMatrix;
116 
117  unsigned int ransacNbInlierConsensus;
118  int ransacMaxTrials;
119  std::vector<vpPoint> ransacInliers;
120  std::vector<unsigned int> ransacInlierIndex;
121  double ransacThreshold;
122  double distanceToPlaneForCoplanarityTest;
123  int ransacFlags;
124  std::vector<vpPoint> listOfPoints;
125  bool useParallelRansac;
126  int nbParallelRansacThreads;
127 
128 
129  //For parallel RANSAC
130  class RansacFunctor {
131  public:
132  RansacFunctor(const vpHomogeneousMatrix &cMo_,
133  const unsigned int ransacNbInlierConsensus_, const int ransacMaxTrials_,
134  const double ransacThreshold_, const unsigned int initial_seed_,
135  const bool checkDegeneratePoints_, const std::vector<vpPoint> &listOfUniquePoints_,
136  bool (*func_)(vpHomogeneousMatrix *)) :
137  m_best_consensus(), m_checkDegeneratePoints(checkDegeneratePoints_), m_cMo(cMo_), m_foundSolution(false),
138  m_func(func_), m_initial_seed(initial_seed_), m_listOfUniquePoints(listOfUniquePoints_), m_nbInliers(0),
139  m_ransacMaxTrials(ransacMaxTrials_), m_ransacNbInlierConsensus(ransacNbInlierConsensus_), m_ransacThreshold(ransacThreshold_) {
140  }
141 
142  RansacFunctor() :
143  m_best_consensus(),m_checkDegeneratePoints(false), m_cMo(), m_foundSolution(false), m_func(NULL),
144  m_initial_seed(0), m_listOfUniquePoints(), m_nbInliers(0), m_ransacMaxTrials(), m_ransacNbInlierConsensus(),
145  m_ransacThreshold() {
146  }
147 
148  void operator()() {
149  m_foundSolution = poseRansacImpl();
150  }
151 
152  // Access the return value.
153  bool getResult() const {
154  return m_foundSolution;
155  }
156 
157  std::vector<unsigned int> getBestConsensus() const {
158  return m_best_consensus;
159  }
160 
161  vpHomogeneousMatrix getEstimatedPose() const {
162  return m_cMo;
163  }
164 
165  unsigned int getNbInliers() const {
166  return m_nbInliers;
167  }
168 
169  private:
170  std::vector<unsigned int> m_best_consensus;
171  bool m_checkDegeneratePoints;
172  vpHomogeneousMatrix m_cMo;
173  bool m_foundSolution;
174  bool (*m_func)(vpHomogeneousMatrix *);
175  unsigned int m_initial_seed;
176  std::vector<vpPoint> m_listOfUniquePoints;
177  unsigned int m_nbInliers;
178  int m_ransacMaxTrials;
179  unsigned int m_ransacNbInlierConsensus;
180  double m_ransacThreshold;
181 
182  bool poseRansacImpl();
183  };
184 
185 #if defined(VISP_HAVE_PTHREAD) || (defined(_WIN32) && !defined(WINRT_8_0))
186  static vpThread::Return poseRansacImplThread(vpThread::Args arg);
187 #endif
188 
189 
190 protected:
191  double computeResidualDementhon(const vpHomogeneousMatrix &cMo) ;
192 
193  // method used in poseDementhonPlan()
194  int calculArbreDementhon(vpMatrix &b, vpColVector &U, vpHomogeneousMatrix &cMo) ;
195 
196 public:
197  vpPose() ;
198  virtual ~vpPose() ;
199  void addPoint(const vpPoint& P) ;
200  void addPoints(const std::vector<vpPoint>& lP);
201  void clearPoint() ;
202 
203  bool computePose(vpPoseMethodType method, vpHomogeneousMatrix &cMo, bool (*func)(vpHomogeneousMatrix *)=NULL) ;
204  double computeResidual(const vpHomogeneousMatrix &cMo) const ;
205  bool coplanar(int &coplanar_plane_type) ;
206  void displayModel(vpImage<unsigned char> &I,
207  vpCameraParameters &cam,
208  vpColor col=vpColor::none) ;
209  void displayModel(vpImage<vpRGBa> &I,
210  vpCameraParameters &cam,
211  vpColor col=vpColor::none) ;
212  void init() ;
213  void poseDementhonPlan(vpHomogeneousMatrix &cMo) ;
214  void poseDementhonNonPlan(vpHomogeneousMatrix &cMo) ;
215  void poseLagrangePlan(vpHomogeneousMatrix &cMo, const int coplanar_plane_type=0) ;
216  void poseLagrangeNonPlan(vpHomogeneousMatrix &cMo) ;
217  void poseLowe(vpHomogeneousMatrix & cMo) ;
218  bool poseRansac(vpHomogeneousMatrix & cMo, bool (*func)(vpHomogeneousMatrix *)=NULL) ;
219  void poseVirtualVSrobust(vpHomogeneousMatrix & cMo) ;
220  void poseVirtualVS(vpHomogeneousMatrix & cMo) ;
221  void printPoint() ;
222  void setDistanceToPlaneForCoplanarityTest(double d) ;
223  void setLambda(double a) { lambda = a ; }
224  void setVvsIterMax(int nb) { vvsIterMax = nb ; }
225 
226  void setRansacNbInliersToReachConsensus(const unsigned int &nbC){ ransacNbInlierConsensus = nbC; }
227  void setRansacThreshold(const double &t) {
228  //Test whether or not t is > 0
229  if(t > 0) {
230  ransacThreshold = t;
231  } else {
232  throw vpException(vpException::badValue, "The Ransac threshold must be positive as we deal with distance.");
233  }
234  }
235  void setRansacMaxTrials(const int &rM){ ransacMaxTrials = rM; }
236  unsigned int getRansacNbInliers() const { return (unsigned int) ransacInliers.size(); }
237  std::vector<unsigned int> getRansacInlierIndex() const{ return ransacInlierIndex; }
238  std::vector<vpPoint> getRansacInliers() const{ return ransacInliers; }
239 
245  void setCovarianceComputation(const bool& flag) { computeCovariance = flag; }
246 
255  if(!computeCovariance)
256  vpTRACE("Warning : The covariance matrix has not been computed. See setCovarianceComputation() to do it.");
257 
258  return covarianceMatrix;
259  }
260 
267  inline void setRansacFilterFlags(const int flags) {
268  ransacFlags = flags;
269  }
270 
276  inline int getNbParallelRansacThreads() const {
277  return nbParallelRansacThreads;
278  }
279 
287  inline void setNbParallelRansacThreads(const int nb) {
288  nbParallelRansacThreads = nb;
289  }
290 
296  inline bool getUseParallelRansac() const {
297  return useParallelRansac;
298  }
299 
305  inline void setUseParallelRansac(const bool use) {
306  useParallelRansac = use;
307  }
308 
314  std::vector<vpPoint> getPoints() const {
315  std::vector<vpPoint> vectorOfPoints(listP.begin(), listP.end());
316  return vectorOfPoints;
317  }
318 
319  static void display(vpImage<unsigned char> &I, vpHomogeneousMatrix &cMo,
320  vpCameraParameters &cam, double size,
321  vpColor col=vpColor::none) ;
322  static void display(vpImage<vpRGBa> &I, vpHomogeneousMatrix &cMo,
323  vpCameraParameters &cam, double size,
324  vpColor col=vpColor::none) ;
325  static double poseFromRectangle(vpPoint &p1,vpPoint &p2,
326  vpPoint &p3,vpPoint &p4,
327  double lx, vpCameraParameters & cam,
328  vpHomogeneousMatrix & cMo) ;
329 
330  static int computeRansacIterations(double probability, double epsilon,
331  const int sampleSize=4, int maxIterations=2000);
332 
333  static void findMatch(std::vector<vpPoint> &p2D,
334  std::vector<vpPoint> &p3D,
335  const unsigned int &numberOfInlierToReachAConsensus,
336  const double &threshold,
337  unsigned int &ninliers,
338  std::vector<vpPoint> &listInliers,
339  vpHomogeneousMatrix &cMo,
340  const int &maxNbTrials = 10000);
341 } ;
342 
343 
344 #endif
345 
346 
347 /*
348  * Local variables:
349  * c-basic-offset: 2
350  * End:
351  */
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
std::vector< vpPoint > getPoints() const
Definition: vpPose.h:314
double residual
Residual in meter.
Definition: vpPose.h:103
bool getUseParallelRansac() const
Definition: vpPose.h:296
void * Return
Definition: vpThread.h:36
Implementation of an homogeneous matrix and operations on such kind of matrices.
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
static const vpColor none
Definition: vpColor.h:175
error that can be emited by ViSP classes.
Definition: vpException.h:73
void setRansacThreshold(const double &t)
Definition: vpPose.h:227
std::vector< unsigned int > getRansacInlierIndex() const
Definition: vpPose.h:237
std::list< vpPoint > listP
Array of point (use here class vpPoint)
Definition: vpPose.h:101
Class that defines what is a point.
Definition: vpPoint.h:59
double lambda
parameters use for the virtual visual servoing approach
Definition: vpPose.h:106
int getNbParallelRansacThreads() const
Definition: vpPose.h:276
FILTERING_RANSAC_FLAGS
Definition: vpPose.h:93
unsigned int getRansacNbInliers() const
Definition: vpPose.h:236
#define vpTRACE
Definition: vpDebug.h:414
void * Args
Definition: vpThread.h:35
Class used for pose computation from N points (pose from point only). Some of the algorithms implemen...
Definition: vpPose.h:76
void setNbParallelRansacThreads(const int nb)
Definition: vpPose.h:287
Generic class defining intrinsic camera parameters.
vpMatrix getCovarianceMatrix() const
Definition: vpPose.h:254
void setVvsIterMax(int nb)
Definition: vpPose.h:224
unsigned int npt
Number of point used in pose computation.
Definition: vpPose.h:100
void setRansacMaxTrials(const int &rM)
Definition: vpPose.h:235
void setRansacNbInliersToReachConsensus(const unsigned int &nbC)
Definition: vpPose.h:226
void setLambda(double a)
Definition: vpPose.h:223
void setUseParallelRansac(const bool use)
Definition: vpPose.h:305
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
void setRansacFilterFlags(const int flags)
Definition: vpPose.h:267
std::vector< vpPoint > getRansacInliers() const
Definition: vpPose.h:238
void setCovarianceComputation(const bool &flag)
Definition: vpPose.h:245