Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_804_Mesh_HoleFilling

Closing the boundary loops (holes) of an open triangle mesh with a smooth filled patch.

Overview

This tutorial demonstrates hole filling with SurfaceMeshHoleFilling. The bunny mesh is not watertight — it has open boundary loops (for example around its base). Those loops are highlighted in red with the "borders" drawable so you can see exactly what will be closed. Pressing the shortcut triangulates each hole and then refines and fairs the new patch so it blends smoothly into the surrounding surface. Hole filling is a common repair step for scanned meshes that have missing data.

The technique

A hole is a boundary loop of manifold vertices. Filling it well is more than just triangulating the loop: a naive fan triangulation creates poor-quality, visible patches. Easy3D follows Peter Liepa, "Filling holes in meshes" (SGP 2003), a three-stage pipeline. (1) Triangulate the hole with a dynamic- programming search that minimizes a combined angle/area weight, avoiding sharp dihedral angles and large flat triangles. (2) Refine the patch by isometric remeshing (splitting long edges, collapsing short ones, flipping, and relaxing) so the new triangles match the density and quality of the existing mesh. (3) Fair the patch by minimizing curvature while keeping the original boundary fixed, so the filled region joins the surrounding surface smoothly rather than as a flat cap. The size argument limits filling to holes whose boundary loop has at most that many edges.

Easy3D API

Running it

The boundary loops are drawn in red. Press Ctrl+f to fill the holes; the console reports how many holes were filled and the red borders disappear as the mesh becomes closed.

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_hole_filling.h>
33#include <easy3d/util/resource.h>
34#include <easy3d/util/initializer.h>
35
80using namespace easy3d;
81
82namespace {
83
84 // Shortcut handler: close every hole whose boundary loop has at most 1000 edges.
85 bool fill_holes(Viewer *viewer, Model *model) {
86 auto mesh = dynamic_cast<SurfaceMesh *>(model);
87 if (!mesh)
88 return false;
89 // fill_holes() triangulates each hole, then refines and fairs the patch (Liepa 2003).
90 SurfaceMeshHoleFilling filling(mesh);
91 const int n = filling.fill_holes(1000);
92 std::cout << "filled " << n << " hole(s)" << std::endl;
93 mesh->renderer()->update(); // connectivity/positions changed: refresh the rendering buffers
94 viewer->update(); // request a redraw (the red borders are now gone)
95 return true;
96 }
97
98} // namespace
99
100int main(int argc, char **argv) {
101 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
102 initialize();
103
104 // The bunny has open boundary loops (holes), e.g. around its base.
105 const std::string file = resource::directory() + "/data/bunny.ply";
106
107 // Create the viewer and load the mesh (centered/scaled to fit the scene).
108 Viewer viewer(EXAMPLE_TITLE);
109 auto mesh = dynamic_cast<SurfaceMesh *>(viewer.add_model(file, true));
110 if (!mesh) {
111 std::cerr << "failed to load model. Please make sure the file exists and format is correct" << std::endl;
112 return EXIT_FAILURE;
113 }
114
115 // Turn the camera to look at the bottom of the bunny model
116 viewer.camera()->set_up_vector(vec3(0, 1, 0));
117 viewer.camera()->set_view_direction(vec3(0, 0, 1));
118
119 // Highlight the boundary loops (the holes to be filled). "borders" is the renderer's built-in
120 // lines drawable for boundary edges; color it red and thicken it so the holes stand out.
121 auto borders = mesh->renderer()->get_lines_drawable("borders");
122 borders->set_visible(true);
123 borders->set_uniform_coloring(vec4(1.0f, 0.0f, 0.0f, 1.0f));
124 borders->set_line_width(3.0f);
125
126 // Register the fill shortcut and the on-screen help text.
127 viewer.bind_shortcut([](Viewer *v, Model *m) { return fill_holes(v, m); }, mesh, Viewer::KEY_F, Viewer::MODIF_CTRL);
128 viewer.set_usage("default", "Ctrl + f: fill the holes");
129
130 // Enter the interactive loop (returns when the window is closed).
131 return viewer.run();
132}
Base class for spatial models with optional rendering and manipulation state.
Definition model.h:53
This class closes simple holes in a surface mesh.
Definition surface_mesh_hole_filling.h:31
int fill_holes(int size=500)
Fill all holes with size smaller than the specified size.
Definition surface_mesh_hole_filling.cpp:99
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
SurfaceMeshHoleFilling, SurfaceMesh, DrawableLines, Tutorial_803_Mesh_Fairing, Tutorial_805_Mesh_Remeshing