| | 14 | |
|---|
| | 15 | #define ID_FEATURE "id" |
|---|
| | 16 | #define COST_FEATURE "cost" |
|---|
| | 17 | #define WEIGHT_FEATURE "weight" |
|---|
| | 18 | #define RC_FEATURE "reverse_cost" |
|---|
| | 19 | |
|---|
| | 20 | #define SOURCE_FEATURE "source" |
|---|
| | 21 | #define TARGET_FEATURE "target" |
|---|
| | 22 | |
|---|
| | 23 | #define TO_COST_FEATURE "to_cost" |
|---|
| | 24 | #define RULE_FEATURE "rule" |
|---|
| | 25 | |
|---|
| | 26 | #define EXIT_SUCCESS 0 |
|---|
| | 27 | #define NO_SOURCE_FOUND 1 |
|---|
| | 28 | #define NO_TARGET_FOUND 2 |
|---|
| | 29 | #define NO_PATH_FOUND 3 |
|---|
| | 30 | #define OVERFLOW 4 |
|---|
| | 31 | |
|---|
| 34 | | |
|---|
| 35 | | // visitor that terminates when we find the goal |
|---|
| 36 | | template <class Vertex> |
|---|
| 37 | | class astar_goal_visitor : public boost::default_astar_visitor |
|---|
| 38 | | { |
|---|
| 39 | | public: |
|---|
| 40 | | astar_goal_visitor(Vertex goal) : m_goal(goal) {} |
|---|
| 41 | | template <class Graph> |
|---|
| 42 | | void examine_vertex(Vertex u, Graph& g) { |
|---|
| 43 | | if(u == m_goal) |
|---|
| 44 | | throw found_goal(); |
|---|
| 45 | | } |
|---|
| 46 | | private: |
|---|
| 47 | | Vertex m_goal; |
|---|
| 48 | | }; |
|---|
| 49 | | |
|---|
| 50 | | template <class Graph, class CostType> |
|---|
| 51 | | class distance_heuristic : public astar_heuristic<Graph, CostType> |
|---|
| 52 | | { |
|---|
| 53 | | public: |
|---|
| 54 | | typedef typename graph_traits<Graph>::vertex_descriptor Vertex; |
|---|
| 55 | | distance_heuristic(Graph& g, Vertex goal):m_g(g), m_goal(goal){} |
|---|
| 56 | | CostType operator()(Vertex u) |
|---|
| 57 | | { |
|---|
| 58 | | CostType dx = m_g[m_goal].x - m_g[u].x; |
|---|
| 59 | | CostType dy = m_g[m_goal].y - m_g[u].y; |
|---|
| 60 | | //You can choose any heuristical function from below |
|---|
| 61 | | //return ::max(dx, dy); |
|---|
| 62 | | //return ::sqrt(dx * dx + dy * dy)/4; |
|---|
| 63 | | //return 0; |
|---|
| 64 | | return (::fabs(dx)+::fabs(dy))/2; |
|---|
| 65 | | } |
|---|
| 66 | | private: |
|---|
| 67 | | Graph& m_g; |
|---|
| 68 | | Vertex m_goal; |
|---|
| 69 | | }; |
|---|
| 70 | | |
|---|
| 71 | | |
|---|
| 72 | | // FIXME: use a template for the directedS parameters |
|---|
| 73 | | typedef adjacency_list < listS, vecS, directedS, no_property, Edge > graph_t; |
|---|
| 110 | | virtual bool addEdge(edge_descriptor *e, int source, int target, graph_t &graph) = 0; |
|---|
| 111 | | virtual void fillFeatures(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j) = 0; |
|---|
| 112 | | virtual void returnEdges() = 0; |
|---|
| | 67 | virtual bool addEdge(edge_descriptor *e, OGRFeature *edge, graph_t &graph) = 0; |
|---|
| | 68 | virtual void fillFeature(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j) = 0; |
|---|
| | 69 | virtual int getRoute(int start, int end, OGRFeature **result, int *resultCount, graph_t &graph) = 0; |
|---|
| | 70 | virtual int makeResult(vertex_descriptor *target, vertex_descriptor *source, |
|---|
| | 71 | std::vector<vertex_descriptor> *predecessors, |
|---|
| | 72 | OGRFeature **result, int *resultCount, graph_t &graph) = 0; |
|---|
| 115 | | int shortestPath(OGRFeature **edges, int *count, int *start, |
|---|
| 116 | | int *end, OGREnvelope *bbox, OGRFeature **result, int *resultCount) |
|---|
| | 75 | int shortestPath(OGRFeature **edges, int count, int start, |
|---|
| | 76 | int end, OGREnvelope *bbox, OGRFeature **result, int *resultCount) |
|---|
| 148 | | class Dijkstra : public Algorithm |
|---|
| 149 | | { |
|---|
| 150 | | public: |
|---|
| 151 | | Dijkstra(){} |
|---|
| 152 | | Dijkstra( char *nameIn, bool directedIn, bool weightedIn, bool reverseCostIn ) |
|---|
| 153 | | : Algorithm( nameIn, directedIn, weightedIn, reverseCostIn ) |
|---|
| 154 | | { |
|---|
| 155 | | } |
|---|
| 156 | | virtual ~Dijkstra(){} |
|---|
| 157 | | |
|---|
| 158 | | static const char *fieldList[]; |
|---|
| 159 | | static const int fieldsCount; |
|---|
| 160 | | |
|---|
| 161 | | bool addEdge(edge_descriptor *e, int source, int target, graph_t &graph) |
|---|
| 162 | | { |
|---|
| 163 | | bool inserted; |
|---|
| 164 | | |
|---|
| 165 | | tie(*e, inserted) = add_edge(source, target, graph); |
|---|
| 166 | | return inserted; |
|---|
| 167 | | } |
|---|
| 168 | | |
|---|
| 169 | | void fillFeatures(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j) |
|---|
| 170 | | { |
|---|
| 171 | | graph[*e].cost = edges[j]->GetFieldAsDouble(Algorithm::costFeature);//set cost |
|---|
| 172 | | graph[*e].id = j;//set id |
|---|
| 173 | | |
|---|
| 174 | | |
|---|
| 175 | | if (!IsDirected() || (IsDirected() && HasReverseCost())) |
|---|
| 176 | | { |
|---|
| 177 | | edge_descriptor *er; |
|---|
| 178 | | //adding an edge for opposite direction |
|---|
| 179 | | bool inserted = addEdge(er, edges[j]->GetFieldAsInteger(Algorithm::sourceFeature), |
|---|
| 180 | | edges[j]->GetFieldAsInteger(Algorithm::targetFeature), graph); |
|---|
| 181 | | |
|---|
| 182 | | graph[*er].id = j;//set id |
|---|
| 183 | | |
|---|
| 184 | | if (HasReverseCost()) |
|---|
| 185 | | { |
|---|
| 186 | | graph[*er].cost = edges[j]->GetFieldAsDouble(Algorithm::reverseCostFeature);//set reverse cost |
|---|
| 187 | | } |
|---|
| 188 | | else |
|---|
| 189 | | { |
|---|
| 190 | | graph[*er].cost = edges[j]->GetFieldAsDouble(Algorithm::costFeature);//set cost |
|---|
| 191 | | } |
|---|
| 192 | | } |
|---|
| 193 | | } |
|---|
| 194 | | }; |
|---|
| 195 | | |
|---|
| 196 | | class AStar : public Algorithm |
|---|
| 197 | | { |
|---|
| 198 | | public: |
|---|
| 199 | | AStar(); |
|---|
| 200 | | AStar(char*, bool, bool, bool); |
|---|
| 201 | | virtual ~AStar(); |
|---|
| 202 | | |
|---|
| 203 | | static const char *fieldList[]; |
|---|
| 204 | | static const int fieldsCount; |
|---|
| 205 | | |
|---|
| 206 | | bool addEdge(edge_descriptor *e, int source, int target, graph_t &graph) |
|---|
| 207 | | { |
|---|
| 208 | | bool inserted; |
|---|
| 209 | | |
|---|
| 210 | | // edges are not inserted in the graph if cost is negative |
|---|
| 211 | | if (edges[j]->GetFieldAsDouble(Algorithm::costFeature) < 0) |
|---|
| 212 | | return; |
|---|
| 213 | | |
|---|
| 214 | | tie(*e, inserted) = add_edge(source, target, graph); |
|---|
| 215 | | return inserted; |
|---|
| 216 | | } |
|---|
| 217 | | |
|---|
| 218 | | |
|---|
| 219 | | }; |
|---|
| 220 | | |
|---|
| 221 | | class ShootingStar : public Algorithm |
|---|
| 222 | | { |
|---|
| 223 | | public: |
|---|
| 224 | | ShootingStar(); |
|---|
| 225 | | ShootingStar(char*, bool, bool, bool); |
|---|
| 226 | | virtual ~ShootingStar(); |
|---|
| 227 | | |
|---|
| 228 | | static const char *fieldList[]; |
|---|
| 229 | | static const int fieldsCount; |
|---|
| 230 | | }; |
|---|
| 231 | | |
|---|