0.2.0-dev Download ZIP
531 lines ·21.0 KB Raw
| 1 | /**************************************************************************/ |
| 2 | /* nav_mesh_generator_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 | #ifdef CLIPPER2_ENABLED |
| 32 | |
| 33 | #include "nav_mesh_generator_2d.h" |
| 34 | |
| 35 | #include "core/config/project_settings.h" |
| 36 | #include "scene/2d/node_2d.h" |
| 37 | #include "scene/main/node.h" |
| 38 | #include "scene/main/scene_tree.h" |
| 39 | #include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h" |
| 40 | #include "scene/resources/2d/navigation_polygon.h" |
| 41 | |
| 42 | #include <thirdparty/clipper2/include/clipper2/clipper.h> |
| 43 | #include <thirdparty/misc/polypartition.h> |
| 44 | |
| 45 | NavMeshGenerator2D *NavMeshGenerator2D::singleton = nullptr; |
| 46 | Mutex NavMeshGenerator2D::baking_navmesh_mutex; |
| 47 | Mutex NavMeshGenerator2D::generator_task_mutex; |
| 48 | RWLock NavMeshGenerator2D::generator_parsers_rwlock; |
| 49 | bool NavMeshGenerator2D::use_threads = true; |
| 50 | bool NavMeshGenerator2D::baking_use_multiple_threads = true; |
| 51 | bool NavMeshGenerator2D::baking_use_high_priority_threads = true; |
| 52 | HashSet<Ref<NavigationPolygon>> NavMeshGenerator2D::baking_navmeshes; |
| 53 | HashMap<WorkerThreadPool::TaskID, NavMeshGenerator2D::NavMeshGeneratorTask2D *> NavMeshGenerator2D::generator_tasks; |
| 54 | LocalVector<NavMeshGeometryParser2D *> NavMeshGenerator2D::generator_parsers; |
| 55 | |
| 56 | NavMeshGenerator2D *NavMeshGenerator2D::get_singleton() { |
| 57 | return singleton; |
| 58 | } |
| 59 | |
| 60 | NavMeshGenerator2D::NavMeshGenerator2D() { |
| 61 | ERR_FAIL_COND(singleton != nullptr); |
| 62 | singleton = this; |
| 63 | |
| 64 | baking_use_multiple_threads = GLOBAL_GET("navigation/baking/thread_model/baking_use_multiple_threads"); |
| 65 | baking_use_high_priority_threads = GLOBAL_GET("navigation/baking/thread_model/baking_use_high_priority_threads"); |
| 66 | |
| 67 | // Using threads might cause problems on certain exports or with the Editor on certain devices. |
| 68 | // This is the main switch to turn threaded navmesh baking off should the need arise. |
| 69 | use_threads = baking_use_multiple_threads; |
| 70 | } |
| 71 | |
| 72 | NavMeshGenerator2D::~NavMeshGenerator2D() { |
| 73 | cleanup(); |
| 74 | } |
| 75 | |
| 76 | void NavMeshGenerator2D::sync() { |
| 77 | if (generator_tasks.is_empty()) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | MutexLock baking_navmesh_lock(baking_navmesh_mutex); |
| 82 | { |
| 83 | MutexLock generator_task_lock(generator_task_mutex); |
| 84 | |
| 85 | LocalVector<WorkerThreadPool::TaskID> finished_task_ids; |
| 86 | |
| 87 | for (KeyValue<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> &E : generator_tasks) { |
| 88 | if (WorkerThreadPool::get_singleton()->is_task_completed(E.key)) { |
| 89 | WorkerThreadPool::get_singleton()->wait_for_task_completion(E.key); |
| 90 | finished_task_ids.push_back(E.key); |
| 91 | |
| 92 | NavMeshGeneratorTask2D *generator_task = E.value; |
| 93 | DEV_ASSERT(generator_task->status == NavMeshGeneratorTask2D::TaskStatus::BAKING_FINISHED); |
| 94 | |
| 95 | baking_navmeshes.erase(generator_task->navigation_mesh); |
| 96 | if (generator_task->callback.is_valid()) { |
| 97 | generator_emit_callback(generator_task->callback); |
| 98 | } |
| 99 | generator_task->navigation_mesh->emit_changed(); |
| 100 | memdelete(generator_task); |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for (WorkerThreadPool::TaskID finished_task_id : finished_task_ids) { |
| 105 | generator_tasks.erase(finished_task_id); |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void NavMeshGenerator2D::cleanup() { |
| 111 | MutexLock baking_navmesh_lock(baking_navmesh_mutex); |
| 112 | { |
| 113 | MutexLock generator_task_lock(generator_task_mutex); |
| 114 | |
| 115 | baking_navmeshes.clear(); |
| 116 | |
| 117 | for (KeyValue<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> &E : generator_tasks) { |
| 118 | WorkerThreadPool::get_singleton()->wait_for_task_completion(E.key); |
| 119 | NavMeshGeneratorTask2D *generator_task = E.value; |
| 120 | memdelete(generator_task); |
| 121 | } |
| 122 | generator_tasks.clear(); |
| 123 | |
| 124 | generator_parsers_rwlock.write_lock(); |
| 125 | generator_parsers.clear(); |
| 126 | generator_parsers_rwlock.write_unlock(); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | void NavMeshGenerator2D::finish() { |
| 131 | cleanup(); |
| 132 | } |
| 133 | |
| 134 | void NavMeshGenerator2D::parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback) { |
| 135 | ERR_FAIL_COND(!Thread::is_main_thread()); |
| 136 | ERR_FAIL_COND(p_navigation_mesh.is_null()); |
| 137 | ERR_FAIL_NULL(p_root_node); |
| 138 | ERR_FAIL_COND(!p_root_node->is_inside_tree()); |
| 139 | ERR_FAIL_COND(p_source_geometry_data.is_null()); |
| 140 | |
| 141 | generator_parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node); |
| 142 | |
| 143 | if (p_callback.is_valid()) { |
| 144 | generator_emit_callback(p_callback); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | void NavMeshGenerator2D::bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback) { |
| 149 | ERR_FAIL_COND(p_navigation_mesh.is_null()); |
| 150 | ERR_FAIL_COND(p_source_geometry_data.is_null()); |
| 151 | |
| 152 | if (p_navigation_mesh->get_outline_count() == 0 && !p_source_geometry_data->has_data()) { |
| 153 | p_navigation_mesh->clear(); |
| 154 | if (p_callback.is_valid()) { |
| 155 | generator_emit_callback(p_callback); |
| 156 | } |
| 157 | p_navigation_mesh->emit_changed(); |
| 158 | return; |
| 159 | } |
| 160 | |
| 161 | if (is_baking(p_navigation_mesh)) { |
| 162 | ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish."); |
| 163 | } |
| 164 | baking_navmesh_mutex.lock(); |
| 165 | baking_navmeshes.insert(p_navigation_mesh); |
| 166 | baking_navmesh_mutex.unlock(); |
| 167 | |
| 168 | generator_bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data); |
| 169 | |
| 170 | baking_navmesh_mutex.lock(); |
| 171 | baking_navmeshes.erase(p_navigation_mesh); |
| 172 | baking_navmesh_mutex.unlock(); |
| 173 | |
| 174 | if (p_callback.is_valid()) { |
| 175 | generator_emit_callback(p_callback); |
| 176 | } |
| 177 | |
| 178 | p_navigation_mesh->emit_changed(); |
| 179 | } |
| 180 | |
| 181 | void NavMeshGenerator2D::bake_from_source_geometry_data_async(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback) { |
| 182 | ERR_FAIL_COND(p_navigation_mesh.is_null()); |
| 183 | ERR_FAIL_COND(p_source_geometry_data.is_null()); |
| 184 | |
| 185 | if (p_navigation_mesh->get_outline_count() == 0 && !p_source_geometry_data->has_data()) { |
| 186 | p_navigation_mesh->clear(); |
| 187 | if (p_callback.is_valid()) { |
| 188 | generator_emit_callback(p_callback); |
| 189 | } |
| 190 | p_navigation_mesh->emit_changed(); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | if (!use_threads) { |
| 195 | bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback); |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | if (is_baking(p_navigation_mesh)) { |
| 200 | ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish."); |
| 201 | } |
| 202 | baking_navmesh_mutex.lock(); |
| 203 | baking_navmeshes.insert(p_navigation_mesh); |
| 204 | baking_navmesh_mutex.unlock(); |
| 205 | |
| 206 | MutexLock generator_task_lock(generator_task_mutex); |
| 207 | NavMeshGeneratorTask2D *generator_task = memnew(NavMeshGeneratorTask2D); |
| 208 | generator_task->navigation_mesh = p_navigation_mesh; |
| 209 | generator_task->source_geometry_data = p_source_geometry_data; |
| 210 | generator_task->callback = p_callback; |
| 211 | generator_task->status = NavMeshGeneratorTask2D::TaskStatus::BAKING_STARTED; |
| 212 | generator_task->thread_task_id = WorkerThreadPool::get_singleton()->add_native_task(&NavMeshGenerator2D::generator_thread_bake, generator_task, NavMeshGenerator2D::baking_use_high_priority_threads, "NavMeshGeneratorBake2D"); |
| 213 | generator_tasks.insert(generator_task->thread_task_id, generator_task); |
| 214 | } |
| 215 | |
| 216 | bool NavMeshGenerator2D::is_baking(Ref<NavigationPolygon> p_navigation_polygon) { |
| 217 | MutexLock baking_navmesh_lock(baking_navmesh_mutex); |
| 218 | return baking_navmeshes.has(p_navigation_polygon); |
| 219 | } |
| 220 | |
| 221 | void NavMeshGenerator2D::generator_thread_bake(void *p_arg) { |
| 222 | NavMeshGeneratorTask2D *generator_task = static_cast<NavMeshGeneratorTask2D *>(p_arg); |
| 223 | |
| 224 | generator_bake_from_source_geometry_data(generator_task->navigation_mesh, generator_task->source_geometry_data); |
| 225 | |
| 226 | generator_task->status = NavMeshGeneratorTask2D::TaskStatus::BAKING_FINISHED; |
| 227 | } |
| 228 | |
| 229 | void NavMeshGenerator2D::generator_parse_geometry_node(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node, bool p_recurse_children) { |
| 230 | generator_parsers_rwlock.read_lock(); |
| 231 | for (const NavMeshGeometryParser2D *parser : generator_parsers) { |
| 232 | if (!parser->callback.is_valid()) { |
| 233 | continue; |
| 234 | } |
| 235 | parser->callback.call(p_navigation_mesh, p_source_geometry_data, p_node); |
| 236 | } |
| 237 | generator_parsers_rwlock.read_unlock(); |
| 238 | |
| 239 | if (p_recurse_children) { |
| 240 | for (int i = 0; i < p_node->get_child_count(); i++) { |
| 241 | generator_parse_geometry_node(p_navigation_mesh, p_source_geometry_data, p_node->get_child(i), p_recurse_children); |
| 242 | } |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | void NavMeshGenerator2D::set_generator_parsers(const LocalVector<NavMeshGeometryParser2D *> &p_parsers) { |
| 247 | RWLockWrite write_lock(generator_parsers_rwlock); |
| 248 | generator_parsers = p_parsers; |
| 249 | } |
| 250 | |
| 251 | void NavMeshGenerator2D::generator_parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node) { |
| 252 | Vector<Node *> parse_nodes; |
| 253 | |
| 254 | if (p_navigation_mesh->get_source_geometry_mode() == NavigationPolygon::SOURCE_GEOMETRY_ROOT_NODE_CHILDREN) { |
| 255 | parse_nodes.push_back(p_root_node); |
| 256 | } else { |
| 257 | parse_nodes = p_root_node->get_tree()->get_nodes_in_group(p_navigation_mesh->get_source_geometry_group_name()); |
| 258 | } |
| 259 | |
| 260 | Transform2D root_node_transform = Transform2D(); |
| 261 | if (Object::cast_to<Node2D>(p_root_node)) { |
| 262 | root_node_transform = Object::cast_to<Node2D>(p_root_node)->get_global_transform().affine_inverse(); |
| 263 | } |
| 264 | |
| 265 | p_source_geometry_data->clear(); |
| 266 | p_source_geometry_data->root_node_transform = root_node_transform; |
| 267 | |
| 268 | bool recurse_children = p_navigation_mesh->get_source_geometry_mode() != NavigationPolygon::SOURCE_GEOMETRY_GROUPS_EXPLICIT; |
| 269 | |
| 270 | for (Node *E : parse_nodes) { |
| 271 | generator_parse_geometry_node(p_navigation_mesh, p_source_geometry_data, E, recurse_children); |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | static void generator_recursive_process_polytree_items(List<TPPLPoly> &p_tppl_in_polygon, const Clipper2Lib::PolyPathD *p_polypath_item) { |
| 276 | using namespace Clipper2Lib; |
| 277 | |
| 278 | TPPLPoly tp; |
| 279 | int size = p_polypath_item->Polygon().size(); |
| 280 | tp.Init(size); |
| 281 | |
| 282 | int j = 0; |
| 283 | for (const PointD &polypath_point : p_polypath_item->Polygon()) { |
| 284 | tp[j] = Vector2(static_cast<real_t>(polypath_point.x), static_cast<real_t>(polypath_point.y)); |
| 285 | ++j; |
| 286 | } |
| 287 | |
| 288 | if (p_polypath_item->IsHole()) { |
| 289 | tp.SetOrientation(TPPL_ORIENTATION_CW); |
| 290 | tp.SetHole(true); |
| 291 | } else { |
| 292 | tp.SetOrientation(TPPL_ORIENTATION_CCW); |
| 293 | } |
| 294 | p_tppl_in_polygon.push_back(tp); |
| 295 | |
| 296 | for (size_t i = 0; i < p_polypath_item->Count(); i++) { |
| 297 | const PolyPathD *polypath_item = p_polypath_item->Child(i); |
| 298 | generator_recursive_process_polytree_items(p_tppl_in_polygon, polypath_item); |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | bool NavMeshGenerator2D::generator_emit_callback(const Callable &p_callback) { |
| 303 | ERR_FAIL_COND_V(!p_callback.is_valid(), false); |
| 304 | |
| 305 | Callable::CallError ce; |
| 306 | Variant result; |
| 307 | p_callback.callp(nullptr, 0, result, ce); |
| 308 | |
| 309 | return ce.error == Callable::CallError::CALL_OK; |
| 310 | } |
| 311 | |
| 312 | void NavMeshGenerator2D::generator_bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data) { |
| 313 | if (p_navigation_mesh.is_null() || p_source_geometry_data.is_null()) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | using namespace Clipper2Lib; |
| 318 | PathsD traversable_polygon_paths; |
| 319 | PathsD obstruction_polygon_paths; |
| 320 | bool empty_projected_obstructions = true; |
| 321 | { |
| 322 | RWLockRead read_lock(p_source_geometry_data->geometry_rwlock); |
| 323 | |
| 324 | const Vector<Vector<Vector2>> &traversable_outlines = p_source_geometry_data->traversable_outlines; |
| 325 | int outline_count = p_navigation_mesh->get_outline_count(); |
| 326 | |
| 327 | if (outline_count == 0 && (!p_source_geometry_data->has_data() || (traversable_outlines.is_empty()))) { |
| 328 | return; |
| 329 | } |
| 330 | |
| 331 | const Vector<Vector<Vector2>> &obstruction_outlines = p_source_geometry_data->obstruction_outlines; |
| 332 | const Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> &projected_obstructions = p_source_geometry_data->_projected_obstructions; |
| 333 | |
| 334 | traversable_polygon_paths.reserve(outline_count + traversable_outlines.size()); |
| 335 | obstruction_polygon_paths.reserve(obstruction_outlines.size()); |
| 336 | |
| 337 | for (int i = 0; i < outline_count; i++) { |
| 338 | const Vector<Vector2> &traversable_outline = p_navigation_mesh->get_outline(i); |
| 339 | PathD subject_path; |
| 340 | subject_path.reserve(traversable_outline.size()); |
| 341 | for (const Vector2 &traversable_point : traversable_outline) { |
| 342 | subject_path.emplace_back(traversable_point.x, traversable_point.y); |
| 343 | } |
| 344 | traversable_polygon_paths.push_back(std::move(subject_path)); |
| 345 | } |
| 346 | |
| 347 | for (const Vector<Vector2> &traversable_outline : traversable_outlines) { |
| 348 | PathD subject_path; |
| 349 | subject_path.reserve(traversable_outline.size()); |
| 350 | for (const Vector2 &traversable_point : traversable_outline) { |
| 351 | subject_path.emplace_back(traversable_point.x, traversable_point.y); |
| 352 | } |
| 353 | traversable_polygon_paths.push_back(std::move(subject_path)); |
| 354 | } |
| 355 | |
| 356 | empty_projected_obstructions = projected_obstructions.is_empty(); |
| 357 | if (!empty_projected_obstructions) { |
| 358 | for (const NavigationMeshSourceGeometryData2D::ProjectedObstruction &projected_obstruction : projected_obstructions) { |
| 359 | if (projected_obstruction.carve) { |
| 360 | continue; |
| 361 | } |
| 362 | if (projected_obstruction.vertices.is_empty() || projected_obstruction.vertices.size() % 2 != 0) { |
| 363 | continue; |
| 364 | } |
| 365 | |
| 366 | PathD clip_path; |
| 367 | clip_path.reserve(projected_obstruction.vertices.size() / 2); |
| 368 | for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) { |
| 369 | clip_path.emplace_back(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]); |
| 370 | } |
| 371 | if (!IsPositive(clip_path)) { |
| 372 | std::reverse(clip_path.begin(), clip_path.end()); |
| 373 | } |
| 374 | obstruction_polygon_paths.push_back(std::move(clip_path)); |
| 375 | } |
| 376 | } |
| 377 | |
| 378 | for (const Vector<Vector2> &obstruction_outline : obstruction_outlines) { |
| 379 | PathD clip_path; |
| 380 | clip_path.reserve(obstruction_outline.size()); |
| 381 | for (const Vector2 &obstruction_point : obstruction_outline) { |
| 382 | clip_path.emplace_back(obstruction_point.x, obstruction_point.y); |
| 383 | } |
| 384 | obstruction_polygon_paths.push_back(std::move(clip_path)); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | Rect2 baking_rect = p_navigation_mesh->get_baking_rect(); |
| 389 | if (baking_rect.has_area()) { |
| 390 | Vector2 baking_rect_offset = p_navigation_mesh->get_baking_rect_offset(); |
| 391 | |
| 392 | const int rect_begin_x = baking_rect.position[0] + baking_rect_offset.x; |
| 393 | const int rect_begin_y = baking_rect.position[1] + baking_rect_offset.y; |
| 394 | const int rect_end_x = baking_rect.position[0] + baking_rect.size[0] + baking_rect_offset.x; |
| 395 | const int rect_end_y = baking_rect.position[1] + baking_rect.size[1] + baking_rect_offset.y; |
| 396 | |
| 397 | RectD clipper_rect = RectD(rect_begin_x, rect_begin_y, rect_end_x, rect_end_y); |
| 398 | |
| 399 | traversable_polygon_paths = RectClip(clipper_rect, traversable_polygon_paths); |
| 400 | obstruction_polygon_paths = RectClip(clipper_rect, obstruction_polygon_paths); |
| 401 | } |
| 402 | |
| 403 | // first merge all traversable polygons according to user specified fill rule |
| 404 | PathsD dummy_clip_path; |
| 405 | traversable_polygon_paths = Union(traversable_polygon_paths, dummy_clip_path, FillRule::NonZero); |
| 406 | // merge all obstruction polygons, don't allow holes for what is considered "solid" 2D geometry |
| 407 | obstruction_polygon_paths = Union(obstruction_polygon_paths, dummy_clip_path, FillRule::NonZero); |
| 408 | |
| 409 | PathsD path_solution = Difference(traversable_polygon_paths, obstruction_polygon_paths, FillRule::NonZero); |
| 410 | |
| 411 | real_t agent_radius_offset = p_navigation_mesh->get_agent_radius(); |
| 412 | if (agent_radius_offset > 0.0) { |
| 413 | path_solution = InflatePaths(path_solution, -agent_radius_offset, JoinType::Miter, EndType::Polygon); |
| 414 | } |
| 415 | |
| 416 | // Apply obstructions that are not affected by agent radius, the ones with carve enabled. |
| 417 | if (!empty_projected_obstructions) { |
| 418 | RWLockRead read_lock(p_source_geometry_data->geometry_rwlock); |
| 419 | const Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> &projected_obstructions = p_source_geometry_data->_projected_obstructions; |
| 420 | obstruction_polygon_paths.resize(0); |
| 421 | for (const NavigationMeshSourceGeometryData2D::ProjectedObstruction &projected_obstruction : projected_obstructions) { |
| 422 | if (!projected_obstruction.carve) { |
| 423 | continue; |
| 424 | } |
| 425 | if (projected_obstruction.vertices.is_empty() || projected_obstruction.vertices.size() % 2 != 0) { |
| 426 | continue; |
| 427 | } |
| 428 | |
| 429 | PathD clip_path; |
| 430 | clip_path.reserve(projected_obstruction.vertices.size() / 2); |
| 431 | for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) { |
| 432 | clip_path.emplace_back(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]); |
| 433 | } |
| 434 | if (!IsPositive(clip_path)) { |
| 435 | std::reverse(clip_path.begin(), clip_path.end()); |
| 436 | } |
| 437 | obstruction_polygon_paths.push_back(std::move(clip_path)); |
| 438 | } |
| 439 | if (obstruction_polygon_paths.size() > 0) { |
| 440 | path_solution = Difference(path_solution, obstruction_polygon_paths, FillRule::NonZero); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | //path_solution = RamerDouglasPeucker(path_solution, 0.025); // |
| 445 | |
| 446 | real_t border_size = p_navigation_mesh->get_border_size(); |
| 447 | if (baking_rect.has_area() && border_size > 0.0) { |
| 448 | Vector2 baking_rect_offset = p_navigation_mesh->get_baking_rect_offset(); |
| 449 | |
| 450 | const double rect_begin_x = baking_rect.position[0] + baking_rect_offset.x + border_size; |
| 451 | const double rect_begin_y = baking_rect.position[1] + baking_rect_offset.y + border_size; |
| 452 | const double rect_end_x = baking_rect.position[0] + baking_rect.size[0] + baking_rect_offset.x - border_size; |
| 453 | const double rect_end_y = baking_rect.position[1] + baking_rect.size[1] + baking_rect_offset.y - border_size; |
| 454 | |
| 455 | RectD clipper_rect = RectD(rect_begin_x, rect_begin_y, rect_end_x, rect_end_y); |
| 456 | |
| 457 | path_solution = RectClip(clipper_rect, path_solution); |
| 458 | } |
| 459 | |
| 460 | if (path_solution.size() == 0) { |
| 461 | p_navigation_mesh->clear(); |
| 462 | return; |
| 463 | } |
| 464 | |
| 465 | ClipType clipper_cliptype = ClipType::Union; |
| 466 | |
| 467 | List<TPPLPoly> tppl_in_polygon, tppl_out_polygon; |
| 468 | |
| 469 | PolyTreeD polytree; |
| 470 | ClipperD clipper_D; |
| 471 | |
| 472 | clipper_D.AddSubject(path_solution); |
| 473 | clipper_D.Execute(clipper_cliptype, FillRule::NonZero, polytree); |
| 474 | |
| 475 | for (size_t i = 0; i < polytree.Count(); i++) { |
| 476 | const PolyPathD *polypath_item = polytree[i]; |
| 477 | generator_recursive_process_polytree_items(tppl_in_polygon, polypath_item); |
| 478 | } |
| 479 | |
| 480 | TPPLPartition tpart; |
| 481 | |
| 482 | NavigationPolygon::SamplePartitionType sample_partition_type = p_navigation_mesh->get_sample_partition_type(); |
| 483 | |
| 484 | switch (sample_partition_type) { |
| 485 | case NavigationPolygon::SamplePartitionType::SAMPLE_PARTITION_CONVEX_PARTITION: |
| 486 | if (tpart.ConvexPartition_HM(&tppl_in_polygon, &tppl_out_polygon) == 0) { |
| 487 | ERR_PRINT("NavigationPolygon polygon convex partition failed. Unable to create a valid navigation mesh polygon layout from provided source geometry."); |
| 488 | p_navigation_mesh->set_vertices(Vector<Vector2>()); |
| 489 | p_navigation_mesh->clear_polygons(); |
| 490 | return; |
| 491 | } |
| 492 | break; |
| 493 | case NavigationPolygon::SamplePartitionType::SAMPLE_PARTITION_TRIANGULATE: |
| 494 | if (tpart.Triangulate_EC(&tppl_in_polygon, &tppl_out_polygon) == 0) { |
| 495 | ERR_PRINT("NavigationPolygon polygon triangulation failed. Unable to create a valid navigation mesh polygon layout from provided source geometry."); |
| 496 | p_navigation_mesh->set_vertices(Vector<Vector2>()); |
| 497 | p_navigation_mesh->clear_polygons(); |
| 498 | return; |
| 499 | } |
| 500 | break; |
| 501 | default: { |
| 502 | ERR_PRINT("NavigationPolygon polygon partitioning failed. Unrecognized partition type."); |
| 503 | p_navigation_mesh->set_vertices(Vector<Vector2>()); |
| 504 | p_navigation_mesh->clear_polygons(); |
| 505 | return; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | Vector<Vector2> new_vertices; |
| 510 | Vector<Vector<int>> new_polygons; |
| 511 | |
| 512 | HashMap<Vector2, int> points; |
| 513 | for (const TPPLPoly &tp : tppl_out_polygon) { |
| 514 | Vector<int> new_polygon; |
| 515 | |
| 516 | for (int64_t i = 0; i < tp.GetNumPoints(); i++) { |
| 517 | HashMap<Vector2, int>::Iterator E = points.find(tp[i]); |
| 518 | if (!E) { |
| 519 | E = points.insert(tp[i], new_vertices.size()); |
| 520 | new_vertices.push_back(tp[i]); |
| 521 | } |
| 522 | new_polygon.push_back(E->value); |
| 523 | } |
| 524 | |
| 525 | new_polygons.push_back(new_polygon); |
| 526 | } |
| 527 | |
| 528 | p_navigation_mesh->set_data(new_vertices, new_polygons); |
| 529 | } |
| 530 | |
| 531 | #endif // CLIPPER2_ENABLED |