Go to the documentation of this file.00001
00013 #ifndef LGL_GRAPH_ASTAR_HPP
00014 #define LGL_GRAPH_ASTAR_HPP
00015
00016 #include <vector>
00017
00018 #include <boost/graph/astar_search.hpp>
00019 #include <boost/function.hpp>
00020
00021 #include <lgl/NavGNode.hpp>
00022 #include <lgl/GraphPath.hpp>
00023 #include <lgl/NavHeuristics.hpp>
00024
00025 using namespace boost;
00026
00027 namespace jafar {
00028 namespace lgl {
00029
00035
00036 struct found_goal {};
00037
00042 template <class Vertex>
00043 class astar_goal_visitor : public boost::default_astar_visitor
00044 {
00045 public:
00047 astar_goal_visitor(Vertex goal) : m_goal(goal)
00048 {
00049 this->eventWatcher = NULL;
00050 }
00051
00052 astar_goal_visitor(Vertex goal, boost::function<void(const NavGNode&, int)> _eventWatcher ) : m_goal(goal)
00053 {
00054 this->eventWatcher = _eventWatcher;
00055 }
00056
00058 template <class Graph>
00059 void examine_vertex(Vertex u, const Graph& g) {
00060 if (this->eventWatcher != NULL)
00061 {
00062 this->eventWatcher((g)[u], 2);
00063 }
00064
00065 if(u == m_goal)
00066 {
00067 throw found_goal();
00068 }
00069 }
00070
00072 template <class Graph>
00073 void discover_vertex(Vertex u, const Graph& g) {
00074 if (eventWatcher != NULL)
00075 {
00076 eventWatcher((g)[u], 1);
00077 }
00078 }
00079
00080 private:
00081 Vertex m_goal;
00082 boost::function<void(const NavGNode&, int)> eventWatcher;
00083
00084 };
00085
00086
00088 template<
00089 class Graph,
00090 class Vertex,
00091 typename CostType
00092 >
00093 class NavAStar {
00094
00095 public:
00096
00097 NavAStar() {}
00098 ~NavAStar() {}
00099
00106 template <class Heuristic,class WeightMap>
00107 GraphPath<Vertex> AStarSearch(const Heuristic& heuristic, const Graph& graph,const WeightMap& weightmap, const Vertex& start, const Vertex& goal,
00108
00109 std::vector<CostType>& resultingFullCost,
00110
00111 std::vector<Vertex>& predecessorMap,
00112 std::vector<default_color_type>& colorMap,
00113 boost::function<void(const NavGNode&, int)> _eventWatcher)
00114 {
00115
00116 GraphPath<Vertex> shortest_path;
00117
00118
00119 std::vector<CostType> dist(num_vertices(graph));
00120
00121
00122
00123 astar_goal_visitor<Vertex> visCatcher(goal, boost::ref(_eventWatcher));
00124
00125 try {
00126
00127 astar_search
00128 (graph, start,
00129 heuristic,
00130 boost::predecessor_map(&predecessorMap[0]).
00131 distance_map(&dist[0]).
00132 weight_map(weightmap).
00133 rank_map(&resultingFullCost[0]).
00134 color_map(&colorMap[0]).
00135 visitor(visCatcher)
00136 );
00137
00138
00139 } catch(found_goal fg) {
00140 for(Vertex v = goal;; v = predecessorMap[v]) {
00141 shortest_path.push_front(v);
00142 if (predecessorMap[v] == v) { break; }
00143 }
00144 }
00145
00146 return shortest_path;
00147 }
00148
00149 };
00150 }
00151 }
00152 #endif