TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
DetourNode.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
3 //
4 // This software is provided 'as-is', without any express or implied
5 // warranty. In no event will the authors be held liable for any damages
6 // arising from the use of this software.
7 // Permission is granted to anyone to use this software for any purpose,
8 // including commercial applications, and to alter it and redistribute it
9 // freely, subject to the following restrictions:
10 // 1. The origin of this software must not be misrepresented; you must not
11 // claim that you wrote the original software. If you use this software
12 // in a product, an acknowledgment in the product documentation would be
13 // appreciated but is not required.
14 // 2. Altered source versions must be plainly marked as such, and must not be
15 // misrepresented as being the original software.
16 // 3. This notice may not be removed or altered from any source distribution.
17 //
18 
19 #ifndef DETOURNODE_H
20 #define DETOURNODE_H
21 
22 #include "DetourNavMesh.h"
23 
25 {
26  DT_NODE_OPEN = 0x01,
28  DT_NODE_PARENT_DETACHED = 0x04, // parent of the node is not adjacent. Found using raycast.
29 };
30 
31 typedef unsigned short dtNodeIndex;
32 static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
33 
34 struct dtNode
35 {
36  float pos[3];
37  float cost;
38  float total;
39  unsigned int pidx : 24;
40  unsigned int state : 2;
41  unsigned int flags : 3;
43 };
44 
45 
46 static const int DT_MAX_STATES_PER_NODE = 4; // number of extra states per node. See dtNode::state
47 
48 
49 
51 {
52 public:
53  dtNodePool(int maxNodes, int hashSize);
54  ~dtNodePool();
55  inline void operator=(const dtNodePool&) {}
56  void clear();
57 
58  // Get a dtNode by ref and extra state information. If there is none then - allocate
59  // There can be more than one node for the same polyRef but with different extra state information
60  dtNode* getNode(dtPolyRef id, unsigned char state=0);
61  dtNode* findNode(dtPolyRef id, unsigned char state);
62  unsigned int findNodes(dtPolyRef id, dtNode** nodes, const int maxNodes);
63 
64  inline unsigned int getNodeIdx(const dtNode* node) const
65  {
66  if (!node) return 0;
67  return (unsigned int)(node - m_nodes)+1;
68  }
69 
70  inline dtNode* getNodeAtIdx(unsigned int idx)
71  {
72  if (!idx) return 0;
73  return &m_nodes[idx-1];
74  }
75 
76  inline const dtNode* getNodeAtIdx(unsigned int idx) const
77  {
78  if (!idx) return 0;
79  return &m_nodes[idx-1];
80  }
81 
82  inline int getMemUsed() const
83  {
84  return sizeof(*this) +
85  sizeof(dtNode)*m_maxNodes +
86  sizeof(dtNodeIndex)*m_maxNodes +
87  sizeof(dtNodeIndex)*m_hashSize;
88  }
89 
90  inline int getMaxNodes() const { return m_maxNodes; }
91 
92  inline int getHashSize() const { return m_hashSize; }
93  inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
94  inline dtNodeIndex getNext(int i) const { return m_next[i]; }
95  inline int getNodeCount() const { return m_nodeCount; }
96 
97 private:
98 
102  const int m_maxNodes;
103  const int m_hashSize;
105 };
106 
108 {
109 public:
110  dtNodeQueue(int n);
111  ~dtNodeQueue();
112  inline void operator=(dtNodeQueue&) {}
113 
114  inline void clear()
115  {
116  m_size = 0;
117  }
118 
119  inline dtNode* top()
120  {
121  return m_heap[0];
122  }
123 
124  inline dtNode* pop()
125  {
126  dtNode* result = m_heap[0];
127  m_size--;
129  return result;
130  }
131 
132  inline void push(dtNode* node)
133  {
134  m_size++;
135  bubbleUp(m_size-1, node);
136  }
137 
138  inline void modify(dtNode* node)
139  {
140  for (int i = 0; i < m_size; ++i)
141  {
142  if (m_heap[i] == node)
143  {
144  bubbleUp(i, node);
145  return;
146  }
147  }
148  }
149 
150  inline bool empty() const { return m_size == 0; }
151 
152  inline int getMemUsed() const
153  {
154  return sizeof(*this) +
155  sizeof(dtNode*)*(m_capacity+1);
156  }
157 
158  inline int getCapacity() const { return m_capacity; }
159 
160 private:
161  void bubbleUp(int i, dtNode* node);
162  void trickleDown(int i, dtNode* node);
163 
165  const int m_capacity;
166  int m_size;
167 };
168 
169 
170 #endif // DETOURNODE_H
int m_nodeCount
Definition: DetourNode.h:104
bool empty() const
Definition: DetourNode.h:150
uint64_d dtPolyRef
Definition: DetourNavMesh.h:49
void clear()
Definition: DetourNode.h:114
const int m_capacity
Definition: DetourNode.h:165
Definition: DetourNode.h:34
~dtNodeQueue()
Definition: DetourNode.cpp:152
float pos[3]
Position of the node.
Definition: DetourNode.h:36
Definition: DetourNode.h:26
int getMemUsed() const
Definition: DetourNode.h:152
unsigned int getNodeIdx(const dtNode *node) const
Definition: DetourNode.h:64
const int m_maxNodes
Definition: DetourNode.h:102
Definition: DetourNode.h:107
dtNodeQueue(int n)
Definition: DetourNode.cpp:141
dtNode * pop()
Definition: DetourNode.h:124
int getCapacity() const
Definition: DetourNode.h:158
unsigned int findNodes(dtPolyRef id, dtNode **nodes, const int maxNodes)
Definition: DetourNode.cpp:74
const dtNode * getNodeAtIdx(unsigned int idx) const
Definition: DetourNode.h:76
unsigned int flags
Node flags. A combination of dtNodeFlags.
Definition: DetourNode.h:41
dtNode * findNode(dtPolyRef id, unsigned char state)
Definition: DetourNode.cpp:93
void operator=(dtNodeQueue &)
Definition: DetourNode.h:112
dtNodeIndex * m_first
Definition: DetourNode.h:100
dtPolyRef id
Polygon ref the node corresponds to.
Definition: DetourNode.h:42
int getHashSize() const
Definition: DetourNode.h:92
dtNodeFlags
Definition: DetourNode.h:24
dtNodePool(int maxNodes, int hashSize)
Definition: DetourNode.cpp:38
Definition: DetourNode.h:28
void operator=(const dtNodePool &)
Definition: DetourNode.h:55
~dtNodePool()
Definition: DetourNode.cpp:61
unsigned int pidx
Index to parent node.
Definition: DetourNode.h:39
int getMaxNodes() const
Definition: DetourNode.h:90
int getNodeCount() const
Definition: DetourNode.h:95
dtNodeIndex getNext(int i) const
Definition: DetourNode.h:94
dtNode * getNodeAtIdx(unsigned int idx)
Definition: DetourNode.h:70
static const dtNodeIndex DT_NULL_IDX
Definition: DetourNode.h:32
Definition: DetourNode.h:27
Definition: DetourNode.h:50
dtNode * getNode(dtPolyRef id, unsigned char state=0)
Definition: DetourNode.cpp:106
dtNodeIndex getFirst(int bucket) const
Definition: DetourNode.h:93
dtNode * top()
Definition: DetourNode.h:119
const int m_hashSize
Definition: DetourNode.h:103
static const int DT_MAX_STATES_PER_NODE
Definition: DetourNode.h:46
void trickleDown(int i, dtNode *node)
Definition: DetourNode.cpp:170
float cost
Cost from previous node to current node.
Definition: DetourNode.h:37
int m_size
Definition: DetourNode.h:166
dtNode * m_nodes
Definition: DetourNode.h:99
void modify(dtNode *node)
Definition: DetourNode.h:138
void bubbleUp(int i, dtNode *node)
Definition: DetourNode.cpp:157
dtNode ** m_heap
Definition: DetourNode.h:164
float total
Cost up to the node.
Definition: DetourNode.h:38
void clear()
Definition: DetourNode.cpp:68
unsigned short dtNodeIndex
Definition: DetourNode.h:31
int getMemUsed() const
Definition: DetourNode.h:82
dtNodeIndex * m_next
Definition: DetourNode.h:101
unsigned int state
extra state information. A polyRef can have multiple nodes with different extra info. see DT_MAX_STATES_PER_NODE
Definition: DetourNode.h:40
void push(dtNode *node)
Definition: DetourNode.h:132