pgRouting

Changeset 73

Show
Ignore:
Timestamp:
12/12/07 17:38:07 (1 year ago)
Author:
anton
Message:

Algorithm header files splitted

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/orkney/algorithm.h

    r71 r73  
    99#include <boost/graph/graph_traits.hpp> 
    1010#include <boost/graph/adjacency_list.hpp> 
    11 #include <boost/graph/dijkstra_shortest_paths.hpp> 
    12 #include <boost/graph/astar_search.hpp> 
     11 
    1312 
    1413#define MAX_NODES 1000000 
     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 
    1532 
    1633using namespace std; 
    1734using namespace boost; 
    1835 
    19 struct Edge 
    20 { 
    21     int id; 
    22     double cost; 
    23 }; 
    24  
    25 struct Vertex 
    26 { 
    27   int id; 
    28   double x; 
    29   double y; 
    30 }; 
    31  
    32  
    3336struct found_goal {}; // exception for termination 
    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; 
    7437 
    7538typedef graph_traits < graph_t >::vertex_descriptor vertex_descriptor; 
     
    10164            } 
    10265    virtual ~Algorithm(){} 
    103      
    104     static const char *costFeature; 
    105     static const char *weightFeature; 
    106     static const char *reverseCostFeature; 
    107     static const char *sourceFeature; 
    108     static const char *targetFeature; 
    10966                    
    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; 
    11373 
    11474    //Template method for shortest path calculation 
    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) 
    11777    {       
    11878      // FIXME: compute this value 
     
    12181      graph_t graph(num_nodes); 
    12282       
    123       for (std::size_t j = 0; j < *count; ++j) 
     83      for (std::size_t j = 0; j < count; ++j) 
    12484      { 
    12585        edge_descriptor *e;  
    12686         
    127         bool inserted = addEdge(e, edges[j]->GetFieldAsInteger(Algorithm::sourceFeature),  
    128                                 edges[j]->GetFieldAsInteger(Algorithm::targetFeature), graph); 
    129          
    130         fillFeatures(edges, e, graph, j); 
    131          
     87        fillFeatures(edges, e, graph, j);        
    13288      } 
    13389       
    134       returnEdges();       
     90      return getRoute(start, end, result, resultCount, graph);       
    13591    } 
    13692     
     
    146102}; 
    147103 
    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  
    232104#endif