nav_utils.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*************************************************************************/
  2. /* nav_utils.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 <vector>
  34. /**
  35. @author AndreaCatania
  36. */
  37. class NavRegion;
  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. bool operator<(const PointKey &p_key) const { return key < p_key.key; }
  48. };
  49. struct EdgeKey {
  50. PointKey a;
  51. PointKey b;
  52. bool operator<(const EdgeKey &p_key) const {
  53. return (a.key == p_key.a.key) ? (b.key < p_key.b.key) : (a.key < p_key.a.key);
  54. }
  55. EdgeKey(const PointKey &p_a = PointKey(), const PointKey &p_b = PointKey()) :
  56. a(p_a),
  57. b(p_b) {
  58. if (a.key > b.key) {
  59. SWAP(a, b);
  60. }
  61. }
  62. };
  63. struct Point {
  64. Vector3 pos;
  65. PointKey key;
  66. };
  67. struct Edge {
  68. /// This edge ID
  69. int this_edge = -1;
  70. /// Other Polygon
  71. Polygon *other_polygon = nullptr;
  72. /// The other `Polygon` at this edge id has this `Polygon`.
  73. int other_edge = -1;
  74. };
  75. struct Polygon {
  76. NavRegion *owner;
  77. /// The points of this `Polygon`
  78. std::vector<Point> points;
  79. /// Are the points clockwise ?
  80. bool clockwise;
  81. /// The edges of this `Polygon`
  82. std::vector<Edge> edges;
  83. /// The center of this `Polygon`
  84. Vector3 center;
  85. };
  86. struct Connection {
  87. Polygon *A = nullptr;
  88. int A_edge = -1;
  89. Polygon *B = nullptr;
  90. int B_edge = -1;
  91. };
  92. struct NavigationPoly {
  93. uint32_t self_id = 0;
  94. /// This poly.
  95. const Polygon *poly;
  96. /// The previous navigation poly (id in the `navigation_poly` array).
  97. int prev_navigation_poly_id = -1;
  98. /// The edge id in this `Poly` to reach the `prev_navigation_poly_id`.
  99. uint32_t back_navigation_edge = 0;
  100. /// The entry location of this poly.
  101. Vector3 entry;
  102. /// The distance to the destination.
  103. float traveled_distance = 0.0;
  104. NavigationPoly(const Polygon *p_poly) :
  105. poly(p_poly) {}
  106. bool operator==(const NavigationPoly &other) const {
  107. return this->poly == other.poly;
  108. }
  109. bool operator!=(const NavigationPoly &other) const {
  110. return !operator==(other);
  111. }
  112. };
  113. struct FreeEdge {
  114. bool is_free = false;
  115. Polygon *poly = nullptr;
  116. uint32_t edge_id = 0;
  117. Vector3 edge_center;
  118. Vector3 edge_dir;
  119. float edge_len_squared = 0.0;
  120. };
  121. } // namespace gd
  122. #endif // NAV_UTILS_H