text
stringlengths
0
2.2M
vb = pa[(ea+1)%na];
int dx = (int)verts[va*3+0] - (int)verts[vb*3+0];
int dy = (int)verts[va*3+2] - (int)verts[vb*3+2];
return dx*dx + dy*dy;
}
static void mergePolys(unsigned short* pa, unsigned short* pb, int ea, int eb)
{
unsigned short tmp[MAX_VERTS_PER_POLY*2];
const int na = countPolyVerts(pa);
const int nb = countPolyVerts(pb);
// Merge polygons.
memset(tmp, 0xff, sizeof(unsigned short)*MAX_VERTS_PER_POLY*2);
int n = 0;
// Add pa
for (int i = 0; i < na-1; ++i)
tmp[n++] = pa[(ea+1+i) % na];
// Add pb
for (int i = 0; i < nb-1; ++i)
tmp[n++] = pb[(eb+1+i) % nb];
memcpy(pa, tmp, sizeof(unsigned short)*MAX_VERTS_PER_POLY);
}
static void pushFront(unsigned short v, unsigned short* arr, int& an)
{
an++;
for (int i = an-1; i > 0; --i)
arr[i] = arr[i-1];
arr[0] = v;
}
static void pushBack(unsigned short v, unsigned short* arr, int& an)
{
arr[an] = v;
an++;
}
static bool canRemoveVertex(dtTileCachePolyMesh& mesh, const unsigned short rem)
{
// Count number of polygons to remove.
int numRemovedVerts = 0;
int numTouchedVerts = 0;
int numRemainingEdges = 0;
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*MAX_VERTS_PER_POLY*2];
const int nv = countPolyVerts(p);
int numRemoved = 0;
int numVerts = 0;
for (int j = 0; j < nv; ++j)
{
if (p[j] == rem)
{
numTouchedVerts++;
numRemoved++;
}
numVerts++;
}
if (numRemoved)
{
numRemovedVerts += numRemoved;
numRemainingEdges += numVerts-(numRemoved+1);
}
}
// There would be too few edges remaining to create a polygon.
// This can happen for example when a tip of a triangle is marked
// as deletion, but there are no other polys that share the vertex.
// In this case, the vertex should not be removed.
if (numRemainingEdges <= 2)
return false;
// Check that there is enough memory for the test.
const int maxEdges = numTouchedVerts*2;
if (maxEdges > MAX_REM_EDGES)
return false;
// Find edges which share the removed vertex.
unsigned short edges[MAX_REM_EDGES];
int nedges = 0;
for (int i = 0; i < mesh.npolys; ++i)
{
unsigned short* p = &mesh.polys[i*MAX_VERTS_PER_POLY*2];
const int nv = countPolyVerts(p);
// Collect edges which touches the removed vertex.
for (int j = 0, k = nv-1; j < nv; k = j++)
{
if (p[j] == rem || p[k] == rem)
{
// Arrange edge so that a=rem.
int a = p[j], b = p[k];
if (b == rem)