Planeshift

DetourNode.h

Go to the documentation of this file.
00001 //
00002 // Copyright (c) 2009-2010 Mikko Mononen [email protected]
00003 //
00004 // This software is provided 'as-is', without any express or implied
00005 // warranty.  In no event will the authors be held liable for any damages
00006 // arising from the use of this software.
00007 // Permission is granted to anyone to use this software for any purpose,
00008 // including commercial applications, and to alter it and redistribute it
00009 // freely, subject to the following restrictions:
00010 // 1. The origin of this software must not be misrepresented; you must not
00011 //    claim that you wrote the original software. If you use this software
00012 //    in a product, an acknowledgment in the product documentation would be
00013 //    appreciated but is not required.
00014 // 2. Altered source versions must be plainly marked as such, and must not be
00015 //    misrepresented as being the original software.
00016 // 3. This notice may not be removed or altered from any source distribution.
00017 //
00018 
00019 #ifndef DETOURNODE_H
00020 #define DETOURNODE_H
00021 
00022 #include "DetourNavMesh.h"
00023 
00024 enum dtNodeFlags
00025 {
00026         DT_NODE_OPEN = 0x01,
00027         DT_NODE_CLOSED = 0x02,
00028 };
00029 
00030 typedef unsigned short dtNodeIndex;
00031 static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
00032 
00033 struct dtNode
00034 {
00035         float pos[3];                           
00036         float cost;                                     
00037         float total;                            
00038         unsigned int pidx : 30;         
00039         unsigned int flags : 2;         
00040         dtPolyRef id;                           
00041 };
00042 
00043 
00044 class dtNodePool
00045 {
00046 public:
00047         dtNodePool(int maxNodes, int hashSize);
00048         ~dtNodePool();
00049         inline void operator=(const dtNodePool&) {}
00050         void clear();
00051         dtNode* getNode(dtPolyRef id);
00052         dtNode* findNode(dtPolyRef id);
00053 
00054         inline unsigned int getNodeIdx(const dtNode* node) const
00055         {
00056                 if (!node) return 0;
00057                 return (unsigned int)(node - m_nodes)+1;
00058         }
00059 
00060         inline dtNode* getNodeAtIdx(unsigned int idx)
00061         {
00062                 if (!idx) return 0;
00063                 return &m_nodes[idx-1];
00064         }
00065 
00066         inline const dtNode* getNodeAtIdx(unsigned int idx) const
00067         {
00068                 if (!idx) return 0;
00069                 return &m_nodes[idx-1];
00070         }
00071         
00072         inline int getMemUsed() const
00073         {
00074                 return sizeof(*this) +
00075                         sizeof(dtNode)*m_maxNodes +
00076                         sizeof(dtNodeIndex)*m_maxNodes +
00077                         sizeof(dtNodeIndex)*m_hashSize;
00078         }
00079         
00080         inline int getMaxNodes() const { return m_maxNodes; }
00081         
00082         inline int getHashSize() const { return m_hashSize; }
00083         inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
00084         inline dtNodeIndex getNext(int i) const { return m_next[i]; }
00085         
00086 private:
00087         
00088         dtNode* m_nodes;
00089         dtNodeIndex* m_first;
00090         dtNodeIndex* m_next;
00091         const int m_maxNodes;
00092         const int m_hashSize;
00093         int m_nodeCount;
00094 };
00095 
00096 class dtNodeQueue
00097 {
00098 public:
00099         dtNodeQueue(int n);
00100         ~dtNodeQueue();
00101         inline void operator=(dtNodeQueue&) {}
00102         
00103         inline void clear()
00104         {
00105                 m_size = 0;
00106         }
00107         
00108         inline dtNode* top()
00109         {
00110                 return m_heap[0];
00111         }
00112         
00113         inline dtNode* pop()
00114         {
00115                 dtNode* result = m_heap[0];
00116                 m_size--;
00117                 trickleDown(0, m_heap[m_size]);
00118                 return result;
00119         }
00120         
00121         inline void push(dtNode* node)
00122         {
00123                 m_size++;
00124                 bubbleUp(m_size-1, node);
00125         }
00126         
00127         inline void modify(dtNode* node)
00128         {
00129                 for (int i = 0; i < m_size; ++i)
00130                 {
00131                         if (m_heap[i] == node)
00132                         {
00133                                 bubbleUp(i, node);
00134                                 return;
00135                         }
00136                 }
00137         }
00138         
00139         inline bool empty() const { return m_size == 0; }
00140         
00141         inline int getMemUsed() const
00142         {
00143                 return sizeof(*this) +
00144                 sizeof(dtNode*)*(m_capacity+1);
00145         }
00146         
00147         inline int getCapacity() const { return m_capacity; }
00148         
00149 private:
00150         void bubbleUp(int i, dtNode* node);
00151         void trickleDown(int i, dtNode* node);
00152         
00153         dtNode** m_heap;
00154         const int m_capacity;
00155         int m_size;
00156 };              
00157 
00158 
00159 #endif // DETOURNODE_H