Correct use of polygon triangulators in LibGDX

744 views Asked by At

I use the following code to triangulate Voronoi areas:

EarClippingTriangulator triangulator = new EarClippingTriangulator();
ConvexHull hull = new ConvexHull();

final FloatArray rawPoints = voronoiArea.getPoints(factor, factor); // This will get a set of points in no particular order
final FloatArray hullArray = hull.computePolygon(rawPoints, false); // To avoid concave polygons
final Polygon polygon = new Polygon(hullArray.toArray());
final PolygonRegion region = new PolygonRegion(texture, polygon.getVertices(),
                    triangulator.computeTriangles(polygon.getVertices()).toArray());
final PolygonSprite sprite = new PolygonSprite(region);

This works fine in 95% of the time, but now and then I get invalid polygons - when rendering they appear as regular polygons with 1-2 triangles 'missing'.

Has anyone else had this problem? Can anyone spot a problem with this code?

Edit: Resulting image:

The green triangle is the problem...

EDIT:

I have done some more testing - I now have a single polygon defined with 10 vertices, and the polygon still renders incorrectly.

The code:

      points = new float[] {
                34.983868f, 31.306454f, 27.999998f, 40.285713f,
                28.0f, 40.285713f, 28.0f, 54.0f,
                34.98387f, 31.306452f, 54.0f, 32.76923f,
                28.0f, 54.0f, 54.0f, 54.0f,
                54.0f, 54.0f, 54.0f, 32.76923f
        };

        net.dermetfan.utils.math.GeometryUtils.arrangeConvexPolygon(points, true);

        for (int i = 0; i < points.length; i++) {
            points[i]*=10f;
        }

        final Polygon p = new Polygon(points);
        final float posX = voronoiArea.getCenter().x;
        final float posY = voronoiArea.getCenter().y;
        p.setOrigin(posX, posY);

        final short[] triangles = triangulator.computeTriangles(p.getVertices()).toArray();
        final PolygonRegion region = new PolygonRegion(fill[voronoiArea.site.getSiteNum() % 3], p.getVertices(), triangles);

Looking at it now I can see that there are several vertices that are very close to each other - 27.999998f and 28.0f for example - this makes me suspect that the whole problem is because of some rounding errors somewhere...

0

There are 0 answers