|
|
@@ -25,8 +25,17 @@
|
|
|
#include "Recast.h"
|
|
|
#include "RecastAlloc.h"
|
|
|
#include "RecastAssert.h"
|
|
|
-#include <new>
|
|
|
|
|
|
+namespace
|
|
|
+{
|
|
|
+struct LevelStackEntry
|
|
|
+{
|
|
|
+ LevelStackEntry(int x_, int y_, int index_) : x(x_), y(y_), index(index_) {}
|
|
|
+ int x;
|
|
|
+ int y;
|
|
|
+ int index;
|
|
|
+};
|
|
|
+} // namespace
|
|
|
|
|
|
static void calculateDistanceField(rcCompactHeightfield& chf, unsigned short* src, unsigned short& maxDist)
|
|
|
{
|
|
|
@@ -245,17 +254,15 @@ static bool floodRegion(int x, int y, int i,
|
|
|
unsigned short level, unsigned short r,
|
|
|
rcCompactHeightfield& chf,
|
|
|
unsigned short* srcReg, unsigned short* srcDist,
|
|
|
- rcIntArray& stack)
|
|
|
+ rcTempVector<LevelStackEntry>& stack)
|
|
|
{
|
|
|
const int w = chf.width;
|
|
|
|
|
|
const unsigned char area = chf.areas[i];
|
|
|
|
|
|
// Flood fill mark region.
|
|
|
- stack.resize(0);
|
|
|
- stack.push((int)x);
|
|
|
- stack.push((int)y);
|
|
|
- stack.push((int)i);
|
|
|
+ stack.clear();
|
|
|
+ stack.push_back(LevelStackEntry(x, y, i));
|
|
|
srcReg[i] = r;
|
|
|
srcDist[i] = 0;
|
|
|
|
|
|
@@ -264,9 +271,11 @@ static bool floodRegion(int x, int y, int i,
|
|
|
|
|
|
while (stack.size() > 0)
|
|
|
{
|
|
|
- int ci = stack.pop();
|
|
|
- int cy = stack.pop();
|
|
|
- int cx = stack.pop();
|
|
|
+ LevelStackEntry& back = stack.back();
|
|
|
+ int cx = back.x;
|
|
|
+ int cy = back.y;
|
|
|
+ int ci = back.index;
|
|
|
+ stack.pop_back();
|
|
|
|
|
|
const rcCompactSpan& cs = chf.spans[ci];
|
|
|
|
|
|
@@ -315,6 +324,7 @@ static bool floodRegion(int x, int y, int i,
|
|
|
srcReg[ci] = 0;
|
|
|
continue;
|
|
|
}
|
|
|
+
|
|
|
count++;
|
|
|
|
|
|
// Expand neighbours.
|
|
|
@@ -331,9 +341,7 @@ static bool floodRegion(int x, int y, int i,
|
|
|
{
|
|
|
srcReg[ai] = r;
|
|
|
srcDist[ai] = 0;
|
|
|
- stack.push(ax);
|
|
|
- stack.push(ay);
|
|
|
- stack.push(ai);
|
|
|
+ stack.push_back(LevelStackEntry(ax, ay, ai));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -342,12 +350,20 @@ static bool floodRegion(int x, int y, int i,
|
|
|
return count > 0;
|
|
|
}
|
|
|
|
|
|
-static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
- rcCompactHeightfield& chf,
|
|
|
- unsigned short* srcReg, unsigned short* srcDist,
|
|
|
- unsigned short* dstReg, unsigned short* dstDist,
|
|
|
- rcIntArray& stack,
|
|
|
- bool fillStack)
|
|
|
+// Struct to keep track of entries in the region table that have been changed.
|
|
|
+struct DirtyEntry
|
|
|
+{
|
|
|
+ DirtyEntry(int index_, unsigned short region_, unsigned short distance2_)
|
|
|
+ : index(index_), region(region_), distance2(distance2_) {}
|
|
|
+ int index;
|
|
|
+ unsigned short region;
|
|
|
+ unsigned short distance2;
|
|
|
+};
|
|
|
+static void expandRegions(int maxIter, unsigned short level,
|
|
|
+ rcCompactHeightfield& chf,
|
|
|
+ unsigned short* srcReg, unsigned short* srcDist,
|
|
|
+ rcTempVector<LevelStackEntry>& stack,
|
|
|
+ bool fillStack)
|
|
|
{
|
|
|
const int w = chf.width;
|
|
|
const int h = chf.height;
|
|
|
@@ -355,7 +371,7 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
if (fillStack)
|
|
|
{
|
|
|
// Find cells revealed by the raised level.
|
|
|
- stack.resize(0);
|
|
|
+ stack.clear();
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
{
|
|
|
for (int x = 0; x < w; ++x)
|
|
|
@@ -365,9 +381,7 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
{
|
|
|
if (chf.dist[i] >= level && srcReg[i] == 0 && chf.areas[i] != RC_NULL_AREA)
|
|
|
{
|
|
|
- stack.push(x);
|
|
|
- stack.push(y);
|
|
|
- stack.push(i);
|
|
|
+ stack.push_back(LevelStackEntry(x, y, i));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
@@ -376,27 +390,26 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
else // use cells in the input stack
|
|
|
{
|
|
|
// mark all cells which already have a region
|
|
|
- for (int j=0; j<stack.size(); j+=3)
|
|
|
+ for (int j=0; j<stack.size(); j++)
|
|
|
{
|
|
|
- int i = stack[j+2];
|
|
|
+ int i = stack[j].index;
|
|
|
if (srcReg[i] != 0)
|
|
|
- stack[j+2] = -1;
|
|
|
+ stack[j].index = -1;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ rcTempVector<DirtyEntry> dirtyEntries;
|
|
|
int iter = 0;
|
|
|
while (stack.size() > 0)
|
|
|
{
|
|
|
int failed = 0;
|
|
|
+ dirtyEntries.clear();
|
|
|
|
|
|
- memcpy(dstReg, srcReg, sizeof(unsigned short)*chf.spanCount);
|
|
|
- memcpy(dstDist, srcDist, sizeof(unsigned short)*chf.spanCount);
|
|
|
-
|
|
|
- for (int j = 0; j < stack.size(); j += 3)
|
|
|
+ for (int j = 0; j < stack.size(); j++)
|
|
|
{
|
|
|
- int x = stack[j+0];
|
|
|
- int y = stack[j+1];
|
|
|
- int i = stack[j+2];
|
|
|
+ int x = stack[j].x;
|
|
|
+ int y = stack[j].y;
|
|
|
+ int i = stack[j].index;
|
|
|
if (i < 0)
|
|
|
{
|
|
|
failed++;
|
|
|
@@ -425,9 +438,8 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
}
|
|
|
if (r)
|
|
|
{
|
|
|
- stack[j+2] = -1; // mark as used
|
|
|
- dstReg[i] = r;
|
|
|
- dstDist[i] = d2;
|
|
|
+ stack[j].index = -1; // mark as used
|
|
|
+ dirtyEntries.push_back(DirtyEntry(i, r, d2));
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
@@ -435,11 +447,14 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- // rcSwap source and dest.
|
|
|
- rcSwap(srcReg, dstReg);
|
|
|
- rcSwap(srcDist, dstDist);
|
|
|
+ // Copy entries that differ between src and dst to keep them in sync.
|
|
|
+ for (int i = 0; i < dirtyEntries.size(); i++) {
|
|
|
+ int idx = dirtyEntries[i].index;
|
|
|
+ srcReg[idx] = dirtyEntries[i].region;
|
|
|
+ srcDist[idx] = dirtyEntries[i].distance2;
|
|
|
+ }
|
|
|
|
|
|
- if (failed*3 == stack.size())
|
|
|
+ if (failed == stack.size())
|
|
|
break;
|
|
|
|
|
|
if (level > 0)
|
|
|
@@ -449,16 +464,14 @@ static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- return srcReg;
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void sortCellsByLevel(unsigned short startLevel,
|
|
|
rcCompactHeightfield& chf,
|
|
|
- unsigned short* srcReg,
|
|
|
- unsigned int nbStacks, rcIntArray* stacks,
|
|
|
+ const unsigned short* srcReg,
|
|
|
+ unsigned int nbStacks, rcTempVector<LevelStackEntry>* stacks,
|
|
|
unsigned short loglevelsPerStack) // the levels per stack (2 in our case) as a bit shift
|
|
|
{
|
|
|
const int w = chf.width;
|
|
|
@@ -466,7 +479,7 @@ static void sortCellsByLevel(unsigned short startLevel,
|
|
|
startLevel = startLevel >> loglevelsPerStack;
|
|
|
|
|
|
for (unsigned int j=0; j<nbStacks; ++j)
|
|
|
- stacks[j].resize(0);
|
|
|
+ stacks[j].clear();
|
|
|
|
|
|
// put all cells in the level range into the appropriate stacks
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
@@ -486,26 +499,23 @@ static void sortCellsByLevel(unsigned short startLevel,
|
|
|
if (sId < 0)
|
|
|
sId = 0;
|
|
|
|
|
|
- stacks[sId].push(x);
|
|
|
- stacks[sId].push(y);
|
|
|
- stacks[sId].push(i);
|
|
|
+ stacks[sId].push_back(LevelStackEntry(x, y, i));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
|
|
|
-static void appendStacks(rcIntArray& srcStack, rcIntArray& dstStack,
|
|
|
- unsigned short* srcReg)
|
|
|
+static void appendStacks(const rcTempVector<LevelStackEntry>& srcStack,
|
|
|
+ rcTempVector<LevelStackEntry>& dstStack,
|
|
|
+ const unsigned short* srcReg)
|
|
|
{
|
|
|
- for (int j=0; j<srcStack.size(); j+=3)
|
|
|
+ for (int j=0; j<srcStack.size(); j++)
|
|
|
{
|
|
|
- int i = srcStack[j+2];
|
|
|
+ int i = srcStack[j].index;
|
|
|
if ((i < 0) || (srcReg[i] != 0))
|
|
|
continue;
|
|
|
- dstStack.push(srcStack[j]);
|
|
|
- dstStack.push(srcStack[j+1]);
|
|
|
- dstStack.push(srcStack[j+2]);
|
|
|
+ dstStack.push_back(srcStack[j]);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -516,7 +526,11 @@ struct rcRegion
|
|
|
id(i),
|
|
|
areaType(0),
|
|
|
remap(false),
|
|
|
- visited(false)
|
|
|
+ visited(false),
|
|
|
+ overlap(false),
|
|
|
+ connectsToBorder(false),
|
|
|
+ ymin(0xffff),
|
|
|
+ ymax(0)
|
|
|
{}
|
|
|
|
|
|
int spanCount; // Number of spans belonging to this region
|
|
|
@@ -524,6 +538,9 @@ struct rcRegion
|
|
|
unsigned char areaType; // Are type.
|
|
|
bool remap;
|
|
|
bool visited;
|
|
|
+ bool overlap;
|
|
|
+ bool connectsToBorder;
|
|
|
+ unsigned short ymin, ymax;
|
|
|
rcIntArray connections;
|
|
|
rcIntArray floors;
|
|
|
};
|
|
|
@@ -663,7 +680,7 @@ static bool isRegionConnectedToBorder(const rcRegion& reg)
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
-static bool isSolidEdge(rcCompactHeightfield& chf, unsigned short* srcReg,
|
|
|
+static bool isSolidEdge(rcCompactHeightfield& chf, const unsigned short* srcReg,
|
|
|
int x, int y, int i, int dir)
|
|
|
{
|
|
|
const rcCompactSpan& s = chf.spans[i];
|
|
|
@@ -682,7 +699,7 @@ static bool isSolidEdge(rcCompactHeightfield& chf, unsigned short* srcReg,
|
|
|
|
|
|
static void walkContour(int x, int y, int i, int dir,
|
|
|
rcCompactHeightfield& chf,
|
|
|
- unsigned short* srcReg,
|
|
|
+ const unsigned short* srcReg,
|
|
|
rcIntArray& cont)
|
|
|
{
|
|
|
int startDir = dir;
|
|
|
@@ -768,25 +785,25 @@ static void walkContour(int x, int y, int i, int dir,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegionSize,
|
|
|
- unsigned short& maxRegionId,
|
|
|
- rcCompactHeightfield& chf,
|
|
|
- unsigned short* srcReg)
|
|
|
+
|
|
|
+static bool mergeAndFilterRegions(rcContext* ctx, int minRegionArea, int mergeRegionSize,
|
|
|
+ unsigned short& maxRegionId,
|
|
|
+ rcCompactHeightfield& chf,
|
|
|
+ unsigned short* srcReg, rcIntArray& overlaps)
|
|
|
{
|
|
|
const int w = chf.width;
|
|
|
const int h = chf.height;
|
|
|
|
|
|
const int nreg = maxRegionId+1;
|
|
|
- rcRegion* regions = (rcRegion*)rcAlloc(sizeof(rcRegion)*nreg, RC_ALLOC_TEMP);
|
|
|
- if (!regions)
|
|
|
- {
|
|
|
- ctx->log(RC_LOG_ERROR, "filterSmallRegions: Out of memory 'regions' (%d).", nreg);
|
|
|
+ rcTempVector<rcRegion> regions;
|
|
|
+ if (!regions.reserve(nreg)) {
|
|
|
+ ctx->log(RC_LOG_ERROR, "mergeAndFilterRegions: Out of memory 'regions' (%d).", nreg);
|
|
|
return false;
|
|
|
}
|
|
|
|
|
|
// Construct regions
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
- new(®ions[i]) rcRegion((unsigned short)i);
|
|
|
+ regions.push_back(rcRegion((unsigned short) i));
|
|
|
|
|
|
// Find edge of a region and find connections around the contour.
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
@@ -803,7 +820,6 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
rcRegion& reg = regions[r];
|
|
|
reg.spanCount++;
|
|
|
|
|
|
-
|
|
|
// Update floors.
|
|
|
for (int j = (int)c.index; j < ni; ++j)
|
|
|
{
|
|
|
@@ -811,6 +827,8 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
unsigned short floorId = srcReg[j];
|
|
|
if (floorId == 0 || floorId >= nreg)
|
|
|
continue;
|
|
|
+ if (floorId == r)
|
|
|
+ reg.overlap = true;
|
|
|
addUniqueFloorRegion(reg, floorId);
|
|
|
}
|
|
|
|
|
|
@@ -906,7 +924,7 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
// Merge too small regions to neighbour regions.
|
|
|
int mergeCount = 0 ;
|
|
|
do
|
|
|
@@ -916,7 +934,9 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
{
|
|
|
rcRegion& reg = regions[i];
|
|
|
if (reg.id == 0 || (reg.id & RC_BORDER_REG))
|
|
|
- continue;
|
|
|
+ continue;
|
|
|
+ if (reg.overlap)
|
|
|
+ continue;
|
|
|
if (reg.spanCount == 0)
|
|
|
continue;
|
|
|
|
|
|
@@ -933,7 +953,7 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
{
|
|
|
if (reg.connections[j] & RC_BORDER_REG) continue;
|
|
|
rcRegion& mreg = regions[reg.connections[j]];
|
|
|
- if (mreg.id == 0 || (mreg.id & RC_BORDER_REG)) continue;
|
|
|
+ if (mreg.id == 0 || (mreg.id & RC_BORDER_REG) || mreg.overlap) continue;
|
|
|
if (mreg.spanCount < smallest &&
|
|
|
canMergeWithRegion(reg, mreg) &&
|
|
|
canMergeWithRegion(mreg, reg))
|
|
|
@@ -1003,14 +1023,224 @@ static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegio
|
|
|
if ((srcReg[i] & RC_BORDER_REG) == 0)
|
|
|
srcReg[i] = regions[srcReg[i]].id;
|
|
|
}
|
|
|
+
|
|
|
+ // Return regions that we found to be overlapping.
|
|
|
+ for (int i = 0; i < nreg; ++i)
|
|
|
+ if (regions[i].overlap)
|
|
|
+ overlaps.push(regions[i].id);
|
|
|
+
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+static void addUniqueConnection(rcRegion& reg, int n)
|
|
|
+{
|
|
|
+ for (int i = 0; i < reg.connections.size(); ++i)
|
|
|
+ if (reg.connections[i] == n)
|
|
|
+ return;
|
|
|
+ reg.connections.push(n);
|
|
|
+}
|
|
|
+
|
|
|
+static bool mergeAndFilterLayerRegions(rcContext* ctx, int minRegionArea,
|
|
|
+ unsigned short& maxRegionId,
|
|
|
+ rcCompactHeightfield& chf,
|
|
|
+ unsigned short* srcReg)
|
|
|
+{
|
|
|
+ const int w = chf.width;
|
|
|
+ const int h = chf.height;
|
|
|
|
|
|
+ const int nreg = maxRegionId+1;
|
|
|
+ rcTempVector<rcRegion> regions;
|
|
|
+
|
|
|
+ // Construct regions
|
|
|
+ if (!regions.reserve(nreg)) {
|
|
|
+ ctx->log(RC_LOG_ERROR, "mergeAndFilterLayerRegions: Out of memory 'regions' (%d).", nreg);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
- regions[i].~rcRegion();
|
|
|
- rcFree(regions);
|
|
|
+ regions.push_back(rcRegion((unsigned short) i));
|
|
|
+
|
|
|
+ // Find region neighbours and overlapping regions.
|
|
|
+ rcIntArray lregs(32);
|
|
|
+ for (int y = 0; y < h; ++y)
|
|
|
+ {
|
|
|
+ for (int x = 0; x < w; ++x)
|
|
|
+ {
|
|
|
+ const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
+
|
|
|
+ lregs.resize(0);
|
|
|
+
|
|
|
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
+ {
|
|
|
+ const rcCompactSpan& s = chf.spans[i];
|
|
|
+ const unsigned short ri = srcReg[i];
|
|
|
+ if (ri == 0 || ri >= nreg) continue;
|
|
|
+ rcRegion& reg = regions[ri];
|
|
|
+
|
|
|
+ reg.spanCount++;
|
|
|
+
|
|
|
+ reg.ymin = rcMin(reg.ymin, s.y);
|
|
|
+ reg.ymax = rcMax(reg.ymax, s.y);
|
|
|
+
|
|
|
+ // Collect all region layers.
|
|
|
+ lregs.push(ri);
|
|
|
+
|
|
|
+ // Update neighbours
|
|
|
+ for (int dir = 0; dir < 4; ++dir)
|
|
|
+ {
|
|
|
+ if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
|
|
|
+ {
|
|
|
+ const int ax = x + rcGetDirOffsetX(dir);
|
|
|
+ const int ay = y + rcGetDirOffsetY(dir);
|
|
|
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, dir);
|
|
|
+ const unsigned short rai = srcReg[ai];
|
|
|
+ if (rai > 0 && rai < nreg && rai != ri)
|
|
|
+ addUniqueConnection(reg, rai);
|
|
|
+ if (rai & RC_BORDER_REG)
|
|
|
+ reg.connectsToBorder = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ // Update overlapping regions.
|
|
|
+ for (int i = 0; i < lregs.size()-1; ++i)
|
|
|
+ {
|
|
|
+ for (int j = i+1; j < lregs.size(); ++j)
|
|
|
+ {
|
|
|
+ if (lregs[i] != lregs[j])
|
|
|
+ {
|
|
|
+ rcRegion& ri = regions[lregs[i]];
|
|
|
+ rcRegion& rj = regions[lregs[j]];
|
|
|
+ addUniqueFloorRegion(ri, lregs[j]);
|
|
|
+ addUniqueFloorRegion(rj, lregs[i]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create 2D layers from regions.
|
|
|
+ unsigned short layerId = 1;
|
|
|
+
|
|
|
+ for (int i = 0; i < nreg; ++i)
|
|
|
+ regions[i].id = 0;
|
|
|
+
|
|
|
+ // Merge montone regions to create non-overlapping areas.
|
|
|
+ rcIntArray stack(32);
|
|
|
+ for (int i = 1; i < nreg; ++i)
|
|
|
+ {
|
|
|
+ rcRegion& root = regions[i];
|
|
|
+ // Skip already visited.
|
|
|
+ if (root.id != 0)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ // Start search.
|
|
|
+ root.id = layerId;
|
|
|
+
|
|
|
+ stack.resize(0);
|
|
|
+ stack.push(i);
|
|
|
+
|
|
|
+ while (stack.size() > 0)
|
|
|
+ {
|
|
|
+ // Pop front
|
|
|
+ rcRegion& reg = regions[stack[0]];
|
|
|
+ for (int j = 0; j < stack.size()-1; ++j)
|
|
|
+ stack[j] = stack[j+1];
|
|
|
+ stack.resize(stack.size()-1);
|
|
|
+
|
|
|
+ const int ncons = (int)reg.connections.size();
|
|
|
+ for (int j = 0; j < ncons; ++j)
|
|
|
+ {
|
|
|
+ const int nei = reg.connections[j];
|
|
|
+ rcRegion& regn = regions[nei];
|
|
|
+ // Skip already visited.
|
|
|
+ if (regn.id != 0)
|
|
|
+ continue;
|
|
|
+ // Skip if the neighbour is overlapping root region.
|
|
|
+ bool overlap = false;
|
|
|
+ for (int k = 0; k < root.floors.size(); k++)
|
|
|
+ {
|
|
|
+ if (root.floors[k] == nei)
|
|
|
+ {
|
|
|
+ overlap = true;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (overlap)
|
|
|
+ continue;
|
|
|
+
|
|
|
+ // Deepen
|
|
|
+ stack.push(nei);
|
|
|
+
|
|
|
+ // Mark layer id
|
|
|
+ regn.id = layerId;
|
|
|
+ // Merge current layers to root.
|
|
|
+ for (int k = 0; k < regn.floors.size(); ++k)
|
|
|
+ addUniqueFloorRegion(root, regn.floors[k]);
|
|
|
+ root.ymin = rcMin(root.ymin, regn.ymin);
|
|
|
+ root.ymax = rcMax(root.ymax, regn.ymax);
|
|
|
+ root.spanCount += regn.spanCount;
|
|
|
+ regn.spanCount = 0;
|
|
|
+ root.connectsToBorder = root.connectsToBorder || regn.connectsToBorder;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ layerId++;
|
|
|
+ }
|
|
|
+
|
|
|
+ // Remove small regions
|
|
|
+ for (int i = 0; i < nreg; ++i)
|
|
|
+ {
|
|
|
+ if (regions[i].spanCount > 0 && regions[i].spanCount < minRegionArea && !regions[i].connectsToBorder)
|
|
|
+ {
|
|
|
+ unsigned short reg = regions[i].id;
|
|
|
+ for (int j = 0; j < nreg; ++j)
|
|
|
+ if (regions[j].id == reg)
|
|
|
+ regions[j].id = 0;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Compress region Ids.
|
|
|
+ for (int i = 0; i < nreg; ++i)
|
|
|
+ {
|
|
|
+ regions[i].remap = false;
|
|
|
+ if (regions[i].id == 0) continue; // Skip nil regions.
|
|
|
+ if (regions[i].id & RC_BORDER_REG) continue; // Skip external regions.
|
|
|
+ regions[i].remap = true;
|
|
|
+ }
|
|
|
+
|
|
|
+ unsigned short regIdGen = 0;
|
|
|
+ for (int i = 0; i < nreg; ++i)
|
|
|
+ {
|
|
|
+ if (!regions[i].remap)
|
|
|
+ continue;
|
|
|
+ unsigned short oldId = regions[i].id;
|
|
|
+ unsigned short newId = ++regIdGen;
|
|
|
+ for (int j = i; j < nreg; ++j)
|
|
|
+ {
|
|
|
+ if (regions[j].id == oldId)
|
|
|
+ {
|
|
|
+ regions[j].id = newId;
|
|
|
+ regions[j].remap = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ maxRegionId = regIdGen;
|
|
|
+
|
|
|
+ // Remap regions.
|
|
|
+ for (int i = 0; i < chf.spanCount; ++i)
|
|
|
+ {
|
|
|
+ if ((srcReg[i] & RC_BORDER_REG) == 0)
|
|
|
+ srcReg[i] = regions[srcReg[i]].id;
|
|
|
+ }
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
/// @par
|
|
|
///
|
|
|
/// This is usually the second to the last step in creating a fully built
|
|
|
@@ -1025,7 +1255,7 @@ bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf)
|
|
|
{
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD);
|
|
|
+ rcScopedTimer timer(ctx, RC_TIMER_BUILD_DISTANCEFIELD);
|
|
|
|
|
|
if (chf.dist)
|
|
|
{
|
|
|
@@ -1049,25 +1279,23 @@ bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf)
|
|
|
|
|
|
unsigned short maxDist = 0;
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD_DIST);
|
|
|
-
|
|
|
- calculateDistanceField(chf, src, maxDist);
|
|
|
- chf.maxDistance = maxDist;
|
|
|
-
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD_DIST);
|
|
|
-
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
|
|
|
-
|
|
|
- // Blur
|
|
|
- if (boxBlur(chf, 1, src, dst) != src)
|
|
|
- rcSwap(src, dst);
|
|
|
-
|
|
|
- // Store distance.
|
|
|
- chf.dist = src;
|
|
|
-
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
|
|
|
+ {
|
|
|
+ rcScopedTimer timerDist(ctx, RC_TIMER_BUILD_DISTANCEFIELD_DIST);
|
|
|
+
|
|
|
+ calculateDistanceField(chf, src, maxDist);
|
|
|
+ chf.maxDistance = maxDist;
|
|
|
+ }
|
|
|
+
|
|
|
+ {
|
|
|
+ rcScopedTimer timerBlur(ctx, RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
|
|
|
+
|
|
|
+ // Blur
|
|
|
+ if (boxBlur(chf, 1, src, dst) != src)
|
|
|
+ rcSwap(src, dst);
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD);
|
|
|
+ // Store distance.
|
|
|
+ chf.dist = src;
|
|
|
+ }
|
|
|
|
|
|
rcFree(dst);
|
|
|
|
|
|
@@ -1127,13 +1355,13 @@ bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
{
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS);
|
|
|
+ rcScopedTimer timer(ctx, RC_TIMER_BUILD_REGIONS);
|
|
|
|
|
|
const int w = chf.width;
|
|
|
const int h = chf.height;
|
|
|
unsigned short id = 1;
|
|
|
|
|
|
- rcScopedDelete<unsigned short> srcReg = (unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_TEMP);
|
|
|
+ rcScopedDelete<unsigned short> srcReg((unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_TEMP));
|
|
|
if (!srcReg)
|
|
|
{
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildRegionsMonotone: Out of memory 'src' (%d).", chf.spanCount);
|
|
|
@@ -1142,7 +1370,7 @@ bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
memset(srcReg,0,sizeof(unsigned short)*chf.spanCount);
|
|
|
|
|
|
const int nsweeps = rcMax(chf.width,chf.height);
|
|
|
- rcScopedDelete<rcSweepSpan> sweeps = (rcSweepSpan*)rcAlloc(sizeof(rcSweepSpan)*nsweeps, RC_ALLOC_TEMP);
|
|
|
+ rcScopedDelete<rcSweepSpan> sweeps((rcSweepSpan*)rcAlloc(sizeof(rcSweepSpan)*nsweeps, RC_ALLOC_TEMP));
|
|
|
if (!sweeps)
|
|
|
{
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildRegionsMonotone: Out of memory 'sweeps' (%d).", nsweeps);
|
|
|
@@ -1161,9 +1389,9 @@ bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
paintRectRegion(w-bw, w, 0, h, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
paintRectRegion(0, w, 0, bh, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
paintRectRegion(0, w, h-bh, h, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
-
|
|
|
- chf.borderSize = borderSize;
|
|
|
}
|
|
|
+
|
|
|
+ chf.borderSize = borderSize;
|
|
|
|
|
|
rcIntArray prev(256);
|
|
|
|
|
|
@@ -1256,20 +1484,22 @@ bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
|
|
|
- // Filter out small regions.
|
|
|
- chf.maxRegions = id;
|
|
|
- if (!filterSmallRegions(ctx, minRegionArea, mergeRegionArea, chf.maxRegions, chf, srcReg))
|
|
|
- return false;
|
|
|
+ {
|
|
|
+ rcScopedTimer timerFilter(ctx, RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
+ // Merge regions and filter out small regions.
|
|
|
+ rcIntArray overlaps;
|
|
|
+ chf.maxRegions = id;
|
|
|
+ if (!mergeAndFilterRegions(ctx, minRegionArea, mergeRegionArea, chf.maxRegions, chf, srcReg, overlaps))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // Monotone partitioning does not generate overlapping regions.
|
|
|
+ }
|
|
|
|
|
|
// Store the result out.
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
|
|
chf.spans[i].reg = srcReg[i];
|
|
|
-
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS);
|
|
|
|
|
|
return true;
|
|
|
}
|
|
|
@@ -1298,12 +1528,12 @@ bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
{
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS);
|
|
|
+ rcScopedTimer timer(ctx, RC_TIMER_BUILD_REGIONS);
|
|
|
|
|
|
const int w = chf.width;
|
|
|
const int h = chf.height;
|
|
|
|
|
|
- rcScopedDelete<unsigned short> buf = (unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount*4, RC_ALLOC_TEMP);
|
|
|
+ rcScopedDelete<unsigned short> buf((unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount*2, RC_ALLOC_TEMP));
|
|
|
if (!buf)
|
|
|
{
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildRegions: Out of memory 'tmp' (%d).", chf.spanCount*4);
|
|
|
@@ -1314,17 +1544,15 @@ bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
|
|
|
const int LOG_NB_STACKS = 3;
|
|
|
const int NB_STACKS = 1 << LOG_NB_STACKS;
|
|
|
- rcIntArray lvlStacks[NB_STACKS];
|
|
|
+ rcTempVector<LevelStackEntry> lvlStacks[NB_STACKS];
|
|
|
for (int i=0; i<NB_STACKS; ++i)
|
|
|
- lvlStacks[i].resize(1024);
|
|
|
+ lvlStacks[i].reserve(256);
|
|
|
|
|
|
- rcIntArray stack(1024);
|
|
|
- rcIntArray visited(1024);
|
|
|
+ rcTempVector<LevelStackEntry> stack;
|
|
|
+ stack.reserve(256);
|
|
|
|
|
|
unsigned short* srcReg = buf;
|
|
|
unsigned short* srcDist = buf+chf.spanCount;
|
|
|
- unsigned short* dstReg = buf+chf.spanCount*2;
|
|
|
- unsigned short* dstDist = buf+chf.spanCount*3;
|
|
|
|
|
|
memset(srcReg, 0, sizeof(unsigned short)*chf.spanCount);
|
|
|
memset(srcDist, 0, sizeof(unsigned short)*chf.spanCount);
|
|
|
@@ -1343,14 +1571,15 @@ bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
// Make sure border will not overflow.
|
|
|
const int bw = rcMin(w, borderSize);
|
|
|
const int bh = rcMin(h, borderSize);
|
|
|
+
|
|
|
// Paint regions
|
|
|
paintRectRegion(0, bw, 0, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++;
|
|
|
paintRectRegion(w-bw, w, 0, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++;
|
|
|
paintRectRegion(0, w, 0, bh, regionId|RC_BORDER_REG, chf, srcReg); regionId++;
|
|
|
paintRectRegion(0, w, h-bh, h, regionId|RC_BORDER_REG, chf, srcReg); regionId++;
|
|
|
-
|
|
|
- chf.borderSize = borderSize;
|
|
|
}
|
|
|
+
|
|
|
+ chf.borderSize = borderSize;
|
|
|
|
|
|
int sId = -1;
|
|
|
while (level > 0)
|
|
|
@@ -1367,60 +1596,217 @@ bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
|
|
|
// ctx->stopTimer(RC_TIMER_DIVIDE_TO_LEVELS);
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS_EXPAND);
|
|
|
-
|
|
|
- // Expand current regions until no empty connected cells found.
|
|
|
- if (expandRegions(expandIters, level, chf, srcReg, srcDist, dstReg, dstDist, lvlStacks[sId], false) != srcReg)
|
|
|
{
|
|
|
- rcSwap(srcReg, dstReg);
|
|
|
- rcSwap(srcDist, dstDist);
|
|
|
+ rcScopedTimer timerExpand(ctx, RC_TIMER_BUILD_REGIONS_EXPAND);
|
|
|
+
|
|
|
+ // Expand current regions until no empty connected cells found.
|
|
|
+ expandRegions(expandIters, level, chf, srcReg, srcDist, lvlStacks[sId], false);
|
|
|
}
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS_EXPAND);
|
|
|
-
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS_FLOOD);
|
|
|
-
|
|
|
- // Mark new regions with IDs.
|
|
|
- for (int j=0; j<lvlStacks[sId].size(); j+=3)
|
|
|
{
|
|
|
- int x = lvlStacks[sId][j];
|
|
|
- int y = lvlStacks[sId][j+1];
|
|
|
- int i = lvlStacks[sId][j+2];
|
|
|
- if (i >= 0 && srcReg[i] == 0)
|
|
|
+ rcScopedTimer timerFloor(ctx, RC_TIMER_BUILD_REGIONS_FLOOD);
|
|
|
+
|
|
|
+ // Mark new regions with IDs.
|
|
|
+ for (int j = 0; j<lvlStacks[sId].size(); j++)
|
|
|
{
|
|
|
- if (floodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack))
|
|
|
- regionId++;
|
|
|
+ LevelStackEntry current = lvlStacks[sId][j];
|
|
|
+ int x = current.x;
|
|
|
+ int y = current.y;
|
|
|
+ int i = current.index;
|
|
|
+ if (i >= 0 && srcReg[i] == 0)
|
|
|
+ {
|
|
|
+ if (floodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack))
|
|
|
+ {
|
|
|
+ if (regionId == 0xFFFF)
|
|
|
+ {
|
|
|
+ ctx->log(RC_LOG_ERROR, "rcBuildRegions: Region ID overflow");
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ regionId++;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FLOOD);
|
|
|
}
|
|
|
|
|
|
// Expand current regions until no empty connected cells found.
|
|
|
- if (expandRegions(expandIters*8, 0, chf, srcReg, srcDist, dstReg, dstDist, stack, true) != srcReg)
|
|
|
+ expandRegions(expandIters*8, 0, chf, srcReg, srcDist, stack, true);
|
|
|
+
|
|
|
+ ctx->stopTimer(RC_TIMER_BUILD_REGIONS_WATERSHED);
|
|
|
+
|
|
|
{
|
|
|
- rcSwap(srcReg, dstReg);
|
|
|
- rcSwap(srcDist, dstDist);
|
|
|
+ rcScopedTimer timerFilter(ctx, RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
+
|
|
|
+ // Merge regions and filter out smalle regions.
|
|
|
+ rcIntArray overlaps;
|
|
|
+ chf.maxRegions = regionId;
|
|
|
+ if (!mergeAndFilterRegions(ctx, minRegionArea, mergeRegionArea, chf.maxRegions, chf, srcReg, overlaps))
|
|
|
+ return false;
|
|
|
+
|
|
|
+ // If overlapping regions were found during merging, split those regions.
|
|
|
+ if (overlaps.size() > 0)
|
|
|
+ {
|
|
|
+ ctx->log(RC_LOG_ERROR, "rcBuildRegions: %d overlapping regions.", overlaps.size());
|
|
|
+ }
|
|
|
}
|
|
|
+
|
|
|
+ // Write the result out.
|
|
|
+ for (int i = 0; i < chf.spanCount; ++i)
|
|
|
+ chf.spans[i].reg = srcReg[i];
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS_WATERSHED);
|
|
|
+ return true;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+bool rcBuildLayerRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
+ const int borderSize, const int minRegionArea)
|
|
|
+{
|
|
|
+ rcAssert(ctx);
|
|
|
+
|
|
|
+ rcScopedTimer timer(ctx, RC_TIMER_BUILD_REGIONS);
|
|
|
|
|
|
- ctx->startTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
+ const int w = chf.width;
|
|
|
+ const int h = chf.height;
|
|
|
+ unsigned short id = 1;
|
|
|
|
|
|
- // Filter out small regions.
|
|
|
- chf.maxRegions = regionId;
|
|
|
- if (!filterSmallRegions(ctx, minRegionArea, mergeRegionArea, chf.maxRegions, chf, srcReg))
|
|
|
+ rcScopedDelete<unsigned short> srcReg((unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_TEMP));
|
|
|
+ if (!srcReg)
|
|
|
+ {
|
|
|
+ ctx->log(RC_LOG_ERROR, "rcBuildLayerRegions: Out of memory 'src' (%d).", chf.spanCount);
|
|
|
return false;
|
|
|
+ }
|
|
|
+ memset(srcReg,0,sizeof(unsigned short)*chf.spanCount);
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
+ const int nsweeps = rcMax(chf.width,chf.height);
|
|
|
+ rcScopedDelete<rcSweepSpan> sweeps((rcSweepSpan*)rcAlloc(sizeof(rcSweepSpan)*nsweeps, RC_ALLOC_TEMP));
|
|
|
+ if (!sweeps)
|
|
|
+ {
|
|
|
+ ctx->log(RC_LOG_ERROR, "rcBuildLayerRegions: Out of memory 'sweeps' (%d).", nsweeps);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Mark border regions.
|
|
|
+ if (borderSize > 0)
|
|
|
+ {
|
|
|
+ // Make sure border will not overflow.
|
|
|
+ const int bw = rcMin(w, borderSize);
|
|
|
+ const int bh = rcMin(h, borderSize);
|
|
|
+ // Paint regions
|
|
|
+ paintRectRegion(0, bw, 0, h, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
+ paintRectRegion(w-bw, w, 0, h, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
+ paintRectRegion(0, w, 0, bh, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
+ paintRectRegion(0, w, h-bh, h, id|RC_BORDER_REG, chf, srcReg); id++;
|
|
|
+ }
|
|
|
+
|
|
|
+ chf.borderSize = borderSize;
|
|
|
+
|
|
|
+ rcIntArray prev(256);
|
|
|
+
|
|
|
+ // Sweep one line at a time.
|
|
|
+ for (int y = borderSize; y < h-borderSize; ++y)
|
|
|
+ {
|
|
|
+ // Collect spans from this row.
|
|
|
+ prev.resize(id+1);
|
|
|
+ memset(&prev[0],0,sizeof(int)*id);
|
|
|
+ unsigned short rid = 1;
|
|
|
|
|
|
- // Write the result out.
|
|
|
+ for (int x = borderSize; x < w-borderSize; ++x)
|
|
|
+ {
|
|
|
+ const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
+
|
|
|
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
+ {
|
|
|
+ const rcCompactSpan& s = chf.spans[i];
|
|
|
+ if (chf.areas[i] == RC_NULL_AREA) continue;
|
|
|
+
|
|
|
+ // -x
|
|
|
+ unsigned short previd = 0;
|
|
|
+ if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
|
|
|
+ {
|
|
|
+ const int ax = x + rcGetDirOffsetX(0);
|
|
|
+ const int ay = y + rcGetDirOffsetY(0);
|
|
|
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 0);
|
|
|
+ if ((srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
|
|
|
+ previd = srcReg[ai];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!previd)
|
|
|
+ {
|
|
|
+ previd = rid++;
|
|
|
+ sweeps[previd].rid = previd;
|
|
|
+ sweeps[previd].ns = 0;
|
|
|
+ sweeps[previd].nei = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ // -y
|
|
|
+ if (rcGetCon(s,3) != RC_NOT_CONNECTED)
|
|
|
+ {
|
|
|
+ const int ax = x + rcGetDirOffsetX(3);
|
|
|
+ const int ay = y + rcGetDirOffsetY(3);
|
|
|
+ const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 3);
|
|
|
+ if (srcReg[ai] && (srcReg[ai] & RC_BORDER_REG) == 0 && chf.areas[i] == chf.areas[ai])
|
|
|
+ {
|
|
|
+ unsigned short nr = srcReg[ai];
|
|
|
+ if (!sweeps[previd].nei || sweeps[previd].nei == nr)
|
|
|
+ {
|
|
|
+ sweeps[previd].nei = nr;
|
|
|
+ sweeps[previd].ns++;
|
|
|
+ prev[nr]++;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sweeps[previd].nei = RC_NULL_NEI;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ srcReg[i] = previd;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Create unique ID.
|
|
|
+ for (int i = 1; i < rid; ++i)
|
|
|
+ {
|
|
|
+ if (sweeps[i].nei != RC_NULL_NEI && sweeps[i].nei != 0 &&
|
|
|
+ prev[sweeps[i].nei] == (int)sweeps[i].ns)
|
|
|
+ {
|
|
|
+ sweeps[i].id = sweeps[i].nei;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ sweeps[i].id = id++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Remap IDs
|
|
|
+ for (int x = borderSize; x < w-borderSize; ++x)
|
|
|
+ {
|
|
|
+ const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
+
|
|
|
+ for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
+ {
|
|
|
+ if (srcReg[i] > 0 && srcReg[i] < rid)
|
|
|
+ srcReg[i] = sweeps[srcReg[i]].id;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ {
|
|
|
+ rcScopedTimer timerFilter(ctx, RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
+
|
|
|
+ // Merge monotone regions to layers and remove small regions.
|
|
|
+ chf.maxRegions = id;
|
|
|
+ if (!mergeAndFilterLayerRegions(ctx, minRegionArea, chf.maxRegions, chf, srcReg))
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ // Store the result out.
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
|
|
chf.spans[i].reg = srcReg[i];
|
|
|
|
|
|
- ctx->stopTimer(RC_TIMER_BUILD_REGIONS);
|
|
|
-
|
|
|
return true;
|
|
|
}
|
|
|
-
|
|
|
-
|