Walking the adjacency (connectivity) of a SurfaceMesh through its halfedge links.
Overview
Using the tetrahedron from Tutorial_104_SurfaceMesh, this console tutorial prints every common adjacency relationship a geometry algorithm needs:
- the incident vertices of each vertex
- the incident outgoing/ingoing edges (halfedges) of each vertex
- the incident faces of each vertex
- the incident vertices of each face
- the incident half-edges of each face
- the two end points of each edge
- the two faces connected by each edge Each query is shown twice — once with a clean range-based for loop and once with an explicit circulator — selected by the USE_FOR_LOOP macro, so you can see both traversal styles.
The technique
In a halfedge data structure every edge is two directed halfedges. A halfedge knows its target vertex, its opposite twin, the next halfedge around its face, and the face it bounds; a vertex and a face each remember one incident halfedge. From those few links you can "circulate" around an element: stepping next() walks the boundary of a face, while opposite() then next() rotates around a vertex. That is how the library answers neighbor queries in time proportional to the number of neighbors, without scanning the whole mesh. A circulator is an iterator that wraps around a closed fan (it has no natural end, so you stop when it returns to the start); the range-based for wrappers hide that bookkeeping. Note the boundary case: an edge on the border has a halfedge whose face is null, which we detect with is_border() and print as NULL.
Easy3D API
- SurfaceMesh::vertices(v) / halfedges(v) / faces(v): the neighbors of a vertex.
- SurfaceMesh::vertices(f) / halfedges(f): the corners and boundary halfedges of a face.
- SurfaceMesh::vertex(e, i) and halfedge(e, i): the i-th (0 or 1) endpoint / halfedge of an edge.
- SurfaceMesh::opposite(h), face(h), is_border(h): cross to the twin halfedge, its face, or test whether that face is the null boundary face.
- The *Circulator types (e.g. VertexAroundVertexCirculator) are the explicit traversal form.
Running it
Console-only. It prints a titled section for each of the seven queries above, listing the incident element handles (e.g. "v0", "h1", "f2") for every vertex, face and edge of the tetrahedron. Border faces print "NULL". The mesh is heap-allocated and deleted at the end.
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/util/initializer.h>
100 mesh->add_triangle(v0, v1, v3);
101 mesh->add_triangle(v1, v2, v3);
102 mesh->add_triangle(v2, v0, v3);
103 mesh->add_triangle(v0, v2, v1);
108int main(
int argc,
char **argv) {
113 SurfaceMesh *mesh = old_mesh_from_previous_example();
115 std::cout <<
"----------------------------------------\n";
116 std::cout <<
"The incident vertices of each vertex" << std::endl;
117 std::cout <<
"----------------------------------------\n";
120 for (
auto v : mesh->vertices()) {
121 std::cout <<
"incident vertices of vertex " << v <<
": ";
124 for (
auto vv : mesh->vertices(v))
125 std::cout << vv <<
" ";
133 std::cout << vv <<
" ";
135 }
while (cir != end);
137 std::cout << std::endl;
140 std::cout <<
"\n--------------------------------------\n";
141 std::cout <<
"The incident outgoing/ingoing edges of each vertex" << std::endl;
142 std::cout <<
"----------------------------------------\n";
145 for (
auto v : mesh->vertices()) {
146 std::cout <<
"incident outgoing/ingoing edges of vertex " << v <<
": ";
149 for (
auto h : mesh->halfedges(v))
150 std::cout << h <<
"/" << mesh->opposite(h) <<
" ";
156 std::cout << h <<
"/" << mesh->opposite(h) <<
" ";
158 }
while (cir != end);
160 std::cout << std::endl;
163 std::cout <<
"\n--------------------------------------\n";
164 std::cout <<
"The incident faces of each vertex" << std::endl;
165 std::cout <<
"----------------------------------------\n";
168 for (
auto v : mesh->vertices()) {
169 std::cout <<
"incident faces of vertex " << v <<
": ";
172 for (
auto f : mesh->faces(v))
173 std::cout << f <<
" ";
179 std::cout << f <<
" ";
181 }
while (cir != end);
183 std::cout << std::endl;
186 std::cout <<
"\n--------------------------------------\n";
187 std::cout <<
"The incident vertices of each face" << std::endl;
188 std::cout <<
"----------------------------------------\n";
191 for (
auto f : mesh->faces()) {
192 std::cout <<
"incident vertices of face " << f <<
": ";
195 for (
auto v : mesh->vertices(f))
196 std::cout << v <<
" ";
202 std::cout << v <<
" ";
204 }
while (cir != end);
206 std::cout << std::endl;
209 std::cout <<
"\n--------------------------------------\n";
210 std::cout <<
"The incident half-edges of each face" << std::endl;
211 std::cout <<
"----------------------------------------\n";
214 for (
auto f : mesh->faces()) {
215 std::cout <<
"half-edges around face " << f <<
": ";
218 for (
auto h : mesh->halfedges(f))
219 std::cout << h <<
" ";
225 std::cout << h <<
" ";
227 }
while (cir != end);
229 std::cout << std::endl;
232 std::cout <<
"\n--------------------------------------\n";
233 std::cout <<
"The two end points of each edge" << std::endl;
234 std::cout <<
"----------------------------------------\n";
237 for (
auto e : mesh->edges()) {
238 std::cout <<
"the two end points of edge " << e <<
": ";
240 std::cout << vs <<
" ";
242 std::cout << vt <<
" " << std::endl;
245 std::cout <<
"\n--------------------------------------\n";
246 std::cout <<
"The two faces connected by each edge" << std::endl;
247 std::cout <<
"----------------------------------------\n";
250 for (
auto e : mesh->edges()) {
251 std::cout <<
"the two faces connected by edge " << e <<
": ";
255 if (mesh->is_border(h0))
256 std::cout <<
"NULL" <<
" ";
258 std::cout << mesh->face(h0) <<
" ";
261 if (mesh->is_border(h1))
262 std::cout <<
"NULL" <<
" ";
264 std::cout << mesh->face(h1) <<
" ";
266 std::cout << std::endl;
Definition surface_mesh.h:1055
Definition surface_mesh.h:1265
Definition surface_mesh.h:954
Definition surface_mesh.h:1167
Definition surface_mesh.h:844
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Definition collider.cpp:182
Definition surface_mesh.h:191
This type represents a halfedge (internally it is basically an index).
Definition surface_mesh.h:155
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135
- See also
- SurfaceMesh, Tutorial_104_SurfaceMesh, Tutorial_109_Graph_Connectivity, Tutorial_113_PolyMesh_Connectivity