MeshKit
1.0
|
00001 #ifndef DIJKSHORT_H 00002 #define DIJKSHORT_H 00003 00004 #include "meshkit/Mesh.hpp" 00005 #include "meshkit/basic_math.hpp" 00006 00007 #ifdef USE_HASHMAP 00008 #include <tr1/unordered_map> 00009 #endif 00010 00011 using namespace Jaal; 00012 00014 00015 class DijkstraShortestPath { 00016 public: 00017 DijkstraShortestPath( Mesh *m, int mtype = 0) { 00018 vsrc = 0; 00019 vdst = 0; 00020 mesh = m; 00021 filter = NULL; 00022 distance_measure_method = mtype; 00023 complete_path_known = 0; 00024 } 00025 00026 const NodeSequence& getPath(Vertex *vs, Vertex *vd = NULL ) { 00027 assert( !vs->isRemoved() ); 00028 filter = NULL; 00029 if( vs != vsrc ) complete_path_known = 0; 00030 vsrc = vs; 00031 vdst = vd; 00032 if(!complete_path_known) fastmarching(); 00033 traceback(); 00034 return nodepath; 00035 } 00036 00037 const NodeSequence& getPath(Vertex *vs, MeshFilter *f) { 00038 assert( vs ); 00039 assert( !vs->isRemoved() ); 00040 filter = f; 00041 if( vs != vsrc ) complete_path_known = 0; 00042 vsrc = vs; 00043 vdst = NULL; 00044 if(!complete_path_known) fastmarching(); 00045 traceback(); 00046 return nodepath; 00047 } 00048 00049 private: 00050 // Input parameters ... 00051 Mesh *mesh; 00052 Vertex *vsrc, *vdst; 00053 MeshFilter *filter; 00054 00055 // Output data ... 00056 NodeSequence nodepath; 00057 00058 // Local data ... 00059 int distance_measure_method; 00060 bool complete_path_known; 00061 struct LVertex { 00062 00063 LVertex() { 00064 distance = 0.0; 00065 previous = NULL; 00066 vertex = NULL; 00067 } 00068 00069 size_t getID() const { 00070 return vertex->getID(); 00071 } 00072 00073 bool operator > ( const LVertex &rhs) const { 00074 return distance > rhs.distance; 00075 } 00076 00077 bool operator < ( const LVertex &rhs) const { 00078 return distance < rhs.distance; 00079 } 00080 00081 double distance; // Shortest distance from the source to this point 00082 Vertex *vertex; // Current Vertex 00083 Vertex *previous; // Previous vertex 00084 }; 00085 00086 #ifdef USE_HASHMAP 00087 std::tr1::unordered_map<Vertex*, LVertex> vmap; 00088 std::tr1::unordered_map<Vertex*,LVertex>::const_iterator miter; 00089 #else 00090 std::map<Vertex*, LVertex> vmap; 00091 std::map<Vertex*, LVertex>::const_iterator miter; 00092 #endif 00093 00094 std::priority_queue<LVertex, vector<LVertex>, greater<LVertex> > vertexQ; 00095 00096 int atomicOp( LVertex &node); 00097 void fastmarching(); // Fast Marching Style algorithm O(nlogn) with heap 00098 void traceback(); 00099 00100 double getCost(const LVertex &vi, const LVertex &vj ) const { 00101 double val = vi.distance; 00102 if(distance_measure_method == TOPOLOGICAL_DISTANCE ) { 00103 val += 1.0; 00104 } else { 00105 const Point3D &p1 = vi.vertex->getXYZCoords(); 00106 const Point3D &p2 = vj.vertex->getXYZCoords(); 00107 double d = Math::length2(p1,p2); 00108 assert( d > 0.0); 00109 val += d; 00110 } 00111 return val; 00112 } 00113 }; 00114 00115 namespace Jaal { 00116 int dijkstra_shortest_path_test(); 00117 }; 00118 00120 00121 #endif