RecastArea.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590
  1. //
  2. // Copyright (c) 2009-2010 Mikko Mononen [email protected]
  3. //
  4. // This software is provided 'as-is', without any express or implied
  5. // warranty. In no event will the authors be held liable for any damages
  6. // arising from the use of this software.
  7. // Permission is granted to anyone to use this software for any purpose,
  8. // including commercial applications, and to alter it and redistribute it
  9. // freely, subject to the following restrictions:
  10. // 1. The origin of this software must not be misrepresented; you must not
  11. // claim that you wrote the original software. If you use this software
  12. // in a product, an acknowledgment in the product documentation would be
  13. // appreciated but is not required.
  14. // 2. Altered source versions must be plainly marked as such, and must not be
  15. // misrepresented as being the original software.
  16. // 3. This notice may not be removed or altered from any source distribution.
  17. //
  18. #include <float.h>
  19. #include <math.h>
  20. #include <string.h>
  21. #include <stdlib.h>
  22. #include <stdio.h>
  23. #include "Recast.h"
  24. #include "RecastAlloc.h"
  25. #include "RecastAssert.h"
  26. /// @par
  27. ///
  28. /// Basically, any spans that are closer to a boundary or obstruction than the specified radius
  29. /// are marked as unwalkable.
  30. ///
  31. /// This method is usually called immediately after the heightfield has been built.
  32. ///
  33. /// @see rcCompactHeightfield, rcBuildCompactHeightfield, rcConfig::walkableRadius
  34. bool rcErodeWalkableArea(rcContext* ctx, int radius, rcCompactHeightfield& chf)
  35. {
  36. rcAssert(ctx);
  37. const int w = chf.width;
  38. const int h = chf.height;
  39. rcScopedTimer timer(ctx, RC_TIMER_ERODE_AREA);
  40. unsigned char* dist = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP);
  41. if (!dist)
  42. {
  43. ctx->log(RC_LOG_ERROR, "erodeWalkableArea: Out of memory 'dist' (%d).", chf.spanCount);
  44. return false;
  45. }
  46. // Init distance.
  47. memset(dist, 0xff, sizeof(unsigned char)*chf.spanCount);
  48. // Mark boundary cells.
  49. for (int y = 0; y < h; ++y)
  50. {
  51. for (int x = 0; x < w; ++x)
  52. {
  53. const rcCompactCell& c = chf.cells[x+y*w];
  54. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  55. {
  56. if (chf.areas[i] == RC_NULL_AREA)
  57. {
  58. dist[i] = 0;
  59. }
  60. else
  61. {
  62. const rcCompactSpan& s = chf.spans[i];
  63. int nc = 0;
  64. for (int dir = 0; dir < 4; ++dir)
  65. {
  66. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  67. {
  68. const int nx = x + rcGetDirOffsetX(dir);
  69. const int ny = y + rcGetDirOffsetY(dir);
  70. const int nidx = (int)chf.cells[nx+ny*w].index + rcGetCon(s, dir);
  71. if (chf.areas[nidx] != RC_NULL_AREA)
  72. {
  73. nc++;
  74. }
  75. }
  76. }
  77. // At least one missing neighbour.
  78. if (nc != 4)
  79. dist[i] = 0;
  80. }
  81. }
  82. }
  83. }
  84. unsigned char nd;
  85. // Pass 1
  86. for (int y = 0; y < h; ++y)
  87. {
  88. for (int x = 0; x < w; ++x)
  89. {
  90. const rcCompactCell& c = chf.cells[x+y*w];
  91. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  92. {
  93. const rcCompactSpan& s = chf.spans[i];
  94. if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
  95. {
  96. // (-1,0)
  97. const int ax = x + rcGetDirOffsetX(0);
  98. const int ay = y + rcGetDirOffsetY(0);
  99. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
  100. const rcCompactSpan& as = chf.spans[ai];
  101. nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
  102. if (nd < dist[i])
  103. dist[i] = nd;
  104. // (-1,-1)
  105. if (rcGetCon(as, 3) != RC_NOT_CONNECTED)
  106. {
  107. const int aax = ax + rcGetDirOffsetX(3);
  108. const int aay = ay + rcGetDirOffsetY(3);
  109. const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 3);
  110. nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
  111. if (nd < dist[i])
  112. dist[i] = nd;
  113. }
  114. }
  115. if (rcGetCon(s, 3) != RC_NOT_CONNECTED)
  116. {
  117. // (0,-1)
  118. const int ax = x + rcGetDirOffsetX(3);
  119. const int ay = y + rcGetDirOffsetY(3);
  120. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
  121. const rcCompactSpan& as = chf.spans[ai];
  122. nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
  123. if (nd < dist[i])
  124. dist[i] = nd;
  125. // (1,-1)
  126. if (rcGetCon(as, 2) != RC_NOT_CONNECTED)
  127. {
  128. const int aax = ax + rcGetDirOffsetX(2);
  129. const int aay = ay + rcGetDirOffsetY(2);
  130. const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 2);
  131. nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
  132. if (nd < dist[i])
  133. dist[i] = nd;
  134. }
  135. }
  136. }
  137. }
  138. }
  139. // Pass 2
  140. for (int y = h-1; y >= 0; --y)
  141. {
  142. for (int x = w-1; x >= 0; --x)
  143. {
  144. const rcCompactCell& c = chf.cells[x+y*w];
  145. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  146. {
  147. const rcCompactSpan& s = chf.spans[i];
  148. if (rcGetCon(s, 2) != RC_NOT_CONNECTED)
  149. {
  150. // (1,0)
  151. const int ax = x + rcGetDirOffsetX(2);
  152. const int ay = y + rcGetDirOffsetY(2);
  153. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 2);
  154. const rcCompactSpan& as = chf.spans[ai];
  155. nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
  156. if (nd < dist[i])
  157. dist[i] = nd;
  158. // (1,1)
  159. if (rcGetCon(as, 1) != RC_NOT_CONNECTED)
  160. {
  161. const int aax = ax + rcGetDirOffsetX(1);
  162. const int aay = ay + rcGetDirOffsetY(1);
  163. const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 1);
  164. nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
  165. if (nd < dist[i])
  166. dist[i] = nd;
  167. }
  168. }
  169. if (rcGetCon(s, 1) != RC_NOT_CONNECTED)
  170. {
  171. // (0,1)
  172. const int ax = x + rcGetDirOffsetX(1);
  173. const int ay = y + rcGetDirOffsetY(1);
  174. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 1);
  175. const rcCompactSpan& as = chf.spans[ai];
  176. nd = (unsigned char)rcMin((int)dist[ai]+2, 255);
  177. if (nd < dist[i])
  178. dist[i] = nd;
  179. // (-1,1)
  180. if (rcGetCon(as, 0) != RC_NOT_CONNECTED)
  181. {
  182. const int aax = ax + rcGetDirOffsetX(0);
  183. const int aay = ay + rcGetDirOffsetY(0);
  184. const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 0);
  185. nd = (unsigned char)rcMin((int)dist[aai]+3, 255);
  186. if (nd < dist[i])
  187. dist[i] = nd;
  188. }
  189. }
  190. }
  191. }
  192. }
  193. const unsigned char thr = (unsigned char)(radius*2);
  194. for (int i = 0; i < chf.spanCount; ++i)
  195. if (dist[i] < thr)
  196. chf.areas[i] = RC_NULL_AREA;
  197. rcFree(dist);
  198. return true;
  199. }
  200. static void insertSort(unsigned char* a, const int n)
  201. {
  202. int i, j;
  203. for (i = 1; i < n; i++)
  204. {
  205. const unsigned char value = a[i];
  206. for (j = i - 1; j >= 0 && a[j] > value; j--)
  207. a[j+1] = a[j];
  208. a[j+1] = value;
  209. }
  210. }
  211. /// @par
  212. ///
  213. /// This filter is usually applied after applying area id's using functions
  214. /// such as #rcMarkBoxArea, #rcMarkConvexPolyArea, and #rcMarkCylinderArea.
  215. ///
  216. /// @see rcCompactHeightfield
  217. bool rcMedianFilterWalkableArea(rcContext* ctx, rcCompactHeightfield& chf)
  218. {
  219. rcAssert(ctx);
  220. const int w = chf.width;
  221. const int h = chf.height;
  222. rcScopedTimer timer(ctx, RC_TIMER_MEDIAN_AREA);
  223. unsigned char* areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*chf.spanCount, RC_ALLOC_TEMP);
  224. if (!areas)
  225. {
  226. ctx->log(RC_LOG_ERROR, "medianFilterWalkableArea: Out of memory 'areas' (%d).", chf.spanCount);
  227. return false;
  228. }
  229. // Init distance.
  230. memset(areas, 0xff, sizeof(unsigned char)*chf.spanCount);
  231. for (int y = 0; y < h; ++y)
  232. {
  233. for (int x = 0; x < w; ++x)
  234. {
  235. const rcCompactCell& c = chf.cells[x+y*w];
  236. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  237. {
  238. const rcCompactSpan& s = chf.spans[i];
  239. if (chf.areas[i] == RC_NULL_AREA)
  240. {
  241. areas[i] = chf.areas[i];
  242. continue;
  243. }
  244. unsigned char nei[9];
  245. for (int j = 0; j < 9; ++j)
  246. nei[j] = chf.areas[i];
  247. for (int dir = 0; dir < 4; ++dir)
  248. {
  249. if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
  250. {
  251. const int ax = x + rcGetDirOffsetX(dir);
  252. const int ay = y + rcGetDirOffsetY(dir);
  253. const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
  254. if (chf.areas[ai] != RC_NULL_AREA)
  255. nei[dir*2+0] = chf.areas[ai];
  256. const rcCompactSpan& as = chf.spans[ai];
  257. const int dir2 = (dir+1) & 0x3;
  258. if (rcGetCon(as, dir2) != RC_NOT_CONNECTED)
  259. {
  260. const int ax2 = ax + rcGetDirOffsetX(dir2);
  261. const int ay2 = ay + rcGetDirOffsetY(dir2);
  262. const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
  263. if (chf.areas[ai2] != RC_NULL_AREA)
  264. nei[dir*2+1] = chf.areas[ai2];
  265. }
  266. }
  267. }
  268. insertSort(nei, 9);
  269. areas[i] = nei[4];
  270. }
  271. }
  272. }
  273. memcpy(chf.areas, areas, sizeof(unsigned char)*chf.spanCount);
  274. rcFree(areas);
  275. return true;
  276. }
  277. /// @par
  278. ///
  279. /// The value of spacial parameters are in world units.
  280. ///
  281. /// @see rcCompactHeightfield, rcMedianFilterWalkableArea
  282. void rcMarkBoxArea(rcContext* ctx, const float* bmin, const float* bmax, unsigned char areaId,
  283. rcCompactHeightfield& chf)
  284. {
  285. rcAssert(ctx);
  286. rcScopedTimer timer(ctx, RC_TIMER_MARK_BOX_AREA);
  287. int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
  288. int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
  289. int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
  290. int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
  291. int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
  292. int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
  293. if (maxx < 0) return;
  294. if (minx >= chf.width) return;
  295. if (maxz < 0) return;
  296. if (minz >= chf.height) return;
  297. if (minx < 0) minx = 0;
  298. if (maxx >= chf.width) maxx = chf.width-1;
  299. if (minz < 0) minz = 0;
  300. if (maxz >= chf.height) maxz = chf.height-1;
  301. for (int z = minz; z <= maxz; ++z)
  302. {
  303. for (int x = minx; x <= maxx; ++x)
  304. {
  305. const rcCompactCell& c = chf.cells[x+z*chf.width];
  306. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  307. {
  308. rcCompactSpan& s = chf.spans[i];
  309. if ((int)s.y >= miny && (int)s.y <= maxy)
  310. {
  311. if (chf.areas[i] != RC_NULL_AREA)
  312. chf.areas[i] = areaId;
  313. }
  314. }
  315. }
  316. }
  317. }
  318. static int pointInPoly(int nvert, const float* verts, const float* p)
  319. {
  320. int i, j, c = 0;
  321. for (i = 0, j = nvert-1; i < nvert; j = i++)
  322. {
  323. const float* vi = &verts[i*3];
  324. const float* vj = &verts[j*3];
  325. if (((vi[2] > p[2]) != (vj[2] > p[2])) &&
  326. (p[0] < (vj[0]-vi[0]) * (p[2]-vi[2]) / (vj[2]-vi[2]) + vi[0]) )
  327. c = !c;
  328. }
  329. return c;
  330. }
  331. /// @par
  332. ///
  333. /// The value of spacial parameters are in world units.
  334. ///
  335. /// The y-values of the polygon vertices are ignored. So the polygon is effectively
  336. /// projected onto the xz-plane at @p hmin, then extruded to @p hmax.
  337. ///
  338. /// @see rcCompactHeightfield, rcMedianFilterWalkableArea
  339. void rcMarkConvexPolyArea(rcContext* ctx, const float* verts, const int nverts,
  340. const float hmin, const float hmax, unsigned char areaId,
  341. rcCompactHeightfield& chf)
  342. {
  343. rcAssert(ctx);
  344. rcScopedTimer timer(ctx, RC_TIMER_MARK_CONVEXPOLY_AREA);
  345. float bmin[3], bmax[3];
  346. rcVcopy(bmin, verts);
  347. rcVcopy(bmax, verts);
  348. for (int i = 1; i < nverts; ++i)
  349. {
  350. rcVmin(bmin, &verts[i*3]);
  351. rcVmax(bmax, &verts[i*3]);
  352. }
  353. bmin[1] = hmin;
  354. bmax[1] = hmax;
  355. int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
  356. int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
  357. int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
  358. int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
  359. int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
  360. int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
  361. if (maxx < 0) return;
  362. if (minx >= chf.width) return;
  363. if (maxz < 0) return;
  364. if (minz >= chf.height) return;
  365. if (minx < 0) minx = 0;
  366. if (maxx >= chf.width) maxx = chf.width-1;
  367. if (minz < 0) minz = 0;
  368. if (maxz >= chf.height) maxz = chf.height-1;
  369. // TODO: Optimize.
  370. for (int z = minz; z <= maxz; ++z)
  371. {
  372. for (int x = minx; x <= maxx; ++x)
  373. {
  374. const rcCompactCell& c = chf.cells[x+z*chf.width];
  375. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  376. {
  377. rcCompactSpan& s = chf.spans[i];
  378. if (chf.areas[i] == RC_NULL_AREA)
  379. continue;
  380. if ((int)s.y >= miny && (int)s.y <= maxy)
  381. {
  382. float p[3];
  383. p[0] = chf.bmin[0] + (x+0.5f)*chf.cs;
  384. p[1] = 0;
  385. p[2] = chf.bmin[2] + (z+0.5f)*chf.cs;
  386. if (pointInPoly(nverts, verts, p))
  387. {
  388. chf.areas[i] = areaId;
  389. }
  390. }
  391. }
  392. }
  393. }
  394. }
  395. int rcOffsetPoly(const float* verts, const int nverts, const float offset,
  396. float* outVerts, const int maxOutVerts)
  397. {
  398. const float MITER_LIMIT = 1.20f;
  399. int n = 0;
  400. for (int i = 0; i < nverts; i++)
  401. {
  402. const int a = (i+nverts-1) % nverts;
  403. const int b = i;
  404. const int c = (i+1) % nverts;
  405. const float* va = &verts[a*3];
  406. const float* vb = &verts[b*3];
  407. const float* vc = &verts[c*3];
  408. float dx0 = vb[0] - va[0];
  409. float dy0 = vb[2] - va[2];
  410. float d0 = dx0*dx0 + dy0*dy0;
  411. if (d0 > 1e-6f)
  412. {
  413. d0 = 1.0f/rcSqrt(d0);
  414. dx0 *= d0;
  415. dy0 *= d0;
  416. }
  417. float dx1 = vc[0] - vb[0];
  418. float dy1 = vc[2] - vb[2];
  419. float d1 = dx1*dx1 + dy1*dy1;
  420. if (d1 > 1e-6f)
  421. {
  422. d1 = 1.0f/rcSqrt(d1);
  423. dx1 *= d1;
  424. dy1 *= d1;
  425. }
  426. const float dlx0 = -dy0;
  427. const float dly0 = dx0;
  428. const float dlx1 = -dy1;
  429. const float dly1 = dx1;
  430. float cross = dx1*dy0 - dx0*dy1;
  431. float dmx = (dlx0 + dlx1) * 0.5f;
  432. float dmy = (dly0 + dly1) * 0.5f;
  433. float dmr2 = dmx*dmx + dmy*dmy;
  434. bool bevel = dmr2 * MITER_LIMIT*MITER_LIMIT < 1.0f;
  435. if (dmr2 > 1e-6f)
  436. {
  437. const float scale = 1.0f / dmr2;
  438. dmx *= scale;
  439. dmy *= scale;
  440. }
  441. if (bevel && cross < 0.0f)
  442. {
  443. if (n+2 >= maxOutVerts)
  444. return 0;
  445. float d = (1.0f - (dx0*dx1 + dy0*dy1))*0.5f;
  446. outVerts[n*3+0] = vb[0] + (-dlx0+dx0*d)*offset;
  447. outVerts[n*3+1] = vb[1];
  448. outVerts[n*3+2] = vb[2] + (-dly0+dy0*d)*offset;
  449. n++;
  450. outVerts[n*3+0] = vb[0] + (-dlx1-dx1*d)*offset;
  451. outVerts[n*3+1] = vb[1];
  452. outVerts[n*3+2] = vb[2] + (-dly1-dy1*d)*offset;
  453. n++;
  454. }
  455. else
  456. {
  457. if (n+1 >= maxOutVerts)
  458. return 0;
  459. outVerts[n*3+0] = vb[0] - dmx*offset;
  460. outVerts[n*3+1] = vb[1];
  461. outVerts[n*3+2] = vb[2] - dmy*offset;
  462. n++;
  463. }
  464. }
  465. return n;
  466. }
  467. /// @par
  468. ///
  469. /// The value of spacial parameters are in world units.
  470. ///
  471. /// @see rcCompactHeightfield, rcMedianFilterWalkableArea
  472. void rcMarkCylinderArea(rcContext* ctx, const float* pos,
  473. const float r, const float h, unsigned char areaId,
  474. rcCompactHeightfield& chf)
  475. {
  476. rcAssert(ctx);
  477. rcScopedTimer timer(ctx, RC_TIMER_MARK_CYLINDER_AREA);
  478. float bmin[3], bmax[3];
  479. bmin[0] = pos[0] - r;
  480. bmin[1] = pos[1];
  481. bmin[2] = pos[2] - r;
  482. bmax[0] = pos[0] + r;
  483. bmax[1] = pos[1] + h;
  484. bmax[2] = pos[2] + r;
  485. const float r2 = r*r;
  486. int minx = (int)((bmin[0]-chf.bmin[0])/chf.cs);
  487. int miny = (int)((bmin[1]-chf.bmin[1])/chf.ch);
  488. int minz = (int)((bmin[2]-chf.bmin[2])/chf.cs);
  489. int maxx = (int)((bmax[0]-chf.bmin[0])/chf.cs);
  490. int maxy = (int)((bmax[1]-chf.bmin[1])/chf.ch);
  491. int maxz = (int)((bmax[2]-chf.bmin[2])/chf.cs);
  492. if (maxx < 0) return;
  493. if (minx >= chf.width) return;
  494. if (maxz < 0) return;
  495. if (minz >= chf.height) return;
  496. if (minx < 0) minx = 0;
  497. if (maxx >= chf.width) maxx = chf.width-1;
  498. if (minz < 0) minz = 0;
  499. if (maxz >= chf.height) maxz = chf.height-1;
  500. for (int z = minz; z <= maxz; ++z)
  501. {
  502. for (int x = minx; x <= maxx; ++x)
  503. {
  504. const rcCompactCell& c = chf.cells[x+z*chf.width];
  505. for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
  506. {
  507. rcCompactSpan& s = chf.spans[i];
  508. if (chf.areas[i] == RC_NULL_AREA)
  509. continue;
  510. if ((int)s.y >= miny && (int)s.y <= maxy)
  511. {
  512. const float sx = chf.bmin[0] + (x+0.5f)*chf.cs;
  513. const float sz = chf.bmin[2] + (z+0.5f)*chf.cs;
  514. const float dx = sx - pos[0];
  515. const float dz = sz - pos[2];
  516. if (dx*dx + dz*dz < r2)
  517. {
  518. chf.areas[i] = areaId;
  519. }
  520. }
  521. }
  522. }
  523. }
  524. }