text
stringlengths
0
2.2M
static void simplifyContour(dtTempContour& cont, const float maxError)
{
cont.npoly = 0;
for (int i = 0; i < cont.nverts; ++i)
{
int j = (i+1) % cont.nverts;
// Check for start of a wall segment.
unsigned char ra = cont.verts[j*4+3];
unsigned char rb = cont.verts[i*4+3];
if (ra != rb)
cont.poly[cont.npoly++] = (unsigned short)i;
}
if (cont.npoly < 2)
{
// If there is no transitions at all,
// create some initial points for the simplification process.
// Find lower-left and upper-right vertices of the contour.
int llx = cont.verts[0];
int llz = cont.verts[2];
int lli = 0;
int urx = cont.verts[0];
int urz = cont.verts[2];
int uri = 0;
for (int i = 1; i < cont.nverts; ++i)
{
int x = cont.verts[i*4+0];
int z = cont.verts[i*4+2];
if (x < llx || (x == llx && z < llz))
{
llx = x;
llz = z;
lli = i;
}
if (x > urx || (x == urx && z > urz))
{
urx = x;
urz = z;
uri = i;
}
}
cont.npoly = 0;
cont.poly[cont.npoly++] = (unsigned short)lli;
cont.poly[cont.npoly++] = (unsigned short)uri;
}
// Add points until all raw points are within
// error tolerance to the simplified shape.
for (int i = 0; i < cont.npoly; )
{
int ii = (i+1) % cont.npoly;
const int ai = (int)cont.poly[i];
const int ax = (int)cont.verts[ai*4+0];
const int az = (int)cont.verts[ai*4+2];
const int bi = (int)cont.poly[ii];
const int bx = (int)cont.verts[bi*4+0];
const int bz = (int)cont.verts[bi*4+2];
// Find maximum deviation from the segment.
float maxd = 0;
int maxi = -1;
int ci, cinc, endi;
// Traverse the segment in lexilogical order so that the
// max deviation is calculated similarly when traversing
// opposite segments.
if (bx > ax || (bx == ax && bz > az))
{
cinc = 1;
ci = (ai+cinc) % cont.nverts;
endi = bi;
}
else
{
cinc = cont.nverts-1;
ci = (bi+cinc) % cont.nverts;
endi = ai;
}
// Tessellate only outer edges or edges between areas.
while (ci != endi)
{
float d = distancePtSeg(cont.verts[ci*4+0], cont.verts[ci*4+2], ax, az, bx, bz);
if (d > maxd)
{
maxd = d;
maxi = ci;
}
ci = (ci+cinc) % cont.nverts;
}
// If the max deviation is larger than accepted error,
// add new point, else continue to next segment.
if (maxi != -1 && maxd > (maxError*maxError))
{
cont.npoly++;
for (int j = cont.npoly-1; j > i; --j)