nav_utils.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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. #include "servers/navigation/navigation_utilities.h"
  37. struct NavBaseIteration;
  38. namespace gd {
  39. struct Polygon;
  40. union PointKey {
  41. struct {
  42. int64_t x : 21;
  43. int64_t y : 22;
  44. int64_t z : 21;
  45. };
  46. uint64_t key = 0;
  47. };
  48. struct EdgeKey {
  49. PointKey a;
  50. PointKey b;
  51. static uint32_t hash(const EdgeKey &p_val) {
  52. return hash_one_uint64(p_val.a.key) ^ hash_one_uint64(p_val.b.key);
  53. }
  54. bool operator==(const EdgeKey &p_key) const {
  55. return (a.key == p_key.a.key) && (b.key == p_key.b.key);
  56. }
  57. EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
  58. a(p_a),
  59. b(p_b) {
  60. if (a.key > b.key) {
  61. SWAP(a, b);
  62. }
  63. }
  64. };
  65. struct Point {
  66. Vector3 pos;
  67. PointKey key;
  68. };
  69. struct Edge {
  70. /// The gateway in the edge, as, in some case, the whole edge might not be navigable.
  71. struct Connection {
  72. /// Polygon that this connection leads to.
  73. Polygon *polygon = nullptr;
  74. /// Edge of the source polygon where this connection starts from.
  75. int edge = -1;
  76. /// Point on the edge where the gateway leading to the poly starts.
  77. Vector3 pathway_start;
  78. /// Point on the edge where the gateway leading to the poly ends.
  79. Vector3 pathway_end;
  80. };
  81. /// Connections from this edge to other polygons.
  82. LocalVector<Connection> connections;
  83. };
  84. struct Polygon {
  85. /// Id of the polygon in the map.
  86. uint32_t id = UINT32_MAX;
  87. /// Navigation region or link that contains this polygon.
  88. const NavBaseIteration *owner = nullptr;
  89. /// The points of this `Polygon`
  90. LocalVector<Point> points;
  91. /// The edges of this `Polygon`
  92. LocalVector<Edge> edges;
  93. real_t surface_area = 0.0;
  94. };
  95. struct NavigationPoly {
  96. /// This poly.
  97. const Polygon *poly = nullptr;
  98. /// Index in the heap of traversable polygons.
  99. uint32_t traversable_poly_index = UINT32_MAX;
  100. /// Those 4 variables are used to travel the path backwards.
  101. int back_navigation_poly_id = -1;
  102. int back_navigation_edge = -1;
  103. Vector3 back_navigation_edge_pathway_start;
  104. Vector3 back_navigation_edge_pathway_end;
  105. /// The entry position of this poly.
  106. Vector3 entry;
  107. /// The distance traveled until now (g cost).
  108. real_t traveled_distance = 0.0;
  109. /// The distance to the destination (h cost).
  110. real_t distance_to_destination = 0.0;
  111. /// The total travel cost (f cost).
  112. real_t total_travel_cost() const {
  113. return traveled_distance + distance_to_destination;
  114. }
  115. bool operator==(const NavigationPoly &p_other) const {
  116. return poly == p_other.poly;
  117. }
  118. bool operator!=(const NavigationPoly &p_other) const {
  119. return !(*this == p_other);
  120. }
  121. void reset() {
  122. poly = nullptr;
  123. traversable_poly_index = UINT32_MAX;
  124. back_navigation_poly_id = -1;
  125. back_navigation_edge = -1;
  126. traveled_distance = FLT_MAX;
  127. distance_to_destination = 0.0;
  128. }
  129. };
  130. struct NavPolyTravelCostGreaterThan {
  131. // Returns `true` if the travel cost of `a` is higher than that of `b`.
  132. bool operator()(const NavigationPoly *p_poly_a, const NavigationPoly *p_poly_b) const {
  133. real_t f_cost_a = p_poly_a->total_travel_cost();
  134. real_t h_cost_a = p_poly_a->distance_to_destination;
  135. real_t f_cost_b = p_poly_b->total_travel_cost();
  136. real_t h_cost_b = p_poly_b->distance_to_destination;
  137. if (f_cost_a != f_cost_b) {
  138. return f_cost_a > f_cost_b;
  139. } else {
  140. return h_cost_a > h_cost_b;
  141. }
  142. }
  143. };
  144. struct NavPolyHeapIndexer {
  145. void operator()(NavigationPoly *p_poly, uint32_t p_heap_index) const {
  146. p_poly->traversable_poly_index = p_heap_index;
  147. }
  148. };
  149. struct ClosestPointQueryResult {
  150. Vector3 point;
  151. Vector3 normal;
  152. RID owner;
  153. };
  154. template <typename T>
  155. struct NoopIndexer {
  156. void operator()(const T &p_value, uint32_t p_index) {}
  157. };
  158. /**
  159. * A max-heap implementation that notifies of element index changes.
  160. */
  161. template <typename T, typename LessThan = Comparator<T>, typename Indexer = NoopIndexer<T>>
  162. class Heap {
  163. LocalVector<T> _buffer;
  164. LessThan _less_than;
  165. Indexer _indexer;
  166. public:
  167. static constexpr uint32_t INVALID_INDEX = UINT32_MAX;
  168. void reserve(uint32_t p_size) {
  169. _buffer.reserve(p_size);
  170. }
  171. uint32_t size() const {
  172. return _buffer.size();
  173. }
  174. bool is_empty() const {
  175. return _buffer.is_empty();
  176. }
  177. void push(const T &p_element) {
  178. _buffer.push_back(p_element);
  179. _indexer(p_element, _buffer.size() - 1);
  180. _shift_up(_buffer.size() - 1);
  181. }
  182. T pop() {
  183. ERR_FAIL_COND_V_MSG(_buffer.is_empty(), T(), "Can't pop an empty heap.");
  184. T value = _buffer[0];
  185. _indexer(value, INVALID_INDEX);
  186. if (_buffer.size() > 1) {
  187. _buffer[0] = _buffer[_buffer.size() - 1];
  188. _indexer(_buffer[0], 0);
  189. _buffer.remove_at(_buffer.size() - 1);
  190. _shift_down(0);
  191. } else {
  192. _buffer.remove_at(_buffer.size() - 1);
  193. }
  194. return value;
  195. }
  196. /**
  197. * Update the position of the element in the heap if necessary.
  198. */
  199. void shift(uint32_t p_index) {
  200. ERR_FAIL_UNSIGNED_INDEX_MSG(p_index, _buffer.size(), "Heap element index is out of range.");
  201. if (!_shift_up(p_index)) {
  202. _shift_down(p_index);
  203. }
  204. }
  205. void clear() {
  206. for (const T &value : _buffer) {
  207. _indexer(value, INVALID_INDEX);
  208. }
  209. _buffer.clear();
  210. }
  211. Heap() {}
  212. Heap(const LessThan &p_less_than) :
  213. _less_than(p_less_than) {}
  214. Heap(const Indexer &p_indexer) :
  215. _indexer(p_indexer) {}
  216. Heap(const LessThan &p_less_than, const Indexer &p_indexer) :
  217. _less_than(p_less_than), _indexer(p_indexer) {}
  218. private:
  219. bool _shift_up(uint32_t p_index) {
  220. T value = _buffer[p_index];
  221. uint32_t current_index = p_index;
  222. uint32_t parent_index = (current_index - 1) / 2;
  223. while (current_index > 0 && _less_than(_buffer[parent_index], value)) {
  224. _buffer[current_index] = _buffer[parent_index];
  225. _indexer(_buffer[current_index], current_index);
  226. current_index = parent_index;
  227. parent_index = (current_index - 1) / 2;
  228. }
  229. if (current_index != p_index) {
  230. _buffer[current_index] = value;
  231. _indexer(value, current_index);
  232. return true;
  233. } else {
  234. return false;
  235. }
  236. }
  237. bool _shift_down(uint32_t p_index) {
  238. T value = _buffer[p_index];
  239. uint32_t current_index = p_index;
  240. uint32_t child_index = 2 * current_index + 1;
  241. while (child_index < _buffer.size()) {
  242. if (child_index + 1 < _buffer.size() &&
  243. _less_than(_buffer[child_index], _buffer[child_index + 1])) {
  244. child_index++;
  245. }
  246. if (_less_than(_buffer[child_index], value)) {
  247. break;
  248. }
  249. _buffer[current_index] = _buffer[child_index];
  250. _indexer(_buffer[current_index], current_index);
  251. current_index = child_index;
  252. child_index = 2 * current_index + 1;
  253. }
  254. if (current_index != p_index) {
  255. _buffer[current_index] = value;
  256. _indexer(value, current_index);
  257. return true;
  258. } else {
  259. return false;
  260. }
  261. }
  262. };
  263. struct EdgeConnectionPair {
  264. gd::Edge::Connection connections[2];
  265. int size = 0;
  266. };
  267. struct PerformanceData {
  268. int pm_region_count = 0;
  269. int pm_agent_count = 0;
  270. int pm_link_count = 0;
  271. int pm_polygon_count = 0;
  272. int pm_edge_count = 0;
  273. int pm_edge_merge_count = 0;
  274. int pm_edge_connection_count = 0;
  275. int pm_edge_free_count = 0;
  276. int pm_obstacle_count = 0;
  277. void reset() {
  278. pm_region_count = 0;
  279. pm_agent_count = 0;
  280. pm_link_count = 0;
  281. pm_polygon_count = 0;
  282. pm_edge_count = 0;
  283. pm_edge_merge_count = 0;
  284. pm_edge_connection_count = 0;
  285. pm_edge_free_count = 0;
  286. pm_obstacle_count = 0;
  287. }
  288. };
  289. } // namespace gd
  290. #endif // NAV_UTILS_H