DetourNode.h 4.5 KB

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