DetourNode.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // Copyright (c) 2009 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. enum dtNodeFlags
  21. {
  22. DT_NODE_OPEN = 0x01,
  23. DT_NODE_CLOSED = 0x02,
  24. };
  25. struct dtNode
  26. {
  27. float cost;
  28. float total;
  29. unsigned int id;
  30. unsigned int pidx : 30;
  31. unsigned int flags : 2;
  32. };
  33. class dtNodePool
  34. {
  35. public:
  36. dtNodePool(int maxNodes, int hashSize);
  37. ~dtNodePool();
  38. inline void operator=(const dtNodePool&) {}
  39. void clear();
  40. dtNode* getNode(unsigned int id);
  41. const dtNode* findNode(unsigned int id) const;
  42. inline unsigned int getNodeIdx(const dtNode* node) const
  43. {
  44. if (!node) return 0;
  45. return (unsigned int)(node - m_nodes)+1;
  46. }
  47. inline dtNode* getNodeAtIdx(unsigned int idx)
  48. {
  49. if (!idx) return 0;
  50. return &m_nodes[idx-1];
  51. }
  52. inline int getMemUsed() const
  53. {
  54. return sizeof(*this) +
  55. sizeof(dtNode)*m_maxNodes +
  56. sizeof(unsigned short)*m_maxNodes +
  57. sizeof(unsigned short)*m_hashSize;
  58. }
  59. private:
  60. inline unsigned int hashint(unsigned int a) const
  61. {
  62. a += ~(a<<15);
  63. a ^= (a>>10);
  64. a += (a<<3);
  65. a ^= (a>>6);
  66. a += ~(a<<11);
  67. a ^= (a>>16);
  68. return a;
  69. }
  70. dtNode* m_nodes;
  71. unsigned short* m_first;
  72. unsigned short* m_next;
  73. const int m_maxNodes;
  74. const int m_hashSize;
  75. int m_nodeCount;
  76. };
  77. class dtNodeQueue
  78. {
  79. public:
  80. dtNodeQueue(int n);
  81. ~dtNodeQueue();
  82. inline void operator=(dtNodeQueue&) {}
  83. inline void clear()
  84. {
  85. m_size = 0;
  86. }
  87. inline dtNode* top()
  88. {
  89. return m_heap[0];
  90. }
  91. inline dtNode* pop()
  92. {
  93. dtNode* result = m_heap[0];
  94. m_size--;
  95. trickleDown(0, m_heap[m_size]);
  96. return result;
  97. }
  98. inline void push(dtNode* node)
  99. {
  100. m_size++;
  101. bubbleUp(m_size-1, node);
  102. }
  103. inline void modify(dtNode* node)
  104. {
  105. for (int i = 0; i < m_size; ++i)
  106. {
  107. if (m_heap[i] == node)
  108. {
  109. bubbleUp(i, node);
  110. return;
  111. }
  112. }
  113. }
  114. inline bool empty() const { return m_size == 0; }
  115. inline int getMemUsed() const
  116. {
  117. return sizeof(*this) +
  118. sizeof(dtNode*)*(m_capacity+1);
  119. }
  120. private:
  121. void bubbleUp(int i, dtNode* node);
  122. void trickleDown(int i, dtNode* node);
  123. dtNode** m_heap;
  124. const int m_capacity;
  125. int m_size;
  126. };
  127. #endif // DETOURNODE_H