RecastRegion.cs 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939
  1. /*
  2. Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
  3. recast4j copyright (c) 2015-2019 Piotr Piastucki piotr@jtilia.org
  4. DotRecast Copyright (c) 2023 Choi Ikpil ikpil@naver.com
  5. This software is provided 'as-is', without any express or implied
  6. warranty. In no event will the authors be held liable for any damages
  7. arising from the use of this software.
  8. Permission is granted to anyone to use this software for any purpose,
  9. including commercial applications, and to alter it and redistribute it
  10. freely, subject to the following restrictions:
  11. 1. The origin of this software must not be misrepresented; you must not
  12. claim that you wrote the original software. If you use this software
  13. in a product, an acknowledgment in the product documentation would be
  14. appreciated but is not required.
  15. 2. Altered source versions must be plainly marked as such, and must not be
  16. misrepresented as being the original software.
  17. 3. This notice may not be removed or altered from any source distribution.
  18. */
  19. using System;
  20. using System.Collections.Generic;
  21. using System.Linq;
  22. using DotRecast.Core;
  23. namespace DotRecast.Recast
  24. {
  25. using static RcConstants;
  26. public static class RecastRegion
  27. {
  28. const int RC_NULL_NEI = 0xffff;
  29. public static int CalculateDistanceField(RcCompactHeightfield chf, int[] src)
  30. {
  31. int maxDist;
  32. int w = chf.width;
  33. int h = chf.height;
  34. // Init distance and points.
  35. for (int i = 0; i < chf.spanCount; ++i)
  36. {
  37. src[i] = 0xffff;
  38. }
  39. // Mark boundary cells.
  40. for (int y = 0; y < h; ++y)
  41. {
  42. for (int x = 0; x < w; ++x)
  43. {
  44. RcCompactCell c = chf.cells[x + y * w];
  45. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  46. {
  47. RcCompactSpan s = chf.spans[i];
  48. int area = chf.areas[i];
  49. int nc = 0;
  50. for (int dir = 0; dir < 4; ++dir)
  51. {
  52. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  53. {
  54. int ax = x + RecastCommon.GetDirOffsetX(dir);
  55. int ay = y + RecastCommon.GetDirOffsetY(dir);
  56. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, dir);
  57. if (area == chf.areas[ai])
  58. {
  59. nc++;
  60. }
  61. }
  62. }
  63. if (nc != 4)
  64. {
  65. src[i] = 0;
  66. }
  67. }
  68. }
  69. }
  70. // Pass 1
  71. for (int y = 0; y < h; ++y)
  72. {
  73. for (int x = 0; x < w; ++x)
  74. {
  75. RcCompactCell c = chf.cells[x + y * w];
  76. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  77. {
  78. RcCompactSpan s = chf.spans[i];
  79. if (RecastCommon.GetCon(s, 0) != RC_NOT_CONNECTED)
  80. {
  81. // (-1,0)
  82. int ax = x + RecastCommon.GetDirOffsetX(0);
  83. int ay = y + RecastCommon.GetDirOffsetY(0);
  84. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 0);
  85. RcCompactSpan @as = chf.spans[ai];
  86. if (src[ai] + 2 < src[i])
  87. {
  88. src[i] = src[ai] + 2;
  89. }
  90. // (-1,-1)
  91. if (RecastCommon.GetCon(@as, 3) != RC_NOT_CONNECTED)
  92. {
  93. int aax = ax + RecastCommon.GetDirOffsetX(3);
  94. int aay = ay + RecastCommon.GetDirOffsetY(3);
  95. int aai = chf.cells[aax + aay * w].index + RecastCommon.GetCon(@as, 3);
  96. if (src[aai] + 3 < src[i])
  97. {
  98. src[i] = src[aai] + 3;
  99. }
  100. }
  101. }
  102. if (RecastCommon.GetCon(s, 3) != RC_NOT_CONNECTED)
  103. {
  104. // (0,-1)
  105. int ax = x + RecastCommon.GetDirOffsetX(3);
  106. int ay = y + RecastCommon.GetDirOffsetY(3);
  107. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 3);
  108. RcCompactSpan @as = chf.spans[ai];
  109. if (src[ai] + 2 < src[i])
  110. {
  111. src[i] = src[ai] + 2;
  112. }
  113. // (1,-1)
  114. if (RecastCommon.GetCon(@as, 2) != RC_NOT_CONNECTED)
  115. {
  116. int aax = ax + RecastCommon.GetDirOffsetX(2);
  117. int aay = ay + RecastCommon.GetDirOffsetY(2);
  118. int aai = chf.cells[aax + aay * w].index + RecastCommon.GetCon(@as, 2);
  119. if (src[aai] + 3 < src[i])
  120. {
  121. src[i] = src[aai] + 3;
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. // Pass 2
  129. for (int y = h - 1; y >= 0; --y)
  130. {
  131. for (int x = w - 1; x >= 0; --x)
  132. {
  133. RcCompactCell c = chf.cells[x + y * w];
  134. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  135. {
  136. RcCompactSpan s = chf.spans[i];
  137. if (RecastCommon.GetCon(s, 2) != RC_NOT_CONNECTED)
  138. {
  139. // (1,0)
  140. int ax = x + RecastCommon.GetDirOffsetX(2);
  141. int ay = y + RecastCommon.GetDirOffsetY(2);
  142. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 2);
  143. RcCompactSpan @as = chf.spans[ai];
  144. if (src[ai] + 2 < src[i])
  145. {
  146. src[i] = src[ai] + 2;
  147. }
  148. // (1,1)
  149. if (RecastCommon.GetCon(@as, 1) != RC_NOT_CONNECTED)
  150. {
  151. int aax = ax + RecastCommon.GetDirOffsetX(1);
  152. int aay = ay + RecastCommon.GetDirOffsetY(1);
  153. int aai = chf.cells[aax + aay * w].index + RecastCommon.GetCon(@as, 1);
  154. if (src[aai] + 3 < src[i])
  155. {
  156. src[i] = src[aai] + 3;
  157. }
  158. }
  159. }
  160. if (RecastCommon.GetCon(s, 1) != RC_NOT_CONNECTED)
  161. {
  162. // (0,1)
  163. int ax = x + RecastCommon.GetDirOffsetX(1);
  164. int ay = y + RecastCommon.GetDirOffsetY(1);
  165. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 1);
  166. RcCompactSpan @as = chf.spans[ai];
  167. if (src[ai] + 2 < src[i])
  168. {
  169. src[i] = src[ai] + 2;
  170. }
  171. // (-1,1)
  172. if (RecastCommon.GetCon(@as, 0) != RC_NOT_CONNECTED)
  173. {
  174. int aax = ax + RecastCommon.GetDirOffsetX(0);
  175. int aay = ay + RecastCommon.GetDirOffsetY(0);
  176. int aai = chf.cells[aax + aay * w].index + RecastCommon.GetCon(@as, 0);
  177. if (src[aai] + 3 < src[i])
  178. {
  179. src[i] = src[aai] + 3;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. maxDist = 0;
  187. for (int i = 0; i < chf.spanCount; ++i)
  188. {
  189. maxDist = Math.Max(src[i], maxDist);
  190. }
  191. return maxDist;
  192. }
  193. private static int[] BoxBlur(RcCompactHeightfield chf, int thr, int[] src)
  194. {
  195. int w = chf.width;
  196. int h = chf.height;
  197. int[] dst = new int[chf.spanCount];
  198. thr *= 2;
  199. for (int y = 0; y < h; ++y)
  200. {
  201. for (int x = 0; x < w; ++x)
  202. {
  203. RcCompactCell c = chf.cells[x + y * w];
  204. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  205. {
  206. RcCompactSpan s = chf.spans[i];
  207. int cd = src[i];
  208. if (cd <= thr)
  209. {
  210. dst[i] = cd;
  211. continue;
  212. }
  213. int d = cd;
  214. for (int dir = 0; dir < 4; ++dir)
  215. {
  216. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  217. {
  218. int ax = x + RecastCommon.GetDirOffsetX(dir);
  219. int ay = y + RecastCommon.GetDirOffsetY(dir);
  220. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, dir);
  221. d += src[ai];
  222. RcCompactSpan @as = chf.spans[ai];
  223. int dir2 = (dir + 1) & 0x3;
  224. if (RecastCommon.GetCon(@as, dir2) != RC_NOT_CONNECTED)
  225. {
  226. int ax2 = ax + RecastCommon.GetDirOffsetX(dir2);
  227. int ay2 = ay + RecastCommon.GetDirOffsetY(dir2);
  228. int ai2 = chf.cells[ax2 + ay2 * w].index + RecastCommon.GetCon(@as, dir2);
  229. d += src[ai2];
  230. }
  231. else
  232. {
  233. d += cd;
  234. }
  235. }
  236. else
  237. {
  238. d += cd * 2;
  239. }
  240. }
  241. dst[i] = ((d + 5) / 9);
  242. }
  243. }
  244. }
  245. return dst;
  246. }
  247. private static bool FloodRegion(int x, int y, int i, int level, int r, RcCompactHeightfield chf, int[] srcReg,
  248. int[] srcDist, List<int> stack)
  249. {
  250. int w = chf.width;
  251. int area = chf.areas[i];
  252. // Flood fill mark region.
  253. stack.Clear();
  254. stack.Add(x);
  255. stack.Add(y);
  256. stack.Add(i);
  257. srcReg[i] = r;
  258. srcDist[i] = 0;
  259. int lev = level >= 2 ? level - 2 : 0;
  260. int count = 0;
  261. while (stack.Count > 0)
  262. {
  263. int ci = stack[^1];
  264. stack.RemoveAt(stack.Count - 1);
  265. int cy = stack[^1];
  266. stack.RemoveAt(stack.Count - 1);
  267. int cx = stack[^1];
  268. stack.RemoveAt(stack.Count - 1);
  269. RcCompactSpan cs = chf.spans[ci];
  270. // Check if any of the neighbours already have a valid region set.
  271. int ar = 0;
  272. for (int dir = 0; dir < 4; ++dir)
  273. {
  274. // 8 connected
  275. if (RecastCommon.GetCon(cs, dir) != RC_NOT_CONNECTED)
  276. {
  277. int ax = cx + RecastCommon.GetDirOffsetX(dir);
  278. int ay = cy + RecastCommon.GetDirOffsetY(dir);
  279. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(cs, dir);
  280. if (chf.areas[ai] != area)
  281. {
  282. continue;
  283. }
  284. int nr = srcReg[ai];
  285. if ((nr & RC_BORDER_REG) != 0)
  286. {
  287. continue;
  288. }
  289. if (nr != 0 && nr != r)
  290. {
  291. ar = nr;
  292. break;
  293. }
  294. RcCompactSpan @as = chf.spans[ai];
  295. int dir2 = (dir + 1) & 0x3;
  296. if (RecastCommon.GetCon(@as, dir2) != RC_NOT_CONNECTED)
  297. {
  298. int ax2 = ax + RecastCommon.GetDirOffsetX(dir2);
  299. int ay2 = ay + RecastCommon.GetDirOffsetY(dir2);
  300. int ai2 = chf.cells[ax2 + ay2 * w].index + RecastCommon.GetCon(@as, dir2);
  301. if (chf.areas[ai2] != area)
  302. {
  303. continue;
  304. }
  305. int nr2 = srcReg[ai2];
  306. if (nr2 != 0 && nr2 != r)
  307. {
  308. ar = nr2;
  309. break;
  310. }
  311. }
  312. }
  313. }
  314. if (ar != 0)
  315. {
  316. srcReg[ci] = 0;
  317. continue;
  318. }
  319. count++;
  320. // Expand neighbours.
  321. for (int dir = 0; dir < 4; ++dir)
  322. {
  323. if (RecastCommon.GetCon(cs, dir) != RC_NOT_CONNECTED)
  324. {
  325. int ax = cx + RecastCommon.GetDirOffsetX(dir);
  326. int ay = cy + RecastCommon.GetDirOffsetY(dir);
  327. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(cs, dir);
  328. if (chf.areas[ai] != area)
  329. {
  330. continue;
  331. }
  332. if (chf.dist[ai] >= lev && srcReg[ai] == 0)
  333. {
  334. srcReg[ai] = r;
  335. srcDist[ai] = 0;
  336. stack.Add(ax);
  337. stack.Add(ay);
  338. stack.Add(ai);
  339. }
  340. }
  341. }
  342. }
  343. return count > 0;
  344. }
  345. private static int[] ExpandRegions(int maxIter, int level, RcCompactHeightfield chf, int[] srcReg, int[] srcDist,
  346. List<int> stack, bool fillStack)
  347. {
  348. int w = chf.width;
  349. int h = chf.height;
  350. if (fillStack)
  351. {
  352. // Find cells revealed by the raised level.
  353. stack.Clear();
  354. for (int y = 0; y < h; ++y)
  355. {
  356. for (int x = 0; x < w; ++x)
  357. {
  358. RcCompactCell c = chf.cells[x + y * w];
  359. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  360. {
  361. if (chf.dist[i] >= level && srcReg[i] == 0 && chf.areas[i] != RC_NULL_AREA)
  362. {
  363. stack.Add(x);
  364. stack.Add(y);
  365. stack.Add(i);
  366. }
  367. }
  368. }
  369. }
  370. }
  371. else // use cells in the input stack
  372. {
  373. // mark all cells which already have a region
  374. for (int j = 0; j < stack.Count; j += 3)
  375. {
  376. int i = stack[j + 2];
  377. if (srcReg[i] != 0)
  378. {
  379. stack[j + 2] = -1;
  380. }
  381. }
  382. }
  383. List<int> dirtyEntries = new List<int>();
  384. int iter = 0;
  385. while (stack.Count > 0)
  386. {
  387. int failed = 0;
  388. dirtyEntries.Clear();
  389. for (int j = 0; j < stack.Count; j += 3)
  390. {
  391. int x = stack[j + 0];
  392. int y = stack[j + 1];
  393. int i = stack[j + 2];
  394. if (i < 0)
  395. {
  396. failed++;
  397. continue;
  398. }
  399. int r = srcReg[i];
  400. int d2 = 0xffff;
  401. int area = chf.areas[i];
  402. RcCompactSpan s = chf.spans[i];
  403. for (int dir = 0; dir < 4; ++dir)
  404. {
  405. if (RecastCommon.GetCon(s, dir) == RC_NOT_CONNECTED)
  406. {
  407. continue;
  408. }
  409. int ax = x + RecastCommon.GetDirOffsetX(dir);
  410. int ay = y + RecastCommon.GetDirOffsetY(dir);
  411. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, dir);
  412. if (chf.areas[ai] != area)
  413. {
  414. continue;
  415. }
  416. if (srcReg[ai] > 0 && (srcReg[ai] & RC_BORDER_REG) == 0)
  417. {
  418. if (srcDist[ai] + 2 < d2)
  419. {
  420. r = srcReg[ai];
  421. d2 = srcDist[ai] + 2;
  422. }
  423. }
  424. }
  425. if (r != 0)
  426. {
  427. stack[j + 2] = -1; // mark as used
  428. dirtyEntries.Add(i);
  429. dirtyEntries.Add(r);
  430. dirtyEntries.Add(d2);
  431. }
  432. else
  433. {
  434. failed++;
  435. }
  436. }
  437. // Copy entries that differ between src and dst to keep them in sync.
  438. for (int i = 0; i < dirtyEntries.Count; i += 3)
  439. {
  440. int idx = dirtyEntries[i];
  441. srcReg[idx] = dirtyEntries[i + 1];
  442. srcDist[idx] = dirtyEntries[i + 2];
  443. }
  444. if (failed * 3 == stack.Count())
  445. {
  446. break;
  447. }
  448. if (level > 0)
  449. {
  450. ++iter;
  451. if (iter >= maxIter)
  452. {
  453. break;
  454. }
  455. }
  456. }
  457. return srcReg;
  458. }
  459. private static void SortCellsByLevel(int startLevel, RcCompactHeightfield chf, int[] srcReg, int nbStacks,
  460. List<List<int>> stacks, int loglevelsPerStack) // the levels per stack (2 in our case) as a bit shift
  461. {
  462. int w = chf.width;
  463. int h = chf.height;
  464. startLevel = startLevel >> loglevelsPerStack;
  465. for (int j = 0; j < nbStacks; ++j)
  466. {
  467. stacks[j].Clear();
  468. }
  469. // put all cells in the level range into the appropriate stacks
  470. for (int y = 0; y < h; ++y)
  471. {
  472. for (int x = 0; x < w; ++x)
  473. {
  474. RcCompactCell c = chf.cells[x + y * w];
  475. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  476. {
  477. if (chf.areas[i] == RC_NULL_AREA || srcReg[i] != 0)
  478. {
  479. continue;
  480. }
  481. int level = chf.dist[i] >> loglevelsPerStack;
  482. int sId = startLevel - level;
  483. if (sId >= nbStacks)
  484. {
  485. continue;
  486. }
  487. if (sId < 0)
  488. {
  489. sId = 0;
  490. }
  491. stacks[sId].Add(x);
  492. stacks[sId].Add(y);
  493. stacks[sId].Add(i);
  494. }
  495. }
  496. }
  497. }
  498. private static void AppendStacks(List<int> srcStack, List<int> dstStack, int[] srcReg)
  499. {
  500. for (int j = 0; j < srcStack.Count; j += 3)
  501. {
  502. int i = srcStack[j + 2];
  503. if ((i < 0) || (srcReg[i] != 0))
  504. {
  505. continue;
  506. }
  507. dstStack.Add(srcStack[j]);
  508. dstStack.Add(srcStack[j + 1]);
  509. dstStack.Add(srcStack[j + 2]);
  510. }
  511. }
  512. private static void RemoveAdjacentNeighbours(RcRegion reg)
  513. {
  514. // Remove adjacent duplicates.
  515. for (int i = 0; i < reg.connections.Count && reg.connections.Count > 1;)
  516. {
  517. int ni = (i + 1) % reg.connections.Count;
  518. if (reg.connections[i] == reg.connections[ni])
  519. {
  520. reg.connections.RemoveAt(i);
  521. }
  522. else
  523. {
  524. ++i;
  525. }
  526. }
  527. }
  528. private static void ReplaceNeighbour(RcRegion reg, int oldId, int newId)
  529. {
  530. bool neiChanged = false;
  531. for (int i = 0; i < reg.connections.Count; ++i)
  532. {
  533. if (reg.connections[i] == oldId)
  534. {
  535. reg.connections[i] = newId;
  536. neiChanged = true;
  537. }
  538. }
  539. for (int i = 0; i < reg.floors.Count; ++i)
  540. {
  541. if (reg.floors[i] == oldId)
  542. {
  543. reg.floors[i] = newId;
  544. }
  545. }
  546. if (neiChanged)
  547. {
  548. RemoveAdjacentNeighbours(reg);
  549. }
  550. }
  551. private static bool CanMergeWithRegion(RcRegion rega, RcRegion regb)
  552. {
  553. if (rega.areaType != regb.areaType)
  554. {
  555. return false;
  556. }
  557. int n = 0;
  558. for (int i = 0; i < rega.connections.Count; ++i)
  559. {
  560. if (rega.connections[i] == regb.id)
  561. {
  562. n++;
  563. }
  564. }
  565. if (n > 1)
  566. {
  567. return false;
  568. }
  569. for (int i = 0; i < rega.floors.Count; ++i)
  570. {
  571. if (rega.floors[i] == regb.id)
  572. {
  573. return false;
  574. }
  575. }
  576. return true;
  577. }
  578. private static void AddUniqueFloorRegion(RcRegion reg, int n)
  579. {
  580. if (!reg.floors.Contains(n))
  581. {
  582. reg.floors.Add(n);
  583. }
  584. }
  585. private static bool MergeRegions(RcRegion rega, RcRegion regb)
  586. {
  587. int aid = rega.id;
  588. int bid = regb.id;
  589. // Duplicate current neighbourhood.
  590. List<int> acon = new List<int>(rega.connections);
  591. List<int> bcon = regb.connections;
  592. // Find insertion point on A.
  593. int insa = -1;
  594. for (int i = 0; i < acon.Count; ++i)
  595. {
  596. if (acon[i] == bid)
  597. {
  598. insa = i;
  599. break;
  600. }
  601. }
  602. if (insa == -1)
  603. {
  604. return false;
  605. }
  606. // Find insertion point on B.
  607. int insb = -1;
  608. for (int i = 0; i < bcon.Count; ++i)
  609. {
  610. if (bcon[i] == aid)
  611. {
  612. insb = i;
  613. break;
  614. }
  615. }
  616. if (insb == -1)
  617. {
  618. return false;
  619. }
  620. // Merge neighbours.
  621. rega.connections.Clear();
  622. for (int i = 0, ni = acon.Count; i < ni - 1; ++i)
  623. {
  624. rega.connections.Add(acon[(insa + 1 + i) % ni]);
  625. }
  626. for (int i = 0, ni = bcon.Count; i < ni - 1; ++i)
  627. {
  628. rega.connections.Add(bcon[(insb + 1 + i) % ni]);
  629. }
  630. RemoveAdjacentNeighbours(rega);
  631. for (int j = 0; j < regb.floors.Count; ++j)
  632. {
  633. AddUniqueFloorRegion(rega, regb.floors[j]);
  634. }
  635. rega.spanCount += regb.spanCount;
  636. regb.spanCount = 0;
  637. regb.connections.Clear();
  638. return true;
  639. }
  640. private static bool IsRegionConnectedToBorder(RcRegion reg)
  641. {
  642. // Region is connected to border if
  643. // one of the neighbours is null id.
  644. return reg.connections.Contains(0);
  645. }
  646. private static bool IsSolidEdge(RcCompactHeightfield chf, int[] srcReg, int x, int y, int i, int dir)
  647. {
  648. RcCompactSpan s = chf.spans[i];
  649. int r = 0;
  650. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  651. {
  652. int ax = x + RecastCommon.GetDirOffsetX(dir);
  653. int ay = y + RecastCommon.GetDirOffsetY(dir);
  654. int ai = chf.cells[ax + ay * chf.width].index + RecastCommon.GetCon(s, dir);
  655. r = srcReg[ai];
  656. }
  657. if (r == srcReg[i])
  658. {
  659. return false;
  660. }
  661. return true;
  662. }
  663. private static void WalkContour(int x, int y, int i, int dir, RcCompactHeightfield chf, int[] srcReg,
  664. List<int> cont)
  665. {
  666. int startDir = dir;
  667. int starti = i;
  668. RcCompactSpan ss = chf.spans[i];
  669. int curReg = 0;
  670. if (RecastCommon.GetCon(ss, dir) != RC_NOT_CONNECTED)
  671. {
  672. int ax = x + RecastCommon.GetDirOffsetX(dir);
  673. int ay = y + RecastCommon.GetDirOffsetY(dir);
  674. int ai = chf.cells[ax + ay * chf.width].index + RecastCommon.GetCon(ss, dir);
  675. curReg = srcReg[ai];
  676. }
  677. cont.Add(curReg);
  678. int iter = 0;
  679. while (++iter < 40000)
  680. {
  681. RcCompactSpan s = chf.spans[i];
  682. if (IsSolidEdge(chf, srcReg, x, y, i, dir))
  683. {
  684. // Choose the edge corner
  685. int r = 0;
  686. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  687. {
  688. int ax = x + RecastCommon.GetDirOffsetX(dir);
  689. int ay = y + RecastCommon.GetDirOffsetY(dir);
  690. int ai = chf.cells[ax + ay * chf.width].index + RecastCommon.GetCon(s, dir);
  691. r = srcReg[ai];
  692. }
  693. if (r != curReg)
  694. {
  695. curReg = r;
  696. cont.Add(curReg);
  697. }
  698. dir = (dir + 1) & 0x3; // Rotate CW
  699. }
  700. else
  701. {
  702. int ni = -1;
  703. int nx = x + RecastCommon.GetDirOffsetX(dir);
  704. int ny = y + RecastCommon.GetDirOffsetY(dir);
  705. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  706. {
  707. RcCompactCell nc = chf.cells[nx + ny * chf.width];
  708. ni = nc.index + RecastCommon.GetCon(s, dir);
  709. }
  710. if (ni == -1)
  711. {
  712. // Should not happen.
  713. return;
  714. }
  715. x = nx;
  716. y = ny;
  717. i = ni;
  718. dir = (dir + 3) & 0x3; // Rotate CCW
  719. }
  720. if (starti == i && startDir == dir)
  721. {
  722. break;
  723. }
  724. }
  725. // Remove adjacent duplicates.
  726. if (cont.Count > 1)
  727. {
  728. for (int j = 0; j < cont.Count;)
  729. {
  730. int nj = (j + 1) % cont.Count;
  731. if (cont[j] == cont[nj])
  732. {
  733. cont.RemoveAt(j);
  734. }
  735. else
  736. {
  737. ++j;
  738. }
  739. }
  740. }
  741. }
  742. private static int MergeAndFilterRegions(RcTelemetry ctx, int minRegionArea, int mergeRegionSize, int maxRegionId,
  743. RcCompactHeightfield chf, int[] srcReg, List<int> overlaps)
  744. {
  745. int w = chf.width;
  746. int h = chf.height;
  747. int nreg = maxRegionId + 1;
  748. RcRegion[] regions = new RcRegion[nreg];
  749. // Construct regions
  750. for (int i = 0; i < nreg; ++i)
  751. {
  752. regions[i] = new RcRegion(i);
  753. }
  754. // Find edge of a region and find connections around the contour.
  755. for (int y = 0; y < h; ++y)
  756. {
  757. for (int x = 0; x < w; ++x)
  758. {
  759. RcCompactCell c = chf.cells[x + y * w];
  760. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  761. {
  762. int r = srcReg[i];
  763. if (r == 0 || r >= nreg)
  764. {
  765. continue;
  766. }
  767. RcRegion reg = regions[r];
  768. reg.spanCount++;
  769. // Update floors.
  770. for (int j = c.index; j < ni; ++j)
  771. {
  772. if (i == j)
  773. {
  774. continue;
  775. }
  776. int floorId = srcReg[j];
  777. if (floorId == 0 || floorId >= nreg)
  778. {
  779. continue;
  780. }
  781. if (floorId == r)
  782. {
  783. reg.overlap = true;
  784. }
  785. AddUniqueFloorRegion(reg, floorId);
  786. }
  787. // Have found contour
  788. if (reg.connections.Count > 0)
  789. {
  790. continue;
  791. }
  792. reg.areaType = chf.areas[i];
  793. // Check if this cell is next to a border.
  794. int ndir = -1;
  795. for (int dir = 0; dir < 4; ++dir)
  796. {
  797. if (IsSolidEdge(chf, srcReg, x, y, i, dir))
  798. {
  799. ndir = dir;
  800. break;
  801. }
  802. }
  803. if (ndir != -1)
  804. {
  805. // The cell is at border.
  806. // Walk around the contour to find all the neighbours.
  807. WalkContour(x, y, i, ndir, chf, srcReg, reg.connections);
  808. }
  809. }
  810. }
  811. }
  812. // Remove too small regions.
  813. List<int> stack = new List<int>(32);
  814. List<int> trace = new List<int>(32);
  815. for (int i = 0; i < nreg; ++i)
  816. {
  817. RcRegion reg = regions[i];
  818. if (reg.id == 0 || (reg.id & RC_BORDER_REG) != 0)
  819. {
  820. continue;
  821. }
  822. if (reg.spanCount == 0)
  823. {
  824. continue;
  825. }
  826. if (reg.visited)
  827. {
  828. continue;
  829. }
  830. // Count the total size of all the connected regions.
  831. // Also keep track of the regions connects to a tile border.
  832. bool connectsToBorder = false;
  833. int spanCount = 0;
  834. stack.Clear();
  835. trace.Clear();
  836. reg.visited = true;
  837. stack.Add(i);
  838. while (stack.Count > 0)
  839. {
  840. // Pop
  841. int ri = stack[^1];
  842. stack.RemoveAt(stack.Count - 1);
  843. RcRegion creg = regions[ri];
  844. spanCount += creg.spanCount;
  845. trace.Add(ri);
  846. for (int j = 0; j < creg.connections.Count; ++j)
  847. {
  848. if ((creg.connections[j] & RC_BORDER_REG) != 0)
  849. {
  850. connectsToBorder = true;
  851. continue;
  852. }
  853. RcRegion neireg = regions[creg.connections[j]];
  854. if (neireg.visited)
  855. {
  856. continue;
  857. }
  858. if (neireg.id == 0 || (neireg.id & RC_BORDER_REG) != 0)
  859. {
  860. continue;
  861. }
  862. // Visit
  863. stack.Add(neireg.id);
  864. neireg.visited = true;
  865. }
  866. }
  867. // If the accumulated regions size is too small, remove it.
  868. // Do not remove areas which connect to tile borders
  869. // as their size cannot be estimated correctly and removing them
  870. // can potentially remove necessary areas.
  871. if (spanCount < minRegionArea && !connectsToBorder)
  872. {
  873. // Kill all visited regions.
  874. for (int j = 0; j < trace.Count; ++j)
  875. {
  876. regions[trace[j]].spanCount = 0;
  877. regions[trace[j]].id = 0;
  878. }
  879. }
  880. }
  881. // Merge too small regions to neighbour regions.
  882. int mergeCount = 0;
  883. do
  884. {
  885. mergeCount = 0;
  886. for (int i = 0; i < nreg; ++i)
  887. {
  888. RcRegion reg = regions[i];
  889. if (reg.id == 0 || (reg.id & RC_BORDER_REG) != 0)
  890. {
  891. continue;
  892. }
  893. if (reg.overlap)
  894. {
  895. continue;
  896. }
  897. if (reg.spanCount == 0)
  898. {
  899. continue;
  900. }
  901. // Check to see if the region should be merged.
  902. if (reg.spanCount > mergeRegionSize && IsRegionConnectedToBorder(reg))
  903. {
  904. continue;
  905. }
  906. // Small region with more than 1 connection.
  907. // Or region which is not connected to a border at all.
  908. // Find smallest neighbour region that connects to this one.
  909. int smallest = 0xfffffff;
  910. int mergeId = reg.id;
  911. for (int j = 0; j < reg.connections.Count; ++j)
  912. {
  913. if ((reg.connections[j] & RC_BORDER_REG) != 0)
  914. {
  915. continue;
  916. }
  917. RcRegion mreg = regions[reg.connections[j]];
  918. if (mreg.id == 0 || (mreg.id & RC_BORDER_REG) != 0 || mreg.overlap)
  919. {
  920. continue;
  921. }
  922. if (mreg.spanCount < smallest && CanMergeWithRegion(reg, mreg) && CanMergeWithRegion(mreg, reg))
  923. {
  924. smallest = mreg.spanCount;
  925. mergeId = mreg.id;
  926. }
  927. }
  928. // Found new id.
  929. if (mergeId != reg.id)
  930. {
  931. int oldId = reg.id;
  932. RcRegion target = regions[mergeId];
  933. // Merge neighbours.
  934. if (MergeRegions(target, reg))
  935. {
  936. // Fixup regions pointing to current region.
  937. for (int j = 0; j < nreg; ++j)
  938. {
  939. if (regions[j].id == 0 || (regions[j].id & RC_BORDER_REG) != 0)
  940. {
  941. continue;
  942. }
  943. // If another region was already merged into current region
  944. // change the nid of the previous region too.
  945. if (regions[j].id == oldId)
  946. {
  947. regions[j].id = mergeId;
  948. }
  949. // Replace the current region with the new one if the
  950. // current regions is neighbour.
  951. ReplaceNeighbour(regions[j], oldId, mergeId);
  952. }
  953. mergeCount++;
  954. }
  955. }
  956. }
  957. } while (mergeCount > 0);
  958. // Compress region Ids.
  959. for (int i = 0; i < nreg; ++i)
  960. {
  961. regions[i].remap = false;
  962. if (regions[i].id == 0)
  963. {
  964. continue; // Skip nil regions.
  965. }
  966. if ((regions[i].id & RC_BORDER_REG) != 0)
  967. {
  968. continue; // Skip external regions.
  969. }
  970. regions[i].remap = true;
  971. }
  972. int regIdGen = 0;
  973. for (int i = 0; i < nreg; ++i)
  974. {
  975. if (!regions[i].remap)
  976. {
  977. continue;
  978. }
  979. int oldId = regions[i].id;
  980. int newId = ++regIdGen;
  981. for (int j = i; j < nreg; ++j)
  982. {
  983. if (regions[j].id == oldId)
  984. {
  985. regions[j].id = newId;
  986. regions[j].remap = false;
  987. }
  988. }
  989. }
  990. maxRegionId = regIdGen;
  991. // Remap regions.
  992. for (int i = 0; i < chf.spanCount; ++i)
  993. {
  994. if ((srcReg[i] & RC_BORDER_REG) == 0)
  995. {
  996. srcReg[i] = regions[srcReg[i]].id;
  997. }
  998. }
  999. // Return regions that we found to be overlapping.
  1000. for (int i = 0; i < nreg; ++i)
  1001. {
  1002. if (regions[i].overlap)
  1003. {
  1004. overlaps.Add(regions[i].id);
  1005. }
  1006. }
  1007. return maxRegionId;
  1008. }
  1009. private static void AddUniqueConnection(RcRegion reg, int n)
  1010. {
  1011. if (!reg.connections.Contains(n))
  1012. {
  1013. reg.connections.Add(n);
  1014. }
  1015. }
  1016. private static int MergeAndFilterLayerRegions(RcTelemetry ctx, int minRegionArea, int maxRegionId,
  1017. RcCompactHeightfield chf, int[] srcReg, List<int> overlaps)
  1018. {
  1019. int w = chf.width;
  1020. int h = chf.height;
  1021. int nreg = maxRegionId + 1;
  1022. RcRegion[] regions = new RcRegion[nreg];
  1023. // Construct regions
  1024. for (int i = 0; i < nreg; ++i)
  1025. {
  1026. regions[i] = new RcRegion(i);
  1027. }
  1028. // Find region neighbours and overlapping regions.
  1029. List<int> lregs = new List<int>(32);
  1030. for (int y = 0; y < h; ++y)
  1031. {
  1032. for (int x = 0; x < w; ++x)
  1033. {
  1034. RcCompactCell c = chf.cells[x + y * w];
  1035. lregs.Clear();
  1036. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1037. {
  1038. RcCompactSpan s = chf.spans[i];
  1039. int ri = srcReg[i];
  1040. if (ri == 0 || ri >= nreg)
  1041. {
  1042. continue;
  1043. }
  1044. RcRegion reg = regions[ri];
  1045. reg.spanCount++;
  1046. reg.areaType = chf.areas[i];
  1047. reg.ymin = Math.Min(reg.ymin, s.y);
  1048. reg.ymax = Math.Max(reg.ymax, s.y);
  1049. // Collect all region layers.
  1050. lregs.Add(ri);
  1051. // Update neighbours
  1052. for (int dir = 0; dir < 4; ++dir)
  1053. {
  1054. if (RecastCommon.GetCon(s, dir) != RC_NOT_CONNECTED)
  1055. {
  1056. int ax = x + RecastCommon.GetDirOffsetX(dir);
  1057. int ay = y + RecastCommon.GetDirOffsetY(dir);
  1058. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, dir);
  1059. int rai = srcReg[ai];
  1060. if (rai > 0 && rai < nreg && rai != ri)
  1061. {
  1062. AddUniqueConnection(reg, rai);
  1063. }
  1064. if ((rai & RC_BORDER_REG) != 0)
  1065. {
  1066. reg.connectsToBorder = true;
  1067. }
  1068. }
  1069. }
  1070. }
  1071. // Update overlapping regions.
  1072. for (int i = 0; i < lregs.Count - 1; ++i)
  1073. {
  1074. for (int j = i + 1; j < lregs.Count; ++j)
  1075. {
  1076. if (lregs[i] != lregs[j])
  1077. {
  1078. RcRegion ri = regions[lregs[i]];
  1079. RcRegion rj = regions[lregs[j]];
  1080. AddUniqueFloorRegion(ri, lregs[j]);
  1081. AddUniqueFloorRegion(rj, lregs[i]);
  1082. }
  1083. }
  1084. }
  1085. }
  1086. }
  1087. // Create 2D layers from regions.
  1088. int layerId = 1;
  1089. for (int i = 0; i < nreg; ++i)
  1090. {
  1091. regions[i].id = 0;
  1092. }
  1093. // Merge montone regions to create non-overlapping areas.
  1094. List<int> stack = new List<int>(32);
  1095. for (int i = 1; i < nreg; ++i)
  1096. {
  1097. RcRegion root = regions[i];
  1098. // Skip already visited.
  1099. if (root.id != 0)
  1100. {
  1101. continue;
  1102. }
  1103. // Start search.
  1104. root.id = layerId;
  1105. stack.Clear();
  1106. stack.Add(i);
  1107. while (stack.Count > 0)
  1108. {
  1109. // Pop front
  1110. var idx = stack[0];
  1111. stack.RemoveAt(0);
  1112. RcRegion reg = regions[idx];
  1113. int ncons = reg.connections.Count;
  1114. for (int j = 0; j < ncons; ++j)
  1115. {
  1116. int nei = reg.connections[j];
  1117. RcRegion regn = regions[nei];
  1118. // Skip already visited.
  1119. if (regn.id != 0)
  1120. {
  1121. continue;
  1122. }
  1123. // Skip if different area type, do not connect regions with different area type.
  1124. if (reg.areaType != regn.areaType)
  1125. {
  1126. continue;
  1127. }
  1128. // Skip if the neighbour is overlapping root region.
  1129. bool overlap = false;
  1130. for (int k = 0; k < root.floors.Count; k++)
  1131. {
  1132. if (root.floors[k] == nei)
  1133. {
  1134. overlap = true;
  1135. break;
  1136. }
  1137. }
  1138. if (overlap)
  1139. {
  1140. continue;
  1141. }
  1142. // Deepen
  1143. stack.Add(nei);
  1144. // Mark layer id
  1145. regn.id = layerId;
  1146. // Merge current layers to root.
  1147. for (int k = 0; k < regn.floors.Count; ++k)
  1148. {
  1149. AddUniqueFloorRegion(root, regn.floors[k]);
  1150. }
  1151. root.ymin = Math.Min(root.ymin, regn.ymin);
  1152. root.ymax = Math.Max(root.ymax, regn.ymax);
  1153. root.spanCount += regn.spanCount;
  1154. regn.spanCount = 0;
  1155. root.connectsToBorder = root.connectsToBorder || regn.connectsToBorder;
  1156. }
  1157. }
  1158. layerId++;
  1159. }
  1160. // Remove small regions
  1161. for (int i = 0; i < nreg; ++i)
  1162. {
  1163. if (regions[i].spanCount > 0 && regions[i].spanCount < minRegionArea && !regions[i].connectsToBorder)
  1164. {
  1165. int reg = regions[i].id;
  1166. for (int j = 0; j < nreg; ++j)
  1167. {
  1168. if (regions[j].id == reg)
  1169. {
  1170. regions[j].id = 0;
  1171. }
  1172. }
  1173. }
  1174. }
  1175. // Compress region Ids.
  1176. for (int i = 0; i < nreg; ++i)
  1177. {
  1178. regions[i].remap = false;
  1179. if (regions[i].id == 0)
  1180. {
  1181. continue; // Skip nil regions.
  1182. }
  1183. if ((regions[i].id & RC_BORDER_REG) != 0)
  1184. {
  1185. continue; // Skip external regions.
  1186. }
  1187. regions[i].remap = true;
  1188. }
  1189. int regIdGen = 0;
  1190. for (int i = 0; i < nreg; ++i)
  1191. {
  1192. if (!regions[i].remap)
  1193. {
  1194. continue;
  1195. }
  1196. int oldId = regions[i].id;
  1197. int newId = ++regIdGen;
  1198. for (int j = i; j < nreg; ++j)
  1199. {
  1200. if (regions[j].id == oldId)
  1201. {
  1202. regions[j].id = newId;
  1203. regions[j].remap = false;
  1204. }
  1205. }
  1206. }
  1207. maxRegionId = regIdGen;
  1208. // Remap regions.
  1209. for (int i = 0; i < chf.spanCount; ++i)
  1210. {
  1211. if ((srcReg[i] & RC_BORDER_REG) == 0)
  1212. {
  1213. srcReg[i] = regions[srcReg[i]].id;
  1214. }
  1215. }
  1216. return maxRegionId;
  1217. }
  1218. /// @par
  1219. ///
  1220. /// This is usually the second to the last step in creating a fully built
  1221. /// compact heightfield. This step is required before regions are built
  1222. /// using #rcBuildRegions or #rcBuildRegionsMonotone.
  1223. ///
  1224. /// After this step, the distance data is available via the rcCompactHeightfield::maxDistance
  1225. /// and rcCompactHeightfield::dist fields.
  1226. ///
  1227. /// @see rcCompactHeightfield, rcBuildRegions, rcBuildRegionsMonotone
  1228. public static void BuildDistanceField(RcTelemetry ctx, RcCompactHeightfield chf)
  1229. {
  1230. using var timer = ctx.ScopedTimer(RcTimerLabel.RC_TIMER_BUILD_DISTANCEFIELD);
  1231. int[] src = new int[chf.spanCount];
  1232. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_DISTANCEFIELD_DIST);
  1233. int maxDist = CalculateDistanceField(chf, src);
  1234. chf.maxDistance = maxDist;
  1235. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_DISTANCEFIELD_DIST);
  1236. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
  1237. // Blur
  1238. src = BoxBlur(chf, 1, src);
  1239. // Store distance.
  1240. chf.dist = src;
  1241. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
  1242. }
  1243. private static void PaintRectRegion(int minx, int maxx, int miny, int maxy, int regId, RcCompactHeightfield chf,
  1244. int[] srcReg)
  1245. {
  1246. int w = chf.width;
  1247. for (int y = miny; y < maxy; ++y)
  1248. {
  1249. for (int x = minx; x < maxx; ++x)
  1250. {
  1251. RcCompactCell c = chf.cells[x + y * w];
  1252. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1253. {
  1254. if (chf.areas[i] != RC_NULL_AREA)
  1255. {
  1256. srcReg[i] = regId;
  1257. }
  1258. }
  1259. }
  1260. }
  1261. }
  1262. /// @par
  1263. ///
  1264. /// Non-null regions will consist of connected, non-overlapping walkable spans that form a single contour.
  1265. /// Contours will form simple polygons.
  1266. ///
  1267. /// If multiple regions form an area that is smaller than @p minRegionArea, then all spans will be
  1268. /// re-assigned to the zero (null) region.
  1269. ///
  1270. /// Partitioning can result in smaller than necessary regions. @p mergeRegionArea helps
  1271. /// reduce unnecessarily small regions.
  1272. ///
  1273. /// See the #rcConfig documentation for more information on the configuration parameters.
  1274. ///
  1275. /// The region data will be available via the rcCompactHeightfield::maxRegions
  1276. /// and rcCompactSpan::reg fields.
  1277. ///
  1278. /// @warning The distance field must be created using #rcBuildDistanceField before attempting to build regions.
  1279. ///
  1280. /// @see rcCompactHeightfield, rcCompactSpan, rcBuildDistanceField, rcBuildRegionsMonotone, rcConfig
  1281. public static void BuildRegionsMonotone(RcTelemetry ctx, RcCompactHeightfield chf, int minRegionArea,
  1282. int mergeRegionArea)
  1283. {
  1284. using var timer = ctx.ScopedTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS);
  1285. int w = chf.width;
  1286. int h = chf.height;
  1287. int borderSize = chf.borderSize;
  1288. int id = 1;
  1289. int[] srcReg = new int[chf.spanCount];
  1290. int nsweeps = Math.Max(chf.width, chf.height);
  1291. RcSweepSpan[] sweeps = new RcSweepSpan[nsweeps];
  1292. for (int i = 0; i < sweeps.Length; i++)
  1293. {
  1294. sweeps[i] = new RcSweepSpan();
  1295. }
  1296. // Mark border regions.
  1297. if (borderSize > 0)
  1298. {
  1299. // Make sure border will not overflow.
  1300. int bw = Math.Min(w, borderSize);
  1301. int bh = Math.Min(h, borderSize);
  1302. // Paint regions
  1303. PaintRectRegion(0, bw, 0, h, id | RC_BORDER_REG, chf, srcReg);
  1304. id++;
  1305. PaintRectRegion(w - bw, w, 0, h, id | RC_BORDER_REG, chf, srcReg);
  1306. id++;
  1307. PaintRectRegion(0, w, 0, bh, id | RC_BORDER_REG, chf, srcReg);
  1308. id++;
  1309. PaintRectRegion(0, w, h - bh, h, id | RC_BORDER_REG, chf, srcReg);
  1310. id++;
  1311. }
  1312. int[] prev = new int[1024];
  1313. // Sweep one line at a time.
  1314. for (int y = borderSize; y < h - borderSize; ++y)
  1315. {
  1316. // Collect spans from this row.
  1317. if (prev.Length < id * 2)
  1318. {
  1319. prev = new int[id * 2];
  1320. }
  1321. else
  1322. {
  1323. Array.Fill(prev, 0, 0, (id) - (0));
  1324. }
  1325. int rid = 1;
  1326. for (int x = borderSize; x < w - borderSize; ++x)
  1327. {
  1328. RcCompactCell c = chf.cells[x + y * w];
  1329. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1330. {
  1331. RcCompactSpan s = chf.spans[i];
  1332. if (chf.areas[i] == RC_NULL_AREA)
  1333. {
  1334. continue;
  1335. }
  1336. // -x
  1337. int previd = 0;
  1338. if (RecastCommon.GetCon(s, 0) != RC_NOT_CONNECTED)
  1339. {
  1340. int ax = x + RecastCommon.GetDirOffsetX(0);
  1341. int ay = y + RecastCommon.GetDirOffsetY(0);
  1342. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 0);
  1343. if ((srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
  1344. {
  1345. previd = srcReg[ai];
  1346. }
  1347. }
  1348. if (previd == 0)
  1349. {
  1350. previd = rid++;
  1351. sweeps[previd].rid = previd;
  1352. sweeps[previd].ns = 0;
  1353. sweeps[previd].nei = 0;
  1354. }
  1355. // -y
  1356. if (RecastCommon.GetCon(s, 3) != RC_NOT_CONNECTED)
  1357. {
  1358. int ax = x + RecastCommon.GetDirOffsetX(3);
  1359. int ay = y + RecastCommon.GetDirOffsetY(3);
  1360. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 3);
  1361. if (srcReg[ai] != 0 && (srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
  1362. {
  1363. int nr = srcReg[ai];
  1364. if (sweeps[previd].nei == 0 || sweeps[previd].nei == nr)
  1365. {
  1366. sweeps[previd].nei = nr;
  1367. sweeps[previd].ns++;
  1368. if (prev.Length <= nr)
  1369. {
  1370. Array.Resize(ref prev, prev.Length * 2);
  1371. }
  1372. prev[nr]++;
  1373. }
  1374. else
  1375. {
  1376. sweeps[previd].nei = RC_NULL_NEI;
  1377. }
  1378. }
  1379. }
  1380. srcReg[i] = previd;
  1381. }
  1382. }
  1383. // Create unique ID.
  1384. for (int i = 1; i < rid; ++i)
  1385. {
  1386. if (sweeps[i].nei != RC_NULL_NEI && sweeps[i].nei != 0 && prev[sweeps[i].nei] == sweeps[i].ns)
  1387. {
  1388. sweeps[i].id = sweeps[i].nei;
  1389. }
  1390. else
  1391. {
  1392. sweeps[i].id = id++;
  1393. }
  1394. }
  1395. // Remap IDs
  1396. for (int x = borderSize; x < w - borderSize; ++x)
  1397. {
  1398. RcCompactCell c = chf.cells[x + y * w];
  1399. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1400. {
  1401. if (srcReg[i] > 0 && srcReg[i] < rid)
  1402. {
  1403. srcReg[i] = sweeps[srcReg[i]].id;
  1404. }
  1405. }
  1406. }
  1407. }
  1408. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1409. // Merge regions and filter out small regions.
  1410. List<int> overlaps = new List<int>();
  1411. chf.maxRegions = MergeAndFilterRegions(ctx, minRegionArea, mergeRegionArea, id, chf, srcReg, overlaps);
  1412. // Monotone partitioning does not generate overlapping regions.
  1413. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1414. // Store the result out.
  1415. for (int i = 0; i < chf.spanCount; ++i)
  1416. {
  1417. chf.spans[i].reg = srcReg[i];
  1418. }
  1419. }
  1420. /// @par
  1421. ///
  1422. /// Non-null regions will consist of connected, non-overlapping walkable spans that form a single contour.
  1423. /// Contours will form simple polygons.
  1424. ///
  1425. /// If multiple regions form an area that is smaller than @p minRegionArea, then all spans will be
  1426. /// re-assigned to the zero (null) region.
  1427. ///
  1428. /// Watershed partitioning can result in smaller than necessary regions, especially in diagonal corridors.
  1429. /// @p mergeRegionArea helps reduce unnecessarily small regions.
  1430. ///
  1431. /// See the #rcConfig documentation for more information on the configuration parameters.
  1432. ///
  1433. /// The region data will be available via the rcCompactHeightfield::maxRegions
  1434. /// and rcCompactSpan::reg fields.
  1435. ///
  1436. /// @warning The distance field must be created using #rcBuildDistanceField before attempting to build regions.
  1437. ///
  1438. /// @see rcCompactHeightfield, rcCompactSpan, rcBuildDistanceField, rcBuildRegionsMonotone, rcConfig
  1439. public static void BuildRegions(RcTelemetry ctx, RcCompactHeightfield chf, int minRegionArea,
  1440. int mergeRegionArea)
  1441. {
  1442. using var timer = ctx.ScopedTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS);
  1443. int w = chf.width;
  1444. int h = chf.height;
  1445. int borderSize = chf.borderSize;
  1446. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_WATERSHED);
  1447. int LOG_NB_STACKS = 3;
  1448. int NB_STACKS = 1 << LOG_NB_STACKS;
  1449. List<List<int>> lvlStacks = new List<List<int>>();
  1450. for (int i = 0; i < NB_STACKS; ++i)
  1451. {
  1452. lvlStacks.Add(new List<int>(1024));
  1453. }
  1454. List<int> stack = new List<int>(1024);
  1455. int[] srcReg = new int[chf.spanCount];
  1456. int[] srcDist = new int[chf.spanCount];
  1457. int regionId = 1;
  1458. int level = (chf.maxDistance + 1) & ~1;
  1459. // TODO: Figure better formula, expandIters defines how much the
  1460. // watershed "overflows" and simplifies the regions. Tying it to
  1461. // agent radius was usually good indication how greedy it could be.
  1462. // readonly int expandIters = 4 + walkableRadius * 2;
  1463. int expandIters = 8;
  1464. if (borderSize > 0)
  1465. {
  1466. // Make sure border will not overflow.
  1467. int bw = Math.Min(w, borderSize);
  1468. int bh = Math.Min(h, borderSize);
  1469. // Paint regions
  1470. PaintRectRegion(0, bw, 0, h, regionId | RC_BORDER_REG, chf, srcReg);
  1471. regionId++;
  1472. PaintRectRegion(w - bw, w, 0, h, regionId | RC_BORDER_REG, chf, srcReg);
  1473. regionId++;
  1474. PaintRectRegion(0, w, 0, bh, regionId | RC_BORDER_REG, chf, srcReg);
  1475. regionId++;
  1476. PaintRectRegion(0, w, h - bh, h, regionId | RC_BORDER_REG, chf, srcReg);
  1477. regionId++;
  1478. }
  1479. chf.borderSize = borderSize;
  1480. int sId = -1;
  1481. while (level > 0)
  1482. {
  1483. level = level >= 2 ? level - 2 : 0;
  1484. sId = (sId + 1) & (NB_STACKS - 1);
  1485. // ctx->StartTimer(RC_TIMER_DIVIDE_TO_LEVELS);
  1486. if (sId == 0)
  1487. {
  1488. SortCellsByLevel(level, chf, srcReg, NB_STACKS, lvlStacks, 1);
  1489. }
  1490. else
  1491. {
  1492. AppendStacks(lvlStacks[sId - 1], lvlStacks[sId], srcReg); // copy left overs from last level
  1493. }
  1494. // ctx->StopTimer(RC_TIMER_DIVIDE_TO_LEVELS);
  1495. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_EXPAND);
  1496. // Expand current regions until no empty connected cells found.
  1497. ExpandRegions(expandIters, level, chf, srcReg, srcDist, lvlStacks[sId], false);
  1498. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_EXPAND);
  1499. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FLOOD);
  1500. // Mark new regions with IDs.
  1501. for (int j = 0; j < lvlStacks[sId].Count; j += 3)
  1502. {
  1503. int x = lvlStacks[sId][j];
  1504. int y = lvlStacks[sId][j + 1];
  1505. int i = lvlStacks[sId][j + 2];
  1506. if (i >= 0 && srcReg[i] == 0)
  1507. {
  1508. if (FloodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack))
  1509. {
  1510. regionId++;
  1511. }
  1512. }
  1513. }
  1514. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FLOOD);
  1515. }
  1516. // Expand current regions until no empty connected cells found.
  1517. ExpandRegions(expandIters * 8, 0, chf, srcReg, srcDist, stack, true);
  1518. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_WATERSHED);
  1519. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1520. // Merge regions and filter out small regions.
  1521. List<int> overlaps = new List<int>();
  1522. chf.maxRegions = MergeAndFilterRegions(ctx, minRegionArea, mergeRegionArea, regionId, chf, srcReg, overlaps);
  1523. // If overlapping regions were found during merging, split those regions.
  1524. if (overlaps.Count > 0)
  1525. {
  1526. ctx.Warn("rcBuildRegions: " + overlaps.Count + " overlapping regions.");
  1527. }
  1528. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1529. // Write the result out.
  1530. for (int i = 0; i < chf.spanCount; ++i)
  1531. {
  1532. chf.spans[i].reg = srcReg[i];
  1533. }
  1534. }
  1535. public static void BuildLayerRegions(RcTelemetry ctx, RcCompactHeightfield chf, int minRegionArea)
  1536. {
  1537. using var timer = ctx.ScopedTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS);
  1538. int w = chf.width;
  1539. int h = chf.height;
  1540. int borderSize = chf.borderSize;
  1541. int id = 1;
  1542. int[] srcReg = new int[chf.spanCount];
  1543. int nsweeps = Math.Max(chf.width, chf.height);
  1544. RcSweepSpan[] sweeps = new RcSweepSpan[nsweeps];
  1545. for (int i = 0; i < sweeps.Length; i++)
  1546. {
  1547. sweeps[i] = new RcSweepSpan();
  1548. }
  1549. // Mark border regions.
  1550. if (borderSize > 0)
  1551. {
  1552. // Make sure border will not overflow.
  1553. int bw = Math.Min(w, borderSize);
  1554. int bh = Math.Min(h, borderSize);
  1555. // Paint regions
  1556. PaintRectRegion(0, bw, 0, h, id | RC_BORDER_REG, chf, srcReg);
  1557. id++;
  1558. PaintRectRegion(w - bw, w, 0, h, id | RC_BORDER_REG, chf, srcReg);
  1559. id++;
  1560. PaintRectRegion(0, w, 0, bh, id | RC_BORDER_REG, chf, srcReg);
  1561. id++;
  1562. PaintRectRegion(0, w, h - bh, h, id | RC_BORDER_REG, chf, srcReg);
  1563. id++;
  1564. }
  1565. int[] prev = new int[1024];
  1566. // Sweep one line at a time.
  1567. for (int y = borderSize; y < h - borderSize; ++y)
  1568. {
  1569. // Collect spans from this row.
  1570. if (prev.Length <= id * 2)
  1571. {
  1572. prev = new int[id * 2];
  1573. }
  1574. else
  1575. {
  1576. Array.Fill(prev, 0, 0, (id) - (0));
  1577. }
  1578. int rid = 1;
  1579. for (int x = borderSize; x < w - borderSize; ++x)
  1580. {
  1581. RcCompactCell c = chf.cells[x + y * w];
  1582. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1583. {
  1584. RcCompactSpan s = chf.spans[i];
  1585. if (chf.areas[i] == RC_NULL_AREA)
  1586. {
  1587. continue;
  1588. }
  1589. // -x
  1590. int previd = 0;
  1591. if (RecastCommon.GetCon(s, 0) != RC_NOT_CONNECTED)
  1592. {
  1593. int ax = x + RecastCommon.GetDirOffsetX(0);
  1594. int ay = y + RecastCommon.GetDirOffsetY(0);
  1595. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 0);
  1596. if ((srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
  1597. {
  1598. previd = srcReg[ai];
  1599. }
  1600. }
  1601. if (previd == 0)
  1602. {
  1603. previd = rid++;
  1604. sweeps[previd].rid = previd;
  1605. sweeps[previd].ns = 0;
  1606. sweeps[previd].nei = 0;
  1607. }
  1608. // -y
  1609. if (RecastCommon.GetCon(s, 3) != RC_NOT_CONNECTED)
  1610. {
  1611. int ax = x + RecastCommon.GetDirOffsetX(3);
  1612. int ay = y + RecastCommon.GetDirOffsetY(3);
  1613. int ai = chf.cells[ax + ay * w].index + RecastCommon.GetCon(s, 3);
  1614. if (srcReg[ai] != 0 && (srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
  1615. {
  1616. int nr = srcReg[ai];
  1617. if (sweeps[previd].nei == 0 || sweeps[previd].nei == nr)
  1618. {
  1619. sweeps[previd].nei = nr;
  1620. sweeps[previd].ns++;
  1621. if (prev.Length <= nr)
  1622. {
  1623. Array.Resize(ref prev, prev.Length * 2);
  1624. }
  1625. prev[nr]++;
  1626. }
  1627. else
  1628. {
  1629. sweeps[previd].nei = RC_NULL_NEI;
  1630. }
  1631. }
  1632. }
  1633. srcReg[i] = previd;
  1634. }
  1635. }
  1636. // Create unique ID.
  1637. for (int i = 1; i < rid; ++i)
  1638. {
  1639. if (sweeps[i].nei != RC_NULL_NEI && sweeps[i].nei != 0 && prev[sweeps[i].nei] == sweeps[i].ns)
  1640. {
  1641. sweeps[i].id = sweeps[i].nei;
  1642. }
  1643. else
  1644. {
  1645. sweeps[i].id = id++;
  1646. }
  1647. }
  1648. // Remap IDs
  1649. for (int x = borderSize; x < w - borderSize; ++x)
  1650. {
  1651. RcCompactCell c = chf.cells[x + y * w];
  1652. for (int i = c.index, ni = c.index + c.count; i < ni; ++i)
  1653. {
  1654. if (srcReg[i] > 0 && srcReg[i] < rid)
  1655. {
  1656. srcReg[i] = sweeps[srcReg[i]].id;
  1657. }
  1658. }
  1659. }
  1660. }
  1661. ctx.StartTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1662. // Merge monotone regions to layers and remove small regions.
  1663. List<int> overlaps = new List<int>();
  1664. chf.maxRegions = MergeAndFilterLayerRegions(ctx, minRegionArea, id, chf, srcReg, overlaps);
  1665. ctx.StopTimer(RcTimerLabel.RC_TIMER_BUILD_REGIONS_FILTER);
  1666. // Store the result out.
  1667. for (int i = 0; i < chf.spanCount; ++i)
  1668. {
  1669. chf.spans[i].reg = srcReg[i];
  1670. }
  1671. }
  1672. }
  1673. }