mirror of
https://github.com/recastnavigation/recastnavigation.git
synced 2026-11-18 13:56:00 +00:00
Added lean heighfield to store minimal, easy to compress representation of the heightfield. Added temporary obstacle demo.
This commit is contained in:
@@ -396,6 +396,237 @@ bool rcBuildCompactHeightfield(rcContext* ctx, const int walkableHeight, const i
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
rcLeanHeightfield* rcBuildLeanHeightfield(rcContext* ctx, rcHeightfield& hf, const int walkableHeight)
|
||||
{
|
||||
rcAssert(ctx);
|
||||
|
||||
ctx->startTimer(RC_TIMER_BUILD_LEANHEIGHTFIELD);
|
||||
|
||||
const int w = hf.width;
|
||||
const int h = hf.height;
|
||||
const int spanCount = rcGetHeightFieldSpanCount(ctx, hf);
|
||||
|
||||
const int headerSize = rcAlign4(sizeof(rcLeanHeightfield));
|
||||
const int countsSize = rcAlign4(sizeof(unsigned char)*w*h);
|
||||
const int floorsSize = rcAlign4(sizeof(unsigned short)*spanCount);
|
||||
const int areasSize = rcAlign4(sizeof(unsigned char)*spanCount);
|
||||
const int dataSize = headerSize + countsSize + floorsSize + areasSize;
|
||||
|
||||
unsigned char* data = (unsigned char*)rcAlloc(dataSize, RC_ALLOC_PERM);
|
||||
if (!data)
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "rcBuildLeanHeightfield: Out of memory (%d)", dataSize);
|
||||
return 0;
|
||||
}
|
||||
|
||||
rcLeanHeightfield* lhf = (rcLeanHeightfield*)&data[0];
|
||||
unsigned char* counts = (unsigned char*)&data[headerSize];
|
||||
unsigned short* floors = (unsigned short*)&data[headerSize+countsSize];
|
||||
unsigned char* areas = (unsigned char*)&data[headerSize+countsSize+floorsSize];
|
||||
|
||||
// Fill in header.
|
||||
lhf->width = w;
|
||||
lhf->height = h;
|
||||
lhf->spanCount = spanCount;
|
||||
rcVcopy(lhf->bmin, hf.bmin);
|
||||
rcVcopy(lhf->bmax, hf.bmax);
|
||||
lhf->cs = hf.cs;
|
||||
lhf->ch = hf.ch;
|
||||
lhf->size = dataSize;
|
||||
|
||||
memset(counts, 0, w*h);
|
||||
memset(floors, 0, sizeof(unsigned short)*spanCount);
|
||||
memset(areas, RC_NULL_AREA, sizeof(unsigned char)*spanCount);
|
||||
|
||||
const int MAX_HEIGHT = 0xffff;
|
||||
const int MAX_Y = (1<<RC_SPAN_HEIGHT_BITS)-1;
|
||||
const int MAX_H = (1<<(16-RC_SPAN_HEIGHT_BITS))-1;
|
||||
|
||||
// Fill in cells and spans.
|
||||
int idx = 0;
|
||||
for (int y = 0; y < h; ++y)
|
||||
{
|
||||
for (int x = 0; x < w; ++x)
|
||||
{
|
||||
const rcSpan* s = hf.spans[x+y*w];
|
||||
|
||||
unsigned char& count = counts[x+y*w];
|
||||
count = 0;
|
||||
|
||||
while (s)
|
||||
{
|
||||
if (s->area != RC_NULL_AREA)
|
||||
{
|
||||
const int bot = (int)s->smax;
|
||||
const int top = s->next ? (int)s->next->smin : MAX_HEIGHT;
|
||||
const int space = top - bot;
|
||||
unsigned short y = (unsigned short)rcClamp(bot, 0, MAX_Y);
|
||||
// TODO: Make sure 'h' can encode at least walkableClimb worth of values.
|
||||
unsigned short h = 0; // Height stores the space above the walkable height.
|
||||
if (space >= walkableHeight)
|
||||
h = (unsigned short)rcClamp(1+(space - walkableHeight)/2, 0, MAX_H);
|
||||
|
||||
floors[idx] = y | (h << RC_SPAN_HEIGHT_BITS);
|
||||
areas[idx] = s->area;
|
||||
idx++;
|
||||
count++;
|
||||
}
|
||||
s = s->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ctx->stopTimer(RC_TIMER_BUILD_LEANHEIGHTFIELD);
|
||||
|
||||
return lhf;
|
||||
}
|
||||
|
||||
bool rcBuildCompactHeightfield(rcContext* ctx, const int walkableHeight, const int walkableClimb,
|
||||
rcLeanHeightfield& lhf, rcCompactHeightfield& chf)
|
||||
{
|
||||
rcAssert(ctx);
|
||||
|
||||
ctx->startTimer(RC_TIMER_BUILD_COMPACTHEIGHTFIELD);
|
||||
|
||||
const int w = lhf.width;
|
||||
const int h = lhf.height;
|
||||
const int spanCount = lhf.spanCount;
|
||||
|
||||
// Fill in header.
|
||||
chf.width = w;
|
||||
chf.height = h;
|
||||
chf.spanCount = spanCount;
|
||||
chf.walkableHeight = walkableHeight;
|
||||
chf.walkableClimb = walkableClimb;
|
||||
chf.maxRegions = 0;
|
||||
rcVcopy(chf.bmin, lhf.bmin);
|
||||
rcVcopy(chf.bmax, lhf.bmax);
|
||||
chf.bmax[1] += walkableHeight*lhf.ch;
|
||||
chf.cs = lhf.cs;
|
||||
chf.ch = lhf.ch;
|
||||
chf.cells = (rcCompactCell*)rcAlloc(sizeof(rcCompactCell)*w*h, RC_ALLOC_PERM);
|
||||
if (!chf.cells)
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.cells' (%d)", w*h);
|
||||
return false;
|
||||
}
|
||||
memset(chf.cells, 0, sizeof(rcCompactCell)*w*h);
|
||||
chf.spans = (rcCompactSpan*)rcAlloc(sizeof(rcCompactSpan)*spanCount, RC_ALLOC_PERM);
|
||||
if (!chf.spans)
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.spans' (%d)", spanCount);
|
||||
return false;
|
||||
}
|
||||
memset(chf.spans, 0, sizeof(rcCompactSpan)*spanCount);
|
||||
chf.areas = (unsigned char*)rcAlloc(sizeof(unsigned char)*spanCount, RC_ALLOC_PERM);
|
||||
if (!chf.areas)
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.areas' (%d)", spanCount);
|
||||
return false;
|
||||
}
|
||||
memset(chf.areas, RC_NULL_AREA, sizeof(unsigned char)*spanCount);
|
||||
|
||||
const int headerSize = rcAlign4(sizeof(rcLeanHeightfield));
|
||||
const int countsSize = rcAlign4(sizeof(unsigned char)*lhf.width*lhf.height);
|
||||
const int floorsSize = rcAlign4(sizeof(unsigned short)*lhf.spanCount);
|
||||
const unsigned char* data = (const unsigned char*)&lhf;
|
||||
const unsigned char* counts = (const unsigned char*)&data[headerSize];
|
||||
const unsigned short* floors = (const unsigned short*)&data[headerSize+countsSize];
|
||||
const unsigned char* areas = (const unsigned char*)&data[headerSize+countsSize+floorsSize];
|
||||
|
||||
const unsigned short MASK_Y = (1<<RC_SPAN_HEIGHT_BITS)-1;
|
||||
const unsigned short MASK_H = (1<<(16-RC_SPAN_HEIGHT_BITS))-1;
|
||||
|
||||
// Fill in cells and spans.
|
||||
int idx = 0;
|
||||
for (int y = 0; y < chf.height; ++y)
|
||||
{
|
||||
for (int x = 0; x < chf.width; ++x)
|
||||
{
|
||||
const int count = counts[x+y*chf.width];
|
||||
|
||||
rcCompactCell& c = chf.cells[x+y*w];
|
||||
c.index = idx;
|
||||
c.count = count;
|
||||
|
||||
for (int i = idx, ni = idx+count; i < ni; ++i)
|
||||
{
|
||||
unsigned short y = (unsigned short)(floors[i] & MASK_Y);
|
||||
unsigned char h = (unsigned char)((floors[i]>>RC_SPAN_HEIGHT_BITS) & MASK_H);
|
||||
chf.spans[i].y = y;
|
||||
chf.spans[i].h = h == 0 ? 0 : (unsigned char)(walkableHeight + (h-1)*2);
|
||||
chf.areas[i] = areas[i];
|
||||
}
|
||||
|
||||
idx += count;
|
||||
}
|
||||
}
|
||||
|
||||
// Find neighbour connections.
|
||||
const int MAX_LAYERS = RC_NOT_CONNECTED-1;
|
||||
int tooHighNeighbour = 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)
|
||||
{
|
||||
rcCompactSpan& s = chf.spans[i];
|
||||
|
||||
for (int dir = 0; dir < 4; ++dir)
|
||||
{
|
||||
rcSetCon(s, dir, RC_NOT_CONNECTED);
|
||||
const int nx = x + rcGetDirOffsetX(dir);
|
||||
const int ny = y + rcGetDirOffsetY(dir);
|
||||
// First check that the neighbour cell is in bounds.
|
||||
if (nx < 0 || ny < 0 || nx >= w || ny >= h)
|
||||
continue;
|
||||
|
||||
// Iterate over all neighbour spans and check if any of the is
|
||||
// accessible from current cell.
|
||||
const rcCompactCell& nc = chf.cells[nx+ny*w];
|
||||
for (int k = (int)nc.index, nk = (int)(nc.index+nc.count); k < nk; ++k)
|
||||
{
|
||||
const rcCompactSpan& ns = chf.spans[k];
|
||||
const int bot = rcMax(s.y, ns.y);
|
||||
const int top = rcMin(s.y+s.h, ns.y+ns.h);
|
||||
|
||||
// Check that the gap between the spans is walkable,
|
||||
// and that the climb height between the gaps is not too high.
|
||||
if ((top - bot) >= walkableHeight && rcAbs((int)ns.y - (int)s.y) <= walkableClimb)
|
||||
{
|
||||
// Mark direction as walkable.
|
||||
const int idx = k - (int)nc.index;
|
||||
if (idx < 0 || idx > MAX_LAYERS)
|
||||
{
|
||||
tooHighNeighbour = rcMax(tooHighNeighbour, idx);
|
||||
continue;
|
||||
}
|
||||
rcSetCon(s, dir, idx);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tooHighNeighbour > MAX_LAYERS)
|
||||
{
|
||||
ctx->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Heightfield has too many layers %d (max: %d)",
|
||||
tooHighNeighbour, MAX_LAYERS);
|
||||
}
|
||||
|
||||
ctx->stopTimer(RC_TIMER_BUILD_COMPACTHEIGHTFIELD);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
static int getHeightfieldMemoryUsage(const rcHeightfield& hf)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user