text
stringlengths
0
2.2M
// For each edge (k,k+1) of P
for (int k = 0; k < n; k++)
{
int k1 = next(k, n);
// Skip edges incident to i or j
if (!((k == i) || (k1 == i) || (k == j) || (k1 == j)))
{
const unsigned char* p0 = &verts[(indices[k] & 0x7fff) * 4];
const unsigned char* p1 = &verts[(indices[k1] & 0x7fff) * 4];
if (vequal(d0, p0) || vequal(d1, p0) || vequal(d0, p1) || vequal(d1, p1))
continue;
if (intersect(d0, d1, p0, p1))
return false;
}
}
return true;
}
// Returns true iff the diagonal (i,j) is strictly internal to the
// polygon P in the neighborhood of the i endpoint.
static bool inCone(int i, int j, int n, const unsigned char* verts, const unsigned short* indices)
{
const unsigned char* pi = &verts[(indices[i] & 0x7fff) * 4];
const unsigned char* pj = &verts[(indices[j] & 0x7fff) * 4];
const unsigned char* pi1 = &verts[(indices[next(i, n)] & 0x7fff) * 4];
const unsigned char* pin1 = &verts[(indices[prev(i, n)] & 0x7fff) * 4];
// If P[i] is a convex vertex [ i+1 left or on (i-1,i) ].
if (leftOn(pin1, pi, pi1))
return left(pi, pj, pin1) && left(pj, pi, pi1);
// Assume (i-1,i,i+1) not collinear.
// else P[i] is reflex.
return !(leftOn(pi, pj, pi1) && leftOn(pj, pi, pin1));
}
// Returns T iff (v_i, v_j) is a proper internal
// diagonal of P.
static bool diagonal(int i, int j, int n, const unsigned char* verts, const unsigned short* indices)
{
return inCone(i, j, n, verts, indices) && diagonalie(i, j, n, verts, indices);
}
static int triangulate(int n, const unsigned char* verts, unsigned short* indices, unsigned short* tris)
{
int ntris = 0;
unsigned short* dst = tris;
// The last bit of the index is used to indicate if the vertex can be removed.
for (int i = 0; i < n; i++)
{
int i1 = next(i, n);
int i2 = next(i1, n);
if (diagonal(i, i2, n, verts, indices))
indices[i1] |= 0x8000;
}
while (n > 3)
{
int minLen = -1;
int mini = -1;
for (int i = 0; i < n; i++)
{
int i1 = next(i, n);
if (indices[i1] & 0x8000)
{
const unsigned char* p0 = &verts[(indices[i] & 0x7fff) * 4];
const unsigned char* p2 = &verts[(indices[next(i1, n)] & 0x7fff) * 4];
const int dx = (int)p2[0] - (int)p0[0];
const int dz = (int)p2[2] - (int)p0[2];
const int len = dx*dx + dz*dz;
if (minLen < 0 || len < minLen)
{
minLen = len;
mini = i;
}
}
}
if (mini == -1)
{
// Should not happen.
/* printf("mini == -1 ntris=%d n=%d\n", ntris, n);
for (int i = 0; i < n; i++)
{
printf("%d ", indices[i] & 0x0fffffff);
}
printf("\n");*/
return -ntris;
}
int i = mini;
int i1 = next(i, n);
int i2 = next(i1, n);
*dst++ = indices[i] & 0x7fff;
*dst++ = indices[i1] & 0x7fff;
*dst++ = indices[i2] & 0x7fff;