Changeset 81
- Timestamp:
- 12/19/07 13:53:41 (1 year ago)
- Files:
-
- sandbox/orkney/algorithm.h (modified) (5 diffs)
- sandbox/orkney/astar.h (modified) (7 diffs)
- sandbox/orkney/dijkstra.h (modified) (8 diffs)
- sandbox/orkney/edge_visitors.hpp (added)
- sandbox/orkney/shootingstar.h (modified) (18 diffs)
- sandbox/orkney/shooting_star_relax.hpp (added)
- sandbox/orkney/shooting_star_search.hpp (added)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
sandbox/orkney/algorithm.h
r78 r81 48 48 #define NO_TARGET_FOUND 2 49 49 #define NO_PATH_FOUND 3 50 #define OVERFLOW450 #define RESULT_OVERFLOW 4 51 51 52 52 … … 54 54 using namespace boost; 55 55 56 struct Edge 57 { 58 int id; 59 int source; 60 int target; 61 double cost; 62 double distance; 63 double rank; 64 std::map< int, vector< std::pair<float, std::vector<int> > >, std::less<int> > adjacent_edges; 65 default_color_type color; 66 }; 67 68 struct Vertex 69 { 70 int id; 71 double x; 72 double y; 73 }; 74 56 75 struct found_goal {}; // exception for termination 57 76 77 typedef adjacency_list < listS, vecS, directedS, Vertex, Edge > graph_t; 78 79 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; 80 typedef graph_traits < graph_t >::edge_descriptor edge_descriptor; 81 58 82 class Algorithm 59 83 { … … 65 89 char *name; 66 90 91 int e_max_id; 67 92 68 93 public: … … 82 107 83 108 virtual bool addEdge(edge_descriptor *e, OGRFeature *edge, graph_t &graph) = 0; 84 virtual void fillFeature(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j) = 0; 85 virtual int getRoute(int start, int end, OGRFeature **result, int *resultCount, graph_t &graph) = 0; 86 virtual int makeResult(vertex_descriptor *target, vertex_descriptor *source, 87 std::vector<vertex_descriptor> *predecessors, 88 OGRFeature **result, int *resultCount, graph_t &graph) = 0; 109 virtual void fillFeature(OGRFeature **edges, unsigned int count, edge_descriptor *e, graph_t &graph, int j) = 0; 110 virtual int getRoute(int start, int end, OGRFeature **edges, OGRFeature **result, int *resultCount, graph_t &graph) = 0; 111 virtual int makeResult(vertex_descriptor target, vertex_descriptor source, 112 OGRFeature **edges, 113 std::vector<vertex_descriptor> &predecessors, 114 OGRFeature **result, int *resultCount, graph_t &graph) = 0; 89 115 90 116 //Template method for shortest path calculation 91 int shortestPath(OGRFeature **edges, int count, int start,117 int shortestPath(OGRFeature **edges, unsigned int count, int start, 92 118 int end, OGREnvelope *bbox, OGRFeature **result, int *resultCount) 93 119 { 94 typedef adjacency_list < listS, vecS, directedS, Vertex, Edge > graph_t;95 96 typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor;97 typedef graph_traits < graph_t >::edge_descriptor edge_descriptor;98 120 99 121 // FIXME: compute this value 100 const unsigned int num_nodes = ((IsDirected() && HasReverseCost() ? 2 : 1) * (*count)) + 100;122 const unsigned int num_nodes = ((IsDirected() && HasReverseCost() ? 2 : 1) * count) + 100; 101 123 102 124 graph_t graph(num_nodes); 125 126 for (unsigned int z = 0; z < count; ++z) 127 { 128 if(edges[z]->GetFieldAsInteger(ID_FEATURE) > e_max_id) 129 e_max_id=edges[z]->GetFieldAsInteger(ID_FEATURE); 130 } 103 131 104 132 for (std::size_t j = 0; j < count; ++j) … … 106 134 edge_descriptor *e; 107 135 108 fillFeatures(edges, e, graph, j);136 fillFeature(edges, count, e, graph, j); 109 137 } 110 138 111 return getRoute(start, end, result, resultCount, graph);139 return getRoute(start, end, edges, result, resultCount, graph); 112 140 } 113 141 sandbox/orkney/astar.h
r78 r81 39 39 using namespace std; 40 40 using namespace boost; 41 42 struct Edge43 {44 int id;45 double cost;46 };47 48 struct Vertex49 {50 int id;51 double x;52 double y;53 };54 55 41 56 42 // visitor that terminates when we find the goal … … 95 81 AStar(){} 96 82 AStar( char *nameIn, bool directedIn, bool weightedIn, bool reverseCostIn ) 97 : Algorithm( nameIn, directedIn, weightedIn, reverseCostIn )83 : Dijkstra( nameIn, directedIn, weightedIn, reverseCostIn ){} 98 84 virtual ~AStar(); 99 85 … … 102 88 bool inserted; 103 89 104 if ( cost< 0) // edges are not inserted in the graph if cost is negative105 return ;90 if (edge->GetFieldAsDouble(COST_FEATURE) < 0) // edges are not inserted in the graph if cost is negative 91 return false; 106 92 107 93 tie(*e, inserted) = add_edge(edge->GetFieldAsInteger(SOURCE_FEATURE), 108 94 edge->GetFieldAsInteger(TARGET_FEATURE), graph); 109 95 110 graph[ e].cost = edge->GetFieldAsDouble(COST_FEATURE);111 graph[ e].id = edge->GetFieldAsInteger(ID_FEATURE);96 graph[*e].cost = edge->GetFieldAsDouble(COST_FEATURE); 97 graph[*e].id = edge->GetFieldAsInteger(ID_FEATURE); 112 98 113 OGRLineString *geom = static_cast<OGRLineString*> edge->GetGeometryRef();99 OGRLineString *geom = static_cast<OGRLineString*>(edge->GetGeometryRef()); 114 100 115 101 vertex_descriptor s = vertex(edge->GetFieldAsInteger(SOURCE_FEATURE), graph); … … 120 106 graph[t].x=geom->getX(geom->getNumPoints()-1); 121 107 graph[t].y=geom->getY(geom->getNumPoints()-1); 108 109 return inserted; 122 110 } 123 111 124 112 125 int getRoute(int start, int end, OGRFeature ** result, int *resultCount, graph_t &graph)113 int getRoute(int start, int end, OGRFeature **edges, OGRFeature **result, int *resultCount, graph_t &graph) 126 114 { 127 115 std::vector<vertex_descriptor> predecessors(num_vertices(graph)); … … 129 117 vertex_descriptor source = vertex(start, graph); 130 118 131 if (source _vertex< 0)119 if (source < 0) 132 120 { 133 121 return NO_SOURCE_FOUND; … … 135 123 136 124 vertex_descriptor target = vertex(end, graph); 137 if (target _vertex< 0)125 if (target < 0) 138 126 { 139 127 return NO_TARGET_FOUND; 140 128 } 141 129 142 std::vector< float8> distances(num_vertices(graph));130 std::vector<double> distances(num_vertices(graph)); 143 131 144 132 try … … 155 143 catch(found_goal fg) 156 144 { 157 return makeResult( *target, *source, *predecessors, result, resultCount, graph);145 return makeResult(target, source, edges, predecessors, result, resultCount, graph); 158 146 } 159 147 } sandbox/orkney/dijkstra.h
r79 r81 38 38 using namespace boost; 39 39 40 struct Edge41 {42 int id;43 double cost;44 };45 46 struct Vertex47 {48 int id;49 };50 51 40 class Dijkstra : public Algorithm 52 41 { … … 66 55 } 67 56 68 virtual void fillFeature s(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j)57 virtual void fillFeature(OGRFeature **edges, unsigned int count, edge_descriptor *e, graph_t &graph, int j) 69 58 { 70 59 bool inserted = addEdge(e, edges[j], graph); … … 93 82 } 94 83 95 virtual int getRoute(int start, int end, OGRFeature ** result, int *resultCount, graph_t &graph)84 virtual int getRoute(int start, int end, OGRFeature **edges, OGRFeature **result, int *resultCount, graph_t &graph) 96 85 { 97 86 std::vector<vertex_descriptor> predecessors(num_vertices(graph)); … … 99 88 vertex_descriptor source = vertex(start, graph); 100 89 101 if (source _vertex< 0)90 if (source < 0) 102 91 { 103 92 return NO_SOURCE_FOUND; … … 105 94 106 95 vertex_descriptor target = vertex(end, graph); 107 if (target _vertex< 0)96 if (target < 0) 108 97 { 109 98 return NO_TARGET_FOUND; 110 99 } 111 100 112 std::vector< float8> distances(num_vertices(graph));101 std::vector<double> distances(num_vertices(graph)); 113 102 114 103 dijkstra_shortest_paths(graph, source, 115 104 predecessor_map(&predecessors[0]). 116 weight_map(get(& Vertex::cost, graph))105 weight_map(get(&Edge::cost, graph)) 117 106 .distance_map(&distances[0])); 118 107 119 return makeResult( *target, *source, *predecessors, result, resultCount, graph);108 return makeResult(target, source, edges, predecessors, result, resultCount, graph); 120 109 121 110 } 122 111 123 virtual int makeResult(vertex_descriptor *target, vertex_descriptor *source, 124 std::vector<vertex_descriptor> *predecessors, 125 OGRFeature **result, int *resultCount, graph_t &graph) 112 virtual int makeResult(vertex_descriptor target, vertex_descriptor source, 113 OGRFeature **edges, 114 std::vector<vertex_descriptor> &predecessors, 115 OGRFeature **result, int *resultCount, graph_t &graph) 126 116 { 127 117 vector<int> path_vect; 128 118 int max = MAX_NODES; 129 path_vect.push_back( end);119 path_vect.push_back(target); 130 120 131 121 while (target != source) … … 140 130 if (!max--) 141 131 { 142 return OVERFLOW;132 return RESULT_OVERFLOW; 143 133 } 144 134 } … … 146 136 //*result = (OGRFeature *) malloc(sizeof(OGRFeature) * (path_vect.size() + 1)); 147 137 //Let's try to do it in C++ style 148 *result = new OGRFeature* [path_vect.size() + 1];138 result = new OGRFeature* [path_vect.size() + 1]; 149 139 *resultCount = path_vect.size(); 140 int i, j; 150 141 151 for(i nt i = path_vect.size() - 1, intj = 0; i >= 0; i--, j++)142 for(i = path_vect.size() - 1, j = 0; i >= 0; i--, j++) 152 143 { 153 144 graph_traits < graph_t >::vertex_descriptor v_src; … … 169 160 graph_traits < graph_t >::vertex_descriptor v, targ; 170 161 e = *out_i; 171 v = source(e, graph);172 targ = target(e, graph);162 v = boost::source(e, graph); 163 targ = boost::target(e, graph); 173 164 174 165 if (targ == v_targ) 175 166 { 176 (*result)[j] = (*edges)[graph[*out_i].id];167 result[j] = edges[graph[*out_i].id]; 177 168 break; 178 169 } sandbox/orkney/shootingstar.h
r80 r81 32 32 #include <boost/graph/adjacency_list.hpp> 33 33 #include <boost/vector_property_map.hpp> 34 #include <shooting_star_search.hpp>35 34 36 35 #include <math.h> // for sqrt and fabs 37 36 38 37 #include "algorithm.h" 38 #include "shooting_star_search.hpp" 39 39 40 40 … … 46 46 using namespace std; 47 47 using namespace boost; 48 49 struct Edge50 {51 int id;52 int source;53 int target;54 double cost;55 double distance;56 double rank;57 std::map< int, vector< std::pair<float, std::vector<int> > >, std::less<int> > adjacent_edges;58 default_color_type color;59 };60 61 struct Vertex62 {63 int id;64 double x;65 double y;66 };67 48 68 49 // visitor that terminates when we find the goal … … 90 71 91 72 template <class Graph, class CostType> 92 class distance_heuristic73 class edge_distance_heuristic 93 74 { 94 75 public: 95 76 typedef typename graph_traits<Graph>::edge_descriptor Edge; 96 distance_heuristic(Graph& g, Edge goal):m_g(g), m_goal(goal){}77 edge_distance_heuristic(Graph& g, Edge goal):m_g(g), m_goal(goal){} 97 78 CostType operator()(Edge e) 98 79 { … … 117 98 ShootingStar():offset(1), rule_num(0){} 118 99 ShootingStar( char *nameIn, bool directedIn, bool weightedIn, bool reverseCostIn ) 119 : Algorithm( nameIn, directedIn, weightedIn, reverseCostIn ) :offset(1), rule_num(0){}100 : Algorithm( nameIn, directedIn, weightedIn, reverseCostIn ), offset(1), rule_num(0){} 120 101 virtual ~ShootingStar(); 121 102 … … 131 112 bool inserted; 132 113 133 if ( cost < 0) // edges are not inserted in the graphif cost is negative134 return;114 if (edge->GetFieldAsDouble(COST_FEATURE) < 0) // edges are inserted as unpassable if cost is negative 115 edge->SetField(COST_FEATURE, MAX_COST); 135 116 136 117 tie(*e, inserted) = add_edge(edge->GetFieldAsInteger(SOURCE_FEATURE), 137 118 edge->GetFieldAsInteger(TARGET_FEATURE), graph); 138 119 139 graph[ e].cost = edge->GetFieldAsDouble(COST_FEATURE);140 graph[ e].id = edge->GetFieldAsInteger(ID_FEATURE);141 142 graph[ e].source = edge->GetFieldAsInteger(SOURCE_FEATURE);143 graph[ e].target = edge->GetFieldAsInteger(TARGET_FEATURE);144 145 graph[ e].adjacent_edges = adjacent_edges;146 147 graph[ e].rank = 0;148 graph[ e].distance = 0;149 150 OGRLineString *geom = static_cast<OGRLineString*> edge->GetGeometryRef();120 graph[*e].cost = edge->GetFieldAsDouble(COST_FEATURE); 121 graph[*e].id = edge->GetFieldAsInteger(ID_FEATURE); 122 123 graph[*e].source = edge->GetFieldAsInteger(SOURCE_FEATURE); 124 graph[*e].target = edge->GetFieldAsInteger(TARGET_FEATURE); 125 126 graph[*e].adjacent_edges = adjacent_edges; 127 128 graph[*e].rank = 0; 129 graph[*e].distance = 0; 130 131 OGRLineString *geom = static_cast<OGRLineString*>(edge->GetGeometryRef()); 151 132 152 133 vertex_descriptor s = vertex(edge->GetFieldAsInteger(SOURCE_FEATURE), graph); … … 160 141 graph[t].x=geom->getX(geom->getNumPoints()-1); 161 142 graph[t].y=geom->getY(geom->getNumPoints()-1); 162 } 163 164 virtual void fillFeatures(OGRFeature **edges, edge_descriptor *e, graph_t &graph, int j) 143 144 return inserted; 145 } 146 147 virtual void fillFeatures(OGRFeature **edges, unsigned int count, edge_descriptor *e, graph_t &graph, int j) 165 148 { 149 int src, trg; 166 150 167 151 //Vertex ids renumbering moved here … … 181 165 } 182 166 edges[j]->SetField(TARGET_FEATURE, vertices[trg]); 183 184 for(z=0; z<edges[j]->GetFieldAsIntegerList(RULE_FEATURE).size();++z) 185 { 186 if(edges[j]->GetFieldAsIntegerList(RULE_FEATURE)[z] > 0) 187 { 188 rule.push_back(edges[j]->GetFieldAsIntegerList(RULE_FEATURE)[z]); 167 168 int *rule_cnt; 169 const int *rule_list; 170 171 rule_list = edges[j]->GetFieldAsIntegerList(RULE_FEATURE, rule_cnt); 172 173 for(int z=0; z< *rule_cnt;++z) 174 { 175 if(rule_list[z] > 0) 176 { 177 rule.push_back(rule_list[z]); 189 178 } 190 179 } … … 193 182 { 194 183 adjacent_edges[edges[j]->GetFieldAsInteger(ID_FEATURE)].push_back( 195 std::pair< float8, vector<int> > (GetFieldAsDouble(TO_COST_FEATURE), rule) );184 std::pair<double, vector<int> > (edges[j]->GetFieldAsDouble(TO_COST_FEATURE), rule) ); 196 185 rule.clear(); 197 186 } … … 209 198 { 210 199 211 if(adjacent_edges[edges _array[j].id].size() > 0)200 if(adjacent_edges[edges[j]->GetFieldAsInteger(ID_FEATURE)].size() > 0) 212 201 { 213 202 adjacent_edges[edges[j]->GetFieldAsInteger(ID_FEATURE)+e_max_id].assign( … … 244 233 } 245 234 246 int getRoute(int start, int end, OGRFeature ** result, int *resultCount, graph_t &graph)235 int getRoute(int start, int end, OGRFeature **edges, OGRFeature **result, int *resultCount, graph_t &graph) 247 236 { 248 237 edge_descriptor source_edge; … … 253 242 graph_traits<graph_t>::edge_iterator ei, ei_end; 254 243 255 for(tie(ei, ei_end) = edges(graph); ei != ei_end; ++ei)244 for(tie(ei, ei_end) = boost::edges(graph); ei != ei_end; ++ei) 256 245 { 257 246 if(graph[*ei].id == start) … … 269 258 270 259 271 for(tie(ei, ei_end) = edges(graph); ei != ei_end; ++ei)260 for(tie(ei, ei_end) = boost::edges(graph); ei != ei_end; ++ei) 272 261 { 273 262 if(graph[*ei].id == end) … … 288 277 std::map< int, edge_descriptor, std::less<int> > predecessors; 289 278 290 property_map<graph_t, floatEdge::*>::type rank = get(&Edge::rank, graph);291 property_map<graph_t, floatEdge::*>::type distance = get(&Edge::distance, graph);279 property_map<graph_t, double Edge::*>::type rank = get(&Edge::rank, graph); 280 property_map<graph_t, double Edge::*>::type distance = get(&Edge::distance, graph); 292 281 293 282 try … … 295 284 shooting_star_search 296 285 (graph, source_edge, 297 distance_heuristic<graph_t, float>(graph, target_edge),286 edge_distance_heuristic<graph_t, float>(graph, target_edge), 298 287 weight_map(get(&Edge::cost, graph)). 299 288 weight_map2(get(&Edge::adjacent_edges, graph)). … … 307 296 catch(found_goal &fg) 308 297 { 309 return makeResult( *target, *source, *predecessors, result, resultCount, graph);298 return makeResult(target_edge, source_edge, edges, predecessors, result, resultCount, graph); 310 299 } 311 300 } 312 301 313 virtual int makeResult(edge_descriptor *target_edge, edge_descriptor *source_edge, 314 std::map< int, edge_descriptor, std::less<int> > *predecessors, 315 OGRFeature **result, int *resultCount, graph_t &graph) 302 int makeResult(edge_descriptor target_edge, edge_descriptor source_edge, 303 OGRFeature **edges, 304 std::map< int, edge_descriptor, std::less<int> > &predecessors, 305 OGRFeature **result, int *resultCount, graph_t &graph) 316 306 { 317 307 vector<edge_descriptor> path_vect; … … 334 324 if (!max--) 335 325 { 336 return OVERFLOW;326 return RESULT_OVERFLOW; 337 327 } 338 328 } … … 340 330 //*result = (OGRFeature *) malloc(sizeof(OGRFeature) * (path_vect.size() + 1)); 341 331 //Let's try to do it in C++ style 342 *result = new OGRFeature * [path_vect.size() + 1];343 * path_count = path_vect.size();332 result = new OGRFeature * [path_vect.size() + 1]; 333 *resultCount = path_vect.size(); 344 334 345 335 for(int i = path_vect.size() - 1, j = 0; i >= 0; i--, j++) … … 354 344 } 355 345 356 (*result)[j] = graph[e].id;346 result[j] = edges[graph[e].id]; 357 347 } 358 348

