Recast Navigation  1.0.35
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  DT_NODE_GOAL = 0x04,
30 };
31 
32 typedef unsigned short dtNodeIndex;
33 static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
34 
35 struct dtNode
36 {
37  float pos[3];
38  float cost;
39  float total;
40  unsigned int pidx : 24;
41  unsigned int state : 2;
42  unsigned int flags : 3;
44 };
45 
46 
47 static const int DT_MAX_STATES_PER_NODE = 4; // number of extra states per node. See dtNode::state
48 
49 
50 
52 {
53 public:
54  dtNodePool(int maxNodes, int hashSize);
55  ~dtNodePool();
56  inline void operator=(const dtNodePool&) {}
57  void clear();
58 
59  // Get a dtNode by ref and extra state information. If there is none then - allocate
60  // There can be more than one node for the same polyRef but with different extra state information
61  dtNode* getNode(dtPolyRef id, unsigned char state=0);
62  dtNode* findNode(dtPolyRef id, unsigned char state);
63  unsigned int findNodes(dtPolyRef id, dtNode** nodes, const int maxNodes);
64 
65  inline unsigned int getNodeIdx(const dtNode* node) const
66  {
67  if (!node) return 0;
68  return (unsigned int)(node - m_nodes)+1;
69  }
70 
71  inline dtNode* getNodeAtIdx(unsigned int idx)
72  {
73  if (!idx) return 0;
74  return &m_nodes[idx-1];
75  }
76 
77  inline const dtNode* getNodeAtIdx(unsigned int idx) const
78  {
79  if (!idx) return 0;
80  return &m_nodes[idx-1];
81  }
82 
83  inline int getMemUsed() const
84  {
85  return sizeof(*this) +
86  sizeof(dtNode)*m_maxNodes +
87  sizeof(dtNodeIndex)*m_maxNodes +
88  sizeof(dtNodeIndex)*m_hashSize;
89  }
90 
91  inline int getMaxNodes() const { return m_maxNodes; }
92 
93  inline int getHashSize() const { return m_hashSize; }
94  inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
95  inline dtNodeIndex getNext(int i) const { return m_next[i]; }
96  inline int getNodeCount() const { return m_nodeCount; }
97 
98 private:
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  inline void operator=(dtNodeQueue&) {}
114 
115  inline void clear()
116  {
117  m_size = 0;
118  }
119 
120  inline dtNode* top()
121  {
122  return m_heap[0];
123  }
124 
125  inline dtNode* pop()
126  {
127  dtNode* result = m_heap[0];
128  m_size--;
129  trickleDown(0, m_heap[m_size]);
130  return result;
131  }
132 
133  inline void push(dtNode* node)
134  {
135  m_size++;
136  bubbleUp(m_size-1, node);
137  }
138 
139  inline void modify(dtNode* node)
140  {
141  for (int i = 0; i < m_size; ++i)
142  {
143  if (m_heap[i] == node)
144  {
145  bubbleUp(i, node);
146  return;
147  }
148  }
149  }
150 
151  inline bool empty() const { return m_size == 0; }
152 
153  inline int getMemUsed() const
154  {
155  return sizeof(*this) +
156  sizeof(dtNode*)*(m_capacity+1);
157  }
158 
159  inline int getCapacity() const { return m_capacity; }
160 
161 private:
162  void bubbleUp(int i, dtNode* node);
163  void trickleDown(int i, dtNode* node);
164 
165  dtNode** m_heap;
166  const int m_capacity;
167  int m_size;
168 };
169 
170 
171 #endif // DETOURNODE_H
bool empty() const
Definition: DetourNode.h:151
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:44
void clear()
Definition: DetourNode.h:115
Definition: DetourNode.h:35
~dtNodeQueue()
Definition: DetourNode.cpp:165
float pos[3]
Position of the node.
Definition: DetourNode.h:37
Definition: DetourNode.h:26
int getMemUsed() const
Definition: DetourNode.h:153
unsigned int getNodeIdx(const dtNode *node) const
Definition: DetourNode.h:65
Definition: DetourNode.h:108
dtNodeQueue(int n)
Definition: DetourNode.cpp:154
dtNode * pop()
Definition: DetourNode.h:125
int getCapacity() const
Definition: DetourNode.h:159
unsigned int findNodes(dtPolyRef id, dtNode **nodes, const int maxNodes)
Definition: DetourNode.cpp:87
const dtNode * getNodeAtIdx(unsigned int idx) const
Definition: DetourNode.h:77
unsigned int flags
Node flags. A combination of dtNodeFlags.
Definition: DetourNode.h:42
dtNode * findNode(dtPolyRef id, unsigned char state)
Definition: DetourNode.cpp:106
void operator=(dtNodeQueue &)
Definition: DetourNode.h:113
dtPolyRef id
Polygon ref the node corresponds to.
Definition: DetourNode.h:43
int getHashSize() const
Definition: DetourNode.h:93
dtNodeFlags
Definition: DetourNode.h:24
dtNodePool(int maxNodes, int hashSize)
Definition: DetourNode.cpp:51
Definition: DetourNode.h:28
void operator=(const dtNodePool &)
Definition: DetourNode.h:56
~dtNodePool()
Definition: DetourNode.cpp:74
unsigned int pidx
Index to parent node.
Definition: DetourNode.h:40
int getMaxNodes() const
Definition: DetourNode.h:91
int getNodeCount() const
Definition: DetourNode.h:96
dtNodeIndex getNext(int i) const
Definition: DetourNode.h:95
dtNode * getNodeAtIdx(unsigned int idx)
Definition: DetourNode.h:71
static const dtNodeIndex DT_NULL_IDX
Definition: DetourNode.h:33
Definition: DetourNode.h:27
Definition: DetourNode.h:51
dtNode * getNode(dtPolyRef id, unsigned char state=0)
Definition: DetourNode.cpp:119
dtNodeIndex getFirst(int bucket) const
Definition: DetourNode.h:94
dtNode * top()
Definition: DetourNode.h:120
static const int DT_MAX_STATES_PER_NODE
Definition: DetourNode.h:47
float cost
Cost from previous node to current node.
Definition: DetourNode.h:38
void modify(dtNode *node)
Definition: DetourNode.h:139
float total
Cost up to the node.
Definition: DetourNode.h:39
void clear()
Definition: DetourNode.cpp:81
unsigned short dtNodeIndex
Definition: DetourNode.h:32
Definition: DetourNode.h:29
int getMemUsed() const
Definition: DetourNode.h:83
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:41
void push(dtNode *node)
Definition: DetourNode.h:133