ivanfratric / polypartition

Tiny Polygon Partitioning and Triangulation Library
MIT License
664 stars 118 forks source link

Fail to decompose/triangulate a simple polygon #20

Closed Mike3D closed 8 years ago

Mike3D commented 8 years ago

Hi Ivan,

I have an issue with this simple polygon: 2.88 11.04 3.84 10.56 5.12 10.56 5.12 10.88 5.44 11.04 It fails both to decompose and triangulate. Here how it should look: poly

ivanfratric commented 8 years ago

Which method did you use for triangulation and are the vertices of the input poly in the counter-clockwise order (you can verify using GetOrientation())?

Mike3D commented 8 years ago

Many thanks for reply. I've tried:

Tests have been done with CCW and CW to ensure that this was not the culprit

ivanfratric commented 8 years ago

Hmm, seems to work for me. I didn't check every output triangle, but the return value and the number of triangles returned are all correct. This is the code I used.

int main()
{
    int ret;

    TPPLPoly poly;
    poly.Init(5);
    poly[0].x = 2.88; poly[0].y = 11.04;
    poly[1].x = 3.84; poly[1].y = 10.56;
    poly[2].x = 5.12; poly[2].y = 10.56;
    poly[3].x = 5.12; poly[3].y = 10.88;
    poly[4].x = 5.44; poly[4].y = 11.04;

    printf("%d\n", poly.GetOrientation());

    list<TPPLPoly> input;
    list<TPPLPoly> results;
    TPPLPartition partition;

    input.push_back(poly);

    ret = partition.Triangulate_EC(&poly, &results);
    printf("%d, %d\n", ret, results.size());
    results.clear();

    ret = partition.Triangulate_EC(&input, &results);
    printf("%d, %d\n", ret, results.size());

    return 0;
}

Have you made any changes to the library?

Mike3D commented 8 years ago

Thanks for checking I found the culprit for the triangulation issue: as I draw the triangles with OpenGL I have to swap second and third vertices for each triangle, otherwise they don't render: :wink:

triangle.Triangle(poly->GetPoint(diagonal.index1),poly->GetPoint(bestvertex),poly->GetPoint(diagonal.index2));

becomes:

triangle.Triangle(poly->GetPoint(diagonal.index1),poly->GetPoint(diagonal.index2),poly->GetPoint(bestvertex));

Now I still experience decompose issues with some more complex polygons :cry: for example: 12.52 15.22 13.52 14.72 12.8 14.72 12.48 14.24 12.16 14.4 10.16 12.84 8.64 12.64 8.32 12.48 8.52 12.22 7.52 12.72

ivanfratric commented 8 years ago

That polygon is given in the wrong (clockwise) vertex order.

Mike3D commented 8 years ago

OK, here is a CCW polygon that fails to decompose: poly

int main()
{
    int ret;

    TPPLPoly poly;
    poly.Init(43);
    poly[0].x = 2.88; poly[0].y = 11.04;
    poly[1].x = 3.84; poly[1].y = 10.56;
    poly[2].x = 4.48; poly[2].y = 10.56;
    poly[3].x = 5.12; poly[3].y = 10.56;
    poly[4].x = 5.12; poly[4].y = 10.88;
    poly[5].x = 5.44; poly[5].y = 11.04;
    poly[6].x = 6.08; poly[6].y = 10.72;
    poly[7].x = 6.4; poly[7].y = 10.88;
    poly[8].x = 5.76; poly[8].y = 11.2;
    poly[9].x = 6.08; poly[9].y = 11.36;
    poly[10].x = 6.72; poly[10].y = 11.36;
    poly[11].x = 6.72; poly[11].y = 11.68;
    poly[12].x = 7.04; poly[12].y = 11.84;
    poly[13].x = 7.68; poly[13].y = 11.52;
    poly[14].x = 8; poly[14].y = 11.68;
    poly[15].x = 7.36; poly[15].y = 12;
    poly[16].x = 7.68; poly[16].y = 12.16;
    poly[17].x = 8.32; poly[17].y = 12.16;
    poly[18].x = 8.32; poly[18].y = 12.48;
    poly[19].x = 8.64; poly[19].y = 12.64;
    poly[20].x = 9.28; poly[20].y = 12.32;
    poly[21].x = 9.6; poly[21].y = 12.48;
    poly[22].x = 8.96; poly[22].y = 12.8;
    poly[23].x = 9.6; poly[23].y = 13.12;
    poly[24].x = 10.56; poly[24].y = 12.64;
    poly[25].x = 11.2; poly[25].y = 12.96;
    poly[26].x = 10.24; poly[26].y = 13.44;
    poly[27].x = 12.16; poly[27].y = 14.4;
    poly[28].x = 12.48; poly[28].y = 14.24;
    poly[29].x = 13.12; poly[29].y = 14.56;
    poly[30].x = 12.8; poly[30].y = 14.72;
    poly[31].x = 13.12; poly[31].y = 14.88;
    poly[32].x = 14.08; poly[32].y = 14.4;
    poly[33].x = 14.72; poly[33].y = 14.4;
    poly[34].x = 14.72; poly[34].y = 14.72;
    poly[35].x = 13.76; poly[35].y = 15.2;
    poly[36].x = 19.84; poly[36].y = 18.24;
    poly[37].x = 20.8; poly[37].y = 17.76;
    poly[38].x = 21.44; poly[38].y = 17.76;
    poly[39].x = 21.76; poly[39].y = 17.92;
    poly[40].x = 19.84; poly[40].y = 18.88;
    poly[41].x = 4.16; poly[41].y = 11.04;
    poly[42].x = 3.52; poly[42].y = 11.36;

    printf("%d\n", poly.GetOrientation());

    list<TPPLPoly> input;
    list<TPPLPoly> results;
    TPPLPartition partition;

    input.push_back(poly);

    ret = partition.ConvexPartition_OPT(&poly, &results);
    printf("%d, %d\n", ret, results.size());
    results.clear();

    return 0;
}
ivanfratric commented 8 years ago

With all due respect, I've already looked at two "issues" that turned out to be issues in your code. My time is limited and I can't afford to go on with this. Please let me know if you can point out an actual bug in my code.

Mike3D commented 8 years ago

Anyway, I've switched to Bayazit and it works perfectly :thumbsup::thumbsup::thumbsup: poly Thanks for your time. Cheers