Sub topic 2
Geometry versus topology: positions and connections
This chapter assumes you know 3D coordinates (x, y, z), the cross product of two vectors, and basic Python lists and tuples.
Geometry and topology
A polygon mesh is a 3D surface built from points, the vertices, joined by segments, the edges, which bound flat faces. Two kinds of information describe a mesh:
- Geometry is where the vertices sit in space.
- Topology is which vertices connect to form edges and faces.
Moving a vertex changes the geometry but not the topology. Two meshes with identical connections can have different shapes, and two meshes with different connections can use the same points.
Triangles, quads and n-gons
A face with three vertices is a triangle, four is a quad, and more is an n-gon. Modeling tools often use quads because they subdivide and deform cleanly. Renderers, however, convert every face to triangles before drawing. Three points always lie in one plane, so a triangle is always flat and simple for the graphics hardware to fill. A quad with non-coplanar corners is not flat, which is why the renderer splits it.
Storing a mesh
A common format is the indexed face set. It stores the positions once in a vertex list, and each face stores indices into that list. Edges need no separate storage, because each consecutive pair of vertices in a face is an edge.
vertices = [
(0.0, 0.0, 0.0), # 0
(1.0, 0.0, 0.0), # 1
(1.0, 1.0, 0.0), # 2
(0.0, 1.0, 0.0), # 3
]
faces = [(0, 1, 2, 3)]
edges = set()
for face in faces:
for i in range(len(face)):
a, b = face[i], face[(i + 1) % len(face)]
edges.add((min(a, b), max(a, b)))
print(sorted(edges)) # [(0, 1), (0, 3), (1, 2), (2, 3)]Winding order and face normals
The order of indices in a face is its winding order. Counter-clockwise order, seen from the front, defines the front side. The face normal is the unit vector perpendicular to the face. The cross product of two edge vectors gives it, and reversing the winding reverses the normal.
def face_normal(vertices, face):
p0, p1, p2 = (vertices[i] for i in face[:3])
e1 = [b - a for a, b in zip(p0, p1)]
e2 = [b - a for a, b in zip(p1, p2)]
n = [
e1[1] * e2[2] - e1[2] * e2[1],
e1[2] * e2[0] - e1[0] * e2[2],
e1[0] * e2[1] - e1[1] * e2[0],
]
length = sum(c * c for c in n) ** 0.5
return tuple(c / length for c in n)
vertices = [(0.0, 0.0, 0.0), (1.0, 0.0, 0.0), (1.0, 1.0, 0.0), (0.0, 1.0, 0.0)]
print(face_normal(vertices, (0, 1, 2, 3))) # (0.0, 0.0, 1.0)
print(face_normal(vertices, (0, 3, 2, 1))) # (0.0, 0.0, -1.0)Exercise
Exercise
Moving vertex 2 to (1.0, 1.0, 0.5) changes the geometry. Does it change the list of faces or the edges derived from them?
Show answer
No. The faces still hold the same indices, so the topology and the derived edges are unchanged. Only the positions, and therefore the normal, change.