Computing and visualizing the 2D Delaunay triangulation of a point set with Delaunay2.
Overview
This tutorial scatters 80 random 2D points, computes their Delaunay triangulation with Delaunay2, and reads the result back into a SurfaceMesh for display. It is the canonical example of turning an unstructured point set into a well-shaped triangle mesh — a building block for terrain modeling, mesh generation, and spatial interpolation.
The technique
A Delaunay triangulation of a point set is a triangulation such that no input point lies inside the circumcircle of any triangle (the "empty circumcircle" property). Among all possible triangulations of the same points, the Delaunay triangulation maximizes the minimum angle, so it avoids skinny "sliver" triangles and produces the most equilateral, numerically well-behaved mesh. It is also the geometric dual of the Voronoi diagram: two points are connected by a Delaunay edge exactly when their Voronoi cells share a boundary. The triangulation covers the convex hull of the point set, which is why the outer border of the result is the convex hull. Easy3D's Delaunay2 wraps Jonathan Richard Shewchuk's robust "Triangle" implementation. References: B. Delaunay, "Sur la sphere vide", 1934; J. R. Shewchuk, "Triangle: Engineering a 2D
Quality Mesh Generator and Delaunay Triangulator", 1996.
Easy3D API
- Delaunay2: set_vertices() computes the triangulation; nb_vertices()/vertex(i) read back the points; nb_triangles()/tri_vertex(t, lv) read back each triangle's three vertex indices.
- SurfaceMesh: stores the vertices (lifted to z = 0) and triangles for rendering.
- Viewer, PointsDrawable, LinesDrawable: show the points, the triangulation edges, and the convex-hull border.
Running it
A window opens showing the 80 points (as spheres) connected by the Delaunay edges, with the convex hull highlighted as the border. The camera looks almost straight down (with a slight tilt) so the flat triangulation is easy to read. The random sample is seeded, so the result is reproducible.
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/delaunay_2d.h>
34#include <easy3d/util/initializer.h>
84std::shared_ptr<SurfaceMesh> create_delaunay_mesh() {
87 std::uniform_real_distribution<float> dis_x(0.0f, 800.0f);
88 std::uniform_real_distribution<float> dis_y(0.0f, 600.0f);
89 std::vector<vec2> points;
90 for (
int i = 0; i < 80; ++i)
91 points.emplace_back(dis_x(gen), dis_y(gen));
98 auto mesh = std::make_shared<SurfaceMesh>();
99 for (
unsigned int i = 0; i < delaunay.
nb_vertices(); ++i) {
101 mesh->add_vertex(
vec3(p, 0.0));
103 for (
unsigned int t = 0; t < delaunay.
nb_triangles(); ++t) {
111int main(
int argc,
char **argv) {
113 Viewer viewer(EXAMPLE_TITLE);
115 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
116 viewer.camera()->set_view_direction(
vec3(0.2, 0.2, -1));
118 auto mesh = create_delaunay_mesh();
119 if (!mesh || mesh->empty())
121 viewer.add_model(mesh,
true);
124 mesh->renderer()->get_points_drawable(
"vertices")->set_visible(
true);
126 mesh->renderer()->get_points_drawable(
"vertices")->set_point_size(8);
128 mesh->renderer()->get_lines_drawable(
"edges")->set_visible(
true);
130 mesh->renderer()->get_lines_drawable(
"borders")->set_visible(
true);
2D Delaunay triangulation, using Jonathan Richard Shewchuk's "triangle" implementation.
Definition delaunay_2d.h:30
unsigned int nb_triangles() const
Returns the number of triangles.
Definition delaunay_2d.h:105
const vec2 & vertex(unsigned int i) const
Returns the coordinates of the vertex with index i.
Definition delaunay_2d.h:78
int tri_vertex(unsigned int t, unsigned int lv) const
Returns the index of the lv-th vertex in the t-th triangle.
Definition delaunay_2d.h:113
void set_vertices(unsigned int nb_vertices, const float *pts) override
Sets the vertices from an array of floating point numbers, in which each consecutive number pair deno...
Definition delaunay_2d.cpp:62
unsigned int nb_vertices() const
Returns the number of vertices.
Definition delaunay.h:80
@ SPHERE
The points will be drawn as spheres.
Definition drawable_points.h:62
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135
- See also
- Delaunay2, SurfaceMesh, Viewer, Tutorial_601_Tessellator