Planeshift
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 };
29 
30 typedef unsigned short dtNodeIndex;
31 static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
32 
33 struct dtNode
34 {
35  float pos[3];
36  float cost;
37  float total;
38  unsigned int pidx : 30;
39  unsigned int flags : 2;
41 };
42 
43 
45 {
46 public:
47  dtNodePool(int maxNodes, int hashSize);
48  ~dtNodePool();
49  inline void operator=(const dtNodePool&) {}
50  void clear();
51  dtNode* getNode(dtPolyRef id);
52  dtNode* findNode(dtPolyRef id);
53 
54  inline unsigned int getNodeIdx(const dtNode* node) const
55  {
56  if (!node) return 0;
57  return (unsigned int)(node - m_nodes)+1;
58  }
59 
60  inline dtNode* getNodeAtIdx(unsigned int idx)
61  {
62  if (!idx) return 0;
63  return &m_nodes[idx-1];
64  }
65 
66  inline const dtNode* getNodeAtIdx(unsigned int idx) const
67  {
68  if (!idx) return 0;
69  return &m_nodes[idx-1];
70  }
71 
72  inline int getMemUsed() const
73  {
74  return sizeof(*this) +
75  sizeof(dtNode)*m_maxNodes +
76  sizeof(dtNodeIndex)*m_maxNodes +
77  sizeof(dtNodeIndex)*m_hashSize;
78  }
79 
80  inline int getMaxNodes() const { return m_maxNodes; }
81 
82  inline int getHashSize() const { return m_hashSize; }
83  inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
84  inline dtNodeIndex getNext(int i) const { return m_next[i]; }
85 
86 private:
87 
88  dtNode* m_nodes;
89  dtNodeIndex* m_first;
90  dtNodeIndex* m_next;
91  const int m_maxNodes;
92  const int m_hashSize;
93  int m_nodeCount;
94 };
95 
97 {
98 public:
99  dtNodeQueue(int n);
100  ~dtNodeQueue();
101  inline void operator=(dtNodeQueue&) {}
102 
103  inline void clear()
104  {
105  m_size = 0;
106  }
107 
108  inline dtNode* top()
109  {
110  return m_heap[0];
111  }
112 
113  inline dtNode* pop()
114  {
115  dtNode* result = m_heap[0];
116  m_size--;
117  trickleDown(0, m_heap[m_size]);
118  return result;
119  }
120 
121  inline void push(dtNode* node)
122  {
123  m_size++;
124  bubbleUp(m_size-1, node);
125  }
126 
127  inline void modify(dtNode* node)
128  {
129  for (int i = 0; i < m_size; ++i)
130  {
131  if (m_heap[i] == node)
132  {
133  bubbleUp(i, node);
134  return;
135  }
136  }
137  }
138 
139  inline bool empty() const { return m_size == 0; }
140 
141  inline int getMemUsed() const
142  {
143  return sizeof(*this) +
144  sizeof(dtNode*)*(m_capacity+1);
145  }
146 
147  inline int getCapacity() const { return m_capacity; }
148 
149 private:
150  void bubbleUp(int i, dtNode* node);
151  void trickleDown(int i, dtNode* node);
152 
153  dtNode** m_heap;
154  const int m_capacity;
155  int m_size;
156 };
157 
158 
159 #endif // DETOURNODE_H
bool empty() const
Definition: DetourNode.h:139
unsigned int dtPolyRef
A handle to a polygon within a navigation mesh tile.
Definition: DetourNavMesh.h:30
void clear()
Definition: DetourNode.h:103
float pos[3]
Position of the node.
Definition: DetourNode.h:35
int getMemUsed() const
Definition: DetourNode.h:141
unsigned int getNodeIdx(const dtNode *node) const
Definition: DetourNode.h:54
dtNode * pop()
Definition: DetourNode.h:113
int getCapacity() const
Definition: DetourNode.h:147
const dtNode * getNodeAtIdx(unsigned int idx) const
Definition: DetourNode.h:66
unsigned int flags
Node flags 0/open/closed.
Definition: DetourNode.h:39
void operator=(dtNodeQueue &)
Definition: DetourNode.h:101
dtPolyRef id
Polygon ref the node corresponds to.
Definition: DetourNode.h:40
int getHashSize() const
Definition: DetourNode.h:82
dtNodeFlags
Definition: DetourNode.h:24
void operator=(const dtNodePool &)
Definition: DetourNode.h:49
unsigned int pidx
Index to parent node.
Definition: DetourNode.h:38
int getMaxNodes() const
Definition: DetourNode.h:80
dtNodeIndex getNext(int i) const
Definition: DetourNode.h:84
dtNode * getNodeAtIdx(unsigned int idx)
Definition: DetourNode.h:60
static const dtNodeIndex DT_NULL_IDX
Definition: DetourNode.h:31
dtNodeIndex getFirst(int bucket) const
Definition: DetourNode.h:83
dtNode * top()
Definition: DetourNode.h:108
float cost
Cost from previous node to current node.
Definition: DetourNode.h:36
void modify(dtNode *node)
Definition: DetourNode.h:127
float total
Cost up to the node.
Definition: DetourNode.h:37
unsigned short dtNodeIndex
Definition: DetourNode.h:30
int getMemUsed() const
Definition: DetourNode.h:72
void push(dtNode *node)
Definition: DetourNode.h:121