Debian Codegraph @

Recast/Source/RecastRegion.cpp

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