VTK
|
00001 /*========================================================================= 00002 00003 Program: Visualization Toolkit 00004 Module: vtkDijkstraGraphInternals.h 00005 00006 Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen 00007 All rights reserved. 00008 See Copyright.txt or http://www.kitware.com/Copyright.htm for details. 00009 00010 This software is distributed WITHOUT ANY WARRANTY; without even 00011 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00012 PURPOSE. See the above copyright notice for more information. 00013 00014 =========================================================================*/ 00024 #ifndef __vtkDijkstraGraphInternals_h 00025 #define __vtkDijkstraGraphInternals_h 00026 00027 #include <vector> 00028 #include <map> 00029 00030 //----------------------------------------------------------------------------- 00031 class vtkDijkstraGraphInternals 00032 { 00033 public: 00034 00035 vtkDijkstraGraphInternals() 00036 { 00037 this->HeapSize = 0; 00038 } 00039 00040 ~vtkDijkstraGraphInternals() 00041 { 00042 } 00043 00044 // CumulativeWeights(v) current summed weight for path to vertex v. 00045 std::vector<double> CumulativeWeights; 00046 00047 // Predecessors(v) predecessor of v. 00048 std::vector<int> Predecessors; 00049 00050 // OpenVertices is the set of vertices which has not a shortest path yet but has a path. 00051 // OpenVertices(v) == 1 means that vertex v is in OpenVertices. 00052 // OpenVertices is a boolean (1/0) array. 00053 std::vector<unsigned char> OpenVertices; 00054 00055 // ClosedVertices is the set of vertices with already determined shortest path 00056 // ClosedVertices(v) == 1 means that vertex v is in ClosedVertices. 00057 // ClosedVertices is a boolean (1/0) array. 00058 std::vector<unsigned char> ClosedVertices; 00059 00060 // Adjacency representation. 00061 std::vector< std::map< int,double > > Adjacency; 00062 00063 // Path repelling by assigning high costs to flagged vertices. 00064 std::vector<unsigned char> BlockedVertices; 00065 00066 00067 void Heapify(const int& i) 00068 { 00069 // left node 00070 unsigned int l = i * 2; 00071 // right node 00072 unsigned int r = i * 2 + 1; 00073 int smallest = -1; 00074 00075 // The value of element v is CumulativeWeights(v) 00076 // the heap stores the vertex numbers 00077 if ( l <= this->HeapSize && 00078 ( this->CumulativeWeights[ this->Heap[l] ] < 00079 this->CumulativeWeights[ this->Heap[i] ] ) ) 00080 { 00081 smallest = l; 00082 } 00083 else 00084 { 00085 smallest = i; 00086 } 00087 00088 if ( r <= this->HeapSize && 00089 ( this->CumulativeWeights[ this->Heap[ r ] ] < 00090 this->CumulativeWeights[ this->Heap[ smallest ] ] ) ) 00091 { 00092 smallest = r; 00093 } 00094 00095 if ( smallest != i ) 00096 { 00097 int t = this->Heap[i]; 00098 00099 this->Heap[ i ] = this->Heap[ smallest ]; 00100 00101 // where is Heap(i) 00102 this->HeapIndices[ this->Heap[i] ] = i; 00103 00104 // Heap and HeapIndices are kinda inverses 00105 this->Heap[ smallest ] = t; 00106 this->HeapIndices[ t ] = smallest; 00107 00108 this->Heapify( smallest ); 00109 } 00110 } 00111 00112 void HeapInsert(const int& v) 00113 { 00114 if ( this->HeapSize >= (this->Heap.size() - 1) ) 00115 { 00116 return; 00117 } 00118 00119 this->HeapSize++; 00120 int i = this->HeapSize; 00121 00122 while ( i > 1 && 00123 this->CumulativeWeights[ this->Heap[i/2] ] > 00124 this->CumulativeWeights[v] ) 00125 { 00126 this->Heap[ i ] = this->Heap[i/2]; 00127 this->HeapIndices[ this->Heap[i] ] = i; 00128 i /= 2; 00129 } 00130 // Heap and HeapIndices are kinda inverses 00131 this->Heap[ i ] = v; 00132 this->HeapIndices[ v ] = i; 00133 } 00134 00135 int HeapExtractMin() 00136 { 00137 if ( this->HeapSize == 0 ) 00138 { 00139 return -1; 00140 } 00141 00142 int minv = this->Heap[ 1 ]; 00143 this->HeapIndices[ minv ] = -1; 00144 00145 this->Heap[ 1 ] = this->Heap[ this->HeapSize ]; 00146 this->HeapIndices[ this->Heap[1] ]= 1; 00147 00148 this->HeapSize--; 00149 this->Heapify( 1 ); 00150 00151 return minv; 00152 } 00153 00154 void HeapDecreaseKey(const int& v) 00155 { 00156 // where in Heap is vertex v 00157 int i = this->HeapIndices[ v ]; 00158 if ( i < 1 || i > static_cast<int>(this->HeapSize) ) 00159 { 00160 return; 00161 } 00162 00163 while ( i > 1 && 00164 this->CumulativeWeights[ this->Heap[ i/2 ] ] > 00165 this->CumulativeWeights[ v ] ) 00166 { 00167 this->Heap[ i ] = this->Heap[i/2]; 00168 this->HeapIndices[ this->Heap[i] ] = i; 00169 i /= 2; 00170 } 00171 00172 // Heap and HeapIndices are kinda inverses 00173 this->Heap[ i ] = v; 00174 this->HeapIndices[ v ] = i; 00175 } 00176 00177 void ResetHeap() 00178 { 00179 this->HeapSize = 0; 00180 } 00181 00182 void InitializeHeap(const int& size) 00183 { 00184 this->Heap.resize( size + 1 ); 00185 this->HeapIndices.resize( size ); 00186 } 00187 00188 private: 00189 unsigned int HeapSize; 00190 00191 // The priority que (a binary heap) with vertex indices. 00192 std::vector<int> Heap; 00193 00194 // HeapIndices(v) the position of v in Heap (HeapIndices and Heap are kind of inverses). 00195 std::vector<int> HeapIndices; 00196 00197 }; 00198 00199 #endif 00200 // VTK-HeaderTest-Exclude: vtkDijkstraGraphInternals.h