image_path_finder.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*************************************************************************/
  2. /* image_path_finder.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  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. /*************************************************************************/
  29. #include "image_path_finder.h"
  30. void ImagePathFinder::_unlock() {
  31. lock=DVector<Cell>::Write();
  32. cells=NULL;
  33. }
  34. void ImagePathFinder::_lock() {
  35. lock = cell_data.write();
  36. cells=lock.ptr();
  37. }
  38. bool ImagePathFinder::_can_go_straigth(const Point2& p_from, const Point2& p_to) const {
  39. int x1=p_from.x;
  40. int y1=p_from.y;
  41. int x2=p_to.x;
  42. int y2=p_to.y;
  43. #define _TEST_VALID \
  44. {\
  45. uint32_t ofs=drawy*width+drawx;\
  46. if (cells[ofs].solid) {\
  47. if (!((drawx>0 && cells[ofs-1].visited) ||\
  48. (drawx<width-1 && cells[ofs+1].visited) ||\
  49. (drawy>0 && cells[ofs-width].visited) ||\
  50. (drawy<height-1 && cells[ofs+width].visited))) {\
  51. return false;\
  52. }\
  53. }\
  54. }\
  55. int n, deltax, deltay, sgndeltax, sgndeltay, deltaxabs, deltayabs, x, y, drawx, drawy;
  56. deltax = x2 - x1;
  57. deltay = y2 - y1;
  58. deltaxabs = ABS(deltax);
  59. deltayabs = ABS(deltay);
  60. sgndeltax = SGN(deltax);
  61. sgndeltay = SGN(deltay);
  62. x = deltayabs >> 1;
  63. y = deltaxabs >> 1;
  64. drawx = x1;
  65. drawy = y1;
  66. int pc=0;
  67. _TEST_VALID
  68. if(deltaxabs >= deltayabs) {
  69. for(n = 0; n < deltaxabs; n++) {
  70. y += deltayabs;
  71. if(y >= deltaxabs){
  72. y -= deltaxabs;
  73. drawy += sgndeltay;
  74. }
  75. drawx += sgndeltax;
  76. _TEST_VALID
  77. }
  78. } else {
  79. for(n = 0; n < deltayabs; n++) {
  80. x += deltaxabs;
  81. if(x >= deltayabs) {
  82. x -= deltayabs;
  83. drawx += sgndeltax;
  84. }
  85. drawy += sgndeltay;
  86. _TEST_VALID
  87. }
  88. }
  89. return true;
  90. }
  91. bool ImagePathFinder::_is_linear_path(const Point2& p_from, const Point2& p_to) {
  92. int x1=p_from.x;
  93. int y1=p_from.y;
  94. int x2=p_to.x;
  95. int y2=p_to.y;
  96. #define _TEST_CELL \
  97. if (cells[drawy*width+drawx].solid)\
  98. return false;
  99. int n, deltax, deltay, sgndeltax, sgndeltay, deltaxabs, deltayabs, x, y, drawx, drawy;
  100. deltax = x2 - x1;
  101. deltay = y2 - y1;
  102. deltaxabs = ABS(deltax);
  103. deltayabs = ABS(deltay);
  104. sgndeltax = SGN(deltax);
  105. sgndeltay = SGN(deltay);
  106. x = deltayabs >> 1;
  107. y = deltaxabs >> 1;
  108. drawx = x1;
  109. drawy = y1;
  110. int pc=0;
  111. _TEST_CELL
  112. if(deltaxabs >= deltayabs) {
  113. for(n = 0; n < deltaxabs; n++) {
  114. y += deltayabs;
  115. if(y >= deltaxabs){
  116. y -= deltaxabs;
  117. drawy += sgndeltay;
  118. }
  119. drawx += sgndeltax;
  120. _TEST_CELL
  121. }
  122. } else {
  123. for(n = 0; n < deltayabs; n++) {
  124. x += deltaxabs;
  125. if(x >= deltayabs) {
  126. x -= deltayabs;
  127. drawx += sgndeltax;
  128. }
  129. drawy += sgndeltay;
  130. _TEST_CELL
  131. }
  132. }
  133. return true;
  134. }
  135. DVector<Point2> ImagePathFinder::find_path(const Point2& p_from, const Point2& p_to,bool p_optimize) {
  136. Point2i from=p_from;
  137. Point2i to=p_to;
  138. ERR_FAIL_COND_V(from.x < 0,DVector<Point2>());
  139. ERR_FAIL_COND_V(from.y < 0,DVector<Point2>());
  140. ERR_FAIL_COND_V(from.x >=width,DVector<Point2>());
  141. ERR_FAIL_COND_V(from.y >=height,DVector<Point2>());
  142. ERR_FAIL_COND_V(to.x < 0,DVector<Point2>());
  143. ERR_FAIL_COND_V(to.y < 0,DVector<Point2>());
  144. ERR_FAIL_COND_V(to.x >=width,DVector<Point2>());
  145. ERR_FAIL_COND_V(to.y >=height,DVector<Point2>());
  146. if (from==to) {
  147. DVector<Point2> p;
  148. p.push_back(from);
  149. return p;
  150. }
  151. _lock();
  152. if (p_optimize) { //try a line first
  153. if (_is_linear_path(p_from,p_to)) {
  154. _unlock();
  155. DVector<Point2> p;
  156. p.push_back(from);
  157. p.push_back(to);
  158. return p;
  159. }
  160. }
  161. //clear all
  162. for(int i=0;i<width*height;i++) {
  163. bool s = cells[i].solid;
  164. cells[i].data=0;
  165. cells[i].solid=s;
  166. }
  167. #define CELL_INDEX(m_p) (m_p.y*width+m_p.x)
  168. #define CELL_COST(m_p) (cells[CELL_INDEX(m_p)].cost+( ABS(m_p.x-to.x)+ABS(m_p.y-to.y))*10)
  169. Set<Point2i> pending;
  170. pending.insert(from);
  171. //helper constants
  172. static const Point2i neighbour_rel[8]={
  173. Point2i(-1,-1), //0
  174. Point2i(-1, 0), //1
  175. Point2i(-1,+1), //2
  176. Point2i( 0,-1), //3
  177. Point2i( 0,+1), //4
  178. Point2i(+1,-1), //5
  179. Point2i(+1, 0), //6
  180. Point2i(+1,+1) }; //7
  181. static const int neighbour_cost[8]={
  182. 14,
  183. 10,
  184. 14,
  185. 10,
  186. 10,
  187. 14,
  188. 10,
  189. 14
  190. };
  191. static const int neighbour_parent[8]={
  192. 7,
  193. 6,
  194. 5,
  195. 4,
  196. 3,
  197. 2,
  198. 1,
  199. 0,
  200. };
  201. while(true) {
  202. if (pending.size() == 0) {
  203. _unlock();
  204. return DVector<Point2>(); // points don't connect
  205. }
  206. Point2i current;
  207. int lc=0x7FFFFFFF;
  208. { //find the one with the least cost
  209. Set<Point2i>::Element *Efound=NULL;
  210. for (Set<Point2i>::Element *E=pending.front();E;E=E->next()) {
  211. int cc =CELL_COST(E->get());
  212. if (cc<lc) {
  213. lc=cc;
  214. current=E->get();
  215. Efound=E;
  216. }
  217. }
  218. pending.erase(Efound);
  219. }
  220. Cell &c = cells[CELL_INDEX(current)];
  221. //search around other cells
  222. int accum_cost = (from==current) ? 0 : cells[CELL_INDEX((current + neighbour_rel[c.parent]))].cost;
  223. bool done=false;
  224. for(int i=0;i<8;i++) {
  225. Point2i neighbour=current+neighbour_rel[i];
  226. if (neighbour.x<0 || neighbour.y<0 || neighbour.x>=width || neighbour.y>=height)
  227. continue;
  228. Cell &n = cells[CELL_INDEX(neighbour)];
  229. if (n.solid)
  230. continue; //no good
  231. int cost = neighbour_cost[i]+accum_cost;
  232. if (n.visited && n.cost < cost)
  233. continue;
  234. n.cost=cost;
  235. n.parent=neighbour_parent[i];
  236. n.visited=true;
  237. pending.insert(neighbour);
  238. if (neighbour==to)
  239. done=true;
  240. }
  241. if (done)
  242. break;
  243. }
  244. // go througuh poins twice, first compute amount, then add them
  245. Point2i current=to;
  246. int pcount=0;
  247. while(true) {
  248. Cell &c = cells[CELL_INDEX(current)];
  249. c.visited=true;
  250. pcount++;
  251. if (current==from)
  252. break;
  253. current+=neighbour_rel[ c.parent ];
  254. }
  255. //now place them in an array
  256. DVector<Vector2> result;
  257. result.resize(pcount);
  258. DVector<Vector2>::Write res=result.write();
  259. current=to;
  260. int pidx=pcount-1;
  261. while(true) {
  262. Cell &c = cells[CELL_INDEX(current)];
  263. res[pidx]=current;
  264. pidx--;
  265. if (current==from)
  266. break;
  267. current+=neighbour_rel[ c.parent ];
  268. }
  269. //simplify..
  270. if (p_optimize) {
  271. int p=pcount-1;
  272. while(p>0) {
  273. int limit=p;
  274. while(limit>0) {
  275. limit--;
  276. if (!_can_go_straigth(res[p],res[limit]))
  277. break;
  278. }
  279. if (limit<p-1) {
  280. int diff = p-limit-1;
  281. pcount-=diff;
  282. for(int i=limit+1;i<pcount;i++) {
  283. res[i]=res[i+diff];
  284. }
  285. }
  286. p=limit;
  287. }
  288. }
  289. res=DVector<Vector2>::Write();
  290. result.resize(pcount);
  291. return result;
  292. }
  293. Size2 ImagePathFinder::get_size() const {
  294. return Size2(width,height);
  295. }
  296. bool ImagePathFinder::is_solid(const Point2& p_pos) {
  297. Point2i pos = p_pos;
  298. ERR_FAIL_COND_V(pos.x<0,true);
  299. ERR_FAIL_COND_V(pos.y<0,true);
  300. ERR_FAIL_COND_V(pos.x>=width,true);
  301. ERR_FAIL_COND_V(pos.y>=height,true);
  302. return cell_data[pos.y*width+pos.x].solid;
  303. }
  304. void ImagePathFinder::create_from_image_alpha(const Image& p_image) {
  305. ERR_FAIL_COND(p_image.get_format() != Image::FORMAT_RGBA);
  306. width = p_image.get_width();
  307. height = p_image.get_height();
  308. DVector<uint8_t> data = p_image.get_data();
  309. cell_data.resize(width * height);
  310. DVector<uint8_t>::Read read = data.read();
  311. DVector<Cell>::Write write = cell_data.write();
  312. for (int i=0; i<width * height; i++) {
  313. Cell cell;
  314. cell.data = 0;
  315. cell.solid = read[i*4+3] < 128;
  316. write[i] = cell;
  317. };
  318. };
  319. void ImagePathFinder::_bind_methods() {
  320. ObjectTypeDB::bind_method(_MD("find_path","from","to","optimize"),&ImagePathFinder::find_path,DEFVAL(false));
  321. ObjectTypeDB::bind_method(_MD("get_size"),&ImagePathFinder::get_size);
  322. ObjectTypeDB::bind_method(_MD("is_solid","pos"),&ImagePathFinder::is_solid);
  323. ObjectTypeDB::bind_method(_MD("create_from_image_alpha"),&ImagePathFinder::create_from_image_alpha);
  324. }
  325. ImagePathFinder::ImagePathFinder()
  326. {
  327. cells=NULL;
  328. width=0;
  329. height=0;
  330. }