Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_113_PolyMesh_Connectivity

Walking the full adjacency of a PolyMesh through its half-face links.

Overview

Using the single-tetrahedron mesh from Tutorial_112_PolyMesh, this console tutorial prints every adjacency relationship of a polyhedral mesh:

The technique

A PolyMesh stores connectivity with halffaces: every face is split into two oppositely oriented halffaces, one for each cell it separates, with the normal pointing out of its cell. cell(h) gives the cell that halfface h bounds, and opposite(h) crosses the face to the twin halfface in the neighboring cell — this is how you walk from one volume to the next. On the boundary of the solid there is no neighboring cell, so the opposite halfface (and its cell) is an invalid handle (index -1); since our mesh is a lone tetrahedron, all four faces are boundary faces and their opposites are invalid. The remaining queries circulate around an element: vertices/edges/ halffaces/cells of a vertex, edge, halfface or cell, and halfface(f, 0/1) recovers the two halffaces of a face. These traversals are the foundation of volumetric algorithms such as cell neighbors, boundary extraction, and finite-element assembly.

Easy3D API

Running it

Console-only. It prints a titled section for each query, listing element handles (e.g. "v0", "e1", "h2", "f0", "c0"). For the boundary faces the opposite halfface/cell print as invalid handles. The mesh is heap-allocated and deleted at the end.

