Visual Servoing Platform  version 3.0.0
vpMbEdgeTracker.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  * Make the complete tracking of an object by using its CAD model
32  *
33  * Authors:
34  * Nicolas Melchior
35  * Romain Tallonneau
36  * Eric Marchand
37  *
38  *****************************************************************************/
39 
45 #ifndef vpMbEdgeTracker_HH
46 #define vpMbEdgeTracker_HH
47 
48 #include <visp3/core/vpPoint.h>
49 #include <visp3/mbt/vpMbTracker.h>
50 #include <visp3/me/vpMe.h>
51 #include <visp3/mbt/vpMbtMeLine.h>
52 #include <visp3/mbt/vpMbtDistanceLine.h>
53 #include <visp3/mbt/vpMbtDistanceCircle.h>
54 #include <visp3/mbt/vpMbtDistanceCylinder.h>
55 #include <visp3/core/vpXmlParser.h>
56 
57 #include <iostream>
58 #include <fstream>
59 #include <vector>
60 #include <list>
61 
62 #if defined(VISP_HAVE_COIN3D)
63 //Inventor includes
64 #include <Inventor/nodes/SoSeparator.h>
65 #include <Inventor/VRMLnodes/SoVRMLIndexedFaceSet.h>
66 #include <Inventor/VRMLnodes/SoVRMLIndexedLineSet.h>
67 #include <Inventor/VRMLnodes/SoVRMLCoordinate.h>
68 #include <Inventor/actions/SoWriteAction.h>
69 #include <Inventor/actions/SoSearchAction.h>
70 #include <Inventor/misc/SoChildList.h>
71 #include <Inventor/actions/SoGetMatrixAction.h>
72 #include <Inventor/actions/SoGetPrimitiveCountAction.h>
73 #include <Inventor/actions/SoToVRML2Action.h>
74 #include <Inventor/VRMLnodes/SoVRMLGroup.h>
75 #include <Inventor/VRMLnodes/SoVRMLShape.h>
76 #endif
77 
78 #ifdef VISP_HAVE_OPENCV
79 # if VISP_HAVE_OPENCV_VERSION >= 0x020101
80 # include <opencv2/core/core.hpp>
81 # include <opencv2/imgproc/imgproc.hpp>
82 # include <opencv2/imgproc/imgproc_c.h>
83 # else
84 # include <cv.h>
85 # endif
86 #endif
87 
260 class VISP_EXPORT vpMbEdgeTracker: virtual public vpMbTracker
261 {
262  protected :
263 
270  double lambda;
271 
275  std::vector< std::list< vpMbtDistanceLine*> > lines;
276 
278  std::vector< std::list< vpMbtDistanceCircle*> > circles;
279 
281  std::vector< std::list< vpMbtDistanceCylinder*> > cylinders;
282 
284  unsigned int nline;
285 
287  unsigned int ncircle;
288 
290  unsigned int ncylinder;
291 
293  unsigned int nbvisiblepolygone;
294 
297 
299  std::vector<bool> scales;
300 
302  std::vector< const vpImage<unsigned char>* > Ipyramid;
303 
305  unsigned int scaleLevel;
306 
307 public:
308 
309  vpMbEdgeTracker();
310  virtual ~vpMbEdgeTracker();
311 
312  void display(const vpImage<unsigned char>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
313  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
314  void display(const vpImage<vpRGBa>& I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam,
315  const vpColor& col , const unsigned int thickness=1, const bool displayFullModel = false);
316 
322  virtual inline double getLambda() const {return lambda;}
323 
324  void getLline(std::list<vpMbtDistanceLine *>& linesList, const unsigned int level = 0);
325  void getLcircle(std::list<vpMbtDistanceCircle *>& circlesList, const unsigned int level = 0);
326  void getLcylinder(std::list<vpMbtDistanceCylinder *>& cylindersList, const unsigned int level = 0);
327 
333  inline void getMovingEdge(vpMe &p_me ) const { p_me = this->me;}
339  inline vpMe getMovingEdge() const { return this->me;}
340 
341  unsigned int getNbPoints(const unsigned int level=0) const;
342 
348  std::vector<bool> getScales() const {return scales;}
357  inline double getGoodMovingEdgesRatioThreshold() const { return percentageGdPt;}
358 
359  void loadConfigFile(const std::string &configFile);
360  void loadConfigFile(const char* configFile);
361  void reInitModel(const vpImage<unsigned char>& I, const std::string &cad_name, const vpHomogeneousMatrix& cMo_,
362  const bool verbose=false);
363  void reInitModel(const vpImage<unsigned char>& I, const char* cad_name, const vpHomogeneousMatrix& cMo,
364  const bool verbose=false);
365  void resetTracker();
366 
372  virtual void setCameraParameters(const vpCameraParameters& camera) {
373  this->cam = camera;
374 
375  for (unsigned int i = 0; i < scales.size(); i += 1){
376  if(scales[i]){
377  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[i].begin(); it!=lines[i].end(); ++it){
378  (*it)->setCameraParameters(cam);
379  }
380 
381  for(std::list<vpMbtDistanceCylinder*>::const_iterator it=cylinders[i].begin(); it!=cylinders[i].end(); ++it){
382  (*it)->setCameraParameters(cam);
383  }
384 
385  for(std::list<vpMbtDistanceCircle*>::const_iterator it=circles[i].begin(); it!=circles[i].end(); ++it){
386  (*it)->setCameraParameters(cam);
387  }
388  }
389  }
390  }
391 
392  virtual void setClipping(const unsigned int &flags);
393 
394  virtual void setFarClippingDistance(const double &dist);
395 
396  virtual void setNearClippingDistance(const double &dist);
397 
405  virtual void setOgreVisibilityTest(const bool &v){
407 #ifdef VISP_HAVE_OGRE
408  faces.getOgreContext()->setWindowName("MBT Edge");
409 #endif
410  }
411 
417  virtual void setScanLineVisibilityTest(const bool &v){
419 
420  for (unsigned int i = 0; i < scales.size(); i += 1){
421  if(scales[i]){
422  for(std::list<vpMbtDistanceLine*>::const_iterator it=lines[i].begin(); it!=lines[i].end(); ++it){
423  (*it)->useScanLine = v;
424  }
425  }
426  }
427  }
428 
441  void setGoodMovingEdgesRatioThreshold(const double threshold) {percentageGdPt = threshold;}
442 
448  virtual inline void setLambda(const double gain) {this->lambda = gain;}
449 
450  void setMovingEdge(const vpMe &me);
451 
452  virtual void setPose(const vpImage<unsigned char> &I, const vpHomogeneousMatrix& cdMo);
453 
454  void setScales(const std::vector<bool>& _scales);
455 
456  void setUseEdgeTracking(const std::string &name, const bool &useEdgeTracking);
457 
458  void track(const vpImage<unsigned char> &I);
459 
460 protected:
461  bool samePoint(const vpPoint &P1, const vpPoint &P2);
462  void addCircle(const vpPoint &P1, const vpPoint &P2, const vpPoint &P3, const double r, int idFace = -1, const std::string& name = "");
463  void addCylinder(const vpPoint &P1, const vpPoint &P2, const double r, int idFace = -1, const std::string& name = "");
464  void addLine(vpPoint &p1, vpPoint &p2, int polygon = -1, std::string name = "");
465  void addPolygon(vpMbtPolygon &p) ;
466  void cleanPyramid(std::vector<const vpImage<unsigned char>* >& _pyramid);
467  void computeProjectionError(const vpImage<unsigned char>& _I);
468  void computeVVS(const vpImage<unsigned char>& _I);
469  void downScale(const unsigned int _scale);
470  void init(const vpImage<unsigned char>& I);
471  virtual void initCircle(const vpPoint& p1, const vpPoint &p2, const vpPoint &p3, const double radius,
472  const int idFace=0, const std::string &name="");
473  virtual void initCylinder(const vpPoint& p1, const vpPoint &p2, const double radius, const int idFace=0,
474  const std::string &name="");
475  virtual void initFaceFromCorners(vpMbtPolygon &polygon);
476  virtual void initFaceFromLines(vpMbtPolygon &polygon);
477  void initMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo) ;
478  void initPyramid(const vpImage<unsigned char>& _I, std::vector<const vpImage<unsigned char>* >& _pyramid);
479  void reInitLevel(const unsigned int _lvl);
480  void reinitMovingEdge(const vpImage<unsigned char> &I, const vpHomogeneousMatrix &_cMo);
481  void removeCircle(const std::string& name);
482  void removeCylinder(const std::string& name);
483  void removeLine(const std::string& name);
484  void resetMovingEdge();
485  void testTracking();
486  void trackMovingEdge(const vpImage<unsigned char> &I) ;
487  void updateMovingEdge(const vpImage<unsigned char> &I) ;
488  void upScale(const unsigned int _scale);
489  void visibleFace(const vpImage<unsigned char> &_I, const vpHomogeneousMatrix &_cMo, bool &newvisibleline) ;
490 };
491 
492 #endif
493 
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
Implementation of an homogeneous matrix and operations on such kind of matrices.
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:121
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.
Definition: vpMe.h:59
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:59
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.
virtual void setScanLineVisibilityTest(const bool &v)
Implementation of a polygon of the model used by the model-based tracker.
Definition: vpMbtPolygon.h:64
void getMovingEdge(vpMe &p_me) const
vpMe getMovingEdge() const
virtual void initFaceFromCorners(vpMbtPolygon &polygon)=0
Generic class defining intrinsic camera parameters.
Main methods for a model-based tracker.
Definition: vpMbTracker.h:103
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.
virtual void setPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cdMo)=0
virtual void setScanLineVisibilityTest(const bool &v)
Definition: vpMbTracker.h:511
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)