nav_region.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /**************************************************************************/
  2. /* nav_region.cpp */
  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. #include "nav_region.h"
  31. #include "nav_map.h"
  32. void NavRegion::set_map(NavMap *p_map) {
  33. if (map == p_map) {
  34. return;
  35. }
  36. if (map) {
  37. map->remove_region(this);
  38. }
  39. map = p_map;
  40. polygons_dirty = true;
  41. connections.clear();
  42. if (map) {
  43. map->add_region(this);
  44. }
  45. }
  46. void NavRegion::set_enabled(bool p_enabled) {
  47. if (enabled == p_enabled) {
  48. return;
  49. }
  50. enabled = p_enabled;
  51. // TODO: This should not require a full rebuild as the region has not really changed.
  52. polygons_dirty = true;
  53. };
  54. void NavRegion::set_use_edge_connections(bool p_enabled) {
  55. if (use_edge_connections != p_enabled) {
  56. use_edge_connections = p_enabled;
  57. polygons_dirty = true;
  58. }
  59. }
  60. void NavRegion::set_transform(Transform3D p_transform) {
  61. if (transform == p_transform) {
  62. return;
  63. }
  64. transform = p_transform;
  65. polygons_dirty = true;
  66. }
  67. void NavRegion::set_mesh(Ref<NavigationMesh> p_mesh) {
  68. mesh = p_mesh;
  69. polygons_dirty = true;
  70. }
  71. int NavRegion::get_connections_count() const {
  72. if (!map) {
  73. return 0;
  74. }
  75. return connections.size();
  76. }
  77. Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const {
  78. ERR_FAIL_NULL_V(map, Vector3());
  79. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  80. return connections[p_connection_id].pathway_start;
  81. }
  82. Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const {
  83. ERR_FAIL_NULL_V(map, Vector3());
  84. ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3());
  85. return connections[p_connection_id].pathway_end;
  86. }
  87. Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
  88. if (!get_enabled()) {
  89. return Vector3();
  90. }
  91. const LocalVector<gd::Polygon> &region_polygons = get_polygons();
  92. if (region_polygons.is_empty()) {
  93. return Vector3();
  94. }
  95. if (p_uniformly) {
  96. real_t accumulated_area = 0;
  97. RBMap<real_t, uint32_t> region_area_map;
  98. for (uint32_t rp_index = 0; rp_index < region_polygons.size(); rp_index++) {
  99. const gd::Polygon &region_polygon = region_polygons[rp_index];
  100. real_t polyon_area = region_polygon.surface_area;
  101. if (polyon_area == 0.0) {
  102. continue;
  103. }
  104. region_area_map[accumulated_area] = rp_index;
  105. accumulated_area += polyon_area;
  106. }
  107. if (region_area_map.is_empty() || accumulated_area == 0) {
  108. // All polygons have no real surface / no area.
  109. return Vector3();
  110. }
  111. real_t region_area_map_pos = Math::random(real_t(0), accumulated_area);
  112. RBMap<real_t, uint32_t>::Iterator region_E = region_area_map.find_closest(region_area_map_pos);
  113. ERR_FAIL_COND_V(!region_E, Vector3());
  114. uint32_t rrp_polygon_index = region_E->value;
  115. ERR_FAIL_UNSIGNED_INDEX_V(rrp_polygon_index, region_polygons.size(), Vector3());
  116. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  117. real_t accumulated_polygon_area = 0;
  118. RBMap<real_t, uint32_t> polygon_area_map;
  119. for (uint32_t rpp_index = 2; rpp_index < rr_polygon.points.size(); rpp_index++) {
  120. real_t face_area = Face3(rr_polygon.points[0].pos, rr_polygon.points[rpp_index - 1].pos, rr_polygon.points[rpp_index].pos).get_area();
  121. if (face_area == 0.0) {
  122. continue;
  123. }
  124. polygon_area_map[accumulated_polygon_area] = rpp_index;
  125. accumulated_polygon_area += face_area;
  126. }
  127. if (polygon_area_map.is_empty() || accumulated_polygon_area == 0) {
  128. // All faces have no real surface / no area.
  129. return Vector3();
  130. }
  131. real_t polygon_area_map_pos = Math::random(real_t(0), accumulated_polygon_area);
  132. RBMap<real_t, uint32_t>::Iterator polygon_E = polygon_area_map.find_closest(polygon_area_map_pos);
  133. ERR_FAIL_COND_V(!polygon_E, Vector3());
  134. uint32_t rrp_face_index = polygon_E->value;
  135. ERR_FAIL_UNSIGNED_INDEX_V(rrp_face_index, rr_polygon.points.size(), Vector3());
  136. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  137. Vector3 face_random_position = face.get_random_point_inside();
  138. return face_random_position;
  139. } else {
  140. uint32_t rrp_polygon_index = Math::random(int(0), region_polygons.size() - 1);
  141. const gd::Polygon &rr_polygon = region_polygons[rrp_polygon_index];
  142. uint32_t rrp_face_index = Math::random(int(2), rr_polygon.points.size() - 1);
  143. const Face3 face(rr_polygon.points[0].pos, rr_polygon.points[rrp_face_index - 1].pos, rr_polygon.points[rrp_face_index].pos);
  144. Vector3 face_random_position = face.get_random_point_inside();
  145. return face_random_position;
  146. }
  147. }
  148. bool NavRegion::sync() {
  149. bool something_changed = polygons_dirty /* || something_dirty? */;
  150. update_polygons();
  151. return something_changed;
  152. }
  153. void NavRegion::update_polygons() {
  154. if (!polygons_dirty) {
  155. return;
  156. }
  157. polygons.clear();
  158. surface_area = 0.0;
  159. polygons_dirty = false;
  160. if (map == nullptr) {
  161. return;
  162. }
  163. if (mesh.is_null()) {
  164. return;
  165. }
  166. #ifdef DEBUG_ENABLED
  167. if (!Math::is_equal_approx(double(map->get_cell_size()), double(mesh->get_cell_size()))) {
  168. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_size` of %s while assigned to a navigation map set to a `cell_size` of %s. The cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function. The cell size for default navigation maps can also be changed in the ProjectSettings.", double(mesh->get_cell_size()), double(map->get_cell_size())));
  169. }
  170. if (!Math::is_equal_approx(double(map->get_cell_height()), double(mesh->get_cell_height()))) {
  171. ERR_PRINT_ONCE(vformat("Navigation map synchronization error. Attempted to update a navigation region with a navigation mesh that uses a `cell_height` of %s while assigned to a navigation map set to a `cell_height` of %s. The cell height for navigation maps can be changed by using the NavigationServer map_set_cell_height() function. The cell height for default navigation maps can also be changed in the ProjectSettings.", double(mesh->get_cell_height()), double(map->get_cell_height())));
  172. }
  173. if (map && Math::rad_to_deg(map->get_up().angle_to(transform.basis.get_column(1))) >= 90.0f) {
  174. ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to update a navigation region transform rotated 90 degrees or more away from the current navigation map UP orientation.");
  175. }
  176. #endif // DEBUG_ENABLED
  177. Vector<Vector3> vertices = mesh->get_vertices();
  178. int len = vertices.size();
  179. if (len == 0) {
  180. return;
  181. }
  182. const Vector3 *vertices_r = vertices.ptr();
  183. polygons.resize(mesh->get_polygon_count());
  184. real_t _new_region_surface_area = 0.0;
  185. // Build
  186. int navigation_mesh_polygon_index = 0;
  187. for (gd::Polygon &polygon : polygons) {
  188. polygon.owner = this;
  189. polygon.surface_area = 0.0;
  190. Vector<int> navigation_mesh_polygon = mesh->get_polygon(navigation_mesh_polygon_index);
  191. navigation_mesh_polygon_index += 1;
  192. int navigation_mesh_polygon_size = navigation_mesh_polygon.size();
  193. if (navigation_mesh_polygon_size < 3) {
  194. continue;
  195. }
  196. const int *indices = navigation_mesh_polygon.ptr();
  197. bool valid(true);
  198. polygon.points.resize(navigation_mesh_polygon_size);
  199. polygon.edges.resize(navigation_mesh_polygon_size);
  200. real_t _new_polygon_surface_area = 0.0;
  201. for (int j(2); j < navigation_mesh_polygon_size; j++) {
  202. const Face3 face = Face3(
  203. transform.xform(vertices_r[indices[0]]),
  204. transform.xform(vertices_r[indices[j - 1]]),
  205. transform.xform(vertices_r[indices[j]]));
  206. _new_polygon_surface_area += face.get_area();
  207. }
  208. polygon.surface_area = _new_polygon_surface_area;
  209. _new_region_surface_area += _new_polygon_surface_area;
  210. Vector3 polygon_center;
  211. real_t sum(0);
  212. for (int j(0); j < navigation_mesh_polygon_size; j++) {
  213. int idx = indices[j];
  214. if (idx < 0 || idx >= len) {
  215. valid = false;
  216. break;
  217. }
  218. Vector3 point_position = transform.xform(vertices_r[idx]);
  219. polygon.points[j].pos = point_position;
  220. polygon.points[j].key = map->get_point_key(point_position);
  221. polygon_center += point_position; // Composing the center of the polygon
  222. if (j >= 2) {
  223. Vector3 epa = transform.xform(vertices_r[indices[j - 2]]);
  224. Vector3 epb = transform.xform(vertices_r[indices[j - 1]]);
  225. sum += map->get_up().dot((epb - epa).cross(point_position - epa));
  226. }
  227. }
  228. if (!valid) {
  229. ERR_BREAK_MSG(!valid, "The navigation mesh set in this region is not valid!");
  230. }
  231. polygon.clockwise = sum > 0;
  232. if (!navigation_mesh_polygon.is_empty()) {
  233. polygon.center = polygon_center / real_t(navigation_mesh_polygon.size());
  234. }
  235. }
  236. surface_area = _new_region_surface_area;
  237. }