Sub topic 8
Boundary Edges and Closed Surfaces
This chapter assumes you can read the indexed face set from the previous chapter (a vertex list plus faces given as tuples of vertex indices). It also assumes you know what an edge is and how winding order works. The Python examples use only the standard library.
A mesh can end somewhere, like a sheet of paper, or it can wrap around and seal itself, like a balloon. Whether a surface is open or closed matters in practice. Solid modeling operations, 3D printing, volume calculations and many physics engines all need a closed surface. Cloth, terrain and leaves are meant to be open. This chapter shows how to tell the two apart by counting how many faces use each edge.
The idea: count faces per edge
Take any edge of a surface and count the faces that contain it. On a surface that is a skin around a solid, an edge sits between exactly two faces, one on each side. If an edge belongs to only one face, there is nothing on its other side, so the surface stops there.
A boundary edge is an edge used by exactly one face. A closed surface is a mesh that has at least one face and no boundary edges. An open surface has one or more boundary edges.
| Faces using the edge | Name | Meaning |
|---|---|---|
| 1 | Boundary edge | The surface ends here |
| 2 | Interior edge | The surface continues across the edge |
| 3 or more | Non-manifold edge | Several sheets meet along the edge (next chapter) |
The counting rule is the whole test. The rest of this chapter covers how to implement it, what it reports on real data, and where it gives misleading answers.
Finding boundary edges
An edge has no direction of its own, so the code stores each edge as a sorted pair (min, max). Then (3, 7) and (7, 3) count as the same edge. The function below walks every face, treats the last vertex as connected back to the first, and tallies the pairs.
from collections import Counter
# Unit cube from the previous chapter.
vertices = [
(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0),
(0, 0, 1), (1, 0, 1), (1, 1, 1), (0, 1, 1),
]
faces = [
(0, 3, 2, 1), # bottom
(4, 5, 6, 7), # top
(0, 1, 5, 4), # front
(3, 7, 6, 2), # back
(0, 4, 7, 3), # left
(1, 2, 6, 5), # right
]
def edge_counts(faces):
"""Map each undirected edge to the number of faces that use it."""
counts = Counter()
for face in faces:
for i in range(len(face)):
a = face[i]
b = face[(i + 1) % len(face)]
counts[(min(a, b), max(a, b))] += 1
return counts
def boundary_edges(faces):
return sorted(e for e, n in edge_counts(faces).items() if n == 1)
def is_closed(faces):
return len(faces) > 0 and not boundary_edges(faces)
print(boundary_edges(faces)) # []
print(is_closed(faces)) # True
# Remove the top face (index 1) to open the cube like a box without a lid.
open_box = [f for i, f in enumerate(faces) if i != 1]
print(boundary_edges(open_box)) # [(4, 5), (4, 7), (5, 6), (6, 7)]
print(is_closed(open_box)) # FalseThe four boundary edges of the open box are exactly the four edges of the removed top face. Each is still used by one side face, but the top face that used to share it is gone.
The len(faces) > 0 check matters. An empty mesh has no boundary edges, but calling it a closed surface would be meaningless.
The cost is one pass over all face corners, so the check is linear in the size of the mesh. Using a dictionary or Counter keyed by edge is the standard approach, and you do not need a nested loop that compares every pair of faces.
Boundary loops
Knowing that an edge is on the boundary is useful. Often you also want the boundary as ordered loops of vertices, for example to fill a hole or to outline a shape. If the mesh has consistent winding, this is easy to do with directed edges.
In a consistently wound mesh, two neighboring faces traverse their shared edge in opposite directions. One face goes from a to b, and the other goes from b to a. A boundary edge has no neighbor, so its reverse never appears. That gives a simple rule: a directed edge (a, b) is on the boundary if (b, a) is not present.
def boundary_loops(faces):
"""Return boundary loops as lists of vertex indices.
Assumes consistent winding and that each boundary vertex has exactly
one outgoing boundary edge (no pinched, bow-tie boundaries).
"""
directed = set()
for face in faces:
for i in range(len(face)):
directed.add((face[i], face[(i + 1) % len(face)]))
nxt = {a: b for (a, b) in directed if (b, a) not in directed}
loops, seen = [], set()
for start in sorted(nxt):
if start in seen:
continue
loop = [start]
seen.add(start)
v = nxt[start]
while v != start:
loop.append(v)
seen.add(v)
v = nxt[v]
loops.append(loop)
return loops
print(boundary_loops(open_box)) # [[4, 7, 6, 5]]
print(boundary_loops(faces)) # []The open box has one loop of four vertices, which is the rim of the missing lid. The loop runs in the opposite order from the removed top face (4, 5, 6, 7), because the remaining side faces traverse those edges the other way. A mesh with several holes, such as a tube open at both ends, returns one loop per hole.
Closed does not always mean "properly stored"
The edge count looks at vertex indices, not positions. Two vertices with the same coordinates but different indices are different vertices as far as the test is concerned. This produces the most common false alarm in practice.
Many file formats and exporters store a separate copy of each corner for every face, so that each face can carry its own normals or UVs. The surface looks closed when drawn, yet the index-based count reports boundary edges everywhere.
# Give every face its own private copy of its corner positions.
split_vertices, split_faces = [], []
for face in faces:
start = len(split_vertices)
split_vertices.extend(vertices[i] for i in face)
split_faces.append(tuple(range(start, start + len(face))))
print(len(split_vertices), len(boundary_edges(split_faces))) # 24 24
def weld(vertices, faces):
"""Merge vertices with identical positions and remap the faces."""
index_of, new_vertices, remap = {}, [], []
for p in vertices:
if p not in index_of:
index_of[p] = len(new_vertices)
new_vertices.append(p)
remap.append(index_of[p])
return new_vertices, [tuple(remap[i] for i in f) for f in faces]
welded_vertices, welded_faces = weld(split_vertices, split_faces)
print(len(welded_vertices), len(boundary_edges(welded_faces))) # 8 0The split cube has 24 vertices and all 24 of its edges are boundary edges, even though it draws as a perfect cube. After welding, it returns to 8 vertices and no boundary.
This version of weld compares positions exactly. Coordinates computed in floating point may differ in the last few digits, so real tools merge vertices that lie within a small tolerance, or round coordinates to a grid before comparing. Choose the tolerance carefully. A tolerance that is too large collapses small features. One that is too small leaves seams.
The vertex attributes chapter explains why this splitting is sometimes deliberate. For topology checks, work on welded positions. For rendering, keep the split copies.
Reading the result
A few cases come up repeatedly when you act on a boundary check:
- A deliberate boundary. A plane, a flag, a leaf card or a terrain patch should have boundary edges. An empty result for these would be the surprise.
- An accidental hole. A single missing face leaves a small loop. Deleting a face by mistake, or a failed boolean operation, produces this. The loop from
boundary_loopstells you where the hole is and how many edges it has. - Seams from unwelded vertices. If boundary edges run along what should be smooth interior lines, suspect duplicated vertices as in the example above.
- Several loops. A mesh with two holes gives two loops. The number of loops is a quick measure of how damaged the mesh is.
A boundary-free result does not prove the mesh is a clean solid. Consider these limits of the check:
- Non-manifold edges. A mesh where an edge is shared by four faces has no boundary edges, so
is_closedreturnsTrue. Such a mesh is still a poor solid, because the inside and outside are ambiguous at that edge. - Inconsistent winding. A closed mesh with one flipped face passes the boundary test. The directed check from the earlier chapter, which looks for a repeated directed edge, catches it.
- Several separate shells. Two cubes in one mesh are closed, because each cube has no boundary. Closedness does not say how many connected pieces exist.
- Self-intersection. Faces that pass through each other still share edges correctly. Edge counting only reads connectivity and ignores where the points are in space.
Treat the boundary count as one necessary condition for a solid. It does not replace the other checks.
A connection to Euler's formula
Open meshes change the Euler characteristic. For the closed cube, V − E + F = 8 − 12 + 6 = 2. For the open box, you can compute 8 − 12 + 5 = 1, because removing a face lowers F by one while V and E stay the same. Each additional hole lowers the value by one more. This gives you a second, independent check on the boundary count, and the Euler's formula chapter develops it fully.
Summary of the working model
Count faces per edge using sorted vertex pairs. One face means a boundary edge, two means an interior edge, and more than two is a topology problem. A mesh is closed when it has faces and no boundary edges. Compute the check on welded vertices, because index-based counting treats duplicated positions as separate points. After that check passes, test for non-manifold edges and winding consistency before treating the mesh as a solid.
Exercise
A modeler exports a cube as 24 vertices, with 4 vertices stored separately for each of the 6 faces, because each face carries its own UV coordinates. You run boundary_edges on the indexed faces and get 24 boundary edges. Is the cube open? What would you do to get a meaningful answer, and what result should you expect?
Show answer
The cube is not necessarily open. The test works on vertex indices, and each face uses private vertex copies, so no two faces share an edge by index. Every one of the 6 x 4 = 24 edges is used by one face. To get a meaningful answer, weld the vertices by position (merging identical or near-identical coordinates) and remap the faces, then run boundary_edges again. The welded cube has 8 vertices and 12 edges, each used by two faces, so the boundary list is empty and the mesh is closed.
Exercise
You delete two adjacent faces from a closed cube, for example the top and the front. How many boundary edges remain, and how many boundary loops does boundary_loops return?
Show answer
The removed faces share the edge (4, 5), which is now used by no remaining face, so it does not appear. The top face has 4 edges and the front face has 4 edges, which gives 8 edge slots, minus the 1 shared edge counted twice, so 7 distinct edges. Of those, (4, 5) belongs to no remaining face. The other 6 are each still used by one remaining face, so there are 6 boundary edges. They form a single connected loop of 6 vertices, so boundary_loops returns one loop.