fix triangulation of complex polygons:

find coordinates inside hole non-convex holes
This commit is contained in:
Hannes Janetzek 2013-01-04 19:49:51 +01:00
parent 3484110e08
commit 45b19d6a91

View File

@ -3,6 +3,7 @@
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "triangle.h"
static void mylog(const char *msg)
@ -51,8 +52,8 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
rings = (int*) malloc(num_rings * sizeof(int));
}
int n = 0;
int h = 0;
int seg = 0;
int hole = 0;
int ring;
int point;
@ -65,32 +66,50 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
if (rings)
rings[ring] = num_points;
// add holes, fixme: works only for convex
// if ring is clockwise one could take the center of
// the first 'triangle' of a convex arc?
// add holes: we need a point inside the hole...
// this is just a heuristic, assuming that two
// 'parallel' lines have a distance of at least
// 1 unit.
if (ring > 0)
{
int k;
float cx = 0;
float cy = 0;
int k = point * 2;
for (k = point, len = k + num_points; k < len; k++)
{
cx += in.pointlist[k*2];
cy += in.pointlist[k*2+1];
float cx = in.pointlist[k+0];
float cy = in.pointlist[k+1];
float nx = in.pointlist[k+2];
float ny = in.pointlist[k+3];
float vx = nx - cx;
float vy = ny - cy;
float a = sqrt(vx*vx + vy*vy);
// fixme: need to check a == 0?
//if (a > 0.00001 || a < -0.0001)
float ux = -vy / a;
float uy = vx / a;
float centerx = cx + vx / 2 - ux;
float centery = cy + vy / 2 - uy;
snprintf(buf, 128, "a: %f in:(%.2f %.2f) "
"cur:(%.2f %.2f), next:(%.2f %.2f)\n",
a, centerx, centery, cx, cy, nx,ny);
mylog(buf);
in.holelist[hole++] = centerx;
in.holelist[hole++] = centery;
}
in.holelist[h++] = cx / num_points;
in.holelist[h++] = cy / num_points;
}
in.segmentlist[n++] = point + (num_points - 1);
in.segmentlist[n++] = point;
in.segmentlist[seg++] = point + (num_points - 1);
in.segmentlist[seg++] = point;
for (len = point + num_points - 1; point < len; point++)
{
in.segmentlist[n++] = point;
in.segmentlist[n++] = point + 1;
in.segmentlist[seg++] = point;
in.segmentlist[seg++] = point + 1;
}
}
#ifdef TESTING
@ -132,9 +151,10 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
}
#endif
// ----------- fix addresses to vertex buffer indices -------------
// ----------- fix offset to vertex buffer indices -------------
// scale to stride
short stride = 2;
int n, m;
for (i = 0, n = out.numberoftriangles * 3; i < n; i++)
out.trianglelist[i] *= stride;
@ -150,7 +170,6 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
short off = offset;
int add = 0;
int start = 0;
int m;
for (j = 0, m = in.numberofholes; j < m; j++)
{