Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_605_Delaunay2D

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

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.

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/delaunay_2d.h>
34#include <easy3d/util/initializer.h>
35
36#include <random>
37
82using namespace easy3d;
83
84std::shared_ptr<SurfaceMesh> create_delaunay_mesh() {
85 // A set of 2D points (a deterministic random sample so the result is reproducible).
86 std::mt19937 gen(42);
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));
92
93 // Compute the 2D Delaunay triangulation.
94 Delaunay2 delaunay;
95 delaunay.set_vertices(points);
96
97 // Store the result in a SurfaceMesh for rendering.
98 auto mesh = std::make_shared<SurfaceMesh>();
99 for (unsigned int i = 0; i < delaunay.nb_vertices(); ++i) {
100 const vec2 &p = delaunay.vertex(i);
101 mesh->add_vertex(vec3(p, 0.0));
102 }
103 for (unsigned int t = 0; t < delaunay.nb_triangles(); ++t) {
104 mesh->add_triangle(SurfaceMesh::Vertex(delaunay.tri_vertex(t, 0)),
105 SurfaceMesh::Vertex(delaunay.tri_vertex(t, 1)),
106 SurfaceMesh::Vertex(delaunay.tri_vertex(t, 2)));
107 }
108 return mesh;
109}
110
111int main(int argc, char **argv) {
112 initialize();
113 Viewer viewer(EXAMPLE_TITLE);
114
115 viewer.camera()->set_up_vector(vec3(0, 1, 0));
116 viewer.camera()->set_view_direction(vec3(0.2, 0.2, -1));
117
118 auto mesh = create_delaunay_mesh();
119 if (!mesh || mesh->empty())
120 return EXIT_FAILURE;
121 viewer.add_model(mesh, true);
122
123 // show the vertices
124 mesh->renderer()->get_points_drawable("vertices")->set_visible(true);
125 mesh->renderer()->get_points_drawable("vertices")->set_impostor_type(easy3d::PointsDrawable::SPHERE);
126 mesh->renderer()->get_points_drawable("vertices")->set_point_size(8);
127 // show the edges (the Delaunay triangulation)
128 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
129 // also show the borders (the convex hull of the point set)
130 mesh->renderer()->get_lines_drawable("borders")->set_visible(true);
131
132 return viewer.run();
133}
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