nav_utils.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. /// The gateway in the edge, as, in some case, the whole edge might not be navigable.
  71. struct Connection {
  72. Polygon *polygon = nullptr;
  73. int edge = -1;
  74. Vector3 pathway_start;
  75. Vector3 pathway_end;
  76. };
  77. Vector<Connection> connections;
  78. };
  79. struct Polygon {
  80. NavRegion *owner;
  81. /// The points of this `Polygon`
  82. std::vector<Point> points;
  83. /// Are the points clockwise ?
  84. bool clockwise;
  85. /// The edges of this `Polygon`
  86. std::vector<Edge> edges;
  87. /// The center of this `Polygon`
  88. Vector3 center;
  89. };
  90. struct NavigationPoly {
  91. uint32_t self_id = 0;
  92. /// This poly.
  93. const Polygon *poly;
  94. /// Those 4 variables are used to travel the path backwards.
  95. int back_navigation_poly_id = -1;
  96. uint32_t back_navigation_edge = UINT32_MAX;
  97. Vector3 back_navigation_edge_pathway_start;
  98. Vector3 back_navigation_edge_pathway_end;
  99. /// The entry location of this poly.
  100. Vector3 entry;
  101. /// The distance to the destination.
  102. float traveled_distance = 0.0;
  103. NavigationPoly(const Polygon *p_poly) :
  104. poly(p_poly) {}
  105. bool operator==(const NavigationPoly &other) const {
  106. return this->poly == other.poly;
  107. }
  108. bool operator!=(const NavigationPoly &other) const {
  109. return !operator==(other);
  110. }
  111. };
  112. } // namespace gd
  113. #endif // NAV_UTILS_H