Sub topic 10
Checking a Mesh with Euler's Formula
This chapter assumes you can read the indexed face set from earlier in the tutorial: a vertex list plus face index lists, with edges derived from consecutive index pairs. It also assumes you know what boundary edges and manifold edges are. The code is plain Python.
The formula
For a connected, closed surface made of vertices (V), edges (E) and faces (F), the Euler characteristic is:
χ = V - E + FA closed mesh that is topologically a sphere has χ = 2, whatever its size or shape. The cube has 8 - 12 + 6 = 2. Pushing a vertex around or stretching the cube changes the geometry and leaves χ alone, because χ depends only on topology.
The general form for a connected, closed, orientable surface with g handles (g is the genus, the number of "holes through" the shape) is:
χ = 2 - 2gA sphere has g = 0 and χ = 2. A torus (a doughnut) has g = 1 and χ = 0. A two-handled pretzel shape has g = 2 and χ = -2.
| Shape | V | E | F | χ |
|---|---|---|---|---|
| Tetrahedron | 4 | 6 | 4 | 2 |
| Cube | 8 | 12 | 6 | 2 |
| Octahedron | 6 | 12 | 8 | 2 |
| Cube with the top face removed | 8 | 12 | 5 | 1 |
| 3 by 3 torus grid | 9 | 18 | 9 | 0 |
The fourth row is not closed. It has one boundary loop, and a surface with b boundary loops has χ = 2 - 2g - b, so that row gives 2 - 0 - 1 = 1.
Why it holds
Start with a single triangle floating in space, counted as a disk with V = 3, E = 3, F = 1, so χ = 1. Every local edit you can make to a mesh keeps χ the same:
- Splitting an edge by inserting a vertex adds one V and one E.
- Cutting a face with a new edge adds one E and one F.
- Gluing an extra face to a boundary loop adds one F.
Each edit changes the three counts by amounts that cancel in V - E + F. Closing a disk into a sphere takes one more face, which gives 1 + 1 = 2. This is not a full proof, but it shows why the number tracks the shape of the surface and not its tessellation.
Computing χ from an indexed face set
You do not need to store edges to count them. Derive them from the faces, as before, and count only the vertices that some face uses.
def edges_of(faces):
"""Return the set of unique undirected edges implied by the faces."""
edges = set()
for face in faces:
for i in range(len(face)):
a = face[i]
b = face[(i + 1) % len(face)]
edges.add((min(a, b), max(a, b)))
return edges
def euler_characteristic(faces):
used = {i for face in faces for i in face}
return len(used) - len(edges_of(faces)) + len(faces)
cube_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),
]
cube_faces = [
(0, 3, 2, 1), (4, 5, 6, 7), (0, 1, 5, 4),
(3, 7, 6, 2), (0, 4, 7, 3), (1, 2, 6, 5),
]
print(euler_characteristic(cube_faces)) # 2
# One face removed: an open cube with a single boundary loop.
print(euler_characteristic(cube_faces[1:])) # 1The helper counts vertices from used, not from len(vertices). A vertex that no face references is an isolated point, and it adds 1 to V - E + F without touching the surface. A mesh with one stray vertex reports χ = 3 if you count the whole vertex list. Tools often leave such vertices behind after deleting faces, so this check also finds them.
Reading the result
The value of χ becomes useful when you compare it with what you expect.
- A character head or a rock should be a sphere-like closed mesh, so χ should be 2. Any other value means a hole, a stray piece or a handle that you did not intend.
- A ring, a mug or a loop of tubing has one handle, so you expect χ = 0.
- If the mesh is closed, connected and orientable, you can solve for the genus: g = (2 - χ) / 2. If this is not a whole number, at least one of those three assumptions is false.
You can test that with a torus built as a grid of quads whose indices wrap around in both directions. Append this to the same file:
def torus_faces(n, m):
"""Quad faces of an n by m torus grid. Vertex (i, j) has index i * m + j."""
faces = []
for i in range(n):
for j in range(m):
a = i * m + j
b = ((i + 1) % n) * m + j
c = ((i + 1) % n) * m + (j + 1) % m
d = i * m + (j + 1) % m
faces.append((a, b, c, d))
return faces
t = torus_faces(3, 3)
chi = euler_characteristic(t)
print(len(t), len(edges_of(t)), chi) # 9 18 0
print((2 - chi) // 2) # genus 1Use n and m of at least 3. With 2 or fewer, wrapped faces share the same pair of vertices twice and the grid collapses into a degenerate surface that no longer represents a torus.
Shortcuts for uniform meshes
When every face has the same number of sides, the counts are linked by the faces' edge usage. In a closed manifold mesh each edge borders exactly two faces, so adding up the sides of all faces counts every edge twice.
- Triangles: 3F = 2E. Substituting into the formula for a sphere gives F = 2V - 4 and E = 3V - 6.
- Quads: 4F = 2E, so E = 2F. For a sphere this gives F = V - 2.
The cube agrees with the quad rule: 8 - 2 = 6 faces. A closed triangle mesh with 100 vertices has 196 faces and 294 edges if it is a sphere. These relations also explain a rule of thumb: a closed triangle mesh has about twice as many faces as vertices, and three edges per vertex.
If a closed, all-triangle mesh fails 3F = 2E, it is not a manifold. Some edge borders one face or three or more.
What the formula cannot prove
Euler's formula is a necessary check. A wrong result proves a problem, but a correct result does not prove the mesh is clean. Three situations produce the expected number for the wrong reasons.
First, separate components add up. A cube and a torus in the same mesh give 2 + 0 = 2, the value you would expect for one sphere. Two cubes give 4. Always count connected components (using shared vertices) before interpreting χ, or compute it per component.
Second, errors can cancel. A hole (which lowers χ by 1 per boundary loop) and a stray handle or duplicated face (which can raise or lower it) can offset each other. Use χ together with the edge-count check for boundary and non-manifold edges, not instead of it.
Third, non-manifold edges can leave the number looking plausible. Three faces meeting at one edge are a defect, but the total might still land on 2. The per-edge counts from the manifold chapter find these directly.
Split vertices break the count
Earlier in this tutorial you saw that shading and UV seams force a renderer to duplicate vertices, because one position can need different normals or UVs on different faces. If you run Euler's formula on those duplicated vertices, you are checking a different mesh from the one you modeled. The duplicates share a position but not an index, so the edge sets no longer join up.
A cube with separate vertices for each face shows this. Append the following:
def split_per_face(vertices, faces):
"""Give every face its own copies of its vertices (hard-edge shading)."""
new_vertices, new_faces = [], []
for face in faces:
start = len(new_vertices)
new_vertices.extend(vertices[i] for i in face)
new_faces.append(tuple(range(start, start + len(face))))
return new_vertices, new_faces
def weld(vertices, faces):
"""Merge vertices with identical positions and rewrite the faces."""
ids = {}
remap = [ids.setdefault(p, len(ids)) for p in vertices]
return [tuple(remap[i] for i in face) for face in faces]
split_vertices, split_faces = split_per_face(cube_vertices, cube_faces)
print(len(split_vertices)) # 24
print(euler_characteristic(split_faces)) # 6, but the cube is closed
print(euler_characteristic(weld(split_vertices, split_faces))) # 2The unwelded mesh has V = 24, E = 24 and F = 6, so χ = 6. The six faces are six unconnected pieces, and each piece is a disk with χ = 1. After welding by position the count returns to 2.
Before an Euler check, merge vertices by position (and usually within a small tolerance for floating-point data) and ignore normals and UVs. Exact-match welding as shown works for integer or exactly equal coordinates. For scanned or exported data, round positions to a fixed precision before using them as dictionary keys.
A checking procedure
The order matters. Welding and removing unused vertices make V meaningful. Splitting into components makes the sum of several shapes visible. The edge checks then confirm what Euler's formula only suggests.
Where it is used
- Import validation: a loader can reject or flag meshes whose χ does not match what the asset pipeline expects, such as characters that must be one closed sphere-like surface.
- Modeling tools: the statistics overlay in most tools shows V, E and F so you can compute χ by hand after an operation. A deleted face, a merge or a boolean operation changes the counts predictably, and an unexpected change signals a problem.
- Subdivision: one round of Catmull-Clark subdivision on a closed quad mesh produces F' equal to the old edge-corner count (every face becomes quads equal to its number of sides), and χ stays at its original value. Checking this after subdividing confirms the operation preserved topology.
- Procedural meshes: when you generate geometry in code, comparing χ with the shape you intended finds off-by-one wrap-around bugs, such as the torus grid with
nless than 3.
Exercise
A closed, connected, all-triangle mesh has 12 vertices and is topologically a sphere. How many edges and faces does it have? Verify with Euler's formula.
Show answer
For a triangle sphere, F = 2V - 4 = 20 and E = 3V - 6 = 30. Check: V - E + F = 12 - 30 + 20 = 2.
Exercise
A tool reports a closed, connected, orientable triangle mesh with V = 9 and F = 18. Compute E and χ, and say what the result tells you.
Show answer
Each triangle contributes 3 edge sides and each edge is shared by two faces, so E = 3F / 2 = 27. Then χ = 9 - 27 + 18 = 0, so the genus is (2 - 0) / 2 = 1. The mesh is a torus-like surface with one handle. If a sphere was intended, the mesh has an extra handle or another defect.
Exercise
You load a mesh exported with hard-edge shading, and the face-and-vertex counts give χ = 6 even though the model looks like a closed cube. What step did you skip, and what value should you get after it?
Show answer
You skipped welding vertices by position. The exporter duplicated vertices per face to store different normals, so the faces do not share indices. After merging vertices with identical positions, the count gives V = 8, E = 12, F = 6 and χ = 2.