Sub topic 2
Edges: connecting vertices
This chapter assumes you know 3D coordinates, can compute a vector difference and a length, and can read short Python programs. It builds on the previous chapter, where a vertex is a point with an (x, y, z) position. Here you will see what an edge is, how to get edges without storing them, and what the number of faces on an edge tells you about the mesh.
What an edge is
An edge is a straight line segment between two vertices. It has no position of its own. Its endpoints are two vertices, and the edge is defined by which two they are. Moving a vertex moves every edge that touches it.
Two properties matter in practice:
- An edge is usually treated as undirected. The edge from vertex 3 to vertex 7 is the same segment as the edge from 7 to 3.
- A face traverses its edges in a direction. A face listing vertices
(0, 1, 2)walks 0 to 1, then 1 to 2, then 2 back to 0. These are directed edges, and the direction is what encodes winding order, covered in a later chapter.
Edges are not curves. A smooth-looking curved surface on screen is made of many short straight edges, or it is a different kind of surface (subdivision or NURBS) that is outside this tutorial.
Edges are topology, lengths are geometry
An edge's existence is topological: it says that vertex a connects to vertex b. Its length is geometric, because it depends on where the two vertices are. You can stretch, rotate, or bend a mesh and keep the same set of edges while every length changes.
import math
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), (4, 5, 6, 7), (0, 1, 5, 4),
(3, 7, 6, 2), (0, 4, 7, 3), (1, 2, 6, 5),
]
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 edge_length(vertices, edge):
a, b = edge
return math.dist(vertices[a], vertices[b])
edges = edges_of(faces)
print(len(edges)) # 12
print(sorted({edge_length(vertices, e) for e in edges})) # [1.0]
# Stretch the cube to twice its height. The edge set does not change.
tall = [(x, y, 2 * z) for x, y, z in vertices]
print(sorted({edge_length(tall, e) for e in edges})) # [1.0, 2.0]The same 12 index pairs describe both cubes. Only the vertex positions changed, and with them the lengths. This is the geometry versus topology split in its smallest form.
Deriving edges from faces
Most mesh formats store only vertices and faces. Every edge is a pair of consecutive vertices in some face, so the edge list can be rebuilt whenever you need it. This is what edges_of does.
Three details in the code are easy to get wrong:
- The wrap-around.
(i + 1) % len(face)closes the loop. Without it, every polygon loses its last edge. - Normalizing the pair.
(min(a, b), max(a, b))makes the edge 3-7 and the edge 7-3 the same dictionary or set key. Without it, an edge shared by two faces appears twice, once in each direction, and your edge count is too high. - Polygon size. The loop works for triangles, quads, and n-gons. A face with n vertices has n edges.
Counting faces per edge
Once you have edges, the most useful question to ask about each one is how many faces use it. In a well-formed surface the answer is almost always two: one face on each side.
| Faces using the edge | Name | Meaning |
|---|---|---|
| 1 | Boundary edge | The surface ends here. A hole, rim, or open border. |
| 2 | Interior edge | A normal edge between two neighboring faces. |
| 3 or more | Non-manifold edge | Several surfaces meet along one edge. Many tools and algorithms cannot handle it. |
The next chapters build on this table for closed surfaces and manifold meshes. Here is the counting step on its own, using two triangles that form a square.
from collections import Counter
def edge_use(faces, key=lambda i: i):
"""Count how many faces use each undirected edge.
key maps a vertex index to the value used to identify it."""
use = Counter()
for face in faces:
for i in range(len(face)):
a = key(face[i])
b = key(face[(i + 1) % len(face)])
if a != b: # skip degenerate edges
use[(min(a, b), max(a, b))] += 1
return use
square = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 1, 0)]
two_triangles = [(0, 1, 2), (0, 2, 3)]
use = edge_use(two_triangles)
for edge, n in sorted(use.items()):
print(edge, n)
# (0, 1) 1
# (0, 2) 2
# (0, 3) 1
# (1, 2) 1
# (2, 3) 1The diagonal (0, 2) is the only edge with two faces. The other four edges form the outer boundary of the square.
Directed edges and consistent winding
Each face walks its edges in one direction. Two neighboring faces that agree about which way is front walk their shared edge in opposite directions. In the example above, face (0, 1, 2) uses the edge 2 to 0, and face (0, 2, 3) uses it as 0 to 2.
This gives a cheap test. In a consistently wound mesh, no directed edge (a, b) appears more than once. If it does, two neighboring faces disagree about winding, and one of them is flipped. The check_edges function in the spine's later material uses exactly this rule.
Edge degree: how many edges meet at a vertex
You can also look at edges from the vertex side. The number of edges meeting at a vertex is its valence, also called its degree. A cube corner has valence 3. Valence matters in modeling because vertices with unusual valence (not 4 on a quad mesh) cause irregular shading and subdivision behavior.
from collections import defaultdict
def valence(edges):
degree = defaultdict(int)
for a, b in edges:
degree[a] += 1
degree[b] += 1
return dict(degree)
print(valence(edges)) # every one of the 8 cube vertices maps to 3Because every edge has two endpoints, the valences always sum to twice the edge count. For the cube, 8 × 3 = 24 = 2 × 12. That makes a quick check for any edge list you build.
Common errors
- Duplicate vertices create phantom boundaries. Two vertices at the same position are different vertices if they have different indices. Faces that use different copies do not share edges, so a seam appears in the edge counts even though the surface looks continuous.
- Degenerate edges. A face that lists the same vertex twice in a row, such as
(0, 1, 1, 2), produces an edge from a vertex to itself. Filter these out, or treat them as an error. - Zero-length edges. Two different vertices at the same position make an edge of length 0. Normals computed from it divide by zero.
- Mixing up index pairs and positions. An edge is a pair of indices, not a pair of coordinates. Positions are looked up only when you need geometry.
The first error is worth seeing directly. Here a square is built from two triangles, but vertex 3 is a duplicate of vertex 0.
verts = [(0, 0, 0), (1, 0, 0), (1, 1, 0), (0, 0, 0), (0, 1, 0)]
tris = [(0, 1, 2), (3, 2, 4)]
by_index = edge_use(tris)
by_position = edge_use(tris, key=lambda i: verts[i])
print(sorted(by_index.values())) # [1, 1, 1, 1, 1, 1]
print(sorted(by_position.values())) # [1, 1, 1, 1, 2]Counted by index, there are six edges and every one is a boundary edge, so the two triangles look disconnected. Counted by position, there are five edges and the diagonal is shared. Merging vertices that have identical positions, often called welding, repairs this kind of mesh.
Summary of the working model
An edge is an unordered pair of vertex indices. It is derived from faces by walking each face's vertex loop and wrapping around. Its topology is the pair of indices. Its geometry is the distance between the two positions. The count of faces using it classifies it as boundary, interior, or non-manifold, and the direction in which faces traverse it reveals winding consistency.
Exercise
A mesh has two triangles, (0, 1, 2) and (0, 2, 3), that share one edge. How many unique edges does the mesh have, and how many of them are boundary edges?
Show answer
The two faces contribute 3 + 3 = 6 edge uses. The shared edge 0-2 is used twice, so there are 5 unique edges. Four of them are used by one face each, so there are 4 boundary edges, and one interior edge.
Exercise
A closed mesh made only of triangles has 20 faces and every edge is shared by exactly two faces. How many edges does it have, and why?
Show answer
30 edges. Each triangle has 3 edges, giving 20 × 3 = 60 edge uses. Each edge is used by exactly 2 faces, so the number of unique edges is 60 / 2 = 30.
Exercise
Why does edges_of store (min(a, b), max(a, b)) instead of (a, b)?
Show answer
It makes the edge undirected. A shared edge is walked as (a, b) by one face and (b, a) by its neighbor. Normalizing the order makes both walks produce the same key, so the edge is stored once instead of twice.