Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_803_Mesh_Fairing

Fairing a noisy open surface by minimizing area (degree 1) or curvature (degree 2).

Overview

This tutorial demonstrates implicit mesh fairing with SurfaceMeshFairing. The input is a noisy hemisphere — an open surface patch whose circular boundary is kept fixed. Two fairing modes are available: minimizing surface area (degree 1) and minimizing surface curvature (degree 2). Gaussian noise is added first so the smoothing effect is visible. Fairing is the constrained cousin of smoothing: instead of denoising a closed mesh everywhere, it reconstructs a smooth patch that interpolates prescribed boundary conditions, which is exactly what hole filling and surface reconstruction need.

The technique

Fairing solves a k-harmonic equation L^k x = 0 on the interior vertices, where L is the (cotangent) Laplace-Beltrami operator and the boundary vertices are held fixed as constraints. The degree k selects which energy is minimized: k = 1 minimizes surface area (a membrane / mean-curvature flow, producing a minimal-area patch), while k = 2 minimizes the integral of squared curvature (a thin-plate / biharmonic energy, producing a smoother, curvature-continuous patch). Because the system is solved implicitly in one sparse linear solve, it is stable and the result depends only on the boundary constraints, not on a timestep. This is the implicit fairing framework of Desbrun et al., "Implicit fairing of irregular meshes using diffusion and curvature flow" (SIGGRAPH 1999). With no vertex selection, the whole interior of the mesh is faired; locked/selected vertices act as constraints.

Easy3D API

Running it

The hemisphere starts noisy. Press Ctrl+a to minimize surface area (degree 1), Ctrl+c to minimize surface curvature (degree 2), and Ctrl+n to add Gaussian noise again for another 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_fairing.h>
32#include <easy3d/algo/gaussian_noise.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
82using namespace easy3d;
83
84namespace {
85
86 // Perturb the vertices by Gaussian noise scaled to the model size (0.5% of the bounding-box
87 // diagonal). The third argument (true) preserves the boundary, so the noisy patch still meets the
88 // fixed rim that fairing will interpolate.
89 void add_noise(SurfaceMesh *mesh) {
90 const float sigma = mesh->bounding_box().diagonal_length() * 0.005f;
91 GaussianNoise::apply(mesh, sigma, true);
92 }
93
94 // Shortcut handler: add noise again so the fairing can be re-tested.
95 bool noise(Viewer *viewer, Model *model) {
96 auto mesh = dynamic_cast<SurfaceMesh *>(model);
97 if (!mesh)
98 return false;
99 add_noise(mesh);
100 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
101 viewer->update(); // request a redraw
102 return true;
103 }
104
105 // Shortcut handler: fairing degree 1 — minimize surface area (membrane / mean-curvature flow).
106 bool fair_area(Viewer *viewer, Model *model) {
107 auto mesh = dynamic_cast<SurfaceMesh *>(model);
108 if (!mesh)
109 return false;
110 SurfaceMeshFairing fairing(mesh);
111 fairing.minimize_area();
112 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
113 viewer->update();
114 return true;
115 }
116
117 // Shortcut handler: fairing degree 2 — minimize surface curvature (thin-plate / biharmonic energy).
118 bool fair_curvature(Viewer *viewer, Model *model) {
119 auto mesh = dynamic_cast<SurfaceMesh *>(model);
120 if (!mesh)
121 return false;
122 SurfaceMeshFairing fairing(mesh);
123 fairing.minimize_curvature();
124 mesh->renderer()->update(); // positions changed: refresh the rendering buffers
125 viewer->update();
126 return true;
127 }
128
129} // namespace
130
131int main(int argc, char **argv) {
132 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
133 initialize();
134
135 // The hemisphere is an open surface patch: its fixed circular boundary gives fairing the
136 // constraints it needs to reconstruct a smooth interior.
137 const std::string file = resource::directory() + "/data/hemisphere.ply";
138
139 // Create the viewer and load the mesh (centered/scaled to fit the scene).
140 Viewer viewer(EXAMPLE_TITLE);
141 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
142 if (!mesh) {
143 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
144 return EXIT_FAILURE;
145 }
146
147 // Look at the hemisphere from the side so the patch and its rim are visible.
148 viewer.camera()->set_view_direction(vec3(0, 1, 0));
149
150 // Perturb the model so the fairing effect is clearly visible.
151 add_noise(mesh);
152 mesh->renderer()->update(); // show the noisy surface before any fairing
153
154 // Register the fairing/noise shortcuts and the on-screen help text.
155 viewer.bind_shortcut([](Viewer *v, Model *m) { return fair_area(v, m); }, mesh, Viewer::KEY_A, Viewer::MODIF_CTRL);
156 viewer.bind_shortcut([](Viewer *v, Model *m) { return fair_curvature(v, m); }, mesh, Viewer::KEY_C, Viewer::MODIF_CTRL);
157 viewer.bind_shortcut([](Viewer *v, Model *m) { return noise(v, m); }, mesh, Viewer::KEY_N, Viewer::MODIF_CTRL);
158 viewer.set_usage(
159 "default",
160 "Ctrl + a: minimize surface area (fairing degree 1)\n" "Ctrl + c: minimize surface curvature (fairing degree 2)\n" "Ctrl + n: add Gaussian noise again");
161
162 // Enter the interactive loop (returns when the window is closed).
163 return viewer.run();
164}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
A class for implicitly fairing a surface mesh.
Definition surface_mesh_fairing.h:26
void minimize_curvature()
Minimize surface curvature.
Definition surface_mesh_fairing.h:49
void minimize_area()
Minimize surface area.
Definition surface_mesh_fairing.h:43
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
Definition collider.cpp:182
See also
SurfaceMeshFairing, GaussianNoise, SurfaceMesh, Tutorial_802_Mesh_Smoothing, Tutorial_804_Mesh_HoleFilling