DetourNode.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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. #ifndef DETOURNODE_H
  19. #define DETOURNODE_H
  20. #include "DetourNavMesh.h"
  21. enum dtNodeFlags
  22. {
  23. DT_NODE_OPEN = 0x01,
  24. DT_NODE_CLOSED = 0x02,
  25. DT_NODE_PARENT_DETACHED = 0x04, // parent of the node is not adjacent. Found using raycast.
  26. };
  27. typedef unsigned short dtNodeIndex;
  28. static const dtNodeIndex DT_NULL_IDX = (dtNodeIndex)~0;
  29. struct dtNode
  30. {
  31. float pos[3]; ///< Position of the node.
  32. float cost; ///< Cost from previous node to current node.
  33. float total; ///< Cost up to the node.
  34. unsigned int pidx : 24; ///< Index to parent node.
  35. unsigned int state : 2; ///< extra state information. A polyRef can have multiple nodes with different extra info. see DT_MAX_STATES_PER_NODE
  36. unsigned int flags : 3; ///< Node flags. A combination of dtNodeFlags.
  37. dtPolyRef id; ///< Polygon ref the node corresponds to.
  38. };
  39. static const int DT_MAX_STATES_PER_NODE = 4; // number of extra states per node. See dtNode::state
  40. class dtNodePool
  41. {
  42. public:
  43. dtNodePool(int maxNodes, int hashSize);
  44. ~dtNodePool();
  45. inline void operator=(const dtNodePool&) {}
  46. void clear();
  47. // Get a dtNode by ref and extra state information. If there is none then - allocate
  48. // There can be more than one node for the same polyRef but with different extra state information
  49. dtNode* getNode(dtPolyRef id, unsigned char state=0);
  50. dtNode* findNode(dtPolyRef id, unsigned char state);
  51. unsigned int findNodes(dtPolyRef id, dtNode** nodes, const int maxNodes);
  52. inline unsigned int getNodeIdx(const dtNode* node) const
  53. {
  54. if (!node) return 0;
  55. return (unsigned int)(node - m_nodes)+1;
  56. }
  57. inline dtNode* getNodeAtIdx(unsigned int idx)
  58. {
  59. if (!idx) return 0;
  60. return &m_nodes[idx-1];
  61. }
  62. inline const dtNode* getNodeAtIdx(unsigned int idx) const
  63. {
  64. if (!idx) return 0;
  65. return &m_nodes[idx-1];
  66. }
  67. inline int getMemUsed() const
  68. {
  69. return sizeof(*this) +
  70. sizeof(dtNode)*m_maxNodes +
  71. sizeof(dtNodeIndex)*m_maxNodes +
  72. sizeof(dtNodeIndex)*m_hashSize;
  73. }
  74. inline int getMaxNodes() const { return m_maxNodes; }
  75. inline int getHashSize() const { return m_hashSize; }
  76. inline dtNodeIndex getFirst(int bucket) const { return m_first[bucket]; }
  77. inline dtNodeIndex getNext(int i) const { return m_next[i]; }
  78. inline int getNodeCount() const { return m_nodeCount; }
  79. private:
  80. dtNode* m_nodes;
  81. dtNodeIndex* m_first;
  82. dtNodeIndex* m_next;
  83. const int m_maxNodes;
  84. const int m_hashSize;
  85. int m_nodeCount;
  86. };
  87. class dtNodeQueue
  88. {
  89. public:
  90. dtNodeQueue(int n);
  91. ~dtNodeQueue();
  92. inline void operator=(dtNodeQueue&) {}
  93. inline void clear()
  94. {
  95. m_size = 0;
  96. }
  97. inline dtNode* top()
  98. {
  99. return m_heap[0];
  100. }
  101. inline dtNode* pop()
  102. {
  103. dtNode* result = m_heap[0];
  104. m_size--;
  105. trickleDown(0, m_heap[m_size]);
  106. return result;
  107. }
  108. inline void push(dtNode* node)
  109. {
  110. m_size++;
  111. bubbleUp(m_size-1, node);
  112. }
  113. inline void modify(dtNode* node)
  114. {
  115. for (int i = 0; i < m_size; ++i)
  116. {
  117. if (m_heap[i] == node)
  118. {
  119. bubbleUp(i, node);
  120. return;
  121. }
  122. }
  123. }
  124. inline bool empty() const { return m_size == 0; }
  125. inline int getMemUsed() const
  126. {
  127. return sizeof(*this) +
  128. sizeof(dtNode*)*(m_capacity+1);
  129. }
  130. inline int getCapacity() const { return m_capacity; }
  131. private:
  132. void bubbleUp(int i, dtNode* node);
  133. void trickleDown(int i, dtNode* node);
  134. dtNode** m_heap;
  135. const int m_capacity;
  136. int m_size;
  137. };
  138. #endif // DETOURNODE_H