VMTK
Loading...
Searching...
No Matches
vtkvmtkMinHeap Class Reference

Implements the min-heap data structure. More...

#include <vtkvmtkMinHeap.h>

Inheritance diagram for vtkvmtkMinHeap:
[legend]
Collaboration diagram for vtkvmtkMinHeap:
[legend]

Public Types

typedef vtkObject Superclass

Public Member Functions

virtual int IsA (const char *type)
vtkvmtkMinHeap * NewInstance () const
void PrintSelf (std::ostream &os, vtkIndent indent) override
void Initialize ()
int GetSize ()
void InsertNextId (vtkIdType id)
void UpdateId (vtkIdType id)
vtkIdType GetMin ()
vtkIdType RemoveMin ()
virtual void SetMinHeapScalars (vtkDoubleArray *)
virtual vtkDoubleArray * GetMinHeapScalars ()

Static Public Member Functions

static int IsTypeOf (const char *type)
static vtkvmtkMinHeap * SafeDownCast (vtkObjectBase *o)
static vtkvmtkMinHeap * New ()

Protected Member Functions

virtual vtkObjectBase * NewInstanceInternal () const
 vtkvmtkMinHeap ()
 ~vtkvmtkMinHeap ()
void Swap (vtkIdType loc0, vtkIdType loc1)
int IsLeaf (vtkIdType loc)
vtkIdType GetLeftChild (vtkIdType loc)
vtkIdType GetRightChild (vtkIdType loc)
vtkIdType GetParent (vtkIdType loc)
void SiftUp (vtkIdType loc)
void SiftDown (vtkIdType loc)
vtkIdType RemoveAt (vtkIdType loc)

Protected Attributes

vtkIdList * Heap
vtkIdList * BackPointers
vtkDoubleArray * MinHeapScalars

Detailed Description

Implements the min-heap data structure.

This class is an implementation of the min heap data structure, used to handle a set of values in such a way that the retrieval of the minimum element takes constant time. A min heap is a complete binary tree where the value at each node is equal or less than the value at its children, and it is represented as an array where the children of a node stored at location k are at location 2k and 2k+1 (so that the parent of k is located at k/2). Keeping the min heap ordered after a value is updated or an id is inserted in the heap takes O(log N).

In the present implementation, values are provided in a vtkDoubleArray, and element ids are inserted in the heap. Backpointers are used to access the heap by id. This class is optimized for working in conjunction with vtkNonManifoldFastMarching.

For more insight see J.A. Sethian, Level Set Methods and Fast Marching Methods, Cambridge University Press, 2nd Edition, 1999.

Warning
Be sure to call Initialize() after defining MinHeapScalars.
See also
vtkNonManifoldFastMarching

Definition at line 43 of file vtkvmtkMinHeap.h.

Member Typedef Documentation

◆ Superclass

typedef vtkObject vtkvmtkMinHeap::Superclass

Definition at line 46 of file vtkvmtkMinHeap.h.

Constructor & Destructor Documentation

◆ vtkvmtkMinHeap()

vtkvmtkMinHeap::vtkvmtkMinHeap ( )
protected

Definition at line 31 of file vtkvmtkMinHeap.cxx.

32{
33 this->MinHeapScalars = NULL;
34 this->Heap = vtkIdList::New();
35 this->BackPointers = vtkIdList::New();
36}
vtkIdList * BackPointers
vtkDoubleArray * MinHeapScalars
vtkIdList * Heap

◆ ~vtkvmtkMinHeap()

vtkvmtkMinHeap::~vtkvmtkMinHeap ( )
protected

Definition at line 38 of file vtkvmtkMinHeap.cxx.

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}

Member Function Documentation

◆ IsTypeOf()

int vtkvmtkMinHeap::IsTypeOf ( const char * type)
static

◆ IsA()

virtual int vtkvmtkMinHeap::IsA ( const char * type)
virtual

◆ SafeDownCast()

vtkvmtkMinHeap * vtkvmtkMinHeap::SafeDownCast ( vtkObjectBase * o)
static

◆ NewInstanceInternal()

virtual vtkObjectBase * vtkvmtkMinHeap::NewInstanceInternal ( ) const
protectedvirtual

◆ NewInstance()

vtkvmtkMinHeap * vtkvmtkMinHeap::NewInstance ( ) const

◆ PrintSelf()

void vtkvmtkMinHeap::PrintSelf ( std::ostream & os,
vtkIndent indent )
override

Definition at line 342 of file vtkvmtkMinHeap.cxx.

343{
344 this->Superclass::PrintSelf(os,indent);
345}

◆ New()

