Debian Codegraph @

Detour/Include/DetourNode.h

raw symbols SCIP
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
24 enum dtNodeFlags
25 {
26 DT_NODE_OPEN = 0x01,
27 DT_NODE_CLOSED = 0x02,
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]; ///< Position of the node.
39 float cost; ///< Cost from previous node to current node.
40 float total; ///< Cost up to the node.
41 unsigned int pidx : DT_NODE_PARENT_BITS; ///< Index to parent node.
42 unsigned int state : DT_NODE_STATE_BITS; ///< extra state information. A polyRef can have multiple nodes with different extra info. see DT_MAX_STATES_PER_NODE
43 unsigned int flags : 3; ///< Node flags. A combination of dtNodeFlags.
44 dtPolyRef id; ///< Polygon ref the node corresponds to.
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
49 class dtNodePool
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
108 class dtNodeQueue
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