Partitioning a simple polygon with holes into convex pieces using PolygonPartition.
Overview
This tutorial takes a rectangle with two rectangular holes and splits it into a set of convex polygons with PolygonPartition::apply(). Convex pieces are the "safe" currency of computational geometry: they are trivial to triangulate, rasterize, and test for point containment or collision, so many pipelines first decompose a complicated region into convex parts.
The technique
A polygon is convex when every interior angle is at most 180 degrees, i.e. the segment between any two interior points stays inside. A concave (or multiply-connected) polygon can always be cut along diagonals into convex pieces. Easy3D's PolygonPartition::apply() uses the Hertel-Mehlhorn algorithm: triangulate the polygon, then repeatedly merge two adjacent triangles across a diagonal whenever the merged piece is still convex (equivalently, whenever the diagonal is "inessential" — removing it does not create a reflex vertex). Hertel and Mehlhorn proved this yields at most four times the optimal number of parts, and in practice it is usually optimal; it runs in O(n^2) time and O(n) space. The truly optimal partition (fewest convex pieces) is available as apply_OPT() via the Keil-Snoeyink algorithm, at a higher O(n^3) cost, but it accepts only a single contour without holes. In apply(), each hole contour is first merged into the contour that encloses it, and the resulting hole-free polygons are partitioned. Each input contour must be simple (no self-intersections); this is a precondition, not something the partitioner checks. For self-intersecting or overlapping contours, first run the CSG operators in easy3d/algo/tessellator.h to obtain simple contours. References: S. Hertel and K. Mehlhorn, "Fast triangulation of simple polygons", 1983; M. Keil and J. Snoeyink, "On the time bound for convex decomposition of simple polygons", 1998.
Easy3D API
- PolygonPartition::apply(points, polys, holes, parts): the all-in-one entry point. points is the shared vertex pool; polys are the outer contours (counter-clockwise); holes are interior contours (clockwise); parts receives the resulting convex polygons as index lists. PolygonPartition::Polygon is just a std::vector of vertex indices.
- SurfaceMesh: the convex parts are stored as faces for display.
- Viewer, PointsDrawable, LinesDrawable: show the vertices, the partition edges, and the borders.
Running it
A window opens showing the rectangle-with-two-holes cut into convex faces; the added diagonals are visible as edges and the hole boundaries as borders. Note the orientation convention in the code: outer contours must be counter-clockwise and holes clockwise, or apply() will fail.
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/renderer/camera.h>
29#include <easy3d/core/surface_mesh.h>
30#include <easy3d/renderer/drawable_points.h>
31#include <easy3d/renderer/drawable_lines.h>
32#include <easy3d/renderer/renderer.h>
33#include <easy3d/algo/polygon_partition.h>
34#include <easy3d/util/initializer.h>
88std::shared_ptr<SurfaceMesh> create_partition_mesh() {
91 std::vector<vec2> points = {
107 std::vector<PolygonPartition::Polygon> polygons = {
112 std::vector<PolygonPartition::Polygon> holes = {
120 std::vector<PolygonPartition::Polygon> parts;
121 if (!PolygonPartition::apply(points, polygons, holes, parts))
128 auto mesh = std::make_shared<SurfaceMesh>();
130 for (
const auto &p : points)
131 mesh->add_vertex({p.x, p.y, 0});
133 for (
const auto &poly : parts) {
134 std::vector<SurfaceMesh::Vertex> vts;
135 for (
const auto &
id : poly)
136 vts.push_back(
SurfaceMesh::Vertex(static_cast<int>(id)));
142int main(
int argc,
char **argv) {
145 Viewer viewer(EXAMPLE_TITLE);
148 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
149 viewer.camera()->set_view_direction(
vec3(0, 0, -1));
150 auto mesh = create_partition_mesh();
151 if (!mesh || mesh->empty())
154 viewer.add_model(mesh);
157 mesh->renderer()->get_points_drawable(
"vertices")->set_visible(
true);
159 mesh->renderer()->get_points_drawable(
"vertices")->set_point_size(8);
161 mesh->renderer()->get_lines_drawable(
"edges")->set_visible(
true);
163 mesh->renderer()->get_lines_drawable(
"borders")->set_visible(
true);
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
- See also
- PolygonPartition, SurfaceMesh, Viewer, Tutorial_601_Tessellator, Tutorial_603_CSG