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 incident vertices of each vertex
- the incident edges of each vertex
- the incident halffaces of each vertex
- the incident cells of each vertex
- the incident vertices of each edge
- the incident halffaces of each edge
- the incident cells of each edge
- the incident vertices of each halfface
- the incident edges of each halfface
- the associated cell of each halfface
- the opposite cell of each halfface
- the incident vertices of each cell
- the incident edges of each cell
- the incident halffaces of each cell
- the two halffaces of each face It is the volumetric counterpart of Tutorial_105_SurfaceMesh_Connectivity.
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
- PolyMesh::vertices(x)/edges(x)/halffaces(x)/cells(x): incident elements of a vertex, edge, halfface or cell (the overloads relevant to each are used below).
- PolyMesh::vertex(e, i): an edge endpoint; halfface(f, i): the i-th halfface of a face.
- PolyMesh::cell(h): the cell bounded by halfface h; opposite(h): its twin across the face.
- PolyMesh::vertices()/edges()/halffaces()/faces()/cells(): ranges over all elements.
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.
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
91PolyMesh *old_mesh_from_previous_example() {
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));
102 mesh->add_tetra(v0, v1, v2, v3);
107int main(
int argc,
char **argv) {
112 PolyMesh *mesh = old_mesh_from_previous_example();
114 std::cout <<
"----------------------------------------\n";
115 std::cout <<
"The incident vertices of each vertex" << std::endl;
116 std::cout <<
"----------------------------------------\n";
118 for (
auto v : mesh->vertices()) {
119 std::cout <<
"incident vertices of vertex " << v <<
": ";
121 for (
auto vv : mesh->vertices(v))
122 std::cout << vv <<
" ";
123 std::cout << std::endl;
126 std::cout <<
"----------------------------------------\n";
127 std::cout <<
"The incident edges of each vertex" << std::endl;
128 std::cout <<
"----------------------------------------\n";
130 for (
auto v : mesh->vertices()) {
131 std::cout <<
"incident edges of vertex " << v <<
": ";
133 for (
auto e : mesh->edges(v))
134 std::cout << e <<
" ";
135 std::cout << std::endl;
138 std::cout <<
"----------------------------------------\n";
139 std::cout <<
"The incident halffaces of each vertex" << std::endl;
140 std::cout <<
"----------------------------------------\n";
142 for (
auto v : mesh->vertices()) {
143 std::cout <<
"incident halffaces of vertex " << v <<
": ";
145 for (
auto h : mesh->halffaces(v))
146 std::cout << h <<
" ";
147 std::cout << std::endl;
150 std::cout <<
"----------------------------------------\n";
151 std::cout <<
"The incident cells of each vertex" << std::endl;
152 std::cout <<
"----------------------------------------\n";
154 for (
auto v : mesh->vertices()) {
155 std::cout <<
"incident cells of vertex " << v <<
": ";
157 for (
auto c : mesh->cells(v))
158 std::cout << c <<
" ";
159 std::cout << std::endl;
162 std::cout <<
"----------------------------------------\n";
163 std::cout <<
"The incident vertices of each edge" << std::endl;
164 std::cout <<
"----------------------------------------\n";
166 for (
auto e : mesh->edges()) {
167 std::cout <<
"incident vertices of edge " << e <<
": " << mesh->vertex(e, 0) <<
" " << mesh->vertex(e, 1)
171 std::cout <<
"----------------------------------------\n";
172 std::cout <<
"The incident halffaces of each edge" << std::endl;
173 std::cout <<
"----------------------------------------\n";
175 for (
auto e : mesh->edges()) {
176 std::cout <<
"incident halffaces of edge " << e <<
": ";
178 for (
auto h : mesh->halffaces(e))
179 std::cout << h <<
" ";
180 std::cout << std::endl;
183 std::cout <<
"----------------------------------------\n";
184 std::cout <<
"The incident cells of each edge" << std::endl;
185 std::cout <<
"----------------------------------------\n";
187 for (
auto e : mesh->edges()) {
188 std::cout <<
"incident cells of edge " << e <<
": ";
190 for (
auto c : mesh->cells(e))
191 std::cout << c <<
" ";
192 std::cout << std::endl;
195 std::cout <<
"----------------------------------------\n";
196 std::cout <<
"The incident vertices of each halfface" << std::endl;
197 std::cout <<
"----------------------------------------\n";
199 for (
auto h : mesh->halffaces()) {
200 std::cout <<
"incident vertices of halfface " << h <<
": ";
202 for (
auto v : mesh->vertices(h))
203 std::cout << v <<
" ";
204 std::cout << std::endl;
207 std::cout <<
"----------------------------------------\n";
208 std::cout <<
"The incident edges of each halfface" << std::endl;
209 std::cout <<
"----------------------------------------\n";
211 for (
auto h : mesh->halffaces()) {
212 std::cout <<
"incident edges of halfface " << h <<
": ";
214 for (
auto e : mesh->edges(h))
215 std::cout << e <<
" ";
216 std::cout << std::endl;
219 std::cout <<
"----------------------------------------\n";
220 std::cout <<
"The associated cell of each halfface" << std::endl;
221 std::cout <<
"----------------------------------------\n";
223 for (
auto h : mesh->halffaces()) {
224 std::cout <<
"incident associated cell of halfface " << h <<
": " << mesh->cell(h) << std::endl;
227 std::cout <<
"----------------------------------------\n";
228 std::cout <<
"The opposite halfface and cell of each halfface" << std::endl;
229 std::cout <<
"----------------------------------------\n";
231 for (
auto h : mesh->halffaces()) {
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;
239 std::cout <<
"----------------------------------------\n";
240 std::cout <<
"The incident vertices of each cell" << std::endl;
241 std::cout <<
"----------------------------------------\n";
243 for (
auto c : mesh->cells()) {
244 std::cout <<
"incident vertices of cell " << c <<
": ";
246 for (
auto v : mesh->vertices(c))
247 std::cout << v <<
" ";
248 std::cout << std::endl;
251 std::cout <<
"----------------------------------------\n";
252 std::cout <<
"The incident edges of each cell" << std::endl;
253 std::cout <<
"----------------------------------------\n";
255 for (
auto c : mesh->cells()) {
256 std::cout <<
"incident edges of cell " << c <<
": ";
258 for (
auto e : mesh->edges(c))
259 std::cout << e <<
" ";
260 std::cout << std::endl;
263 std::cout <<
"----------------------------------------\n";
264 std::cout <<
"The incident halffaces of each cell" << std::endl;
265 std::cout <<
"----------------------------------------\n";
267 for (
auto c : mesh->cells()) {
268 std::cout <<
"incident halffaces of cell " << c <<
": ";
270 for (
auto h : mesh->halffaces(c))
271 std::cout << h <<
" ";
272 std::cout << std::endl;
275 std::cout <<
"----------------------------------------\n";
276 std::cout <<
"The two halffaces of each face" << std::endl;
277 std::cout <<
"----------------------------------------\n";
279 for (
auto f : mesh->faces()) {
282 std::cout <<
"incident halffaces of face " << f <<
": " << mesh->halfface(f, 0) <<
" " << mesh->halfface(f, 1)
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