Path: blob/master/modules/navigation_3d/3d/nav_region_builder_3d.cpp
10278 views
/**************************************************************************/1/* nav_region_builder_3d.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "nav_region_builder_3d.h"3132#include "../nav_map_3d.h"33#include "../nav_region_3d.h"34#include "nav_region_iteration_3d.h"3536using namespace Nav3D;3738void NavRegionBuilder3D::build_iteration(NavRegionIterationBuild3D &r_build) {39PerformanceData &performance_data = r_build.performance_data;4041performance_data.pm_polygon_count = 0;42performance_data.pm_edge_count = 0;43performance_data.pm_edge_merge_count = 0;44performance_data.pm_edge_connection_count = 0;45performance_data.pm_edge_free_count = 0;4647_build_step_process_navmesh_data(r_build);4849_build_step_find_edge_connection_pairs(r_build);5051_build_step_merge_edge_connection_pairs(r_build);5253_build_update_iteration(r_build);54}5556void NavRegionBuilder3D::_build_step_process_navmesh_data(NavRegionIterationBuild3D &r_build) {57Vector<Vector3> _navmesh_vertices = r_build.navmesh_data.vertices;58Vector<Vector<int>> _navmesh_polygons = r_build.navmesh_data.polygons;5960if (_navmesh_vertices.is_empty() || _navmesh_polygons.is_empty()) {61return;62}6364PerformanceData &performance_data = r_build.performance_data;65Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;6667const Transform3D ®ion_transform = region_iteration->transform;68LocalVector<Nav3D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;6970const int vertex_count = _navmesh_vertices.size();7172const Vector3 *vertices_ptr = _navmesh_vertices.ptr();73const Vector<int> *polygons_ptr = _navmesh_polygons.ptr();7475navmesh_polygons.resize(_navmesh_polygons.size());7677real_t _new_region_surface_area = 0.0;78AABB _new_region_bounds;7980bool first_vertex = true;8182for (uint32_t i = 0; i < navmesh_polygons.size(); i++) {83Polygon &polygon = navmesh_polygons[i];84polygon.id = i;85polygon.owner = region_iteration.ptr();86polygon.surface_area = 0.0;8788Vector<int> polygon_indices = polygons_ptr[i];8990int polygon_size = polygon_indices.size();91if (polygon_size < 3) {92continue;93}9495const int *indices_ptr = polygon_indices.ptr();9697bool polygon_valid = true;9899polygon.vertices.resize(polygon_size);100101{102real_t _new_polygon_surface_area = 0.0;103104for (int j(2); j < polygon_size; j++) {105const Face3 face = Face3(106region_transform.xform(vertices_ptr[indices_ptr[0]]),107region_transform.xform(vertices_ptr[indices_ptr[j - 1]]),108region_transform.xform(vertices_ptr[indices_ptr[j]]));109110_new_polygon_surface_area += face.get_area();111}112113polygon.surface_area = _new_polygon_surface_area;114_new_region_surface_area += _new_polygon_surface_area;115}116117for (int j(0); j < polygon_size; j++) {118int vertex_index = indices_ptr[j];119if (vertex_index < 0 || vertex_index >= vertex_count) {120polygon_valid = false;121break;122}123124const Vector3 point_position = region_transform.xform(vertices_ptr[vertex_index]);125polygon.vertices[j] = point_position;126127if (first_vertex) {128first_vertex = false;129_new_region_bounds.position = point_position;130} else {131_new_region_bounds.expand_to(point_position);132}133}134135if (!polygon_valid) {136polygon.surface_area = 0.0;137polygon.vertices.clear();138ERR_FAIL_COND_MSG(!polygon_valid, "Corrupted navigation mesh set on region. The indices of a polygon are out of range.");139}140}141142region_iteration->surface_area = _new_region_surface_area;143region_iteration->bounds = _new_region_bounds;144145performance_data.pm_polygon_count = navmesh_polygons.size();146}147148Nav3D::PointKey NavRegionBuilder3D::get_point_key(const Vector3 &p_pos, const Vector3 &p_cell_size) {149const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));150const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));151const int z = static_cast<int>(Math::floor(p_pos.z / p_cell_size.z));152153PointKey p;154p.key = 0;155p.x = x;156p.y = y;157p.z = z;158return p;159}160161Nav3D::EdgeKey NavRegionBuilder3D::get_edge_key(const Vector3 &p_vertex1, const Vector3 &p_vertex2, const Vector3 &p_cell_size) {162EdgeKey ek(get_point_key(p_vertex1, p_cell_size), get_point_key(p_vertex2, p_cell_size));163return ek;164}165166void NavRegionBuilder3D::_build_step_find_edge_connection_pairs(NavRegionIterationBuild3D &r_build) {167PerformanceData &performance_data = r_build.performance_data;168169const Vector3 &map_cell_size = r_build.map_cell_size;170Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;171LocalVector<Nav3D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;172173HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;174connection_pairs_map.clear();175176region_iteration->internal_connections.clear();177region_iteration->internal_connections.resize(navmesh_polygons.size());178179region_iteration->external_edges.clear();180181int free_edges_count = 0;182183for (Polygon &poly : region_iteration->navmesh_polygons) {184for (uint32_t p = 0; p < poly.vertices.size(); p++) {185const int next_point = (p + 1) % poly.vertices.size();186const EdgeKey ek = get_edge_key(poly.vertices[p], poly.vertices[next_point], map_cell_size);187188HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);189if (!pair_it) {190pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());191performance_data.pm_edge_count += 1;192++free_edges_count;193}194EdgeConnectionPair &pair = pair_it->value;195if (pair.size < 2) {196// Add the polygon/edge tuple to this key.197Connection new_connection;198new_connection.polygon = &poly;199new_connection.edge = p;200new_connection.pathway_start = poly.vertices[p];201new_connection.pathway_end = poly.vertices[next_point];202203pair.connections[pair.size] = new_connection;204++pair.size;205if (pair.size == 2) {206--free_edges_count;207}208209} else {210// The edge is already connected with another edge, skip.211ERR_FAIL_COND_MSG(pair.size >= 2, "Navigation region synchronization error. More than 2 edges tried to occupy the same map rasterization space. This is a logical error in the navigation mesh caused by overlap or too densely placed edges.");212}213}214}215216performance_data.pm_edge_free_count = free_edges_count;217}218219void NavRegionBuilder3D::_build_step_merge_edge_connection_pairs(NavRegionIterationBuild3D &r_build) {220PerformanceData &performance_data = r_build.performance_data;221222Ref<NavRegionIteration3D> region_iteration = r_build.region_iteration;223224HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;225226for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {227const EdgeConnectionPair &pair = pair_it.value;228if (pair.size == 2) {229// Connect edge that are shared in different polygons.230const Connection &c1 = pair.connections[0];231const Connection &c2 = pair.connections[1];232region_iteration->internal_connections[c1.polygon->id].push_back(c2);233region_iteration->internal_connections[c2.polygon->id].push_back(c1);234performance_data.pm_edge_merge_count += 1;235236} else {237ERR_FAIL_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));238239const Connection &connection = pair.connections[0];240241ConnectableEdge ce;242ce.ek = pair_it.key;243ce.polygon_index = connection.polygon->id;244ce.edge = connection.edge;245ce.pathway_start = connection.pathway_start;246ce.pathway_end = connection.pathway_end;247248region_iteration->external_edges.push_back(ce);249}250}251}252253void NavRegionBuilder3D::_build_update_iteration(NavRegionIterationBuild3D &r_build) {254ERR_FAIL_NULL(r_build.region);255// Stub. End of the build.256}257258259