text
stringlengths
0
2.2M
for (int i = 0; i < sweepId; ++i)
{
// If the neighbour is set and there is only one continuous connection to it,
// the sweep will be merged with the previous one, else new region is created.
if (sweeps[i].nei != 0xff && (unsigned short)prevCount[sweeps[i].nei] == sweeps[i].ns)
{
sweeps[i].id = sweeps[i].nei;
}
else
{
if (regId == 255)
{
// Region ID's overflow.
return DT_FAILURE | DT_BUFFER_TOO_SMALL;
}
sweeps[i].id = regId++;
}
}
// Remap local sweep ids to region ids.
for (int x = 0; x < w; ++x)
{
const int idx = x+y*w;
if (layer.regs[idx] != 0xff)
layer.regs[idx] = sweeps[layer.regs[idx]].id;
}
}
// Allocate and init layer regions.
const int nregs = (int)regId;
dtFixedArray<dtLayerMonotoneRegion> regs(alloc, nregs);
if (!regs)
return DT_FAILURE | DT_OUT_OF_MEMORY;
memset(regs, 0, sizeof(dtLayerMonotoneRegion)*nregs);
for (int i = 0; i < nregs; ++i)
regs[i].regId = 0xff;
// Find region neighbours.
for (int y = 0; y < h; ++y)
{
for (int x = 0; x < w; ++x)
{
const int idx = x+y*w;
const unsigned char ri = layer.regs[idx];
if (ri == 0xff)
continue;
// Update area.
regs[ri].area++;
regs[ri].areaId = layer.areas[idx];
// Update neighbours
const int ymi = x+(y-1)*w;
if (y > 0 && isConnected(layer, idx, ymi, walkableClimb))
{
const unsigned char rai = layer.regs[ymi];
if (rai != 0xff && rai != ri)
{
addUniqueLast(regs[ri].neis, regs[ri].nneis, rai);
addUniqueLast(regs[rai].neis, regs[rai].nneis, ri);
}
}
}
}
for (int i = 0; i < nregs; ++i)
regs[i].regId = (unsigned char)i;
for (int i = 0; i < nregs; ++i)
{
dtLayerMonotoneRegion& reg = regs[i];
int merge = -1;
int mergea = 0;
for (int j = 0; j < (int)reg.nneis; ++j)
{
const unsigned char nei = reg.neis[j];
dtLayerMonotoneRegion& regn = regs[nei];
if (reg.regId == regn.regId)
continue;
if (reg.areaId != regn.areaId)
continue;
if (regn.area > mergea)
{
if (canMerge(reg.regId, regn.regId, regs, nregs))
{
mergea = regn.area;
merge = (int)nei;
}
}
}
if (merge != -1)
{
const unsigned char oldId = reg.regId;
const unsigned char newId = regs[merge].regId;
for (int j = 0; j < nregs; ++j)
if (regs[j].regId == oldId)
regs[j].regId = newId;
}