javagisjts

Transform triangles into a list of vertex indices with JTS and DelaunayTriangulationBuilder


I have this little piece of code:

DelaunayTriangulationBuilder builder = new DelaunayTriangulationBuilder();
builder.setSites(geometry);
builder.setTolerance(0.0000001);
Geometry triangulation = builder.getTriangles(new GeometryFactory());

It will tessellate a Geometry to create a list of triangles. I want to use this result in a 3d application, but in order to optimize the storage space I would like to have it in the following structure:

I have a hard time finding an efficient way to do that using the JTS DelaunayTriangulationBuilder class.

Any help would be appreciated, thank you!


Solution

  • I think that rather than a Geometry you want to work with a QuadEdgeSubdivision. Then you can have code like:

        QuadEdgeSubdivision quadEdgeSubdivision = builder.getSubdivision();
        @SuppressWarnings("unchecked")
        Collection<QuadEdge> primaryEdges = quadEdgeSubdivision.getPrimaryEdges(false);
    
        for (QuadEdge edge : primaryEdges) {
          if (quadEdgeSubdivision.isFrameEdge(edge)){
             // this is an outer edge
          }
            // Edges have vertexes and are part of a triangle
            Vertex[] v = new Vertex[3];
            v[0] = edge.orig();
            v[1] = edge.dest();
            v[2] = edge.oNext().dest();
            // or you can get a LineString 
            LineSegment lineSegment = edges[i].toLineSegment();
            ...
         }
    

    You can see this sort of thing in action in the GeoTools Contour process that I wrote about a couple of years ago.