Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_2d/2d/nav_region_builder_2d.cpp
10278 views
1
/**************************************************************************/
2
/* nav_region_builder_2d.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "nav_region_builder_2d.h"
32
33
#include "../nav_map_2d.h"
34
#include "../nav_region_2d.h"
35
#include "../triangle2.h"
36
#include "nav_region_iteration_2d.h"
37
38
using namespace Nav2D;
39
40
void NavRegionBuilder2D::build_iteration(NavRegionIterationBuild2D &r_build) {
41
PerformanceData &performance_data = r_build.performance_data;
42
43
performance_data.pm_polygon_count = 0;
44
performance_data.pm_edge_count = 0;
45
performance_data.pm_edge_merge_count = 0;
46
performance_data.pm_edge_connection_count = 0;
47
performance_data.pm_edge_free_count = 0;
48
49
_build_step_process_navmesh_data(r_build);
50
51
_build_step_find_edge_connection_pairs(r_build);
52
53
_build_step_merge_edge_connection_pairs(r_build);
54
55
_build_update_iteration(r_build);
56
}
57
58
void NavRegionBuilder2D::_build_step_process_navmesh_data(NavRegionIterationBuild2D &r_build) {
59
Vector<Vector2> _navmesh_vertices = r_build.navmesh_data.vertices;
60
Vector<Vector<int>> _navmesh_polygons = r_build.navmesh_data.polygons;
61
62
if (_navmesh_vertices.is_empty() || _navmesh_polygons.is_empty()) {
63
return;
64
}
65
66
PerformanceData &performance_data = r_build.performance_data;
67
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
68
69
const Transform2D &region_transform = region_iteration->transform;
70
LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
71
72
const int vertex_count = _navmesh_vertices.size();
73
74
const Vector2 *vertices_ptr = _navmesh_vertices.ptr();
75
const Vector<int> *polygons_ptr = _navmesh_polygons.ptr();
76
77
navmesh_polygons.resize(_navmesh_polygons.size());
78
79
real_t _new_region_surface_area = 0.0;
80
Rect2 _new_region_bounds;
81
82
bool first_vertex = true;
83
84
for (uint32_t i = 0; i < navmesh_polygons.size(); i++) {
85
Polygon &polygon = navmesh_polygons[i];
86
polygon.id = i;
87
polygon.owner = region_iteration.ptr();
88
polygon.surface_area = 0.0;
89
90
Vector<int> polygon_indices = polygons_ptr[i];
91
92
int polygon_size = polygon_indices.size();
93
if (polygon_size < 3) {
94
continue;
95
}
96
97
const int *indices_ptr = polygon_indices.ptr();
98
99
bool polygon_valid = true;
100
101
polygon.vertices.resize(polygon_size);
102
103
{
104
real_t _new_polygon_surface_area = 0.0;
105
106
for (int j(2); j < polygon_size; j++) {
107
const Triangle2 triangle = Triangle2(
108
region_transform.xform(vertices_ptr[indices_ptr[0]]),
109
region_transform.xform(vertices_ptr[indices_ptr[j - 1]]),
110
region_transform.xform(vertices_ptr[indices_ptr[j]]));
111
112
_new_polygon_surface_area += triangle.get_area();
113
}
114
115
polygon.surface_area = _new_polygon_surface_area;
116
_new_region_surface_area += _new_polygon_surface_area;
117
}
118
119
for (int j(0); j < polygon_size; j++) {
120
int vertex_index = indices_ptr[j];
121
if (vertex_index < 0 || vertex_index >= vertex_count) {
122
polygon_valid = false;
123
break;
124
}
125
126
const Vector2 point_position = region_transform.xform(vertices_ptr[vertex_index]);
127
polygon.vertices[j] = point_position;
128
129
if (first_vertex) {
130
first_vertex = false;
131
_new_region_bounds.position = point_position;
132
} else {
133
_new_region_bounds.expand_to(point_position);
134
}
135
}
136
137
if (!polygon_valid) {
138
polygon.surface_area = 0.0;
139
polygon.vertices.clear();
140
ERR_FAIL_COND_MSG(!polygon_valid, "Corrupted navigation mesh set on region. The indices of a polygon are out of range.");
141
}
142
}
143
144
region_iteration->surface_area = _new_region_surface_area;
145
region_iteration->bounds = _new_region_bounds;
146
147
performance_data.pm_polygon_count = navmesh_polygons.size();
148
}
149
150
Nav2D::PointKey NavRegionBuilder2D::get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size) {
151
const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
152
const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
153
154
PointKey p;
155
p.key = 0;
156
p.x = x;
157
p.y = y;
158
return p;
159
}
160
161
Nav2D::EdgeKey NavRegionBuilder2D::get_edge_key(const Vector2 &p_vertex1, const Vector2 &p_vertex2, const Vector2 &p_cell_size) {
162
EdgeKey ek(get_point_key(p_vertex1, p_cell_size), get_point_key(p_vertex2, p_cell_size));
163
return ek;
164
}
165
166
void NavRegionBuilder2D::_build_step_find_edge_connection_pairs(NavRegionIterationBuild2D &r_build) {
167
PerformanceData &performance_data = r_build.performance_data;
168
169
const Vector2 &map_cell_size = r_build.map_cell_size;
170
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
171
LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
172
173
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
174
connection_pairs_map.clear();
175
176
region_iteration->internal_connections.clear();
177
region_iteration->internal_connections.resize(navmesh_polygons.size());
178
179
region_iteration->external_edges.clear();
180
181
int free_edges_count = 0;
182
183
for (Polygon &poly : region_iteration->navmesh_polygons) {
184
for (uint32_t p = 0; p < poly.vertices.size(); p++) {
185
const int next_point = (p + 1) % poly.vertices.size();
186
const EdgeKey ek = get_edge_key(poly.vertices[p], poly.vertices[next_point], map_cell_size);
187
188
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
189
if (!pair_it) {
190
pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
191
performance_data.pm_edge_count += 1;
192
++free_edges_count;
193
}
194
EdgeConnectionPair &pair = pair_it->value;
195
if (pair.size < 2) {
196
// Add the polygon/edge tuple to this key.
197
Connection new_connection;
198
new_connection.polygon = &poly;
199
new_connection.edge = p;
200
new_connection.pathway_start = poly.vertices[p];
201
new_connection.pathway_end = poly.vertices[next_point];
202
203
pair.connections[pair.size] = new_connection;
204
++pair.size;
205
if (pair.size == 2) {
206
--free_edges_count;
207
}
208
209
} else {
210
// The edge is already connected with another edge, skip.
211
ERR_FAIL_COND_MSG(pair.size >= 2, "Navigation region synchronization error. More than 2 edges tried to occupy the same map rasterization space. This is a logical error in the navigation mesh caused by overlap or too densely placed edges.");
212
}
213
}
214
}
215
216
performance_data.pm_edge_free_count = free_edges_count;
217
}
218
219
void NavRegionBuilder2D::_build_step_merge_edge_connection_pairs(NavRegionIterationBuild2D &r_build) {
220
PerformanceData &performance_data = r_build.performance_data;
221
222
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
223
224
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
225
226
for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
227
const EdgeConnectionPair &pair = pair_it.value;
228
if (pair.size == 2) {
229
// Connect edge that are shared in different polygons.
230
const Connection &c1 = pair.connections[0];
231
const Connection &c2 = pair.connections[1];
232
region_iteration->internal_connections[c1.polygon->id].push_back(c2);
233
region_iteration->internal_connections[c2.polygon->id].push_back(c1);
234
performance_data.pm_edge_merge_count += 1;
235
236
} else {
237
ERR_FAIL_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
238
239
const Connection &connection = pair.connections[0];
240
241
ConnectableEdge ce;
242
ce.ek = pair_it.key;
243
ce.polygon_index = connection.polygon->id;
244
ce.edge = connection.edge;
245
ce.pathway_start = connection.pathway_start;
246
ce.pathway_end = connection.pathway_end;
247
248
region_iteration->external_edges.push_back(ce);
249
}
250
}
251
}
252
253
void NavRegionBuilder2D::_build_update_iteration(NavRegionIterationBuild2D &r_build) {
254
ERR_FAIL_NULL(r_build.region);
255
// Stub. End of the build.
256
}
257
258