Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

GridWorld.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_GridWorld_h_
00003 #define INCLUDED_GridWorld_h_
00004 
00005 #include <vector>
00006 #include <iostream>
00007 #include <iomanip>
00008 #include <cmath>
00009 
00010 class GridWorld {
00011   static const float HCOST; //!< cost for horizontal moves
00012   static const float VCOST; //!< cost for vertical moves
00013   static const float DCOST; //!< cost for diagonal moves: <=0 means disable diagonals
00014   
00015   //! can read from istream (until end of stream), spaces are empty, non-spaces are blocked
00016   friend std::istream& operator>>(std::istream& is, GridWorld& gw);
00017   //! writes map to ostream
00018   friend std::ostream& operator<<(std::ostream& os, const GridWorld& gw);
00019   
00020   //! storage for map, spaces are empty, non-spaces are blocked
00021   std::vector< std::vector<char> > world;
00022   
00023 public:
00024   //! Stores context used per node in path planning, stores row and column position
00025   struct State {
00026     //! default constructor, initializes to invalid position for fail-fast
00027     State() : r((size_t)-1), c((size_t)-1) {}
00028     //! constructor from a coordinate pair
00029     State(size_t row, size_t col) : r(row), c(col) {}
00030     //! a silly function to scatter row and column across the span of size_t
00031     size_t hash() const {
00032       size_t s1 = (c << (sizeof(size_t)*8/4*1));
00033       size_t s2 = (r << (sizeof(size_t)*8/4*2));
00034       size_t s3 = (c << (sizeof(size_t)*8/4*3));
00035       return s3+s2+s1+r;
00036     }
00037     //! comparison operator to allow sorting states for faster lookup in std::set
00038     bool operator<(const State& other) const { return r<other.r || (r==other.r && c<other.c); }
00039     //! equality is used to test against goal
00040     bool operator==(const State& other) const { return r==other.r && c==other.c; }
00041     //! just for debugging
00042     friend std::ostream& operator<<(std::ostream& os, const State& st) { return os << st.r << ',' << st.c; }
00043 
00044     size_t r; //!< row
00045     size_t c; //!< column
00046   };
00047   
00048   //! default constructor, does nothing
00049   GridWorld() : world() {}
00050   
00051   //! constructs empty world of the specified size
00052   GridWorld(size_t r, size_t c) : world(r,std::vector<char>(c,' ')) {}
00053   
00054   //! allows validation on queue-pop instead of before queue-push
00055   bool validate(const State& st) const { return true; }
00056   
00057   //! The heuristic function accepts the current state, and goal state, and should return an admissable (aka optimistic) estimate of the remaining cost
00058   float heuristic(const State& st, const State& goal) const;
00059   
00060   //! Generates a vector of successor states, paired with the cost to get to that state from @a st
00061   /*! Note that this implementation returns a reference to a static instance, which is not thread safe but slightly faster */
00062   const std::vector<std::pair<float,GridWorld::State> >& expand(const State* parent, const State& st, const State& goal) const;
00063   
00064   char& operator()(size_t r, size_t c) { return world[r][c]; } //!< map cell accessor
00065   char operator()(size_t r, size_t c) const { return world[r][c]; } //!< map cell accessor
00066   
00067   std::vector<char>& operator[](size_t r) { return world[r]; } //!< map row accessor
00068   const std::vector<char>& operator[](size_t r) const { return world[r]; } //!< map row accessor
00069   
00070   char& operator[](const State& x) { return world[x.r][x.c]; } //!< map cell accessor
00071   char operator[](const State& x) const { return world[x.r][x.c]; } //!< map cell accessor
00072 };
00073 
00074 #endif

Tekkotsu v5.1CVS
Generated Mon May 9 04:58:41 2016 by Doxygen 1.6.3