fix triangulation of complex polygons:
find coordinates inside hole non-convex holes
This commit is contained in:
parent
3484110e08
commit
45b19d6a91
@ -3,6 +3,7 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <math.h>
|
||||||
#include "triangle.h"
|
#include "triangle.h"
|
||||||
|
|
||||||
static void mylog(const char *msg)
|
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));
|
rings = (int*) malloc(num_rings * sizeof(int));
|
||||||
}
|
}
|
||||||
|
|
||||||
int n = 0;
|
int seg = 0;
|
||||||
int h = 0;
|
int hole = 0;
|
||||||
|
|
||||||
int ring;
|
int ring;
|
||||||
int point;
|
int point;
|
||||||
@ -65,32 +66,50 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
|
|||||||
if (rings)
|
if (rings)
|
||||||
rings[ring] = num_points;
|
rings[ring] = num_points;
|
||||||
|
|
||||||
// add holes, fixme: works only for convex
|
// add holes: we need a point inside the hole...
|
||||||
// if ring is clockwise one could take the center of
|
// this is just a heuristic, assuming that two
|
||||||
// the first 'triangle' of a convex arc?
|
// 'parallel' lines have a distance of at least
|
||||||
|
// 1 unit.
|
||||||
if (ring > 0)
|
if (ring > 0)
|
||||||
{
|
{
|
||||||
int k;
|
int k = point * 2;
|
||||||
float cx = 0;
|
|
||||||
float cy = 0;
|
|
||||||
|
|
||||||
for (k = point, len = k + num_points; k < len; k++)
|
float cx = in.pointlist[k+0];
|
||||||
{
|
float cy = in.pointlist[k+1];
|
||||||
cx += in.pointlist[k*2];
|
|
||||||
cy += in.pointlist[k*2+1];
|
|
||||||
}
|
|
||||||
|
|
||||||
in.holelist[h++] = cx / num_points;
|
float nx = in.pointlist[k+2];
|
||||||
in.holelist[h++] = cy / num_points;
|
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.segmentlist[n++] = point + (num_points - 1);
|
in.segmentlist[seg++] = point + (num_points - 1);
|
||||||
in.segmentlist[n++] = point;
|
in.segmentlist[seg++] = point;
|
||||||
|
|
||||||
for (len = point + num_points - 1; point < len; point++)
|
for (len = point + num_points - 1; point < len; point++)
|
||||||
{
|
{
|
||||||
in.segmentlist[n++] = point;
|
in.segmentlist[seg++] = point;
|
||||||
in.segmentlist[n++] = point + 1;
|
in.segmentlist[seg++] = point + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef TESTING
|
#ifdef TESTING
|
||||||
@ -132,9 +151,10 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// ----------- fix addresses to vertex buffer indices -------------
|
// ----------- fix offset to vertex buffer indices -------------
|
||||||
// scale to stride
|
// scale to stride
|
||||||
short stride = 2;
|
short stride = 2;
|
||||||
|
int n, m;
|
||||||
|
|
||||||
for (i = 0, n = out.numberoftriangles * 3; i < n; i++)
|
for (i = 0, n = out.numberoftriangles * 3; i < n; i++)
|
||||||
out.trianglelist[i] *= stride;
|
out.trianglelist[i] *= stride;
|
||||||
@ -150,7 +170,6 @@ jint Java_org_quake_triangle_TriangleJNI_triangulate(JNIEnv *env, jclass c,
|
|||||||
short off = offset;
|
short off = offset;
|
||||||
int add = 0;
|
int add = 0;
|
||||||
int start = 0;
|
int start = 0;
|
||||||
int m;
|
|
||||||
|
|
||||||
for (j = 0, m = in.numberofholes; j < m; j++)
|
for (j = 0, m = in.numberofholes; j < m; j++)
|
||||||
{
|
{
|
||||||
|
Loading…
x
Reference in New Issue
Block a user