Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_3d/3d/nav_map_builder_3d.cpp
10278 views
1
/**************************************************************************/
2
/* nav_map_builder_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_map_builder_3d.h"
32
33
#include "../nav_link_3d.h"
34
#include "../nav_map_3d.h"
35
#include "../nav_region_3d.h"
36
#include "nav_map_iteration_3d.h"
37
#include "nav_region_iteration_3d.h"
38
39
using namespace Nav3D;
40
41
PointKey NavMapBuilder3D::get_point_key(const Vector3 &p_pos, const Vector3 &p_cell_size) {
42
const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
43
const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
44
const int z = static_cast<int>(Math::floor(p_pos.z / p_cell_size.z));
45
46
PointKey p;
47
p.key = 0;
48
p.x = x;
49
p.y = y;
50
p.z = z;
51
return p;
52
}
53
54
void NavMapBuilder3D::build_navmap_iteration(NavMapIterationBuild3D &r_build) {
55
PerformanceData &performance_data = r_build.performance_data;
56
57
performance_data.pm_polygon_count = 0;
58
performance_data.pm_edge_count = 0;
59
performance_data.pm_edge_merge_count = 0;
60
performance_data.pm_edge_connection_count = 0;
61
performance_data.pm_edge_free_count = 0;
62
63
_build_step_gather_region_polygons(r_build);
64
65
_build_step_find_edge_connection_pairs(r_build);
66
67
_build_step_merge_edge_connection_pairs(r_build);
68
69
_build_step_edge_connection_margin_connections(r_build);
70
71
_build_step_navlink_connections(r_build);
72
73
_build_update_map_iteration(r_build);
74
}
75
76
void NavMapBuilder3D::_build_step_gather_region_polygons(NavMapIterationBuild3D &r_build) {
77
PerformanceData &performance_data = r_build.performance_data;
78
NavMapIteration3D *map_iteration = r_build.map_iteration;
79
80
const LocalVector<Ref<NavRegionIteration3D>> &regions = map_iteration->region_iterations;
81
HashMap<const NavBaseIteration3D *, LocalVector<Connection>> &region_external_connections = map_iteration->external_region_connections;
82
83
map_iteration->navbases_polygons_external_connections.clear();
84
85
// Remove regions connections.
86
region_external_connections.clear();
87
88
// Copy all region polygons in the map.
89
int polygon_count = 0;
90
for (const Ref<NavRegionIteration3D> &region : regions) {
91
const uint32_t polygons_size = region->navmesh_polygons.size();
92
polygon_count += polygons_size;
93
94
region_external_connections[region.ptr()] = LocalVector<Connection>();
95
map_iteration->navbases_polygons_external_connections[region.ptr()] = LocalVector<LocalVector<Connection>>();
96
map_iteration->navbases_polygons_external_connections[region.ptr()].resize(polygons_size);
97
}
98
99
performance_data.pm_polygon_count = polygon_count;
100
r_build.polygon_count = polygon_count;
101
}
102
103
void NavMapBuilder3D::_build_step_find_edge_connection_pairs(NavMapIterationBuild3D &r_build) {
104
PerformanceData &performance_data = r_build.performance_data;
105
NavMapIteration3D *map_iteration = r_build.map_iteration;
106
int polygon_count = r_build.polygon_count;
107
108
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
109
110
// Group all edges per key.
111
connection_pairs_map.clear();
112
connection_pairs_map.reserve(polygon_count);
113
int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
114
115
for (const Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
116
for (const ConnectableEdge &connectable_edge : region->get_external_edges()) {
117
const EdgeKey &ek = connectable_edge.ek;
118
119
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
120
if (!pair_it) {
121
pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
122
performance_data.pm_edge_count += 1;
123
++free_edges_count;
124
}
125
EdgeConnectionPair &pair = pair_it->value;
126
if (pair.size < 2) {
127
// Add the polygon/edge tuple to this key.
128
Connection new_connection;
129
new_connection.polygon = &region->navmesh_polygons[connectable_edge.polygon_index];
130
new_connection.edge = connectable_edge.edge;
131
new_connection.pathway_start = connectable_edge.pathway_start;
132
new_connection.pathway_end = connectable_edge.pathway_end;
133
134
pair.connections[pair.size] = new_connection;
135
++pair.size;
136
if (pair.size == 2) {
137
--free_edges_count;
138
}
139
140
} else {
141
// The edge is already connected with another edge, skip.
142
ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to merge a navigation mesh polygon edge with another already-merged edge. This is usually caused by crossing edges, overlapping polygons, or a mismatch of the NavigationMesh / NavigationPolygon baked 'cell_size' and navigation map 'cell_size'. If you're certain none of above is the case, change 'navigation/3d/merge_rasterizer_cell_scale' to 0.001.");
143
}
144
}
145
}
146
147
r_build.free_edge_count = free_edges_count;
148
}
149
150
void NavMapBuilder3D::_build_step_merge_edge_connection_pairs(NavMapIterationBuild3D &r_build) {
151
PerformanceData &performance_data = r_build.performance_data;
152
153
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
154
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
155
int free_edges_count = r_build.free_edge_count;
156
bool use_edge_connections = r_build.use_edge_connections;
157
158
free_edges.clear();
159
free_edges.reserve(free_edges_count);
160
161
NavMapIteration3D *map_iteration = r_build.map_iteration;
162
163
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
164
165
for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
166
const EdgeConnectionPair &pair = pair_it.value;
167
if (pair.size == 2) {
168
// Connect edge that are shared in different polygons.
169
const Connection &c1 = pair.connections[0];
170
const Connection &c2 = pair.connections[1];
171
172
navbases_polygons_external_connections[c1.polygon->owner][c1.polygon->id].push_back(c2);
173
navbases_polygons_external_connections[c2.polygon->owner][c2.polygon->id].push_back(c1);
174
performance_data.pm_edge_connection_count += 1;
175
176
} else {
177
CRASH_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
178
if (use_edge_connections && pair.connections[0].polygon->owner->get_use_edge_connections()) {
179
free_edges.push_back(pair.connections[0]);
180
}
181
}
182
}
183
}
184
185
void NavMapBuilder3D::_build_step_edge_connection_margin_connections(NavMapIterationBuild3D &r_build) {
186
PerformanceData &performance_data = r_build.performance_data;
187
NavMapIteration3D *map_iteration = r_build.map_iteration;
188
189
real_t edge_connection_margin = r_build.edge_connection_margin;
190
191
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
192
HashMap<const NavBaseIteration3D *, LocalVector<Connection>> &region_external_connections = map_iteration->external_region_connections;
193
194
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
195
196
// Find the compatible near edges.
197
//
198
// Note:
199
// Considering that the edges must be compatible (for obvious reasons)
200
// to be connected, create new polygons to remove that small gap is
201
// not really useful and would result in wasteful computation during
202
// connection, integration and path finding.
203
performance_data.pm_edge_free_count = free_edges.size();
204
205
const real_t edge_connection_margin_squared = edge_connection_margin * edge_connection_margin;
206
207
for (uint32_t i = 0; i < free_edges.size(); i++) {
208
const Connection &free_edge = free_edges[i];
209
const Vector3 &edge_p1 = free_edge.pathway_start;
210
const Vector3 &edge_p2 = free_edge.pathway_end;
211
212
for (uint32_t j = 0; j < free_edges.size(); j++) {
213
const Connection &other_edge = free_edges[j];
214
if (i == j || free_edge.polygon->owner == other_edge.polygon->owner) {
215
continue;
216
}
217
218
const Vector3 &other_edge_p1 = other_edge.pathway_start;
219
const Vector3 &other_edge_p2 = other_edge.pathway_end;
220
221
// Compute the projection of the opposite edge on the current one
222
Vector3 edge_vector = edge_p2 - edge_p1;
223
real_t projected_p1_ratio = edge_vector.dot(other_edge_p1 - edge_p1) / (edge_vector.length_squared());
224
real_t projected_p2_ratio = edge_vector.dot(other_edge_p2 - edge_p1) / (edge_vector.length_squared());
225
if ((projected_p1_ratio < 0.0 && projected_p2_ratio < 0.0) || (projected_p1_ratio > 1.0 && projected_p2_ratio > 1.0)) {
226
continue;
227
}
228
229
// Check if the two edges are close to each other enough and compute a pathway between the two regions.
230
Vector3 self1 = edge_vector * CLAMP(projected_p1_ratio, 0.0, 1.0) + edge_p1;
231
Vector3 other1;
232
if (projected_p1_ratio >= 0.0 && projected_p1_ratio <= 1.0) {
233
other1 = other_edge_p1;
234
} else {
235
other1 = other_edge_p1.lerp(other_edge_p2, (1.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
236
}
237
if (other1.distance_squared_to(self1) > edge_connection_margin_squared) {
238
continue;
239
}
240
241
Vector3 self2 = edge_vector * CLAMP(projected_p2_ratio, 0.0, 1.0) + edge_p1;
242
Vector3 other2;
243
if (projected_p2_ratio >= 0.0 && projected_p2_ratio <= 1.0) {
244
other2 = other_edge_p2;
245
} else {
246
other2 = other_edge_p1.lerp(other_edge_p2, (0.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
247
}
248
if (other2.distance_squared_to(self2) > edge_connection_margin_squared) {
249
continue;
250
}
251
252
// The edges can now be connected.
253
Connection new_connection = other_edge;
254
new_connection.pathway_start = (self1 + other1) / 2.0;
255
new_connection.pathway_end = (self2 + other2) / 2.0;
256
//free_edge.polygon->connections.push_back(new_connection);
257
258
// Add the connection to the region_connection map.
259
region_external_connections[free_edge.polygon->owner].push_back(new_connection);
260
navbases_polygons_external_connections[free_edge.polygon->owner][free_edge.polygon->id].push_back(new_connection);
261
performance_data.pm_edge_connection_count += 1;
262
}
263
}
264
}
265
266
void NavMapBuilder3D::_build_step_navlink_connections(NavMapIterationBuild3D &r_build) {
267
NavMapIteration3D *map_iteration = r_build.map_iteration;
268
269
real_t link_connection_radius = r_build.link_connection_radius;
270
271
const LocalVector<Ref<NavLinkIteration3D>> &links = map_iteration->link_iterations;
272
273
int polygon_count = r_build.polygon_count;
274
275
real_t link_connection_radius_sqr = link_connection_radius * link_connection_radius;
276
277
HashMap<const NavBaseIteration3D *, LocalVector<LocalVector<Nav3D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
278
LocalVector<Nav3D::Polygon> &navlink_polygons = map_iteration->navlink_polygons;
279
navlink_polygons.clear();
280
navlink_polygons.resize(links.size());
281
uint32_t navlink_index = 0;
282
283
// Search for polygons within range of a nav link.
284
for (const Ref<NavLinkIteration3D> &link : links) {
285
polygon_count++;
286
Polygon &new_polygon = navlink_polygons[navlink_index++];
287
288
new_polygon.id = 0;
289
new_polygon.owner = link.ptr();
290
291
const Vector3 link_start_pos = link->get_start_position();
292
const Vector3 link_end_pos = link->get_end_position();
293
294
Polygon *closest_start_polygon = nullptr;
295
real_t closest_start_sqr_dist = link_connection_radius_sqr;
296
Vector3 closest_start_point;
297
298
Polygon *closest_end_polygon = nullptr;
299
real_t closest_end_sqr_dist = link_connection_radius_sqr;
300
Vector3 closest_end_point;
301
302
for (const Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
303
AABB region_bounds = region->get_bounds().grow(link_connection_radius);
304
if (!region_bounds.has_point(link_start_pos) && !region_bounds.has_point(link_end_pos)) {
305
continue;
306
}
307
308
for (Polygon &polyon : region->navmesh_polygons) {
309
for (uint32_t point_id = 2; point_id < polyon.vertices.size(); point_id += 1) {
310
const Face3 face(polyon.vertices[0], polyon.vertices[point_id - 1], polyon.vertices[point_id]);
311
312
{
313
const Vector3 start_point = face.get_closest_point_to(link_start_pos);
314
const real_t sqr_dist = start_point.distance_squared_to(link_start_pos);
315
316
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
317
if (sqr_dist < closest_start_sqr_dist) {
318
closest_start_sqr_dist = sqr_dist;
319
closest_start_point = start_point;
320
closest_start_polygon = &polyon;
321
}
322
}
323
324
{
325
const Vector3 end_point = face.get_closest_point_to(link_end_pos);
326
const real_t sqr_dist = end_point.distance_squared_to(link_end_pos);
327
328
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
329
if (sqr_dist < closest_end_sqr_dist) {
330
closest_end_sqr_dist = sqr_dist;
331
closest_end_point = end_point;
332
closest_end_polygon = &polyon;
333
}
334
}
335
}
336
}
337
}
338
339
// If we have both a start and end point, then create a synthetic polygon to route through.
340
if (closest_start_polygon && closest_end_polygon) {
341
new_polygon.vertices.resize(4);
342
343
// Build a set of vertices that create a thin polygon going from the start to the end point.
344
new_polygon.vertices[0] = closest_start_point;
345
new_polygon.vertices[1] = closest_start_point;
346
new_polygon.vertices[2] = closest_end_point;
347
new_polygon.vertices[3] = closest_end_point;
348
349
// Setup connections to go forward in the link.
350
{
351
Connection entry_connection;
352
entry_connection.polygon = &new_polygon;
353
entry_connection.edge = -1;
354
entry_connection.pathway_start = new_polygon.vertices[0];
355
entry_connection.pathway_end = new_polygon.vertices[1];
356
navbases_polygons_external_connections[closest_start_polygon->owner][closest_start_polygon->id].push_back(entry_connection);
357
358
Connection exit_connection;
359
exit_connection.polygon = closest_end_polygon;
360
exit_connection.edge = -1;
361
exit_connection.pathway_start = new_polygon.vertices[2];
362
exit_connection.pathway_end = new_polygon.vertices[3];
363
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav3D::Connection>());
364
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
365
}
366
367
// If the link is bi-directional, create connections from the end to the start.
368
if (link->is_bidirectional()) {
369
Connection entry_connection;
370
entry_connection.polygon = &new_polygon;
371
entry_connection.edge = -1;
372
entry_connection.pathway_start = new_polygon.vertices[2];
373
entry_connection.pathway_end = new_polygon.vertices[3];
374
navbases_polygons_external_connections[closest_end_polygon->owner][closest_end_polygon->id].push_back(entry_connection);
375
376
Connection exit_connection;
377
exit_connection.polygon = closest_start_polygon;
378
exit_connection.edge = -1;
379
exit_connection.pathway_start = new_polygon.vertices[0];
380
exit_connection.pathway_end = new_polygon.vertices[1];
381
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav3D::Connection>());
382
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
383
}
384
}
385
}
386
387
r_build.polygon_count = polygon_count;
388
}
389
390
void NavMapBuilder3D::_build_update_map_iteration(NavMapIterationBuild3D &r_build) {
391
NavMapIteration3D *map_iteration = r_build.map_iteration;
392
393
map_iteration->navmesh_polygon_count = r_build.polygon_count;
394
395
uint32_t navmesh_polygon_count = r_build.polygon_count;
396
uint32_t total_polygon_count = navmesh_polygon_count;
397
398
map_iteration->path_query_slots_mutex.lock();
399
for (NavMeshQueries3D::PathQuerySlot &p_path_query_slot : map_iteration->path_query_slots) {
400
p_path_query_slot.traversable_polys.clear();
401
p_path_query_slot.traversable_polys.reserve(navmesh_polygon_count * 0.25);
402
p_path_query_slot.path_corridor.clear();
403
404
p_path_query_slot.path_corridor.resize(total_polygon_count);
405
406
p_path_query_slot.poly_to_id.clear();
407
p_path_query_slot.poly_to_id.reserve(total_polygon_count);
408
409
int polygon_id = 0;
410
for (Ref<NavRegionIteration3D> &region : map_iteration->region_iterations) {
411
for (const Polygon &polygon : region->navmesh_polygons) {
412
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
413
polygon_id++;
414
}
415
}
416
417
for (const Polygon &polygon : map_iteration->navlink_polygons) {
418
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
419
polygon_id++;
420
}
421
422
DEV_ASSERT(p_path_query_slot.path_corridor.size() == p_path_query_slot.poly_to_id.size());
423
}
424
425
map_iteration->path_query_slots_mutex.unlock();
426
}
427
428