ViSP  2.10.0
vpMbEdgeTracker.h
1 /****************************************************************************
2  *
3  * $Id: vpMbEdgeTracker.h 5217 2015-01-28 09:40:02Z fspindle $
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  *
34  * Description:
35  * Make the complete tracking of an object by using its CAD model
36  *
37  * Authors:
38  * Nicolas Melchior
39  * Romain Tallonneau
40  * Eric Marchand
41  *
42  *****************************************************************************/
43 
49 #ifndef vpMbEdgeTracker_HH
50 #define vpMbEdgeTracker_HH
51 
52 #include <visp/vpPoint.h>
53 #include <visp/vpMbTracker.h>
54 #include <visp/vpMe.h>
55 #include <visp/vpMbtMeLine.h>
56 #include <visp/vpMbtDistanceLine.h>
57 #include <visp/vpMbtDistanceCircle.h>
58 #include <visp/vpMbtDistanceCylinder.h>
59 #include <visp/vpXmlParser.h>
60 
61 #include <iostream>
62 #include <fstream>
63 #include <vector>
64 #include <list>
65 
66 #if defined(VISP_HAVE_COIN)
67 //Inventor includes
68 #include <Inventor/nodes/SoSeparator.h>
69 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
70 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
71 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
72 #include <Inventor/actions/SoWriteAction.h>
73 #include <Inventor/actions/SoSearchAction.h>
74 #include <Inventor/misc/SoChildList.h>
75 #include <Inventor/actions/SoGetMatrixAction.h>
76 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
77 #include <Inventor/actions/SoToVRML2Action.h>
78 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
79 #include <Inventor/VRMLnodes/SoVRMLShape.h>
80 #endif
81 
82 #ifdef VISP_HAVE_OPENCV
83 # if VISP_HAVE_OPENCV_VERSION >= 0x020101
84 # include <opencv2/core/core.hpp>
85 # include <opencv2/imgproc/imgproc.hpp>
86 # include <opencv2/imgproc/imgproc_c.h>
87 # else
88 # include <cv.h>
89 # endif
90 #endif
91 
264 class VISP_EXPORT vpMbEdgeTracker: virtual public vpMbTracker
265 {
266  protected :
267 
274  double lambda;
275 
279  std::vector< std::list< vpMbtDistanceLine*> > lines;
280 
282  std::vector< std::list< vpMbtDistanceCircle*> > circles;
283 
285  std::vector< std::list< vpMbtDistanceCylinder*> > cylinders;
286 
288  unsigned int nline;
289 
291  unsigned int ncircle;
292 
294  unsigned int ncylinder;
295 
297  unsigned int nbvisiblepolygone;
298 
301 
303  std::vector<bool> scales;
304 
306  std::vector< const vpImage<unsigned char>* > Ipyramid;
307 
309  unsigned int scaleLevel;
310 
311 public:
312 
313  vpMbEdgeTracker();
314  virtual ~vpMbEdgeTracker();
315 
316  void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
317  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
318  void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
319  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
320 
326  virtual inline double getLambda() const {return lambda;}
327 
328  void getLline(std::list<vpMbtDistanceLine *>& linesList, const unsigned int level = 0);
329  void getLcircle(std::list<vpMbtDistanceCircle *>& circlesList, const unsigned int level = 0);
330  void getLcylinder(std::list<vpMbtDistanceCylinder *>& cylindersList, const unsigned int level = 0);
331 
337  inline void getMovingEdge(vpMe &p_me ) const { p_me = this->me;}
343  inline vpMe getMovingEdge() const { return this->me;}
344 
345  unsigned int getNbPoints(const unsigned int level=0) const;
346 
352  std::vector<bool> getScales() const {return scales;}
361  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt;}
362 
363  void loadConfigFile(const std::string &configFile);
364  void loadConfigFile(const char* configFile);
365  void reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name, const vpHomogeneousMatrix& cMo_,
366  const bool verbose=false);
367  void reInitModel(const vpImage<unsigned char>& I, const char* cad_name, const vpHomogeneousMatrix& cMo,
368  const bool verbose=false);
369  void resetTracker();
370 
376  virtual void setCameraParameters(const vpCameraParameters& camera) {
377  this->cam = camera;
378 
379  for (unsigned int i = 0; i < scales.size(); i += 1){
380  if(scales[i]){
381  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[i].begin(); it!=lines[i].end(); ++it){
382  (*it)->setCameraParameters(cam);
383  }
384 
385  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[i].begin(); it!=cylinders[i].end(); ++it){
386  (*it)->setCameraParameters(cam);
387  }
388 
389  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[i].begin(); it!=circles[i].end(); ++it){
390  (*it)->setCameraParameters(cam);
391  }
392  }
393  }
394  }
395 
396  virtual void setClipping(const unsigned int &flags);
397 
398  virtual void setFarClippingDistance(const double &dist);
399 
400  virtual void setNearClippingDistance(const double &dist);
401 
409  virtual void setOgreVisibilityTest(const bool &v){
411 #ifdef VISP_HAVE_OGRE
412  faces.getOgreContext()->setWindowName("MBT Edge");
413 #endif
414  }
415 
428  void setGoodMovingEdgesRatioThreshold(const double threshold) {percentageGdPt = threshold;}
429 
435  virtual inline void setLambda(const double gain) {this->lambda = gain;}
436 
437  void setMovingEdge(const vpMe &me);
438 
439  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo);
440 
441  void setScales(const std::vector<bool>& _scales);
442 
443  void track(const vpImage<unsigned char> &I);
444 
445 protected:
446  bool samePoint(const vpPoint &P1, const vpPoint &P2);
447  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, int idFace = -1, const std::string& name = "");
448  void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace = -1, const std::string& name = "");
449  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
450  void addPolygon(vpMbtPolygon &p) ;
451  void cleanPyramid(std::vector<const vpImage<unsigned char>* >& _pyramid);
452  void computeVVS(const vpImage<unsigned char>& _I);
453  void downScale(const unsigned int _scale);
454  void init(const vpImage<unsigned char>& I);
455  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
456  const int idFace=0, const std::string &name="");
457  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
458  const std::string &name="");
459  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
460  virtual void initFaceFromLines(vpMbtPolygon &polygon);
461  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo) ;
462  void initPyramid(const vpImage<unsigned char>& _I, std::vector<const vpImage<unsigned char>* >& _pyramid);
463  void reInitLevel(const unsigned int _lvl);
464  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
465  void removeCircle(const std::string& name);
466  void removeCylinder(const std::string& name);
467  void removeLine(const std::string& name);
468  void testTracking();
469  void trackMovingEdge(const vpImage<unsigned char> &I) ;
470  void updateMovingEdge(const vpImage<unsigned char> &I) ;
471  void upScale(const unsigned int _scale);
472  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline) ;
473 
474 #ifdef VISP_BUILD_DEPRECATED_FUNCTIONS
475 
478  vp_deprecated void visibleFace(const vpHomogeneousMatrix &_cMo, bool &newvisibleline);
479 
480 public:
488  vp_deprecated inline double getFirstThreshold() const { return percentageGdPt;}
500  vp_deprecated void setDisplayMovingEdges(const bool displayMe) {displayFeatures = displayMe;}
513  vp_deprecated void setFirstThreshold(const double threshold1) {percentageGdPt = threshold1;}
514  #endif
515 };
516 
517 #endif
518 
std::vector< bool > getScales() const
unsigned int ncylinder
Index of the cylinder to add, and total number of cylinders extracted so far.
virtual void track(const vpImage< unsigned char > &I)=0
The class provides a data structure for the homogeneous matrices as well as a set of operations on th...
double lambda
The gain of the virtual visual servoing stage.
unsigned int scaleLevel
Current scale level used. This attribute must not be modified outside of the downScale() and upScale(...
std::vector< std::list< vpMbtDistanceCircle * > > circles
Vector of the tracked circles.
Class to define colors available for display functionnalities.
Definition: vpColor.h:125
virtual void initCircle(const vpPoint &p1, const vpPoint &p2, const vpPoint &p3, const double radius, const int idFace=0, const std::string &name="")=0
unsigned int ncircle
Index of the circle to add, and total number of circles extracted so far.
Contains predetermined masks for sites and holds moving edges tracking parameters.
Definition: vpMe.h:70
vp_deprecated void setFirstThreshold(const double threshold1)
Make the complete tracking of an object by using its CAD model.
std::vector< const vpImage< unsigned char > * > Ipyramid
Pyramid of image associated to the current image. This pyramid is computed in the init() and in the t...
virtual void setCameraParameters(const vpCameraParameters &camera)
vpMe me
The moving edges parameters.
virtual void resetTracker()=0
std::vector< std::list< vpMbtDistanceLine * > > lines
Vector of list of all the lines tracked (each line is linked to a list of moving edges). Each element of the vector is for a scale (element 0 = level 0 = no subsampling).
void setGoodMovingEdgesRatioThreshold(const double threshold)
Class that defines what is a point.
Definition: vpPoint.h:65
virtual void init(const vpImage< unsigned char > &I)=0
virtual void setOgreVisibilityTest(const bool &v)
virtual void display(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, const vpColor &col, const unsigned int thickness=1, const bool displayFullModel=false)=0
std::vector< bool > scales
Vector of scale level to use for the multi-scale tracking.
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:67
void getMovingEdge(vpMe &p_me) const
vpMe getMovingEdge() const
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
Generic class defining intrinsic camera parameters.
vp_deprecated double getFirstThreshold() const
Main methods for a model-based tracker.
Definition: vpMbTracker.h:106
double percentageGdPt
Percentage of good points over total number of points below which tracking is supposed to have failed...
virtual void loadConfigFile(const std::string &configFile)=0
virtual double getLambda() const
virtual void setOgreVisibilityTest(const bool &v)
double getGoodMovingEdgesRatioThreshold() const
std::vector< std::list< vpMbtDistanceCylinder * > > cylinders
Vector of the tracked cylinders.
virtual void initFaceFromLines(vpMbtPolygon &polygon)=0
virtual void testTracking()=0
unsigned int nbvisiblepolygone
Number of polygon (face) currently visible.
vp_deprecated void setDisplayMovingEdges(const bool displayMe)
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)=0
virtual void setClipping(const unsigned int &flags)
void addPolygon(const std::vector< vpPoint > &corners, const int idFace=-1, const std::string &polygonName="", const bool useLod=false, const double minPolygonAreaThreshold=2500.0, const double minLineLengthThreshold=50.0)
unsigned int nline
Index of the polygon to add, and total number of polygon extracted so far.
virtual void setFarClippingDistance(const double &dist)
virtual void setLambda(const double gain)
virtual void initCylinder(const vpPoint &p1, const vpPoint &p2, const double radius, const int idFace=0, const std::string &name="")=0
virtual void setNearClippingDistance(const double &dist)