Triangulating general polygonal faces (concave, self-intersecting, and with holes) using the Tessellator.
This tutorial builds a tiny SurfaceMesh whose three faces are deliberately "hard" to triangulate — a concave quad, a self-intersecting star, and a quad with a rectangular hole — and then converts the whole mesh into triangles with the Tessellator. It shows the general recipe for turning arbitrary planar contours into a triangle soup that a GPU can actually rasterize.
Graphics hardware renders triangles, so any polygon that is not already a triangle must be decomposed. Simple convex polygons are easy (a fan from one vertex), but concave polygons, polygons with holes, and self-intersecting contours break naive fans and ear-clipping. The robust solution, used by the OpenGL Utility (GLU) tessellator and by Easy3D's Tessellator, is a sweep-line algorithm: the contour edges are intersected to form a planar subdivision, and each resulting face is classified as interior or exterior using a winding rule. The winding number of a point counts how many times the contour loops around it; WINDING_NONZERO fills every region with a non-zero count (so a self-intersecting star becomes a solid pentagram), while WINDING_ODD fills only regions encircled an odd number of times (which is how a hole — a contour nested inside the outer boundary — is carved out). All input is first projected onto the plane perpendicular to a supplied normal, and the output triangles are oriented counter-clockwise with respect to that normal. See the OpenGL Programming Guide (the "Red Book"), chapter 11, for the classic description of winding rules and the GLU tessellator.
A window opens showing the three tessellated shapes side by side, with spheres at the vertices and the triangle edges and borders drawn. The star is filled solid (non-zero rule) and the third quad has a rectangular hole (odd rule). For general rendering you can also let the tessellator build a DrawableTriangles directly, without explicitly rewriting the mesh.
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/tessellator.h>
34#include <easy3d/util/initializer.h>
88typedef std::vector<vec3> Hole;
97 mesh->update_face_normals();
98 auto normals = mesh->face_property<
vec3>(
"f:normal");
100 auto holes = mesh->get_face_property<Hole>(
"f:holes");
103 for (
auto f : mesh->faces()) {
110 tessellator.set_winding_rule(Tessellator::WINDING_NONZERO);
111 tessellator.begin_contour();
112 for (
auto h : mesh->halfedges(f)) {
116 tessellator.add_vertex(mesh->position(v), v.
idx());
118 tessellator.end_contour();
120 if (holes && holes[f].size() >= 3) {
123 tessellator.set_winding_rule(Tessellator::WINDING_ODD);
124 tessellator.begin_contour();
125 for (
const auto &p : holes[f])
126 tessellator.add_vertex(p);
127 tessellator.end_contour();
131 tessellator.end_polygon();
139 const auto &triangles = tessellator.elements();
140 if (triangles.empty())
145 const auto &vts = tessellator.vertices();
147 mesh->add_vertex(
vec3(v->data()));
150 for (
const auto &t : triangles) {
156std::shared_ptr<SurfaceMesh> create_tessellation_mesh() {
160 auto mesh = std::make_shared<SurfaceMesh>();
164 auto v0 = mesh->add_vertex(
vec3(0, 0, 0));
165 auto v1 = mesh->add_vertex(
vec3(800, 0, 0));
166 auto v2 = mesh->add_vertex(
vec3(800, 800, 0));
167 auto v3 = mesh->add_vertex(
vec3(600, 300, 0));
168 mesh->add_quad(v0, v1, v2, v3);
174 auto vertices = {mesh->add_vertex(
vec3(1500, 0, 0)), mesh->add_vertex(
vec3(1300, 800, 0)),
175 mesh->add_vertex(
vec3(1100, 0, 0)), mesh->add_vertex(
vec3(1700, 500, 0)),
176 mesh->add_vertex(
vec3(900, 500, 0))};
177 mesh->add_face(vertices);
181 auto vertices = {mesh->add_vertex(
vec3(1800, 0, 0)), mesh->add_vertex(
vec3(2200, 0, 0)),
182 mesh->add_vertex(
vec3(2200, 700, 0)), mesh->add_vertex(
vec3(1800, 700, 0))};
183 auto f = mesh->add_face(vertices);
188 auto holes = mesh->add_face_property<Hole>(
"f:holes");
189 holes[f] = {
vec3(1900, 100, 0),
vec3(2100, 100, 0),
vec3(2100, 600, 0),
vec3(1900, 600, 0)};
194 triangulate(mesh.get());
201int main(
int argc,
char **argv) {
204 Viewer viewer(EXAMPLE_TITLE);
207 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
208 viewer.camera()->set_view_direction(
vec3(0, 0, -1));
209 auto mesh = create_tessellation_mesh();
210 if (!mesh || mesh->empty())
213 viewer.add_model(mesh,
true);
216 mesh->renderer()->get_points_drawable(
"vertices")->set_visible(
true);
218 mesh->renderer()->get_points_drawable(
"vertices")->set_point_size(8);
220 mesh->renderer()->get_lines_drawable(
"edges")->set_visible(
true);
222 mesh->renderer()->get_lines_drawable(
"borders")->set_visible(
true);
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
int idx() const
Get the underlying index of this handle.
Definition surface_mesh.h:72
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Tessellator subdivides concave planar polygons, polygons with holes, or polygons with intersecting ed...
Definition tessellator.h:55
void begin_polygon(const vec3 &normal)
Begin the tessellation of a complex polygon.
Definition tessellator.cpp:145
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
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135