Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_105_SurfaceMesh_Connectivity

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 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

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.

1
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/util/initializer.h>
29
81using namespace easy3d;
82
83// There are two ways to traverse the incident entities of an element.
84// - use a "for" loop (cleaner code);
85// - use a circulator.
86#define USE_FOR_LOOP
87
88// the mesh created in the previous tutorial (so you can skip it)
89SurfaceMesh *old_mesh_from_previous_example() {
90 // Create a surface mesh
91 auto mesh = new SurfaceMesh;
92
93 // Add 4 vertices
94 SurfaceMesh::Vertex v0 = mesh->add_vertex(vec3(0, 0, 0));
95 SurfaceMesh::Vertex v1 = mesh->add_vertex(vec3(1, 0, 0));
96 SurfaceMesh::Vertex v2 = mesh->add_vertex(vec3(0, 1, 0));
97 SurfaceMesh::Vertex v3 = mesh->add_vertex(vec3(0, 0, 1));
98
99 // Add 4 triangular faces
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);
104
105 return mesh;
106}
107
108int main(int argc, char **argv) {
109 // initialize Easy3D.
110 initialize();
111
112 // Create a mesh
113 SurfaceMesh *mesh = old_mesh_from_previous_example();
114
115 std::cout << "----------------------------------------\n";
116 std::cout << "The incident vertices of each vertex" << std::endl;
117 std::cout << "----------------------------------------\n";
118
119 // loop over all vertices
120 for (auto v : mesh->vertices()) {
121 std::cout << "incident vertices of vertex " << v << ": ";
122#ifdef USE_FOR_LOOP
123 // loop over all incident vertices
124 for (auto vv : mesh->vertices(v))
125 std::cout << vv << " ";
126#else // use circulator
127 // A circulator wraps around a closed fan and has no natural end, so we save the start
128 // position and stop once advancing brings us back to it.
129 SurfaceMesh::VertexAroundVertexCirculator cir = mesh->vertices(v);
131 do {
132 SurfaceMesh::Vertex vv = *cir;
133 std::cout << vv << " ";
134 ++cir;
135 } while (cir != end);
136#endif
137 std::cout << std::endl;
138 }
139
140 std::cout << "\n--------------------------------------\n";
141 std::cout << "The incident outgoing/ingoing edges of each vertex" << std::endl;
142 std::cout << "----------------------------------------\n";
143
144 // loop over all vertices
145 for (auto v : mesh->vertices()) {
146 std::cout << "incident outgoing/ingoing edges of vertex " << v << ": ";
147#ifdef USE_FOR_LOOP
148 // loop over all incident outgoing edges
149 for (auto h : mesh->halfedges(v))
150 std::cout << h << "/" << mesh->opposite(h) << " ";
151#else // use circulator
152 SurfaceMesh::HalfedgeAroundVertexCirculator cir = mesh->halfedges(v);
154 do {
155 SurfaceMesh::Halfedge h = *cir;
156 std::cout << h << "/" << mesh->opposite(h) << " ";
157 ++cir;
158 } while (cir != end);
159#endif
160 std::cout << std::endl;
161 }
162
163 std::cout << "\n--------------------------------------\n";
164 std::cout << "The incident faces of each vertex" << std::endl;
165 std::cout << "----------------------------------------\n";
166
167 // loop over all vertices
168 for (auto v : mesh->vertices()) {
169 std::cout << "incident faces of vertex " << v << ": ";
170#ifdef USE_FOR_LOOP
171 // loop over all incident faces
172 for (auto f : mesh->faces(v))
173 std::cout << f << " ";
174#else // use circulator
175 SurfaceMesh::FaceAroundVertexCirculator cir = mesh->faces(v);
177 do {
178 SurfaceMesh::Face f = *cir;
179 std::cout << f << " ";
180 ++cir;
181 } while (cir != end);
182#endif
183 std::cout << std::endl;
184 }
185
186 std::cout << "\n--------------------------------------\n";
187 std::cout << "The incident vertices of each face" << std::endl;
188 std::cout << "----------------------------------------\n";
189
190 // loop over all faces
191 for (auto f : mesh->faces()) {
192 std::cout << "incident vertices of face " << f << ": ";
193#ifdef USE_FOR_LOOP
194 // loop over all incident vertices
195 for (auto v : mesh->vertices(f))
196 std::cout << v << " ";
197#else // use circulator
198 SurfaceMesh::VertexAroundFaceCirculator cir = mesh->vertices(f);
200 do {
201 SurfaceMesh::Vertex v = *cir;
202 std::cout << v << " ";
203 ++cir;
204 } while (cir != end);
205#endif
206 std::cout << std::endl;
207 }
208
209 std::cout << "\n--------------------------------------\n";
210 std::cout << "The incident half-edges of each face" << std::endl;
211 std::cout << "----------------------------------------\n";
212
213 // loop over all faces
214 for (auto f : mesh->faces()) {
215 std::cout << "half-edges around face " << f << ": ";
216#ifdef USE_FOR_LOOP
217 // loop over all half-edges around the face
218 for (auto h : mesh->halfedges(f))
219 std::cout << h << " ";
220#else
221 SurfaceMesh::HalfedgeAroundFaceCirculator cir = mesh->halfedges(f);
223 do {
224 SurfaceMesh::Halfedge h = *cir;
225 std::cout << h << " ";
226 ++cir;
227 } while (cir != end);
228#endif
229 std::cout << std::endl;
230 }
231
232 std::cout << "\n--------------------------------------\n";
233 std::cout << "The two end points of each edge" << std::endl;
234 std::cout << "----------------------------------------\n";
235
236 // loop over all edges
237 for (auto e : mesh->edges()) {
238 std::cout << "the two end points of edge " << e << ": ";
239 SurfaceMesh::Vertex vs = mesh->vertex(e, 0);
240 std::cout << vs << " ";
241 SurfaceMesh::Vertex vt = mesh->vertex(e, 1);
242 std::cout << vt << " " << std::endl;
243 }
244
245 std::cout << "\n--------------------------------------\n";
246 std::cout << "The two faces connected by each edge" << std::endl;
247 std::cout << "----------------------------------------\n";
248
249 // loop over all edges
250 for (auto e : mesh->edges()) {
251 std::cout << "the two faces connected by edge " << e << ": ";
252 // An interior edge borders two real faces; a boundary edge has a null face on one side,
253 // which is_border() detects so we can print NULL instead of dereferencing it.
254 SurfaceMesh::Halfedge h0 = mesh->halfedge(e, 0);
255 if (mesh->is_border(h0))
256 std::cout << "NULL" << " ";
257 else
258 std::cout << mesh->face(h0) << " ";
259
260 SurfaceMesh::Halfedge h1 = mesh->halfedge(e, 1);
261 if (mesh->is_border(h1))
262 std::cout << "NULL" << " ";
263 else
264 std::cout << mesh->face(h1) << " ";
265
266 std::cout << std::endl;
267 }
268
269 // Delete the mesh (i.e., release memory)
270 delete mesh;
271
272 return EXIT_SUCCESS;
273}
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