DetourNode.h 3.5 KB

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