| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#ifndef DETOURNODE_H |
| 20 |
#define DETOURNODE_H |
| 21 |
|
| 22 |
#include "DetourNavMesh.h" |
| 23 |
|
| 24 |
enum dtNodeFlags |
| 25 |
{ |
| 26 |
DT_NODE_OPEN = 0x01, |
| 27 |
DT_NODE_CLOSED = 0x02, |
| 28 |
DT_NODE_PARENT_DETACHED = 0x04 |
| 29 |
}; |
| 30 |
|
| 31 |
typedef unsigned short dtNodeIndex; |
| 32 |
static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0; |
| 33 |
|
| 34 |
static const int DT_NODE_PARENT_BITS = 24; |
| 35 |
static const int DT_NODE_STATE_BITS = 2; |
| 36 |
struct dtNode |
| 37 |
{ |
| 38 |
float pos[3]; |
| 39 |
float cost; |
| 40 |
float total; |
| 41 |
unsigned int pidx : DT_NODE_PARENT_BITS; |
| 42 |
unsigned int state : DT_NODE_STATE_BITS; |
| 43 |
unsigned int flags : 3; |
| 44 |
dtPolyRef id; |
| 45 |
}; |
| 46 |
|
| 47 |
static const int DT_MAX_STATES_PER_NODE = 1 << DT_NODE_STATE_BITS; |
| 48 |
|
| 49 |
class dtNodePool |
| 50 |
{ |
| 51 |
public: |
| 52 |
dtNodePool(int maxNodes, int hashSize); |
| 53 |
~dtNodePool(); |
| 54 |
void clear(); |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
dtNode* getNode(dtPolyRef id, unsigned char state=0); |
| 59 |
dtNode* findNode(dtPolyRef id, unsigned char state); |
| 60 |
unsigned int findNodes(dtPolyRef id, dtNode** nodes, const int maxNodes); |
| 61 |
|
| 62 |
inline unsigned int getNodeIdx(const dtNode* node) const |
| 63 |
{ |
| 64 |
if (!node) return 0; |
| 65 |
return (unsigned int)(node - m_nodes) + 1; |
| 66 |
} |
| 67 |
|
| 68 |
inline dtNode* getNodeAtIdx(unsigned int idx) |
| 69 |
{ |
| 70 |
if (!idx) return 0; |
| 71 |
return &m_nodes[idx - 1]; |
| 72 |
} |
| 73 |
|
| 74 |
inline const dtNode* getNodeAtIdx(unsigned int idx) const |
| 75 |
{ |
| 76 |
if (!idx) return 0; |
| 77 |
return &m_nodes[idx - 1]; |
| 78 |
} |
| 79 |
|
| 80 |
inline int getMemUsed() const |
| 81 |
{ |
| 82 |
return sizeof(*this) + |
| 83 |
sizeof(dtNode)*m_maxNodes + |
| 84 |
sizeof(dtNodeIndex)*m_maxNodes + |
| 85 |
sizeof(dtNodeIndex)*m_hashSize; |
| 86 |
} |
| 87 |
|
| 88 |
inline int getMaxNodes() const { return m_maxNodes; } |
| 89 |
|
| 90 |
inline int getHashSize() const { return m_hashSize; } |
| 91 |
inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; } |
| 92 |
inline dtNodeIndex getNext(int i) const { return m_next[i]; } |
| 93 |
inline int getNodeCount() const { return m_nodeCount; } |
| 94 |
|
| 95 |
private: |
| 96 |
|
| 97 |
dtNodePool(const dtNodePool&); |
| 98 |
dtNodePool& operator=(const dtNodePool&); |
| 99 |
|
| 100 |
dtNode* m_nodes; |
| 101 |
dtNodeIndex* m_first; |
| 102 |
dtNodeIndex* m_next; |
| 103 |
const int m_maxNodes; |
| 104 |
const int m_hashSize; |
| 105 |
int m_nodeCount; |
| 106 |
}; |
| 107 |
|
| 108 |
class dtNodeQueue |
| 109 |
{ |
| 110 |
public: |
| 111 |
dtNodeQueue(int n); |
| 112 |
~dtNodeQueue(); |
| 113 |
|
| 114 |
inline void clear() { m_size = 0; } |
| 115 |
|
| 116 |
inline dtNode* top() { return m_heap[0]; } |
| 117 |
|
| 118 |
inline dtNode* pop() |
| 119 |
{ |
| 120 |
dtNode* result = m_heap[0]; |
| 121 |
m_size--; |
| 122 |
trickleDown(0, m_heap[m_size]); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
inline void push(dtNode* node) |
| 127 |
{ |
| 128 |
m_size++; |
| 129 |
bubbleUp(m_size-1, node); |
| 130 |
} |
| 131 |
|
| 132 |
inline void modify(dtNode* node) |
| 133 |
{ |
| 134 |
for (int i = 0; i < m_size; ++i) |
| 135 |
{ |
| 136 |
if (m_heap[i] == node) |
| 137 |
{ |
| 138 |
bubbleUp(i, node); |
| 139 |
return; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
inline bool empty() const { return m_size == 0; } |
| 145 |
|
| 146 |
inline int getMemUsed() const |
| 147 |
{ |
| 148 |
return sizeof(*this) + |
| 149 |
sizeof(dtNode*) * (m_capacity + 1); |
| 150 |
} |
| 151 |
|
| 152 |
inline int getCapacity() const { return m_capacity; } |
| 153 |
|
| 154 |
private: |
| 155 |
|
| 156 |
dtNodeQueue(const dtNodeQueue&); |
| 157 |
dtNodeQueue& operator=(const dtNodeQueue&); |
| 158 |
|
| 159 |
void bubbleUp(int i, dtNode* node); |
| 160 |
void trickleDown(int i, dtNode* node); |
| 161 |
|
| 162 |
dtNode** m_heap; |
| 163 |
const int m_capacity; |
| 164 |
int m_size; |
| 165 |
}; |
| 166 |
|
| 167 |
|
| 168 |
#endif |