Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/2d/polygon_path_finder.cpp
23418 views
1
/**************************************************************************/
2
/* polygon_path_finder.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 "polygon_path_finder.h"
32
33
#include "core/math/geometry_2d.h"
34
#include "core/object/class_db.h"
35
36
bool PolygonPathFinder::_is_point_inside(const Vector2 &p_point) const {
37
int crosses = 0;
38
39
for (const Edge &E : edges) {
40
const Edge &e = E;
41
42
Vector2 a = points[e.points[0]].pos;
43
Vector2 b = points[e.points[1]].pos;
44
45
if (Geometry2D::segment_intersects_segment(a, b, p_point, outside_point, nullptr)) {
46
crosses++;
47
}
48
}
49
50
return crosses & 1;
51
}
52
53
void PolygonPathFinder::setup(const Vector<Vector2> &p_points, const Vector<int> &p_connections) {
54
ERR_FAIL_COND(p_connections.size() & 1);
55
56
points.clear();
57
edges.clear();
58
59
//insert points
60
61
int point_count = p_points.size();
62
points.resize(point_count + 2);
63
bounds = Rect2();
64
65
for (int i = 0; i < p_points.size(); i++) {
66
points.write[i].pos = p_points[i];
67
points.write[i].penalty = 0;
68
69
outside_point = i == 0 ? p_points[0] : p_points[i].max(outside_point);
70
71
if (i == 0) {
72
bounds.position = points[i].pos;
73
} else {
74
bounds.expand_to(points[i].pos);
75
}
76
}
77
78
outside_point.x += 20.451 + Math::randf() * 10.2039;
79
outside_point.y += 21.193 + Math::randf() * 12.5412;
80
81
//insert edges (which are also connections)
82
83
for (int i = 0; i < p_connections.size(); i += 2) {
84
Edge e(p_connections[i], p_connections[i + 1]);
85
ERR_FAIL_INDEX(e.points[0], point_count);
86
ERR_FAIL_INDEX(e.points[1], point_count);
87
points.write[p_connections[i]].connections.insert(p_connections[i + 1]);
88
points.write[p_connections[i + 1]].connections.insert(p_connections[i]);
89
edges.insert(e);
90
}
91
92
//fill the remaining connections based on visibility
93
94
for (int i = 0; i < point_count; i++) {
95
for (int j = i + 1; j < point_count; j++) {
96
if (edges.has(Edge(i, j))) {
97
continue; //if in edge ignore
98
}
99
100
Vector2 from = points[i].pos;
101
Vector2 to = points[j].pos;
102
103
if (!_is_point_inside(from * 0.5 + to * 0.5)) { //connection between points in inside space
104
continue;
105
}
106
107
bool valid = true;
108
109
for (const Edge &E : edges) {
110
const Edge &e = E;
111
if (e.points[0] == i || e.points[1] == i || e.points[0] == j || e.points[1] == j) {
112
continue;
113
}
114
115
Vector2 a = points[e.points[0]].pos;
116
Vector2 b = points[e.points[1]].pos;
117
118
if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {
119
valid = false;
120
break;
121
}
122
}
123
124
if (valid) {
125
points.write[i].connections.insert(j);
126
points.write[j].connections.insert(i);
127
}
128
}
129
}
130
}
131
132
Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector2 &p_to) {
133
Vector<Vector2> path;
134
135
Vector2 from = p_from;
136
Vector2 to = p_to;
137
Edge ignore_from_edge(-1, -1);
138
Edge ignore_to_edge(-1, -1);
139
140
if (!_is_point_inside(from)) {
141
float closest_dist = 1e20f;
142
Vector2 closest_point;
143
144
for (const Edge &E : edges) {
145
const Edge &e = E;
146
const Vector2 segment_a = points[e.points[0]].pos;
147
const Vector2 segment_b = points[e.points[1]].pos;
148
Vector2 closest = Geometry2D::get_closest_point_to_segment(from, segment_a, segment_b);
149
float d = from.distance_squared_to(closest);
150
151
if (d < closest_dist) {
152
ignore_from_edge = E;
153
closest_dist = d;
154
closest_point = closest;
155
}
156
}
157
158
from = closest_point;
159
};
160
161
if (!_is_point_inside(to)) {
162
float closest_dist = 1e20f;
163
Vector2 closest_point;
164
165
for (const Edge &E : edges) {
166
const Edge &e = E;
167
const Vector2 segment_a = points[e.points[0]].pos;
168
const Vector2 segment_b = points[e.points[1]].pos;
169
Vector2 closest = Geometry2D::get_closest_point_to_segment(to, segment_a, segment_b);
170
float d = to.distance_squared_to(closest);
171
172
if (d < closest_dist) {
173
ignore_to_edge = E;
174
closest_dist = d;
175
closest_point = closest;
176
}
177
}
178
179
to = closest_point;
180
};
181
182
//test direct connection
183
{
184
bool can_see_eachother = true;
185
186
for (const Edge &E : edges) {
187
const Edge &e = E;
188
if (e.points[0] == ignore_from_edge.points[0] && e.points[1] == ignore_from_edge.points[1]) {
189
continue;
190
}
191
if (e.points[0] == ignore_to_edge.points[0] && e.points[1] == ignore_to_edge.points[1]) {
192
continue;
193
}
194
195
Vector2 a = points[e.points[0]].pos;
196
Vector2 b = points[e.points[1]].pos;
197
198
if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {
199
can_see_eachother = false;
200
break;
201
}
202
}
203
204
if (can_see_eachother) {
205
path.push_back(from);
206
path.push_back(to);
207
return path;
208
}
209
}
210
211
//add to graph
212
213
int aidx = points.size() - 2;
214
int bidx = points.size() - 1;
215
points.write[aidx].pos = from;
216
points.write[bidx].pos = to;
217
points.write[aidx].distance = 0;
218
points.write[bidx].distance = 0;
219
points.write[aidx].prev = -1;
220
points.write[bidx].prev = -1;
221
points.write[aidx].penalty = 0;
222
points.write[bidx].penalty = 0;
223
224
for (int i = 0; i < points.size() - 2; i++) {
225
bool valid_a = true;
226
bool valid_b = true;
227
points.write[i].prev = -1;
228
points.write[i].distance = 0;
229
230
if (!_is_point_inside(from * 0.5 + points[i].pos * 0.5)) {
231
valid_a = false;
232
}
233
234
if (!_is_point_inside(to * 0.5 + points[i].pos * 0.5)) {
235
valid_b = false;
236
}
237
238
for (const Edge &E : edges) {
239
const Edge &e = E;
240
241
if (e.points[0] == i || e.points[1] == i) {
242
continue;
243
}
244
245
Vector2 a = points[e.points[0]].pos;
246
Vector2 b = points[e.points[1]].pos;
247
248
if (valid_a) {
249
if (e.points[0] != ignore_from_edge.points[1] &&
250
e.points[1] != ignore_from_edge.points[1] &&
251
e.points[0] != ignore_from_edge.points[0] &&
252
e.points[1] != ignore_from_edge.points[0]) {
253
if (Geometry2D::segment_intersects_segment(a, b, from, points[i].pos, nullptr)) {
254
valid_a = false;
255
}
256
}
257
}
258
259
if (valid_b) {
260
if (e.points[0] != ignore_to_edge.points[1] &&
261
e.points[1] != ignore_to_edge.points[1] &&
262
e.points[0] != ignore_to_edge.points[0] &&
263
e.points[1] != ignore_to_edge.points[0]) {
264
if (Geometry2D::segment_intersects_segment(a, b, to, points[i].pos, nullptr)) {
265
valid_b = false;
266
}
267
}
268
}
269
270
if (!valid_a && !valid_b) {
271
break;
272
}
273
}
274
275
if (valid_a) {
276
points.write[i].connections.insert(aidx);
277
points.write[aidx].connections.insert(i);
278
}
279
280
if (valid_b) {
281
points.write[i].connections.insert(bidx);
282
points.write[bidx].connections.insert(i);
283
}
284
}
285
//solve graph
286
287
HashSet<int> open_list;
288
289
points.write[aidx].distance = 0;
290
points.write[aidx].prev = aidx;
291
for (const int &E : points[aidx].connections) {
292
open_list.insert(E);
293
points.write[E].distance = from.distance_to(points[E].pos);
294
points.write[E].prev = aidx;
295
}
296
297
bool found_route = false;
298
299
while (true) {
300
if (open_list.is_empty()) {
301
print_verbose("Open list empty.");
302
break;
303
}
304
//check open list
305
306
int least_cost_point = -1;
307
float least_cost = 1e30;
308
309
//this could be faster (cache previous results)
310
for (const int &E : open_list) {
311
const Point &p = points[E];
312
float cost = p.distance;
313
cost += p.pos.distance_to(to);
314
cost += p.penalty;
315
316
if (cost < least_cost) {
317
least_cost_point = E;
318
least_cost = cost;
319
}
320
}
321
322
const Point &np = points[least_cost_point];
323
//open the neighbors for search
324
325
for (const int &E : np.connections) {
326
Point &p = points.write[E];
327
float distance = np.pos.distance_to(p.pos) + np.distance;
328
329
if (p.prev != -1) {
330
//oh this was visited already, can we win the cost?
331
332
if (p.distance > distance) {
333
p.prev = least_cost_point; //reassign previous
334
p.distance = distance;
335
}
336
} else {
337
//add to open neighbors
338
339
p.prev = least_cost_point;
340
p.distance = distance;
341
open_list.insert(E);
342
343
if (E == bidx) {
344
//oh my reached end! stop algorithm
345
found_route = true;
346
break;
347
}
348
}
349
}
350
351
if (found_route) {
352
break;
353
}
354
355
open_list.erase(least_cost_point);
356
}
357
358
if (found_route) {
359
int at = bidx;
360
path.push_back(points[at].pos);
361
do {
362
at = points[at].prev;
363
path.push_back(points[at].pos);
364
} while (at != aidx);
365
366
path.reverse();
367
}
368
369
for (int i = 0; i < points.size() - 2; i++) {
370
points.write[i].connections.erase(aidx);
371
points.write[i].connections.erase(bidx);
372
points.write[i].prev = -1;
373
points.write[i].distance = 0;
374
}
375
376
points.write[aidx].connections.clear();
377
points.write[aidx].prev = -1;
378
points.write[aidx].distance = 0;
379
points.write[bidx].connections.clear();
380
points.write[bidx].prev = -1;
381
points.write[bidx].distance = 0;
382
383
return path;
384
}
385
386
void PolygonPathFinder::_set_data(const Dictionary &p_data) {
387
ERR_FAIL_COND(!p_data.has("points"));
388
ERR_FAIL_COND(!p_data.has("connections"));
389
ERR_FAIL_COND(!p_data.has("segments"));
390
ERR_FAIL_COND(!p_data.has("bounds"));
391
392
Vector<Vector2> p = p_data["points"];
393
Array c = p_data["connections"];
394
395
ERR_FAIL_COND(c.size() != p.size());
396
if (c.size()) {
397
return;
398
}
399
400
int pc = p.size();
401
points.resize(pc + 2);
402
403
const Vector2 *pr = p.ptr();
404
for (int i = 0; i < pc; i++) {
405
points.write[i].pos = pr[i];
406
Vector<int> con = c[i];
407
const int *cr = con.ptr();
408
int cc = con.size();
409
for (int j = 0; j < cc; j++) {
410
points.write[i].connections.insert(cr[j]);
411
}
412
}
413
414
if (p_data.has("penalties")) {
415
Vector<real_t> penalties = p_data["penalties"];
416
if (penalties.size() == pc) {
417
const real_t *pr2 = penalties.ptr();
418
for (int i = 0; i < pc; i++) {
419
points.write[i].penalty = pr2[i];
420
}
421
}
422
}
423
424
Vector<int> segs = p_data["segments"];
425
int sc = segs.size();
426
ERR_FAIL_COND(sc & 1);
427
const int *sr = segs.ptr();
428
for (int i = 0; i < sc; i += 2) {
429
Edge e(sr[i], sr[i + 1]);
430
edges.insert(e);
431
}
432
bounds = p_data["bounds"];
433
}
434
435
Dictionary PolygonPathFinder::_get_data() const {
436
Dictionary d;
437
Vector<Vector2> p;
438
Vector<int> ind;
439
Array path_connections;
440
p.resize(MAX(0, points.size() - 2));
441
path_connections.resize(MAX(0, points.size() - 2));
442
ind.resize(edges.size() * 2);
443
Vector<real_t> penalties;
444
penalties.resize(MAX(0, points.size() - 2));
445
{
446
Vector2 *wp = p.ptrw();
447
real_t *pw = penalties.ptrw();
448
449
for (int i = 0; i < points.size() - 2; i++) {
450
wp[i] = points[i].pos;
451
pw[i] = points[i].penalty;
452
Vector<int> c;
453
c.resize(points[i].connections.size());
454
{
455
int *cw = c.ptrw();
456
int idx = 0;
457
for (const int &E : points[i].connections) {
458
cw[idx++] = E;
459
}
460
}
461
path_connections[i] = c;
462
}
463
}
464
{
465
int *iw = ind.ptrw();
466
int idx = 0;
467
for (const Edge &E : edges) {
468
iw[idx++] = E.points[0];
469
iw[idx++] = E.points[1];
470
}
471
}
472
473
d["bounds"] = bounds;
474
d["points"] = p;
475
d["penalties"] = penalties;
476
d["connections"] = path_connections;
477
d["segments"] = ind;
478
479
return d;
480
}
481
482
bool PolygonPathFinder::is_point_inside(const Vector2 &p_point) const {
483
return _is_point_inside(p_point);
484
}
485
486
Vector2 PolygonPathFinder::get_closest_point(const Vector2 &p_point) const {
487
float closest_dist = 1e20f;
488
Vector2 closest_point;
489
490
for (const Edge &E : edges) {
491
const Edge &e = E;
492
const Vector2 segment_a = points[e.points[0]].pos;
493
const Vector2 segment_b = points[e.points[1]].pos;
494
Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, segment_a, segment_b);
495
float d = p_point.distance_squared_to(closest);
496
497
if (d < closest_dist) {
498
closest_dist = d;
499
closest_point = closest;
500
}
501
}
502
503
ERR_FAIL_COND_V(Math::is_equal_approx(closest_dist, 1e20f), Vector2());
504
505
return closest_point;
506
}
507
508
Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2 &p_from, const Vector2 &p_to) const {
509
Vector<Vector2> inters;
510
511
for (const Edge &E : edges) {
512
Vector2 a = points[E.points[0]].pos;
513
Vector2 b = points[E.points[1]].pos;
514
515
Vector2 res;
516
if (Geometry2D::segment_intersects_segment(a, b, p_from, p_to, &res)) {
517
inters.push_back(res);
518
}
519
}
520
521
return inters;
522
}
523
524
Rect2 PolygonPathFinder::get_bounds() const {
525
return bounds;
526
}
527
528
void PolygonPathFinder::set_point_penalty(int p_point, float p_penalty) {
529
ERR_FAIL_INDEX(p_point, points.size() - 2);
530
points.write[p_point].penalty = p_penalty;
531
}
532
533
float PolygonPathFinder::get_point_penalty(int p_point) const {
534
ERR_FAIL_INDEX_V(p_point, points.size() - 2, 0);
535
return points[p_point].penalty;
536
}
537
538
void PolygonPathFinder::_bind_methods() {
539
ClassDB::bind_method(D_METHOD("setup", "points", "connections"), &PolygonPathFinder::setup);
540
ClassDB::bind_method(D_METHOD("find_path", "from", "to"), &PolygonPathFinder::find_path);
541
ClassDB::bind_method(D_METHOD("get_intersections", "from", "to"), &PolygonPathFinder::get_intersections);
542
ClassDB::bind_method(D_METHOD("get_closest_point", "point"), &PolygonPathFinder::get_closest_point);
543
ClassDB::bind_method(D_METHOD("is_point_inside", "point"), &PolygonPathFinder::is_point_inside);
544
ClassDB::bind_method(D_METHOD("set_point_penalty", "idx", "penalty"), &PolygonPathFinder::set_point_penalty);
545
ClassDB::bind_method(D_METHOD("get_point_penalty", "idx"), &PolygonPathFinder::get_point_penalty);
546
547
ClassDB::bind_method(D_METHOD("get_bounds"), &PolygonPathFinder::get_bounds);
548
ClassDB::bind_method(D_METHOD("_set_data", "data"), &PolygonPathFinder::_set_data);
549
ClassDB::bind_method(D_METHOD("_get_data"), &PolygonPathFinder::_get_data);
550
551
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
552
}
553
554
PolygonPathFinder::PolygonPathFinder() {
555
}
556
557