Recast Navigation
Navigation-mesh Toolset for Games
DetourNode.h
Go to the documentation of this file.
1 //
2 // Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
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 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;
45 };
46 
47 static const int DT_MAX_STATES_PER_NODE = 1 << DT_NODE_STATE_BITS; // number of extra states per node. See dtNode::state
48 
50 {
51 public:
52  dtNodePool(int maxNodes, int hashSize);
53  ~dtNodePool();
54  void clear();
55 
56  // Get a dtNode by ref and extra state information. If there is none then - allocate
57  // There can be more than one node for the same polyRef but with different extra state information
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  // Explicitly disabled copy constructor and copy assignment operator.
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 
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  // Explicitly disabled copy constructor and copy assignment operator.
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 // DETOURNODE_H
static const int DT_MAX_STATES_PER_NODE
Definition: DetourNode.h:47
dtNodeFlags
Definition: DetourNode.h:25
@ DT_NODE_OPEN
Definition: DetourNode.h:26
@ DT_NODE_CLOSED
Definition: DetourNode.h:27
@ DT_NODE_PARENT_DETACHED
Definition: DetourNode.h:28
static const int DT_NODE_STATE_BITS
Definition: DetourNode.h:35
static const dtNodeIndex DT_NULL_IDX
Definition: DetourNode.h:32
static const int DT_NODE_PARENT_BITS
Definition: DetourNode.h:34
unsigned short dtNodeIndex
Definition: DetourNode.h:31
Definition: DetourNode.h:50
int getMemUsed() const
Definition: DetourNode.h:80
dtNode * findNode(dtPolyRef id, unsigned char state)
Definition: DetourNode.cpp:108
void clear()
Definition: DetourNode.cpp:83
unsigned int getNodeIdx(const dtNode *node) const
Definition: DetourNode.h:62
int getNodeCount() const
Definition: DetourNode.h:93
dtNode * getNodeAtIdx(unsigned int idx)
Definition: DetourNode.h:68
unsigned int findNodes(dtPolyRef id, dtNode **nodes, const int maxNodes)
Definition: DetourNode.cpp:89
dtNodePool(int maxNodes, int hashSize)
Definition: DetourNode.cpp:51
dtNodeIndex getFirst(int bucket) const
Definition: DetourNode.h:91
int getHashSize() const
Definition: DetourNode.h:90
dtNode * getNode(dtPolyRef id, unsigned char state=0)
Definition: DetourNode.cpp:121
~dtNodePool()
Definition: DetourNode.cpp:76
int getMaxNodes() const
Definition: DetourNode.h:88
dtNodeIndex getNext(int i) const
Definition: DetourNode.h:92
const dtNode * getNodeAtIdx(unsigned int idx) const
Definition: DetourNode.h:74
Definition: DetourNode.h:109
int getMemUsed() const
Definition: DetourNode.h:146
bool empty() const
Definition: DetourNode.h:144
int getCapacity() const
Definition: DetourNode.h:152
dtNode * pop()
Definition: DetourNode.h:118
void push(dtNode *node)
Definition: DetourNode.h:126
~dtNodeQueue()
Definition: DetourNode.cpp:167
void modify(dtNode *node)
Definition: DetourNode.h:132
dtNodeQueue(int n)
Definition: DetourNode.cpp:156
void clear()
Definition: DetourNode.h:114
dtNode * top()
Definition: DetourNode.h:116
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:48
Definition: DetourNode.h:37
dtPolyRef id
Polygon ref the node corresponds to.
Definition: DetourNode.h:44
float cost
Cost from previous node to current node.
Definition: DetourNode.h:39
float pos[3]
Position of the node.
Definition: DetourNode.h:38
unsigned int state
extra state information. A polyRef can have multiple nodes with different extra info....
Definition: DetourNode.h:42
unsigned int flags
Node flags. A combination of dtNodeFlags.
Definition: DetourNode.h:43
float total
Cost up to the node.
Definition: DetourNode.h:40
unsigned int pidx
Index to parent node.
Definition: DetourNode.h:41