Debian Codegraph @

scene/resources/2d/navigation_mesh_source_geometry_data_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_mesh_source_geometry_data_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 "navigation_mesh_source_geometry_data_2d.h"
32
33 #include "core/variant/typed_array.h"
34
35 void NavigationMeshSourceGeometryData2D::clear() {
36 RWLockWrite write_lock(geometry_rwlock);
37 traversable_outlines.clear();
38 obstruction_outlines.clear();
39 _projected_obstructions.clear();
40 bounds_dirty = true;
41 }
42
43 bool NavigationMeshSourceGeometryData2D::has_data() {
44 RWLockRead read_lock(geometry_rwlock);
45 return traversable_outlines.size();
46 }
47
48 void NavigationMeshSourceGeometryData2D::clear_projected_obstructions() {
49 RWLockWrite write_lock(geometry_rwlock);
50 _projected_obstructions.clear();
51 bounds_dirty = true;
52 }
53
54 void NavigationMeshSourceGeometryData2D::_set_traversable_outlines(const Vector<Vector<Vector2>> &p_traversable_outlines) {
55 RWLockWrite write_lock(geometry_rwlock);
56 traversable_outlines = p_traversable_outlines;
57 bounds_dirty = true;
58 }
59
60 void NavigationMeshSourceGeometryData2D::_set_obstruction_outlines(const Vector<Vector<Vector2>> &p_obstruction_outlines) {
61 RWLockWrite write_lock(geometry_rwlock);
62 obstruction_outlines = p_obstruction_outlines;
63 bounds_dirty = true;
64 }
65
66 const Vector<Vector<Vector2>> &NavigationMeshSourceGeometryData2D::_get_traversable_outlines() const {
67 RWLockRead read_lock(geometry_rwlock);
68 return traversable_outlines;
69 }
70
71 const Vector<Vector<Vector2>> &NavigationMeshSourceGeometryData2D::_get_obstruction_outlines() const {
72 RWLockRead read_lock(geometry_rwlock);
73 return obstruction_outlines;
74 }
75
76 void NavigationMeshSourceGeometryData2D::_add_traversable_outline(const Vector<Vector2> &p_shape_outline) {
77 if (p_shape_outline.size() > 1) {
78 RWLockWrite write_lock(geometry_rwlock);
79 traversable_outlines.push_back(p_shape_outline);
80 bounds_dirty = true;
81 }
82 }
83
84 void NavigationMeshSourceGeometryData2D::_add_obstruction_outline(const Vector<Vector2> &p_shape_outline) {
85 if (p_shape_outline.size() > 1) {
86 RWLockWrite write_lock(geometry_rwlock);
87 obstruction_outlines.push_back(p_shape_outline);
88 bounds_dirty = true;
89 }
90 }
91
92 void NavigationMeshSourceGeometryData2D::set_traversable_outlines(const TypedArray<Vector<Vector2>> &p_traversable_outlines) {
93 RWLockWrite write_lock(geometry_rwlock);
94 traversable_outlines.resize(p_traversable_outlines.size());
95 for (int i = 0; i < p_traversable_outlines.size(); i++) {
96 traversable_outlines.write[i] = p_traversable_outlines[i];
97 }
98 bounds_dirty = true;
99 }
100
101 TypedArray<Vector<Vector2>> NavigationMeshSourceGeometryData2D::get_traversable_outlines() const {
102 RWLockRead read_lock(geometry_rwlock);
103 TypedArray<Vector<Vector2>> typed_array_traversable_outlines;
104 typed_array_traversable_outlines.resize(traversable_outlines.size());
105 for (int i = 0; i < typed_array_traversable_outlines.size(); i++) {
106 typed_array_traversable_outlines[i] = traversable_outlines[i];
107 }
108
109 return typed_array_traversable_outlines;
110 }
111
112 void NavigationMeshSourceGeometryData2D::set_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines) {
113 RWLockWrite write_lock(geometry_rwlock);
114 obstruction_outlines.resize(p_obstruction_outlines.size());
115 for (int i = 0; i < p_obstruction_outlines.size(); i++) {
116 obstruction_outlines.write[i] = p_obstruction_outlines[i];
117 }
118 bounds_dirty = true;
119 }
120
121 TypedArray<Vector<Vector2>> NavigationMeshSourceGeometryData2D::get_obstruction_outlines() const {
122 RWLockRead read_lock(geometry_rwlock);
123 TypedArray<Vector<Vector2>> typed_array_obstruction_outlines;
124 typed_array_obstruction_outlines.resize(obstruction_outlines.size());
125 for (int i = 0; i < typed_array_obstruction_outlines.size(); i++) {
126 typed_array_obstruction_outlines[i] = obstruction_outlines[i];
127 }
128
129 return typed_array_obstruction_outlines;
130 }
131
132 void NavigationMeshSourceGeometryData2D::append_traversable_outlines(const TypedArray<Vector<Vector2>> &p_traversable_outlines) {
133 RWLockWrite write_lock(geometry_rwlock);
134 int traversable_outlines_size = traversable_outlines.size();
135 traversable_outlines.resize(traversable_outlines_size + p_traversable_outlines.size());
136 for (int i = traversable_outlines_size; i < p_traversable_outlines.size(); i++) {
137 traversable_outlines.write[i] = p_traversable_outlines[i];
138 }
139 bounds_dirty = true;
140 }
141
142 void NavigationMeshSourceGeometryData2D::append_obstruction_outlines(const TypedArray<Vector<Vector2>> &p_obstruction_outlines) {
143 RWLockWrite write_lock(geometry_rwlock);
144 int obstruction_outlines_size = obstruction_outlines.size();
145 obstruction_outlines.resize(obstruction_outlines_size + p_obstruction_outlines.size());
146 for (int i = obstruction_outlines_size; i < p_obstruction_outlines.size(); i++) {
147 obstruction_outlines.write[i] = p_obstruction_outlines[i];
148 }
149 bounds_dirty = true;
150 }
151
152 void NavigationMeshSourceGeometryData2D::add_traversable_outline(const PackedVector2Array &p_shape_outline) {
153 if (p_shape_outline.size() > 1) {
154 RWLockWrite write_lock(geometry_rwlock);
155 Vector<Vector2> traversable_outline;
156 traversable_outline.resize(p_shape_outline.size());
157 for (int i = 0; i < p_shape_outline.size(); i++) {
158 traversable_outline.write[i] = p_shape_outline[i];
159 }
160 traversable_outlines.push_back(traversable_outline);
161 bounds_dirty = true;
162 }
163 }
164
165 void NavigationMeshSourceGeometryData2D::add_obstruction_outline(const PackedVector2Array &p_shape_outline) {
166 if (p_shape_outline.size() > 1) {
167 RWLockWrite write_lock(geometry_rwlock);
168 Vector<Vector2> obstruction_outline;
169 obstruction_outline.resize(p_shape_outline.size());
170 for (int i = 0; i < p_shape_outline.size(); i++) {
171 obstruction_outline.write[i] = p_shape_outline[i];
172 }
173 obstruction_outlines.push_back(obstruction_outline);
174 bounds_dirty = true;
175 }
176 }
177
178 void NavigationMeshSourceGeometryData2D::merge(const Ref<NavigationMeshSourceGeometryData2D> &p_other_geometry) {
179 ERR_FAIL_COND(p_other_geometry.is_null());
180
181 Vector<Vector<Vector2>> other_traversable_outlines;
182 Vector<Vector<Vector2>> other_obstruction_outlines;
183 Vector<ProjectedObstruction> other_projected_obstructions;
184
185 p_other_geometry->get_data(other_traversable_outlines, other_obstruction_outlines, other_projected_obstructions);
186
187 RWLockWrite write_lock(geometry_rwlock);
188 traversable_outlines.append_array(other_traversable_outlines);
189 obstruction_outlines.append_array(other_obstruction_outlines);
190 _projected_obstructions.append_array(other_projected_obstructions);
191 bounds_dirty = true;
192 }
193
194 void NavigationMeshSourceGeometryData2D::add_projected_obstruction(const Vector<Vector2> &p_vertices, bool p_carve) {
195 ERR_FAIL_COND(p_vertices.size() < 2);
196
197 ProjectedObstruction projected_obstruction;
198 projected_obstruction.vertices.resize(p_vertices.size() * 2);
199 projected_obstruction.carve = p_carve;
200
201 float *obstruction_vertices_ptrw = projected_obstruction.vertices.ptrw();
202
203 int vertex_index = 0;
204 for (const Vector2 &vertex : p_vertices) {
205 obstruction_vertices_ptrw[vertex_index++] = vertex.x;
206 obstruction_vertices_ptrw[vertex_index++] = vertex.y;
207 }
208
209 RWLockWrite write_lock(geometry_rwlock);
210 _projected_obstructions.push_back(projected_obstruction);
211 bounds_dirty = true;
212 }
213
214 void NavigationMeshSourceGeometryData2D::set_projected_obstructions(const Array &p_array) {
215 clear_projected_obstructions();
216
217 for (int i = 0; i < p_array.size(); i++) {
218 Dictionary data = p_array[i];
219 ERR_FAIL_COND(!data.has("version"));
220
221 uint32_t po_version = data["version"];
222
223 if (po_version == 1) {
224 ERR_FAIL_COND(!data.has("vertices"));
225 ERR_FAIL_COND(!data.has("carve"));
226 }
227
228 ProjectedObstruction projected_obstruction;
229 projected_obstruction.vertices = Vector<float>(data["vertices"]);
230 projected_obstruction.carve = data["carve"];
231
232 RWLockWrite write_lock(geometry_rwlock);
233 _projected_obstructions.push_back(projected_obstruction);
234 bounds_dirty = true;
235 }
236 }
237
238 Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> NavigationMeshSourceGeometryData2D::_get_projected_obstructions() const {
239 RWLockRead read_lock(geometry_rwlock);
240 return _projected_obstructions;
241 }
242
243 Array NavigationMeshSourceGeometryData2D::get_projected_obstructions() const {
244 RWLockRead read_lock(geometry_rwlock);
245
246 Array ret;
247 ret.resize(_projected_obstructions.size());
248
249 for (int i = 0; i < _projected_obstructions.size(); i++) {
250 const ProjectedObstruction &projected_obstruction = _projected_obstructions[i];
251
252 Dictionary data;
253 data["version"] = (int)ProjectedObstruction::VERSION;
254 data["vertices"] = projected_obstruction.vertices;
255 data["carve"] = projected_obstruction.carve;
256
257 ret[i] = data;
258 }
259
260 return ret;
261 }
262
263 bool NavigationMeshSourceGeometryData2D::_set(const StringName &p_name, const Variant &p_value) {
264 if (p_name == "projected_obstructions") {
265 set_projected_obstructions(p_value);
266 return true;
267 }
268 return false;
269 }
270
271 bool NavigationMeshSourceGeometryData2D::_get(const StringName &p_name, Variant &r_ret) const {
272 if (p_name == "projected_obstructions") {
273 r_ret = get_projected_obstructions();
274 return true;
275 }
276 return false;
277 }
278
279 void NavigationMeshSourceGeometryData2D::set_data(const Vector<Vector<Vector2>> &p_traversable_outlines, const Vector<Vector<Vector2>> &p_obstruction_outlines, Vector<ProjectedObstruction> &p_projected_obstructions) {
280 RWLockWrite write_lock(geometry_rwlock);
281 traversable_outlines = p_traversable_outlines;
282 obstruction_outlines = p_obstruction_outlines;
283 _projected_obstructions = p_projected_obstructions;
284 bounds_dirty = true;
285 }
286
287 void NavigationMeshSourceGeometryData2D::get_data(Vector<Vector<Vector2>> &r_traversable_outlines, Vector<Vector<Vector2>> &r_obstruction_outlines, Vector<ProjectedObstruction> &r_projected_obstructions) {
288 RWLockRead read_lock(geometry_rwlock);
289 r_traversable_outlines = traversable_outlines;
290 r_obstruction_outlines = obstruction_outlines;
291 r_projected_obstructions = _projected_obstructions;
292 }
293
294 Rect2 NavigationMeshSourceGeometryData2D::get_bounds() {
295 geometry_rwlock.read_lock();
296
297 if (bounds_dirty) {
298 geometry_rwlock.read_unlock();
299 RWLockWrite write_lock(geometry_rwlock);
300
301 bounds_dirty = false;
302 bounds = Rect2();
303 bool first_vertex = true;
304
305 for (const Vector<Vector2> &traversable_outline : traversable_outlines) {
306 for (const Vector2 &traversable_point : traversable_outline) {
307 if (first_vertex) {
308 first_vertex = false;
309 bounds.position = traversable_point;
310 } else {
311 bounds.expand_to(traversable_point);
312 }
313 }
314 }
315
316 for (const Vector<Vector2> &obstruction_outline : obstruction_outlines) {
317 for (const Vector2 &obstruction_point : obstruction_outline) {
318 if (first_vertex) {
319 first_vertex = false;
320 bounds.position = obstruction_point;
321 } else {
322 bounds.expand_to(obstruction_point);
323 }
324 }
325 }
326
327 for (const ProjectedObstruction &projected_obstruction : _projected_obstructions) {
328 for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) {
329 const Vector2 vertex = Vector2(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]);
330 if (first_vertex) {
331 first_vertex = false;
332 bounds.position = vertex;
333 } else {
334 bounds.expand_to(vertex);
335 }
336 }
337 }
338 } else {
339 geometry_rwlock.read_unlock();
340 }
341
342 RWLockRead read_lock(geometry_rwlock);
343 return bounds;
344 }
345
346 void NavigationMeshSourceGeometryData2D::_bind_methods() {
347 ClassDB::bind_method(D_METHOD("clear"), &NavigationMeshSourceGeometryData2D::clear);
348 ClassDB::bind_method(D_METHOD("has_data"), &NavigationMeshSourceGeometryData2D::has_data);
349
350 ClassDB::bind_method(D_METHOD("set_traversable_outlines", "traversable_outlines"), &NavigationMeshSourceGeometryData2D::set_traversable_outlines);
351 ClassDB::bind_method(D_METHOD("get_traversable_outlines"), &NavigationMeshSourceGeometryData2D::get_traversable_outlines);
352
353 ClassDB::bind_method(D_METHOD("set_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::set_obstruction_outlines);
354 ClassDB::bind_method(D_METHOD("get_obstruction_outlines"), &NavigationMeshSourceGeometryData2D::get_obstruction_outlines);
355
356 ClassDB::bind_method(D_METHOD("append_traversable_outlines", "traversable_outlines"), &NavigationMeshSourceGeometryData2D::append_traversable_outlines);
357 ClassDB::bind_method(D_METHOD("append_obstruction_outlines", "obstruction_outlines"), &NavigationMeshSourceGeometryData2D::append_obstruction_outlines);
358
359 ClassDB::bind_method(D_METHOD("add_traversable_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_traversable_outline);
360 ClassDB::bind_method(D_METHOD("add_obstruction_outline", "shape_outline"), &NavigationMeshSourceGeometryData2D::add_obstruction_outline);
361
362 ClassDB::bind_method(D_METHOD("merge", "other_geometry"), &NavigationMeshSourceGeometryData2D::merge);
363
364 ClassDB::bind_method(D_METHOD("add_projected_obstruction", "vertices", "carve"), &NavigationMeshSourceGeometryData2D::add_projected_obstruction);
365 ClassDB::bind_method(D_METHOD("clear_projected_obstructions"), &NavigationMeshSourceGeometryData2D::clear_projected_obstructions);
366 ClassDB::bind_method(D_METHOD("set_projected_obstructions", "projected_obstructions"), &NavigationMeshSourceGeometryData2D::set_projected_obstructions);
367 ClassDB::bind_method(D_METHOD("get_projected_obstructions"), &NavigationMeshSourceGeometryData2D::get_projected_obstructions);
368
369 ClassDB::bind_method(D_METHOD("get_bounds"), &NavigationMeshSourceGeometryData2D::get_bounds);
370
371 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "traversable_outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_traversable_outlines", "get_traversable_outlines");
372 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "obstruction_outlines", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_obstruction_outlines", "get_obstruction_outlines");
373 ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "projected_obstructions", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "set_projected_obstructions", "get_projected_obstructions");
374 }