| 1 | /**************************************************************************/ |
| 2 | /* nav_region_builder_2d.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 | |
| 31 | #include "nav_region_builder_2d.h" |
| 32 | |
| 33 | #include "../nav_map_2d.h" |
| 34 | #include "../nav_region_2d.h" |
| 35 | #include "../triangle2.h" |
| 36 | #include "nav_region_iteration_2d.h" |
| 37 | |
| 38 | #include "core/config/project_settings.h" |
| 39 | |
| 40 | using namespace Nav2D; |
| 41 | |
| 42 | void NavRegionBuilder2D::build_iteration(NavRegionIterationBuild2D &r_build) { |
| 43 | PerformanceData &performance_data = r_build.performance_data; |
| 44 | |
| 45 | performance_data.pm_polygon_count = 0; |
| 46 | performance_data.pm_edge_count = 0; |
| 47 | performance_data.pm_edge_merge_count = 0; |
| 48 | performance_data.pm_edge_connection_count = 0; |
| 49 | performance_data.pm_edge_free_count = 0; |
| 50 | |
| 51 | _build_step_process_navmesh_data(r_build); |
| 52 | |
| 53 | _build_step_find_edge_connection_pairs(r_build); |
| 54 | |
| 55 | _build_step_merge_edge_connection_pairs(r_build); |
| 56 | |
| 57 | _build_update_iteration(r_build); |
| 58 | } |
| 59 | |
| 60 | void NavRegionBuilder2D::_build_step_process_navmesh_data(NavRegionIterationBuild2D &r_build) { |
| 61 | Vector<Vector2> _navmesh_vertices = r_build.navmesh_data.vertices; |
| 62 | Vector<Vector<int>> _navmesh_polygons = r_build.navmesh_data.polygons; |
| 63 | |
| 64 | if (_navmesh_vertices.is_empty() || _navmesh_polygons.is_empty()) { |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | PerformanceData &performance_data = r_build.performance_data; |
| 69 | Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration; |
| 70 | |
| 71 | const Transform2D ®ion_transform = region_iteration->transform; |
| 72 | LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons; |
| 73 | |
| 74 | const int vertex_count = _navmesh_vertices.size(); |
| 75 | |
| 76 | const Vector2 *vertices_ptr = _navmesh_vertices.ptr(); |
| 77 | const Vector<int> *polygons_ptr = _navmesh_polygons.ptr(); |
| 78 | |
| 79 | navmesh_polygons.resize(_navmesh_polygons.size()); |
| 80 | |
| 81 | real_t _new_region_surface_area = 0.0; |
| 82 | Rect2 _new_region_bounds; |
| 83 | |
| 84 | bool first_vertex = true; |
| 85 | |
| 86 | for (uint32_t i = 0; i < navmesh_polygons.size(); i++) { |
| 87 | Polygon &polygon = navmesh_polygons[i]; |
| 88 | polygon.id = i; |
| 89 | polygon.owner = region_iteration.ptr(); |
| 90 | polygon.surface_area = 0.0; |
| 91 | |
| 92 | Vector<int> polygon_indices = polygons_ptr[i]; |
| 93 | |
| 94 | int polygon_size = polygon_indices.size(); |
| 95 | if (polygon_size < 3) { |
| 96 | continue; |
| 97 | } |
| 98 | |
| 99 | const int *indices_ptr = polygon_indices.ptr(); |
| 100 | |
| 101 | bool polygon_valid = true; |
| 102 | |
| 103 | polygon.vertices.resize(polygon_size); |
| 104 | |
| 105 | { |
| 106 | real_t _new_polygon_surface_area = 0.0; |
| 107 | |
| 108 | for (int j(2); j < polygon_size; j++) { |
| 109 | const Triangle2 triangle = Triangle2( |
| 110 | region_transform.xform(vertices_ptr[indices_ptr[0]]), |
| 111 | region_transform.xform(vertices_ptr[indices_ptr[j - 1]]), |
| 112 | region_transform.xform(vertices_ptr[indices_ptr[j]])); |
| 113 | |
| 114 | _new_polygon_surface_area += triangle.get_area(); |
| 115 | } |
| 116 | |
| 117 | polygon.surface_area = _new_polygon_surface_area; |
| 118 | _new_region_surface_area += _new_polygon_surface_area; |
| 119 | } |
| 120 | |
| 121 | for (int j(0); j < polygon_size; j++) { |
| 122 | int vertex_index = indices_ptr[j]; |
| 123 | if (vertex_index < 0 || vertex_index >= vertex_count) { |
| 124 | polygon_valid = false; |
| 125 | break; |
| 126 | } |
| 127 | |
| 128 | const Vector2 point_position = region_transform.xform(vertices_ptr[vertex_index]); |
| 129 | polygon.vertices[j] = point_position; |
| 130 | |
| 131 | if (first_vertex) { |
| 132 | first_vertex = false; |
| 133 | _new_region_bounds.position = point_position; |
| 134 | } else { |
| 135 | _new_region_bounds.expand_to(point_position); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | if (!polygon_valid) { |
| 140 | polygon.surface_area = 0.0; |
| 141 | polygon.vertices.clear(); |
| 142 | ERR_FAIL_COND_MSG(!polygon_valid, "Corrupted navigation mesh set on region. The indices of a polygon are out of range."); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | region_iteration->surface_area = _new_region_surface_area; |
| 147 | region_iteration->bounds = _new_region_bounds; |
| 148 | |
| 149 | performance_data.pm_polygon_count = navmesh_polygons.size(); |
| 150 | } |
| 151 | |
| 152 | Nav2D::PointKey NavRegionBuilder2D::get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size) { |
| 153 | const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x)); |
| 154 | const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y)); |
| 155 | |
| 156 | PointKey p; |
| 157 | p.key = 0; |
| 158 | p.x = x; |
| 159 | p.y = y; |
| 160 | return p; |
| 161 | } |
| 162 | |
| 163 | Nav2D::EdgeKey NavRegionBuilder2D::get_edge_key(const Vector2 &p_vertex1, const Vector2 &p_vertex2, const Vector2 &p_cell_size) { |
| 164 | EdgeKey ek(get_point_key(p_vertex1, p_cell_size), get_point_key(p_vertex2, p_cell_size)); |
| 165 | return ek; |
| 166 | } |
| 167 | |
| 168 | void NavRegionBuilder2D::_build_step_find_edge_connection_pairs(NavRegionIterationBuild2D &r_build) { |
| 169 | PerformanceData &performance_data = r_build.performance_data; |
| 170 | |
| 171 | const Vector2 &map_cell_size = r_build.map_cell_size; |
| 172 | Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration; |
| 173 | LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons; |
| 174 | |
| 175 | HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map; |
| 176 | connection_pairs_map.clear(); |
| 177 | |
| 178 | region_iteration->internal_connections.clear(); |
| 179 | region_iteration->internal_connections.resize(navmesh_polygons.size()); |
| 180 | |
| 181 | region_iteration->external_edges.clear(); |
| 182 | |
| 183 | int free_edges_count = 0; |
| 184 | int edge_merge_error_count = 0; |
| 185 | |
| 186 | for (Polygon &poly : region_iteration->navmesh_polygons) { |
| 187 | for (uint32_t p = 0; p < poly.vertices.size(); p++) { |
| 188 | const int next_point = (p + 1) % poly.vertices.size(); |
| 189 | const EdgeKey ek = get_edge_key(poly.vertices[p], poly.vertices[next_point], map_cell_size); |
| 190 | |
| 191 | HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek); |
| 192 | if (!pair_it) { |
| 193 | pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair()); |
| 194 | performance_data.pm_edge_count += 1; |
| 195 | ++free_edges_count; |
| 196 | } |
| 197 | EdgeConnectionPair &pair = pair_it->value; |
| 198 | if (pair.size < 2) { |
| 199 | // Add the polygon/edge tuple to this key. |
| 200 | Connection new_connection; |
| 201 | new_connection.polygon = &poly; |
| 202 | new_connection.edge = p; |
| 203 | new_connection.pathway_start = poly.vertices[p]; |
| 204 | new_connection.pathway_end = poly.vertices[next_point]; |
| 205 | |
| 206 | pair.connections[pair.size] = new_connection; |
| 207 | ++pair.size; |
| 208 | if (pair.size == 2) { |
| 209 | --free_edges_count; |
| 210 | } |
| 211 | |
| 212 | } else { |
| 213 | // The edge is already connected with another edge, skip. |
| 214 | edge_merge_error_count++; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | if (edge_merge_error_count > 0 && GLOBAL_GET_CACHED(bool, "navigation/2d/warnings/navmesh_edge_merge_errors")) { |
| 220 | WARN_PRINT("Navigation region synchronization had " + itos(edge_merge_error_count) + " edge error(s).\nMore than 2 edges tried to occupy the same map rasterization space.\nThis causes a logical error in the navigation mesh geometry and is commonly caused by overlap or too densely placed edges.\nConsider baking with a higher 'cell_size', greater geometry margin, and less detailed bake objects to cause fewer edges.\nConsider lowering the 'navigation/2d/merge_rasterizer_cell_scale' in the project settings.\nThis warning can be toggled under 'navigation/2d/warnings/navmesh_edge_merge_errors' in the project settings."); |
| 221 | } |
| 222 | |
| 223 | performance_data.pm_edge_free_count = free_edges_count; |
| 224 | } |
| 225 | |
| 226 | void NavRegionBuilder2D::_build_step_merge_edge_connection_pairs(NavRegionIterationBuild2D &r_build) { |
| 227 | PerformanceData &performance_data = r_build.performance_data; |
| 228 | |
| 229 | Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration; |
| 230 | |
| 231 | HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map; |
| 232 | |
| 233 | for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) { |
| 234 | const EdgeConnectionPair &pair = pair_it.value; |
| 235 | if (pair.size == 2) { |
| 236 | // Connect edge that are shared in different polygons. |
| 237 | const Connection &c1 = pair.connections[0]; |
| 238 | const Connection &c2 = pair.connections[1]; |
| 239 | region_iteration->internal_connections[c1.polygon->id].push_back(c2); |
| 240 | region_iteration->internal_connections[c2.polygon->id].push_back(c1); |
| 241 | performance_data.pm_edge_merge_count += 1; |
| 242 | |
| 243 | } else { |
| 244 | ERR_FAIL_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size)); |
| 245 | |
| 246 | const Connection &connection = pair.connections[0]; |
| 247 | |
| 248 | ConnectableEdge ce; |
| 249 | ce.ek = pair_it.key; |
| 250 | ce.polygon_index = connection.polygon->id; |
| 251 | ce.edge = connection.edge; |
| 252 | ce.pathway_start = connection.pathway_start; |
| 253 | ce.pathway_end = connection.pathway_end; |
| 254 | |
| 255 | region_iteration->external_edges.push_back(ce); |
| 256 | } |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | void NavRegionBuilder2D::_build_update_iteration(NavRegionIterationBuild2D &r_build) { |
| 261 | ERR_FAIL_NULL(r_build.region); |
| 262 | // Stub. End of the build. |
| 263 | } |