polygon_path_finder.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603
  1. #include "polygon_path_finder.h"
  2. #include "geometry.h"
  3. bool PolygonPathFinder::_is_point_inside(const Vector2& p_point) const {
  4. int crosses=0;
  5. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  6. const Edge& e=E->get();
  7. Vector2 a = points[e.points[0]].pos;
  8. Vector2 b = points[e.points[1]].pos;
  9. if (Geometry::segment_intersects_segment_2d(a,b,p_point,outside_point,NULL)) {
  10. crosses++;
  11. }
  12. }
  13. return crosses&1;
  14. }
  15. void PolygonPathFinder::setup(const Vector<Vector2>& p_points, const Vector<int>& p_connections) {
  16. ERR_FAIL_COND(p_connections.size()&1);
  17. points.clear();
  18. edges.clear();
  19. //insert points
  20. int point_count=p_points.size();
  21. points.resize(point_count+2);
  22. bounds=Rect2();
  23. for(int i=0;i<p_points.size();i++) {
  24. points[i].pos=p_points[i];
  25. points[i].penalty=0;
  26. outside_point.x = i==0?p_points[0].x:(MAX( p_points[i].x, outside_point.x ));
  27. outside_point.y = i==0?p_points[0].y:(MAX( p_points[i].y, outside_point.y ));
  28. if (i==0) {
  29. bounds.pos=points[i].pos;
  30. } else {
  31. bounds.expand_to(points[i].pos);
  32. }
  33. }
  34. outside_point.x+=20.451+Math::randf()*10.2039;
  35. outside_point.y+=21.193+Math::randf()*12.5412;
  36. //insert edges (which are also connetions)
  37. for(int i=0;i<p_connections.size();i+=2) {
  38. Edge e(p_connections[i],p_connections[i+1]);
  39. ERR_FAIL_INDEX(e.points[0],point_count);
  40. ERR_FAIL_INDEX(e.points[1],point_count);
  41. points[p_connections[i]].connections.insert(p_connections[i+1]);
  42. points[p_connections[i+1]].connections.insert(p_connections[i]);
  43. edges.insert(e);
  44. }
  45. //fill the remaining connections based on visibility
  46. for(int i=0;i<point_count;i++) {
  47. for(int j=i+1;j<point_count;j++) {
  48. if (edges.has(Edge(i,j)))
  49. continue; //if in edge ignore
  50. Vector2 from=points[i].pos;
  51. Vector2 to=points[j].pos;
  52. if (!_is_point_inside(from*0.5+to*0.5)) //connection between points in inside space
  53. continue;
  54. bool valid=true;
  55. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  56. const Edge& e=E->get();
  57. if (e.points[0]==i || e.points[1]==i || e.points[0]==j || e.points[1]==j )
  58. continue;
  59. Vector2 a = points[e.points[0]].pos;
  60. Vector2 b = points[e.points[1]].pos;
  61. if (Geometry::segment_intersects_segment_2d(a,b,from,to,NULL)) {
  62. valid=false;
  63. break;
  64. }
  65. }
  66. if (valid) {
  67. points[i].connections.insert(j);
  68. points[j].connections.insert(i);
  69. }
  70. }
  71. }
  72. }
  73. Vector<Vector2> PolygonPathFinder::find_path(const Vector2& p_from, const Vector2& p_to) {
  74. Vector<Vector2> path;
  75. Vector2 from=p_from;
  76. Vector2 to=p_to;
  77. Edge ignore_from_edge(-1,-1);
  78. Edge ignore_to_edge(-1,-1);
  79. if (!_is_point_inside(from)) {
  80. float closest_dist=1e20;
  81. Vector2 closest_point;
  82. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  83. const Edge& e=E->get();
  84. Vector2 seg[2]={
  85. points[e.points[0]].pos,
  86. points[e.points[1]].pos
  87. };
  88. Vector2 closest = Geometry::get_closest_point_to_segment_2d(from,seg);
  89. float d = from.distance_squared_to(closest);
  90. if (d<closest_dist) {
  91. ignore_from_edge=E->get();
  92. closest_dist=d;
  93. }
  94. }
  95. from=closest_point;
  96. };
  97. if (!_is_point_inside(to)) {
  98. float closest_dist=1e20;
  99. Vector2 closest_point;
  100. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  101. const Edge& e=E->get();
  102. Vector2 seg[2]={
  103. points[e.points[0]].pos,
  104. points[e.points[1]].pos
  105. };
  106. Vector2 closest = Geometry::get_closest_point_to_segment_2d(to,seg);
  107. float d = to.distance_squared_to(closest);
  108. if (d<closest_dist) {
  109. ignore_to_edge=E->get();
  110. closest_dist=d;
  111. }
  112. }
  113. to=closest_point;
  114. };
  115. //test direct connection
  116. {
  117. bool can_see_eachother=true;
  118. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  119. const Edge& e=E->get();
  120. if (e.points[0]==ignore_from_edge.points[0] && e.points[1]==ignore_from_edge.points[1])
  121. continue;
  122. if (e.points[0]==ignore_to_edge.points[0] && e.points[1]==ignore_to_edge.points[1])
  123. continue;
  124. Vector2 a = points[e.points[0]].pos;
  125. Vector2 b = points[e.points[1]].pos;
  126. if (Geometry::segment_intersects_segment_2d(a,b,from,to,NULL)) {
  127. can_see_eachother=false;
  128. break;
  129. }
  130. }
  131. if (can_see_eachother) {
  132. path.push_back(from);
  133. path.push_back(to);
  134. return path;
  135. }
  136. }
  137. //add to graph
  138. int aidx = points.size()-2;
  139. int bidx = points.size()-1;
  140. points[aidx].pos=from;
  141. points[bidx].pos=to;
  142. points[aidx].distance=0;
  143. points[bidx].distance=0;
  144. points[aidx].prev=-1;
  145. points[bidx].prev=-1;
  146. points[aidx].penalty=0;
  147. points[bidx].penalty=0;
  148. for(int i=0;i<points.size()-2;i++) {
  149. bool valid_a=true;
  150. bool valid_b=true;
  151. points[i].prev=-1;
  152. points[i].distance=0;
  153. if (!_is_point_inside(from*0.5+points[i].pos*0.5)) {
  154. valid_a=false;
  155. }
  156. if (!_is_point_inside(to*0.5+points[i].pos*0.5)) {
  157. valid_b=false;
  158. }
  159. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  160. const Edge& e=E->get();
  161. if (e.points[0]==i || e.points[1]==i)
  162. continue;
  163. Vector2 a = points[e.points[0]].pos;
  164. Vector2 b = points[e.points[1]].pos;
  165. if (valid_a) {
  166. if (e.points[0]!=ignore_from_edge.points[1] &&
  167. e.points[1]!=ignore_from_edge.points[1] &&
  168. e.points[0]!=ignore_from_edge.points[0] &&
  169. e.points[1]!=ignore_from_edge.points[0]) {
  170. if (Geometry::segment_intersects_segment_2d(a,b,from,points[i].pos,NULL)) {
  171. valid_a=false;
  172. }
  173. }
  174. }
  175. if (valid_b) {
  176. if (e.points[0]!=ignore_to_edge.points[1] &&
  177. e.points[1]!=ignore_to_edge.points[1] &&
  178. e.points[0]!=ignore_to_edge.points[0] &&
  179. e.points[1]!=ignore_to_edge.points[0]) {
  180. if (Geometry::segment_intersects_segment_2d(a,b,to,points[i].pos,NULL)) {
  181. valid_b=false;
  182. }
  183. }
  184. }
  185. if (!valid_a && !valid_b)
  186. break;
  187. }
  188. if (valid_a) {
  189. points[i].connections.insert(aidx);
  190. points[aidx].connections.insert(i);
  191. }
  192. if (valid_b) {
  193. points[i].connections.insert(bidx);
  194. points[bidx].connections.insert(i);
  195. }
  196. }
  197. //solve graph
  198. Set<int> open_list;
  199. points[aidx].distance=0;
  200. points[aidx].prev=aidx;
  201. for(Set<int>::Element *E=points[aidx].connections.front();E;E=E->next()) {
  202. open_list.insert(E->get());
  203. points[E->get()].distance=from.distance_to(points[E->get()].pos);
  204. points[E->get()].prev=aidx;
  205. }
  206. bool found_route=false;
  207. while(true) {
  208. if (open_list.size()==0) {
  209. printf("open list empty\n");
  210. break;
  211. }
  212. //check open list
  213. int least_cost_point=-1;
  214. float least_cost=1e30;
  215. //this could be faster (cache previous results)
  216. for (Set<int>::Element *E=open_list.front();E;E=E->next()) {
  217. const Point& p =points[E->get()];
  218. float cost = p.distance;
  219. cost+=p.pos.distance_to(to);
  220. cost+=p.penalty;
  221. if (cost<least_cost) {
  222. least_cost_point=E->get();
  223. least_cost=cost;
  224. }
  225. }
  226. Point &np = points[least_cost_point];
  227. //open the neighbours for search
  228. for(Set<int>::Element *E=np.connections.front();E;E=E->next()) {
  229. Point& p =points[E->get()];
  230. float distance = np.pos.distance_to(p.pos) + np.distance;
  231. if (p.prev!=-1) {
  232. //oh this was visited already, can we win the cost?
  233. if (p.distance>distance) {
  234. p.prev=least_cost_point; //reasign previous
  235. p.distance=distance;
  236. }
  237. } else {
  238. //add to open neighbours
  239. p.prev=least_cost_point;
  240. p.distance=distance;
  241. open_list.insert(E->get());
  242. if (E->get()==bidx) {
  243. //oh my reached end! stop algorithm
  244. found_route=true;
  245. break;
  246. }
  247. }
  248. }
  249. if (found_route)
  250. break;
  251. open_list.erase(least_cost_point);
  252. }
  253. if (found_route) {
  254. int at = bidx;
  255. path.push_back(points[at].pos);
  256. do {
  257. at=points[at].prev;
  258. path.push_back(points[at].pos);
  259. } while (at!=aidx);
  260. path.invert();;
  261. }
  262. for(int i=0;i<points.size()-2;i++) {
  263. points[i].connections.erase(aidx);
  264. points[i].connections.erase(bidx);
  265. points[i].prev=-1;
  266. points[i].distance=0;
  267. }
  268. points[aidx].connections.clear();
  269. points[aidx].prev=-1;
  270. points[aidx].distance=0;
  271. points[bidx].connections.clear();
  272. points[bidx].prev=-1;
  273. points[bidx].distance=0;
  274. return path;
  275. }
  276. void PolygonPathFinder::_set_data(const Dictionary& p_data) {
  277. ERR_FAIL_COND(!p_data.has("points"));
  278. ERR_FAIL_COND(!p_data.has("connections"));
  279. ERR_FAIL_COND(!p_data.has("segments"));
  280. ERR_FAIL_COND(!p_data.has("bounds"));
  281. DVector<Vector2> p=p_data["points"];
  282. Array c=p_data["connections"];
  283. ERR_FAIL_COND(c.size()!=p.size());
  284. if (c.size())
  285. return;
  286. int pc = p.size();
  287. points.resize(pc+2);
  288. DVector<Vector2>::Read pr=p.read();
  289. for(int i=0;i<pc;i++) {
  290. points[i].pos=pr[i];
  291. DVector<int> con=c[i];
  292. DVector<int>::Read cr=con.read();
  293. int cc=con.size();
  294. for(int j=0;j<cc;j++) {
  295. points[i].connections.insert(cr[j]);
  296. }
  297. }
  298. if (p_data.has("penalties")) {
  299. DVector<float> penalties=p_data["penalties"];
  300. if (penalties.size()==pc) {
  301. DVector<float>::Read pr = penalties.read();
  302. for(int i=0;i<pc;i++) {
  303. points[i].penalty=pr[i];
  304. }
  305. }
  306. }
  307. DVector<int> segs=p_data["segments"];
  308. int sc=segs.size();
  309. ERR_FAIL_COND(sc&1);
  310. DVector<int>::Read sr = segs.read();
  311. for(int i=0;i<sc;i+=2) {
  312. Edge e(sr[i],sr[i+1]);
  313. edges.insert(e);
  314. }
  315. bounds=p_data["bounds"];
  316. }
  317. Dictionary PolygonPathFinder::_get_data() const{
  318. Dictionary d;
  319. DVector<Vector2> p;
  320. DVector<int> ind;
  321. Array connections;
  322. p.resize(points.size()-2);
  323. connections.resize(points.size()-2);
  324. ind.resize(edges.size()*2);
  325. DVector<float> penalties;
  326. penalties.resize(points.size()-2);
  327. {
  328. DVector<Vector2>::Write wp=p.write();
  329. DVector<float>::Write pw=penalties.write();
  330. for(int i=0;i<points.size()-2;i++) {
  331. wp[i]=points[i].pos;
  332. pw[i]=points[i].penalty;
  333. DVector<int> c;
  334. c.resize(points[i].connections.size());
  335. {
  336. DVector<int>::Write cw=c.write();
  337. int idx=0;
  338. for (Set<int>::Element *E=points[i].connections.front();E;E=E->next()) {
  339. cw[idx++]=E->get();
  340. }
  341. }
  342. connections[i]=c;
  343. }
  344. }
  345. {
  346. DVector<int>::Write iw=ind.write();
  347. int idx=0;
  348. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  349. iw[idx++]=E->get().points[0];
  350. iw[idx++]=E->get().points[1];
  351. }
  352. }
  353. d["bounds"]=bounds;
  354. d["points"]=p;
  355. d["penalties"]=penalties;
  356. d["connections"]=connections;
  357. d["segments"]=ind;
  358. return d;
  359. }
  360. bool PolygonPathFinder::is_point_inside(const Vector2& p_point) const {
  361. return _is_point_inside(p_point);
  362. }
  363. Vector2 PolygonPathFinder::get_closest_point(const Vector2& p_point) const {
  364. int closest_idx=-1;
  365. float closest_dist=1e20;
  366. for(int i=0;i<points.size()-2;i++) {
  367. float d = p_point.distance_squared_to(points[i].pos);
  368. if (d<closest_dist) {
  369. d=closest_dist;
  370. closest_idx=i;
  371. }
  372. }
  373. ERR_FAIL_COND_V(closest_idx==-1,Vector2());
  374. return points[closest_idx].pos;
  375. }
  376. Vector<Vector2> PolygonPathFinder::get_intersections(const Vector2& p_from, const Vector2& p_to) const {
  377. Vector<Vector2> inters;
  378. for (Set<Edge>::Element *E=edges.front();E;E=E->next()) {
  379. Vector2 a = points[E->get().points[0]].pos;
  380. Vector2 b = points[E->get().points[1]].pos;
  381. Vector2 res;
  382. if (Geometry::segment_intersects_segment_2d(a,b,p_from,p_to,&res)) {
  383. inters.push_back(res);
  384. }
  385. }
  386. return inters;
  387. }
  388. Rect2 PolygonPathFinder::get_bounds() const {
  389. return bounds;
  390. }
  391. void PolygonPathFinder::set_point_penalty(int p_point,float p_penalty) {
  392. ERR_FAIL_INDEX(p_point,points.size()-2);
  393. points[p_point].penalty=p_penalty;
  394. }
  395. float PolygonPathFinder::get_point_penalty(int p_point) const {
  396. ERR_FAIL_INDEX_V(p_point,points.size()-2,0);
  397. return points[p_point].penalty;
  398. }
  399. void PolygonPathFinder::_bind_methods() {
  400. ObjectTypeDB::bind_method(_MD("setup","points","connections"),&PolygonPathFinder::setup);
  401. ObjectTypeDB::bind_method(_MD("find_path","from","to"),&PolygonPathFinder::find_path);
  402. ObjectTypeDB::bind_method(_MD("get_intersections","from","to"),&PolygonPathFinder::get_intersections);
  403. ObjectTypeDB::bind_method(_MD("get_closest_point","point"),&PolygonPathFinder::get_closest_point);
  404. ObjectTypeDB::bind_method(_MD("is_point_inside","point"),&PolygonPathFinder::is_point_inside);
  405. ObjectTypeDB::bind_method(_MD("set_point_penalty","idx","penalty"),&PolygonPathFinder::set_point_penalty);
  406. ObjectTypeDB::bind_method(_MD("get_point_penalty","idx"),&PolygonPathFinder::get_point_penalty);
  407. ObjectTypeDB::bind_method(_MD("get_bounds"),&PolygonPathFinder::get_bounds);
  408. ObjectTypeDB::bind_method(_MD("_set_data"),&PolygonPathFinder::_set_data);
  409. ObjectTypeDB::bind_method(_MD("_get_data"),&PolygonPathFinder::_get_data);
  410. ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY,"data",PROPERTY_HINT_NONE,"",PROPERTY_USAGE_NOEDITOR),_SCS("_set_data"),_SCS("_get_data"));
  411. }
  412. PolygonPathFinder::PolygonPathFinder()
  413. {
  414. }