vtkvmtkMinHeap * vtkvmtkMinHeap::New ( )
static

◆ Initialize()

void vtkvmtkMinHeap::Initialize ( )

Initializes the heap to an empty state and prepares back pointers. Calls this method before using the min heap once MinHeapScalars have been defined.

Definition at line 50 of file vtkvmtkMinHeap.cxx.

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}

◆ SetMinHeapScalars()

virtual void vtkvmtkMinHeap::SetMinHeapScalars ( vtkDoubleArray * )
virtual

Set/Get the array containing the values indexed by the min heap.

◆ GetMinHeapScalars()

virtual vtkDoubleArray * vtkvmtkMinHeap::GetMinHeapScalars ( )
virtual

Set/Get the array containing the values indexed by the min heap.

◆ GetSize()

int vtkvmtkMinHeap::GetSize ( )

Get heap size.

Definition at line 106 of file vtkvmtkMinHeap.cxx.

107{
108 return this->Heap->GetNumberOfIds();
109}

◆ InsertNextId()

void vtkvmtkMinHeap::InsertNextId ( vtkIdType id)

Insert an index to a value in HeapScalars in the min heap.

Definition at line 71 of file vtkvmtkMinHeap.cxx.

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}
void SiftUp(vtkIdType loc)

◆ UpdateId()

void vtkvmtkMinHeap::UpdateId ( vtkIdType id)

Tells the min heap that the value indexed by id has changed in MinHeapScalars array.

Definition at line 111 of file vtkvmtkMinHeap.cxx.

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}

◆ GetMin()

vtkIdType vtkvmtkMinHeap::GetMin ( )

Gets the id of the minimum value in the min heap.

Definition at line 287 of file vtkvmtkMinHeap.cxx.

288{
289 vtkIdType minId;
290
291 minId = this->Heap->GetId(0);
292
293 return minId;
294}

◆ RemoveMin()

vtkIdType vtkvmtkMinHeap::RemoveMin ( )

Gets the id of the minimum value in the min heap and removes it from the min heap.

Definition at line 269 of file vtkvmtkMinHeap.cxx.

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}
void Swap(vtkIdType loc0, vtkIdType loc1)
void SiftDown(vtkIdType loc)

◆ Swap()

void vtkvmtkMinHeap::Swap ( vtkIdType loc0,
vtkIdType loc1 )
protected

Definition at line 321 of file vtkvmtkMinHeap.cxx.

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}

◆ IsLeaf()

int vtkvmtkMinHeap::IsLeaf ( vtkIdType loc)
protected

Definition at line 129 of file vtkvmtkMinHeap.cxx.

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}

◆ GetLeftChild()

vtkIdType vtkvmtkMinHeap::GetLeftChild ( vtkIdType loc)
protected

Definition at line 146 of file vtkvmtkMinHeap.cxx.

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}

◆ GetRightChild()

vtkIdType vtkvmtkMinHeap::GetRightChild ( vtkIdType loc)
protected

Definition at line 163 of file vtkvmtkMinHeap.cxx.

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}

◆ GetParent()

vtkIdType vtkvmtkMinHeap::GetParent ( vtkIdType loc)
protected

Definition at line 180 of file vtkvmtkMinHeap.cxx.

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}

◆ SiftUp()

void vtkvmtkMinHeap::SiftUp ( vtkIdType loc)
protected

Definition at line 194 of file vtkvmtkMinHeap.cxx.

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}
vtkIdType GetParent(vtkIdType loc)
#define VTK_VMTK_DOUBLE_TOL

◆ SiftDown()

void vtkvmtkMinHeap::SiftDown ( vtkIdType loc)
protected

Definition at line 227 of file vtkvmtkMinHeap.cxx.

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}
int IsLeaf(vtkIdType loc)
vtkIdType GetLeftChild(vtkIdType loc)

◆ RemoveAt()

vtkIdType vtkvmtkMinHeap::RemoveAt ( vtkIdType loc)
protected

Definition at line 296 of file vtkvmtkMinHeap.cxx.

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}

Member Data Documentation

◆ Heap

vtkIdList* vtkvmtkMinHeap::Heap
protected

Definition at line 103 of file vtkvmtkMinHeap.h.

◆ BackPointers

vtkIdList* vtkvmtkMinHeap::BackPointers
protected

Definition at line 104 of file vtkvmtkMinHeap.h.

◆ MinHeapScalars

vtkDoubleArray* vtkvmtkMinHeap::MinHeapScalars
protected

Definition at line 106 of file vtkvmtkMinHeap.h.


The documentation for this class was generated from the following files: