Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to triangulate polygons in Boost?

What is the best way to triangulate a polygon with Boost?

I use Boost.polygon.

My current algorithm:

  1. Compute a voronoï diagram from my polygon vertices.

  2. Create one directed polygon-edge for each cell-edge (this will create two directed polygon edge per cell-edge)

  3. Iterate over all created edges to create triangles (not trivial)

Any better solution?

Edit: I just realized that it is probably possible to walk through the cells in a special way to create the triangles directly (3 neighbor cells create a triangle).

like image 854
arthur.sw Avatar asked Dec 17 '15 18:12

arthur.sw


People also ask

How do you triangulate polygons?

A monotone polygon can be triangulated in linear time by using a simple greedy algorithm which repeatedly cuts off the convex corners of the polygon [Fournier and Montuno 1984]. Hence, all the monotone polygons can be triangulated in O(n) time.

How many ways are there to triangulate a polygon?

The 42 possible triangulations for a convex heptagon (7-sided convex polygon).

How do you triangulate a polygon with holes?

Let a polygon P with h holes have n vertices total, counting vertices on the holes as well as on the outer boundary. Then a triangulation of P has t = n + 2h - 2 triangles, and a quadrilateralization has q = n/2 + h — 1 quadrilaterals.


1 Answers

The main idea is to iterate through the Voronoi vertices, and create a triangle from the generating points of each cell incident on the Voronoi vertex. In the case of degenerate vertex with degree > 3 then you'll need to generate more than one triangle, but that is easily done using a triangle fan.

Using Boost Polygon:

#include "boost/polygon/voronoi.hpp"

std::vector<Point> vertices;
// add your input vertices

boost::polygon::voronoi_diagram<double> vd;
boost::polygon::construct_voronoi(vertices.begin(), vertices.end(), &vd);

for (const auto& vertex: vd.vertices()) {
    std::vector<Point> triangle;
    auto edge = vertex.incident_edge();
    do {
        auto cell = edge->cell();
        assert(cell->contains_point());

        triangle.push_back(vertices[cell->source_index()]);
        if (triangle.size() == 3) {
            // process output triangles
            std::cout << "Got triangle:" << triangle << std::endl;
            triangle.erase(triangle.begin() + 1);
        }

        edge = edge->rot_next();
    } while (edge != vertex.incident_edge());
}

See also How to triangulate from a Voronoï diagram? for more background on the problem.

like image 180
Giovanni Funchal Avatar answered Sep 20 '22 03:09

Giovanni Funchal