Atoms Crowd  7.0.0
DetourNode.h
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 <AtomsUtils/Globals.h>
23 #include <AtomsUtils/NavigationMesh/Detour/DetourNavMesh.h>
24 
25 namespace AtomsUtils {
26 
27 enum dtNodeFlags
28 {
29  DT_NODE_OPEN = 0x01,
30  DT_NODE_CLOSED = 0x02,
31 };
32 
33 typedef unsigned short dtNodeIndex;
34 static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
35 
36 struct dtNode
37 {
38  float pos[3];
39  float cost;
40  float total;
41  unsigned int pidx : 30;
42  unsigned int flags : 2;
44 };
45 
46 
48 {
49 public:
50  dtNodePool(int maxNodes, int hashSize);
51  ~dtNodePool();
52  inline void operator=(const dtNodePool&) {}
53  void clear();
54  dtNode* getNode(dtPolyRef id);
55  dtNode* findNode(dtPolyRef id);
56 
57  inline unsigned int getNodeIdx(const dtNode* node) const
58  {
59  if (!node) return 0;
60  return (unsigned int)(node - m_nodes)+1;
61  }
62 
63  inline dtNode* getNodeAtIdx(unsigned int idx)
64  {
65  if (!idx) return 0;
66  return &m_nodes[idx-1];
67  }
68 
69  inline const dtNode* getNodeAtIdx(unsigned int idx) const
70  {
71  if (!idx) return 0;
72  return &m_nodes[idx-1];
73  }
74 
75  inline int getMemUsed() const
76  {
77  return sizeof(*this) +
78  sizeof(dtNode)*m_maxNodes +
79  sizeof(dtNodeIndex)*m_maxNodes +
80  sizeof(dtNodeIndex)*m_hashSize;
81  }
82 
83  inline int getMaxNodes() const { return m_maxNodes; }
84 
85  // If using a shared query instance it's possible that m_maxNodes is greater
86  // than pool size requested by callee. There's no point in reallocating the
87  // pool so we artificially limit the number of available nodes
88  inline int getMaxRuntimeNodes() const { return m_maxRuntimeNodes; }
89 
90  inline int getNodeCount() const { return m_nodeCount; }
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 
96 
97  // overrides m_maxNodes for runtime purposes
98  inline void setMaxRuntimeNodes(const int newMaxRuntimeNodes) { m_maxRuntimeNodes = newMaxRuntimeNodes; }
99 
100 
101 private:
102 
103  dtNode* m_nodes;
104  dtNodeIndex* m_first;
105  dtNodeIndex* m_next;
106  const int m_maxNodes;
107  const int m_hashSize;
108 
109  int m_maxRuntimeNodes;
110 
111  int m_nodeCount;
112 };
113 
115 {
116 public:
117  dtNodeQueue(int n);
118  ~dtNodeQueue();
119  inline void operator=(dtNodeQueue&) {}
120 
121  inline void clear()
122  {
123  m_size = 0;
124  }
125 
126  inline dtNode* top()
127  {
128  return m_heap[0];
129  }
130 
131  inline dtNode* pop()
132  {
133  dtNode* result = m_heap[0];
134  m_size--;
135  trickleDown(0, m_heap[m_size]);
136  return result;
137  }
138 
139  inline void push(dtNode* node)
140  {
141  m_size++;
142  bubbleUp(m_size-1, node);
143  }
144 
145  inline void modify(dtNode* node)
146  {
147  for (int i = 0; i < m_size; ++i)
148  {
149  if (m_heap[i] == node)
150  {
151  bubbleUp(i, node);
152  return;
153  }
154  }
155  }
156 
157  inline bool empty() const { return m_size == 0; }
158 
159  inline int getMemUsed() const
160  {
161  return sizeof(*this) +
162  sizeof(dtNode*)*(m_capacity+1);
163  }
164 
165  inline int getCapacity() const { return m_capacity; }
166 
167 private:
168  void bubbleUp(int i, dtNode* node);
169  void trickleDown(int i, dtNode* node);
170 
171  dtNode** m_heap;
172  const int m_capacity;
173  int m_size;
174 };
175 
176 }
177 
178 #endif // DETOURNODE_H
Definition: DetourNode.h:48
Definition: DetourNode.h:115
Type_uint64 dtPolyRef
Definition: DetourNavMesh.h:47
AtomsCore namespace.
Definition: Base64.h:13
Definition: DetourNode.h:37
float cost
Cost from previous node to current node.
Definition: DetourNode.h:39
float total
Cost up to the node.
Definition: DetourNode.h:40
unsigned int flags
Node flags 0/open/closed.
Definition: DetourNode.h:42
float pos[3]
Position of the node.
Definition: DetourNode.h:38
dtPolyRef id
Polygon ref the node corresponds to.
Definition: DetourNode.h:43
unsigned int pidx
Index to parent node.
Definition: DetourNode.h:41