jni: use double coordinates in Triangle. try to return safely when internal error occured

This commit is contained in:
Hannes Janetzek 2013-09-12 23:44:00 +02:00
parent 67a3cbadf7
commit 5389f59df0
3 changed files with 56 additions and 56 deletions

View File

@ -129,12 +129,12 @@ jint Java_org_oscim_utils_geom_Triangulator_triangulate(JNIEnv *env, jclass c,
int *rings = NULL; int *rings = NULL;
if (in.numberofholes > 0) { if (in.numberofholes > 0) {
in.holelist = (float *) malloc(in.numberofholes * 2 * sizeof(float)); in.holelist = (double *) malloc(in.numberofholes * 2 * sizeof(double));
rings = (int*) malloc(num_rings * sizeof(int)); rings = (int*) malloc(num_rings * sizeof(int));
} }
int *seg = in.segmentlist; int *seg = in.segmentlist;
float *hole = in.holelist; double *hole = in.holelist;
// counter going through all points // counter going through all points
int point; int point;
@ -182,8 +182,8 @@ jint Java_org_oscim_utils_geom_Triangulator_triangulate(JNIEnv *env, jclass c,
float ux = -vy / a; float ux = -vy / a;
float uy = vx / a; float uy = vx / a;
float centerx = cx + vx / 2.0 - (ux * 0.1); double centerx = cx + vx / 2.0 - (ux * 0.1);
float centery = cy + vy / 2.0 - (uy * 0.1); double centery = cy + vy / 2.0 - (uy * 0.1);
*hole++ = centerx; *hole++ = centerx;
*hole++ = centery; *hole++ = centery;
@ -268,8 +268,8 @@ jint Java_org_oscim_utils_geom_Triangulator_triangulate(JNIEnv *env, jclass c,
INDICE *tri = out.trianglelist; INDICE *tri = out.trianglelist;
for (int n = out.numberoftriangles * 3; n > 0; n--, tri++) for (int n = out.numberoftriangles * 3; n > 0; n--)
*tri = *tri * stride + offset; *tri++ = *tri * stride + offset;
// when a ring has an odd number of points one (or rather two) // when a ring has an odd number of points one (or rather two)
// additional vertices will be added. so the following rings // additional vertices will be added. so the following rings

View File

@ -311,10 +311,6 @@ int error_set = 0;
void internalerror() { void internalerror() {
error_set = 1; error_set = 1;
printf("Triangle is going to quit its job now\n"); printf("Triangle is going to quit its job now\n");
//printf(" Please report this bug to jrs@cs.berkeley.edu\n");
///printf(" Include the message above, your input data set, and the exact\n");
//printf(" command line you used to run Triangle.\n");
//triexit(1);
} }
/*****************************************************************************/ /*****************************************************************************/
@ -329,8 +325,6 @@ void parsecommandline(int argc, char **argv, struct behavior *b) {
#define STARTINDEX 0 #define STARTINDEX 0
int i, j;
b->poly = b->refine = b->quality = 0; b->poly = b->refine = b->quality = 0;
b->vararea = b->fixedarea = b->usertest = 0; b->vararea = b->fixedarea = b->usertest = 0;
b->regionattrib = b->convex = b->weighted = b->jettison = 0; b->regionattrib = b->convex = b->weighted = b->jettison = 0;
@ -351,6 +345,8 @@ void parsecommandline(int argc, char **argv, struct behavior *b) {
b->maxarea = -1.0; b->maxarea = -1.0;
b->quiet = b->verbose = 0; b->quiet = b->verbose = 0;
int i, j;
for (i = STARTINDEX; i < argc; i++) { for (i = STARTINDEX; i < argc; i++) {
for (j = STARTINDEX; argv[i][j] != '\0'; j++) { for (j = STARTINDEX; argv[i][j] != '\0'; j++) {
if (argv[i][j] == 'p') { if (argv[i][j] == 'p') {
@ -3862,8 +3858,9 @@ enum insertvertexresult insertvertex(struct mesh *m, struct behavior *b, vertex
sorg(brokensubseg, encroached->subsegorg); sorg(brokensubseg, encroached->subsegorg);
sdest(brokensubseg, encroached->subsegdest); sdest(brokensubseg, encroached->subsegdest);
if (b->verbose > 2) { if (b->verbose > 2) {
printf( printf(" Queueing encroached subsegment (%.12g, %.12g) (%.12g, %.12g).\n",
" Queueing encroached subsegment (%.12g, %.12g) (%.12g, %.12g).\n", encroached->subsegorg[0], encroached->subsegorg[1], encroached->subsegdest[0], encroached->subsegdest[1]); encroached->subsegorg[0], encroached->subsegorg[1],
encroached->subsegdest[0], encroached->subsegdest[1]);
} }
} }
} }
@ -5143,10 +5140,10 @@ long divconqdelaunay(struct mesh *m, struct behavior *b) {
i = 0; i = 0;
for (j = 1; j < m->invertices; j++) { for (j = 1; j < m->invertices; j++) {
if ((sortarray[i][0] == sortarray[j][0]) && (sortarray[i][1] == sortarray[j][1])) { if ((sortarray[i][0] == sortarray[j][0]) && (sortarray[i][1] == sortarray[j][1])) {
if (!b->quiet) { //if (!b->quiet) {
printf( printf("Warning: A duplicate vertex at (%.12g, %.12g) appeared and was ignored.\n",
"Warning: A duplicate vertex at (%.12g, %.12g) appeared and was ignored.\n", sortarray[j][0], sortarray[j][1]); sortarray[j][0], sortarray[j][1]);
} //}
setvertextype(sortarray[j], UNDEADVERTEX); setvertextype(sortarray[j], UNDEADVERTEX);
m->undeads++; m->undeads++;
} }
@ -5305,6 +5302,7 @@ enum finddirectionresult finddirection(struct mesh *m, struct behavior *b, struc
printf(" triangle leading from (%.12g, %.12g) to", startvertex[0], startvertex[1]); printf(" triangle leading from (%.12g, %.12g) to", startvertex[0], startvertex[1]);
printf(" (%.12g, %.12g).\n", searchpoint[0], searchpoint[1]); printf(" (%.12g, %.12g).\n", searchpoint[0], searchpoint[1]);
internalerror(); internalerror();
return 0;
} }
apex(*searchtri, leftvertex); apex(*searchtri, leftvertex);
rightccw = leftccw; rightccw = leftccw;
@ -5319,6 +5317,7 @@ enum finddirectionresult finddirection(struct mesh *m, struct behavior *b, struc
printf(" triangle leading from (%.12g, %.12g) to", startvertex[0], startvertex[1]); printf(" triangle leading from (%.12g, %.12g) to", startvertex[0], startvertex[1]);
printf(" (%.12g, %.12g).\n", searchpoint[0], searchpoint[1]); printf(" (%.12g, %.12g).\n", searchpoint[0], searchpoint[1]);
internalerror(); internalerror();
return 0;
} }
dest(*searchtri, rightvertex); dest(*searchtri, rightvertex);
leftccw = rightccw; leftccw = rightccw;
@ -5434,6 +5433,8 @@ void segmentintersection(struct mesh *m, struct behavior *b, struct otri *splitt
// FIXME collinear = // FIXME collinear =
finddirection(m, b, splittri, endpoint1); finddirection(m, b, splittri, endpoint1);
if (error_set)
return;
dest(*splittri, rightvertex); dest(*splittri, rightvertex);
apex(*splittri, leftvertex); apex(*splittri, leftvertex);
@ -5750,6 +5751,9 @@ void constrainededge(struct mesh *m, struct behavior *b, struct otri *starttri,
collision = 1; collision = 1;
/* Insert a vertex at the intersection. */ /* Insert a vertex at the intersection. */
segmentintersection(m, b, &fixuptri, &crosssubseg, endpoint2); segmentintersection(m, b, &fixuptri, &crosssubseg, endpoint2);
if (error_set)
return;
done = 1; done = 1;
} }
} }
@ -5802,6 +5806,7 @@ void insertsegment(struct mesh *m, struct behavior *b, vertex endpoint1, vertex
printf( "Internal error in insertsegment(): Unable to locate PSLG vertex\n"); printf( "Internal error in insertsegment(): Unable to locate PSLG vertex\n");
printf(" (%.12g, %.12g) in triangulation.\n", endpoint1[0], endpoint1[1]); printf(" (%.12g, %.12g) in triangulation.\n", endpoint1[0], endpoint1[1]);
internalerror(); internalerror();
return;
} }
} }
/* Remember this triangle to improve subsequent point location. */ /* Remember this triangle to improve subsequent point location. */
@ -5833,6 +5838,7 @@ void insertsegment(struct mesh *m, struct behavior *b, vertex endpoint1, vertex
printf( "Internal error in insertsegment(): Unable to locate PSLG vertex\n"); printf( "Internal error in insertsegment(): Unable to locate PSLG vertex\n");
printf(" (%.12g, %.12g) in triangulation.\n", endpoint2[0], endpoint2[1]); printf(" (%.12g, %.12g) in triangulation.\n", endpoint2[0], endpoint2[1]);
internalerror(); internalerror();
return;
} }
} }
/* Remember this triangle to improve subsequent point location. */ /* Remember this triangle to improve subsequent point location. */
@ -5847,24 +5853,8 @@ void insertsegment(struct mesh *m, struct behavior *b, vertex endpoint1, vertex
/* vertex on the segment occurred. */ /* vertex on the segment occurred. */
org(searchtri2, endpoint2); org(searchtri2, endpoint2);
#ifndef REDUCED
#ifndef CDT_ONLY
if (b->splitseg)
{
/* Insert vertices to force the segment into the triangulation. */
conformingedge(m, b, endpoint1, endpoint2, newmark);
}
else
{
#endif /* not CDT_ONLY */
#endif /* not REDUCED */
/* Insert the segment directly into the triangulation. */ /* Insert the segment directly into the triangulation. */
constrainededge(m, b, &searchtri1, endpoint2, newmark); constrainededge(m, b, &searchtri1, endpoint2, newmark);
#ifndef REDUCED
#ifndef CDT_ONLY
}
#endif /* not CDT_ONLY */
#endif /* not REDUCED */
} }
/*****************************************************************************/ /*****************************************************************************/
@ -5974,6 +5964,8 @@ void formskeleton(struct mesh *m, struct behavior *b, int *segmentlist, int *seg
} }
else { else {
insertsegment(m, b, endpoint1, endpoint2, boundmarker); insertsegment(m, b, endpoint1, endpoint2, boundmarker);
if (error_set)
return;
} }
} }
} }
@ -6630,7 +6622,7 @@ void highorder(struct mesh *m, struct behavior *b) {
/* */ /* */
/*****************************************************************************/ /*****************************************************************************/
void transfernodes(struct mesh *m, struct behavior *b, REAL *pointlist, REAL *pointattriblist, void transfernodes(struct mesh *m, struct behavior *b, IO_REAL *pointlist, IO_REAL *pointattriblist,
int *pointmarkerlist, int numberofpoints, int numberofpointattribs) { int *pointmarkerlist, int numberofpoints, int numberofpointattribs) {
vertex vertexloop; vertex vertexloop;
REAL x, y; REAL x, y;
@ -6674,15 +6666,18 @@ void transfernodes(struct mesh *m, struct behavior *b, REAL *pointlist, REAL *po
} }
// ---------------------------------------------- // ----------------------------------------------
for (j = (i - 1) * 2; j >= 0; j -= 2){ // for (j = (i - 1) * 2; j >= 0; j -= 2){
if (x == pointlist[j] && y == pointlist[j+1]){ // if (x == pointlist[j] && y == pointlist[j+1]){
printf("skip duplicate %d\n", j >> 1); // printf("skip duplicate %d\n", j >> 1);
setvertextype(vertexloop, UNDEADVERTEX); // setvertextype(vertexloop, UNDEADVERTEX);
vertexloop[0] = 0xffffffff; // m->undeads++;
vertexloop[1] = 0xffffffff; //
break; // vertexloop[0] = 0xffffffff;
} // vertexloop[1] = 0xffffffff;
} // break;
// }
// }
if (j >= 0) if (j >= 0)
continue; continue;
// ---------------------------------------------- // ----------------------------------------------
@ -7250,6 +7245,8 @@ void triangulate(struct behavior *command, struct triangulateio *in, struct tria
if (!b->refine) { if (!b->refine) {
/* Insert PSLG segments and/or convex hull segments. */ /* Insert PSLG segments and/or convex hull segments. */
formskeleton(&m, b, in->segmentlist, in->segmentmarkerlist, in->numberofsegments); formskeleton(&m, b, in->segmentlist, in->segmentmarkerlist, in->numberofsegments);
if (error_set)
return;
} }
} }
@ -7328,10 +7325,10 @@ void triangulate(struct behavior *command, struct triangulateio *in, struct tria
} }
numbernodes(&m, b); /* We must remember to number the vertices. */ numbernodes(&m, b); /* We must remember to number the vertices. */
} }
else { //else {
/* writenodes() numbers the vertices too. */ /* writenodes() numbers the vertices too. */
writenodes(&m, b, &out->pointlist, &out->pointattributelist, &out->pointmarkerlist); // writenodes(&m, b, &out->pointlist, &out->pointattributelist, &out->pointmarkerlist);
} //}
if (b->noelewritten) { if (b->noelewritten) {
if (!b->quiet) { if (!b->quiet) {
printf("NOT writing triangles.\n"); printf("NOT writing triangles.\n");
@ -7358,18 +7355,19 @@ void triangulate(struct behavior *command, struct triangulateio *in, struct tria
out->regionlist = in->regionlist; out->regionlist = in->regionlist;
} }
else { else {
out->holelist = (REAL *) NULL; out->holelist = NULL;
out->regionlist = (REAL *) NULL; out->regionlist = NULL;
} }
} }
} }
if (b->edgesout) { if (b->edgesout) {
writeedges(&m, b, &out->edgelist, &out->edgemarkerlist); writeedges(&m, b, &out->edgelist, &out->edgemarkerlist);
} }
if (b->voronoi) {
writevoronoi(&m, b, &vorout->pointlist, &vorout->pointattributelist, &vorout->pointmarkerlist, //if (b->voronoi) {
&vorout->edgelist, &vorout->edgemarkerlist, &vorout->normlist); // writevoronoi(&m, b, &vorout->pointlist, &vorout->pointattributelist, &vorout->pointmarkerlist,
} // &vorout->edgelist, &vorout->edgemarkerlist, &vorout->normlist);
//}
if (b->neighbors) { if (b->neighbors) {
writeneighbors(&m, b, &out->neighborlist); writeneighbors(&m, b, &out->neighborlist);
} }

View File

@ -248,7 +248,7 @@
/* */ /* */
/*****************************************************************************/ /*****************************************************************************/
#define SINGLE //#define SINGLE
#ifdef SINGLE #ifdef SINGLE
#define REAL float #define REAL float
@ -256,13 +256,15 @@
#define REAL double #define REAL double
#endif /* not SINGLE */ #endif /* not SINGLE */
#define IO_REAL float
#define INDICE unsigned short #define INDICE unsigned short
typedef struct triangulateio TriangleIO; typedef struct triangulateio TriangleIO;
struct triangulateio { struct triangulateio {
REAL *pointlist; /* In / out */ IO_REAL *pointlist; /* In / out */
REAL *pointattributelist; /* In / out */ IO_REAL *pointattributelist; /* In / out */
int *pointmarkerlist; /* In / out */ int *pointmarkerlist; /* In / out */
int numberofpoints; /* In / out */ int numberofpoints; /* In / out */
int numberofpointattributes; /* In / out */ int numberofpointattributes; /* In / out */