Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_3d/nav_map_3d.h
10277 views
1
/**************************************************************************/
2
/* nav_map_3d.h */
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
#pragma once
32
33
#include "3d/nav_map_iteration_3d.h"
34
#include "3d/nav_mesh_queries_3d.h"
35
#include "nav_rid_3d.h"
36
#include "nav_utils_3d.h"
37
38
#include "core/math/math_defs.h"
39
#include "core/object/worker_thread_pool.h"
40
#include "servers/navigation/navigation_globals.h"
41
42
#include <KdTree2d.h>
43
#include <KdTree3d.h>
44
#include <RVOSimulator2d.h>
45
#include <RVOSimulator3d.h>
46
47
class NavLink3D;
48
class NavRegion3D;
49
class NavAgent3D;
50
class NavObstacle3D;
51
52
class NavMap3D : public NavRid3D {
53
/// Map Up
54
Vector3 up = Vector3(0, 1, 0);
55
56
/// To find the polygons edges the vertices are displaced in a grid where
57
/// each cell has the following cell_size and cell_height.
58
real_t cell_size = NavigationDefaults3D::NAV_MESH_CELL_SIZE;
59
real_t cell_height = NavigationDefaults3D::NAV_MESH_CELL_HEIGHT;
60
61
// For the inter-region merging to work, internal rasterization is performed.
62
Vector3 merge_rasterizer_cell_size = Vector3(cell_size, cell_height, cell_size);
63
64
// This value is used to control sensitivity of internal rasterizer.
65
float merge_rasterizer_cell_scale = 1.0;
66
67
bool use_edge_connections = true;
68
/// This value is used to detect the near edges to connect.
69
real_t edge_connection_margin = NavigationDefaults3D::EDGE_CONNECTION_MARGIN;
70
71
/// This value is used to limit how far links search to find polygons to connect to.
72
real_t link_connection_radius = NavigationDefaults3D::LINK_CONNECTION_RADIUS;
73
74
bool map_settings_dirty = true;
75
76
/// Map regions
77
LocalVector<NavRegion3D *> regions;
78
79
/// Map links
80
LocalVector<NavLink3D *> links;
81
82
/// RVO avoidance worlds
83
RVO2D::RVOSimulator2D rvo_simulation_2d;
84
RVO3D::RVOSimulator3D rvo_simulation_3d;
85
86
/// avoidance controlled agents
87
LocalVector<NavAgent3D *> active_2d_avoidance_agents;
88
LocalVector<NavAgent3D *> active_3d_avoidance_agents;
89
90
/// dirty flag when one of the agent's arrays are modified
91
bool agents_dirty = true;
92
93
/// All the Agents (even the controlled one)
94
LocalVector<NavAgent3D *> agents;
95
96
/// All the avoidance obstacles (both static and dynamic)
97
LocalVector<NavObstacle3D *> obstacles;
98
99
/// Are rvo obstacles modified?
100
bool obstacles_dirty = true;
101
102
/// Change the id each time the map is updated.
103
uint32_t iteration_id = 0;
104
105
bool use_threads = true;
106
bool avoidance_use_multiple_threads = true;
107
bool avoidance_use_high_priority_threads = true;
108
109
// Performance Monitor
110
Nav3D::PerformanceData performance_data;
111
112
struct {
113
struct {
114
RWLock rwlock;
115
SelfList<NavRegion3D>::List list;
116
} regions;
117
struct {
118
RWLock rwlock;
119
SelfList<NavLink3D>::List list;
120
} links;
121
struct {
122
RWLock rwlock;
123
SelfList<NavAgent3D>::List list;
124
} agents;
125
struct {
126
RWLock rwlock;
127
SelfList<NavObstacle3D>::List list;
128
} obstacles;
129
} sync_dirty_requests;
130
131
struct {
132
struct {
133
RWLock rwlock;
134
SelfList<NavRegion3D>::List list;
135
} regions;
136
} async_dirty_requests;
137
138
int path_query_slots_max = 4;
139
140
bool use_async_iterations = true;
141
142
uint32_t iteration_slot_index = 0;
143
LocalVector<NavMapIteration3D> iteration_slots;
144
mutable RWLock iteration_slot_rwlock;
145
146
NavMapIterationBuild3D iteration_build;
147
WorkerThreadPool::TaskID iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
148
static void _build_iteration_threaded(void *p_arg);
149
150
bool iteration_dirty = true;
151
bool iteration_building = false;
152
bool iteration_ready = false;
153
154
void _build_iteration();
155
void _sync_iteration();
156
157
public:
158
NavMap3D();
159
~NavMap3D();
160
161
uint32_t get_iteration_id() const { return iteration_id; }
162
163
void set_up(Vector3 p_up);
164
Vector3 get_up() const {
165
return up;
166
}
167
168
void set_cell_size(real_t p_cell_size);
169
real_t get_cell_size() const {
170
return cell_size;
171
}
172
173
void set_cell_height(real_t p_cell_height);
174
real_t get_cell_height() const { return cell_height; }
175
176
void set_merge_rasterizer_cell_scale(float p_value);
177
float get_merge_rasterizer_cell_scale() const {
178
return merge_rasterizer_cell_scale;
179
}
180
181
void set_use_edge_connections(bool p_enabled);
182
bool get_use_edge_connections() const {
183
return use_edge_connections;
184
}
185
186
void set_edge_connection_margin(real_t p_edge_connection_margin);
187
real_t get_edge_connection_margin() const {
188
return edge_connection_margin;
189
}
190
191
void set_link_connection_radius(real_t p_link_connection_radius);
192
real_t get_link_connection_radius() const {
193
return link_connection_radius;
194
}
195
196
Nav3D::PointKey get_point_key(const Vector3 &p_pos) const;
197
const Vector3 &get_merge_rasterizer_cell_size() const;
198
199
void query_path(NavMeshQueries3D::NavMeshPathQueryTask3D &p_query_task);
200
201
Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
202
Vector3 get_closest_point(const Vector3 &p_point) const;
203
Vector3 get_closest_point_normal(const Vector3 &p_point) const;
204
Nav3D::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
205
RID get_closest_point_owner(const Vector3 &p_point) const;
206
207
void add_region(NavRegion3D *p_region);
208
void remove_region(NavRegion3D *p_region);
209
const LocalVector<NavRegion3D *> &get_regions() const {
210
return regions;
211
}
212
213
void add_link(NavLink3D *p_link);
214
void remove_link(NavLink3D *p_link);
215
const LocalVector<NavLink3D *> &get_links() const {
216
return links;
217
}
218
219
bool has_agent(NavAgent3D *agent) const;
220
void add_agent(NavAgent3D *agent);
221
void remove_agent(NavAgent3D *agent);
222
const LocalVector<NavAgent3D *> &get_agents() const {
223
return agents;
224
}
225
226
void set_agent_as_controlled(NavAgent3D *agent);
227
void remove_agent_as_controlled(NavAgent3D *agent);
228
229
bool has_obstacle(NavObstacle3D *obstacle) const;
230
void add_obstacle(NavObstacle3D *obstacle);
231
void remove_obstacle(NavObstacle3D *obstacle);
232
const LocalVector<NavObstacle3D *> &get_obstacles() const {
233
return obstacles;
234
}
235
236
Vector3 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
237
238
void sync();
239
void step(double p_delta_time);
240
void dispatch_callbacks();
241
242
// Performance Monitor
243
int get_pm_region_count() const { return performance_data.pm_region_count; }
244
int get_pm_agent_count() const { return performance_data.pm_agent_count; }
245
int get_pm_link_count() const { return performance_data.pm_link_count; }
246
int get_pm_polygon_count() const { return performance_data.pm_polygon_count; }
247
int get_pm_edge_count() const { return performance_data.pm_edge_count; }
248
int get_pm_edge_merge_count() const { return performance_data.pm_edge_merge_count; }
249
int get_pm_edge_connection_count() const { return performance_data.pm_edge_connection_count; }
250
int get_pm_edge_free_count() const { return performance_data.pm_edge_free_count; }
251
int get_pm_obstacle_count() const { return performance_data.pm_obstacle_count; }
252
253
int get_region_connections_count(NavRegion3D *p_region) const;
254
Vector3 get_region_connection_pathway_start(NavRegion3D *p_region, int p_connection_id) const;
255
Vector3 get_region_connection_pathway_end(NavRegion3D *p_region, int p_connection_id) const;
256
257
void add_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request);
258
void remove_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request);
259
260
void add_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);
261
void add_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);
262
void add_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);
263
void add_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);
264
265
void remove_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request);
266
void remove_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request);
267
void remove_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request);
268
void remove_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request);
269
270
void set_use_async_iterations(bool p_enabled);
271
bool get_use_async_iterations() const;
272
273
private:
274
void _sync_dirty_map_update_requests();
275
void _sync_dirty_avoidance_update_requests();
276
void _sync_async_tasks();
277
278
void compute_single_step(uint32_t index, NavAgent3D **agent);
279
280
void compute_single_avoidance_step_2d(uint32_t index, NavAgent3D **agent);
281
void compute_single_avoidance_step_3d(uint32_t index, NavAgent3D **agent);
282
283
void _sync_avoidance();
284
void _update_rvo_simulation();
285
void _update_rvo_obstacles_tree_2d();
286
void _update_rvo_agents_tree_2d();
287
void _update_rvo_agents_tree_3d();
288
289
void _update_merge_rasterizer_cell_dimensions();
290
};
291
292