nav_region_builder_3d.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /**************************************************************************/
  2. /* nav_region_builder_3d.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_builder_3d.h"
  31. #include "../nav_map_3d.h"
  32. #include "../nav_region_3d.h"
  33. #include "nav_region_iteration_3d.h"
  34. using namespace Nav3D;
  35. void NavRegionBuilder3D::build_iteration(NavRegionIterationBuild3D &r_build) {
  36. PerformanceData &performance_data = r_build.performance_data;
  37. performance_data.pm_polygon_count = 0;
  38. performance_data.pm_edge_count = 0;
  39. performance_data.pm_edge_merge_count = 0;
  40. performance_data.pm_edge_connection_count = 0;
  41. performance_data.pm_edge_free_count = 0;
  42. _build_step_process_navmesh_data(r_build);
  43. _build_step_find_edge_connection_pairs(r_build);
  44. _build_step_merge_edge_connection_pairs(r_build);
  45. _build_update_iteration(r_build);
  46. }
  47. void NavRegionBuilder3D::_build_step_process_navmesh_data(NavRegionIterationBuild3D &r_build) {
  48. Vector<Vector3> _navmesh_vertices = r_build.navmesh_data.vertices;
  49. Vector<Vector<int>> _navmesh_polygons = r_build.navmesh_data.polygons;
  50. if (_navmesh_vertices.is_empty() || _navmesh_polygons.is_empty()) {
  51. return;
  52. }
  53. PerformanceData &performance_data = r_build.performance_data;
  54. Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;
  55. const Transform3D &region_transform = region_iteration->transform;
  56. LocalVector<Nav3D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
  57. const int vertex_count = _navmesh_vertices.size();
  58. const Vector3 *vertices_ptr = _navmesh_vertices.ptr();
  59. const Vector<int> *polygons_ptr = _navmesh_polygons.ptr();
  60. navmesh_polygons.resize(_navmesh_polygons.size());
  61. real_t _new_region_surface_area = 0.0;
  62. AABB _new_region_bounds;
  63. bool first_vertex = true;
  64. for (uint32_t i = 0; i < navmesh_polygons.size(); i++) {
  65. Polygon &polygon = navmesh_polygons[i];
  66. polygon.id = i;
  67. polygon.owner = region_iteration.ptr();
  68. polygon.surface_area = 0.0;
  69. Vector<int> polygon_indices = polygons_ptr[i];
  70. int polygon_size = polygon_indices.size();
  71. if (polygon_size < 3) {
  72. continue;
  73. }
  74. const int *indices_ptr = polygon_indices.ptr();
  75. bool polygon_valid = true;
  76. polygon.vertices.resize(polygon_size);
  77. {
  78. real_t _new_polygon_surface_area = 0.0;
  79. for (int j(2); j < polygon_size; j++) {
  80. const Face3 face = Face3(
  81. region_transform.xform(vertices_ptr[indices_ptr[0]]),
  82. region_transform.xform(vertices_ptr[indices_ptr[j - 1]]),
  83. region_transform.xform(vertices_ptr[indices_ptr[j]]));
  84. _new_polygon_surface_area += face.get_area();
  85. }
  86. polygon.surface_area = _new_polygon_surface_area;
  87. _new_region_surface_area += _new_polygon_surface_area;
  88. }
  89. for (int j(0); j < polygon_size; j++) {
  90. int vertex_index = indices_ptr[j];
  91. if (vertex_index < 0 || vertex_index >= vertex_count) {
  92. polygon_valid = false;
  93. break;
  94. }
  95. const Vector3 point_position = region_transform.xform(vertices_ptr[vertex_index]);
  96. polygon.vertices[j] = point_position;
  97. if (first_vertex) {
  98. first_vertex = false;
  99. _new_region_bounds.position = point_position;
  100. } else {
  101. _new_region_bounds.expand_to(point_position);
  102. }
  103. }
  104. if (!polygon_valid) {
  105. polygon.surface_area = 0.0;
  106. polygon.vertices.clear();
  107. ERR_FAIL_COND_MSG(!polygon_valid, "Corrupted navigation mesh set on region. The indices of a polygon are out of range.");
  108. }
  109. }
  110. region_iteration->surface_area = _new_region_surface_area;
  111. region_iteration->bounds = _new_region_bounds;
  112. performance_data.pm_polygon_count = navmesh_polygons.size();
  113. }
  114. Nav3D::PointKey NavRegionBuilder3D::get_point_key(const Vector3 &p_pos, const Vector3 &p_cell_size) {
  115. const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
  116. const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
  117. const int z = static_cast<int>(Math::floor(p_pos.z / p_cell_size.z));
  118. PointKey p;
  119. p.key = 0;
  120. p.x = x;
  121. p.y = y;
  122. p.z = z;
  123. return p;
  124. }
  125. Nav3D::EdgeKey NavRegionBuilder3D::get_edge_key(const Vector3 &p_vertex1, const Vector3 &p_vertex2, const Vector3 &p_cell_size) {
  126. EdgeKey ek(get_point_key(p_vertex1, p_cell_size), get_point_key(p_vertex2, p_cell_size));
  127. return ek;
  128. }
  129. void NavRegionBuilder3D::_build_step_find_edge_connection_pairs(NavRegionIterationBuild3D &r_build) {
  130. PerformanceData &performance_data = r_build.performance_data;
  131. const Vector3 &map_cell_size = r_build.map_cell_size;
  132. Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;
  133. LocalVector<Nav3D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
  134. HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
  135. connection_pairs_map.clear();
  136. region_iteration->internal_connections.clear();
  137. region_iteration->internal_connections.resize(navmesh_polygons.size());
  138. region_iteration->external_edges.clear();
  139. int free_edges_count = 0;
  140. for (Polygon &poly : region_iteration->navmesh_polygons) {
  141. for (uint32_t p = 0; p < poly.vertices.size(); p++) {
  142. const int next_point = (p + 1) % poly.vertices.size();
  143. const EdgeKey ek = get_edge_key(poly.vertices[p], poly.vertices[next_point], map_cell_size);
  144. HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
  145. if (!pair_it) {
  146. pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
  147. performance_data.pm_edge_count += 1;
  148. ++free_edges_count;
  149. }
  150. EdgeConnectionPair &pair = pair_it->value;
  151. if (pair.size < 2) {
  152. // Add the polygon/edge tuple to this key.
  153. Connection new_connection;
  154. new_connection.polygon = &poly;
  155. new_connection.edge = p;
  156. new_connection.pathway_start = poly.vertices[p];
  157. new_connection.pathway_end = poly.vertices[next_point];
  158. pair.connections[pair.size] = new_connection;
  159. ++pair.size;
  160. if (pair.size == 2) {
  161. --free_edges_count;
  162. }
  163. } else {
  164. // The edge is already connected with another edge, skip.
  165. ERR_FAIL_COND_MSG(pair.size >= 2, "Navigation region synchronization error. More than 2 edges tried to occupy the same map rasterization space. This is a logical error in the navigation mesh caused by overlap or too densely placed edges.");
  166. }
  167. }
  168. }
  169. performance_data.pm_edge_free_count = free_edges_count;
  170. }
  171. void NavRegionBuilder3D::_build_step_merge_edge_connection_pairs(NavRegionIterationBuild3D &r_build) {
  172. PerformanceData &performance_data = r_build.performance_data;
  173. Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;
  174. HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
  175. for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
  176. const EdgeConnectionPair &pair = pair_it.value;
  177. if (pair.size == 2) {
  178. // Connect edge that are shared in different polygons.
  179. const Connection &c1 = pair.connections[0];
  180. const Connection &c2 = pair.connections[1];
  181. region_iteration->internal_connections[c1.polygon->id].push_back(c2);
  182. region_iteration->internal_connections[c2.polygon->id].push_back(c1);
  183. performance_data.pm_edge_merge_count += 1;
  184. } else {
  185. ERR_FAIL_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
  186. const Connection &connection = pair.connections[0];
  187. ConnectableEdge ce;
  188. ce.ek = pair_it.key;
  189. ce.polygon_index = connection.polygon->id;
  190. ce.pathway_start = connection.pathway_start;
  191. ce.pathway_end = connection.pathway_end;
  192. region_iteration->external_edges.push_back(ce);
  193. }
  194. }
  195. }
  196. void NavRegionBuilder3D::_build_update_iteration(NavRegionIterationBuild3D &r_build) {
  197. ERR_FAIL_NULL(r_build.region);
  198. // Stub. End of the build.
  199. }