text
stringlengths
0
2.2M
unsigned char pareas[MAX_REM_EDGES];
// Build initial polygons.
int npolys = 0;
memset(polys, 0xff, ntris*MAX_VERTS_PER_POLY*sizeof(unsigned short));
for (int j = 0; j < ntris; ++j)
{
unsigned short* t = &tris[j*3];
if (t[0] != t[1] && t[0] != t[2] && t[1] != t[2])
{
polys[npolys*MAX_VERTS_PER_POLY+0] = hole[t[0]];
polys[npolys*MAX_VERTS_PER_POLY+1] = hole[t[1]];
polys[npolys*MAX_VERTS_PER_POLY+2] = hole[t[2]];
pareas[npolys] = (unsigned char)harea[t[0]];
npolys++;
}
}
if (!npolys)
return DT_SUCCESS;
// Merge polygons.
int maxVertsPerPoly = MAX_VERTS_PER_POLY;
if (maxVertsPerPoly > 3)
{
for (;;)
{
// Find best polygons to merge.
int bestMergeVal = 0;
int bestPa = 0, bestPb = 0, bestEa = 0, bestEb = 0;
for (int j = 0; j < npolys-1; ++j)
{
unsigned short* pj = &polys[j*MAX_VERTS_PER_POLY];
for (int k = j+1; k < npolys; ++k)
{
unsigned short* pk = &polys[k*MAX_VERTS_PER_POLY];
int ea, eb;
int v = getPolyMergeValue(pj, pk, mesh.verts, ea, eb);
if (v > bestMergeVal)
{
bestMergeVal = v;
bestPa = j;
bestPb = k;
bestEa = ea;
bestEb = eb;
}
}
}
if (bestMergeVal > 0)
{
// Found best, merge.
unsigned short* pa = &polys[bestPa*MAX_VERTS_PER_POLY];
unsigned short* pb = &polys[bestPb*MAX_VERTS_PER_POLY];
mergePolys(pa, pb, bestEa, bestEb);
memcpy(pb, &polys[(npolys-1)*MAX_VERTS_PER_POLY], sizeof(unsigned short)*MAX_VERTS_PER_POLY);
pareas[bestPb] = pareas[npolys-1];
npolys--;
}
else
{
// Could not merge any polygons, stop.
break;
}
}
}
// Store polygons.
for (int i = 0; i < npolys; ++i)
{
if (mesh.npolys >= maxTris) break;
unsigned short* p = &mesh.polys[mesh.npolys*MAX_VERTS_PER_POLY*2];
memset(p,0xff,sizeof(unsigned short)*MAX_VERTS_PER_POLY*2);
for (int j = 0; j < MAX_VERTS_PER_POLY; ++j)
p[j] = polys[i*MAX_VERTS_PER_POLY+j];
mesh.areas[mesh.npolys] = pareas[i];
mesh.npolys++;
if (mesh.npolys > maxTris)
return DT_FAILURE | DT_BUFFER_TOO_SMALL;
}
return DT_SUCCESS;
}
dtStatus dtBuildTileCachePolyMesh(dtTileCacheAlloc* alloc,
dtTileCacheContourSet& lcset,
dtTileCachePolyMesh& mesh)
{
dtAssert(alloc);
int maxVertices = 0;
int maxTris = 0;
int maxVertsPerCont = 0;
for (int i = 0; i < lcset.nconts; ++i)
{
// Skip null contours.
if (lcset.conts[i].nverts < 3) continue;
maxVertices += lcset.conts[i].nverts;
maxTris += lcset.conts[i].nverts - 2;