| 1 | /**************************************************************************/ |
| 2 | /* nav_mesh_queries_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 | |
| 31 | #include "nav_mesh_queries_3d.h" |
| 32 | |
| 33 | #include "../nav_base_3d.h" |
| 34 | #include "../nav_map_3d.h" |
| 35 | #include "nav_region_iteration_3d.h" |
| 36 | |
| 37 | #include "core/math/geometry_2d.h" |
| 38 | #include "core/math/geometry_3d.h" |
| 39 | |
| 40 | using namespace Nav3D; |
| 41 | |
| 42 | #define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (((m_c) - (m_a)).cross((m_b) - (m_a))) |
| 43 | |
| 44 | bool NavMeshQueries3D::emit_callback(const Callable &p_callback) { |
| 45 | ERR_FAIL_COND_V(!p_callback.is_valid(), false); |
| 46 | |
| 47 | Callable::CallError ce; |
| 48 | Variant result; |
| 49 | p_callback.callp(nullptr, 0, result, ce); |
| 50 | |
| 51 | return ce.error == Callable::CallError::CALL_OK; |
| 52 | } |
| 53 | |
| 54 | Vector3 NavMeshQueries3D::polygons_get_random_point(const LocalVector<Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly) { |
| 55 | const LocalVector<Polygon> ®ion_polygons = p_polygons; |
| 56 | |
| 57 | if (region_polygons.is_empty()) { |
| 58 | return Vector3(); |
| 59 | } |
| 60 | |
| 61 | if (p_uniformly) { |
| 62 | real_t accumulated_area = 0; |
| 63 | RBMap<real_t, uint32_t> region_area_map; |
| 64 | |
| 65 | for (uint32_t rp_index = 0; rp_index < region_polygons.size(); rp_index++) { |
| 66 | const Polygon ®ion_polygon = region_polygons[rp_index]; |
| 67 | real_t polyon_area = region_polygon.surface_area; |
| 68 | |
| 69 | if (polyon_area == 0.0) { |
| 70 | continue; |
| 71 | } |
| 72 | region_area_map[accumulated_area] = rp_index; |
| 73 | accumulated_area += polyon_area; |
| 74 | } |
| 75 | if (region_area_map.is_empty() || accumulated_area == 0) { |
| 76 | // All polygons have no real surface / no area. |
| 77 | return Vector3(); |
| 78 | } |
| 79 | |
| 80 | real_t region_area_map_pos = Math::random(real_t(0), accumulated_area); |
| 81 | |
| 82 | RBMap<real_t, uint32_t>::Iterator region_E = region_area_map.find_closest(region_area_map_pos); |
| 83 | ERR_FAIL_COND_V(!region_E, Vector3()); |
| 84 | uint32_t rrp_polygon_index = region_E->value; |
| 85 | ERR_FAIL_UNSIGNED_INDEX_V(rrp_polygon_index, region_polygons.size(), Vector3()); |
| 86 | |
| 87 | const Polygon &rr_polygon = region_polygons[rrp_polygon_index]; |
| 88 | |
| 89 | real_t accumulated_polygon_area = 0; |
| 90 | RBMap<real_t, uint32_t> polygon_area_map; |
| 91 | |
| 92 | for (uint32_t rpp_index = 2; rpp_index < rr_polygon.vertices.size(); rpp_index++) { |
| 93 | real_t face_area = Face3(rr_polygon.vertices[0], rr_polygon.vertices[rpp_index - 1], rr_polygon.vertices[rpp_index]).get_area(); |
| 94 | |
| 95 | if (face_area == 0.0) { |
| 96 | continue; |
| 97 | } |
| 98 | polygon_area_map[accumulated_polygon_area] = rpp_index; |
| 99 | accumulated_polygon_area += face_area; |
| 100 | } |
| 101 | if (polygon_area_map.is_empty() || accumulated_polygon_area == 0) { |
| 102 | // All faces have no real surface / no area. |
| 103 | return Vector3(); |
| 104 | } |
| 105 | |
| 106 | real_t polygon_area_map_pos = Math::random(real_t(0), accumulated_polygon_area); |
| 107 | |
| 108 | RBMap<real_t, uint32_t>::Iterator polygon_E = polygon_area_map.find_closest(polygon_area_map_pos); |
| 109 | ERR_FAIL_COND_V(!polygon_E, Vector3()); |
| 110 | uint32_t rrp_face_index = polygon_E->value; |
| 111 | ERR_FAIL_UNSIGNED_INDEX_V(rrp_face_index, rr_polygon.vertices.size(), Vector3()); |
| 112 | |
| 113 | const Face3 face(rr_polygon.vertices[0], rr_polygon.vertices[rrp_face_index - 1], rr_polygon.vertices[rrp_face_index]); |
| 114 | |
| 115 | Vector3 face_random_position = face.get_random_point_inside(); |
| 116 | return face_random_position; |
| 117 | |
| 118 | } else { |
| 119 | uint32_t rrp_polygon_index = Math::random(int(0), region_polygons.size() - 1); |
| 120 | |
| 121 | const Polygon &rr_polygon = region_polygons[rrp_polygon_index]; |
| 122 | |
| 123 | uint32_t rrp_face_index = Math::random(int(2), rr_polygon.vertices.size() - 1); |
| 124 | |
| 125 | const Face3 face(rr_polygon.vertices[0], rr_polygon.vertices[rrp_face_index - 1], rr_polygon.vertices[rrp_face_index]); |
| 126 | |
| 127 | Vector3 face_random_position = face.get_random_point_inside(); |
| 128 | return face_random_position; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | void NavMeshQueries3D::_query_task_push_back_point_with_metadata(NavMeshPathQueryTask3D &p_query_task, const Vector3 &p_point, const Polygon *p_point_polygon) { |
| 133 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) { |
| 134 | p_query_task.path_meta_point_types.push_back(p_point_polygon->owner->get_type()); |
| 135 | } |
| 136 | |
| 137 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) { |
| 138 | p_query_task.path_meta_point_rids.push_back(p_point_polygon->owner->get_self()); |
| 139 | } |
| 140 | |
| 141 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) { |
| 142 | p_query_task.path_meta_point_owners.push_back(p_point_polygon->owner->get_owner_id()); |
| 143 | } |
| 144 | |
| 145 | p_query_task.path_points.push_back(p_point); |
| 146 | } |
| 147 | |
| 148 | void NavMeshQueries3D::map_query_path(NavMap3D *map, const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result, const Callable &p_callback) { |
| 149 | ERR_FAIL_NULL(map); |
| 150 | ERR_FAIL_COND(p_query_parameters.is_null()); |
| 151 | ERR_FAIL_COND(p_query_result.is_null()); |
| 152 | |
| 153 | using namespace NavigationDefaults3D; |
| 154 | |
| 155 | NavMeshQueries3D::NavMeshPathQueryTask3D query_task; |
| 156 | query_task.start_position = p_query_parameters->get_start_position(); |
| 157 | query_task.target_position = p_query_parameters->get_target_position(); |
| 158 | query_task.navigation_layers = p_query_parameters->get_navigation_layers(); |
| 159 | query_task.callback = p_callback; |
| 160 | |
| 161 | const TypedArray<RID> &_excluded_regions = p_query_parameters->get_excluded_regions(); |
| 162 | const TypedArray<RID> &_included_regions = p_query_parameters->get_included_regions(); |
| 163 | |
| 164 | uint32_t _excluded_region_count = _excluded_regions.size(); |
| 165 | uint32_t _included_region_count = _included_regions.size(); |
| 166 | |
| 167 | query_task.exclude_regions = _excluded_region_count > 0; |
| 168 | query_task.include_regions = _included_region_count > 0; |
| 169 | |
| 170 | if (query_task.exclude_regions) { |
| 171 | query_task.excluded_regions.resize(_excluded_region_count); |
| 172 | for (uint32_t i = 0; i < _excluded_region_count; i++) { |
| 173 | query_task.excluded_regions[i] = _excluded_regions[i]; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | if (query_task.include_regions) { |
| 178 | query_task.included_regions.resize(_included_region_count); |
| 179 | for (uint32_t i = 0; i < _included_region_count; i++) { |
| 180 | query_task.included_regions[i] = _included_regions[i]; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | switch (p_query_parameters->get_pathfinding_algorithm()) { |
| 185 | case NavigationPathQueryParameters3D::PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR: { |
| 186 | query_task.pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR; |
| 187 | } break; |
| 188 | default: { |
| 189 | WARN_PRINT("No match for used PathfindingAlgorithm - fallback to default"); |
| 190 | query_task.pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR; |
| 191 | } break; |
| 192 | } |
| 193 | |
| 194 | switch (p_query_parameters->get_path_postprocessing()) { |
| 195 | case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL: { |
| 196 | query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL; |
| 197 | } break; |
| 198 | case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED: { |
| 199 | query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED; |
| 200 | } break; |
| 201 | case NavigationPathQueryParameters3D::PathPostProcessing::PATH_POSTPROCESSING_NONE: { |
| 202 | query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_NONE; |
| 203 | } break; |
| 204 | default: { |
| 205 | WARN_PRINT("No match for used PathPostProcessing - fallback to default"); |
| 206 | query_task.path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL; |
| 207 | } break; |
| 208 | } |
| 209 | |
| 210 | query_task.metadata_flags = (int64_t)p_query_parameters->get_metadata_flags(); |
| 211 | query_task.simplify_path = p_query_parameters->get_simplify_path(); |
| 212 | query_task.simplify_epsilon = p_query_parameters->get_simplify_epsilon(); |
| 213 | query_task.path_return_max_length = p_query_parameters->get_path_return_max_length(); |
| 214 | query_task.path_return_max_radius = p_query_parameters->get_path_return_max_radius(); |
| 215 | query_task.path_search_max_polygons = p_query_parameters->get_path_search_max_polygons(); |
| 216 | query_task.path_search_max_distance = p_query_parameters->get_path_search_max_distance(); |
| 217 | query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_STARTED; |
| 218 | |
| 219 | map->query_path(query_task); |
| 220 | |
| 221 | p_query_result->set_data( |
| 222 | query_task.path_points, |
| 223 | query_task.path_meta_point_types, |
| 224 | query_task.path_meta_point_rids, |
| 225 | query_task.path_meta_point_owners); |
| 226 | p_query_result->set_path_length(query_task.path_length); |
| 227 | |
| 228 | if (query_task.callback.is_valid()) { |
| 229 | if (emit_callback(query_task.callback)) { |
| 230 | query_task.status = NavMeshPathQueryTask3D::TaskStatus::CALLBACK_DISPATCHED; |
| 231 | } else { |
| 232 | query_task.status = NavMeshPathQueryTask3D::TaskStatus::CALLBACK_FAILED; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | void NavMeshQueries3D::_query_task_find_start_end_positions(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration) { |
| 238 | real_t begin_d = FLT_MAX; |
| 239 | real_t end_d = FLT_MAX; |
| 240 | |
| 241 | const LocalVector<Ref<NavRegionIteration3D>> ®ions = p_map_iteration.region_iterations; |
| 242 | |
| 243 | for (const Ref<NavRegionIteration3D> ®ion : regions) { |
| 244 | if (!_query_task_is_connection_owner_usable(p_query_task, region.ptr())) { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | // Find the initial poly and the end poly on this map. |
| 249 | for (const Polygon &p : region->get_navmesh_polygons()) { |
| 250 | // Only consider the polygon if it in a region with compatible layers. |
| 251 | if ((p_query_task.navigation_layers & p.owner->get_navigation_layers()) == 0) { |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | // For each face check the distance between the origin/destination. |
| 256 | for (uint32_t point_id = 2; point_id < p.vertices.size(); point_id++) { |
| 257 | const Face3 face(p.vertices[0], p.vertices[point_id - 1], p.vertices[point_id]); |
| 258 | |
| 259 | Vector3 point = face.get_closest_point_to(p_query_task.start_position); |
| 260 | real_t distance_to_point = point.distance_to(p_query_task.start_position); |
| 261 | if (distance_to_point < begin_d) { |
| 262 | begin_d = distance_to_point; |
| 263 | p_query_task.begin_polygon = &p; |
| 264 | p_query_task.begin_position = point; |
| 265 | } |
| 266 | |
| 267 | point = face.get_closest_point_to(p_query_task.target_position); |
| 268 | distance_to_point = point.distance_to(p_query_task.target_position); |
| 269 | if (distance_to_point < end_d) { |
| 270 | end_d = distance_to_point; |
| 271 | p_query_task.end_polygon = &p; |
| 272 | p_query_task.end_position = point; |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | void NavMeshQueries3D::_query_task_search_polygon_connections(NavMeshPathQueryTask3D &p_query_task, const Connection &p_connection, uint32_t p_least_cost_id, const NavigationPoly &p_least_cost_poly, real_t p_poly_enter_cost, const Vector3 &p_end_point) { |
| 280 | const NavBaseIteration3D *connection_owner = p_connection.polygon->owner; |
| 281 | ERR_FAIL_NULL(connection_owner); |
| 282 | const bool owner_is_usable = _query_task_is_connection_owner_usable(p_query_task, connection_owner); |
| 283 | if (!owner_is_usable) { |
| 284 | return; |
| 285 | } |
| 286 | |
| 287 | Heap<NavigationPoly *, NavPolyTravelCostGreaterThan, NavPolyHeapIndexer> |
| 288 | &traversable_polys = p_query_task.path_query_slot->traversable_polys; |
| 289 | LocalVector<NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 290 | |
| 291 | real_t poly_travel_cost = p_least_cost_poly.poly->owner->get_travel_cost(); |
| 292 | |
| 293 | Vector3 new_entry = Geometry3D::get_closest_point_to_segment(p_least_cost_poly.entry, p_connection.pathway_start, p_connection.pathway_end); |
| 294 | real_t new_traveled_distance = p_least_cost_poly.entry.distance_to(new_entry) * poly_travel_cost + p_poly_enter_cost + p_least_cost_poly.traveled_distance; |
| 295 | |
| 296 | // Check if the neighbor polygon has already been processed. |
| 297 | NavigationPoly &neighbor_poly = navigation_polys[p_query_task.path_query_slot->poly_to_id[p_connection.polygon]]; |
| 298 | if (new_traveled_distance < neighbor_poly.traveled_distance) { |
| 299 | // Add the polygon to the heap of polygons to traverse next. |
| 300 | neighbor_poly.back_navigation_poly_id = p_least_cost_id; |
| 301 | neighbor_poly.back_navigation_edge = p_connection.edge; |
| 302 | neighbor_poly.back_navigation_edge_pathway_start = p_connection.pathway_start; |
| 303 | neighbor_poly.back_navigation_edge_pathway_end = p_connection.pathway_end; |
| 304 | neighbor_poly.traveled_distance = new_traveled_distance; |
| 305 | neighbor_poly.distance_to_destination = |
| 306 | new_entry.distance_to(p_end_point) * |
| 307 | connection_owner->get_travel_cost(); |
| 308 | neighbor_poly.entry = new_entry; |
| 309 | |
| 310 | if (neighbor_poly.traversable_poly_index != traversable_polys.INVALID_INDEX) { |
| 311 | traversable_polys.shift(neighbor_poly.traversable_poly_index); |
| 312 | } else { |
| 313 | neighbor_poly.poly = p_connection.polygon; |
| 314 | traversable_polys.push(&neighbor_poly); |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | void NavMeshQueries3D::_query_task_build_path_corridor(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration) { |
| 320 | const Vector3 p_target_position = p_query_task.target_position; |
| 321 | const Polygon *begin_poly = p_query_task.begin_polygon; |
| 322 | const Polygon *end_poly = p_query_task.end_polygon; |
| 323 | Vector3 begin_point = p_query_task.begin_position; |
| 324 | Vector3 end_point = p_query_task.end_position; |
| 325 | |
| 326 | // Heap of polygons to travel next. |
| 327 | Heap<NavigationPoly *, NavPolyTravelCostGreaterThan, NavPolyHeapIndexer> |
| 328 | &traversable_polys = p_query_task.path_query_slot->traversable_polys; |
| 329 | traversable_polys.clear(); |
| 330 | |
| 331 | LocalVector<NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 332 | for (NavigationPoly &polygon : navigation_polys) { |
| 333 | polygon.reset(); |
| 334 | } |
| 335 | |
| 336 | // Initialize the matching navigation polygon. |
| 337 | NavigationPoly &begin_navigation_poly = navigation_polys[p_query_task.path_query_slot->poly_to_id[begin_poly]]; |
| 338 | begin_navigation_poly.poly = begin_poly; |
| 339 | begin_navigation_poly.entry = begin_point; |
| 340 | begin_navigation_poly.back_navigation_edge_pathway_start = begin_point; |
| 341 | begin_navigation_poly.back_navigation_edge_pathway_end = begin_point; |
| 342 | begin_navigation_poly.traveled_distance = 0.f; |
| 343 | |
| 344 | // This is an implementation of the A* algorithm. |
| 345 | uint32_t least_cost_id = p_query_task.path_query_slot->poly_to_id[begin_poly]; |
| 346 | bool found_route = false; |
| 347 | |
| 348 | const Polygon *reachable_end = nullptr; |
| 349 | real_t distance_to_reachable_end = FLT_MAX; |
| 350 | bool is_reachable = true; |
| 351 | real_t poly_enter_cost = 0.0; |
| 352 | |
| 353 | const HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = p_map_iteration.navbases_polygons_external_connections; |
| 354 | |
| 355 | // True if we reached the max polygon search count or distance from the begin position. |
| 356 | bool path_search_max_reached = false; |
| 357 | |
| 358 | const float path_search_max_distance_sqr = p_query_task.path_search_max_distance * p_query_task.path_search_max_distance; |
| 359 | bool has_path_search_max_distance = path_search_max_distance_sqr > 0.0; |
| 360 | |
| 361 | int processed_polygon_count = 0; |
| 362 | bool has_path_search_max_polygons = p_query_task.path_search_max_polygons > 0; |
| 363 | |
| 364 | bool has_path_search_max = p_query_task.path_search_max_polygons > 0 || path_search_max_distance_sqr > 0.0; |
| 365 | |
| 366 | while (true) { |
| 367 | const NavigationPoly &least_cost_poly = navigation_polys[least_cost_id]; |
| 368 | |
| 369 | const NavBaseIteration3D *least_cost_navbase = least_cost_poly.poly->owner; |
| 370 | |
| 371 | processed_polygon_count += 1; |
| 372 | |
| 373 | const uint32_t navbase_local_polygon_id = least_cost_poly.poly->id; |
| 374 | const LocalVector<LocalVector<Connection>> &navbase_polygons_to_connections = least_cost_poly.poly->owner->get_internal_connections(); |
| 375 | |
| 376 | if (navbase_polygons_to_connections.size() > 0) { |
| 377 | const LocalVector<Connection> &polygon_connections = navbase_polygons_to_connections[navbase_local_polygon_id]; |
| 378 | |
| 379 | for (const Connection &connection : polygon_connections) { |
| 380 | _query_task_search_polygon_connections(p_query_task, connection, least_cost_id, least_cost_poly, poly_enter_cost, end_point); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Search region external navmesh polygon connections, aka connections to other regions created by outline edge merge or links. |
| 385 | for (const Connection &connection : navbases_polygons_external_connections[least_cost_navbase][navbase_local_polygon_id]) { |
| 386 | _query_task_search_polygon_connections(p_query_task, connection, least_cost_id, least_cost_poly, poly_enter_cost, end_point); |
| 387 | } |
| 388 | |
| 389 | if (has_path_search_max && !path_search_max_reached) { |
| 390 | if (has_path_search_max_polygons && processed_polygon_count >= p_query_task.path_search_max_polygons) { |
| 391 | path_search_max_reached = true; |
| 392 | traversable_polys.clear(); |
| 393 | } else if (has_path_search_max_distance && begin_point.distance_squared_to(least_cost_poly.entry) > path_search_max_distance_sqr) { |
| 394 | path_search_max_reached = true; |
| 395 | traversable_polys.clear(); |
| 396 | } |
| 397 | } |
| 398 | |
| 399 | poly_enter_cost = 0; |
| 400 | // When the heap of traversable polygons is empty at this point it means the end polygon is |
| 401 | // unreachable. |
| 402 | if (traversable_polys.is_empty()) { |
| 403 | // Thus use the further reachable polygon |
| 404 | ERR_BREAK_MSG(is_reachable == false, "It's not expect to not find the most reachable polygons"); |
| 405 | is_reachable = false; |
| 406 | if (reachable_end == nullptr) { |
| 407 | // The path is not found and there is not a way out. |
| 408 | break; |
| 409 | } |
| 410 | |
| 411 | // Set as end point the furthest reachable point. |
| 412 | end_poly = reachable_end; |
| 413 | real_t end_d = FLT_MAX; |
| 414 | |
| 415 | for (uint32_t point_id = 2; point_id < end_poly->vertices.size(); point_id++) { |
| 416 | Face3 f(end_poly->vertices[0], end_poly->vertices[point_id - 1], end_poly->vertices[point_id]); |
| 417 | Vector3 spoint = f.get_closest_point_to(p_target_position); |
| 418 | real_t dpoint = spoint.distance_squared_to(p_target_position); |
| 419 | if (dpoint < end_d) { |
| 420 | end_point = spoint; |
| 421 | end_d = dpoint; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | // Search all faces of start polygon as well. |
| 426 | bool closest_point_on_start_poly = false; |
| 427 | |
| 428 | for (uint32_t point_id = 2; point_id < begin_poly->vertices.size(); point_id++) { |
| 429 | Face3 f(begin_poly->vertices[0], begin_poly->vertices[point_id - 1], begin_poly->vertices[point_id]); |
| 430 | Vector3 spoint = f.get_closest_point_to(p_target_position); |
| 431 | real_t dpoint = spoint.distance_squared_to(p_target_position); |
| 432 | if (dpoint < end_d) { |
| 433 | end_point = spoint; |
| 434 | end_d = dpoint; |
| 435 | closest_point_on_start_poly = true; |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | if (closest_point_on_start_poly) { |
| 440 | // No point to run PostProcessing when start and end convex polygon is the same. |
| 441 | p_query_task.path_clear(); |
| 442 | |
| 443 | _query_task_push_back_point_with_metadata(p_query_task, begin_point, begin_poly); |
| 444 | _query_task_push_back_point_with_metadata(p_query_task, end_point, begin_poly); |
| 445 | p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED; |
| 446 | return; |
| 447 | } |
| 448 | |
| 449 | for (NavigationPoly &nav_poly : navigation_polys) { |
| 450 | nav_poly.poly = nullptr; |
| 451 | nav_poly.traveled_distance = FLT_MAX; |
| 452 | } |
| 453 | uint32_t _bp_id = p_query_task.path_query_slot->poly_to_id[begin_poly]; |
| 454 | navigation_polys[_bp_id].poly = begin_poly; |
| 455 | navigation_polys[_bp_id].traveled_distance = 0; |
| 456 | least_cost_id = _bp_id; |
| 457 | reachable_end = nullptr; |
| 458 | } else { |
| 459 | // Pop the polygon with the lowest travel cost from the heap of traversable polygons. |
| 460 | least_cost_id = p_query_task.path_query_slot->poly_to_id[traversable_polys.pop()->poly]; |
| 461 | |
| 462 | // Store the farthest reachable end polygon in case our goal is not reachable. |
| 463 | if (is_reachable) { |
| 464 | real_t distance = navigation_polys[least_cost_id].entry.distance_squared_to(p_target_position); |
| 465 | if (distance_to_reachable_end > distance) { |
| 466 | distance_to_reachable_end = distance; |
| 467 | reachable_end = navigation_polys[least_cost_id].poly; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | // Check if we reached the end |
| 472 | if (navigation_polys[least_cost_id].poly == end_poly) { |
| 473 | found_route = true; |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (navigation_polys[least_cost_id].poly->owner->get_self() != least_cost_poly.poly->owner->get_self()) { |
| 478 | ERR_FAIL_NULL(least_cost_poly.poly->owner); |
| 479 | poly_enter_cost = least_cost_poly.poly->owner->get_enter_cost(); |
| 480 | } |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | // We did not find a route but we have both a start polygon and an end polygon at this point. |
| 485 | // Usually this happens because there was not a single external or internal connected edge, e.g. our start polygon is an isolated, single convex polygon. |
| 486 | if (!found_route) { |
| 487 | real_t end_d = FLT_MAX; |
| 488 | // Search all faces of the start polygon for the closest point to our target position. |
| 489 | |
| 490 | for (uint32_t point_id = 2; point_id < begin_poly->vertices.size(); point_id++) { |
| 491 | Face3 f(begin_poly->vertices[0], begin_poly->vertices[point_id - 1], begin_poly->vertices[point_id]); |
| 492 | Vector3 spoint = f.get_closest_point_to(p_target_position); |
| 493 | real_t dpoint = spoint.distance_squared_to(p_target_position); |
| 494 | if (dpoint < end_d) { |
| 495 | end_point = spoint; |
| 496 | end_d = dpoint; |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | p_query_task.path_clear(); |
| 501 | |
| 502 | _query_task_push_back_point_with_metadata(p_query_task, begin_point, begin_poly); |
| 503 | _query_task_push_back_point_with_metadata(p_query_task, end_point, begin_poly); |
| 504 | p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED; |
| 505 | } else { |
| 506 | p_query_task.end_position = end_point; |
| 507 | p_query_task.end_polygon = end_poly; |
| 508 | p_query_task.begin_position = begin_point; |
| 509 | p_query_task.begin_polygon = begin_poly; |
| 510 | p_query_task.least_cost_id = least_cost_id; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | void NavMeshQueries3D::query_task_map_iteration_get_path(NavMeshPathQueryTask3D &p_query_task, const NavMapIteration3D &p_map_iteration) { |
| 515 | p_query_task.path_clear(); |
| 516 | |
| 517 | _query_task_find_start_end_positions(p_query_task, p_map_iteration); |
| 518 | |
| 519 | // Check for trivial cases. |
| 520 | if (!p_query_task.begin_polygon || !p_query_task.end_polygon) { |
| 521 | p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED; |
| 522 | return; |
| 523 | } |
| 524 | if (p_query_task.begin_polygon == p_query_task.end_polygon) { |
| 525 | p_query_task.path_clear(); |
| 526 | _query_task_push_back_point_with_metadata(p_query_task, p_query_task.begin_position, p_query_task.begin_polygon); |
| 527 | _query_task_push_back_point_with_metadata(p_query_task, p_query_task.end_position, p_query_task.end_polygon); |
| 528 | _query_task_process_path_result_limits(p_query_task); |
| 529 | p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED; |
| 530 | return; |
| 531 | } |
| 532 | |
| 533 | _query_task_build_path_corridor(p_query_task, p_map_iteration); |
| 534 | |
| 535 | if (p_query_task.status == NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED || p_query_task.status == NavMeshPathQueryTask3D::TaskStatus::QUERY_FAILED) { |
| 536 | _query_task_process_path_result_limits(p_query_task); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | // Post-Process path. |
| 541 | switch (p_query_task.path_postprocessing) { |
| 542 | case PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL: { |
| 543 | _query_task_post_process_corridorfunnel(p_query_task); |
| 544 | } break; |
| 545 | case PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED: { |
| 546 | _query_task_post_process_edgecentered(p_query_task); |
| 547 | } break; |
| 548 | case PathPostProcessing::PATH_POSTPROCESSING_NONE: { |
| 549 | _query_task_post_process_nopostprocessing(p_query_task); |
| 550 | } break; |
| 551 | default: { |
| 552 | WARN_PRINT("No match for used PathPostProcessing - fallback to default"); |
| 553 | _query_task_post_process_corridorfunnel(p_query_task); |
| 554 | } break; |
| 555 | } |
| 556 | |
| 557 | p_query_task.path_reverse(); |
| 558 | |
| 559 | if (p_query_task.simplify_path) { |
| 560 | _query_task_simplified_path_points(p_query_task); |
| 561 | } |
| 562 | |
| 563 | _query_task_process_path_result_limits(p_query_task); |
| 564 | |
| 565 | #ifdef DEBUG_ENABLED |
| 566 | // Ensure post conditions as path meta arrays if used MUST match in array size with the path points. |
| 567 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) { |
| 568 | DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_types.size()); |
| 569 | } |
| 570 | |
| 571 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) { |
| 572 | DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_rids.size()); |
| 573 | } |
| 574 | |
| 575 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) { |
| 576 | DEV_ASSERT(p_query_task.path_points.size() == p_query_task.path_meta_point_owners.size()); |
| 577 | } |
| 578 | #endif // DEBUG_ENABLED |
| 579 | |
| 580 | p_query_task.status = NavMeshPathQueryTask3D::TaskStatus::QUERY_FINISHED; |
| 581 | } |
| 582 | |
| 583 | float NavMeshQueries3D::_calculate_path_length(const LocalVector<Vector3> &p_path, uint32_t p_start_index, uint32_t p_end_index) { |
| 584 | const uint32_t path_size = p_path.size(); |
| 585 | if (path_size < 2) { |
| 586 | return 0.0; |
| 587 | } |
| 588 | |
| 589 | ERR_FAIL_COND_V(p_start_index >= p_end_index, 0.0); |
| 590 | ERR_FAIL_COND_V(p_start_index >= path_size - 1, 0.0); |
| 591 | ERR_FAIL_COND_V(p_end_index >= path_size, 0.0); |
| 592 | |
| 593 | const Vector3 *path_ptr = p_path.ptr(); |
| 594 | |
| 595 | float path_length = 0.0; |
| 596 | |
| 597 | for (uint32_t i = p_start_index; i < p_end_index; i++) { |
| 598 | const Vector3 &vertex1 = path_ptr[i]; |
| 599 | const Vector3 &vertex2 = path_ptr[i + 1]; |
| 600 | float edge_length = vertex1.distance_to(vertex2); |
| 601 | path_length += edge_length; |
| 602 | } |
| 603 | |
| 604 | return path_length; |
| 605 | } |
| 606 | |
| 607 | void NavMeshQueries3D::_query_task_process_path_result_limits(NavMeshPathQueryTask3D &p_query_task) { |
| 608 | if (p_query_task.path_points.size() < 2) { |
| 609 | return; |
| 610 | } |
| 611 | |
| 612 | bool check_max_length = p_query_task.path_return_max_length > 0.0; |
| 613 | bool check_max_radius = p_query_task.path_return_max_radius > 0.0; |
| 614 | |
| 615 | if (!check_max_length && !check_max_radius) { |
| 616 | p_query_task.path_length = _calculate_path_length(p_query_task.path_points, 0, p_query_task.path_points.size() - 1); |
| 617 | return; |
| 618 | } |
| 619 | |
| 620 | LocalVector<Vector3> &path = p_query_task.path_points; |
| 621 | |
| 622 | const float max_length = p_query_task.path_return_max_length; |
| 623 | const float max_radius = p_query_task.path_return_max_radius; |
| 624 | const float max_radius_sqr = max_radius * max_radius; |
| 625 | |
| 626 | const Vector3 &start_pos = path[0]; |
| 627 | |
| 628 | float accumulated_path_length = 0.0; |
| 629 | |
| 630 | Vector3 *path_ptrw = path.ptr(); |
| 631 | |
| 632 | uint32_t path_max_size = path.size(); |
| 633 | bool path_max_reached = false; |
| 634 | |
| 635 | for (uint32_t i = 0; i < path.size() - 1; i++) { |
| 636 | uint32_t next_index = i + 1; |
| 637 | const Vector3 &vertex1 = path_ptrw[i]; |
| 638 | Vector3 &vertex2 = path_ptrw[next_index]; |
| 639 | |
| 640 | float edge_length = (vertex2 - vertex1).length(); |
| 641 | |
| 642 | if (check_max_radius && start_pos.distance_squared_to(vertex2) > max_radius_sqr) { |
| 643 | // Path point segment goes over max radius, clip it. |
| 644 | Vector3 intersect_positon, intersect_normal; |
| 645 | bool intersected = Geometry3D::segment_intersects_sphere(vertex2, vertex1, start_pos, max_radius, &intersect_positon, &intersect_normal); |
| 646 | if (intersected) { |
| 647 | edge_length = (intersect_positon - vertex1).length(); |
| 648 | |
| 649 | path_ptrw[next_index] = intersect_positon; |
| 650 | path_max_size = next_index + 1; |
| 651 | path_max_reached = true; |
| 652 | } |
| 653 | } |
| 654 | |
| 655 | if (check_max_length && accumulated_path_length + edge_length > max_length) { |
| 656 | // Path point segment goes over max length, clip it. |
| 657 | edge_length = max_length - accumulated_path_length; |
| 658 | Vector3 edge_direction = vertex1.direction_to(vertex2); |
| 659 | |
| 660 | path_ptrw[next_index] = vertex1 + (edge_direction * edge_length); |
| 661 | path_max_size = next_index + 1; |
| 662 | |
| 663 | p_query_task.path_length = accumulated_path_length + edge_length; |
| 664 | path_max_reached = true; |
| 665 | } |
| 666 | |
| 667 | accumulated_path_length += edge_length; |
| 668 | |
| 669 | if (path_max_reached) { |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | p_query_task.path_length = accumulated_path_length; |
| 675 | |
| 676 | if (path_max_size < path.size()) { |
| 677 | p_query_task.path_points.resize(path_max_size); |
| 678 | |
| 679 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) { |
| 680 | p_query_task.path_meta_point_types.resize(path_max_size); |
| 681 | } |
| 682 | |
| 683 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) { |
| 684 | p_query_task.path_meta_point_rids.resize(path_max_size); |
| 685 | } |
| 686 | |
| 687 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) { |
| 688 | p_query_task.path_meta_point_owners.resize(path_max_size); |
| 689 | } |
| 690 | } |
| 691 | } |
| 692 | |
| 693 | void NavMeshQueries3D::_query_task_simplified_path_points(NavMeshPathQueryTask3D &p_query_task) { |
| 694 | if (!p_query_task.simplify_path || p_query_task.path_points.size() <= 2) { |
| 695 | return; |
| 696 | } |
| 697 | |
| 698 | const LocalVector<uint32_t> &simplified_path_indices = NavMeshQueries3D::get_simplified_path_indices(p_query_task.path_points, p_query_task.simplify_epsilon); |
| 699 | |
| 700 | uint32_t index_count = simplified_path_indices.size(); |
| 701 | |
| 702 | { |
| 703 | Vector3 *points_ptr = p_query_task.path_points.ptr(); |
| 704 | for (uint32_t i = 0; i < index_count; i++) { |
| 705 | points_ptr[i] = points_ptr[simplified_path_indices[i]]; |
| 706 | } |
| 707 | p_query_task.path_points.resize(index_count); |
| 708 | } |
| 709 | |
| 710 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES)) { |
| 711 | int32_t *types_ptr = p_query_task.path_meta_point_types.ptr(); |
| 712 | for (uint32_t i = 0; i < index_count; i++) { |
| 713 | types_ptr[i] = types_ptr[simplified_path_indices[i]]; |
| 714 | } |
| 715 | p_query_task.path_meta_point_types.resize(index_count); |
| 716 | } |
| 717 | |
| 718 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS)) { |
| 719 | RID *rids_ptr = p_query_task.path_meta_point_rids.ptr(); |
| 720 | for (uint32_t i = 0; i < index_count; i++) { |
| 721 | rids_ptr[i] = rids_ptr[simplified_path_indices[i]]; |
| 722 | } |
| 723 | p_query_task.path_meta_point_rids.resize(index_count); |
| 724 | } |
| 725 | |
| 726 | if (p_query_task.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS)) { |
| 727 | int64_t *owners_ptr = p_query_task.path_meta_point_owners.ptr(); |
| 728 | for (uint32_t i = 0; i < index_count; i++) { |
| 729 | owners_ptr[i] = owners_ptr[simplified_path_indices[i]]; |
| 730 | } |
| 731 | p_query_task.path_meta_point_owners.resize(index_count); |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | void NavMeshQueries3D::_query_task_post_process_corridorfunnel(NavMeshPathQueryTask3D &p_query_task) { |
| 736 | Vector3 end_point = p_query_task.end_position; |
| 737 | const Polygon *end_poly = p_query_task.end_polygon; |
| 738 | Vector3 begin_point = p_query_task.begin_position; |
| 739 | const Polygon *begin_poly = p_query_task.begin_polygon; |
| 740 | uint32_t least_cost_id = p_query_task.least_cost_id; |
| 741 | LocalVector<NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 742 | Vector3 p_map_up = p_query_task.map_up; |
| 743 | |
| 744 | // Set the apex poly/point to the end point |
| 745 | NavigationPoly *apex_poly = &navigation_polys[least_cost_id]; |
| 746 | |
| 747 | const Vector3 back_edge_closest_point = Geometry3D::get_closest_point_to_segment(end_point, apex_poly->back_navigation_edge_pathway_start, apex_poly->back_navigation_edge_pathway_end); |
| 748 | if (end_point.is_equal_approx(back_edge_closest_point)) { |
| 749 | // The end point is basically on top of the last crossed edge, funneling around the corners would at best do nothing. |
| 750 | // At worst it would add an unwanted path point before the last point due to precision issues so skip to the next polygon. |
| 751 | if (apex_poly->back_navigation_poly_id != -1) { |
| 752 | apex_poly = &navigation_polys[apex_poly->back_navigation_poly_id]; |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | Vector3 apex_point = end_point; |
| 757 | |
| 758 | NavigationPoly *left_poly = apex_poly; |
| 759 | Vector3 left_portal = apex_point; |
| 760 | NavigationPoly *right_poly = apex_poly; |
| 761 | Vector3 right_portal = apex_point; |
| 762 | |
| 763 | NavigationPoly *p = apex_poly; |
| 764 | |
| 765 | _query_task_push_back_point_with_metadata(p_query_task, end_point, end_poly); |
| 766 | |
| 767 | while (p) { |
| 768 | // Set left and right points of the pathway between polygons. |
| 769 | Vector3 left = p->back_navigation_edge_pathway_start; |
| 770 | Vector3 right = p->back_navigation_edge_pathway_end; |
| 771 | if (THREE_POINTS_CROSS_PRODUCT(apex_point, left, right).dot(p_map_up) < 0) { |
| 772 | SWAP(left, right); |
| 773 | } |
| 774 | |
| 775 | bool skip = false; |
| 776 | if (THREE_POINTS_CROSS_PRODUCT(apex_point, left_portal, left).dot(p_map_up) >= 0) { |
| 777 | //process |
| 778 | if (left_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, left, right_portal).dot(p_map_up) > 0) { |
| 779 | left_poly = p; |
| 780 | left_portal = left; |
| 781 | } else { |
| 782 | _query_task_clip_path(p_query_task, apex_poly, right_portal, right_poly); |
| 783 | |
| 784 | apex_point = right_portal; |
| 785 | p = right_poly; |
| 786 | left_poly = p; |
| 787 | apex_poly = p; |
| 788 | left_portal = apex_point; |
| 789 | right_portal = apex_point; |
| 790 | |
| 791 | _query_task_push_back_point_with_metadata(p_query_task, apex_point, apex_poly->poly); |
| 792 | skip = true; |
| 793 | } |
| 794 | } |
| 795 | |
| 796 | if (!skip && THREE_POINTS_CROSS_PRODUCT(apex_point, right_portal, right).dot(p_map_up) <= 0) { |
| 797 | //process |
| 798 | if (right_portal == apex_point || THREE_POINTS_CROSS_PRODUCT(apex_point, right, left_portal).dot(p_map_up) < 0) { |
| 799 | right_poly = p; |
| 800 | right_portal = right; |
| 801 | } else { |
| 802 | _query_task_clip_path(p_query_task, apex_poly, left_portal, left_poly); |
| 803 | |
| 804 | apex_point = left_portal; |
| 805 | p = left_poly; |
| 806 | right_poly = p; |
| 807 | apex_poly = p; |
| 808 | right_portal = apex_point; |
| 809 | left_portal = apex_point; |
| 810 | |
| 811 | _query_task_push_back_point_with_metadata(p_query_task, apex_point, apex_poly->poly); |
| 812 | } |
| 813 | } |
| 814 | |
| 815 | // Go to the previous polygon. |
| 816 | if (p->back_navigation_poly_id != -1) { |
| 817 | p = &navigation_polys[p->back_navigation_poly_id]; |
| 818 | } else { |
| 819 | // The end |
| 820 | p = nullptr; |
| 821 | } |
| 822 | } |
| 823 | |
| 824 | // If the last point is not the begin point, add it to the list. |
| 825 | if (p_query_task.path_points[p_query_task.path_points.size() - 1] != begin_point) { |
| 826 | _query_task_push_back_point_with_metadata(p_query_task, begin_point, begin_poly); |
| 827 | } |
| 828 | } |
| 829 | |
| 830 | void NavMeshQueries3D::_query_task_post_process_edgecentered(NavMeshPathQueryTask3D &p_query_task) { |
| 831 | Vector3 end_point = p_query_task.end_position; |
| 832 | const Polygon *end_poly = p_query_task.end_polygon; |
| 833 | Vector3 begin_point = p_query_task.begin_position; |
| 834 | const Polygon *begin_poly = p_query_task.begin_polygon; |
| 835 | uint32_t least_cost_id = p_query_task.least_cost_id; |
| 836 | LocalVector<NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 837 | |
| 838 | _query_task_push_back_point_with_metadata(p_query_task, end_point, end_poly); |
| 839 | |
| 840 | // Add mid points |
| 841 | int np_id = least_cost_id; |
| 842 | while (np_id != -1 && navigation_polys[np_id].back_navigation_poly_id != -1) { |
| 843 | if (navigation_polys[np_id].back_navigation_edge != -1) { |
| 844 | int prev = navigation_polys[np_id].back_navigation_edge; |
| 845 | int prev_n = (navigation_polys[np_id].back_navigation_edge + 1) % navigation_polys[np_id].poly->vertices.size(); |
| 846 | Vector3 point = (navigation_polys[np_id].poly->vertices[prev] + navigation_polys[np_id].poly->vertices[prev_n]) * 0.5; |
| 847 | |
| 848 | _query_task_push_back_point_with_metadata(p_query_task, point, navigation_polys[np_id].poly); |
| 849 | } else { |
| 850 | _query_task_push_back_point_with_metadata(p_query_task, navigation_polys[np_id].entry, navigation_polys[np_id].poly); |
| 851 | } |
| 852 | |
| 853 | np_id = navigation_polys[np_id].back_navigation_poly_id; |
| 854 | } |
| 855 | |
| 856 | _query_task_push_back_point_with_metadata(p_query_task, begin_point, begin_poly); |
| 857 | } |
| 858 | |
| 859 | void NavMeshQueries3D::_query_task_post_process_nopostprocessing(NavMeshPathQueryTask3D &p_query_task) { |
| 860 | Vector3 end_point = p_query_task.end_position; |
| 861 | const Polygon *end_poly = p_query_task.end_polygon; |
| 862 | Vector3 begin_point = p_query_task.begin_position; |
| 863 | const Polygon *begin_poly = p_query_task.begin_polygon; |
| 864 | uint32_t least_cost_id = p_query_task.least_cost_id; |
| 865 | LocalVector<NavigationPoly> &navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 866 | |
| 867 | _query_task_push_back_point_with_metadata(p_query_task, end_point, end_poly); |
| 868 | |
| 869 | // Add mid points |
| 870 | int np_id = least_cost_id; |
| 871 | while (np_id != -1 && navigation_polys[np_id].back_navigation_poly_id != -1) { |
| 872 | _query_task_push_back_point_with_metadata(p_query_task, navigation_polys[np_id].entry, navigation_polys[np_id].poly); |
| 873 | |
| 874 | np_id = navigation_polys[np_id].back_navigation_poly_id; |
| 875 | } |
| 876 | |
| 877 | _query_task_push_back_point_with_metadata(p_query_task, begin_point, begin_poly); |
| 878 | } |
| 879 | |
| 880 | Vector3 NavMeshQueries3D::map_iteration_get_closest_point_to_segment(const NavMapIteration3D &p_map_iteration, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) { |
| 881 | bool use_collision = p_use_collision; |
| 882 | Vector3 closest_point; |
| 883 | real_t closest_point_distance = FLT_MAX; |
| 884 | |
| 885 | const LocalVector<Ref<NavRegionIteration3D>> ®ions = p_map_iteration.region_iterations; |
| 886 | for (const Ref<NavRegionIteration3D> ®ion : regions) { |
| 887 | for (const Polygon &polygon : region->get_navmesh_polygons()) { |
| 888 | // For each face check the distance to the segment. |
| 889 | for (uint32_t point_id = 2; point_id < polygon.vertices.size(); point_id += 1) { |
| 890 | const Face3 face(polygon.vertices[0], polygon.vertices[point_id - 1], polygon.vertices[point_id]); |
| 891 | Vector3 intersection_point; |
| 892 | if (face.intersects_segment(p_from, p_to, &intersection_point)) { |
| 893 | const real_t d = p_from.distance_to(intersection_point); |
| 894 | if (!use_collision) { |
| 895 | closest_point = intersection_point; |
| 896 | use_collision = true; |
| 897 | closest_point_distance = d; |
| 898 | } else if (closest_point_distance > d) { |
| 899 | closest_point = intersection_point; |
| 900 | closest_point_distance = d; |
| 901 | } |
| 902 | } |
| 903 | // If segment does not itersect face, check the distance from segment's endpoints. |
| 904 | else if (!use_collision) { |
| 905 | const Vector3 p_from_closest = face.get_closest_point_to(p_from); |
| 906 | const real_t d_p_from = p_from.distance_to(p_from_closest); |
| 907 | if (closest_point_distance > d_p_from) { |
| 908 | closest_point = p_from_closest; |
| 909 | closest_point_distance = d_p_from; |
| 910 | } |
| 911 | |
| 912 | const Vector3 p_to_closest = face.get_closest_point_to(p_to); |
| 913 | const real_t d_p_to = p_to.distance_to(p_to_closest); |
| 914 | if (closest_point_distance > d_p_to) { |
| 915 | closest_point = p_to_closest; |
| 916 | closest_point_distance = d_p_to; |
| 917 | } |
| 918 | } |
| 919 | } |
| 920 | // Finally, check for a case when shortest distance is between some point located on a face's edge and some point located on a line segment. |
| 921 | if (!use_collision) { |
| 922 | for (uint32_t point_id = 0; point_id < polygon.vertices.size(); point_id += 1) { |
| 923 | Vector3 a, b; |
| 924 | |
| 925 | Geometry3D::get_closest_points_between_segments( |
| 926 | p_from, |
| 927 | p_to, |
| 928 | polygon.vertices[point_id], |
| 929 | polygon.vertices[(point_id + 1) % polygon.vertices.size()], |
| 930 | a, |
| 931 | b); |
| 932 | |
| 933 | const real_t d = a.distance_to(b); |
| 934 | if (d < closest_point_distance) { |
| 935 | closest_point_distance = d; |
| 936 | closest_point = b; |
| 937 | } |
| 938 | } |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | |
| 943 | return closest_point; |
| 944 | } |
| 945 | |
| 946 | Vector3 NavMeshQueries3D::map_iteration_get_closest_point(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point) { |
| 947 | ClosestPointQueryResult cp = map_iteration_get_closest_point_info(p_map_iteration, p_point); |
| 948 | return cp.point; |
| 949 | } |
| 950 | |
| 951 | Vector3 NavMeshQueries3D::map_iteration_get_closest_point_normal(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point) { |
| 952 | ClosestPointQueryResult cp = map_iteration_get_closest_point_info(p_map_iteration, p_point); |
| 953 | return cp.normal; |
| 954 | } |
| 955 | |
| 956 | RID NavMeshQueries3D::map_iteration_get_closest_point_owner(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point) { |
| 957 | ClosestPointQueryResult cp = map_iteration_get_closest_point_info(p_map_iteration, p_point); |
| 958 | return cp.owner; |
| 959 | } |
| 960 | |
| 961 | ClosestPointQueryResult NavMeshQueries3D::map_iteration_get_closest_point_info(const NavMapIteration3D &p_map_iteration, const Vector3 &p_point) { |
| 962 | ClosestPointQueryResult result; |
| 963 | real_t closest_point_distance_squared = FLT_MAX; |
| 964 | |
| 965 | const LocalVector<Ref<NavRegionIteration3D>> ®ions = p_map_iteration.region_iterations; |
| 966 | for (const Ref<NavRegionIteration3D> ®ion : regions) { |
| 967 | for (const Polygon &polygon : region->get_navmesh_polygons()) { |
| 968 | Vector3 plane_normal = (polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]); |
| 969 | Vector3 closest_on_polygon; |
| 970 | real_t closest = FLT_MAX; |
| 971 | bool inside = true; |
| 972 | Vector3 previous = polygon.vertices[polygon.vertices.size() - 1]; |
| 973 | for (uint32_t point_id = 0; point_id < polygon.vertices.size(); ++point_id) { |
| 974 | Vector3 edge = polygon.vertices[point_id] - previous; |
| 975 | Vector3 to_point = p_point - previous; |
| 976 | Vector3 edge_to_point_pormal = edge.cross(to_point); |
| 977 | bool clockwise = edge_to_point_pormal.dot(plane_normal) > 0; |
| 978 | // If we are not clockwise, the point will never be inside the polygon and so the closest point will be on an edge. |
| 979 | if (!clockwise) { |
| 980 | inside = false; |
| 981 | real_t point_projected_on_edge = edge.dot(to_point); |
| 982 | real_t edge_square = edge.length_squared(); |
| 983 | |
| 984 | if (point_projected_on_edge > edge_square) { |
| 985 | real_t distance = polygon.vertices[point_id].distance_squared_to(p_point); |
| 986 | if (distance < closest) { |
| 987 | closest_on_polygon = polygon.vertices[point_id]; |
| 988 | closest = distance; |
| 989 | } |
| 990 | } else if (point_projected_on_edge < 0.f) { |
| 991 | real_t distance = previous.distance_squared_to(p_point); |
| 992 | if (distance < closest) { |
| 993 | closest_on_polygon = previous; |
| 994 | closest = distance; |
| 995 | } |
| 996 | } else { |
| 997 | // If we project on this edge, this will be the closest point. |
| 998 | real_t percent = point_projected_on_edge / edge_square; |
| 999 | closest_on_polygon = previous + percent * edge; |
| 1000 | break; |
| 1001 | } |
| 1002 | } |
| 1003 | previous = polygon.vertices[point_id]; |
| 1004 | } |
| 1005 | |
| 1006 | if (inside) { |
| 1007 | Vector3 plane_normalized = plane_normal.normalized(); |
| 1008 | real_t distance = plane_normalized.dot(p_point - polygon.vertices[0]); |
| 1009 | real_t distance_squared = distance * distance; |
| 1010 | if (distance_squared < closest_point_distance_squared) { |
| 1011 | closest_point_distance_squared = distance_squared; |
| 1012 | result.point = p_point - plane_normalized * distance; |
| 1013 | result.normal = plane_normal; |
| 1014 | result.owner = polygon.owner->get_self(); |
| 1015 | |
| 1016 | if (Math::is_zero_approx(distance)) { |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | } else { |
| 1021 | real_t distance = closest_on_polygon.distance_squared_to(p_point); |
| 1022 | if (distance < closest_point_distance_squared) { |
| 1023 | closest_point_distance_squared = distance; |
| 1024 | result.point = closest_on_polygon; |
| 1025 | result.normal = plane_normal; |
| 1026 | result.owner = polygon.owner->get_self(); |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | return result; |
| 1033 | } |
| 1034 | |
| 1035 | Vector3 NavMeshQueries3D::map_iteration_get_random_point(const NavMapIteration3D &p_map_iteration, uint32_t p_navigation_layers, bool p_uniformly) { |
| 1036 | if (p_map_iteration.region_iterations.is_empty()) { |
| 1037 | return Vector3(); |
| 1038 | } |
| 1039 | |
| 1040 | LocalVector<uint32_t> accessible_regions; |
| 1041 | accessible_regions.reserve(p_map_iteration.region_iterations.size()); |
| 1042 | |
| 1043 | for (uint32_t i = 0; i < p_map_iteration.region_iterations.size(); i++) { |
| 1044 | const Ref<NavRegionIteration3D> ®ion = p_map_iteration.region_iterations[i]; |
| 1045 | if (!region->get_enabled() || (p_navigation_layers & region->get_navigation_layers()) == 0) { |
| 1046 | continue; |
| 1047 | } |
| 1048 | accessible_regions.push_back(i); |
| 1049 | } |
| 1050 | |
| 1051 | if (accessible_regions.is_empty()) { |
| 1052 | // All existing region polygons are disabled. |
| 1053 | return Vector3(); |
| 1054 | } |
| 1055 | |
| 1056 | if (p_uniformly) { |
| 1057 | real_t accumulated_region_surface_area = 0; |
| 1058 | RBMap<real_t, uint32_t> accessible_regions_area_map; |
| 1059 | |
| 1060 | for (uint32_t accessible_region_index = 0; accessible_region_index < accessible_regions.size(); accessible_region_index++) { |
| 1061 | const Ref<NavRegionIteration3D> ®ion = p_map_iteration.region_iterations[accessible_regions[accessible_region_index]]; |
| 1062 | |
| 1063 | real_t region_surface_area = region->surface_area; |
| 1064 | |
| 1065 | if (region_surface_area == 0.0f) { |
| 1066 | continue; |
| 1067 | } |
| 1068 | |
| 1069 | accessible_regions_area_map[accumulated_region_surface_area] = accessible_region_index; |
| 1070 | accumulated_region_surface_area += region_surface_area; |
| 1071 | } |
| 1072 | if (accessible_regions_area_map.is_empty() || accumulated_region_surface_area == 0) { |
| 1073 | // All faces have no real surface / no area. |
| 1074 | return Vector3(); |
| 1075 | } |
| 1076 | |
| 1077 | real_t random_accessible_regions_area_map = Math::random(real_t(0), accumulated_region_surface_area); |
| 1078 | |
| 1079 | RBMap<real_t, uint32_t>::Iterator E = accessible_regions_area_map.find_closest(random_accessible_regions_area_map); |
| 1080 | ERR_FAIL_COND_V(!E, Vector3()); |
| 1081 | uint32_t random_region_index = E->value; |
| 1082 | ERR_FAIL_UNSIGNED_INDEX_V(random_region_index, accessible_regions.size(), Vector3()); |
| 1083 | |
| 1084 | const Ref<NavRegionIteration3D> &random_region = p_map_iteration.region_iterations[accessible_regions[random_region_index]]; |
| 1085 | |
| 1086 | return NavMeshQueries3D::polygons_get_random_point(random_region->navmesh_polygons, p_navigation_layers, p_uniformly); |
| 1087 | |
| 1088 | } else { |
| 1089 | uint32_t random_region_index = Math::random(int(0), accessible_regions.size() - 1); |
| 1090 | |
| 1091 | const Ref<NavRegionIteration3D> &random_region = p_map_iteration.region_iterations[accessible_regions[random_region_index]]; |
| 1092 | |
| 1093 | return NavMeshQueries3D::polygons_get_random_point(random_region->navmesh_polygons, p_navigation_layers, p_uniformly); |
| 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | Vector3 NavMeshQueries3D::polygons_get_closest_point_to_segment(const LocalVector<Polygon> &p_polygons, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) { |
| 1098 | bool use_collision = p_use_collision; |
| 1099 | Vector3 closest_point; |
| 1100 | real_t closest_point_distance = FLT_MAX; |
| 1101 | |
| 1102 | for (const Polygon &polygon : p_polygons) { |
| 1103 | // For each face check the distance to the segment. |
| 1104 | for (uint32_t point_id = 2; point_id < polygon.vertices.size(); point_id += 1) { |
| 1105 | const Face3 face(polygon.vertices[0], polygon.vertices[point_id - 1], polygon.vertices[point_id]); |
| 1106 | Vector3 intersection_point; |
| 1107 | if (face.intersects_segment(p_from, p_to, &intersection_point)) { |
| 1108 | const real_t d = p_from.distance_to(intersection_point); |
| 1109 | if (!use_collision) { |
| 1110 | closest_point = intersection_point; |
| 1111 | use_collision = true; |
| 1112 | closest_point_distance = d; |
| 1113 | } else if (closest_point_distance > d) { |
| 1114 | closest_point = intersection_point; |
| 1115 | closest_point_distance = d; |
| 1116 | } |
| 1117 | } |
| 1118 | // If segment does not itersect face, check the distance from segment's endpoints. |
| 1119 | else if (!use_collision) { |
| 1120 | const Vector3 p_from_closest = face.get_closest_point_to(p_from); |
| 1121 | const real_t d_p_from = p_from.distance_to(p_from_closest); |
| 1122 | if (closest_point_distance > d_p_from) { |
| 1123 | closest_point = p_from_closest; |
| 1124 | closest_point_distance = d_p_from; |
| 1125 | } |
| 1126 | |
| 1127 | const Vector3 p_to_closest = face.get_closest_point_to(p_to); |
| 1128 | const real_t d_p_to = p_to.distance_to(p_to_closest); |
| 1129 | if (closest_point_distance > d_p_to) { |
| 1130 | closest_point = p_to_closest; |
| 1131 | closest_point_distance = d_p_to; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | // Finally, check for a case when shortest distance is between some point located on a face's edge and some point located on a line segment. |
| 1136 | if (!use_collision) { |
| 1137 | for (uint32_t point_id = 0; point_id < polygon.vertices.size(); point_id += 1) { |
| 1138 | Vector3 a, b; |
| 1139 | |
| 1140 | Geometry3D::get_closest_points_between_segments( |
| 1141 | p_from, |
| 1142 | p_to, |
| 1143 | polygon.vertices[point_id], |
| 1144 | polygon.vertices[(point_id + 1) % polygon.vertices.size()], |
| 1145 | a, |
| 1146 | b); |
| 1147 | |
| 1148 | const real_t d = a.distance_to(b); |
| 1149 | if (d < closest_point_distance) { |
| 1150 | closest_point_distance = d; |
| 1151 | closest_point = b; |
| 1152 | } |
| 1153 | } |
| 1154 | } |
| 1155 | } |
| 1156 | |
| 1157 | return closest_point; |
| 1158 | } |
| 1159 | |
| 1160 | Vector3 NavMeshQueries3D::polygons_get_closest_point(const LocalVector<Polygon> &p_polygons, const Vector3 &p_point) { |
| 1161 | ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point); |
| 1162 | return cp.point; |
| 1163 | } |
| 1164 | |
| 1165 | Vector3 NavMeshQueries3D::polygons_get_closest_point_normal(const LocalVector<Polygon> &p_polygons, const Vector3 &p_point) { |
| 1166 | ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point); |
| 1167 | return cp.normal; |
| 1168 | } |
| 1169 | |
| 1170 | ClosestPointQueryResult NavMeshQueries3D::polygons_get_closest_point_info(const LocalVector<Polygon> &p_polygons, const Vector3 &p_point) { |
| 1171 | ClosestPointQueryResult result; |
| 1172 | real_t closest_point_distance_squared = FLT_MAX; |
| 1173 | |
| 1174 | for (const Polygon &polygon : p_polygons) { |
| 1175 | Vector3 plane_normal = (polygon.vertices[1] - polygon.vertices[0]).cross(polygon.vertices[2] - polygon.vertices[0]); |
| 1176 | Vector3 closest_on_polygon; |
| 1177 | real_t closest = FLT_MAX; |
| 1178 | bool inside = true; |
| 1179 | Vector3 previous = polygon.vertices[polygon.vertices.size() - 1]; |
| 1180 | for (uint32_t point_id = 0; point_id < polygon.vertices.size(); ++point_id) { |
| 1181 | Vector3 edge = polygon.vertices[point_id] - previous; |
| 1182 | Vector3 to_point = p_point - previous; |
| 1183 | Vector3 edge_to_point_pormal = edge.cross(to_point); |
| 1184 | bool clockwise = edge_to_point_pormal.dot(plane_normal) > 0; |
| 1185 | // If we are not clockwise, the point will never be inside the polygon and so the closest point will be on an edge. |
| 1186 | if (!clockwise) { |
| 1187 | inside = false; |
| 1188 | real_t point_projected_on_edge = edge.dot(to_point); |
| 1189 | real_t edge_square = edge.length_squared(); |
| 1190 | |
| 1191 | if (point_projected_on_edge > edge_square) { |
| 1192 | real_t distance = polygon.vertices[point_id].distance_squared_to(p_point); |
| 1193 | if (distance < closest) { |
| 1194 | closest_on_polygon = polygon.vertices[point_id]; |
| 1195 | closest = distance; |
| 1196 | } |
| 1197 | } else if (point_projected_on_edge < 0.f) { |
| 1198 | real_t distance = previous.distance_squared_to(p_point); |
| 1199 | if (distance < closest) { |
| 1200 | closest_on_polygon = previous; |
| 1201 | closest = distance; |
| 1202 | } |
| 1203 | } else { |
| 1204 | // If we project on this edge, this will be the closest point. |
| 1205 | real_t percent = point_projected_on_edge / edge_square; |
| 1206 | closest_on_polygon = previous + percent * edge; |
| 1207 | break; |
| 1208 | } |
| 1209 | } |
| 1210 | previous = polygon.vertices[point_id]; |
| 1211 | } |
| 1212 | |
| 1213 | if (inside) { |
| 1214 | Vector3 plane_normalized = plane_normal.normalized(); |
| 1215 | real_t distance = plane_normalized.dot(p_point - polygon.vertices[0]); |
| 1216 | real_t distance_squared = distance * distance; |
| 1217 | if (distance_squared < closest_point_distance_squared) { |
| 1218 | closest_point_distance_squared = distance_squared; |
| 1219 | result.point = p_point - plane_normalized * distance; |
| 1220 | result.normal = plane_normal; |
| 1221 | result.owner = polygon.owner->get_self(); |
| 1222 | |
| 1223 | if (Math::is_zero_approx(distance)) { |
| 1224 | break; |
| 1225 | } |
| 1226 | } |
| 1227 | } else { |
| 1228 | real_t distance = closest_on_polygon.distance_squared_to(p_point); |
| 1229 | if (distance < closest_point_distance_squared) { |
| 1230 | closest_point_distance_squared = distance; |
| 1231 | result.point = closest_on_polygon; |
| 1232 | result.normal = plane_normal; |
| 1233 | result.owner = polygon.owner->get_self(); |
| 1234 | } |
| 1235 | } |
| 1236 | } |
| 1237 | |
| 1238 | return result; |
| 1239 | } |
| 1240 | |
| 1241 | RID NavMeshQueries3D::polygons_get_closest_point_owner(const LocalVector<Polygon> &p_polygons, const Vector3 &p_point) { |
| 1242 | ClosestPointQueryResult cp = polygons_get_closest_point_info(p_polygons, p_point); |
| 1243 | return cp.owner; |
| 1244 | } |
| 1245 | |
| 1246 | void NavMeshQueries3D::_query_task_clip_path(NavMeshPathQueryTask3D &p_query_task, const NavigationPoly *from_poly, const Vector3 &p_to_point, const NavigationPoly *p_to_poly) { |
| 1247 | Vector3 from = p_query_task.path_points[p_query_task.path_points.size() - 1]; |
| 1248 | const LocalVector<NavigationPoly> &p_navigation_polys = p_query_task.path_query_slot->path_corridor; |
| 1249 | const Vector3 p_map_up = p_query_task.map_up; |
| 1250 | |
| 1251 | if (from.is_equal_approx(p_to_point)) { |
| 1252 | return; |
| 1253 | } |
| 1254 | |
| 1255 | Plane cut_plane; |
| 1256 | cut_plane.normal = (from - p_to_point).cross(p_map_up); |
| 1257 | if (cut_plane.normal == Vector3()) { |
| 1258 | return; |
| 1259 | } |
| 1260 | cut_plane.normal.normalize(); |
| 1261 | cut_plane.d = cut_plane.normal.dot(from); |
| 1262 | |
| 1263 | while (from_poly != p_to_poly) { |
| 1264 | Vector3 pathway_start = from_poly->back_navigation_edge_pathway_start; |
| 1265 | Vector3 pathway_end = from_poly->back_navigation_edge_pathway_end; |
| 1266 | |
| 1267 | ERR_FAIL_COND(from_poly->back_navigation_poly_id == -1); |
| 1268 | from_poly = &p_navigation_polys[from_poly->back_navigation_poly_id]; |
| 1269 | |
| 1270 | if (!pathway_start.is_equal_approx(pathway_end)) { |
| 1271 | Vector3 inters; |
| 1272 | if (cut_plane.intersects_segment(pathway_start, pathway_end, &inters)) { |
| 1273 | if (!inters.is_equal_approx(p_to_point) && !inters.is_equal_approx(p_query_task.path_points[p_query_task.path_points.size() - 1])) { |
| 1274 | _query_task_push_back_point_with_metadata(p_query_task, inters, from_poly->poly); |
| 1275 | } |
| 1276 | } |
| 1277 | } |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | bool NavMeshQueries3D::_query_task_is_connection_owner_usable(const NavMeshPathQueryTask3D &p_query_task, const NavBaseIteration3D *p_owner) { |
| 1282 | ERR_FAIL_NULL_V(p_owner, false); |
| 1283 | |
| 1284 | bool owner_usable = true; |
| 1285 | |
| 1286 | if (!p_owner->get_enabled()) { |
| 1287 | owner_usable = false; |
| 1288 | return owner_usable; |
| 1289 | } |
| 1290 | |
| 1291 | if ((p_query_task.navigation_layers & p_owner->get_navigation_layers()) == 0) { |
| 1292 | // Not usable. No matching bit between task filter bitmask and owner bitmask. |
| 1293 | owner_usable = false; |
| 1294 | return owner_usable; |
| 1295 | } |
| 1296 | |
| 1297 | if (p_query_task.exclude_regions || p_query_task.include_regions) { |
| 1298 | switch (p_owner->get_type()) { |
| 1299 | case NavigationEnums3D::PathSegmentType::PATH_SEGMENT_TYPE_REGION: { |
| 1300 | if (p_query_task.exclude_regions && p_query_task.excluded_regions.has(p_owner->get_self())) { |
| 1301 | // Not usable. Exclude region filter is active and this region is excluded. |
| 1302 | owner_usable = false; |
| 1303 | } else if (p_query_task.include_regions && !p_query_task.included_regions.has(p_owner->get_self())) { |
| 1304 | // Not usable. Include region filter is active and this region is not included. |
| 1305 | owner_usable = false; |
| 1306 | } |
| 1307 | } break; |
| 1308 | case NavigationEnums3D::PathSegmentType::PATH_SEGMENT_TYPE_LINK: { |
| 1309 | const LocalVector<Polygon> &link_polygons = p_owner->get_navmesh_polygons(); |
| 1310 | if (link_polygons.size() != 2) { |
| 1311 | // Not usable. Whatever this is, it is not a valid connected link. |
| 1312 | owner_usable = false; |
| 1313 | } else { |
| 1314 | const RID link_start_region = link_polygons[0].owner->get_self(); |
| 1315 | const RID link_end_region = link_polygons[1].owner->get_self(); |
| 1316 | if (p_query_task.exclude_regions && (p_query_task.excluded_regions.has(link_start_region) || p_query_task.excluded_regions.has(link_end_region))) { |
| 1317 | // Not usable. Exclude region filter is active and at least one region of the link is excluded. |
| 1318 | owner_usable = false; |
| 1319 | } |
| 1320 | if (p_query_task.include_regions && (!p_query_task.included_regions.has(link_start_region) || !p_query_task.excluded_regions.has(link_end_region))) { |
| 1321 | // Not usable. Include region filter is active and not both regions of the links are included. |
| 1322 | owner_usable = false; |
| 1323 | } |
| 1324 | } |
| 1325 | } break; |
| 1326 | } |
| 1327 | } |
| 1328 | |
| 1329 | return owner_usable; |
| 1330 | } |
| 1331 | |
| 1332 | LocalVector<uint32_t> NavMeshQueries3D::get_simplified_path_indices(const LocalVector<Vector3> &p_path, real_t p_epsilon) { |
| 1333 | p_epsilon = MAX(0.0, p_epsilon); |
| 1334 | real_t squared_epsilon = p_epsilon * p_epsilon; |
| 1335 | |
| 1336 | LocalVector<uint32_t> simplified_path_indices; |
| 1337 | simplified_path_indices.reserve(p_path.size()); |
| 1338 | simplified_path_indices.push_back(0); |
| 1339 | simplify_path_segment(0, p_path.size() - 1, p_path, squared_epsilon, simplified_path_indices); |
| 1340 | simplified_path_indices.push_back(p_path.size() - 1); |
| 1341 | |
| 1342 | return simplified_path_indices; |
| 1343 | } |
| 1344 | |
| 1345 | void NavMeshQueries3D::simplify_path_segment(int p_start_inx, int p_end_inx, const LocalVector<Vector3> &p_points, real_t p_epsilon, LocalVector<uint32_t> &r_simplified_path_indices) { |
| 1346 | const Vector3 path_segment_a = p_points[p_start_inx]; |
| 1347 | const Vector3 path_segment_b = p_points[p_end_inx]; |
| 1348 | |
| 1349 | real_t point_max_distance = 0.0; |
| 1350 | int point_max_index = 0; |
| 1351 | |
| 1352 | for (int i = p_start_inx; i < p_end_inx; i++) { |
| 1353 | const Vector3 &checked_point = p_points[i]; |
| 1354 | |
| 1355 | const Vector3 closest_point = Geometry3D::get_closest_point_to_segment(checked_point, path_segment_a, path_segment_b); |
| 1356 | real_t distance_squared = closest_point.distance_squared_to(checked_point); |
| 1357 | |
| 1358 | if (distance_squared > point_max_distance) { |
| 1359 | point_max_index = i; |
| 1360 | point_max_distance = distance_squared; |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | if (point_max_distance > p_epsilon) { |
| 1365 | simplify_path_segment(p_start_inx, point_max_index, p_points, p_epsilon, r_simplified_path_indices); |
| 1366 | r_simplified_path_indices.push_back(point_max_index); |
| 1367 | simplify_path_segment(point_max_index, p_end_inx, p_points, p_epsilon, r_simplified_path_indices); |
| 1368 | } |
| 1369 | } |