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
- SurfaceMeshHoleFilling::fill_holes(size): fills every hole whose boundary loop has <= size edges and returns the number of holes filled (fill_hole() fills a single hole given a boundary halfedge).
- DrawableLines ("borders"): the renderer's boundary-loop drawable; set_visible(), set_uniform_coloring(), and set_line_width() make the holes easy to spot before filling.
- Renderer::update(): re-extracts the rendering buffers after the connectivity and positions change.
- Viewer::bind_shortcut() / set_usage(): register the Ctrl shortcut and the help text.
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.
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>
92 std::cout <<
"filled " << n <<
" hole(s)" << std::endl;
93 mesh->renderer()->update();
100int main(
int argc,
char **argv) {
105 const std::string file = resource::directory() +
"/data/bunny.ply";
108 Viewer viewer(EXAMPLE_TITLE);
109 auto mesh =
dynamic_cast<SurfaceMesh *
>(viewer.add_model(file,
true));
111 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
116 viewer.camera()->set_up_vector(
vec3(0, 1, 0));
117 viewer.camera()->set_view_direction(
vec3(0, 0, 1));
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);
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");
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