Skip to content

Hole filling ​

What it does ​

surface%fill_holes closes the small holes of a triangulated surface: gaps left by a CAD export, a missing facet, a cut that was never capped. A hole is a closed loop of boundary edges, facet edges with no neighbouring facet. Each loop that is small enough is triangulated with a minimum-weight patch. The patch is oriented like the surrounding surface and reuses its vertices exactly, so after the fill the surface is connected across it.

For immersed-boundary preprocessing this matters because the default pseudo-normal sign and the per-shell orientation are exact only on closed surfaces. A filled surface can also be checked with is_watertight(). Genuine open boundaries (a sheet, an inlet cut, a symmetry plane) are left open by two size thresholds.

A unit sphere (marching cubes) with three small holes and its cap above z = 0.55 cut off. fill_holes at its defaults closes the three holes with 11 facets; the opening is larger than max_hole_size (0.1 of the box diagonal) and stays open:

left, a sphere with its top cut off and three small holes on its side, every boundary loop in red; right, after fill_holes: the three holes closed by small ember patches, the cut at the top still outlined in red

Pipeline ​

  1. Boundary loops.

    • After analyze, a facet edge without a neighbour (fcon_edge == 0) is either a boundary edge (one incident facet) or lies on a non-manifold edge (three or more incident facets, none linked). Both are chained; the counts tell them apart. Edges are matched on the welded vertex pool, so coincident coordinates count as one vertex.
    • Each boundary edge is reversed with respect to its facet: a patch triangle sharing it must traverse it the other way to keep the surface consistently oriented.
    • Edges are chained into closed walks. A walk that passes a vertex twice (a pinch, where two holes touch at a corner) is split there into simple loops, so no angle heuristic is needed.
    • Boundary edges that cannot close a walk (inconsistent winding, non-manifold boundaries) are reported as open_edges and left alone. The non-manifold edges met are reported as nonmanifold_edges, once per edge: they are not holes. Before issue #88 their facet edges left out of the loops were counted in open_edges.
    • By default a loop may pass through the facet edges of a non-manifold edge. manifold_only=.true. (issue #91) chains true boundary edges only: the boundary edges that then close no loop are reported as open_edges and left open.
  2. Selection. A loop is filled only if both hold:

    • it has at most max_hole_edges edges (default 200; the triangulation is O(n³));
    • its bounding-box diagonal is at most max_hole_size times the surface's (default 0.1).

    Larger loops are counted in holes_skipped.

    a sphere with four round holes of different sizes, closed one per frame by fill_holes(refine, fair) as max_hole_size grows past each loop's size: the patches in ember, the loops still open outlined

  3. Triangulation. Liepa's minimum-weight dynamic programming over the loop's sub-polygons. The weight is compared lexicographically:

    • first the largest dihedral angle between a patch triangle and its neighbours, both inside the patch and across the loop edges to the existing facets;
    • then the total area.

    Flat holes therefore get flat patches, and a hole wrapping an edge of the body follows that edge.

  4. Refinement (optional, refine=.true.). The patch is refined to the edge density of its loop (Liepa 2003):

    • each loop vertex gets the mean length of its two loop edges as target scale, a new vertex the mean scale of the triangle it splits;
    • a triangle is split into three at its centroid c when √2 |c − v| exceeds max(scale at c, scale at v) for each of its vertices v;
    • interior edges are flipped when the two opposite angles sum to more than π (not locally Delaunay), if the flip keeps the patch's orientation;
    • split and relax alternate to a fixed point. Loop vertices never move.
  5. Fairing (optional, fair=.true., implies refinement). The patch's interior vertices move to the thin-plate surface spanned by the loop and its 1-ring in the mesh: with the uniform Laplacian (L x)ᵥ = Σ_{u∼v} (xᵤ − xᵥ), the equations (L (L x))ᵢ = 0 at the interior vertices, with the loop and its ring fixed, form a symmetric positive-definite system, solved by conjugate gradient (fossil_linear_solver) once per coordinate. A hole in a curved surface gets a curved patch with matching tangents. The ring is feature-aware: only mesh facets within 60° of the patch's own normal at a loop vertex carry tangents, so a hole along a sharp edge keeps the crease and a planar hole stays planar.

  6. Re-analysis. The patches are appended and the surface is re-analysed with the same AABB tree kind and refinement.

The three stages on a unit sphere of 32 × 16 longitude-latitude facets (volume 4.12194) with the cap z > 0.7 removed, filled with max_hole_size=1 (the program is docs/examples/src/fig_ref_hole_filling.f90):

the sphere with its cap filled by the minimum-weight patch alone, in ember: a flat disk of 30 long thin triangles fanning across the planar loop

the same hole with refine: still a flat disk, now of 186 triangles about as large as the facets around the loop

the same hole with fair: the 186 triangles raised into a dome that continues the sphere's curvature across the loop

$ fig_ref_hole_filling
sphere: 960 facets, watertight T, volume  4.12194
refine F, fair F: holes 1, patch facets 30, max distance from the sphere  0.255, volume  3.88616, watertight T
refine T, fair F: holes 1, patch facets 186, max distance from the sphere  0.292, volume  3.88616, watertight T
refine T, fair T: holes 1, patch facets 186, max distance from the sphere  0.035, volume  4.14567, watertight T

The loop is a planar ring, so the minimum-weight patch is flat and refinement keeps it flat: both enclose 3.88616. Fairing raises it to the thin plate through the loop and its ring: the patch centroids lie within 0.035 of the sphere, against 0.255 for the flat patch. The thin plate is not the sphere: the faired volume, 4.14567, is more than the closed polyhedron's 4.12194 and less than the sphere's 4π/3 = 4.18879.

Inside sanitize (with fill_holes=.true.) the fill runs after orientation: loops are chained along consistently wound edges, and on mixed winding they would not close. If anything was filled, orientation runs once more, because a closed hole can turn an open part into a closed shell, which is then oriented by its nesting parity.

API ​

fortran
call surface%fill_holes(max_hole_edges, max_hole_size, holes_filled, holes_skipped, open_edges, status, refine, fair, &
                        nonmanifold_edges, manifold_only)
   class(surface_stl_object), intent(inout)         :: self
   integer(I4P),              intent(in),  optional :: max_hole_edges ! default 200
   real(R8P),                 intent(in),  optional :: max_hole_size  ! default 0.1 (of the bbox diagonal)
   integer(I4P),              intent(out), optional :: holes_filled
   integer(I4P),              intent(out), optional :: holes_skipped  ! loops over the thresholds
   integer(I4P),              intent(out), optional :: open_edges     ! boundary edges not forming a loop
   integer(I4P),              intent(out), optional :: status         ! STATUS_OK
   logical,                   intent(in),  optional :: refine         ! default .false.
   logical,                   intent(in),  optional :: fair           ! default .false.; implies refine
   integer(I4P),              intent(out), optional :: nonmanifold_edges ! non-manifold edges met (3+ facets)
   logical,                   intent(in),  optional :: manifold_only  ! default .false.: loops through true boundary edges only

call surface%sanitize(fill_holes=.true., max_hole_edges=..., max_hole_size=..., refine_holes=..., fair_holes=..., &
                      manifold_holes=...)

The surface must be analysed: load_from_file, adopt_facets and sanitize all do it. sanitize fills nothing unless fill_holes=.true., so existing pipelines are unchanged.

From the command line:

bash
fossilizer -i part.stl --sanitize --fill_holes --max_hole_size 0.05 --output part-closed
fossilizer -i part.stl --sanitize --fill_holes --fair_holes --output part-closed   # refined and faired patches

If the fairing solve fails (it should not on a valid patch), the refined patch is used unfaired and a warning is written to stderr.

Example ​

Tutorial 2 closes a hole in the dragon with sanitize(fill_holes=.true.), and the cookbook closes the open top of a box. This program makes three holes in the unit cube cube-01.stl, whose faces are 2 × 2 grids of quads split in two, and lets the size threshold decide which are filled:

f90
use fossil,              only : surface_stl_object
use fossil_facet_object, only : facet_object
use penf,                only : I4P, R8P
implicit none
type(surface_stl_object)        :: cube, holed, dragon, strict
type(facet_object), allocatable :: kept(:), every(:)
type(facet_object), pointer     :: f
integer(I4P)                    :: i, m, filled, skipped, open_edges, nonmanifold

call cube%load_from_file(file_name='cube-01.stl', guess_format=.true.)
allocate(kept(cube%get_facets_number()))
m = 0
do i = 1, cube%get_facets_number()
   f => cube%facet_at(i)
   if (f%normal%z < -0.5_R8P) cycle                                               ! the face z = 0, whole
   if (f%normal%z > 0.5_R8P .and. abs(f%centroid%x + f%centroid%y - 7._R8P / 6._R8P) < 1.e-9_R8P .and. &
       abs(abs(f%centroid%x - f%centroid%y) - 0.5_R8P) < 1.e-9_R8P) cycle       ! two facets of z = 1, a pinch
   m = m + 1 ; kept(m) = f
enddo
call move_alloc(kept, every)
allocate(kept, source=every(1:m))
call holed%adopt_facets(facets=kept)
print '(A,I0,A,L1)', 'cut cube: ', holed%get_facets_number(), ' facets, watertight ', holed%is_watertight()
f90
call holed%fill_holes(holes_filled=filled, holes_skipped=skipped, open_edges=open_edges)
print '(A,I0,A,I0,A,I0)', 'default max_hole_size 0.1: filled ', filled, ', skipped ', skipped, ', open edges ', open_edges
call holed%fill_holes(max_hole_size=0.5_R8P, holes_filled=filled, holes_skipped=skipped)
print '(A,I0,A,I0)', 'max_hole_size 0.5:         filled ', filled, ', skipped ', skipped
call holed%fill_holes(max_hole_size=1._R8P, holes_filled=filled, holes_skipped=skipped)
print '(A,I0,A,I0)', 'max_hole_size 1:           filled ', filled, ', skipped ', skipped
print '(A,I0,A,L1,A,F8.5)', 'result: ', holed%get_facets_number(), ' facets, watertight ', holed%is_watertight(), &
                            ', volume ', holed%get_volume()
$ ref_hole_filling
cut cube: 38 facets, watertight F
default max_hole_size 0.1: filled 0, skipped 3, open edges 0
max_hole_size 0.5:         filled 2, skipped 1
max_hole_size 1:           filled 1, skipped 0
result: 46 facets, watertight T, volume  1.00000
WARNING: sanitize: 62 duplicate facet(s) removed
WARNING: sanitize: 129 facet(s) with 1 disconnected edge remain
WARNING: sanitize: 14 facet(s) with 2 disconnected edges remain
WARNING: sanitize: 49 facet(s) with 3 disconnected edges remain
WARNING: sanitize: 54 non-manifold edge(s) (3+ incident facets) detected
dragon, default:              filled 54, open edges 0, non-manifold edges 54
dragon, manifold_only=.true.: filled 20, open edges 65, non-manifold edges 54
  • The holes. The face z = 0 is removed whole: one loop of 8 edges around the square. On the face z = 1 two facets are removed that touch only at the centre of the face: their boundary is one walk through that vertex twice, which the pinch rule splits into two loops of 3 edges. Three loops in all, none of them leftover open_edges.
  • The size threshold. The cube's bounding-box diagonal is √3. The triangles' loops have a diagonal of 0.5√2 = 0.41 of it, the square's √2 = 0.82: the default 0.1 skips all three, 0.5 fills the two triangles, 1 fills the square. Each call works on the loops still open, so the counts are per call.
  • The result. The two triangles take one facet each (the original facets again, since a 3-edge loop has one triangulation) and the square six: 38 + 2 + 6 = 46 facets, watertight, volume 1 to the printed digits.

The last region repairs the dragon with sanitize and fills its holes twice, with and without manifold_only (the last two lines of the output above):

f90
call dragon%load_from_file(file_name='dragon.stl', guess_format=.true.)
call dragon%sanitize
strict = dragon
call dragon%fill_holes(holes_filled=filled, open_edges=open_edges, nonmanifold_edges=nonmanifold)
print '(A,I0,A,I0,A,I0)', 'dragon, default:              filled ', filled, ', open edges ', open_edges, &
                          ', non-manifold edges ', nonmanifold
call strict%fill_holes(holes_filled=filled, open_edges=open_edges, nonmanifold_edges=nonmanifold, manifold_only=.true.)
print '(A,I0,A,I0,A,I0)', 'dragon, manifold_only=.true.: filled ', filled, ', open edges ', open_edges, &
                          ', non-manifold edges ', nonmanifold
  • Default. sanitize removes 62 duplicate facets, which opens holes next to the dragon's 54 non-manifold edges (its warnings precede the two lines). The default chains boundary edges and the facet edges on non-manifold edges alike and fills 54 loops, no boundary edge left over.
  • manifold_only=.true. Only 20 loops close through true boundary edges alone. The other 65 boundary edges end at a non-manifold edge, close no loop and stay open, reported in open_edges. Use it when a patch must never be glued to a non-manifold edge; the default suits a repair that wants every boundary edge closed.

Known limitations ​

  • O(n³) triangulation. The dynamic program costs O(n³) time and O(n²) memory in the loop length, hence max_hole_edges (default 200). Larger holes are skipped, not triangulated badly.
  • Fairing approximates, it does not reconstruct. The thin plate with uniform weights is a smooth surface with matching tangents, not the shape that was cut away: a sphere is not a thin plate. On a unit icosphere with the cap z > 0.75 removed, the faired patch's largest distance from the sphere is 0.100 (plain patch 0.141), its largest dihedral angle 30.5° (plain 35.1°), and the enclosed volume 3.987 against 4.047 for the closed polyhedron (plain 3.949). The patch is only as dense as its loop.
  • Feature angle. The 60° threshold decides which surrounding facets give the tangents. A curved hole whose surroundings turn by more than that relative to the patch (a hole covering most of a hemisphere) gets fewer tangent constraints and a flatter patch.
  • Orientation comes from the mesh. Patches follow the winding of the surrounding facets. Around a hole whose neighbourhood is inconsistently wound, the loop does not close and is reported in open_edges. Run sanitize first, or use sanitize(fill_holes=.true.), which orients before filling.
  • Thresholds are relative to the whole surface. max_hole_size compares the hole with the surface's bounding box. On a small part, or with holes as large as the unit-cube example, set it explicitly.
  • Self-intersecting patches are not checked. A patch over a strongly non-planar or twisted hole can intersect nearby facets. Run find_self_intersections if that matters.

See also ​

References ​

  • P. Liepa, Filling Holes in Meshes, Eurographics Symposium on Geometry Processing, 2003. The minimum-weight triangulation and its weight.
  • G. Barequet, M. Sharir, Filling Gaps in the Boundary of a Polyhedron, Computer Aided Geometric Design 12(2), 1995. Dynamic programming for polygon triangulation in 3D.