2010-05-19 01:01:21 +00:00
|
|
|
//
|
2011-09-29 21:38:57 +00:00
|
|
|
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
|
2010-05-19 01:01:21 +00:00
|
|
|
//
|
|
|
|
// This software is provided 'as-is', without any express or implied
|
|
|
|
// warranty. In no event will the authors be held liable for any damages
|
|
|
|
// arising from the use of this software.
|
|
|
|
// Permission is granted to anyone to use this software for any purpose,
|
|
|
|
// including commercial applications, and to alter it and redistribute it
|
|
|
|
// freely, subject to the following restrictions:
|
|
|
|
// 1. The origin of this software must not be misrepresented; you must not
|
|
|
|
// claim that you wrote the original software. If you use this software
|
|
|
|
// in a product, an acknowledgment in the product documentation would be
|
|
|
|
// appreciated but is not required.
|
|
|
|
// 2. Altered source versions must be plainly marked as such, and must not be
|
|
|
|
// misrepresented as being the original software.
|
|
|
|
// 3. This notice may not be removed or altered from any source distribution.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <float.h>
|
|
|
|
#define _USE_MATH_DEFINES
|
|
|
|
#include <math.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "Recast.h"
|
2011-09-29 21:38:57 +00:00
|
|
|
#include "RecastAlloc.h"
|
|
|
|
#include "RecastAssert.h"
|
|
|
|
#include <new>
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static void calculateDistanceField(rcCompactHeightfield& chf, unsigned short* src, unsigned short& maxDist)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int w = chf.width;
|
|
|
|
const int h = chf.height;
|
|
|
|
|
|
|
|
// Init distance and points.
|
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
|
|
|
src[i] = 0xffff;
|
|
|
|
|
|
|
|
// Mark boundary cells.
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++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];
|
2011-09-29 21:38:57 +00:00
|
|
|
const unsigned char area = chf.areas[i];
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
int nc = 0;
|
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
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);
|
|
|
|
if (area == chf.areas[ai])
|
|
|
|
nc++;
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
if (nc != 4)
|
|
|
|
src[i] = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Pass 1
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++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];
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, 0) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
// (-1,0)
|
|
|
|
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);
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
if (src[ai]+2 < src[i])
|
|
|
|
src[i] = src[ai]+2;
|
|
|
|
|
|
|
|
// (-1,-1)
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, 3) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int aax = ax + rcGetDirOffsetX(3);
|
|
|
|
const int aay = ay + rcGetDirOffsetY(3);
|
|
|
|
const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 3);
|
|
|
|
if (src[aai]+3 < src[i])
|
|
|
|
src[i] = src[aai]+3;
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, 3) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
// (0,-1)
|
|
|
|
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);
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
if (src[ai]+2 < src[i])
|
|
|
|
src[i] = src[ai]+2;
|
|
|
|
|
|
|
|
// (1,-1)
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, 2) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int aax = ax + rcGetDirOffsetX(2);
|
|
|
|
const int aay = ay + rcGetDirOffsetY(2);
|
|
|
|
const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 2);
|
|
|
|
if (src[aai]+3 < src[i])
|
|
|
|
src[i] = src[aai]+3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pass 2
|
|
|
|
for (int y = h-1; y >= 0; --y)
|
|
|
|
{
|
|
|
|
for (int x = w-1; x >= 0; --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];
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, 2) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
// (1,0)
|
|
|
|
const int ax = x + rcGetDirOffsetX(2);
|
|
|
|
const int ay = y + rcGetDirOffsetY(2);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 2);
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
if (src[ai]+2 < src[i])
|
|
|
|
src[i] = src[ai]+2;
|
|
|
|
|
|
|
|
// (1,1)
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, 1) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int aax = ax + rcGetDirOffsetX(1);
|
|
|
|
const int aay = ay + rcGetDirOffsetY(1);
|
|
|
|
const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 1);
|
|
|
|
if (src[aai]+3 < src[i])
|
|
|
|
src[i] = src[aai]+3;
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, 1) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
// (0,1)
|
|
|
|
const int ax = x + rcGetDirOffsetX(1);
|
|
|
|
const int ay = y + rcGetDirOffsetY(1);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(s, 1);
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
if (src[ai]+2 < src[i])
|
|
|
|
src[i] = src[ai]+2;
|
|
|
|
|
|
|
|
// (-1,1)
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, 0) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int aax = ax + rcGetDirOffsetX(0);
|
|
|
|
const int aay = ay + rcGetDirOffsetY(0);
|
|
|
|
const int aai = (int)chf.cells[aax+aay*w].index + rcGetCon(as, 0);
|
|
|
|
if (src[aai]+3 < src[i])
|
|
|
|
src[i] = src[aai]+3;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
maxDist = 0;
|
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
|
|
|
maxDist = rcMax(src[i], maxDist);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned short* boxBlur(rcCompactHeightfield& chf, int thr,
|
|
|
|
unsigned short* src, unsigned short* dst)
|
|
|
|
{
|
|
|
|
const int w = chf.width;
|
|
|
|
const int h = chf.height;
|
|
|
|
|
|
|
|
thr *= 2;
|
|
|
|
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++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];
|
2011-09-29 21:38:57 +00:00
|
|
|
const unsigned short cd = src[i];
|
2010-05-19 01:01:21 +00:00
|
|
|
if (cd <= thr)
|
|
|
|
{
|
|
|
|
dst[i] = cd;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
int d = (int)cd;
|
2010-05-19 01:01:21 +00:00
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
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);
|
|
|
|
d += (int)src[ai];
|
|
|
|
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
const int dir2 = (dir+1) & 0x3;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, dir2) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax2 = ax + rcGetDirOffsetX(dir2);
|
|
|
|
const int ay2 = ay + rcGetDirOffsetY(dir2);
|
|
|
|
const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
|
|
|
|
d += (int)src[ai2];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d += cd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
d += cd*2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dst[i] = (unsigned short)((d+5)/9);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return dst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool floodRegion(int x, int y, int i,
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short level, unsigned short r,
|
2010-05-19 01:01:21 +00:00
|
|
|
rcCompactHeightfield& chf,
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* srcReg, unsigned short* srcDist,
|
2010-05-19 01:01:21 +00:00
|
|
|
rcIntArray& stack)
|
|
|
|
{
|
|
|
|
const int w = chf.width;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
const unsigned char area = chf.areas[i];
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Flood fill mark region.
|
|
|
|
stack.resize(0);
|
|
|
|
stack.push((int)x);
|
|
|
|
stack.push((int)y);
|
|
|
|
stack.push((int)i);
|
2011-09-29 21:38:57 +00:00
|
|
|
srcReg[i] = r;
|
|
|
|
srcDist[i] = 0;
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short lev = level >= 2 ? level-2 : 0;
|
2010-05-19 01:01:21 +00:00
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
while (stack.size() > 0)
|
|
|
|
{
|
|
|
|
int ci = stack.pop();
|
|
|
|
int cy = stack.pop();
|
|
|
|
int cx = stack.pop();
|
|
|
|
|
|
|
|
const rcCompactSpan& cs = chf.spans[ci];
|
|
|
|
|
|
|
|
// Check if any of the neighbours already have a valid region set.
|
|
|
|
unsigned short ar = 0;
|
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
|
|
|
// 8 connected
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(cs, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax = cx + rcGetDirOffsetX(dir);
|
|
|
|
const int ay = cy + rcGetDirOffsetY(dir);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.areas[ai] != area)
|
|
|
|
continue;
|
|
|
|
unsigned short nr = srcReg[ai];
|
|
|
|
if (nr & RC_BORDER_REG) // Do not take borders into account.
|
|
|
|
continue;
|
2010-05-19 01:01:21 +00:00
|
|
|
if (nr != 0 && nr != r)
|
|
|
|
ar = nr;
|
|
|
|
|
|
|
|
const rcCompactSpan& as = chf.spans[ai];
|
|
|
|
|
|
|
|
const int dir2 = (dir+1) & 0x3;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(as, dir2) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax2 = ax + rcGetDirOffsetX(dir2);
|
|
|
|
const int ay2 = ay + rcGetDirOffsetY(dir2);
|
|
|
|
const int ai2 = (int)chf.cells[ax2+ay2*w].index + rcGetCon(as, dir2);
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.areas[ai2] != area)
|
|
|
|
continue;
|
|
|
|
unsigned short nr = srcReg[ai2];
|
2010-05-19 01:01:21 +00:00
|
|
|
if (nr != 0 && nr != r)
|
|
|
|
ar = nr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ar != 0)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
srcReg[ci] = 0;
|
2010-05-19 01:01:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
// Expand neighbours.
|
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(cs, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax = cx + rcGetDirOffsetX(dir);
|
|
|
|
const int ay = cy + rcGetDirOffsetY(dir);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*w].index + rcGetCon(cs, dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.areas[ai] != area)
|
|
|
|
continue;
|
|
|
|
if (chf.dist[ai] >= lev && srcReg[ai] == 0)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
srcReg[ai] = r;
|
|
|
|
srcDist[ai] = 0;
|
|
|
|
stack.push(ax);
|
|
|
|
stack.push(ay);
|
|
|
|
stack.push(ai);
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return count > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static unsigned short* expandRegions(int maxIter, unsigned short level,
|
|
|
|
rcCompactHeightfield& chf,
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* srcReg, unsigned short* srcDist,
|
|
|
|
unsigned short* dstReg, unsigned short* dstDist,
|
2010-05-19 01:01:21 +00:00
|
|
|
rcIntArray& stack)
|
|
|
|
{
|
|
|
|
const int w = chf.width;
|
|
|
|
const int h = chf.height;
|
|
|
|
|
|
|
|
// Find cells revealed by the raised level.
|
|
|
|
stack.resize(0);
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++x)
|
|
|
|
{
|
|
|
|
const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
|
for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.dist[i] >= level && srcReg[i] == 0 && chf.areas[i] != RC_NULL_AREA)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
stack.push(x);
|
|
|
|
stack.push(y);
|
|
|
|
stack.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int iter = 0;
|
|
|
|
while (stack.size() > 0)
|
|
|
|
{
|
|
|
|
int failed = 0;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
memcpy(dstReg, srcReg, sizeof(unsigned short)*chf.spanCount);
|
|
|
|
memcpy(dstDist, srcDist, sizeof(unsigned short)*chf.spanCount);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
for (int j = 0; j < stack.size(); j += 3)
|
|
|
|
{
|
|
|
|
int x = stack[j+0];
|
|
|
|
int y = stack[j+1];
|
|
|
|
int i = stack[j+2];
|
|
|
|
if (i < 0)
|
|
|
|
{
|
|
|
|
failed++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short r = srcReg[i];
|
2010-05-19 01:01:21 +00:00
|
|
|
unsigned short d2 = 0xffff;
|
2011-09-29 21:38:57 +00:00
|
|
|
const unsigned char area = chf.areas[i];
|
2010-05-19 01:01:21 +00:00
|
|
|
const rcCompactSpan& s = chf.spans[i];
|
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, dir) == RC_NOT_CONNECTED) continue;
|
2010-05-19 01:01:21 +00:00
|
|
|
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);
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.areas[ai] != area) continue;
|
|
|
|
if (srcReg[ai] > 0 && (srcReg[ai] & RC_BORDER_REG) == 0)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if ((int)srcDist[ai]+2 < (int)d2)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
r = srcReg[ai];
|
|
|
|
d2 = srcDist[ai]+2;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (r)
|
|
|
|
{
|
|
|
|
stack[j+2] = -1; // mark as used
|
2011-09-29 21:38:57 +00:00
|
|
|
dstReg[i] = r;
|
|
|
|
dstDist[i] = d2;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
failed++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// rcSwap source and dest.
|
2011-09-29 21:38:57 +00:00
|
|
|
rcSwap(srcReg, dstReg);
|
|
|
|
rcSwap(srcDist, dstDist);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
if (failed*3 == stack.size())
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (level > 0)
|
|
|
|
{
|
|
|
|
++iter;
|
|
|
|
if (iter >= maxIter)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
return srcReg;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct rcRegion
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
inline rcRegion(unsigned short i) :
|
|
|
|
spanCount(0),
|
|
|
|
id(i),
|
|
|
|
areaType(0),
|
|
|
|
remap(false),
|
|
|
|
visited(false)
|
|
|
|
{}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
int spanCount; // Number of spans belonging to this region
|
|
|
|
unsigned short id; // ID of the region
|
|
|
|
unsigned char areaType; // Are type.
|
2010-05-19 01:01:21 +00:00
|
|
|
bool remap;
|
2011-09-29 21:38:57 +00:00
|
|
|
bool visited;
|
2010-05-19 01:01:21 +00:00
|
|
|
rcIntArray connections;
|
|
|
|
rcIntArray floors;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void removeAdjacentNeighbours(rcRegion& reg)
|
|
|
|
{
|
|
|
|
// Remove adjacent duplicates.
|
|
|
|
for (int i = 0; i < reg.connections.size() && reg.connections.size() > 1; )
|
|
|
|
{
|
|
|
|
int ni = (i+1) % reg.connections.size();
|
|
|
|
if (reg.connections[i] == reg.connections[ni])
|
|
|
|
{
|
|
|
|
// Remove duplicate
|
|
|
|
for (int j = i; j < reg.connections.size()-1; ++j)
|
|
|
|
reg.connections[j] = reg.connections[j+1];
|
|
|
|
reg.connections.pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void replaceNeighbour(rcRegion& reg, unsigned short oldId, unsigned short newId)
|
|
|
|
{
|
|
|
|
bool neiChanged = false;
|
|
|
|
for (int i = 0; i < reg.connections.size(); ++i)
|
|
|
|
{
|
|
|
|
if (reg.connections[i] == oldId)
|
|
|
|
{
|
|
|
|
reg.connections[i] = newId;
|
|
|
|
neiChanged = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int i = 0; i < reg.floors.size(); ++i)
|
|
|
|
{
|
|
|
|
if (reg.floors[i] == oldId)
|
|
|
|
reg.floors[i] = newId;
|
|
|
|
}
|
|
|
|
if (neiChanged)
|
|
|
|
removeAdjacentNeighbours(reg);
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static bool canMergeWithRegion(const rcRegion& rega, const rcRegion& regb)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rega.areaType != regb.areaType)
|
|
|
|
return false;
|
2010-05-19 01:01:21 +00:00
|
|
|
int n = 0;
|
2011-09-29 21:38:57 +00:00
|
|
|
for (int i = 0; i < rega.connections.size(); ++i)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rega.connections[i] == regb.id)
|
2010-05-19 01:01:21 +00:00
|
|
|
n++;
|
|
|
|
}
|
|
|
|
if (n > 1)
|
|
|
|
return false;
|
2011-09-29 21:38:57 +00:00
|
|
|
for (int i = 0; i < rega.floors.size(); ++i)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rega.floors[i] == regb.id)
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static void addUniqueFloorRegion(rcRegion& reg, int n)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
for (int i = 0; i < reg.floors.size(); ++i)
|
|
|
|
if (reg.floors[i] == n)
|
|
|
|
return;
|
|
|
|
reg.floors.push(n);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool mergeRegions(rcRegion& rega, rcRegion& regb)
|
|
|
|
{
|
|
|
|
unsigned short aid = rega.id;
|
|
|
|
unsigned short bid = regb.id;
|
|
|
|
|
|
|
|
// Duplicate current neighbourhood.
|
|
|
|
rcIntArray acon;
|
|
|
|
acon.resize(rega.connections.size());
|
|
|
|
for (int i = 0; i < rega.connections.size(); ++i)
|
|
|
|
acon[i] = rega.connections[i];
|
|
|
|
rcIntArray& bcon = regb.connections;
|
|
|
|
|
|
|
|
// Find insertion point on A.
|
|
|
|
int insa = -1;
|
|
|
|
for (int i = 0; i < acon.size(); ++i)
|
|
|
|
{
|
|
|
|
if (acon[i] == bid)
|
|
|
|
{
|
|
|
|
insa = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (insa == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Find insertion point on B.
|
|
|
|
int insb = -1;
|
|
|
|
for (int i = 0; i < bcon.size(); ++i)
|
|
|
|
{
|
|
|
|
if (bcon[i] == aid)
|
|
|
|
{
|
|
|
|
insb = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (insb == -1)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
// Merge neighbours.
|
|
|
|
rega.connections.resize(0);
|
|
|
|
for (int i = 0, ni = acon.size(); i < ni-1; ++i)
|
|
|
|
rega.connections.push(acon[(insa+1+i) % ni]);
|
|
|
|
|
|
|
|
for (int i = 0, ni = bcon.size(); i < ni-1; ++i)
|
|
|
|
rega.connections.push(bcon[(insb+1+i) % ni]);
|
|
|
|
|
|
|
|
removeAdjacentNeighbours(rega);
|
|
|
|
|
|
|
|
for (int j = 0; j < regb.floors.size(); ++j)
|
|
|
|
addUniqueFloorRegion(rega, regb.floors[j]);
|
2011-09-29 21:38:57 +00:00
|
|
|
rega.spanCount += regb.spanCount;
|
|
|
|
regb.spanCount = 0;
|
2010-05-19 01:01:21 +00:00
|
|
|
regb.connections.resize(0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool isRegionConnectedToBorder(const rcRegion& reg)
|
|
|
|
{
|
|
|
|
// Region is connected to border if
|
|
|
|
// one of the neighbours is null id.
|
|
|
|
for (int i = 0; i < reg.connections.size(); ++i)
|
|
|
|
{
|
|
|
|
if (reg.connections[i] == 0)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static bool isSolidEdge(rcCompactHeightfield& chf, unsigned short* srcReg,
|
2010-05-19 01:01:21 +00:00
|
|
|
int x, int y, int i, int dir)
|
|
|
|
{
|
|
|
|
const rcCompactSpan& s = chf.spans[i];
|
|
|
|
unsigned short r = 0;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax = x + rcGetDirOffsetX(dir);
|
|
|
|
const int ay = y + rcGetDirOffsetY(dir);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
r = srcReg[ai];
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
if (r == srcReg[i])
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void walkContour(int x, int y, int i, int dir,
|
|
|
|
rcCompactHeightfield& chf,
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* srcReg,
|
2010-05-19 01:01:21 +00:00
|
|
|
rcIntArray& cont)
|
|
|
|
{
|
|
|
|
int startDir = dir;
|
|
|
|
int starti = i;
|
|
|
|
|
|
|
|
const rcCompactSpan& ss = chf.spans[i];
|
|
|
|
unsigned short curReg = 0;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(ss, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax = x + rcGetDirOffsetX(dir);
|
|
|
|
const int ay = y + rcGetDirOffsetY(dir);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(ss, dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
curReg = srcReg[ai];
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
cont.push(curReg);
|
|
|
|
|
|
|
|
int iter = 0;
|
|
|
|
while (++iter < 40000)
|
|
|
|
{
|
|
|
|
const rcCompactSpan& s = chf.spans[i];
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
if (isSolidEdge(chf, srcReg, x, y, i, dir))
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
// Choose the edge corner
|
|
|
|
unsigned short r = 0;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int ax = x + rcGetDirOffsetX(dir);
|
|
|
|
const int ay = y + rcGetDirOffsetY(dir);
|
|
|
|
const int ai = (int)chf.cells[ax+ay*chf.width].index + rcGetCon(s, dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
r = srcReg[ai];
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
if (r != curReg)
|
|
|
|
{
|
|
|
|
curReg = r;
|
|
|
|
cont.push(curReg);
|
|
|
|
}
|
|
|
|
|
|
|
|
dir = (dir+1) & 0x3; // Rotate CW
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
int ni = -1;
|
|
|
|
const int nx = x + rcGetDirOffsetX(dir);
|
|
|
|
const int ny = y + rcGetDirOffsetY(dir);
|
2011-09-29 21:38:57 +00:00
|
|
|
if (rcGetCon(s, dir) != RC_NOT_CONNECTED)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const rcCompactCell& nc = chf.cells[nx+ny*chf.width];
|
|
|
|
ni = (int)nc.index + rcGetCon(s, dir);
|
|
|
|
}
|
|
|
|
if (ni == -1)
|
|
|
|
{
|
|
|
|
// Should not happen.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
x = nx;
|
|
|
|
y = ny;
|
|
|
|
i = ni;
|
|
|
|
dir = (dir+3) & 0x3; // Rotate CCW
|
|
|
|
}
|
|
|
|
|
|
|
|
if (starti == i && startDir == dir)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove adjacent duplicates.
|
|
|
|
if (cont.size() > 1)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < cont.size(); )
|
|
|
|
{
|
|
|
|
int ni = (i+1) % cont.size();
|
|
|
|
if (cont[i] == cont[ni])
|
|
|
|
{
|
|
|
|
for (int j = i; j < cont.size()-1; ++j)
|
|
|
|
cont[j] = cont[j+1];
|
|
|
|
cont.pop();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static bool filterSmallRegions(rcContext* ctx, int minRegionArea, int mergeRegionSize,
|
2010-05-19 01:01:21 +00:00
|
|
|
unsigned short& maxRegionId,
|
|
|
|
rcCompactHeightfield& chf,
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* srcReg)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
const int w = chf.width;
|
|
|
|
const int h = chf.height;
|
2011-09-29 21:38:57 +00:00
|
|
|
|
|
|
|
const int nreg = maxRegionId+1;
|
|
|
|
rcRegion* regions = (rcRegion*)rcAlloc(sizeof(rcRegion)*nreg, RC_ALLOC_TEMP);
|
2010-05-19 01:01:21 +00:00
|
|
|
if (!regions)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->log(RC_LOG_ERROR, "filterSmallRegions: Out of memory 'regions' (%d).", nreg);
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
// Construct regions
|
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
|
new(®ions[i]) rcRegion((unsigned short)i);
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Find edge of a region and find connections around the contour.
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++x)
|
|
|
|
{
|
|
|
|
const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
|
for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short r = srcReg[i];
|
2010-05-19 01:01:21 +00:00
|
|
|
if (r == 0 || r >= nreg)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
rcRegion& reg = regions[r];
|
2011-09-29 21:38:57 +00:00
|
|
|
reg.spanCount++;
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Update floors.
|
|
|
|
for (int j = (int)c.index; j < ni; ++j)
|
|
|
|
{
|
|
|
|
if (i == j) continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short floorId = srcReg[j];
|
2010-05-19 01:01:21 +00:00
|
|
|
if (floorId == 0 || floorId >= nreg)
|
|
|
|
continue;
|
|
|
|
addUniqueFloorRegion(reg, floorId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Have found contour
|
|
|
|
if (reg.connections.size() > 0)
|
|
|
|
continue;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
reg.areaType = chf.areas[i];
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Check if this cell is next to a border.
|
|
|
|
int ndir = -1;
|
|
|
|
for (int dir = 0; dir < 4; ++dir)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (isSolidEdge(chf, srcReg, x, y, i, dir))
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
ndir = dir;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ndir != -1)
|
|
|
|
{
|
|
|
|
// The cell is at border.
|
|
|
|
// Walk around the contour to find all the neighbours.
|
2011-09-29 21:38:57 +00:00
|
|
|
walkContour(x, y, i, ndir, chf, srcReg, reg.connections);
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
|
|
|
|
// Remove too small regions.
|
|
|
|
rcIntArray stack(32);
|
|
|
|
rcIntArray trace(32);
|
2010-05-19 01:01:21 +00:00
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
|
{
|
|
|
|
rcRegion& reg = regions[i];
|
|
|
|
if (reg.id == 0 || (reg.id & RC_BORDER_REG))
|
2011-09-29 21:38:57 +00:00
|
|
|
continue;
|
|
|
|
if (reg.spanCount == 0)
|
2010-05-19 01:01:21 +00:00
|
|
|
continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (reg.visited)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Count the total size of all the connected regions.
|
|
|
|
// Also keep track of the regions connects to a tile border.
|
|
|
|
bool connectsToBorder = false;
|
|
|
|
int spanCount = 0;
|
|
|
|
stack.resize(0);
|
|
|
|
trace.resize(0);
|
|
|
|
|
|
|
|
reg.visited = true;
|
|
|
|
stack.push(i);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
while (stack.size())
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
// Pop
|
|
|
|
int ri = stack.pop();
|
|
|
|
|
|
|
|
rcRegion& creg = regions[ri];
|
|
|
|
|
|
|
|
spanCount += creg.spanCount;
|
|
|
|
trace.push(ri);
|
|
|
|
|
|
|
|
for (int j = 0; j < creg.connections.size(); ++j)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (creg.connections[j] & RC_BORDER_REG)
|
|
|
|
{
|
|
|
|
connectsToBorder = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
rcRegion& nreg = regions[creg.connections[j]];
|
|
|
|
if (nreg.visited)
|
|
|
|
continue;
|
|
|
|
if (nreg.id == 0 || (nreg.id & RC_BORDER_REG))
|
|
|
|
continue;
|
|
|
|
// Visit
|
|
|
|
stack.push(nreg.id);
|
|
|
|
nreg.visited = true;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
// If the accumulated regions size is too small, remove it.
|
|
|
|
// Do not remove areas which connect to tile borders
|
|
|
|
// as their size cannot be estimated correctly and removing them
|
|
|
|
// can potentially remove necessary areas.
|
|
|
|
if (spanCount < minRegionArea && !connectsToBorder)
|
|
|
|
{
|
|
|
|
// Kill all visited regions.
|
|
|
|
for (int j = 0; j < trace.size(); ++j)
|
|
|
|
{
|
|
|
|
regions[trace[j]].spanCount = 0;
|
|
|
|
regions[trace[j]].id = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Merge too small regions to neighbour regions.
|
|
|
|
int mergeCount = 0 ;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
mergeCount = 0;
|
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
|
{
|
|
|
|
rcRegion& reg = regions[i];
|
|
|
|
if (reg.id == 0 || (reg.id & RC_BORDER_REG))
|
2011-09-29 21:38:57 +00:00
|
|
|
continue;
|
|
|
|
if (reg.spanCount == 0)
|
2010-05-19 01:01:21 +00:00
|
|
|
continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Check to see if the region should be merged.
|
2011-09-29 21:38:57 +00:00
|
|
|
if (reg.spanCount > mergeRegionSize && isRegionConnectedToBorder(reg))
|
2010-05-19 01:01:21 +00:00
|
|
|
continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Small region with more than 1 connection.
|
|
|
|
// Or region which is not connected to a border at all.
|
|
|
|
// Find smallest neighbour region that connects to this one.
|
|
|
|
int smallest = 0xfffffff;
|
|
|
|
unsigned short mergeId = reg.id;
|
|
|
|
for (int j = 0; j < reg.connections.size(); ++j)
|
|
|
|
{
|
|
|
|
if (reg.connections[j] & RC_BORDER_REG) continue;
|
|
|
|
rcRegion& mreg = regions[reg.connections[j]];
|
|
|
|
if (mreg.id == 0 || (mreg.id & RC_BORDER_REG)) continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (mreg.spanCount < smallest &&
|
|
|
|
canMergeWithRegion(reg, mreg) &&
|
|
|
|
canMergeWithRegion(mreg, reg))
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
smallest = mreg.spanCount;
|
2010-05-19 01:01:21 +00:00
|
|
|
mergeId = mreg.id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Found new id.
|
|
|
|
if (mergeId != reg.id)
|
|
|
|
{
|
|
|
|
unsigned short oldId = reg.id;
|
|
|
|
rcRegion& target = regions[mergeId];
|
|
|
|
|
|
|
|
// Merge neighbours.
|
|
|
|
if (mergeRegions(target, reg))
|
|
|
|
{
|
|
|
|
// Fixup regions pointing to current region.
|
|
|
|
for (int j = 0; j < nreg; ++j)
|
|
|
|
{
|
|
|
|
if (regions[j].id == 0 || (regions[j].id & RC_BORDER_REG)) continue;
|
|
|
|
// If another region was already merged into current region
|
|
|
|
// change the nid of the previous region too.
|
|
|
|
if (regions[j].id == oldId)
|
|
|
|
regions[j].id = mergeId;
|
|
|
|
// Replace the current region with the new one if the
|
|
|
|
// current regions is neighbour.
|
|
|
|
replaceNeighbour(regions[j], oldId, mergeId);
|
|
|
|
}
|
|
|
|
mergeCount++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
while (mergeCount > 0);
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Compress region Ids.
|
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
|
{
|
|
|
|
regions[i].remap = false;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (regions[i].id == 0) continue; // Skip nil regions.
|
|
|
|
if (regions[i].id & RC_BORDER_REG) continue; // Skip external regions.
|
2010-05-19 01:01:21 +00:00
|
|
|
regions[i].remap = true;
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
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;
|
2011-09-29 21:38:57 +00:00
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Remap regions.
|
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if ((srcReg[i] & RC_BORDER_REG) == 0)
|
|
|
|
srcReg[i] = regions[srcReg[i]].id;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
for (int i = 0; i < nreg; ++i)
|
|
|
|
regions[i].~rcRegion();
|
|
|
|
rcFree(regions);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
/// @par
|
|
|
|
///
|
|
|
|
/// This is usually the second to the last step in creating a fully built
|
|
|
|
/// compact heightfield. This step is required before regions are built
|
|
|
|
/// using #rcBuildRegions or #rcBuildRegionsMonotone.
|
|
|
|
///
|
|
|
|
/// After this step, the distance data is available via the rcCompactHeightfield::maxDistance
|
|
|
|
/// and rcCompactHeightfield::dist fields.
|
|
|
|
///
|
|
|
|
/// @see rcCompactHeightfield, rcBuildRegions, rcBuildRegionsMonotone
|
|
|
|
bool rcBuildDistanceField(rcContext* ctx, rcCompactHeightfield& chf)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
|
|
ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD);
|
|
|
|
|
|
|
|
if (chf.dist)
|
|
|
|
{
|
|
|
|
rcFree(chf.dist);
|
|
|
|
chf.dist = 0;
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* src = (unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_TEMP);
|
|
|
|
if (!src)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'src' (%d).", chf.spanCount);
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* dst = (unsigned short*)rcAlloc(sizeof(unsigned short)*chf.spanCount, RC_ALLOC_TEMP);
|
|
|
|
if (!dst)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildDistanceField: Out of memory 'dst' (%d).", chf.spanCount);
|
|
|
|
rcFree(src);
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned short maxDist = 0;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD_DIST);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
calculateDistanceField(chf, src, maxDist);
|
2010-05-19 01:01:21 +00:00
|
|
|
chf.maxDistance = maxDist;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD_DIST);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Blur
|
|
|
|
if (boxBlur(chf, 1, src, dst) != src)
|
|
|
|
rcSwap(src, dst);
|
|
|
|
|
|
|
|
// Store distance.
|
2011-09-29 21:38:57 +00:00
|
|
|
chf.dist = src;
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD_BLUR);
|
|
|
|
|
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_DISTANCEFIELD);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
rcFree(dst);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
static void paintRectRegion(int minx, int maxx, int miny, int maxy, unsigned short regId,
|
|
|
|
rcCompactHeightfield& chf, unsigned short* srcReg)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
const int w = chf.width;
|
2010-05-19 01:01:21 +00:00
|
|
|
for (int y = miny; y < maxy; ++y)
|
|
|
|
{
|
|
|
|
for (int x = minx; x < maxx; ++x)
|
|
|
|
{
|
|
|
|
const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
|
for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.areas[i] != RC_NULL_AREA)
|
|
|
|
srcReg[i] = regId;
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
|
|
|
|
static const unsigned short RC_NULL_NEI = 0xffff;
|
|
|
|
|
|
|
|
struct rcSweepSpan
|
|
|
|
{
|
|
|
|
unsigned short rid; // row id
|
|
|
|
unsigned short id; // region id
|
|
|
|
unsigned short ns; // number samples
|
|
|
|
unsigned short nei; // neighbour id
|
|
|
|
};
|
|
|
|
|
|
|
|
/// @par
|
|
|
|
///
|
|
|
|
/// Non-null regions will consist of connected, non-overlapping walkable spans that form a single contour.
|
|
|
|
/// Contours will form simple polygons.
|
|
|
|
///
|
|
|
|
/// If multiple regions form an area that is smaller than @p minRegionArea, then all spans will be
|
|
|
|
/// re-assigned to the zero (null) region.
|
|
|
|
///
|
|
|
|
/// Partitioning can result in smaller than necessary regions. @p mergeRegionArea helps
|
|
|
|
/// reduce unecessarily small regions.
|
|
|
|
///
|
|
|
|
/// See the #rcConfig documentation for more information on the configuration parameters.
|
|
|
|
///
|
|
|
|
/// The region data will be available via the rcCompactHeightfield::maxRegions
|
|
|
|
/// and rcCompactSpan::reg fields.
|
|
|
|
///
|
|
|
|
/// @warning The distance field must be created using #rcBuildDistanceField before attempting to build regions.
|
|
|
|
///
|
|
|
|
/// @see rcCompactHeightfield, rcCompactSpan, rcBuildDistanceField, rcBuildRegionsMonotone, rcConfig
|
|
|
|
bool rcBuildRegionsMonotone(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
|
const int borderSize, const int minRegionArea, const int mergeRegionArea)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
|
|
ctx->startTimer(RC_TIMER_BUILD_REGIONS);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
const int w = chf.width;
|
|
|
|
const int h = chf.height;
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short id = 1;
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
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);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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);
|
|
|
|
if (!sweeps)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildRegionsMonotone: Out of memory 'sweeps' (%d).", nsweeps);
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
2011-09-29 21:38:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
// 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)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
// Collect spans from this row.
|
|
|
|
prev.resize(id+1);
|
|
|
|
memset(&prev[0],0,sizeof(int)*id);
|
|
|
|
unsigned short rid = 1;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// @par
|
|
|
|
///
|
|
|
|
/// Non-null regions will consist of connected, non-overlapping walkable spans that form a single contour.
|
|
|
|
/// Contours will form simple polygons.
|
|
|
|
///
|
|
|
|
/// If multiple regions form an area that is smaller than @p minRegionArea, then all spans will be
|
|
|
|
/// re-assigned to the zero (null) region.
|
|
|
|
///
|
|
|
|
/// Watershed partitioning can result in smaller than necessary regions, especially in diagonal corridors.
|
|
|
|
/// @p mergeRegionArea helps reduce unecessarily small regions.
|
|
|
|
///
|
|
|
|
/// See the #rcConfig documentation for more information on the configuration parameters.
|
|
|
|
///
|
|
|
|
/// The region data will be available via the rcCompactHeightfield::maxRegions
|
|
|
|
/// and rcCompactSpan::reg fields.
|
|
|
|
///
|
|
|
|
/// @warning The distance field must be created using #rcBuildDistanceField before attempting to build regions.
|
|
|
|
///
|
|
|
|
/// @see rcCompactHeightfield, rcCompactSpan, rcBuildDistanceField, rcBuildRegionsMonotone, rcConfig
|
|
|
|
bool rcBuildRegions(rcContext* ctx, rcCompactHeightfield& chf,
|
|
|
|
const int borderSize, const int minRegionArea, const int mergeRegionArea)
|
|
|
|
{
|
|
|
|
rcAssert(ctx);
|
|
|
|
|
|
|
|
ctx->startTimer(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);
|
|
|
|
if (!buf)
|
|
|
|
{
|
|
|
|
ctx->log(RC_LOG_ERROR, "rcBuildRegions: Out of memory 'tmp' (%d).", chf.spanCount*4);
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_REGIONS_WATERSHED);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
rcIntArray stack(1024);
|
|
|
|
rcIntArray visited(1024);
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
unsigned short* srcReg = buf;
|
|
|
|
unsigned short* srcDist = buf+chf.spanCount;
|
|
|
|
unsigned short* dstReg = buf+chf.spanCount*2;
|
|
|
|
unsigned short* dstDist = buf+chf.spanCount*3;
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
memset(srcReg, 0, sizeof(unsigned short)*chf.spanCount);
|
|
|
|
memset(srcDist, 0, sizeof(unsigned short)*chf.spanCount);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
unsigned short regionId = 1;
|
|
|
|
unsigned short level = (chf.maxDistance+1) & ~1;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
// TODO: Figure better formula, expandIters defines how much the
|
|
|
|
// watershed "overflows" and simplifies the regions. Tying it to
|
|
|
|
// agent radius was usually good indication how greedy it could be.
|
|
|
|
// const int expandIters = 4 + walkableRadius * 2;
|
|
|
|
const int expandIters = 8;
|
|
|
|
|
|
|
|
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, 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++;
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
chf.borderSize = borderSize;
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
while (level > 0)
|
2010-05-19 01:01:21 +00:00
|
|
|
{
|
|
|
|
level = level >= 2 ? level-2 : 0;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_REGIONS_EXPAND);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Expand current regions until no empty connected cells found.
|
2011-09-29 21:38:57 +00:00
|
|
|
if (expandRegions(expandIters, level, chf, srcReg, srcDist, dstReg, dstDist, stack) != srcReg)
|
|
|
|
{
|
|
|
|
rcSwap(srcReg, dstReg);
|
|
|
|
rcSwap(srcDist, dstDist);
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS_EXPAND);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_REGIONS_FLOOD);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Mark new regions with IDs.
|
|
|
|
for (int y = 0; y < h; ++y)
|
|
|
|
{
|
|
|
|
for (int x = 0; x < w; ++x)
|
|
|
|
{
|
|
|
|
const rcCompactCell& c = chf.cells[x+y*w];
|
|
|
|
for (int i = (int)c.index, ni = (int)(c.index+c.count); i < ni; ++i)
|
|
|
|
{
|
2011-09-29 21:38:57 +00:00
|
|
|
if (chf.dist[i] < level || srcReg[i] != 0 || chf.areas[i] == RC_NULL_AREA)
|
2010-05-19 01:01:21 +00:00
|
|
|
continue;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (floodRegion(x, y, i, level, regionId, chf, srcReg, srcDist, stack))
|
2010-05-19 01:01:21 +00:00
|
|
|
regionId++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FLOOD);
|
2010-05-19 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expand current regions until no empty connected cells found.
|
2011-09-29 21:38:57 +00:00
|
|
|
if (expandRegions(expandIters*8, 0, chf, srcReg, srcDist, dstReg, dstDist, stack) != srcReg)
|
|
|
|
{
|
|
|
|
rcSwap(srcReg, dstReg);
|
|
|
|
rcSwap(srcDist, dstDist);
|
|
|
|
}
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS_WATERSHED);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->startTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
// Filter out small regions.
|
|
|
|
chf.maxRegions = regionId;
|
2011-09-29 21:38:57 +00:00
|
|
|
if (!filterSmallRegions(ctx, minRegionArea, mergeRegionArea, chf.maxRegions, chf, srcReg))
|
2010-05-19 01:01:21 +00:00
|
|
|
return false;
|
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS_FILTER);
|
|
|
|
|
2010-05-19 01:01:21 +00:00
|
|
|
// Write the result out.
|
|
|
|
for (int i = 0; i < chf.spanCount; ++i)
|
2011-09-29 21:38:57 +00:00
|
|
|
chf.spans[i].reg = srcReg[i];
|
2010-05-19 01:01:21 +00:00
|
|
|
|
2011-09-29 21:38:57 +00:00
|
|
|
ctx->stopTimer(RC_TIMER_BUILD_REGIONS);
|
2010-05-19 01:01:21 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|