Visual Servoing Platform  version 3.0.0
vpMbTracker.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 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  * Generic model based tracker. This class declares the methods to implement in
32  * order to have a model based tracker.
33  *
34  * Authors:
35  * Romain Tallonneau
36  * Aurelien Yol
37  *
38  *****************************************************************************/
39 
44 #ifndef vpMbTracker_hh
45 #define vpMbTracker_hh
46 
47 #include <vector>
48 #include <string>
49 #include <sstream>
50 #include <fstream>
51 #include <map>
52 #include <algorithm>
53 #include <cctype>
54 #include <locale>
55 #include <functional> // std::not1
56 
57 
58 #include <visp3/core/vpHomogeneousMatrix.h>
59 #include <visp3/core/vpImage.h>
60 #include <visp3/core/vpImagePoint.h>
61 #include <visp3/core/vpColVector.h>
62 #include <visp3/core/vpMatrix.h>
63 #include <visp3/core/vpRGBa.h>
64 #include <visp3/core/vpCameraParameters.h>
65 #include <visp3/core/vpPoint.h>
66 #include <visp3/mbt/vpMbtPolygon.h>
67 #include <visp3/mbt/vpMbHiddenFaces.h>
68 #include <visp3/core/vpPolygon.h>
69 
70 #ifdef VISP_HAVE_COIN3D
71 //Work around to avoid type redefinition int8_t with Coin
72 // #if defined(_WIN32) && defined(VISP_HAVE_OGRE) && (_MSC_VER >= 1600) // Visual Studio 2010
73 // #define HAVE_INT8_T 1
74 // #endif
75 
76 //Inventor includes
77 # include <Inventor/VRMLnodes/SoVRMLGroup.h>
78 # include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
79 # include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
80 #endif
81 
103 class VISP_EXPORT vpMbTracker
104 {
105 public:
106  typedef enum {
107  GAUSS_NEWTON_OPT = 0,
108  LEVENBERG_MARQUARDT_OPT = 1
109  } vpMbtOptimizationMethod;
110 
111 protected:
121  std::string modelFileName;
125  std::string poseSavingFilename;
142 
146  double angleAppears;
150  double distNearClip;
152  double distFarClip;
154  unsigned int clippingFlag;
156  bool useOgre;
161  unsigned int nbPoints;
163  unsigned int nbLines;
165  unsigned int nbPolygonLines;
167  unsigned int nbPolygonPoints;
169  unsigned int nbCylinders;
171  unsigned int nbCircles;
181  std::map<std::string, std::string> mapOfParameterNames;
182 
183 public:
184  vpMbTracker();
185  virtual ~vpMbTracker();
186 
198  virtual void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
199  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
211  virtual void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
212  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
213 
215  virtual inline double getAngleAppear() const { return angleAppears; }
216 
218  virtual inline double getAngleDisappear() const { return angleDisappears; }
219 
225  virtual void getCameraParameters(vpCameraParameters& camera) const { camera = this->cam;}
226 
232  virtual inline unsigned int getClipping() const { return clippingFlag; }
233 
237  virtual vpMatrix getCovarianceMatrix() const {
238  if(!computeCovariance)
239  vpTRACE("Warning : The covariance matrix has not been computed. See setCovarianceComputation() to do it.");
240 
241  return covarianceMatrix;
242  }
243 
249  virtual double getProjectionError() const { return projectionError; }
250 
251  virtual vpColVector getEstimatedDoF();
252 
266  virtual vpColVector getError() {
267  return m_error;
268  }
269 
271  inline vpMbHiddenFaces<vpMbtPolygon>& getFaces() { return faces;}
272 
278  virtual inline double getFarClippingDistance() const { return distFarClip; }
279 
301  return m_w;
302  }
303 
309  virtual inline unsigned int getNbPolygon() const {
310  return static_cast<unsigned int>(faces.size());
311  }
312 
318  virtual inline double getNearClippingDistance() const { return distNearClip; }
319 
327  virtual inline vpMbtOptimizationMethod getOptimizationMethod() const { return m_optimizationMethod; }
328 
338  virtual inline vpMbtPolygon* getPolygon(const unsigned int index) {
339  if(index >= static_cast<unsigned int>(faces.size()) ){
340  throw vpException(vpException::dimensionError, "index out of range");
341  }
342 
343  return faces[index];
344  }
345 
346  virtual std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > getPolygonFaces(const bool orderPolygons=true,
347  const bool useVisibility=true);
348 
356  inline void getPose(vpHomogeneousMatrix& cMo_) const {cMo_ = this->cMo;}
357 
365  inline vpHomogeneousMatrix getPose() const {return this->cMo;}
366 
367  /* PURE VIRTUAL METHODS */
368 
374  virtual void init(const vpImage<unsigned char>& I)=0;
375 
376  // Intializer
377 
378 #ifdef VISP_HAVE_MODULE_GUI
379  virtual void initClick( const vpImage<unsigned char>& I, const std::string& initFile, const bool displayHelp = false );
380  virtual void initClick( const vpImage<unsigned char>& I, const std::vector<vpPoint> &points3D_list,
381  const std::string &displayFile = "" );
382 #endif
383 
384  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::string& initFile );
385  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::vector<vpImagePoint> &points2D_list, const std::vector<vpPoint> &points3D_list );
386 
387  virtual void initFromPose(const vpImage<unsigned char>& I, const std::string &initFile);
388  virtual void initFromPose(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo);
389  virtual void initFromPose(const vpImage<unsigned char>& I, const vpPoseVector &cPo);
390 
398  virtual void loadConfigFile(const std::string& configFile)=0;
399 
400  virtual void loadModel(const char *modelFile, const bool verbose=false);
401  virtual void loadModel(const std::string &modelFile, const bool verbose=false);
402 
406  virtual void resetTracker() = 0;
407 
408  void savePose(const std::string &filename);
409 
419  virtual inline void setAngleAppear(const double &a) { angleAppears = a; }
420 
430  virtual inline void setAngleDisappear(const double &a) { angleDisappears = a; }
431 
437  virtual void setCameraParameters(const vpCameraParameters& camera) {this->cam = camera;}
438 
439  virtual void setClipping(const unsigned int &flags);
440 
446  virtual void setCovarianceComputation(const bool& flag) { computeCovariance = flag; }
447 
459  void setDisplayFeatures(const bool displayF) {displayFeatures = displayF;}
460 
461  virtual void setEstimatedDoF(const vpColVector& v);
462 
463  virtual void setFarClippingDistance(const double &dist);
464 
465  virtual void setLod(const bool useLod, const std::string &name="");
466 
467  virtual void setMinLineLengthThresh(const double minLineLengthThresh, const std::string &name="");
468 
469  virtual void setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name="");
470 
471  virtual void setNearClippingDistance(const double &dist);
472 
478  virtual inline void setOptimizationMethod(const vpMbtOptimizationMethod &opt) { m_optimizationMethod = opt; }
479 
489  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo) = 0;
490 
500  inline void setPoseSavingFilename(const std::string& filename){
501  poseSavingFilename = filename;
502  }
503 
509  virtual void setProjectionErrorComputation(const bool &flag) { computeProjError = flag; }
510 
511  virtual void setScanLineVisibilityTest(const bool &v){ useScanLine = v; }
512 
523  inline void setOgreShowConfigDialog(const bool showConfigDialog){
524  ogreShowConfigDialog = showConfigDialog;
525  }
526 
527  virtual void setOgreVisibilityTest(const bool &v);
528 
529 #ifdef VISP_HAVE_OGRE
530 
538  void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts) { faces.setNbRayCastingAttemptsForVisibility(attempts); }
539 
547  void setGoodNbRayCastingAttemptsRatio(const double &ratio){ faces.setGoodNbRayCastingAttemptsRatio(ratio); }
548 #endif
549 
555  virtual void testTracking() = 0;
556 
562  virtual void track(const vpImage<unsigned char>& I)=0;
563 
564 protected:
565  void addPolygon(const std::vector<vpPoint>& corners, const int idFace=-1, const std::string &polygonName="",
566  const bool useLod=false, const double minPolygonAreaThreshold=2500.0, const double minLineLengthThreshold=50.0);
567  void addPolygon(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=-1,
568  const std::string &polygonName="", const bool useLod=false, const double minPolygonAreaThreshold=2500.0);
569  void addPolygon(const vpPoint& p1, const vpPoint &p2, const int idFace=-1, const std::string &polygonName="",
570  const bool useLod=false, const double minLineLengthThreshold=50);
571  void addPolygon(const std::vector<std::vector<vpPoint> > &listFaces, const int idFace=-1, const std::string &polygonName="",
572  const bool useLod=false, const double minLineLengthThreshold=50);
573 
574  void createCylinderBBox(const vpPoint& p1, const vpPoint &p2, const double &radius, std::vector<std::vector<vpPoint> > &listFaces);
575 
576  void computeJTR(const vpMatrix& J, const vpColVector& R, vpColVector& JTR);
577 
578 #ifdef VISP_HAVE_COIN3D
579  virtual void extractGroup(SoVRMLGroup *sceneGraphVRML2, vpHomogeneousMatrix &transform, int &idFace);
580  virtual void extractFaces(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace, const std::string &polygonName="");
581  virtual void extractLines(SoVRMLIndexedLineSet* line_set, int &idFace, const std::string &polygonName="");
582  virtual void extractCylinders(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace, const std::string &polygonName="");
583 #endif
584 
585  vpPoint getGravityCenter(const std::vector<vpPoint>& _pts);
586 
598  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
599  const int idFace=0, const std::string &name="")=0;
610  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
611  const std::string &name="")=0;
612 
623  virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0;
624  virtual void initFaceFromLines(vpMbtPolygon &polygon)=0;
625 
626  virtual void loadVRMLModel(const std::string& modelFile);
627  virtual void loadCAOModel(const std::string& modelFile, std::vector<std::string>& vectorOfModelFilename, int& startIdFace,
628  const bool verbose=false, const bool parent=true);
629 
630  void removeComment(std::ifstream& fileId);
631 
632  inline bool parseBoolean(std::string &input) {
633  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
634  std::istringstream is(input);
635  bool b;
636  //Parse string to boolean either in the textual representation (True/False)
637  //or in numeric representation (1/0)
638  is >> (input.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
639  return b;
640  }
641 
642  std::map<std::string, std::string> parseParameters(std::string& endLine);
643 
644  inline std::string &ltrim(std::string &s) {
645  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
646  return s;
647  }
648 
649  inline std::string &rtrim(std::string &s) {
650  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
651  return s;
652  }
653 
654  inline std::string &trim(std::string &s) {
655  return ltrim(rtrim(s));
656  }
657 };
658 
659 
660 #endif
661 
bool computeProjError
Flag used to specify if the gradient error criteria has to be computed or not.
Definition: vpMbTracker.h:131
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Definition: vpMbTracker.h:547
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:446
std::string & rtrim(std::string &s)
Definition: vpMbTracker.h:649
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:92
vpMatrix covarianceMatrix
Covariance matrix.
Definition: vpMbTracker.h:129
bool parseBoolean(std::string &input)
Definition: vpMbTracker.h:632
std::string & trim(std::string &s)
Definition: vpMbTracker.h:654
unsigned int nbLines
Number of lines in CAO model.
Definition: vpMbTracker.h:163
virtual double getAngleAppear() const
Definition: vpMbTracker.h:215
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:232
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:430
unsigned int nbCircles
Number of circles in CAO model.
Definition: vpMbTracker.h:171
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:144
virtual vpMbtOptimizationMethod getOptimizationMethod() const
Definition: vpMbTracker.h:327
Implementation of an homogeneous matrix and operations on such kind of matrices.
std::map< std::string, std::string > mapOfParameterNames
Map with [map.first]=parameter_names and [map.second]=type (string, number or boolean) ...
Definition: vpMbTracker.h:181
unsigned int nbCylinders
Number of cylinders in CAO model.
Definition: vpMbTracker.h:169
unsigned int nbPoints
Number of points in CAO model.
Definition: vpMbTracker.h:161
virtual void setOptimizationMethod(const vpMbtOptimizationMethod &opt)
Definition: vpMbTracker.h:478
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:115
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
Definition: vpMbTracker.h:123
vpHomogeneousMatrix getPose() const
Definition: vpMbTracker.h:365
error that can be emited by ViSP classes.
Definition: vpException.h:73
bool useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:156
std::string modelFileName
The name of the file containing the model (it is used to create a file name.0.pos used to store the c...
Definition: vpMbTracker.h:121
bool computeCovariance
Flag used to specify if the covariance matrix has to be computed or not.
Definition: vpMbTracker.h:127
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Class that defines what is a point.
Definition: vpPoint.h:59
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
virtual double getAngleDisappear() const
Definition: vpMbTracker.h:218
void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:523
double distFarClip
Distance for near clipping.
Definition: vpMbTracker.h:152
double projectionError
Error angle between the gradient direction of the model features projected at the resulting pose and ...
Definition: vpMbTracker.h:133
vpMatrix oJo
The Degrees of Freedom to estimate.
Definition: vpMbTracker.h:117
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:64
virtual void setCameraParameters(const vpCameraParameters &camera)
Definition: vpMbTracker.h:437
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:356
bool useScanLine
Use Scanline for visibility tests.
Definition: vpMbTracker.h:159
#define vpTRACE
Definition: vpDebug.h:414
std::string & ltrim(std::string &s)
Definition: vpMbTracker.h:644
double minLineLengthThresholdGeneral
Minimum line length threshold for LOD mode (general setting)
Definition: vpMbTracker.h:177
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
vpColVector m_error
Error s-s*.
Definition: vpMbTracker.h:139
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:225
virtual double getProjectionError() const
Definition: vpMbTracker.h:249
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
unsigned int nbPolygonLines
Number of polygon lines in CAO model.
Definition: vpMbTracker.h:165
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:419
vpMbtOptimizationMethod m_optimizationMethod
Optimization method used.
Definition: vpMbTracker.h:141
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:146
void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts)
Definition: vpMbTracker.h:538
virtual vpColVector getError()
Definition: vpMbTracker.h:266
std::string poseSavingFilename
Filename used to save the initial pose computed using the initClick() method. It is also used to read...
Definition: vpMbTracker.h:125
unsigned int size() const
virtual double getNearClippingDistance() const
Definition: vpMbTracker.h:318
unsigned int nbPolygonPoints
Number of polygon points in CAO model.
Definition: vpMbTracker.h:167
double minPolygonAreaThresholdGeneral
Minimum polygon area threshold for LOD mode (general setting)
Definition: vpMbTracker.h:179
bool displayFeatures
If true, the features are displayed.
Definition: vpMbTracker.h:135
bool ogreShowConfigDialog
Definition: vpMbTracker.h:157
virtual vpColVector getRobustWeights()
Definition: vpMbTracker.h:300
bool applyLodSettingInConfig
True if the CAO model is loaded before the call to loadConfigFile, (deduced by the number of polygons...
Definition: vpMbTracker.h:175
Implementation of column vector and the associated operations.
Definition: vpColVector.h:72
Implementation of a pose vector and operations on poses.
Definition: vpPoseVector.h:93
virtual vpMbtPolygon * getPolygon(const unsigned int index)
Definition: vpMbTracker.h:338
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:148
virtual unsigned int getNbPolygon() const
Definition: vpMbTracker.h:309
void setPoseSavingFilename(const std::string &filename)
Definition: vpMbTracker.h:500
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:511
virtual double getFarClippingDistance() const
Definition: vpMbTracker.h:278
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:459
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:154
double distNearClip
Distance for near clipping.
Definition: vpMbTracker.h:150
vpMbHiddenFaces< vpMbtPolygon > & getFaces()
Definition: vpMbTracker.h:271
bool useLodGeneral
True if LOD mode is enabled.
Definition: vpMbTracker.h:173
bool isoJoIdentity
Boolean to know if oJo is identity (for fast computation)
Definition: vpMbTracker.h:119
vpColVector m_w
Weights used in the robust scheme.
Definition: vpMbTracker.h:137
virtual void setProjectionErrorComputation(const bool &flag)
Definition: vpMbTracker.h:509
virtual vpMatrix getCovarianceMatrix() const
Definition: vpMbTracker.h:237