Easy3D 2.5.3
Tutorial_113_PolyMesh_Connectivity
1/********************************************************************
2 * Copyright (C) 2015 Liangliang Nan <liangliang.nan@gmail.com>
3 * https://3d.bk.tudelft.nl/liangliang/
4 *
5 * This file is part of Easy3D. If it is useful in your research/work,
6 * I would be grateful if you show your appreciation by citing it:
7 * ------------------------------------------------------------------
8 * Liangliang Nan.
9 * Easy3D: a lightweight, easy-to-use, and efficient C++ library
10 * for processing and rendering 3D data.
11 * Journal of Open Source Software, 6(64), 3255, 2021.
12 * ------------------------------------------------------------------
13 *
14 * Easy3D is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License Version 3
16 * as published by the Free Software Foundation.
17 *
18 * Easy3D is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 ********************************************************************/
26
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
29
30
31using namespace easy3d;
32
33
34// This example shows how to access the adjacency information of a polyhedral mesh (consisting of a single tetrahedron),
35// i.e.,
36// - the incident vertices of each vertex
37// - the incident edges of each vertex
38// - the incident halffaces of each vertex
39// - the incident cells of each vertex
40// - the incident vertices of each edge
41// - the incident halffaces of each edge
42// - the incident cells of each edge
43// - the incident vertices of each halfface
44// - the incident edges of each halfface
45// - the associated cell of each halfface
46// - the opposite cell of each halfface
47// - the incident vertices of each cell
48// - the incident edges of each cell
49// - the incident halffaces of each cell
50// - the two halffaces of each face
51
52
53// the mesh created in the previous tutorial (so you can skip it)
54PolyMesh *old_mesh_from_previous_example() {
55 // Create a polyhedral mesh
56 auto mesh = new PolyMesh;
57
58 // Add four vertices
59 auto v0 = mesh->add_vertex(vec3(-1.0, 0.0, 0.0));
60 auto v1 = mesh->add_vertex(vec3(0.0, 0.0, 1.0));
61 auto v2 = mesh->add_vertex(vec3(1.0, 0.0, 0.0));
62 auto v3 = mesh->add_vertex(vec3(0.0, 0.0, -1.0));
63
64 // Add the only tetrahedron
65 mesh->add_tetra(v0, v1, v2, v3);
66
67 return mesh;
68}
69
70int main(int argc, char **argv) {
71 // initialize Easy3D.
72 initialize();
73
74 // Create mesh object
75 PolyMesh *mesh = old_mesh_from_previous_example();
76
77 std::cout << "----------------------------------------\n";
78 std::cout << "The incident vertices of each vertex" << std::endl;
79 std::cout << "----------------------------------------\n";
80 // loop over all vertices
81 for (auto v : mesh->vertices()) {
82 std::cout << "incident vertices of vertex " << v << ": ";
83 // loop over all incident vertices
84 for (auto vv : mesh->vertices(v))
85 std::cout << vv << " ";
86 std::cout << std::endl;
87 }
88
89 std::cout << "----------------------------------------\n";
90 std::cout << "The incident edges of each vertex" << std::endl;
91 std::cout << "----------------------------------------\n";
92 // loop over all vertices
93 for (auto v : mesh->vertices()) {
94 std::cout << "incident edges of vertex " << v << ": ";
95 // loop over all incident edges
96 for (auto e : mesh->edges(v))
97 std::cout << e << " ";
98 std::cout << std::endl;
99 }
100
101 std::cout << "----------------------------------------\n";
102 std::cout << "The incident halffaces of each vertex" << std::endl;
103 std::cout << "----------------------------------------\n";
104 // loop over all vertices
105 for (auto v : mesh->vertices()) {
106 std::cout << "incident halffaces of vertex " << v << ": ";
107 // loop over all incident edges
108 for (auto h : mesh->halffaces(v))
109 std::cout << h << " ";
110 std::cout << std::endl;
111 }
112
113 std::cout << "----------------------------------------\n";
114 std::cout << "The incident cells of each vertex" << std::endl;
115 std::cout << "----------------------------------------\n";
116 // loop over all vertices
117 for (auto v : mesh->vertices()) {
118 std::cout << "incident cells of vertex " << v << ": ";
119 // loop over all incident cells
120 for (auto c : mesh->cells(v))
121 std::cout << c << " ";
122 std::cout << std::endl;
123 }
124
125 std::cout << "----------------------------------------\n";
126 std::cout << "The incident vertices of each edge" << std::endl;
127 std::cout << "----------------------------------------\n";
128 // loop over all edges
129 for (auto e : mesh->edges()) {
130 std::cout << "incident vertices of edge " << e << ": " << mesh->vertex(e, 0) << " " << mesh->vertex(e, 1)
131 << std::endl;
132 }
133
134 std::cout << "----------------------------------------\n";
135 std::cout << "The incident halffaces of each edge" << std::endl;
136 std::cout << "----------------------------------------\n";
137 // loop over all edges
138 for (auto e : mesh->edges()) {
139 std::cout << "incident halffaces of edge " << e << ": ";
140 // loop over all incident halffaces
141 for (auto h : mesh->halffaces(e))
142 std::cout << h << " ";
143 std::cout << std::endl;
144 }
145
146 std::cout << "----------------------------------------\n";
147 std::cout << "The incident cells of each edge" << std::endl;
148 std::cout << "----------------------------------------\n";
149 // loop over all edges
150 for (auto e : mesh->edges()) {
151 std::cout << "incident cells of edge " << e << ": ";
152 // loop over all incident cells
153 for (auto c : mesh->cells(e))
154 std::cout << c << " ";
155 std::cout << std::endl;
156 }
157
158 std::cout << "----------------------------------------\n";
159 std::cout << "The incident vertices of each halfface" << std::endl;
160 std::cout << "----------------------------------------\n";
161 // loop over all halffaces
162 for (auto h : mesh->halffaces()) {
163 std::cout << "incident vertices of halfface " << h << ": ";
164 // loop over all incident cells
165 for (auto v : mesh->vertices(h))
166 std::cout << v << " ";
167 std::cout << std::endl;
168 }
169
170 std::cout << "----------------------------------------\n";
171 std::cout << "The incident edges of each halfface" << std::endl;
172 std::cout << "----------------------------------------\n";
173 // loop over all halffaces
174 for (auto h : mesh->halffaces()) {
175 std::cout << "incident edges of halfface " << h << ": ";
176 // loop over all incident edges
177 for (auto e : mesh->edges(h))
178 std::cout << e << " ";
179 std::cout << std::endl;
180 }
181
182 std::cout << "----------------------------------------\n";
183 std::cout << "The associated cell of each halfface" << std::endl;
184 std::cout << "----------------------------------------\n";
185 // loop over all halffaces
186 for (auto h : mesh->halffaces()) {
187 std::cout << "incident associated cell of halfface " << h << ": " << mesh->cell(h) << std::endl;
188 }
189
190 std::cout << "----------------------------------------\n";
191 std::cout << "The opposite halfface and cell of each halfface" << std::endl;
192 std::cout << "----------------------------------------\n";
193 // loop over all halffaces
194 for (auto h : mesh->halffaces()) {
195 std::cout << "opposite halfface of halfface " << h << ": " << mesh->opposite(h) << ". ";
196 std::cout << "opposite cell of halfface " << h << ": " << mesh->cell(mesh->opposite(h)) << std::endl;
197 }
198
199 std::cout << "----------------------------------------\n";
200 std::cout << "The incident vertices of each cell" << std::endl;
201 std::cout << "----------------------------------------\n";
202 // loop over all cells
203 for (auto c : mesh->cells()) {
204 std::cout << "incident vertices of cell " << c << ": ";
205 // loop over all incident vertices
206 for (auto v : mesh->vertices(c))
207 std::cout << v << " ";
208 std::cout << std::endl;
209 }
210
211 std::cout << "----------------------------------------\n";
212 std::cout << "The incident edges of each cell" << std::endl;
213 std::cout << "----------------------------------------\n";
214 // loop over all cells
215 for (auto c : mesh->cells()) {
216 std::cout << "incident edges of cell " << c << ": ";
217 // loop over all incident edges
218 for (auto e : mesh->edges(c))
219 std::cout << e << " ";
220 std::cout << std::endl;
221 }
222
223 std::cout << "----------------------------------------\n";
224 std::cout << "The incident halffaces of each cell" << std::endl;
225 std::cout << "----------------------------------------\n";
226 // loop over all cells
227 for (auto c : mesh->cells()) {
228 std::cout << "incident halffaces of cell " << c << ": ";
229 // loop over all incident halffaces
230 for (auto h : mesh->halffaces(c))
231 std::cout << h << " ";
232 std::cout << std::endl;
233 }
234
235 std::cout << "----------------------------------------\n";
236 std::cout << "The two halffaces of each face" << std::endl;
237 std::cout << "----------------------------------------\n";
238 // loop over all faces
239 for (auto f : mesh->faces()) {
240 std::cout << "incident halffaces of face " << f << ": " << mesh->halfface(f, 0) << " " << mesh->halfface(f, 1)
241 << std::endl;
242 }
243
244 return EXIT_SUCCESS;
245}
246
Data structure representing a polyhedral mesh.
Definition: poly_mesh.h:50
HalfFace opposite(HalfFace h) const
returns the twin halfface of halfface h.
Definition: poly_mesh.h:1231
EdgeContainer edges() const
returns edge container for C++11 range-based for-loops
Definition: poly_mesh.h:1147
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: poly_mesh.h:1129
FaceContainer faces() const
returns face container for C++11 range-based for-loops
Definition: poly_mesh.h:1183
CellContainer cells() const
returns cell container for C++11 range-based for-loops
Definition: poly_mesh.h:1201
HalffaceContainer halffaces() const
returns halfface container for C++11 range-based for-loops
Definition: poly_mesh.h:1165
Cell cell(HalfFace h) const
returns the cell associated with halfface h
Definition: poly_mesh.h:1311
Vertex add_vertex(const vec3 &p)
add a new vertex with position p
Definition: poly_mesh.h:709
HalfFace halfface(Face f, unsigned int i) const
returns the i'th halfface of face f. i has to be 0 or 1.
Definition: poly_mesh.h:1219
Vertex vertex(Edge e, unsigned int i) const
returns the i'th vertex of edge e. i has to be 0 or 1.
Definition: poly_mesh.h:1237
Definition: collider.cpp:182
void initialize(bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition: initializer.cpp:35