Path: blob/master/scene/resources/2d/polygon_path_finder.cpp
9898 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"31#include "core/math/geometry_2d.h"3233bool PolygonPathFinder::_is_point_inside(const Vector2 &p_point) const {34int crosses = 0;3536for (const Edge &E : edges) {37const Edge &e = E;3839Vector2 a = points[e.points[0]].pos;40Vector2 b = points[e.points[1]].pos;4142if (Geometry2D::segment_intersects_segment(a, b, p_point, outside_point, nullptr)) {43crosses++;44}45}4647return crosses & 1;48}4950void PolygonPathFinder::setup(const Vector<Vector2> &p_points, const Vector<int> &p_connections) {51ERR_FAIL_COND(p_connections.size() & 1);5253points.clear();54edges.clear();5556//insert points5758int point_count = p_points.size();59points.resize(point_count + 2);60bounds = Rect2();6162for (int i = 0; i < p_points.size(); i++) {63points.write[i].pos = p_points[i];64points.write[i].penalty = 0;6566outside_point = i == 0 ? p_points[0] : p_points[i].max(outside_point);6768if (i == 0) {69bounds.position = points[i].pos;70} else {71bounds.expand_to(points[i].pos);72}73}7475outside_point.x += 20.451 + Math::randf() * 10.2039;76outside_point.y += 21.193 + Math::randf() * 12.5412;7778//insert edges (which are also connections)7980for (int i = 0; i < p_connections.size(); i += 2) {81Edge e(p_connections[i], p_connections[i + 1]);82ERR_FAIL_INDEX(e.points[0], point_count);83ERR_FAIL_INDEX(e.points[1], point_count);84points.write[p_connections[i]].connections.insert(p_connections[i + 1]);85points.write[p_connections[i + 1]].connections.insert(p_connections[i]);86edges.insert(e);87}8889//fill the remaining connections based on visibility9091for (int i = 0; i < point_count; i++) {92for (int j = i + 1; j < point_count; j++) {93if (edges.has(Edge(i, j))) {94continue; //if in edge ignore95}9697Vector2 from = points[i].pos;98Vector2 to = points[j].pos;99100if (!_is_point_inside(from * 0.5 + to * 0.5)) { //connection between points in inside space101continue;102}103104bool valid = true;105106for (const Edge &E : edges) {107const Edge &e = E;108if (e.points[0] == i || e.points[1] == i || e.points[0] == j || e.points[1] == j) {109continue;110}111112Vector2 a = points[e.points[0]].pos;113Vector2 b = points[e.points[1]].pos;114115if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {116valid = false;117break;118}119}120121if (valid) {122points.write[i].connections.insert(j);123points.write[j].connections.insert(i);124}125}126}127}128129Vector<Vector2> PolygonPathFinder::find_path(const Vector2 &p_from, const Vector2 &p_to) {130Vector<Vector2> path;131132Vector2 from = p_from;133Vector2 to = p_to;134Edge ignore_from_edge(-1, -1);135Edge ignore_to_edge(-1, -1);136137if (!_is_point_inside(from)) {138float closest_dist = 1e20f;139Vector2 closest_point;140141for (const Edge &E : edges) {142const Edge &e = E;143const Vector2 segment_a = points[e.points[0]].pos;144const Vector2 segment_b = points[e.points[1]].pos;145Vector2 closest = Geometry2D::get_closest_point_to_segment(from, segment_a, segment_b);146float d = from.distance_squared_to(closest);147148if (d < closest_dist) {149ignore_from_edge = E;150closest_dist = d;151closest_point = closest;152}153}154155from = closest_point;156};157158if (!_is_point_inside(to)) {159float closest_dist = 1e20f;160Vector2 closest_point;161162for (const Edge &E : edges) {163const Edge &e = E;164const Vector2 segment_a = points[e.points[0]].pos;165const Vector2 segment_b = points[e.points[1]].pos;166Vector2 closest = Geometry2D::get_closest_point_to_segment(to, segment_a, segment_b);167float d = to.distance_squared_to(closest);168169if (d < closest_dist) {170ignore_to_edge = E;171closest_dist = d;172closest_point = closest;173}174}175176to = closest_point;177};178179//test direct connection180{181bool can_see_eachother = true;182183for (const Edge &E : edges) {184const Edge &e = E;185if (e.points[0] == ignore_from_edge.points[0] && e.points[1] == ignore_from_edge.points[1]) {186continue;187}188if (e.points[0] == ignore_to_edge.points[0] && e.points[1] == ignore_to_edge.points[1]) {189continue;190}191192Vector2 a = points[e.points[0]].pos;193Vector2 b = points[e.points[1]].pos;194195if (Geometry2D::segment_intersects_segment(a, b, from, to, nullptr)) {196can_see_eachother = false;197break;198}199}200201if (can_see_eachother) {202path.push_back(from);203path.push_back(to);204return path;205}206}207208//add to graph209210int aidx = points.size() - 2;211int bidx = points.size() - 1;212points.write[aidx].pos = from;213points.write[bidx].pos = to;214points.write[aidx].distance = 0;215points.write[bidx].distance = 0;216points.write[aidx].prev = -1;217points.write[bidx].prev = -1;218points.write[aidx].penalty = 0;219points.write[bidx].penalty = 0;220221for (int i = 0; i < points.size() - 2; i++) {222bool valid_a = true;223bool valid_b = true;224points.write[i].prev = -1;225points.write[i].distance = 0;226227if (!_is_point_inside(from * 0.5 + points[i].pos * 0.5)) {228valid_a = false;229}230231if (!_is_point_inside(to * 0.5 + points[i].pos * 0.5)) {232valid_b = false;233}234235for (const Edge &E : edges) {236const Edge &e = E;237238if (e.points[0] == i || e.points[1] == i) {239continue;240}241242Vector2 a = points[e.points[0]].pos;243Vector2 b = points[e.points[1]].pos;244245if (valid_a) {246if (e.points[0] != ignore_from_edge.points[1] &&247e.points[1] != ignore_from_edge.points[1] &&248e.points[0] != ignore_from_edge.points[0] &&249e.points[1] != ignore_from_edge.points[0]) {250if (Geometry2D::segment_intersects_segment(a, b, from, points[i].pos, nullptr)) {251valid_a = false;252}253}254}255256if (valid_b) {257if (e.points[0] != ignore_to_edge.points[1] &&258e.points[1] != ignore_to_edge.points[1] &&259e.points[0] != ignore_to_edge.points[0] &&260e.points[1] != ignore_to_edge.points[0]) {261if (Geometry2D::segment_intersects_segment(a, b, to, points[i].pos, nullptr)) {262valid_b = false;263}264}265}266267if (!valid_a && !valid_b) {268break;269}270}271272if (valid_a) {273points.write[i].connections.insert(aidx);274points.write[aidx].connections.insert(i);275}276277if (valid_b) {278points.write[i].connections.insert(bidx);279points.write[bidx].connections.insert(i);280}281}282//solve graph283284HashSet<int> open_list;285286points.write[aidx].distance = 0;287points.write[aidx].prev = aidx;288for (const int &E : points[aidx].connections) {289open_list.insert(E);290points.write[E].distance = from.distance_to(points[E].pos);291points.write[E].prev = aidx;292}293294bool found_route = false;295296while (true) {297if (open_list.is_empty()) {298print_verbose("Open list empty.");299break;300}301//check open list302303int least_cost_point = -1;304float least_cost = 1e30;305306//this could be faster (cache previous results)307for (const int &E : open_list) {308const Point &p = points[E];309float cost = p.distance;310cost += p.pos.distance_to(to);311cost += p.penalty;312313if (cost < least_cost) {314least_cost_point = E;315least_cost = cost;316}317}318319const Point &np = points[least_cost_point];320//open the neighbors for search321322for (const int &E : np.connections) {323Point &p = points.write[E];324float distance = np.pos.distance_to(p.pos) + np.distance;325326if (p.prev != -1) {327//oh this was visited already, can we win the cost?328329if (p.distance > distance) {330p.prev = least_cost_point; //reassign previous331p.distance = distance;332}333} else {334//add to open neighbors335336p.prev = least_cost_point;337p.distance = distance;338open_list.insert(E);339340if (E == bidx) {341//oh my reached end! stop algorithm342found_route = true;343break;344}345}346}347348if (found_route) {349break;350}351352open_list.erase(least_cost_point);353}354355if (found_route) {356int at = bidx;357path.push_back(points[at].pos);358do {359at = points[at].prev;360path.push_back(points[at].pos);361} while (at != aidx);362363path.reverse();364}365366for (int i = 0; i < points.size() - 2; i++) {367points.write[i].connections.erase(aidx);368points.write[i].connections.erase(bidx);369points.write[i].prev = -1;370points.write[i].distance = 0;371}372373points.write[aidx].connections.clear();374points.write[aidx].prev = -1;375points.write[aidx].distance = 0;376points.write[bidx].connections.clear();377points.write[bidx].prev = -1;378points.write[bidx].distance = 0;379380return path;381}382383void PolygonPathFinder::_set_data(const Dictionary &p_data) {384ERR_FAIL_COND(!p_data.has("points"));385ERR_FAIL_COND(!p_data.has("connections"));386ERR_FAIL_COND(!p_data.has("segments"));387ERR_FAIL_COND(!p_data.has("bounds"));388389Vector<Vector2> p = p_data["points"];390Array c = p_data["connections"];391392ERR_FAIL_COND(c.size() != p.size());393if (c.size()) {394return;395}396397int pc = p.size();398points.resize(pc + 2);399400const Vector2 *pr = p.ptr();401for (int i = 0; i < pc; i++) {402points.write[i].pos = pr[i];403Vector<int> con = c[i];404const int *cr = con.ptr();405int cc = con.size();406for (int j = 0; j < cc; j++) {407points.write[i].connections.insert(cr[j]);408}409}410411if (p_data.has("penalties")) {412Vector<real_t> penalties = p_data["penalties"];413if (penalties.size() == pc) {414const real_t *pr2 = penalties.ptr();415for (int i = 0; i < pc; i++) {416points.write[i].penalty = pr2[i];417}418}419}420421Vector<int> segs = p_data["segments"];422int sc = segs.size();423ERR_FAIL_COND(sc & 1);424const int *sr = segs.ptr();425for (int i = 0; i < sc; i += 2) {426Edge e(sr[i], sr[i + 1]);427edges.insert(e);428}429bounds = p_data["bounds"];430}431432Dictionary PolygonPathFinder::_get_data() const {433Dictionary d;434Vector<Vector2> p;435Vector<int> ind;436Array path_connections;437p.resize(MAX(0, points.size() - 2));438path_connections.resize(MAX(0, points.size() - 2));439ind.resize(edges.size() * 2);440Vector<real_t> penalties;441penalties.resize(MAX(0, points.size() - 2));442{443Vector2 *wp = p.ptrw();444real_t *pw = penalties.ptrw();445446for (int i = 0; i < points.size() - 2; i++) {447wp[i] = points[i].pos;448pw[i] = points[i].penalty;449Vector<int> c;450c.resize(points[i].connections.size());451{452int *cw = c.ptrw();453int idx = 0;454for (const int &E : points[i].connections) {455cw[idx++] = E;456}457}458path_connections[i] = c;459}460}461{462int *iw = ind.ptrw();463int idx = 0;464for (const Edge &E : edges) {465iw[idx++] = E.points[0];466iw[idx++] = E.points[1];467}468}469470d["bounds"] = bounds;471d["points"] = p;472d["penalties"] = penalties;473d["connections"] = path_connections;474d["segments"] = ind;475476return d;477}478479bool PolygonPathFinder::is_point_inside(const Vector2 &p_point) const {480return _is_point_inside(p_point);481}482483Vector2 PolygonPathFinder::get_closest_point(const Vector2 &p_point) const {484float closest_dist = 1e20f;485Vector2 closest_point;486487for (const Edge &E : edges) {488const Edge &e = E;489const Vector2 segment_a = points[e.points[0]].pos;490const Vector2 segment_b = points[e.points[1]].pos;491Vector2 closest = Geometry2D::get_closest_point_to_segment(p_point, segment_a, segment_b);492float d = p_point.distance_squared_to(closest);493494if (d < closest_dist) {495closest_dist = d;496closest_point = closest;497}498}499500ERR_FAIL_COND_V(Math::is_equal_approx(closest_dist, 1e20f), Vector2());501502return closest_point;503}504505Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2 &p_from, const Vector2 &p_to) const {506Vector<Vector2> inters;507508for (const Edge &E : edges) {509Vector2 a = points[E.points[0]].pos;510Vector2 b = points[E.points[1]].pos;511512Vector2 res;513if (Geometry2D::segment_intersects_segment(a, b, p_from, p_to, &res)) {514inters.push_back(res);515}516}517518return inters;519}520521Rect2 PolygonPathFinder::get_bounds() const {522return bounds;523}524525void PolygonPathFinder::set_point_penalty(int p_point, float p_penalty) {526ERR_FAIL_INDEX(p_point, points.size() - 2);527points.write[p_point].penalty = p_penalty;528}529530float PolygonPathFinder::get_point_penalty(int p_point) const {531ERR_FAIL_INDEX_V(p_point, points.size() - 2, 0);532return points[p_point].penalty;533}534535void PolygonPathFinder::_bind_methods() {536ClassDB::bind_method(D_METHOD("setup", "points", "connections"), &PolygonPathFinder::setup);537ClassDB::bind_method(D_METHOD("find_path", "from", "to"), &PolygonPathFinder::find_path);538ClassDB::bind_method(D_METHOD("get_intersections", "from", "to"), &PolygonPathFinder::get_intersections);539ClassDB::bind_method(D_METHOD("get_closest_point", "point"), &PolygonPathFinder::get_closest_point);540ClassDB::bind_method(D_METHOD("is_point_inside", "point"), &PolygonPathFinder::is_point_inside);541ClassDB::bind_method(D_METHOD("set_point_penalty", "idx", "penalty"), &PolygonPathFinder::set_point_penalty);542ClassDB::bind_method(D_METHOD("get_point_penalty", "idx"), &PolygonPathFinder::get_point_penalty);543544ClassDB::bind_method(D_METHOD("get_bounds"), &PolygonPathFinder::get_bounds);545ClassDB::bind_method(D_METHOD("_set_data", "data"), &PolygonPathFinder::_set_data);546ClassDB::bind_method(D_METHOD("_get_data"), &PolygonPathFinder::_get_data);547548ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR | PROPERTY_USAGE_INTERNAL), "_set_data", "_get_data");549}550551PolygonPathFinder::PolygonPathFinder() {552}553554555