2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
|
|
|
/* polygon_path_finder.cpp */
|
|
|
|
/*************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
2017-08-27 12:16:55 +00:00
|
|
|
/* https://godotengine.org */
|
2016-06-18 12:46:12 +00:00
|
|
|
/*************************************************************************/
|
2022-01-03 20:27:34 +00:00
|
|
|
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
|
2016-06-18 12:46:12 +00:00
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/*************************************************************************/
|
2018-01-04 23:50:27 +00:00
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
#include "polygon_path_finder.h"
|
2020-05-25 17:20:45 +00:00
|
|
|
#include "core/math/geometry_2d.h"
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2014-06-17 14:58:35 +00:00
|
|
|
bool PolygonPathFinder::_is_point_inside(const Vector2 &p_point) const {
|
|
|
|
int crosses = 0;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
Vector2 a = points[e.points[0]].pos;
|
|
|
|
Vector2 b = points[e.points[1]].pos;
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, p_point, outside_point, nullptr)) {
|
2014-06-16 13:22:26 +00:00
|
|
|
crosses++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return crosses & 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PolygonPathFinder::setup(const Vector<Vector2> &p_points, const Vector<int> &p_connections) {
|
|
|
|
ERR_FAIL_COND(p_connections.size() & 1);
|
|
|
|
|
|
|
|
points.clear();
|
|
|
|
edges.clear();
|
|
|
|
|
|
|
|
//insert points
|
|
|
|
|
|
|
|
int point_count = p_points.size();
|
|
|
|
points.resize(point_count + 2);
|
2014-06-17 14:58:35 +00:00
|
|
|
bounds = Rect2();
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < p_points.size(); i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].pos = p_points[i];
|
|
|
|
points.write[i].penalty = 0;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
outside_point.x = i == 0 ? p_points[0].x : (MAX(p_points[i].x, outside_point.x));
|
|
|
|
outside_point.y = i == 0 ? p_points[0].y : (MAX(p_points[i].y, outside_point.y));
|
2014-06-17 14:58:35 +00:00
|
|
|
|
|
|
|
if (i == 0) {
|
2017-06-03 22:25:13 +00:00
|
|
|
bounds.position = points[i].pos;
|
2014-06-17 14:58:35 +00:00
|
|
|
} else {
|
|
|
|
bounds.expand_to(points[i].pos);
|
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
outside_point.x += 20.451 + Math::randf() * 10.2039;
|
|
|
|
outside_point.y += 21.193 + Math::randf() * 12.5412;
|
|
|
|
|
2021-03-12 13:35:16 +00:00
|
|
|
//insert edges (which are also connections)
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
for (int i = 0; i < p_connections.size(); i += 2) {
|
|
|
|
Edge e(p_connections[i], p_connections[i + 1]);
|
|
|
|
ERR_FAIL_INDEX(e.points[0], point_count);
|
|
|
|
ERR_FAIL_INDEX(e.points[1], point_count);
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[p_connections[i]].connections.insert(p_connections[i + 1]);
|
|
|
|
points.write[p_connections[i + 1]].connections.insert(p_connections[i]);
|
2014-06-16 13:22:26 +00:00
|
|
|
edges.insert(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
//fill the remaining connections based on visibility
|
|
|
|
|
|
|
|
for (int i = 0; i < point_count; i++) {
|
|
|
|
for (int j = i + 1; j < point_count; j++) {
|
2020-05-14 14:41:43 +00:00
|
|
|
if (edges.has(Edge(i, j))) {
|
2014-06-16 13:22:26 +00:00
|
|
|
continue; //if in edge ignore
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
Vector2 from = points[i].pos;
|
|
|
|
Vector2 to = points[j].pos;
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!_is_point_inside(from * 0.5 + to * 0.5)) { //connection between points in inside space
|
2014-06-16 13:22:26 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (e.points[0] == i || e.points[1] == i || e.points[0] == j || e.points[1] == j) {
|
2014-06-16 13:22:26 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
Vector2 a = points[e.points[0]].pos;
|
|
|
|
Vector2 b = points[e.points[1]].pos;
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {
|
2014-06-16 13:22:26 +00:00
|
|
|
valid = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].connections.insert(j);
|
|
|
|
points.write[j].connections.insert(i);
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector2 &p_to) {
|
|
|
|
Vector<Vector2> path;
|
2014-06-28 02:21:45 +00:00
|
|
|
|
|
|
|
Vector2 from = p_from;
|
|
|
|
Vector2 to = p_to;
|
|
|
|
Edge ignore_from_edge(-1, -1);
|
|
|
|
Edge ignore_to_edge(-1, -1);
|
|
|
|
|
|
|
|
if (!_is_point_inside(from)) {
|
2022-05-10 10:05:52 +00:00
|
|
|
float closest_dist = 1e20f;
|
2014-06-28 02:21:45 +00:00
|
|
|
Vector2 closest_point;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2014-06-28 02:21:45 +00:00
|
|
|
Vector2 seg[2] = {
|
|
|
|
points[e.points[0]].pos,
|
|
|
|
points[e.points[1]].pos
|
|
|
|
};
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
Vector2 closest = Geometry2D::get_closest_point_to_segment(from, seg);
|
2014-06-28 02:21:45 +00:00
|
|
|
float d = from.distance_squared_to(closest);
|
|
|
|
|
|
|
|
if (d < closest_dist) {
|
2022-05-18 23:43:40 +00:00
|
|
|
ignore_from_edge = E;
|
2014-06-28 02:21:45 +00:00
|
|
|
closest_dist = d;
|
2014-12-20 23:28:08 +00:00
|
|
|
closest_point = closest;
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
from = closest_point;
|
2014-06-17 14:58:35 +00:00
|
|
|
};
|
2014-06-28 02:21:45 +00:00
|
|
|
|
|
|
|
if (!_is_point_inside(to)) {
|
2022-05-10 10:05:52 +00:00
|
|
|
float closest_dist = 1e20f;
|
2014-06-28 02:21:45 +00:00
|
|
|
Vector2 closest_point;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2014-06-28 02:21:45 +00:00
|
|
|
Vector2 seg[2] = {
|
|
|
|
points[e.points[0]].pos,
|
|
|
|
points[e.points[1]].pos
|
|
|
|
};
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
Vector2 closest = Geometry2D::get_closest_point_to_segment(to, seg);
|
2014-06-28 02:21:45 +00:00
|
|
|
float d = to.distance_squared_to(closest);
|
|
|
|
|
|
|
|
if (d < closest_dist) {
|
2022-05-18 23:43:40 +00:00
|
|
|
ignore_to_edge = E;
|
2014-06-28 02:21:45 +00:00
|
|
|
closest_dist = d;
|
2014-12-20 23:28:08 +00:00
|
|
|
closest_point = closest;
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
to = closest_point;
|
2014-06-17 14:58:35 +00:00
|
|
|
};
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
//test direct connection
|
|
|
|
{
|
|
|
|
bool can_see_eachother = true;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2020-05-14 14:41:43 +00:00
|
|
|
if (e.points[0] == ignore_from_edge.points[0] && e.points[1] == ignore_from_edge.points[1]) {
|
2014-06-28 02:21:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
|
|
|
if (e.points[0] == ignore_to_edge.points[0] && e.points[1] == ignore_to_edge.points[1]) {
|
2014-06-28 02:21:45 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-28 02:21:45 +00:00
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
Vector2 a = points[e.points[0]].pos;
|
|
|
|
Vector2 b = points[e.points[1]].pos;
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {
|
2014-06-16 13:22:26 +00:00
|
|
|
can_see_eachother = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (can_see_eachother) {
|
2014-06-28 02:21:45 +00:00
|
|
|
path.push_back(from);
|
|
|
|
path.push_back(to);
|
2014-06-16 13:22:26 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//add to graph
|
|
|
|
|
|
|
|
int aidx = points.size() - 2;
|
|
|
|
int bidx = points.size() - 1;
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[aidx].pos = from;
|
|
|
|
points.write[bidx].pos = to;
|
|
|
|
points.write[aidx].distance = 0;
|
|
|
|
points.write[bidx].distance = 0;
|
|
|
|
points.write[aidx].prev = -1;
|
|
|
|
points.write[bidx].prev = -1;
|
|
|
|
points.write[aidx].penalty = 0;
|
|
|
|
points.write[bidx].penalty = 0;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
for (int i = 0; i < points.size() - 2; i++) {
|
|
|
|
bool valid_a = true;
|
|
|
|
bool valid_b = true;
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].prev = -1;
|
|
|
|
points.write[i].distance = 0;
|
2017-03-05 15:44:50 +00:00
|
|
|
|
2014-06-28 02:21:45 +00:00
|
|
|
if (!_is_point_inside(from * 0.5 + points[i].pos * 0.5)) {
|
|
|
|
valid_a = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!_is_point_inside(to * 0.5 + points[i].pos * 0.5)) {
|
|
|
|
valid_b = false;
|
|
|
|
}
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (e.points[0] == i || e.points[1] == i) {
|
2014-06-16 13:22:26 +00:00
|
|
|
continue;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
Vector2 a = points[e.points[0]].pos;
|
|
|
|
Vector2 b = points[e.points[1]].pos;
|
|
|
|
|
|
|
|
if (valid_a) {
|
2014-06-28 02:21:45 +00:00
|
|
|
if (e.points[0] != ignore_from_edge.points[1] &&
|
|
|
|
e.points[1] != ignore_from_edge.points[1] &&
|
|
|
|
e.points[0] != ignore_from_edge.points[0] &&
|
|
|
|
e.points[1] != ignore_from_edge.points[0]) {
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, from, points[i].pos, nullptr)) {
|
2014-06-28 02:21:45 +00:00
|
|
|
valid_a = false;
|
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (valid_b) {
|
2014-06-28 02:21:45 +00:00
|
|
|
if (e.points[0] != ignore_to_edge.points[1] &&
|
|
|
|
e.points[1] != ignore_to_edge.points[1] &&
|
|
|
|
e.points[0] != ignore_to_edge.points[0] &&
|
|
|
|
e.points[1] != ignore_to_edge.points[0]) {
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, to, points[i].pos, nullptr)) {
|
2014-06-28 02:21:45 +00:00
|
|
|
valid_b = false;
|
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (!valid_a && !valid_b) {
|
2014-06-17 14:58:35 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (valid_a) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].connections.insert(aidx);
|
|
|
|
points.write[aidx].connections.insert(i);
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (valid_b) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].connections.insert(bidx);
|
|
|
|
points.write[bidx].connections.insert(i);
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//solve graph
|
|
|
|
|
2022-05-19 15:00:06 +00:00
|
|
|
HashSet<int> open_list;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[aidx].distance = 0;
|
|
|
|
points.write[aidx].prev = aidx;
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : points[aidx].connections) {
|
|
|
|
open_list.insert(E);
|
|
|
|
points.write[E].distance = from.distance_to(points[E].pos);
|
|
|
|
points.write[E].prev = aidx;
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool found_route = false;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
if (open_list.size() == 0) {
|
2014-06-17 14:58:35 +00:00
|
|
|
printf("open list empty\n");
|
2014-06-16 13:22:26 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
//check open list
|
|
|
|
|
|
|
|
int least_cost_point = -1;
|
|
|
|
float least_cost = 1e30;
|
|
|
|
|
|
|
|
//this could be faster (cache previous results)
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : open_list) {
|
|
|
|
const Point &p = points[E];
|
2014-06-16 13:22:26 +00:00
|
|
|
float cost = p.distance;
|
2014-06-28 02:21:45 +00:00
|
|
|
cost += p.pos.distance_to(to);
|
|
|
|
cost += p.penalty;
|
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
if (cost < least_cost) {
|
2022-05-18 23:43:40 +00:00
|
|
|
least_cost_point = E;
|
2014-06-16 13:22:26 +00:00
|
|
|
least_cost = cost;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
const Point &np = points[least_cost_point];
|
2014-06-16 13:22:26 +00:00
|
|
|
//open the neighbours for search
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : np.connections) {
|
|
|
|
Point &p = points.write[E];
|
2014-06-16 13:22:26 +00:00
|
|
|
float distance = np.pos.distance_to(p.pos) + np.distance;
|
|
|
|
|
|
|
|
if (p.prev != -1) {
|
|
|
|
//oh this was visited already, can we win the cost?
|
|
|
|
|
|
|
|
if (p.distance > distance) {
|
2021-03-12 13:35:16 +00:00
|
|
|
p.prev = least_cost_point; //reassign previous
|
2014-06-16 13:22:26 +00:00
|
|
|
p.distance = distance;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
//add to open neighbours
|
|
|
|
|
|
|
|
p.prev = least_cost_point;
|
|
|
|
p.distance = distance;
|
2022-05-18 23:43:40 +00:00
|
|
|
open_list.insert(E);
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
if (E == bidx) {
|
2014-06-16 13:22:26 +00:00
|
|
|
//oh my reached end! stop algorithm
|
|
|
|
found_route = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-14 14:41:43 +00:00
|
|
|
if (found_route) {
|
2014-06-16 13:22:26 +00:00
|
|
|
break;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
open_list.erase(least_cost_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (found_route) {
|
|
|
|
int at = bidx;
|
|
|
|
path.push_back(points[at].pos);
|
|
|
|
do {
|
|
|
|
at = points[at].prev;
|
|
|
|
path.push_back(points[at].pos);
|
|
|
|
} while (at != aidx);
|
|
|
|
|
2021-03-14 07:21:32 +00:00
|
|
|
path.reverse();
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < points.size() - 2; i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].connections.erase(aidx);
|
|
|
|
points.write[i].connections.erase(bidx);
|
|
|
|
points.write[i].prev = -1;
|
|
|
|
points.write[i].distance = 0;
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[aidx].connections.clear();
|
|
|
|
points.write[aidx].prev = -1;
|
|
|
|
points.write[aidx].distance = 0;
|
|
|
|
points.write[bidx].connections.clear();
|
|
|
|
points.write[bidx].prev = -1;
|
|
|
|
points.write[bidx].distance = 0;
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PolygonPathFinder::_set_data(const Dictionary &p_data) {
|
|
|
|
ERR_FAIL_COND(!p_data.has("points"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("connections"));
|
|
|
|
ERR_FAIL_COND(!p_data.has("segments"));
|
2014-06-17 14:58:35 +00:00
|
|
|
ERR_FAIL_COND(!p_data.has("bounds"));
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector2> p = p_data["points"];
|
2014-06-16 13:22:26 +00:00
|
|
|
Array c = p_data["connections"];
|
|
|
|
|
|
|
|
ERR_FAIL_COND(c.size() != p.size());
|
2020-05-14 14:41:43 +00:00
|
|
|
if (c.size()) {
|
2014-06-16 13:22:26 +00:00
|
|
|
return;
|
2020-05-14 14:41:43 +00:00
|
|
|
}
|
2014-06-16 13:22:26 +00:00
|
|
|
|
|
|
|
int pc = p.size();
|
|
|
|
points.resize(pc + 2);
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
const Vector2 *pr = p.ptr();
|
2014-06-16 13:22:26 +00:00
|
|
|
for (int i = 0; i < pc; i++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].pos = pr[i];
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<int> con = c[i];
|
|
|
|
const int *cr = con.ptr();
|
2014-06-16 13:22:26 +00:00
|
|
|
int cc = con.size();
|
|
|
|
for (int j = 0; j < cc; j++) {
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[i].connections.insert(cr[j]);
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-28 02:21:45 +00:00
|
|
|
if (p_data.has("penalties")) {
|
2021-08-09 22:15:17 +00:00
|
|
|
Vector<real_t> penalties = p_data["penalties"];
|
2014-06-28 02:21:45 +00:00
|
|
|
if (penalties.size() == pc) {
|
2021-08-09 22:15:17 +00:00
|
|
|
const real_t *pr2 = penalties.ptr();
|
2014-06-28 02:21:45 +00:00
|
|
|
for (int i = 0; i < pc; i++) {
|
2019-02-12 20:10:08 +00:00
|
|
|
points.write[i].penalty = pr2[i];
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<int> segs = p_data["segments"];
|
2014-06-16 13:22:26 +00:00
|
|
|
int sc = segs.size();
|
|
|
|
ERR_FAIL_COND(sc & 1);
|
2020-02-17 21:06:54 +00:00
|
|
|
const int *sr = segs.ptr();
|
2014-06-16 13:22:26 +00:00
|
|
|
for (int i = 0; i < sc; i += 2) {
|
|
|
|
Edge e(sr[i], sr[i + 1]);
|
|
|
|
edges.insert(e);
|
|
|
|
}
|
2014-06-17 14:58:35 +00:00
|
|
|
bounds = p_data["bounds"];
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Dictionary PolygonPathFinder::_get_data() const {
|
|
|
|
Dictionary d;
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<Vector2> p;
|
|
|
|
Vector<int> ind;
|
2022-09-29 09:53:28 +00:00
|
|
|
Array path_connections;
|
2019-09-03 10:42:34 +00:00
|
|
|
p.resize(MAX(0, points.size() - 2));
|
2022-09-29 09:53:28 +00:00
|
|
|
path_connections.resize(MAX(0, points.size() - 2));
|
2014-06-16 13:22:26 +00:00
|
|
|
ind.resize(edges.size() * 2);
|
2021-08-09 22:15:17 +00:00
|
|
|
Vector<real_t> penalties;
|
2019-09-03 10:42:34 +00:00
|
|
|
penalties.resize(MAX(0, points.size() - 2));
|
2014-06-16 13:22:26 +00:00
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector2 *wp = p.ptrw();
|
2021-08-09 22:15:17 +00:00
|
|
|
real_t *pw = penalties.ptrw();
|
2014-06-28 02:21:45 +00:00
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
for (int i = 0; i < points.size() - 2; i++) {
|
|
|
|
wp[i] = points[i].pos;
|
2014-06-28 02:21:45 +00:00
|
|
|
pw[i] = points[i].penalty;
|
2020-02-17 21:06:54 +00:00
|
|
|
Vector<int> c;
|
2014-06-16 13:22:26 +00:00
|
|
|
c.resize(points[i].connections.size());
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
int *cw = c.ptrw();
|
2014-06-16 13:22:26 +00:00
|
|
|
int idx = 0;
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const int &E : points[i].connections) {
|
|
|
|
cw[idx++] = E;
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
2022-09-29 09:53:28 +00:00
|
|
|
path_connections[i] = c;
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
{
|
2020-02-17 21:06:54 +00:00
|
|
|
int *iw = ind.ptrw();
|
2014-06-16 13:22:26 +00:00
|
|
|
int idx = 0;
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
iw[idx++] = E.points[0];
|
|
|
|
iw[idx++] = E.points[1];
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:58:35 +00:00
|
|
|
d["bounds"] = bounds;
|
2014-06-16 13:22:26 +00:00
|
|
|
d["points"] = p;
|
2014-06-28 02:21:45 +00:00
|
|
|
d["penalties"] = penalties;
|
2022-09-29 09:53:28 +00:00
|
|
|
d["connections"] = path_connections;
|
2014-06-16 13:22:26 +00:00
|
|
|
d["segments"] = ind;
|
|
|
|
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
|
2014-06-17 14:58:35 +00:00
|
|
|
bool PolygonPathFinder::is_point_inside(const Vector2 &p_point) const {
|
|
|
|
return _is_point_inside(p_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
Vector2 PolygonPathFinder::get_closest_point(const Vector2 &p_point) const {
|
2022-05-10 10:05:52 +00:00
|
|
|
float closest_dist = 1e20f;
|
2015-02-01 17:23:31 +00:00
|
|
|
Vector2 closest_point;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
const Edge &e = E;
|
2015-02-01 17:23:31 +00:00
|
|
|
Vector2 seg[2] = {
|
|
|
|
points[e.points[0]].pos,
|
|
|
|
points[e.points[1]].pos
|
|
|
|
};
|
|
|
|
|
2020-05-25 17:20:45 +00:00
|
|
|
Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, seg);
|
2015-02-01 17:23:31 +00:00
|
|
|
float d = p_point.distance_squared_to(closest);
|
2014-06-17 14:58:35 +00:00
|
|
|
|
|
|
|
if (d < closest_dist) {
|
2014-12-20 23:28:08 +00:00
|
|
|
closest_dist = d;
|
2015-02-01 17:23:31 +00:00
|
|
|
closest_point = closest;
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-08 23:00:52 +00:00
|
|
|
|
2022-05-10 10:05:52 +00:00
|
|
|
ERR_FAIL_COND_V(Math::is_equal_approx(closest_dist, 1e20f), Vector2());
|
2014-06-17 14:58:35 +00:00
|
|
|
|
2015-02-01 17:23:31 +00:00
|
|
|
return closest_point;
|
2014-06-17 14:58:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2 &p_from, const Vector2 &p_to) const {
|
|
|
|
Vector<Vector2> inters;
|
|
|
|
|
2022-05-18 23:43:40 +00:00
|
|
|
for (const Edge &E : edges) {
|
|
|
|
Vector2 a = points[E.points[0]].pos;
|
|
|
|
Vector2 b = points[E.points[1]].pos;
|
2014-06-17 14:58:35 +00:00
|
|
|
|
|
|
|
Vector2 res;
|
2020-05-25 17:20:45 +00:00
|
|
|
if (Geometry2D::segment_intersects_segment(a, b, p_from, p_to, &res)) {
|
2014-06-17 14:58:35 +00:00
|
|
|
inters.push_back(res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return inters;
|
|
|
|
}
|
|
|
|
|
|
|
|
Rect2 PolygonPathFinder::get_bounds() const {
|
|
|
|
return bounds;
|
|
|
|
}
|
|
|
|
|
2014-06-28 02:21:45 +00:00
|
|
|
void PolygonPathFinder::set_point_penalty(int p_point, float p_penalty) {
|
|
|
|
ERR_FAIL_INDEX(p_point, points.size() - 2);
|
2018-07-25 01:11:03 +00:00
|
|
|
points.write[p_point].penalty = p_penalty;
|
2014-06-28 02:21:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
float PolygonPathFinder::get_point_penalty(int p_point) const {
|
|
|
|
ERR_FAIL_INDEX_V(p_point, points.size() - 2, 0);
|
|
|
|
return points[p_point].penalty;
|
|
|
|
}
|
|
|
|
|
2014-06-16 13:22:26 +00:00
|
|
|
void PolygonPathFinder::_bind_methods() {
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("setup", "points", "connections"), &PolygonPathFinder::setup);
|
|
|
|
ClassDB::bind_method(D_METHOD("find_path", "from", "to"), &PolygonPathFinder::find_path);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_intersections", "from", "to"), &PolygonPathFinder::get_intersections);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_closest_point", "point"), &PolygonPathFinder::get_closest_point);
|
|
|
|
ClassDB::bind_method(D_METHOD("is_point_inside", "point"), &PolygonPathFinder::is_point_inside);
|
|
|
|
ClassDB::bind_method(D_METHOD("set_point_penalty", "idx", "penalty"), &PolygonPathFinder::set_point_penalty);
|
|
|
|
ClassDB::bind_method(D_METHOD("get_point_penalty", "idx"), &PolygonPathFinder::get_point_penalty);
|
|
|
|
|
|
|
|
ClassDB::bind_method(D_METHOD("get_bounds"), &PolygonPathFinder::get_bounds);
|
2022-08-08 12:18:26 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_set_data", "data"), &PolygonPathFinder::_set_data);
|
2017-02-13 11:47:24 +00:00
|
|
|
ClassDB::bind_method(D_METHOD("_get_data"), &PolygonPathFinder::_get_data);
|
2014-06-16 13:22:26 +00:00
|
|
|
|
2021-11-03 22:06:17 +00:00
|
|
|
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");
|
2014-06-16 13:22:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PolygonPathFinder::PolygonPathFinder() {
|
|
|
|
}
|