1
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
29
88using namespace easy3d;
89
90// the mesh created in the previous tutorial (so you can skip it)
91PolyMesh *old_mesh_from_previous_example() {
92 // Create a polyhedral mesh
93 auto mesh = new PolyMesh;
94
95 // Add four vertices
96 auto v0 = mesh->add_vertex(vec3(-1.0, 0.0, 0.0));
97 auto v1 = mesh->add_vertex(vec3(0.0, 0.0, 1.0));
98 auto v2 = mesh->add_vertex(vec3(1.0, 0.0, 0.0));
99 auto v3 = mesh->add_vertex(vec3(0.0, 0.0, -1.0));
100
101 // Add the only tetrahedron
102 mesh->add_tetra(v0, v1, v2, v3);
103
104 return mesh;
105}
106
107int main(int argc, char **argv) {
108 // initialize Easy3D.
109 initialize();
110
111 // Create a mesh
112 PolyMesh *mesh = old_mesh_from_previous_example();
113
114 std::cout << "----------------------------------------\n";
115 std::cout << "The incident vertices of each vertex" << std::endl;
116 std::cout << "----------------------------------------\n";
117 // loop over all vertices
118 for (auto v : mesh->vertices()) {
119 std::cout << "incident vertices of vertex " << v << ": ";
120 // loop over all incident vertices
121 for (auto vv : mesh->vertices(v))
122 std::cout << vv << " ";
123 std::cout << std::endl;
124 }
125
126 std::cout << "----------------------------------------\n";
127 std::cout << "The incident edges of each vertex" << std::endl;
128 std::cout << "----------------------------------------\n";
129 // loop over all vertices
130 for (auto v : mesh->vertices()) {
131 std::cout << "incident edges of vertex " << v << ": ";
132 // loop over all incident edges
133 for (auto e : mesh->edges(v))
134 std::cout << e << " ";
135 std::cout << std::endl;
136 }
137
138 std::cout << "----------------------------------------\n";
139 std::cout << "The incident halffaces of each vertex" << std::endl;
140 std::cout << "----------------------------------------\n";
141 // loop over all vertices
142 for (auto v : mesh->vertices()) {
143 std::cout << "incident halffaces of vertex " << v << ": ";
144 // loop over all incident edges
145 for (auto h : mesh->halffaces(v))
146 std::cout << h << " ";
147 std::cout << std::endl;
148 }
149
150 std::cout << "----------------------------------------\n";
151 std::cout << "The incident cells of each vertex" << std::endl;
152 std::cout << "----------------------------------------\n";
153 // loop over all vertices
154 for (auto v : mesh->vertices()) {
155 std::cout << "incident cells of vertex " << v << ": ";
156 // loop over all incident cells
157 for (auto c : mesh->cells(v))
158 std::cout << c << " ";
159 std::cout << std::endl;
160 }
161
162 std::cout << "----------------------------------------\n";
163 std::cout << "The incident vertices of each edge" << std::endl;
164 std::cout << "----------------------------------------\n";
165 // loop over all edges
166 for (auto e : mesh->edges()) {
167 std::cout << "incident vertices of edge " << e << ": " << mesh->vertex(e, 0) << " " << mesh->vertex(e, 1)
168 << std::endl;
169 }
170
171 std::cout << "----------------------------------------\n";
172 std::cout << "The incident halffaces of each edge" << std::endl;
173 std::cout << "----------------------------------------\n";
174 // loop over all edges
175 for (auto e : mesh->edges()) {
176 std::cout << "incident halffaces of edge " << e << ": ";
177 // loop over all incident halffaces
178 for (auto h : mesh->halffaces(e))
179 std::cout << h << " ";
180 std::cout << std::endl;
181 }
182
183 std::cout << "----------------------------------------\n";
184 std::cout << "The incident cells of each edge" << std::endl;
185 std::cout << "----------------------------------------\n";
186 // loop over all edges
187 for (auto e : mesh->edges()) {
188 std::cout << "incident cells of edge " << e << ": ";
189 // loop over all incident cells
190 for (auto c : mesh->cells(e))
191 std::cout << c << " ";
192 std::cout << std::endl;
193 }
194
195 std::cout << "----------------------------------------\n";
196 std::cout << "The incident vertices of each halfface" << std::endl;
197 std::cout << "----------------------------------------\n";
198 // loop over all halffaces
199 for (auto h : mesh->halffaces()) {
200 std::cout << "incident vertices of halfface " << h << ": ";
201 // loop over all incident cells
202 for (auto v : mesh->vertices(h))
203 std::cout << v << " ";
204 std::cout << std::endl;
205 }
206
207 std::cout << "----------------------------------------\n";
208 std::cout << "The incident edges of each halfface" << std::endl;
209 std::cout << "----------------------------------------\n";
210 // loop over all halffaces
211 for (auto h : mesh->halffaces()) {
212 std::cout << "incident edges of halfface " << h << ": ";
213 // loop over all incident edges
214 for (auto e : mesh->edges(h))
215 std::cout << e << " ";
216 std::cout << std::endl;
217 }
218
219 std::cout << "----------------------------------------\n";
220 std::cout << "The associated cell of each halfface" << std::endl;
221 std::cout << "----------------------------------------\n";
222 // loop over all halffaces
223 for (auto h : mesh->halffaces()) {
224 std::cout << "incident associated cell of halfface " << h << ": " << mesh->cell(h) << std::endl;
225 }
226
227 std::cout << "----------------------------------------\n";
228 std::cout << "The opposite halfface and cell of each halfface" << std::endl;
229 std::cout << "----------------------------------------\n";
230 // loop over all halffaces
231 for (auto h : mesh->halffaces()) {
232 // opposite(h) crosses the face to the twin halfface in the neighboring cell, and
233 // cell(opposite(h)) is that neighbor. On the boundary there is no neighbor, so both are
234 // invalid handles (index -1) — which is the case for every face of this lone tetrahedron.
235 std::cout << "opposite halfface of halfface " << h << ": " << mesh->opposite(h) << ". ";
236 std::cout << "opposite cell of halfface " << h << ": " << mesh->cell(mesh->opposite(h)) << std::endl;
237 }
238
239 std::cout << "----------------------------------------\n";
240 std::cout << "The incident vertices of each cell" << std::endl;
241 std::cout << "----------------------------------------\n";
242 // loop over all cells
243 for (auto c : mesh->cells()) {
244 std::cout << "incident vertices of cell " << c << ": ";
245 // loop over all incident vertices
246 for (auto v : mesh->vertices(c))
247 std::cout << v << " ";
248 std::cout << std::endl;
249 }
250
251 std::cout << "----------------------------------------\n";
252 std::cout << "The incident edges of each cell" << std::endl;
253 std::cout << "----------------------------------------\n";
254 // loop over all cells
255 for (auto c : mesh->cells()) {
256 std::cout << "incident edges of cell " << c << ": ";
257 // loop over all incident edges
258 for (auto e : mesh->edges(c))
259 std::cout << e << " ";
260 std::cout << std::endl;
261 }
262
263 std::cout << "----------------------------------------\n";
264 std::cout << "The incident halffaces of each cell" << std::endl;
265 std::cout << "----------------------------------------\n";
266 // loop over all cells
267 for (auto c : mesh->cells()) {
268 std::cout << "incident halffaces of cell " << c << ": ";
269 // loop over all incident halffaces
270 for (auto h : mesh->halffaces(c))
271 std::cout << h << " ";
272 std::cout << std::endl;
273 }
274
275 std::cout << "----------------------------------------\n";
276 std::cout << "The two halffaces of each face" << std::endl;
277 std::cout << "----------------------------------------\n";
278 // loop over all faces
279 for (auto f : mesh->faces()) {
280 // A face is stored as two halffaces (one per side). For a boundary face the outward side
281 // has no cell, so one of these two handles is invalid.
282 std::cout << "incident halffaces of face " << f << ": " << mesh->halfface(f, 0) << " " << mesh->halfface(f, 1)
283 << std::endl;
284 }
285
286 // Delete the mesh (i.e., release memory)
287 delete mesh;
288
289 return EXIT_SUCCESS;
290}
Data structure representing a polyhedral mesh.
Definition poly_mesh.h:50
Definition collider.cpp:182
See also
PolyMesh, Tutorial_112_PolyMesh, Tutorial_105_SurfaceMesh_Connectivity, Tutorial_109_Graph_Connectivity