Path: blob/master/scene/resources/2d/polygon_path_finder.cpp
23418 views
/**************************************************************************/1/* polygon_path_finder.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "polygon_path_finder.h"3132#include "core/math/geometry_2d.h"33#include "core/object/class_db.h"3435bool PolygonPathFinder::_is_point_inside(const Vector2 &p_point) const {36int crosses = 0;3738for (const Edge &E : edges) {39const Edge &e = E;4041Vector2 a = points[e.points[0]].pos;42Vector2 b = points[e.points[1]].pos;4344if (Geometry2D::segment_intersects_segment(a, b, p_point, outside_point, nullptr)) {45crosses++;46}47}4849return crosses & 1;50}5152void PolygonPathFinder::setup(const Vector<Vector2> &p_points, const Vector<int> &p_connections) {53ERR_FAIL_COND(p_connections.size() & 1);5455points.clear();56edges.clear();5758//insert points5960int point_count = p_points.size();61points.resize(point_count + 2);62bounds = Rect2();6364for (int i = 0; i < p_points.size(); i++) {65points.write[i].pos = p_points[i];66points.write[i].penalty = 0;6768outside_point = i == 0 ? p_points[0] : p_points[i].max(outside_point);6970if (i == 0) {71bounds.position = points[i].pos;72} else {73bounds.expand_to(points[i].pos);74}75}7677outside_point.x += 20.451 + Math::randf() * 10.2039;78outside_point.y += 21.193 + Math::randf() * 12.5412;7980//insert edges (which are also connections)8182for (int i = 0; i < p_connections.size(); i += 2) {83Edge e(p_connections[i], p_connections[i + 1]);84ERR_FAIL_INDEX(e.points[0], point_count);85ERR_FAIL_INDEX(e.points[1], point_count);86points.write[p_connections[i]].connections.insert(p_connections[i + 1]);87points.write[p_connections[i + 1]].connections.insert(p_connections[i]);88edges.insert(e);89}9091//fill the remaining connections based on visibility9293for (int i = 0; i < point_count; i++) {94for (int j = i + 1; j < point_count; j++) {95if (edges.has(Edge(i, j))) {96continue; //if in edge ignore97}9899Vector2 from = points[i].pos;100Vector2 to = points[j].pos;101102if (!_is_point_inside(from * 0.5 + to * 0.5)) { //connection between points in inside space103continue;104}105106bool valid = true;107108for (const Edge &E : edges) {109const Edge &e = E;110if (e.points[0] == i || e.points[1] == i || e.points[0] == j || e.points[1] == j) {111continue;112}113114Vector2 a = points[e.points[0]].pos;115Vector2 b = points[e.points[1]].pos;116117if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {118valid = false;119break;120}121}122123if (valid) {124points.write[i].connections.insert(j);125points.write[j].connections.insert(i);126}127}128}129}130131Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector2 &p_to) {132Vector<Vector2> path;133134Vector2 from = p_from;135Vector2 to = p_to;136Edge ignore_from_edge(-1, -1);137Edge ignore_to_edge(-1, -1);138139if (!_is_point_inside(from)) {140float closest_dist = 1e20f;141Vector2 closest_point;142143for (const Edge &E : edges) {144const Edge &e = E;145const Vector2 segment_a = points[e.points[0]].pos;146const Vector2 segment_b = points[e.points[1]].pos;147Vector2 closest = Geometry2D::get_closest_point_to_segment(from, segment_a, segment_b);148float d = from.distance_squared_to(closest);149150if (d < closest_dist) {151ignore_from_edge = E;152closest_dist = d;153closest_point = closest;154}155}156157from = closest_point;158};159160if (!_is_point_inside(to)) {161float closest_dist = 1e20f;162Vector2 closest_point;163164for (const Edge &E : edges) {165const Edge &e = E;166const Vector2 segment_a = points[e.points[0]].pos;167const Vector2 segment_b = points[e.points[1]].pos;168Vector2 closest = Geometry2D::get_closest_point_to_segment(to, segment_a, segment_b);169float d = to.distance_squared_to(closest);170171if (d < closest_dist) {172ignore_to_edge = E;173closest_dist = d;174closest_point = closest;175}176}177178to = closest_point;179};180181//test direct connection182{183bool can_see_eachother = true;184185for (const Edge &E : edges) {186const Edge &e = E;187if (e.points[0] == ignore_from_edge.points[0] && e.points[1] == ignore_from_edge.points[1]) {188continue;189}190if (e.points[0] == ignore_to_edge.points[0] && e.points[1] == ignore_to_edge.points[1]) {191continue;192}193194Vector2 a = points[e.points[0]].pos;195Vector2 b = points[e.points[1]].pos;196197if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {198can_see_eachother = false;199break;200}201}202203if (can_see_eachother) {204path.push_back(from);205path.push_back(to);206return path;207}208}209210//add to graph211212int aidx = points.size() - 2;213int bidx = points.size() - 1;214points.write[aidx].pos = from;215points.write[bidx].pos = to;216points.write[aidx].distance = 0;217points.write[bidx].distance = 0;218points.write[aidx].prev = -1;219points.write[bidx].prev = -1;220points.write[aidx].penalty = 0;221points.write[bidx].penalty = 0;222223for (int i = 0; i < points.size() - 2; i++) {224bool valid_a = true;225bool valid_b = true;226points.write[i].prev = -1;227points.write[i].distance = 0;228229if (!_is_point_inside(from * 0.5 + points[i].pos * 0.5)) {230valid_a = false;231}232233if (!_is_point_inside(to * 0.5 + points[i].pos * 0.5)) {234valid_b = false;235}236237for (const Edge &E : edges) {238const Edge &e = E;239240if (e.points[0] == i || e.points[1] == i) {241continue;242}243244Vector2 a = points[e.points[0]].pos;245Vector2 b = points[e.points[1]].pos;246247if (valid_a) {248if (e.points[0] != ignore_from_edge.points[1] &&249e.points[1] != ignore_from_edge.points[1] &&250e.points[0] != ignore_from_edge.points[0] &&251e.points[1] != ignore_from_edge.points[0]) {252if (Geometry2D::segment_intersects_segment(a, b, from, points[i].pos, nullptr)) {253valid_a = false;254}255}256}257258if (valid_b) {259if (e.points[0] != ignore_to_edge.points[1] &&260e.points[1] != ignore_to_edge.points[1] &&261e.points[0] != ignore_to_edge.points[0] &&262e.points[1] != ignore_to_edge.points[0]) {263if (Geometry2D::segment_intersects_segment(a, b, to, points[i].pos, nullptr)) {264valid_b = false;265}266}267}268269if (!valid_a && !valid_b) {270break;271}272}273274if (valid_a) {275points.write[i].connections.insert(aidx);276points.write[aidx].connections.insert(i);277}278279if (valid_b) {280points.write[i].connections.insert(bidx);281points.write[bidx].connections.insert(i);282}283}284//solve graph285286HashSet<int> open_list;287288points.write[aidx].distance = 0;289points.write[aidx].prev = aidx;290for (const int &E : points[aidx].connections) {291open_list.insert(E);292points.write[E].distance = from.distance_to(points[E].pos);293points.write[E].prev = aidx;294}295296bool found_route = false;297298while (true) {299if (open_list.is_empty()) {300print_verbose("Open list empty.");301break;302}303//check open list304305int least_cost_point = -1;306float least_cost = 1e30;307308//this could be faster (cache previous results)309for (const int &E : open_list) {310const Point &p = points[E];311float cost = p.distance;312cost += p.pos.distance_to(to);313cost += p.penalty;314315if (cost < least_cost) {316least_cost_point = E;317least_cost = cost;318}319}320321const Point &np = points[least_cost_point];322//open the neighbors for search323324for (const int &E : np.connections) {325Point &p = points.write[E];326float distance = np.pos.distance_to(p.pos) + np.distance;327328if (p.prev != -1) {329//oh this was visited already, can we win the cost?330331if (p.distance > distance) {332p.prev = least_cost_point; //reassign previous333p.distance = distance;334}335} else {336//add to open neighbors337338p.prev = least_cost_point;339p.distance = distance;340open_list.insert(E);341342if (E == bidx) {343//oh my reached end! stop algorithm344found_route = true;345break;346}347}348}349350if (found_route) {351break;352}353354open_list.erase(least_cost_point);355}356357if (found_route) {358int at = bidx;359path.push_back(points[at].pos);360do {361at = points[at].prev;362path.push_back(points[at].pos);363} while (at != aidx);364365path.reverse();366}367368for (int i = 0; i < points.size() - 2; i++) {369points.write[i].connections.erase(aidx);370points.write[i].connections.erase(bidx);371points.write[i].prev = -1;372points.write[i].distance = 0;373}374375points.write[aidx].connections.clear();376points.write[aidx].prev = -1;377points.write[aidx].distance = 0;378points.write[bidx].connections.clear();379points.write[bidx].prev = -1;380points.write[bidx].distance = 0;381382return path;383}384385void PolygonPathFinder::_set_data(const Dictionary &p_data) {386ERR_FAIL_COND(!p_data.has("points"));387ERR_FAIL_COND(!p_data.has("connections"));388ERR_FAIL_COND(!p_data.has("segments"));389ERR_FAIL_COND(!p_data.has("bounds"));390391Vector<Vector2> p = p_data["points"];392Array c = p_data["connections"];393394ERR_FAIL_COND(c.size() != p.size());395if (c.size()) {396return;397}398399int pc = p.size();400points.resize(pc + 2);401402const Vector2 *pr = p.ptr();403for (int i = 0; i < pc; i++) {404points.write[i].pos = pr[i];405Vector<int> con = c[i];406const int *cr = con.ptr();407int cc = con.size();408for (int j = 0; j < cc; j++) {409points.write[i].connections.insert(cr[j]);410}411}412413if (p_data.has("penalties")) {414Vector<real_t> penalties = p_data["penalties"];415if (penalties.size() == pc) {416const real_t *pr2 = penalties.ptr();417for (int i = 0; i < pc; i++) {418points.write[i].penalty = pr2[i];419}420}421}422423Vector<int> segs = p_data["segments"];424int sc = segs.size();425ERR_FAIL_COND(sc & 1);426const int *sr = segs.ptr();427for (int i = 0; i < sc; i += 2) {428Edge e(sr[i], sr[i + 1]);429edges.insert(e);430}431bounds = p_data["bounds"];432}433434Dictionary PolygonPathFinder::_get_data() const {435Dictionary d;436Vector<Vector2> p;437Vector<int> ind;438Array path_connections;439p.resize(MAX(0, points.size() - 2));440path_connections.resize(MAX(0, points.size() - 2));441ind.resize(edges.size() * 2);442Vector<real_t> penalties;443penalties.resize(MAX(0, points.size() - 2));444{445Vector2 *wp = p.ptrw();446real_t *pw = penalties.ptrw();447448for (int i = 0; i < points.size() - 2; i++) {449wp[i] = points[i].pos;450pw[i] = points[i].penalty;451Vector<int> c;452c.resize(points[i].connections.size());453{454int *cw = c.ptrw();455int idx = 0;456for (const int &E : points[i].connections) {457cw[idx++] = E;458}459}460path_connections[i] = c;461}462}463{464int *iw = ind.ptrw();465int idx = 0;466for (const Edge &E : edges) {467iw[idx++] = E.points[0];468iw[idx++] = E.points[1];469}470}471472d["bounds"] = bounds;473d["points"] = p;474d["penalties"] = penalties;475d["connections"] = path_connections;476d["segments"] = ind;477478return d;479}480481bool PolygonPathFinder::is_point_inside(const Vector2 &p_point) const {482return _is_point_inside(p_point);483}484485Vector2 PolygonPathFinder::get_closest_point(const Vector2 &p_point) const {486float closest_dist = 1e20f;487Vector2 closest_point;488489for (const Edge &E : edges) {490const Edge &e = E;491const Vector2 segment_a = points[e.points[0]].pos;492const Vector2 segment_b = points[e.points[1]].pos;493Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, segment_a, segment_b);494float d = p_point.distance_squared_to(closest);495496if (d < closest_dist) {497closest_dist = d;498closest_point = closest;499}500}501502ERR_FAIL_COND_V(Math::is_equal_approx(closest_dist, 1e20f), Vector2());503504return closest_point;505}506507Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2 &p_from, const Vector2 &p_to) const {508Vector<Vector2> inters;509510for (const Edge &E : edges) {511Vector2 a = points[E.points[0]].pos;512Vector2 b = points[E.points[1]].pos;513514Vector2 res;515if (Geometry2D::segment_intersects_segment(a, b, p_from, p_to, &res)) {516inters.push_back(res);517}518}519520return inters;521}522523Rect2 PolygonPathFinder::get_bounds() const {524return bounds;525}526527void PolygonPathFinder::set_point_penalty(int p_point, float p_penalty) {528ERR_FAIL_INDEX(p_point, points.size() - 2);529points.write[p_point].penalty = p_penalty;530}531532float PolygonPathFinder::get_point_penalty(int p_point) const {533ERR_FAIL_INDEX_V(p_point, points.size() - 2, 0);534return points[p_point].penalty;535}536537void PolygonPathFinder::_bind_methods() {538ClassDB::bind_method(D_METHOD("setup", "points", "connections"), &PolygonPathFinder::setup);539ClassDB::bind_method(D_METHOD("find_path", "from", "to"), &PolygonPathFinder::find_path);540ClassDB::bind_method(D_METHOD("get_intersections", "from", "to"), &PolygonPathFinder::get_intersections);541ClassDB::bind_method(D_METHOD("get_closest_point", "point"), &PolygonPathFinder::get_closest_point);542ClassDB::bind_method(D_METHOD("is_point_inside", "point"), &PolygonPathFinder::is_point_inside);543ClassDB::bind_method(D_METHOD("set_point_penalty", "idx", "penalty"), &PolygonPathFinder::set_point_penalty);544ClassDB::bind_method(D_METHOD("get_point_penalty", "idx"), &PolygonPathFinder::get_point_penalty);545546ClassDB::bind_method(D_METHOD("get_bounds"), &PolygonPathFinder::get_bounds);547ClassDB::bind_method(D_METHOD("_set_data", "data"), &PolygonPathFinder::_set_data);548ClassDB::bind_method(D_METHOD("_get_data"), &PolygonPathFinder::_get_data);549550ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");551}552553PolygonPathFinder::PolygonPathFinder() {554}555556557