Visual Servoing Platform  version 3.0.1
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
vpMbTracker.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  * 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 
189 
191  virtual inline double getAngleAppear() const { return angleAppears; }
192 
194  virtual inline double getAngleDisappear() const { return angleDisappears; }
195 
201  virtual void getCameraParameters(vpCameraParameters& camera) const { camera = this->cam;}
202 
208  virtual inline unsigned int getClipping() const { return clippingFlag; }
209 
213  virtual vpMatrix getCovarianceMatrix() const {
214  if(!computeCovariance)
215  vpTRACE("Warning : The covariance matrix has not been computed. See setCovarianceComputation() to do it.");
216 
217  return covarianceMatrix;
218  }
219 
226  virtual double getProjectionError() const { return projectionError; }
227 
228  virtual vpColVector getEstimatedDoF() const;
229 
243  virtual vpColVector getError() const {
244  return m_error;
245  }
246 
248  virtual inline vpMbHiddenFaces<vpMbtPolygon>& getFaces() { return faces;}
249 
255  virtual inline double getFarClippingDistance() const { return distFarClip; }
256 
277  virtual vpColVector getRobustWeights() const {
278  return m_w;
279  }
280 
286  virtual inline unsigned int getNbPolygon() const {
287  return static_cast<unsigned int>(faces.size());
288  }
289 
295  virtual inline double getNearClippingDistance() const { return distNearClip; }
296 
304  virtual inline vpMbtOptimizationMethod getOptimizationMethod() const { return m_optimizationMethod; }
305 
315  virtual inline vpMbtPolygon* getPolygon(const unsigned int index) {
316  if(index >= static_cast<unsigned int>(faces.size()) ){
317  throw vpException(vpException::dimensionError, "index out of range");
318  }
319 
320  return faces[index];
321  }
322 
323  virtual std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > getPolygonFaces(const bool orderPolygons=true,
324  const bool useVisibility=true, const bool clipPolygon=false);
325 
333  virtual inline void getPose(vpHomogeneousMatrix& cMo_) const {cMo_ = this->cMo;}
334 
342  virtual inline vpHomogeneousMatrix getPose() const {return this->cMo;}
343 
344  // Intializer
345 
346 #ifdef VISP_HAVE_MODULE_GUI
347  virtual void initClick( const vpImage<unsigned char>& I, const std::string& initFile, const bool displayHelp = false );
348  virtual void initClick( const vpImage<unsigned char>& I, const std::vector<vpPoint> &points3D_list,
349  const std::string &displayFile = "" );
350 #endif
351 
352  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::string& initFile );
353  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::vector<vpImagePoint> &points2D_list, const std::vector<vpPoint> &points3D_list );
354 
355  virtual void initFromPose(const vpImage<unsigned char>& I, const std::string &initFile);
356  virtual void initFromPose(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo);
357  virtual void initFromPose(const vpImage<unsigned char>& I, const vpPoseVector &cPo);
358 
359  virtual void loadModel(const char *modelFile, const bool verbose=false);
360  virtual void loadModel(const std::string &modelFile, const bool verbose=false);
361 
371  virtual inline void setAngleAppear(const double &a) { angleAppears = a; }
372 
382  virtual inline void setAngleDisappear(const double &a) { angleDisappears = a; }
383 
389  virtual void setCameraParameters(const vpCameraParameters& camera) {this->cam = camera;}
390 
391  virtual void setClipping(const unsigned int &flags);
392 
398  virtual void setCovarianceComputation(const bool& flag) { computeCovariance = flag; }
399 
411  virtual void setDisplayFeatures(const bool displayF) {displayFeatures = displayF;}
412 
413  virtual void setEstimatedDoF(const vpColVector& v);
414 
415  virtual void setFarClippingDistance(const double &dist);
416 
417  virtual void setLod(const bool useLod, const std::string &name="");
418 
419  virtual void setMinLineLengthThresh(const double minLineLengthThresh, const std::string &name="");
420 
421  virtual void setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name="");
422 
423  virtual void setNearClippingDistance(const double &dist);
424 
430  virtual inline void setOptimizationMethod(const vpMbtOptimizationMethod &opt) { m_optimizationMethod = opt; }
431 
437  virtual void setProjectionErrorComputation(const bool &flag) { computeProjError = flag; }
438 
439  virtual void setScanLineVisibilityTest(const bool &v){ useScanLine = v; }
440 
441  virtual void setOgreVisibilityTest(const bool &v);
442 
443  void savePose(const std::string &filename) const;
444 
445 #ifdef VISP_HAVE_OGRE
446 
453  void setGoodNbRayCastingAttemptsRatio(const double &ratio) {
454  faces.setGoodNbRayCastingAttemptsRatio(ratio);
455  }
464  void setNbRayCastingAttemptsForVisibility(const unsigned int &attempts) {
465  faces.setNbRayCastingAttemptsForVisibility(attempts);
466  }
467 #endif
468 
479  inline void setOgreShowConfigDialog(const bool showConfigDialog){
480  ogreShowConfigDialog = showConfigDialog;
481  }
482 
492  inline void setPoseSavingFilename(const std::string& filename){
493  poseSavingFilename = filename;
494  }
495 
496  /* PURE VIRTUAL METHODS */
497 
509  virtual void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
510  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
522  virtual void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
523  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
524 
530  virtual void init(const vpImage<unsigned char>& I)=0;
531 
539  virtual void loadConfigFile(const std::string& configFile)=0;
540 
544  virtual void resetTracker() = 0;
545 
555  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo) = 0;
556 
562  virtual void testTracking() = 0;
563 
569  virtual void track(const vpImage<unsigned char>& I)=0;
571 
572 protected:
575  void addPolygon(const std::vector<vpPoint>& corners, const int idFace=-1, const std::string &polygonName="",
576  const bool useLod=false, const double minPolygonAreaThreshold=2500.0, const double minLineLengthThreshold=50.0);
577  void addPolygon(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=-1,
578  const std::string &polygonName="", const bool useLod=false, const double minPolygonAreaThreshold=2500.0);
579  void addPolygon(const vpPoint& p1, const vpPoint &p2, const int idFace=-1, const std::string &polygonName="",
580  const bool useLod=false, const double minLineLengthThreshold=50);
581  void addPolygon(const std::vector<std::vector<vpPoint> > &listFaces, const int idFace=-1, const std::string &polygonName="",
582  const bool useLod=false, const double minLineLengthThreshold=50);
583 
584  void createCylinderBBox(const vpPoint& p1, const vpPoint &p2, const double &radius, std::vector<std::vector<vpPoint> > &listFaces);
585 
586  void computeJTR(const vpMatrix& J, const vpColVector& R, vpColVector& JTR) const;
587 
588 #ifdef VISP_HAVE_COIN3D
589  virtual void extractGroup(SoVRMLGroup *sceneGraphVRML2, vpHomogeneousMatrix &transform, int &idFace);
590  virtual void extractFaces(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace, const std::string &polygonName="");
591  virtual void extractLines(SoVRMLIndexedLineSet* line_set, int &idFace, const std::string &polygonName="");
592  virtual void extractCylinders(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace, const std::string &polygonName="");
593 #endif
594 
595  vpPoint getGravityCenter(const std::vector<vpPoint>& _pts) const;
596 
609  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
610  const int idFace=0, const std::string &name="")=0;
621  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
622  const std::string &name="")=0;
623 
634  virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0;
635  virtual void initFaceFromLines(vpMbtPolygon &polygon)=0;
636 
637  virtual void loadVRMLModel(const std::string& modelFile);
638  virtual void loadCAOModel(const std::string& modelFile, std::vector<std::string>& vectorOfModelFilename, int& startIdFace,
639  const bool verbose=false, const bool parent=true);
640 
641  void removeComment(std::ifstream& fileId);
642 
643  inline bool parseBoolean(std::string &input) {
644  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
645  std::istringstream is(input);
646  bool b;
647  //Parse string to boolean either in the textual representation (True/False)
648  //or in numeric representation (1/0)
649  is >> (input.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
650  return b;
651  }
652 
653  std::map<std::string, std::string> parseParameters(std::string& endLine);
654 
655  inline std::string &ltrim(std::string &s) const {
656  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
657  return s;
658  }
659 
660  inline std::string &rtrim(std::string &s) const {
661  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
662  return s;
663  }
664 
665  inline std::string &trim(std::string &s) const {
666  return ltrim(rtrim(s));
667  }
669 };
670 
671 
672 #endif
673 
bool computeProjError
Flag used to specify if the gradient error criteria has to be computed or not.
Definition: vpMbTracker.h:131
virtual void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:411
void setGoodNbRayCastingAttemptsRatio(const double &ratio)
Definition: vpMbTracker.h:453
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:398
Implementation of a matrix and operations on matrices.
Definition: vpMatrix.h:97
vpMatrix covarianceMatrix
Covariance matrix.
Definition: vpMbTracker.h:129
bool parseBoolean(std::string &input)
Definition: vpMbTracker.h:643
unsigned int nbLines
Number of lines in CAO model.
Definition: vpMbTracker.h:163
virtual double getAngleAppear() const
Definition: vpMbTracker.h:191
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:208
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:382
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:304
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:430
Class to define colors available for display functionnalities.
Definition: vpColor.h:121
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:115
std::string & rtrim(std::string &s) const
Definition: vpMbTracker.h:660
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
Definition: vpMbTracker.h:123
virtual vpColVector getRobustWeights() const
Definition: vpMbTracker.h:277
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
Class that defines what is a point.
Definition: vpPoint.h:59
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:113
std::string & ltrim(std::string &s) const
Definition: vpMbTracker.h:655
virtual double getAngleDisappear() const
Definition: vpMbTracker.h:194
void setOgreShowConfigDialog(const bool showConfigDialog)
Definition: vpMbTracker.h:479
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:389
virtual vpHomogeneousMatrix getPose() const
Definition: vpMbTracker.h:342
bool useScanLine
Use Scanline for visibility tests.
Definition: vpMbTracker.h:159
#define vpTRACE
Definition: vpDebug.h:414
double minLineLengthThresholdGeneral
Minimum line length threshold for LOD mode (general setting)
Definition: vpMbTracker.h:177
vpColVector m_error
Error s-s*.
Definition: vpMbTracker.h:139
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:201
virtual double getProjectionError() const
Definition: vpMbTracker.h:226
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
std::string & trim(std::string &s) const
Definition: vpMbTracker.h:665
unsigned int nbPolygonLines
Number of polygon lines in CAO model.
Definition: vpMbTracker.h:165
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:371
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:464
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
virtual double getNearClippingDistance() const
Definition: vpMbTracker.h:295
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
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 void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:333
virtual vpMbtPolygon * getPolygon(const unsigned int index)
Definition: vpMbTracker.h:315
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:148
virtual unsigned int getNbPolygon() const
Definition: vpMbTracker.h:286
void setPoseSavingFilename(const std::string &filename)
Definition: vpMbTracker.h:492
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:439
virtual double getFarClippingDistance() const
Definition: vpMbTracker.h:255
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:154
double distNearClip
Distance for near clipping.
Definition: vpMbTracker.h:150
bool useLodGeneral
True if LOD mode is enabled.
Definition: vpMbTracker.h:173
virtual vpMbHiddenFaces< vpMbtPolygon > & getFaces()
Definition: vpMbTracker.h:248
virtual vpColVector getError() const
Definition: vpMbTracker.h:243
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:437
virtual vpMatrix getCovarianceMatrix() const
Definition: vpMbTracker.h:213