| 1 | // heap.h |
| 2 | /**************************************************************************/ |
| 3 | /* This class implements a binary min heap data structure to support the */ |
| 4 | /* fast marching algorithm. A min heap is a list which has the property */ |
| 5 | /* that the smallest element is always the first element. */ |
| 6 | /* */ |
| 7 | /* The fast marching method uses this data structure to track elements in */ |
| 8 | /* the solution narrow band. The fast marching method needs to know which */ |
| 9 | /* element in the narrow band is nearest the zero level-set at each */ |
| 10 | /* iteration. */ |
| 11 | /* */ |
| 12 | /* When a new point enters the solution narrow band the element is added */ |
| 13 | /* to the heap. */ |
| 14 | /* */ |
| 15 | /* New elements are added to the heap with the push() method. The address */ |
| 16 | /* passed to pop is the (flat) address of the element in the grid and the */ |
| 17 | /* value passed is the distance. The push() method returns an integer (a */ |
| 18 | /* heap index) which is used later to refer to the element. */ |
| 19 | /* */ |
| 20 | /* As the solution evolves the distance of points already in the heap can */ |
| 21 | /* be updated via the set() method. The set() method takes the heap index */ |
| 22 | /* returned by the push() along with a new distance value. */ |
| 23 | /* */ |
| 24 | /* The narrow band element nearest the zero-level set is taken off the */ |
| 25 | /* heap with the pop() method. The grid address of the top element is */ |
| 26 | /* returned to the caller. */ |
| 27 | /* */ |
| 28 | /* The constructor for heap needs to know the number of elements that */ |
| 29 | /* will enter the narrow band. See heap.cpp for implementation details. */ |
| 30 | /**************************************************************************/ |
| 31 | |
| 32 | class heap |
| 33 | { |
| 34 | public: |
| 35 | heap(int depth, bool selfTest=false); |
| 36 | virtual ~heap(); |
| 37 | int push(int address, double value); |
| 38 | void pop(int *address, double *value); |
| 39 | void set(int index, double value); |
| 40 | bool empty() const; |
| 41 | |
| 42 | // return the distance of the top element on the heap |
| 43 | const double &peek() const; |
| 44 | |
| 45 | private: |
| 46 | //void print() const; |
| 47 | void test() const; |
| 48 | inline void _siftUp(int pos); |
| 49 | inline void _siftDown(int startPos, int pos); |
| 50 | |
| 51 | int maxLength_; |
| 52 | int listLength_; |
| 53 | int heapLength_; |
| 54 | |
| 55 | double * distance_; |
| 56 | int * heap_; |
| 57 | int * address_; |
| 58 | int * backPointer_; |
| 59 | bool selfTest_; |
| 60 | }; |