1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include "meshkit/DijkstraShortestPath.hpp"
#include "meshkit/StopWatch.hpp"
#include <limits>
///////////////////////////////////////////////////////////////////////////////

int DijkstraShortestPath::atomicOp(LVertex &currnode)
{
     Vertex *vi = currnode.vertex;

     if (vi == vdst) return 0;

     if( filter ) {
          if( vi != vsrc  ) {
               if( !filter->pass( vi ) ) {
                    vdst = vi;
                    return 0;
               }
          }
     }

     NodeSequence vneighs;
     vi->getRelations( vneighs );
     int nSize = vneighs.size();

//   int offset = Math::random_value((int)0, nSize-1);
     int offset = 0;

     LVertex lv;
     for (int i = 0; i < nSize; i++) {
          Vertex *vj = vneighs[(i+offset)%nSize];
          assert( !vj->isRemoved() );
          miter = vmap.find( vj );
          if( miter == vmap.end()) {
               lv.distance = std::numeric_limits< double >:: max();
               lv.vertex   = vj;
               lv.previous = vi;
               vmap.insert(make_pair(vj,lv));
          } else {
               lv = miter->second;
          }
          assert( lv.vertex == vj );

          double vcost = getCost(currnode, lv);
          if (vcost < lv.distance) {
               lv.previous = vi;
               lv.distance = vcost;
               vmap[vj]    = lv;
               vertexQ.push(lv);
          }
     }

     return 1;
}

///////////////////////////////////////////////////////////////////////////////

void DijkstraShortestPath::traceback()
{
     nodepath.clear();
     if( vdst == NULL ) return;

     miter = vmap.find(vdst);
     if( miter == vmap.end() ) return;

     LVertex currnode = miter->second;
     while(1) {
          nodepath.push_back( currnode.vertex );
          Vertex *v = currnode.previous;
          if (v == NULL) break;
          currnode = vmap[v];
     }

     assert( nodepath.front() == vdst );
     assert( nodepath.back()  == vsrc );
}
///////////////////////////////////////////////////////////////////////////////

void DijkstraShortestPath::fastmarching()
{
     assert( mesh->getAdjTable(0,0) );

     while (!vertexQ.empty()) vertexQ.pop();

     vmap.clear();

     LVertex lv;
     lv.vertex   = vsrc;
     lv.distance = 0.0;
     lv.previous = NULL;
     vmap[vsrc]  = lv;
     vertexQ.push( lv );

     assert( !vsrc->isRemoved() );

     int progress;<--- The scope of the variable 'progress' can be reduced.
     while (!vertexQ.empty()) {
          LVertex currVertex = vertexQ.top();
          vertexQ.pop();
          progress = atomicOp(currVertex);
          if (!progress) break;
     }
}
///////////////////////////////////////////////////////////////////////////////

int Jaal::dijkstra_shortest_path_test()<--- The function 'dijkstra_shortest_path_test' is never used.
{
     double origin[]   = { 0.0, 0.0, 0.0};
     double length[]   = { 1.0, 1.0, 1.0};
     int    gridim[]   = { 5, 5, 5};

     Jaal::Mesh *mesh = Jaal::create_structured_mesh(origin, length, gridim, 2 );

     struct MyFilter: public MeshFilter {
          MyFilter( int i ) {
               id = i;
          }
          size_t id;
          bool pass( const Vertex *v) const{
               return v->getID() != id;
          }
     };

     MeshFilter *filter = new MyFilter(18);

     DijkstraShortestPath djk(mesh);

     NodeSequence sq = djk.getPath(mesh->getNodeAt(0), filter);

     for( size_t i = 0; i < sq.size(); i++)
          cout << sq[i]->getID() << endl;

     delete filter;

     return 0;
}

///////////////////////////////////////////////////////////////////////////////