VMTK
Loading...
Searching...
No Matches
vtkvmtkMinHeap.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3Program: VMTK
4Module: $RCSfile: vtkvmtkMinHeap.cxx,v $
5Language: C++
6Date: $Date: 2006/04/06 16:46:43 $
7Version: $Revision: 1.3 $
8
9 Copyright (c) Luca Antiga, David Steinman. All rights reserved.
10 See LICENSE file for details.
11
12 Portions of this code are covered under the VTK copyright.
13 See VTKCopyright.txt or http://www.kitware.com/VTKCopyright.htm
14 for details.
15
16 This software is distributed WITHOUT ANY WARRANTY; without even
17 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
18 PURPOSE. See the above copyright notices for more information.
19
20=========================================================================*/
21
22#include "vtkvmtkMinHeap.h"
23#include "vtkDoubleArray.h"
24#include "vtkIdList.h"
25#include "vtkObjectFactory.h"
26#include "vtkvmtkConstants.h"
27
28
30
32{
33 this->MinHeapScalars = NULL;
34 this->Heap = vtkIdList::New();
35 this->BackPointers = vtkIdList::New();
36}
37
39{
40 if (this->MinHeapScalars)
41 {
42 this->MinHeapScalars->Delete();
43 this->MinHeapScalars = NULL;
44 }
45
46 this->Heap->Delete();
47 this->BackPointers->Delete();
48}
49
51{
52 vtkIdType i;
53
54 if (this->MinHeapScalars == NULL)
55 {
56 vtkErrorMacro(<< "No HeapScalars.");
57 return;
58 }
59
60 this->Heap->Initialize();
61 this->BackPointers->Initialize();
62 vtkIdType numberOfScalars;
63 numberOfScalars = this->MinHeapScalars->GetNumberOfTuples();
64 this->BackPointers->SetNumberOfIds(numberOfScalars);
65 for (i=0; i<numberOfScalars; i++)
66 {
67 this->BackPointers->SetId(i,-1);
68 }
69}
70
72{
73 if (this->MinHeapScalars == NULL)
74 {
75 vtkErrorMacro(<< "No MinHeapScalars.");
76 return;
77 }
78
79 vtkIdType numberOfScalars;
80 numberOfScalars = this->MinHeapScalars->GetNumberOfTuples();
81
82 if ((id<0)||(id>=numberOfScalars))
83 {
84 vtkErrorMacro(<< "Id inserted exceeds MinHeapScalars dimension.");
85 return;
86 }
87
88 vtkIdType currentLoc;
89
90 currentLoc = this->Heap->InsertNextId(id);
91 this->BackPointers->InsertId(id,currentLoc);
92
93 this->SiftUp(currentLoc);
94
95 while (this->BackPointers->GetNumberOfIds() < numberOfScalars)
96 {
97 this->BackPointers->InsertNextId(-1);
98 }
99
100 if (this->BackPointers->GetNumberOfIds() < numberOfScalars)
101 {
102 this->BackPointers->SetNumberOfIds(numberOfScalars);
103 }
104}
105
107{
108 return this->Heap->GetNumberOfIds();
109}
110
111void vtkvmtkMinHeap::UpdateId(vtkIdType id)
112{
113 if (this->MinHeapScalars == NULL)
114 {
115 vtkErrorMacro(<<"No MinHeapScalars.");
116 return;
117 }
118
119 if ((id<0)||(id>=this->MinHeapScalars->GetNumberOfTuples()))
120 {
121 vtkErrorMacro(<<"Id inserted exceeds MinHeapScalars dimension.");
122 return;
123 }
124
125 this->SiftUp(this->BackPointers->GetId(id));
126
127}
128
129int vtkvmtkMinHeap::IsLeaf(vtkIdType loc)
130{
131 vtkIdType heapSize;
132 heapSize = this->Heap->GetNumberOfIds();
133
134 if ((loc<0)||(loc>=heapSize))
135 {
136 vtkErrorMacro(<<"Heap smaller than requested location.");
137 return -1;
138 }
139
140 if ((loc >= heapSize/2)&&(loc < heapSize))
141 return 1;
142
143 return 0;
144}
145
146vtkIdType vtkvmtkMinHeap::GetLeftChild(vtkIdType loc)
147{
148 vtkIdType heapSize;
149 heapSize = this->Heap->GetNumberOfIds();
150
151 if ((loc<0)||(loc>=heapSize))
152 {
153 vtkErrorMacro(<<"Heap smaller than requested location.");
154 return -1;
155 }
156
157 if (loc >= heapSize/2)
158 return -1;
159
160 return 2*loc + 1;
161}
162
163vtkIdType vtkvmtkMinHeap::GetRightChild(vtkIdType loc)
164{
165 vtkIdType heapSize;
166 heapSize = this->Heap->GetNumberOfIds();
167
168 if ((loc<0)||(loc>=heapSize))
169 {
170 vtkErrorMacro(<< "Heap smaller than requested location.");
171 return -1;
172 }
173
174 if (loc >= (heapSize-1)/2)
175 return -1;
176
177 return 2*loc + 2;
178}
179
180vtkIdType vtkvmtkMinHeap::GetParent(vtkIdType loc)
181{
182 if (loc<0)
183 {
184 vtkErrorMacro(<<"Negative location requested.");
185 return -1;
186 }
187
188 if (loc==0)
189 return -1;
190
191 return (loc-1)/2;
192}
193
194void vtkvmtkMinHeap::SiftUp(vtkIdType loc)
195{
196 if (this->MinHeapScalars == NULL)
197 {
198 vtkErrorMacro(<<"No MinHeapScalars.");
199 return;
200 }
201
202 vtkIdType heapSize;
203 heapSize = this->Heap->GetNumberOfIds();
204
205 if ((loc<0)||(loc>=heapSize))
206 {
207 vtkErrorMacro(<<"Heap smaller than requested location.");
208 return;
209 }
210
211 vtkIdType parentLoc;
212
213 while (loc>0)
214 {
215 parentLoc = this->GetParent(loc);
216
217 if (this->MinHeapScalars->GetValue(this->Heap->GetId(loc)) - this->MinHeapScalars->GetValue(this->Heap->GetId(parentLoc)) > VTK_VMTK_DOUBLE_TOL)
218 {
219 return;
220 }
221
222 this->Swap(loc,parentLoc);
223 loc = parentLoc;
224 }
225}
226
227void vtkvmtkMinHeap::SiftDown(vtkIdType loc)
228{
229 if (this->MinHeapScalars == NULL)
230 {
231 vtkErrorMacro(<<"No MinHeapScalars.");
232 return;
233 }
234
235 vtkIdType heapSize;
236 heapSize = this->Heap->GetNumberOfIds();
237
238 if ((loc<0)||(loc>=heapSize))
239 {
240 vtkErrorMacro(<<"Heap smaller than requested location.");
241 return;
242 }
243
244 vtkIdType minChildLoc, leftChildLoc, rightChildLoc;
245
246 while (!this->IsLeaf(loc))
247 {
248 leftChildLoc = this->GetLeftChild(loc);
249 rightChildLoc = leftChildLoc + 1;
250
251 minChildLoc = leftChildLoc;
252 if ((leftChildLoc<this->Heap->GetNumberOfIds()-1) &&
253 (this->MinHeapScalars->GetValue(this->Heap->GetId(leftChildLoc)) - this->MinHeapScalars->GetValue(this->Heap->GetId(rightChildLoc))) > VTK_VMTK_DOUBLE_TOL)
254 {
255 minChildLoc = rightChildLoc;
256 }
257
258 if (this->MinHeapScalars->GetValue(this->Heap->GetId(loc)) - this->MinHeapScalars->GetValue(this->Heap->GetId(minChildLoc)) < -VTK_VMTK_DOUBLE_TOL)
259 {
260 return;
261 }
262
263 this->Swap(loc,minChildLoc);
264
265 loc = minChildLoc;
266 }
267}
268
270{
271 vtkIdType minId, lastElementLoc;
272
273 minId = this->Heap->GetId(0);
274 lastElementLoc = this->Heap->GetNumberOfIds()-1;
275 this->Swap(0,lastElementLoc);
276 this->Heap->SetNumberOfIds(lastElementLoc);
277 this->BackPointers->SetId(minId,-1);
278
279 if (this->Heap->GetNumberOfIds()>0)
280 {
281 this->SiftDown(0);
282 }
283
284 return minId;
285}
286
288{
289 vtkIdType minId;
290
291 minId = this->Heap->GetId(0);
292
293 return minId;
294}
295
296vtkIdType vtkvmtkMinHeap::RemoveAt(vtkIdType loc)
297{
298 vtkIdType heapSize;
299 heapSize = this->Heap->GetNumberOfIds();
300
301 if ((loc<0)||(loc>=heapSize))
302 {
303 vtkErrorMacro(<<"Heap smaller than requested location.");
304 return -1;
305 }
306
307 vtkIdType locId, lastElementLoc;
308
309 locId = this->Heap->GetId(loc);
310 lastElementLoc = heapSize-1;
311 this->Swap(locId,lastElementLoc);
312 this->Heap->SetNumberOfIds(lastElementLoc);
313 this->BackPointers->SetId(locId,-1);
314
315 if (this->Heap->GetNumberOfIds()>0)
316 this->SiftDown(loc);
317
318 return locId;
319}
320
321void vtkvmtkMinHeap::Swap(vtkIdType loc0, vtkIdType loc1)
322{
323 vtkIdType temp;
324
325 vtkIdType heapSize;
326 heapSize = this->Heap->GetNumberOfIds();
327
328 if ((heapSize<loc0)||(heapSize<loc1))
329 {
330 vtkErrorMacro(<<"Heap smaller than requested location.");
331 return;
332 }
333
334 temp = this->Heap->GetId(loc0);
335 this->Heap->SetId(loc0,this->Heap->GetId(loc1));
336 this->Heap->SetId(loc1,temp);
337
338 this->BackPointers->SetId(this->Heap->GetId(loc0),loc0);
339 this->BackPointers->SetId(this->Heap->GetId(loc1),loc1);
340}
341
342void vtkvmtkMinHeap::PrintSelf(std::ostream& os, vtkIndent indent)
343{
344 this->Superclass::PrintSelf(os,indent);
345}
Implements the min-heap data structure.
void Swap(vtkIdType loc0, vtkIdType loc1)
vtkIdList * BackPointers
void SiftDown(vtkIdType loc)
void PrintSelf(std::ostream &os, vtkIndent indent) override
vtkIdType RemoveMin()
int IsLeaf(vtkIdType loc)
vtkDoubleArray * MinHeapScalars
vtkIdType GetRightChild(vtkIdType loc)
vtkIdType GetLeftChild(vtkIdType loc)
vtkIdType RemoveAt(vtkIdType loc)
vtkIdList * Heap
void SiftUp(vtkIdType loc)
vtkIdType GetParent(vtkIdType loc)
void UpdateId(vtkIdType id)
void InsertNextId(vtkIdType id)
#define VTK_VMTK_DOUBLE_TOL
vtkStandardNewMacro(vtkvmtkMinHeap)