nav_utils.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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. Vector<Connection> connections;
  82. };
  83. struct Polygon {
  84. /// Navigation region or link that contains this polygon.
  85. const NavBase *owner = nullptr;
  86. /// The points of this `Polygon`
  87. LocalVector<Point> points;
  88. /// Are the points clockwise?
  89. bool clockwise;
  90. /// The edges of this `Polygon`
  91. LocalVector<Edge> edges;
  92. /// The center of this `Polygon`
  93. Vector3 center;
  94. };
  95. struct NavigationPoly {
  96. uint32_t self_id = 0;
  97. /// This poly.
  98. const Polygon *poly;
  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 to the destination.
  107. real_t traveled_distance = 0.0;
  108. NavigationPoly() { poly = nullptr; }
  109. NavigationPoly(const Polygon *p_poly) :
  110. poly(p_poly) {}
  111. bool operator==(const NavigationPoly &other) const {
  112. return this->poly == other.poly;
  113. }
  114. bool operator!=(const NavigationPoly &other) const {
  115. return !operator==(other);
  116. }
  117. };
  118. struct ClosestPointQueryResult {
  119. Vector3 point;
  120. Vector3 normal;
  121. RID owner;
  122. };
  123. } // namespace gd
  124. #endif // NAV_UTILS_H