virtualx-engine/thirdparty/recastnavigation/Recast/Source/Recast.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

543 lines
13 KiB
C++
Raw Normal View History

2017-02-28 13:10:29 +01:00
//
// Copyright (c) 2009-2010 Mikko Mononen memon@inside.org
//
// 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 "Recast.h"
#include "RecastAlloc.h"
#include "RecastAssert.h"
2017-02-28 13:10:29 +01:00
#include <math.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
namespace
{
/// Allocates and constructs an object of the given type, returning a pointer.
/// @param[in] allocLifetime Allocation lifetime hint
template<typename T>
T* rcNew(const rcAllocHint allocLifetime)
{
T* ptr = (T*)rcAlloc(sizeof(T), allocLifetime);
::new(rcNewTag(), (void*)ptr) T();
return ptr;
}
/// Destroys and frees an object allocated with rcNew.
/// @param[in] ptr The object pointer to delete.
template<typename T>
void rcDelete(T* ptr)
{
if (ptr)
{
ptr->~T();
rcFree((void*)ptr);
}
}
} // anonymous namespace
2017-02-28 13:10:29 +01:00
float rcSqrt(float x)
{
return sqrtf(x);
}
void rcContext::log(const rcLogCategory category, const char* format, ...)
{
if (!m_logEnabled)
{
2017-02-28 13:10:29 +01:00
return;
}
2017-02-28 13:10:29 +01:00
static const int MSG_SIZE = 512;
char msg[MSG_SIZE];
va_list argList;
va_start(argList, format);
int len = vsnprintf(msg, MSG_SIZE, format, argList);
2017-02-28 13:10:29 +01:00
if (len >= MSG_SIZE)
{
len = MSG_SIZE - 1;
msg[MSG_SIZE - 1] = '\0';
const char* errorMessage = "Log message was truncated";
doLog(RC_LOG_ERROR, errorMessage, (int)strlen(errorMessage));
2017-02-28 13:10:29 +01:00
}
va_end(argList);
2017-02-28 13:10:29 +01:00
doLog(category, msg, len);
}
void rcContext::doResetLog()
{
// Defined out of line to fix the weak v-tables warning
}
2017-02-28 13:10:29 +01:00
rcHeightfield* rcAllocHeightfield()
{
return rcNew<rcHeightfield>(RC_ALLOC_PERM);
2017-02-28 13:10:29 +01:00
}
void rcFreeHeightField(rcHeightfield* heightfield)
{
rcDelete(heightfield);
}
2017-02-28 13:10:29 +01:00
rcHeightfield::rcHeightfield()
: width()
, height()
, bmin()
, bmax()
, cs()
, ch()
, spans()
, pools()
, freelist()
2017-02-28 13:10:29 +01:00
{
}
rcHeightfield::~rcHeightfield()
{
// Delete span array.
rcFree(spans);
// Delete span pools.
while (pools)
{
rcSpanPool* next = pools->next;
rcFree(pools);
pools = next;
}
}
rcCompactHeightfield* rcAllocCompactHeightfield()
{
return rcNew<rcCompactHeightfield>(RC_ALLOC_PERM);
2017-02-28 13:10:29 +01:00
}
void rcFreeCompactHeightfield(rcCompactHeightfield* compactHeightfield)
2017-02-28 13:10:29 +01:00
{
rcDelete(compactHeightfield);
2017-02-28 13:10:29 +01:00
}
rcCompactHeightfield::rcCompactHeightfield()
: width()
, height()
, spanCount()
, walkableHeight()
, walkableClimb()
, borderSize()
, maxDistance()
, maxRegions()
, bmin()
, bmax()
, cs()
, ch()
, cells()
, spans()
, dist()
, areas()
2017-02-28 13:10:29 +01:00
{
}
rcCompactHeightfield::~rcCompactHeightfield()
{
rcFree(cells);
rcFree(spans);
rcFree(dist);
rcFree(areas);
2017-02-28 13:10:29 +01:00
}
rcHeightfieldLayerSet* rcAllocHeightfieldLayerSet()
{
return rcNew<rcHeightfieldLayerSet>(RC_ALLOC_PERM);
}
void rcFreeHeightfieldLayerSet(rcHeightfieldLayerSet* layerSet)
2017-02-28 13:10:29 +01:00
{
rcDelete(layerSet);
}
rcHeightfieldLayerSet::rcHeightfieldLayerSet()
: layers()
, nlayers()
{
}
rcHeightfieldLayerSet::~rcHeightfieldLayerSet()
{
for (int i = 0; i < nlayers; ++i)
2017-02-28 13:10:29 +01:00
{
rcFree(layers[i].heights);
rcFree(layers[i].areas);
rcFree(layers[i].cons);
2017-02-28 13:10:29 +01:00
}
rcFree(layers);
2017-02-28 13:10:29 +01:00
}
rcContourSet* rcAllocContourSet()
{
return rcNew<rcContourSet>(RC_ALLOC_PERM);
2017-02-28 13:10:29 +01:00
}
void rcFreeContourSet(rcContourSet* contourSet)
2017-02-28 13:10:29 +01:00
{
rcDelete(contourSet);
}
rcContourSet::rcContourSet()
: conts()
, nconts()
, bmin()
, bmax()
, cs()
, ch()
, width()
, height()
, borderSize()
, maxError()
{
}
rcContourSet::~rcContourSet()
{
for (int i = 0; i < nconts; ++i)
2017-02-28 13:10:29 +01:00
{
rcFree(conts[i].verts);
rcFree(conts[i].rverts);
2017-02-28 13:10:29 +01:00
}
rcFree(conts);
2017-02-28 13:10:29 +01:00
}
rcPolyMesh* rcAllocPolyMesh()
{
return rcNew<rcPolyMesh>(RC_ALLOC_PERM);
2017-02-28 13:10:29 +01:00
}
void rcFreePolyMesh(rcPolyMesh* polyMesh)
2017-02-28 13:10:29 +01:00
{
rcDelete(polyMesh);
}
rcPolyMesh::rcPolyMesh()
: verts()
, polys()
, regs()
, flags()
, areas()
, nverts()
, npolys()
, maxpolys()
, nvp()
, bmin()
, bmax()
, cs()
, ch()
, borderSize()
, maxEdgeError()
{
}
rcPolyMesh::~rcPolyMesh()
{
rcFree(verts);
rcFree(polys);
rcFree(regs);
rcFree(flags);
rcFree(areas);
2017-02-28 13:10:29 +01:00
}
rcPolyMeshDetail* rcAllocPolyMeshDetail()
{
return rcNew<rcPolyMeshDetail>(RC_ALLOC_PERM);
2017-02-28 13:10:29 +01:00
}
void rcFreePolyMeshDetail(rcPolyMeshDetail* detailMesh)
{
if (detailMesh == NULL)
{
return;
}
rcFree(detailMesh->meshes);
rcFree(detailMesh->verts);
rcFree(detailMesh->tris);
rcFree(detailMesh);
}
rcPolyMeshDetail::rcPolyMeshDetail()
: meshes()
, verts()
, tris()
, nmeshes()
, nverts()
, ntris()
2017-02-28 13:10:29 +01:00
{
}
void rcCalcBounds(const float* verts, int numVerts, float* minBounds, float* maxBounds)
2017-02-28 13:10:29 +01:00
{
// Calculate bounding box.
rcVcopy(minBounds, verts);
rcVcopy(maxBounds, verts);
for (int i = 1; i < numVerts; ++i)
2017-02-28 13:10:29 +01:00
{
const float* v = &verts[i * 3];
rcVmin(minBounds, v);
rcVmax(maxBounds, v);
2017-02-28 13:10:29 +01:00
}
}
void rcCalcGridSize(const float* minBounds, const float* maxBounds, const float cellSize, int* sizeX, int* sizeZ)
2017-02-28 13:10:29 +01:00
{
*sizeX = (int)((maxBounds[0] - minBounds[0]) / cellSize + 0.5f);
*sizeZ = (int)((maxBounds[2] - minBounds[2]) / cellSize + 0.5f);
2017-02-28 13:10:29 +01:00
}
bool rcCreateHeightfield(rcContext* context, rcHeightfield& heightfield, int sizeX, int sizeZ,
const float* minBounds, const float* maxBounds,
float cellSize, float cellHeight)
2017-02-28 13:10:29 +01:00
{
rcIgnoreUnused(context);
heightfield.width = sizeX;
heightfield.height = sizeZ;
rcVcopy(heightfield.bmin, minBounds);
rcVcopy(heightfield.bmax, maxBounds);
heightfield.cs = cellSize;
heightfield.ch = cellHeight;
heightfield.spans = (rcSpan**)rcAlloc(sizeof(rcSpan*) * heightfield.width * heightfield.height, RC_ALLOC_PERM);
if (!heightfield.spans)
{
2017-02-28 13:10:29 +01:00
return false;
}
memset(heightfield.spans, 0, sizeof(rcSpan*) * heightfield.width * heightfield.height);
2017-02-28 13:10:29 +01:00
return true;
}
static void calcTriNormal(const float* v0, const float* v1, const float* v2, float* faceNormal)
2017-02-28 13:10:29 +01:00
{
float e0[3], e1[3];
rcVsub(e0, v1, v0);
rcVsub(e1, v2, v0);
rcVcross(faceNormal, e0, e1);
rcVnormalize(faceNormal);
}
void rcMarkWalkableTriangles(rcContext* context, const float walkableSlopeAngle,
const float* verts, const int numVerts,
const int* tris, const int numTris,
unsigned char* triAreaIDs)
{
rcIgnoreUnused(context);
rcIgnoreUnused(numVerts);
const float walkableThr = cosf(walkableSlopeAngle / 180.0f * RC_PI);
2017-02-28 13:10:29 +01:00
float norm[3];
for (int i = 0; i < numTris; ++i)
2017-02-28 13:10:29 +01:00
{
const int* tri = &tris[i * 3];
calcTriNormal(&verts[tri[0] * 3], &verts[tri[1] * 3], &verts[tri[2] * 3], norm);
2017-02-28 13:10:29 +01:00
// Check if the face is walkable.
if (norm[1] > walkableThr)
{
triAreaIDs[i] = RC_WALKABLE_AREA;
}
2017-02-28 13:10:29 +01:00
}
}
void rcClearUnwalkableTriangles(rcContext* context, const float walkableSlopeAngle,
const float* verts, int numVerts,
const int* tris, int numTris,
unsigned char* triAreaIDs)
{
rcIgnoreUnused(context);
rcIgnoreUnused(numVerts);
// The minimum Y value for a face normal of a triangle with a walkable slope.
const float walkableLimitY = cosf(walkableSlopeAngle / 180.0f * RC_PI);
float faceNormal[3];
for (int i = 0; i < numTris; ++i)
2017-02-28 13:10:29 +01:00
{
const int* tri = &tris[i * 3];
calcTriNormal(&verts[tri[0] * 3], &verts[tri[1] * 3], &verts[tri[2] * 3], faceNormal);
2017-02-28 13:10:29 +01:00
// Check if the face is walkable.
if (faceNormal[1] <= walkableLimitY)
{
triAreaIDs[i] = RC_NULL_AREA;
}
2017-02-28 13:10:29 +01:00
}
}
int rcGetHeightFieldSpanCount(rcContext* context, const rcHeightfield& heightfield)
2017-02-28 13:10:29 +01:00
{
rcIgnoreUnused(context);
const int numCols = heightfield.width * heightfield.height;
2017-02-28 13:10:29 +01:00
int spanCount = 0;
for (int columnIndex = 0; columnIndex < numCols; ++columnIndex)
2017-02-28 13:10:29 +01:00
{
for (rcSpan* span = heightfield.spans[columnIndex]; span != NULL; span = span->next)
2017-02-28 13:10:29 +01:00
{
if (span->area != RC_NULL_AREA)
2017-02-28 13:10:29 +01:00
{
spanCount++;
2017-02-28 13:10:29 +01:00
}
}
}
return spanCount;
}
bool rcBuildCompactHeightfield(rcContext* context, const int walkableHeight, const int walkableClimb,
const rcHeightfield& heightfield, rcCompactHeightfield& compactHeightfield)
2017-02-28 13:10:29 +01:00
{
rcAssert(context);
rcScopedTimer timer(context, RC_TIMER_BUILD_COMPACTHEIGHTFIELD);
const int xSize = heightfield.width;
const int zSize = heightfield.height;
const int spanCount = rcGetHeightFieldSpanCount(context, heightfield);
2017-02-28 13:10:29 +01:00
// Fill in header.
compactHeightfield.width = xSize;
compactHeightfield.height = zSize;
compactHeightfield.spanCount = spanCount;
compactHeightfield.walkableHeight = walkableHeight;
compactHeightfield.walkableClimb = walkableClimb;
compactHeightfield.maxRegions = 0;
rcVcopy(compactHeightfield.bmin, heightfield.bmin);
rcVcopy(compactHeightfield.bmax, heightfield.bmax);
compactHeightfield.bmax[1] += walkableHeight * heightfield.ch;
compactHeightfield.cs = heightfield.cs;
compactHeightfield.ch = heightfield.ch;
compactHeightfield.cells = (rcCompactCell*)rcAlloc(sizeof(rcCompactCell) * xSize * zSize, RC_ALLOC_PERM);
if (!compactHeightfield.cells)
2017-02-28 13:10:29 +01:00
{
context->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.cells' (%d)", xSize * zSize);
2017-02-28 13:10:29 +01:00
return false;
}
memset(compactHeightfield.cells, 0, sizeof(rcCompactCell) * xSize * zSize);
compactHeightfield.spans = (rcCompactSpan*)rcAlloc(sizeof(rcCompactSpan) * spanCount, RC_ALLOC_PERM);
if (!compactHeightfield.spans)
2017-02-28 13:10:29 +01:00
{
context->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.spans' (%d)", spanCount);
2017-02-28 13:10:29 +01:00
return false;
}
memset(compactHeightfield.spans, 0, sizeof(rcCompactSpan) * spanCount);
compactHeightfield.areas = (unsigned char*)rcAlloc(sizeof(unsigned char) * spanCount, RC_ALLOC_PERM);
if (!compactHeightfield.areas)
2017-02-28 13:10:29 +01:00
{
context->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Out of memory 'chf.areas' (%d)", spanCount);
2017-02-28 13:10:29 +01:00
return false;
}
memset(compactHeightfield.areas, RC_NULL_AREA, sizeof(unsigned char) * spanCount);
2017-02-28 13:10:29 +01:00
const int MAX_HEIGHT = 0xffff;
2017-02-28 13:10:29 +01:00
// Fill in cells and spans.
int currentCellIndex = 0;
const int numColumns = xSize * zSize;
for (int columnIndex = 0; columnIndex < numColumns; ++columnIndex)
2017-02-28 13:10:29 +01:00
{
const rcSpan* span = heightfield.spans[columnIndex];
// If there are no spans at this cell, just leave the data to index=0, count=0.
if (span == NULL)
{
continue;
}
rcCompactCell& cell = compactHeightfield.cells[columnIndex];
cell.index = currentCellIndex;
cell.count = 0;
for (; span != NULL; span = span->next)
2017-02-28 13:10:29 +01:00
{
if (span->area != RC_NULL_AREA)
2017-02-28 13:10:29 +01:00
{
const int bot = (int)span->smax;
const int top = span->next ? (int)span->next->smin : MAX_HEIGHT;
compactHeightfield.spans[currentCellIndex].y = (unsigned short)rcClamp(bot, 0, 0xffff);
compactHeightfield.spans[currentCellIndex].h = (unsigned char)rcClamp(top - bot, 0, 0xff);
compactHeightfield.areas[currentCellIndex] = span->area;
currentCellIndex++;
cell.count++;
2017-02-28 13:10:29 +01:00
}
}
}
2017-02-28 13:10:29 +01:00
// Find neighbour connections.
const int MAX_LAYERS = RC_NOT_CONNECTED - 1;
int maxLayerIndex = 0;
const int zStride = xSize; // for readability
for (int z = 0; z < zSize; ++z)
2017-02-28 13:10:29 +01:00
{
for (int x = 0; x < xSize; ++x)
2017-02-28 13:10:29 +01:00
{
const rcCompactCell& cell = compactHeightfield.cells[x + z * zStride];
for (int i = (int)cell.index, ni = (int)(cell.index + cell.count); i < ni; ++i)
2017-02-28 13:10:29 +01:00
{
rcCompactSpan& span = compactHeightfield.spans[i];
2017-02-28 13:10:29 +01:00
for (int dir = 0; dir < 4; ++dir)
{
rcSetCon(span, dir, RC_NOT_CONNECTED);
const int neighborX = x + rcGetDirOffsetX(dir);
const int neighborZ = z + rcGetDirOffsetY(dir);
2017-02-28 13:10:29 +01:00
// First check that the neighbour cell is in bounds.
if (neighborX < 0 || neighborZ < 0 || neighborX >= xSize || neighborZ >= zSize)
{
2017-02-28 13:10:29 +01:00
continue;
}
2017-02-28 13:10:29 +01:00
// Iterate over all neighbour spans and check if any of the is
// accessible from current cell.
const rcCompactCell& neighborCell = compactHeightfield.cells[neighborX + neighborZ * zStride];
for (int k = (int)neighborCell.index, nk = (int)(neighborCell.index + neighborCell.count); k < nk; ++k)
2017-02-28 13:10:29 +01:00
{
const rcCompactSpan& neighborSpan = compactHeightfield.spans[k];
const int bot = rcMax(span.y, neighborSpan.y);
const int top = rcMin(span.y + span.h, neighborSpan.y + neighborSpan.h);
2017-02-28 13:10:29 +01:00
// 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)neighborSpan.y - (int)span.y) <= walkableClimb)
2017-02-28 13:10:29 +01:00
{
// Mark direction as walkable.
const int layerIndex = k - (int)neighborCell.index;
if (layerIndex < 0 || layerIndex > MAX_LAYERS)
2017-02-28 13:10:29 +01:00
{
maxLayerIndex = rcMax(maxLayerIndex, layerIndex);
2017-02-28 13:10:29 +01:00
continue;
}
rcSetCon(span, dir, layerIndex);
2017-02-28 13:10:29 +01:00
break;
}
}
}
}
}
}
if (maxLayerIndex > MAX_LAYERS)
2017-02-28 13:10:29 +01:00
{
context->log(RC_LOG_ERROR, "rcBuildCompactHeightfield: Heightfield has too many layers %d (max: %d)",
maxLayerIndex, MAX_LAYERS);
2017-02-28 13:10:29 +01:00
}
return true;
2017-02-28 13:10:29 +01:00
}