Jafar
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
PursuitNavigator.hpp
Go to the documentation of this file.
00001 
00013 #ifndef LGL_PURSUIT_NAVIGATOR_HPP
00014 #define LGL_PURSUIT_NAVIGATOR_HPP
00015 
00016 #include <lgl/GeoData.hpp>
00017 #include <lgl/CellID.hpp>
00018 
00019 #include <string>
00020 #include <set>
00021 #include <vector>
00022 #include <list>
00023 #include <queue>
00024 
00025 #include <boost/shared_ptr.hpp>
00026 
00027 namespace jafar {
00028   namespace lgl {
00029     
00030     /* Structure used for navigation computation, especialy A-Star */
00031     class pNavPos 
00032     {
00033 
00034     public :
00035       int x ;       // x Position
00036       int y ;       // y Position
00037 
00038       double g ;      // cost from start to the current position
00039       double h ;      // heuristic : estimated cost from the current position to the goal
00040 
00041       unsigned int t ;  // time
00042 
00043       std::list< boost::shared_ptr< pNavPos> > prev; // previous positions (cf A-Star)
00044 
00045       pNavPos(int _x, int _y, double _g, double _h, unsigned int _t) :
00046         x (_x),
00047         y (_y),
00048         g (_g),
00049         h (_h),
00050         t (_t)
00051       { }
00052 
00053       pNavPos(int _x, int _y, double _g, double _h, unsigned int _t, boost::shared_ptr< pNavPos > &_prev) :
00054         x (_x),
00055         y (_y),
00056         g (_g),
00057         h (_h),
00058         t (_t)
00059       {  prev.push_back(_prev) ; }
00060 
00061       bool operator< (const pNavPos &rhs) const
00062       {
00063         // NB : this is NOT the inverse function of >
00064         return ( g < rhs.g );
00065       }
00066 
00067       bool operator> (const pNavPos &rhs) const
00068       {
00069         // NB : this is NOT the inverse function of <
00070         return ( (g+h) > (rhs.g + rhs.h));
00071       }
00072 
00073     };
00074 
00075     /* comparators using shared_ptr (used for priority_queue) */ 
00076     template <typename Pointer> struct ptr_less
00077     { 
00078       bool operator()( const Pointer & lhs, const Pointer & rhs)
00079       {
00080         return (*lhs < *rhs) ;
00081       }     
00082     };
00083     template <typename Pointer> struct ptr_greater
00084     { 
00085       bool operator()( const Pointer & lhs, const Pointer & rhs)
00086       {
00087         return (*lhs > *rhs) ;
00088       }     
00089     };
00090     
00091     struct ptr_cmp
00092     { 
00093       bool operator()( const boost::shared_ptr< pNavPos > & lhs, const boost::shared_ptr< pNavPos > & rhs)
00094       {
00095         return ( (lhs->x < rhs->x) || (lhs->x == rhs->x && lhs->y < rhs->y ) );
00096       }     
00097     };
00098 
00099 
00100 
00106     class PursuitNavigator {
00108     
00109       private :
00110         
00111         // Geodata information
00112         // used for accessibility information
00113         // robGeoData -> robot  accessibility model
00114         // tarGeoData -> target accessibility model
00115         GeoData* robGeoData ;
00116         GeoData* tarGeoData ;
00117 
00118         // ysize is the Y-axis size of the map 
00119         // (use by Cell_ID)
00120         unsigned int ysize ;
00121         unsigned int xsize ;
00122         
00123       public : 
00124     
00129         PursuitNavigator( const std::string &robotAccessMapPath, const std::string &targetAccessMapPath) 
00130         {
00131           robGeoData = new GeoData (robotAccessMapPath  );
00132           tarGeoData = new GeoData (targetAccessMapPath );
00133 
00134           ysize = 2 * robGeoData->getysize() + 1 ;      // (2*N+1) vertices for N cells
00135           xsize = 2 * robGeoData->getxsize() + 1 ;      // (2*N+1) vertices for N cells 
00136           //TODO Check compatibility between the two GeoData 
00137         }
00138 
00139         ~PursuitNavigator() 
00140         {
00141           if ( robGeoData != NULL )
00142             delete robGeoData ;
00143 
00144           if ( tarGeoData != NULL )
00145             delete tarGeoData ;
00146         }
00147 
00148         /* Find the accessible cells for the robot
00149          * Here : 3x3 models (9-connexity)
00150          */
00151         void findRobotAccessibility( const Cell_ID &currentRobotPos, std::set<Cell_ID> &robotAccess);
00152 
00153         /* Find the accessible cells for the robot
00154          * Here : 3x3 models (9-connexity)
00155          */
00156         void findTargetAccessibility( const std::set<Cell_ID> &currentTargetPos, std::set<Cell_ID> &targetAcess );
00157 
00158         /* tool function : compute distance */
00159         double distCellID ( Cell_ID a, Cell_ID b) ;
00160 
00161         /* tool function : compute distance */
00162         double distPos ( pNavPos A, pNavPos B ) ;
00163         double distPos ( int xA, int yA, int xB, int yB) ;
00164 
00165         /* Find a way from the Cell_ID Start to the Cell_ID Goal 
00166          * Return the path as a Cell_ID vector.
00167          *
00168          * Use the algorithm A-Star to compute the path.
00169          * Return true if a Path has been found, false otherwise.
00170          *
00171          */
00172         bool navAStar ( Cell_ID start, Cell_ID goal, std::vector<Cell_ID> &Path);
00173         bool navAStar ( int xStart, int yStart, int xGoal, int yGoal, std::vector<Cell_ID> &Path);
00174 
00175         /* Find a way from the Cell_ID Start to the closest cell of the set Goals 
00176          * Return the path as a Cell_ID vector.
00177          *
00178          * Use the algorithm Dijkstra to compute the path.
00179          * Return true if a Path has been found, false otherwise.
00180          *
00181          */
00182         bool nav2Set ( Cell_ID start, const std::set< Cell_ID > &goals, std::vector<Cell_ID> &Path);
00183         bool nav2Set ( int xStart, int yStart, const std::set<Cell_ID> &goals, std::vector<Cell_ID> &Path);
00184 
00185         bool nav2Set ( Cell_ID start, const std::set< Cell_ID > &goals, std::vector<Cell_ID> &Path, const unsigned int &pursuitTemporalTolerance);
00186 
00187         /* heuristic for A-Star algorithm 
00188          * Here heuristic = distance
00189          */
00190         double heuristic(int xA, int yA, int xB, int yB) ;
00191         
00192         /* develop the nodes in the A-Star algorithm (use heuristics)
00193          */
00194         void addNeighbours(   boost::shared_ptr< pNavPos> &currPos, 
00195                     const int &xGoal, const int &yGoal, 
00196                     std::set< boost::shared_ptr<pNavPos>, ptr_cmp > &visitedPos, 
00197                     std::list<  boost::shared_ptr<pNavPos> >  &queue);
00198 
00199         /* develop the nodes in the Djisktra algorithm (do NOT use any heuristic)
00200          */
00201         void addNeighbours_classic(   boost::shared_ptr< pNavPos> &currPos, 
00202                         std::set< boost::shared_ptr<pNavPos>, ptr_cmp > &visitedPos, 
00203                         std::list<  boost::shared_ptr<pNavPos> >  &queue);
00204 
00205         /* for a given set of positions, return another set of positions, if it exits, of positions from which all the first ones are visible
00206          */
00207         void findIdealPositions( const std::set<Cell_ID> &targetPos, std::set<Cell_ID> &positions);
00208 
00209         /* get functions */
00210         GeoData* getrobGeoData() ;
00211         GeoData* gettarGeoData() ;
00212 
00213         unsigned int getysize() const;
00214         unsigned int getxsize() const;
00215 
00216     };
00217 
00218   }
00219 }
00220 
00221 #endif /* LGL_PURSUIT_NAVIGATOR_HPP */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on Wed Oct 15 2014 00:37:24 for Jafar by doxygen 1.7.6.1
LAAS-CNRS