Sub topic 13
Adjacency Queries and the Half-Edge Structure
Prerequisites: you should know what vertices, edges, and faces are, understand indexed face lists, and be comfortable reading simple Python. You should also know that an edge may be shared by two faces.
A mesh often needs adjacency queries:
- Which faces touch this face?
- Which edges meet at this vertex?
- Which vertices surround this face?
- Is an edge on the boundary?
An indexed face set can answer these questions, but it may need repeated searches. A half-edge structure stores the connections explicitly so that the mesh can be traversed efficiently.
From edges to half-edges
A half-edge is one directed use of an edge by a face. If an undirected edge connects vertices 2 and 5, a face may use the directed half-edge 2 → 5. The neighboring face uses the opposite half-edge, 5 → 2.
Each half-edge commonly stores:
origin: its starting vertexnext: the next half-edge around the same facetwin: the opposite half-edge in the neighboring faceface: the face that owns it
Some implementations also store prev, the previous half-edge. This makes movement in both directions easier.
The next links form a cycle for every face. Following next repeatedly returns to the starting half-edge. The twin link crosses an edge to the adjacent face.
Building the structure
The following code creates half-edges from indexed triangular faces. It pairs two half-edges when they use the same edge in opposite directions.
faces = [
(0, 1, 2),
(0, 3, 1),
(1, 3, 2),
(2, 3, 0),
]
halfedges = []
directed_edges = {}
for face_id, face in enumerate(faces):
start = len(halfedges)
count = len(face)
for vertex in face:
halfedges.append({
"origin": vertex,
"next": None,
"twin": None,
"face": face_id,
})
for local_index in range(count):
current = start + local_index
following = start + (local_index + 1) % count
halfedges[current]["next"] = following
a = face[local_index]
b = face[(local_index + 1) % count]
if (a, b) in directed_edges:
raise ValueError(f"Repeated directed edge: {(a, b)}")
directed_edges[(a, b)] = current
for (a, b), halfedge_id in directed_edges.items():
opposite_id = directed_edges.get((b, a))
if opposite_id is not None:
halfedges[halfedge_id]["twin"] = opposite_id
def face_neighbors(face_id):
"""Return faces sharing an edge with face_id."""
result = set()
for halfedge in halfedges:
if halfedge["face"] != face_id:
continue
twin_id = halfedge["twin"]
if twin_id is not None:
result.add(halfedges[twin_id]["face"])
return sorted(result)
print(face_neighbors(0))The face list is still useful for input and serialization. The half-edge records add shortcuts for traversal. In this example, each edge of the tetrahedron has a twin, so every face has three neighboring faces.
A boundary edge has no twin. That happens when only one face uses the edge, such as on an open plane. Boundary detection is therefore a simple test:
boundary = [
halfedge_id
for halfedge_id, halfedge in enumerate(halfedges)
if halfedge["twin"] is None
]
print(boundary)Failure modes and design choices
A half-edge structure assumes that an edge has at most two incident faces. If three faces share one edge, the edge is non-manifold and cannot be represented by only one twin link. A mesh library must reject that input or use a more general radial-edge structure.
Attributes affect vertex adjacency too. Two faces may share a position but require different normals or UV coordinates. In that case, the rendering mesh may contain separate attribute vertices even though the geometric positions coincide.
Use a half-edge structure when you need frequent local edits, neighborhood queries, or traversal. For simple rendering, an indexed face set is often smaller and sufficient.
Exercise
How do you identify a boundary edge in a half-edge structure, and why can one twin link not represent a non-manifold edge?
Show answer
A boundary half-edge has no twin. One twin link can point to only one opposite face, so it cannot represent an edge shared by three or more faces.