Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_806_Mesh_Simplification

Reducing a mesh's vertex count with quadric-error-metric edge collapse (decimation).

Overview

This tutorial demonstrates mesh simplification (decimation) with SurfaceMeshSimplification. Each press of the shortcut collapses the mesh down to half of its current vertices (with a floor of 100), so you can watch detail disappear progressively while the overall shape is preserved. The edges are drawn to make the coarsening visible. Simplification is essential for level-of-detail rendering, faster processing, and compact storage of high-resolution scans.

The technique

The algorithm is incremental greedy decimation by halfedge collapse: repeatedly merge one vertex into a neighbor, removing the two adjacent triangles, until the target vertex count is reached. The key question is which collapse to do next and where to place the surviving vertex. Easy3D uses the quadric error metric of Garland and Heckbert, "Surface simplification using quadric error metrics" (SIGGRAPH 1997): each vertex accumulates a 4x4 symmetric matrix Q that encodes the sum of squared distances to the planes of its incident faces. The cost of placing a vertex at position p is p^T Q p, so the optimal collapse position minimizes this quadratic error, and collapses are prioritized by their error in a heap (cheapest first). This preserves the geometry far better than naive midpoint collapse. The implementation also follows the general decimation framework of Kobbelt et al. (Graphics Interface 1998), with optional fairness/quality constraints (aspect ratio, normal deviation, Hausdorff error).

Easy3D API

Running it

Press Ctrl+d to simplify to half of the current vertices; repeat to decimate further. The console prints the resulting vertex/face counts after each pass.

1
27#include <easy3d/viewer/viewer.h>
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/renderer/drawable_lines.h>
32#include <easy3d/algo/surface_mesh_simplification.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
36#include <algorithm>
37
82using namespace easy3d;
83
84namespace {
85
86 // Shortcut handler: decimate the mesh to half its current vertices (but never below 100).
87 bool simplify(Viewer *viewer, Model *model) {
88 auto mesh = dynamic_cast<SurfaceMesh *>(model);
89 if (!mesh)
90 return false;
91 SurfaceMeshSimplification simplification(mesh);
92 simplification.initialize(); // build per-vertex error quadrics and the collapse priority queue
93 const unsigned int target = std::max(100u, mesh->n_vertices() / 2);
94 simplification.simplify(target); // greedily collapse the cheapest edges until the target is met
95 std::cout << "simplified to " << mesh->n_vertices() << " vertices / " << mesh->n_faces() << " faces"
96 << std::endl;
97 mesh->renderer()->update(); // connectivity changed: refresh the rendering buffers
98 viewer->update(); // request a redraw
99 return true;
100 }
101
102} // namespace
103
104int main(int argc, char **argv) {
105 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
106 initialize();
107
108 const std::string file = resource::directory() + "/data/bunny.ply";
109
110 // Create the viewer and load the mesh (centered/scaled to fit the scene).
111 Viewer viewer(EXAMPLE_TITLE);
112 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
113 if (!mesh) {
114 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
115 return EXIT_FAILURE;
116 }
117
118 viewer.camera()->set_view_direction(vec3(0, 1, 0));
119
120 // Show the edges so the coarsening is visible.
121 mesh->renderer()->get_lines_drawable("edges")->set_visible(true);
122
123 // Register the simplify shortcut and the on-screen help text.
124 viewer.bind_shortcut([](Viewer *v, Model *m) { return simplify(v, m); }, mesh, Viewer::KEY_D, Viewer::MODIF_CTRL);
125 viewer.set_usage("default", "Ctrl + d: simplify to half of the current vertices");
126
127 // Enter the interactive loop (returns when the window is closed).
128 return viewer.run();
129}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Surface mesh simplification based on approximation error and fairness criteria.
Definition surface_mesh_simplification.h:33
void simplify(unsigned int n_vertices)
Simplify mesh to n vertices.
Definition surface_mesh_simplification.cpp:124
void initialize(float aspect_ratio=0.0, float edge_length=0.0, unsigned int max_valence=0, float normal_deviation=0.0, float hausdorff_error=0.0)
Initialize with given parameters.
Definition surface_mesh_simplification.cpp:42
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshSimplification, SurfaceMesh, DrawableLines, Tutorial_805_Mesh_Remeshing, Tutorial_807_Mesh_Subdivision