nav_utils.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**************************************************************************/
  2. /* nav_utils.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef NAV_UTILS_H
  31. #define NAV_UTILS_H
  32. #include "core/math/vector3.h"
  33. #include "core/templates/hash_map.h"
  34. #include "core/templates/hashfuncs.h"
  35. #include "core/templates/local_vector.h"
  36. class NavBase;
  37. namespace gd {
  38. struct Polygon;
  39. union PointKey {
  40. struct {
  41. int64_t x : 21;
  42. int64_t y : 22;
  43. int64_t z : 21;
  44. };
  45. uint64_t key = 0;
  46. };
  47. struct EdgeKey {
  48. PointKey a;
  49. PointKey b;
  50. static uint32_t hash(const EdgeKey &p_val) {
  51. return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
  52. }
  53. bool operator==(const EdgeKey &p_key) const {
  54. return (a.key == p_key.a.key) && (b.key == p_key.b.key);
  55. }
  56. EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
  57. a(p_a),
  58. b(p_b) {
  59. if (a.key > b.key) {
  60. SWAP(a, b);
  61. }
  62. }
  63. };
  64. struct Point {
  65. Vector3 pos;
  66. PointKey key;
  67. };
  68. struct Edge {
  69. /// The gateway in the edge, as, in some case, the whole edge might not be navigable.
  70. struct Connection {
  71. /// Polygon that this connection leads to.
  72. Polygon *polygon = nullptr;
  73. /// Edge of the source polygon where this connection starts from.
  74. int edge = -1;
  75. /// Point on the edge where the gateway leading to the poly starts.
  76. Vector3 pathway_start;
  77. /// Point on the edge where the gateway leading to the poly ends.
  78. Vector3 pathway_end;
  79. };
  80. /// Connections from this edge to other polygons.
  81. LocalVector<Connection> connections;
  82. };
  83. struct Polygon {
  84. /// Id of the polygon in the map.
  85. uint32_t id = UINT32_MAX;
  86. /// Navigation region or link that contains this polygon.
  87. const NavBase *owner = nullptr;
  88. /// The points of this `Polygon`
  89. LocalVector<Point> points;
  90. /// The edges of this `Polygon`
  91. LocalVector<Edge> edges;
  92. real_t surface_area = 0.0;
  93. };
  94. struct NavigationPoly {
  95. /// This poly.
  96. const Polygon *poly = nullptr;
  97. /// Index in the heap of traversable polygons.
  98. uint32_t traversable_poly_index = UINT32_MAX;
  99. /// Those 4 variables are used to travel the path backwards.
  100. int back_navigation_poly_id = -1;
  101. int back_navigation_edge = -1;
  102. Vector3 back_navigation_edge_pathway_start;
  103. Vector3 back_navigation_edge_pathway_end;
  104. /// The entry position of this poly.
  105. Vector3 entry;
  106. /// The distance traveled until now (g cost).
  107. real_t traveled_distance = 0.0;
  108. /// The distance to the destination (h cost).
  109. real_t distance_to_destination = 0.0;
  110. /// The total travel cost (f cost).
  111. real_t total_travel_cost() const {
  112. return traveled_distance + distance_to_destination;
  113. }
  114. bool operator==(const NavigationPoly &p_other) const {
  115. return poly == p_other.poly;
  116. }
  117. bool operator!=(const NavigationPoly &p_other) const {
  118. return !(*this == p_other);
  119. }
  120. };
  121. struct NavPolyTravelCostGreaterThan {
  122. // Returns `true` if the travel cost of `a` is higher than that of `b`.
  123. bool operator()(const NavigationPoly *p_poly_a, const NavigationPoly *p_poly_b) const {
  124. real_t f_cost_a = p_poly_a->total_travel_cost();
  125. real_t h_cost_a = p_poly_a->distance_to_destination;
  126. real_t f_cost_b = p_poly_b->total_travel_cost();
  127. real_t h_cost_b = p_poly_b->distance_to_destination;
  128. if (f_cost_a != f_cost_b) {
  129. return f_cost_a > f_cost_b;
  130. } else {
  131. return h_cost_a > h_cost_b;
  132. }
  133. }
  134. };
  135. struct NavPolyHeapIndexer {
  136. void operator()(NavigationPoly *p_poly, uint32_t p_heap_index) const {
  137. p_poly->traversable_poly_index = p_heap_index;
  138. }
  139. };
  140. struct ClosestPointQueryResult {
  141. Vector3 point;
  142. Vector3 normal;
  143. RID owner;
  144. };
  145. template <typename T>
  146. struct NoopIndexer {
  147. void operator()(const T &p_value, uint32_t p_index) {}
  148. };
  149. /**
  150. * A max-heap implementation that notifies of element index changes.
  151. */
  152. template <typename T, typename LessThan = Comparator<T>, typename Indexer = NoopIndexer<T>>
  153. class Heap {
  154. LocalVector<T> _buffer;
  155. LessThan _less_than;
  156. Indexer _indexer;
  157. public:
  158. void reserve(uint32_t p_size) {
  159. _buffer.reserve(p_size);
  160. }
  161. uint32_t size() const {
  162. return _buffer.size();
  163. }
  164. bool is_empty() const {
  165. return _buffer.is_empty();
  166. }
  167. void push(const T &p_element) {
  168. _buffer.push_back(p_element);
  169. _indexer(p_element, _buffer.size() - 1);
  170. _shift_up(_buffer.size() - 1);
  171. }
  172. T pop() {
  173. ERR_FAIL_COND_V_MSG(_buffer.is_empty(), T(), "Can't pop an empty heap.");
  174. T value = _buffer[0];
  175. _indexer(value, UINT32_MAX);
  176. if (_buffer.size() > 1) {
  177. _buffer[0] = _buffer[_buffer.size() - 1];
  178. _indexer(_buffer[0], 0);
  179. _buffer.remove_at(_buffer.size() - 1);
  180. _shift_down(0);
  181. } else {
  182. _buffer.remove_at(_buffer.size() - 1);
  183. }
  184. return value;
  185. }
  186. /**
  187. * Update the position of the element in the heap if necessary.
  188. */
  189. void shift(uint32_t p_index) {
  190. ERR_FAIL_UNSIGNED_INDEX_MSG(p_index, _buffer.size(), "Heap element index is out of range.");
  191. if (!_shift_up(p_index)) {
  192. _shift_down(p_index);
  193. }
  194. }
  195. void clear() {
  196. for (const T &value : _buffer) {
  197. _indexer(value, UINT32_MAX);
  198. }
  199. _buffer.clear();
  200. }
  201. Heap() {}
  202. Heap(const LessThan &p_less_than) :
  203. _less_than(p_less_than) {}
  204. Heap(const Indexer &p_indexer) :
  205. _indexer(p_indexer) {}
  206. Heap(const LessThan &p_less_than, const Indexer &p_indexer) :
  207. _less_than(p_less_than), _indexer(p_indexer) {}
  208. private:
  209. bool _shift_up(uint32_t p_index) {
  210. T value = _buffer[p_index];
  211. uint32_t current_index = p_index;
  212. uint32_t parent_index = (current_index - 1) / 2;
  213. while (current_index > 0 && _less_than(_buffer[parent_index], value)) {
  214. _buffer[current_index] = _buffer[parent_index];
  215. _indexer(_buffer[current_index], current_index);
  216. current_index = parent_index;
  217. parent_index = (current_index - 1) / 2;
  218. }
  219. if (current_index != p_index) {
  220. _buffer[current_index] = value;
  221. _indexer(value, current_index);
  222. return true;
  223. } else {
  224. return false;
  225. }
  226. }
  227. bool _shift_down(uint32_t p_index) {
  228. T value = _buffer[p_index];
  229. uint32_t current_index = p_index;
  230. uint32_t child_index = 2 * current_index + 1;
  231. while (child_index < _buffer.size()) {
  232. if (child_index + 1 < _buffer.size() &&
  233. _less_than(_buffer[child_index], _buffer[child_index + 1])) {
  234. child_index++;
  235. }
  236. if (_less_than(_buffer[child_index], value)) {
  237. break;
  238. }
  239. _buffer[current_index] = _buffer[child_index];
  240. _indexer(_buffer[current_index], current_index);
  241. current_index = child_index;
  242. child_index = 2 * current_index + 1;
  243. }
  244. if (current_index != p_index) {
  245. _buffer[current_index] = value;
  246. _indexer(value, current_index);
  247. return true;
  248. } else {
  249. return false;
  250. }
  251. }
  252. };
  253. } // namespace gd
  254. #endif // NAV_UTILS_H