Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_802_Mesh_Smoothing

Removing noise from a triangle mesh with explicit and implicit Laplacian smoothing.

Overview

This tutorial demonstrates mesh smoothing (denoising) with SurfaceMeshSmoothing. To make the effect obvious, Gaussian noise is first added to the bunny mesh; the smoothing shortcuts then progressively remove it. Two variants are provided: explicit Laplacian smoothing (a few local averaging iterations) and implicit Laplacian smoothing (a single global solve). Smoothing is the workhorse for cleaning up scanned or reconstructed geometry before further processing.

The technique

Laplacian smoothing moves each vertex toward the average of its neighbors, which attenuates high-frequency noise. The discrete Laplacian of the vertex positions is L(p); with the cotangent weights it approximates the mean-curvature normal, so iterating p <- p + dt * L(p) is a discretization of mean-curvature flow (the surface diffuses like heat). Explicit smoothing applies this update directly for a fixed number of iterations: it is cheap and local, but the timestep must be small or the iteration becomes unstable, and repeated averaging also shrinks the mesh and blurs features. Implicit smoothing instead solves (I - dt * L) p_new = p_old in one sparse linear system. Because the update is implicit (backward Euler), it is unconditionally stable for large timesteps and removes noise in a single step, at the cost of solving a global system. Both approaches are described in Desbrun et al., "Implicit fairing of irregular meshes using diffusion and curvature flow" (SIGGRAPH 1999); see also Kazhdan et al. (CGF 2012) on non-singular mean-curvature flow. Easy3D defaults to the cotangent (geometry-aware) Laplacian rather than uniform weights.

Easy3D API

Running it

The model starts noisy. Press Ctrl+e for explicit smoothing (10 iterations), Ctrl+i for implicit smoothing, and Ctrl+n to add Gaussian noise again so you can repeat the comparison.

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/algo/surface_mesh_smoothing.h>
32#include <easy3d/algo/gaussian_noise.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
83using namespace easy3d;
84
85namespace {
86
87 // Perturb every vertex along its normal by Gaussian noise. The standard deviation is scaled to
88 // the model size (0.3% of the bounding-box diagonal) so the noise is visible but not extreme.
89 void add_noise(SurfaceMesh *mesh) {
90 const float sigma = mesh->bounding_box().diagonal_length() * 0.003f;
91 GaussianNoise::apply(mesh, sigma);
92 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
93 }
94
95 // Shortcut handler: add noise again so the smoothing can be re-tested.
96 bool noise(Viewer *viewer, Model *model) {
97 auto mesh = dynamic_cast<SurfaceMesh *>(model);
98 if (!mesh)
99 return false;
100 add_noise(mesh);
101 mesh->renderer()->update();
102 viewer->update(); // request a redraw
103 return true;
104 }
105
106 // Shortcut handler: explicit Laplacian smoothing — 10 local neighbor-averaging iterations.
107 bool smooth_explicit(Viewer *viewer, Model *model) {
108 auto mesh = dynamic_cast<SurfaceMesh *>(model);
109 if (!mesh)
110 return false;
111 SurfaceMeshSmoothing smoothing(mesh);
112 smoothing.explicit_smoothing(10);
113 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
114 viewer->update();
115 return true;
116 }
117
118 // Shortcut handler: implicit Laplacian smoothing — one stable global solve (default timestep).
119 bool smooth_implicit(Viewer *viewer, Model *model) {
120 auto mesh = dynamic_cast<SurfaceMesh *>(model);
121 if (!mesh)
122 return false;
123 SurfaceMeshSmoothing smoothing(mesh);
124 smoothing.implicit_smoothing();
125 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
126 viewer->update();
127 return true;
128 }
129
130} // namespace
131
132int main(int argc, char **argv) {
133 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
134 initialize();
135
136 // Create the viewer and load the bunny triangle mesh (centered/scaled to fit the scene).
137 Viewer viewer(EXAMPLE_TITLE);
138
139 const std::string file = resource::directory() + "/data/bunny.ply";
140 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
141 if (!mesh) {
142 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
143 return EXIT_FAILURE;
144 }
145
146 // Look at the bunny from the front.
147 viewer.camera()->set_view_direction(vec3(0, 1, 0));
148
149 // Perturb the model so the smoothing effect is clearly visible.
150 add_noise(mesh);
151
152 // Register the smoothing/noise shortcuts and the on-screen help text.
153 viewer.bind_shortcut([](Viewer *v, Model *m) { return smooth_explicit(v, m); }, mesh, Viewer::KEY_E, Viewer::MODIF_CTRL);
154 viewer.bind_shortcut([](Viewer *v, Model *m) { return smooth_implicit(v, m); }, mesh, Viewer::KEY_I, Viewer::MODIF_CTRL);
155 viewer.bind_shortcut([](Viewer *v, Model *m) { return noise(v, m); }, mesh, Viewer::KEY_N, Viewer::MODIF_CTRL);
156 viewer.set_usage(
157 "default",
158 "Ctrl + e: explicit Laplacian smoothing (10 iterations)\n" "Ctrl + i: implicit Laplacian smoothing\n" "Ctrl + n: add Gaussian noise again");
159
160 // Enter the interactive loop (returns when the window is closed).
161 return viewer.run();
162}
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
A class for Laplacian smoothing.
Definition surface_mesh_smoothing.h:26
void implicit_smoothing(float timestep=0.001, bool use_uniform_laplace=false, bool rescale=false)
Perform implicit Laplacian smoothing with timestep.
Definition surface_mesh_smoothing.cpp:127
void explicit_smoothing(unsigned int iters=10, bool use_uniform_laplace=false)
Perform iters iterations of explicit Laplacian smoothing.
Definition surface_mesh_smoothing.cpp:77
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshSmoothing, GaussianNoise, SurfaceMesh, Tutorial_803_Mesh_Fairing