ViSP  2.10.0
vpMbTracker.h
1 /****************************************************************************
2  *
3  * $Id: vpMbTracker.h 5162 2015-01-14 19:09:23Z strinh $
4  *
5  * This file is part of the ViSP software.
6  * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7  *
8  * This software is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * ("GPL") version 2 as published by the Free Software Foundation.
11  * See the file LICENSE.txt at the root directory of this source
12  * distribution for additional information about the GNU GPL.
13  *
14  * For using ViSP with software that can not be combined with the GNU
15  * GPL, please contact INRIA about acquiring a ViSP Professional
16  * Edition License.
17  *
18  * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19  *
20  * This software was developed at:
21  * INRIA Rennes - Bretagne Atlantique
22  * Campus Universitaire de Beaulieu
23  * 35042 Rennes Cedex
24  * France
25  * http://www.irisa.fr/lagadic
26  *
27  * If you have questions regarding the use of this file, please contact
28  * INRIA at visp@inria.fr
29  *
30  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32  *
33  * Description:
34  * Generic model based tracker. This class declares the methods to implement in
35  * order to have a model based tracker.
36  *
37  * Authors:
38  * Romain Tallonneau
39  * Aurelien Yol
40  *
41  *****************************************************************************/
42 
47 #ifndef vpMbTracker_hh
48 #define vpMbTracker_hh
49 
50 #include <vector>
51 #include <string>
52 #include <sstream>
53 #include <fstream>
54 #include <map>
55 #include <algorithm>
56 #include <cctype>
57 #include <locale>
58 #include <functional> // std::not1
59 
60 
61 #include <visp/vpHomogeneousMatrix.h>
62 #include <visp/vpImage.h>
63 #include <visp/vpImagePoint.h>
64 #include <visp/vpColVector.h>
65 #include <visp/vpMatrix.h>
66 #include <visp/vpRGBa.h>
67 #include <visp/vpCameraParameters.h>
68 #include <visp/vpPoint.h>
69 #include <visp/vpMbtPolygon.h>
70 #include <visp/vpMbHiddenFaces.h>
71 #include <visp/vpPolygon.h>
72 
73 #ifdef VISP_HAVE_COIN
74 //Work around to avoid type redefinition int8_t with Coin
75 // #if defined(_WIN32) && defined(VISP_HAVE_OGRE) && (_MSC_VER >= 1600) // Visual Studio 2010
76 // #define HAVE_INT8_T 1
77 // #endif
78 
79 //Inventor includes
80 # include <Inventor/VRMLnodes/SoVRMLGroup.h>
81 # include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
82 # include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
83 #endif
84 
106 class VISP_EXPORT vpMbTracker
107 {
108 protected:
118  std::string modelFileName;
122  std::string poseSavingFilename;
133 
137  double angleAppears;
141  double distNearClip;
143  double distFarClip;
145  unsigned int clippingFlag;
147  bool useOgre;
149  unsigned int nbPoints;
151  unsigned int nbLines;
153  unsigned int nbPolygonLines;
155  unsigned int nbPolygonPoints;
157  unsigned int nbCylinders;
159  unsigned int nbCircles;
169  std::map<std::string, std::string> mapOfParameterNames;
170 
171 public:
172  vpMbTracker();
173  virtual ~vpMbTracker();
174 
186  virtual void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
187  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
199  virtual void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
200  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false)=0;
201 
203  virtual inline double getAngleAppear() const { return angleAppears; }
204 
206  virtual inline double getAngleDisappear() const { return angleDisappears; }
207 
213  virtual void getCameraParameters(vpCameraParameters& camera) const { camera = this->cam;}
214 
220  virtual inline unsigned int getClipping() const { return clippingFlag; }
221 
225  virtual vpMatrix getCovarianceMatrix() const {
226  if(!computeCovariance)
227  vpTRACE("Warning : The covariance matrix has not been computed. See setCovarianceComputation() to do it.");
228 
229  return covarianceMatrix;
230  }
231 
232  virtual vpColVector getEstimatedDoF();
233 
247  virtual vpColVector getError() {
248  return m_error;
249  }
250 
252  inline vpMbHiddenFaces<vpMbtPolygon>& getFaces() { return faces;}
253 
259  virtual inline double getFarClippingDistance() const { return distFarClip; }
260 
282  return m_w;
283  }
284 
290  virtual inline unsigned int getNbPolygon() const {
291  return static_cast<unsigned int>(faces.size());
292  }
293 
299  virtual inline double getNearClippingDistance() const { return distNearClip; }
300 
310  virtual inline vpMbtPolygon* getPolygon(const unsigned int index) {
311  if(index >= static_cast<unsigned int>(faces.size()) ){
312  throw vpException(vpException::dimensionError, "index out of range");
313  }
314 
315  return faces[index];
316  }
317 
318  virtual std::pair<std::vector<vpPolygon>, std::vector<std::vector<vpPoint> > > getPolygonFaces(const bool orderPolygons=true,
319  const bool useVisibility=true);
320 
328  inline void getPose(vpHomogeneousMatrix& cMo_) const {cMo_ = this->cMo;}
329 
337  inline vpHomogeneousMatrix getPose() const {return this->cMo;}
338 
339  /* PURE VIRTUAL METHODS */
340 
346  virtual void init(const vpImage<unsigned char>& I)=0;
347 
348  // Intializer
349 
350  virtual void initClick( const vpImage<unsigned char>& I, const std::string& initFile, const bool displayHelp = false );
351  virtual void initClick( const vpImage<unsigned char>& I, const std::vector<vpPoint> &points3D_list,
352  const std::string &displayFile = "" );
353 
354  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::string& initFile );
355  virtual void initFromPoints( const vpImage<unsigned char>& I, const std::vector<vpImagePoint> &points2D_list, const std::vector<vpPoint> &points3D_list );
356 
357  virtual void initFromPose(const vpImage<unsigned char>& I, const std::string &initFile);
358  virtual void initFromPose(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo);
359  virtual void initFromPose(const vpImage<unsigned char>& I, const vpPoseVector &cPo);
360 
368  virtual void loadConfigFile(const std::string& configFile)=0;
369 
370  virtual void loadModel(const char *modelFile, const bool verbose=false);
371  virtual void loadModel(const std::string &modelFile, const bool verbose=false);
372 
376  virtual void resetTracker() = 0;
377 
378  void savePose(const std::string &filename);
379 
389  virtual inline void setAngleAppear(const double &a) { angleAppears = a; }
390 
400  virtual inline void setAngleDisappear(const double &a) { angleDisappears = a; }
401 
407  virtual void setCameraParameters(const vpCameraParameters& camera) {this->cam = camera;}
408 
409  virtual void setClipping(const unsigned int &flags);
410 
416  virtual void setCovarianceComputation(const bool& flag) { computeCovariance = flag; }
417 
418  virtual void setEstimatedDoF(const vpColVector& v);
419 
431  void setDisplayFeatures(const bool displayF) {displayFeatures = displayF;}
432 
433  virtual void setFarClippingDistance(const double &dist);
434 
435  virtual void setLod(const bool useLod, const std::string &name="");
436 
437  virtual void setMinLineLengthThresh(const double minLineLengthThresh, const std::string &name="");
438 
439  virtual void setMinPolygonAreaThresh(const double minPolygonAreaThresh, const std::string &name="");
440 
441  virtual void setNearClippingDistance(const double &dist);
442 
452  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo) = 0;
453 
463  inline void setPoseSavingFilename(const std::string& filename){
464  poseSavingFilename = filename;
465  }
466 
467  virtual void setOgreVisibilityTest(const bool &v);
468 
474  virtual void testTracking() = 0;
475 
481  virtual void track(const vpImage<unsigned char>& I)=0;
482 
483 protected:
484  void addPolygon(const std::vector<vpPoint>& corners, const int idFace=-1, const std::string &polygonName="",
485  const bool useLod=false, const double minPolygonAreaThreshold=2500.0, const double minLineLengthThreshold=50.0);
486  void addPolygon(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=-1,
487  const std::string &polygonName="", const bool useLod=false, const double minPolygonAreaThreshold=2500.0);
488  void addPolygon(const vpPoint& p1, const vpPoint &p2, const int idFace=-1, const std::string &polygonName="",
489  const bool useLod=false, const double minLineLengthThreshold=50);
490 
491  void computeCovarianceMatrix(const vpHomogeneousMatrix &cMoPrev, const vpColVector &deltaS, const vpMatrix &Ls, const vpMatrix &W);
492  void computeJTR(const vpMatrix& J, const vpColVector& R, vpMatrix& JTR);
493 
494 #ifdef VISP_HAVE_COIN
495  virtual void extractGroup(SoVRMLGroup *sceneGraphVRML2, vpHomogeneousMatrix &transform, int &idFace);
496  virtual void extractFaces(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace);
497  virtual void extractLines(SoVRMLIndexedLineSet* line_set, int &idFace);
498  virtual void extractCylinders(SoVRMLIndexedFaceSet* face_set, vpHomogeneousMatrix &transform, int &idFace);
499 #endif
500 
501  vpPoint getGravityCenter(const std::vector<vpPoint>& _pts);
502 
514  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
515  const int idFace=0, const std::string &name="")=0;
526  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
527  const std::string &name="")=0;
528 
539  virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0;
540  virtual void initFaceFromLines(vpMbtPolygon &polygon)=0;
541 
542  virtual void loadVRMLModel(const std::string& modelFile);
543  virtual void loadCAOModel(const std::string& modelFile, std::vector<std::string>& vectorOfModelFilename, int& startIdFace,
544  const bool verbose=false, const bool parent=true);
545 
546  void removeComment(std::ifstream& fileId);
547 
548  inline bool parseBoolean(std::string &input) {
549  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
550  std::istringstream is(input);
551  bool b;
552  //Parse string to boolean either in the textual representation (True/False)
553  //or in numeric representation (1/0)
554  is >> (input.size() > 1 ? std::boolalpha : std::noboolalpha) >> b;
555  return b;
556  }
557 
558  std::map<std::string, std::string> parseParameters(std::string& endLine);
559 
560  inline std::string &ltrim(std::string &s) {
561  s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
562  return s;
563  }
564 
565  inline std::string &rtrim(std::string &s) {
566  s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
567  return s;
568  }
569 
570  inline std::string &trim(std::string &s) {
571  return ltrim(rtrim(s));
572  }
573 };
574 
575 
576 #endif
577 
virtual void setCovarianceComputation(const bool &flag)
Definition: vpMbTracker.h:416
std::string & rtrim(std::string &s)
Definition: vpMbTracker.h:565
Definition of the vpMatrix class.
Definition: vpMatrix.h:98
vpMatrix covarianceMatrix
Covariance matrix.
Definition: vpMbTracker.h:126
bool parseBoolean(std::string &input)
Definition: vpMbTracker.h:548
std::string & trim(std::string &s)
Definition: vpMbTracker.h:570
unsigned int nbLines
Number of lines in CAO model.
Definition: vpMbTracker.h:151
virtual double getAngleAppear() const
Definition: vpMbTracker.h:203
virtual unsigned int getClipping() const
Definition: vpMbTracker.h:220
virtual void setAngleDisappear(const double &a)
Definition: vpMbTracker.h:400
unsigned int nbCircles
Number of circles in CAO model.
Definition: vpMbTracker.h:159
vpMbHiddenFaces< vpMbtPolygon > faces
Set of faces describing the object.
Definition: vpMbTracker.h:135
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
std::map< std::string, std::string > mapOfParameterNames
Map with [map.first]=parameter_names and [map.second]=type (string, number or boolean) ...
Definition: vpMbTracker.h:169
#define vpTRACE
Definition: vpDebug.h:418
unsigned int nbCylinders
Number of cylinders in CAO model.
Definition: vpMbTracker.h:157
unsigned int nbPoints
Number of points in CAO model.
Definition: vpMbTracker.h:149
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
vpHomogeneousMatrix cMo
The current pose.
Definition: vpMbTracker.h:112
bool modelInitialised
Flag used to ensure that the CAD model is loaded before the initialisation.
Definition: vpMbTracker.h:120
vpHomogeneousMatrix getPose() const
Definition: vpMbTracker.h:337
error that can be emited by ViSP classes.
Definition: vpException.h:76
bool useOgre
Use Ogre3d for visibility tests.
Definition: vpMbTracker.h:147
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:118
bool computeCovariance
Flag used to specify if the covariance matrix has to be computed or not.
Definition: vpMbTracker.h:124
Class that defines what is a point.
Definition: vpPoint.h:65
vpCameraParameters cam
The camera parameters.
Definition: vpMbTracker.h:110
virtual double getAngleDisappear() const
Definition: vpMbTracker.h:206
double distFarClip
Distance for near clipping.
Definition: vpMbTracker.h:143
vpMatrix oJo
The Degrees of Freedom to estimate.
Definition: vpMbTracker.h:114
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:67
virtual void setCameraParameters(const vpCameraParameters &camera)
Definition: vpMbTracker.h:407
void getPose(vpHomogeneousMatrix &cMo_) const
Definition: vpMbTracker.h:328
std::string & ltrim(std::string &s)
Definition: vpMbTracker.h:560
double minLineLengthThresholdGeneral
Minimum line length threshold for LOD mode (general setting)
Definition: vpMbTracker.h:165
vpColVector m_error
Error s-s*.
Definition: vpMbTracker.h:132
virtual void getCameraParameters(vpCameraParameters &camera) const
Definition: vpMbTracker.h:213
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:106
unsigned int nbPolygonLines
Number of polygon lines in CAO model.
Definition: vpMbTracker.h:153
virtual void setAngleAppear(const double &a)
Definition: vpMbTracker.h:389
double angleAppears
Angle used to detect a face appearance.
Definition: vpMbTracker.h:137
virtual vpColVector getError()
Definition: vpMbTracker.h:247
std::string poseSavingFilename
Filename used to save the initial pose computed using the initClick() method. It is also used to read...
Definition: vpMbTracker.h:122
unsigned int size() const
virtual double getNearClippingDistance() const
Definition: vpMbTracker.h:299
unsigned int nbPolygonPoints
Number of polygon points in CAO model.
Definition: vpMbTracker.h:155
double minPolygonAreaThresholdGeneral
Minimum polygon area threshold for LOD mode (general setting)
Definition: vpMbTracker.h:167
bool displayFeatures
If true, the features are displayed.
Definition: vpMbTracker.h:128
virtual vpColVector getRobustWeights()
Definition: vpMbTracker.h:281
bool applyLodSettingInConfig
True if the CAO model is loaded before the call to loadConfigFile, (deduced by the number of polygons...
Definition: vpMbTracker.h:163
Class that provides a data structure for the column vectors as well as a set of operations on these v...
Definition: vpColVector.h:72
The pose is a complete representation of every rigid motion in the euclidian space.
Definition: vpPoseVector.h:92
virtual vpMbtPolygon * getPolygon(const unsigned int index)
Definition: vpMbTracker.h:310
double angleDisappears
Angle used to detect a face disappearance.
Definition: vpMbTracker.h:139
virtual unsigned int getNbPolygon() const
Definition: vpMbTracker.h:290
void setPoseSavingFilename(const std::string &filename)
Definition: vpMbTracker.h:463
virtual double getFarClippingDistance() const
Definition: vpMbTracker.h:259
void setDisplayFeatures(const bool displayF)
Definition: vpMbTracker.h:431
unsigned int clippingFlag
Flags specifying which clipping to used.
Definition: vpMbTracker.h:145
double distNearClip
Distance for near clipping.
Definition: vpMbTracker.h:141
vpMbHiddenFaces< vpMbtPolygon > & getFaces()
Definition: vpMbTracker.h:252
bool useLodGeneral
True if LOD mode is enabled.
Definition: vpMbTracker.h:161
bool isoJoIdentity
Boolean to know if oJo is identity (for fast computation)
Definition: vpMbTracker.h:116
vpColVector m_w
Weights used in the robust scheme.
Definition: vpMbTracker.h:130
virtual vpMatrix getCovarianceMatrix() const
Definition: vpMbTracker.h:225