diff --git a/Recast/Source/RecastRegion.cpp b/Recast/Source/RecastRegion.cpp index 7d7c8df..2b721a7 100644 --- a/Recast/Source/RecastRegion.cpp +++ b/Recast/Source/RecastRegion.cpp @@ -27,6 +27,16 @@ #include "RecastAssert.h" #include +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 +255,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& 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 +272,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]; @@ -332,9 +342,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)); } } } @@ -344,7 +352,8 @@ static bool floodRegion(int x, int y, int i, } // Struct to keep track of entries in the region table that have been changed. -struct DirtyEntry { +struct DirtyEntry +{ DirtyEntry(int index_, unsigned short region_, unsigned short distance2_) : index(index_), region(region_), distance2(distance2_) {} int index; @@ -354,7 +363,7 @@ struct DirtyEntry { static void expandRegions(int maxIter, unsigned short level, rcCompactHeightfield& chf, unsigned short* srcReg, unsigned short* srcDist, - rcIntArray& stack, + rcTempVector& stack, bool fillStack) { const int w = chf.width; @@ -363,7 +372,7 @@ static void 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) @@ -373,9 +382,7 @@ static void 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)); } } } @@ -384,11 +391,11 @@ static void 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 0) @@ -465,7 +472,7 @@ static void expandRegions(int maxIter, unsigned short level, static void sortCellsByLevel(unsigned short startLevel, rcCompactHeightfield& chf, const unsigned short* srcReg, - unsigned int nbStacks, rcIntArray* stacks, + unsigned int nbStacks, rcTempVector* stacks, unsigned short loglevelsPerStack) // the levels per stack (2 in our case) as a bit shift { const int w = chf.width; @@ -473,7 +480,7 @@ static void sortCellsByLevel(unsigned short startLevel, startLevel = startLevel >> loglevelsPerStack; for (unsigned int j=0; j& srcStack, + rcTempVector& dstStack, const unsigned short* srcReg) { - for (int j=0; j lvlStacks[NB_STACKS]; for (int i=0; i stack; + stack.reserve(256); unsigned short* srcReg = buf; unsigned short* srcDist = buf+chf.spanCount; @@ -1615,11 +1619,12 @@ bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf, rcScopedTimer timerFloor(ctx, RC_TIMER_BUILD_REGIONS_FLOOD); // Mark new regions with IDs. - for (int j = 0; j= 0 && srcReg[i] == 0) { if (floodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack)) @@ -1805,9 +1810,8 @@ bool rcBuildLayerRegions(rcContext* ctx, rcCompactHeightfield& chf, rcScopedTimer timerFilter(ctx, RC_TIMER_BUILD_REGIONS_FILTER); // Merge monotone regions to layers and remove small regions. - rcIntArray overlaps; chf.maxRegions = id; - if (!mergeAndFilterLayerRegions(ctx, minRegionArea, chf.maxRegions, chf, srcReg, overlaps)) + if (!mergeAndFilterLayerRegions(ctx, minRegionArea, chf.maxRegions, chf, srcReg)) return false; }