What is the best way to triangulate a polygon with Boost?
I use Boost.polygon.
My current algorithm:
Compute a voronoï diagram from my polygon vertices.
Create one directed polygon-edge for each cell-edge (this will create two directed polygon edge per cell-edge)
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).
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.
The 42 possible triangulations for a convex heptagon (7-sided convex polygon).
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With