Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_602_ConvexPartition

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

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.

1
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>
35
86using namespace easy3d;
87
88std::shared_ptr<SurfaceMesh> create_partition_mesh() {
89 // the vertices of the polygon (including the holes).
90 // All contours share one vertex pool; each contour below refers to these points by index.
91 std::vector<vec2> points = {
92 vec2(0, 0), // 0: start of outer contour, in counterclockwise order
93 vec2(500, 0), // 1
94 vec2(500, 700), // 2
95 vec2(0, 700), // 3
96 vec2(100, 100), // 4: start of 1st hole, also in counterclockwise order
97 vec2(400, 100), // 5
98 vec2(400, 300), // 6
99 vec2(100, 300), // 7
100 vec2(100, 400), // 8: start of 2nd hole, also in counterclockwise order
101 vec2(400, 400), // 9
102 vec2(400, 600), // 10
103 vec2(100, 600), // 11
104 };
105
106 // the outer contour represented by the vertex indices. Note: must be in counterclockwise order.
107 std::vector<PolygonPartition::Polygon> polygons = {
108 {0, 1, 2, 3} // outer contour
109 };
110
111 // the interior contours representing holes. Note: hole vertices must be in clockwise order.
112 std::vector<PolygonPartition::Polygon> holes = {
113 {7, 6, 5, 4}, // {4, 5, 6, 7} won't work
114 {11, 10, 9, 8}, // {8, 9, 10, 11} won't work
115 };
116
117 // convex partition
118 // apply() runs Hertel-Mehlhorn over the outer contour plus the hole contours and returns
119 // the convex pieces as index lists into the shared 'points' pool.
120 std::vector<PolygonPartition::Polygon> parts;
121 if (!PolygonPartition::apply(points, polygons, holes, parts))
122 return {};
123
124 //--------------------------------------------------------
125
126 // Visualizing the result.
127 // we create a mesh to store the resulted convex polygons.
128 auto mesh = std::make_shared<SurfaceMesh>();
129 // Lift the 2D points into the z = 0 plane so the 3D viewer can display them.
130 for (const auto &p : points)
131 mesh->add_vertex({p.x, p.y, 0});
132 // Each convex part becomes one face; the indices already refer to the vertices added above.
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)));
137 mesh->add_face(vts);
138 }
139 return mesh;
140}
141
142int main(int argc, char **argv) {
143 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
144 initialize();
145 Viewer viewer(EXAMPLE_TITLE);
146
147 // Look straight down onto the XY plane: y is up on screen, the camera looks along -z.
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())
152 return EXIT_FAILURE;
153 // add the mesh to the viewer.
154 viewer.add_model(mesh);
155
156 // show the vertices (as sphere impostors, so they stay round at any zoom level)
157 mesh->renderer()->get_points_drawable("vertices")->set_visible(true);
158 mesh->renderer()->get_points_drawable("vertices")->set_impostor_type(easy3d::PointsDrawable::SPHERE);
159 mesh->renderer()->get_points_drawable("vertices")->set_point_size(8);
160 // show the edges (including the diagonals added by the partition)
161 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
162 // also show the borders (the outer boundary and the hole boundaries)
163 mesh->renderer()->get_lines_drawable("borders")->set_visible(true);
164
165 // Enter the interactive loop (returns when the window is closed).
166 return viewer.run();
167}
@ 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