VTK
vtkDijkstraGraphInternals.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Visualization Toolkit
4  Module: vtkDijkstraGraphInternals.h
5 
6  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7  All rights reserved.
8  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10  This software is distributed WITHOUT ANY WARRANTY; without even
11  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12  PURPOSE. See the above copyright notice for more information.
13 
14 =========================================================================*/
24 #ifndef vtkDijkstraGraphInternals_h
25 #define vtkDijkstraGraphInternals_h
26 
27 #include <vector>
28 #include <map>
29 
30 //-----------------------------------------------------------------------------
32 {
33 public:
34 
36  {
37  this->HeapSize = 0;
38  }
39 
41  {
42  }
43 
44  // CumulativeWeights(v) current summed weight for path to vertex v.
45  std::vector<double> CumulativeWeights;
46 
47  // Predecessors(v) predecessor of v.
48  std::vector<int> Predecessors;
49 
50  // OpenVertices is the set of vertices which has not a shortest path yet but has a path.
51  // OpenVertices(v) == 1 means that vertex v is in OpenVertices.
52  // OpenVertices is a boolean (1/0) array.
53  std::vector<unsigned char> OpenVertices;
54 
55  // ClosedVertices is the set of vertices with already determined shortest path
56  // ClosedVertices(v) == 1 means that vertex v is in ClosedVertices.
57  // ClosedVertices is a boolean (1/0) array.
58  std::vector<unsigned char> ClosedVertices;
59 
60  // Adjacency representation.
61  std::vector< std::map< int,double > > Adjacency;
62 
63  // Path repelling by assigning high costs to flagged vertices.
64  std::vector<unsigned char> BlockedVertices;
65 
66 
67  void Heapify(const int& i)
68  {
69  // left node
70  unsigned int l = i * 2;
71  // right node
72  unsigned int r = i * 2 + 1;
73  int smallest = -1;
74 
75  // The value of element v is CumulativeWeights(v)
76  // the heap stores the vertex numbers
77  if ( l <= this->HeapSize &&
78  ( this->CumulativeWeights[ this->Heap[l] ] <
79  this->CumulativeWeights[ this->Heap[i] ] ) )
80  {
81  smallest = l;
82  }
83  else
84  {
85  smallest = i;
86  }
87 
88  if ( r <= this->HeapSize &&
89  ( this->CumulativeWeights[ this->Heap[ r ] ] <
90  this->CumulativeWeights[ this->Heap[ smallest ] ] ) )
91  {
92  smallest = r;
93  }
94 
95  if ( smallest != i )
96  {
97  int t = this->Heap[i];
98 
99  this->Heap[ i ] = this->Heap[ smallest ];
100 
101  // where is Heap(i)
102  this->HeapIndices[ this->Heap[i] ] = i;
103 
104  // Heap and HeapIndices are kinda inverses
105  this->Heap[ smallest ] = t;
106  this->HeapIndices[ t ] = smallest;
107 
108  this->Heapify( smallest );
109  }
110  }
111 
112  void HeapInsert(const int& v)
113  {
114  if ( this->HeapSize >= (this->Heap.size() - 1) )
115  {
116  return;
117  }
118 
119  this->HeapSize++;
120  int i = this->HeapSize;
121 
122  while ( i > 1 &&
123  this->CumulativeWeights[ this->Heap[i/2] ] >
124  this->CumulativeWeights[v] )
125  {
126  this->Heap[ i ] = this->Heap[i/2];
127  this->HeapIndices[ this->Heap[i] ] = i;
128  i /= 2;
129  }
130  // Heap and HeapIndices are kinda inverses
131  this->Heap[ i ] = v;
132  this->HeapIndices[ v ] = i;
133  }
134 
136  {
137  if ( this->HeapSize == 0 )
138  {
139  return -1;
140  }
141 
142  int minv = this->Heap[ 1 ];
143  this->HeapIndices[ minv ] = -1;
144 
145  this->Heap[ 1 ] = this->Heap[ this->HeapSize ];
146  this->HeapIndices[ this->Heap[1] ]= 1;
147 
148  this->HeapSize--;
149  this->Heapify( 1 );
150 
151  return minv;
152  }
153 
154  void HeapDecreaseKey(const int& v)
155  {
156  // where in Heap is vertex v
157  int i = this->HeapIndices[ v ];
158  if ( i < 1 || i > static_cast<int>(this->HeapSize) )
159  {
160  return;
161  }
162 
163  while ( i > 1 &&
164  this->CumulativeWeights[ this->Heap[ i/2 ] ] >
165  this->CumulativeWeights[ v ] )
166  {
167  this->Heap[ i ] = this->Heap[i/2];
168  this->HeapIndices[ this->Heap[i] ] = i;
169  i /= 2;
170  }
171 
172  // Heap and HeapIndices are kinda inverses
173  this->Heap[ i ] = v;
174  this->HeapIndices[ v ] = i;
175  }
176 
177  void ResetHeap()
178  {
179  this->HeapSize = 0;
180  }
181 
182  void InitializeHeap(const int& size)
183  {
184  this->Heap.resize( size + 1 );
185  this->HeapIndices.resize( size );
186  }
187 
188 private:
189  unsigned int HeapSize;
190 
191  // The priority que (a binary heap) with vertex indices.
192  std::vector<int> Heap;
193 
194  // HeapIndices(v) the position of v in Heap (HeapIndices and Heap are kind of inverses).
195  std::vector<int> HeapIndices;
196 
197 };
198 
199 #endif
200 // VTK-HeaderTest-Exclude: vtkDijkstraGraphInternals.h
std::vector< unsigned char > BlockedVertices
std::vector< unsigned char > ClosedVertices
std::vector< double > CumulativeWeights
Helper class due to PIMPL excess.
std::vector< std::map< int, double > > Adjacency
void InitializeHeap(const int &size)
std::vector< unsigned char > OpenVertices