Easy3D 2.5.3
Tutorial_105_SurfaceMesh_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/surface_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 surface mesh, i.e.,
35// - the incident vertices of each vertex
36// - the incident outgoing/ingoing edges of each vertex
37// - the incident faces of each vertex
38// - the incident vertices of each face
39// - the incident half-edges of each face
40// - the two end points of each edge;
41// - the two faces connected by each edge
42
43
44// There are two ways to traverse the incident entities of an element.
45// - use a "for" loop (cleaner code);
46// - use a circulator.
47#define USE_FOR_LOOP
48
49// the mesh created in the previous tutorial (so you can skip it)
50SurfaceMesh* old_mesh_from_previous_example() {
51 // Create a surface mesh
52 auto mesh = new SurfaceMesh;
53
54 // Add 4 vertices
55 SurfaceMesh::Vertex v0 = mesh->add_vertex(vec3(0, 0, 0));
56 SurfaceMesh::Vertex v1 = mesh->add_vertex(vec3(1, 0, 0));
57 SurfaceMesh::Vertex v2 = mesh->add_vertex(vec3(0, 1, 0));
58 SurfaceMesh::Vertex v3 = mesh->add_vertex(vec3(0, 0, 1));
59
60 // Add 4 triangular faces
61 mesh->add_triangle(v0, v1, v3);
62 mesh->add_triangle(v1, v2, v3);
63 mesh->add_triangle(v2, v0, v3);
64 mesh->add_triangle(v0, v2, v1);
65
66 return mesh;
67}
68
69
70int main(int argc, char** argv) {
71 // initialize Easy3D.
72 initialize();
73
74 SurfaceMesh* mesh = old_mesh_from_previous_example();
75
76 std::cout << "----------------------------------------\n";
77 std::cout << "The incident vertices of each vertex" << std::endl;
78 std::cout << "----------------------------------------\n";
79
80 // loop over all vertices
81 for (auto v : mesh->vertices()) {
82 std::cout << "incident vertices of vertex " << v << ": ";
83#ifdef USE_FOR_LOOP
84 // loop over all incident vertices
85 for (auto vv : mesh->vertices(v))
86 std::cout << vv << " ";
87#else // use circulator
90 do {
91 SurfaceMesh::Vertex vv = *cir;
92 std::cout << vv << " ";
93 ++cir;
94 } while (cir != end);
95#endif
96 std::cout << std::endl;
97 }
98
99 std::cout << "\n--------------------------------------\n";
100 std::cout << "The incident outgoing/ingoing edges of each vertex" << std::endl;
101 std::cout << "----------------------------------------\n";
102
103 // loop over all vertices
104 for (auto v : mesh->vertices()) {
105 std::cout << "incident outgoing/ingoing edges of vertex " << v << ": ";
106#ifdef USE_FOR_LOOP
107 // loop over all incident outgoing edges
108 for (auto h : mesh->halfedges(v))
109 std::cout << h << "/" << mesh->opposite(h) << " ";
110#else // use circulator
113 do {
114 SurfaceMesh::Halfedge h = *cir;
115 std::cout << h << "/" << mesh->opposite(h) << " ";
116 ++cir;
117 } while (cir != end);
118#endif
119 std::cout << std::endl;
120 }
121
122 std::cout << "\n--------------------------------------\n";
123 std::cout << "The incident faces of each vertex" << std::endl;
124 std::cout << "----------------------------------------\n";
125
126 // loop over all vertices
127 for (auto v : mesh->vertices()) {
128 std::cout << "incident faces of vertex " << v << ": ";
129#ifdef USE_FOR_LOOP
130 // loop over all incident faces
131 for (auto f : mesh->faces(v))
132 std::cout << f << " ";
133#else // use circulator
136 do {
137 SurfaceMesh::Face f = *cir;
138 std::cout << f << " ";
139 ++cir;
140 } while (cir != end);
141#endif
142 std::cout << std::endl;
143 }
144
145 std::cout << "\n--------------------------------------\n";
146 std::cout << "The incident vertices of each face" << std::endl;
147 std::cout << "----------------------------------------\n";
148
149 // loop over all faces
150 for (auto f : mesh->faces()) {
151 std::cout << "incident vertices of face " << f << ": ";
152#ifdef USE_FOR_LOOP
153 // loop over all incident vertices
154 for (auto v : mesh->vertices(f))
155 std::cout << v << " ";
156#else // use circulator
159 do {
160 SurfaceMesh::Vertex v = *cir;
161 std::cout << v << " ";
162 ++cir;
163 } while (cir != end);
164#endif
165 std::cout << std::endl;
166 }
167
168 std::cout << "\n--------------------------------------\n";
169 std::cout << "The incident half-edges of each face" << std::endl;
170 std::cout << "----------------------------------------\n";
171
172 // loop over all faces
173 for (auto f : mesh->faces()) {
174 std::cout << "half-edges around face " << f << ": ";
175#ifdef USE_FOR_LOOP
176 // loop over all half-edges around the face
177 for (auto h : mesh->halfedges(f))
178 std::cout << h << " ";
179#else
182 do {
183 SurfaceMesh::Halfedge h = *cir;
184 std::cout << h << " ";
185 ++cir;
186 } while (cir != end);
187#endif
188 std::cout << std::endl;
189 }
190
191 std::cout << "\n--------------------------------------\n";
192 std::cout << "The two end points of each edge" << std::endl;
193 std::cout << "----------------------------------------\n";
194
195 // loop over all edges
196 for (auto e : mesh->edges()) {
197 std::cout << "the two end points of edge " << e << ": ";
198 SurfaceMesh::Vertex vs = mesh->vertex(e, 0);
199 std::cout << vs << " ";
200 SurfaceMesh::Vertex vt = mesh->vertex(e, 1);
201 std::cout << vt << " " << std::endl;
202 }
203
204 std::cout << "\n--------------------------------------\n";
205 std::cout << "The two faces connected by each edge" << std::endl;
206 std::cout << "----------------------------------------\n";
207
208 // loop over all edges
209 for (auto e : mesh->edges()) {
210 std::cout << "the two faces connected by edge " << e << ": ";
211 SurfaceMesh::Halfedge h0 = mesh->halfedge(e, 0);
212 if (mesh->is_border(h0))
213 std::cout << "NULL" << " ";
214 else
215 std::cout << mesh->face(h0) << " ";
216
217 SurfaceMesh::Halfedge h1 = mesh->halfedge(e, 1);
218 if (mesh->is_border(h1))
219 std::cout << "NULL" << " ";
220 else
221 std::cout << mesh->face(h1) << " ";
222
223 std::cout << std::endl;
224 }
225
226 // Delete the mesh (i.e., release memory)
227 delete mesh;
228
229 return EXIT_SUCCESS;
230}
231
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
bool is_border(Vertex v) const
returns whether v is a boundary vertex
Definition: surface_mesh.h:1176
EdgeContainer edges() const
returns edge container for C++11 range-based for-loops
Definition: surface_mesh.h:1667
VertexContainer vertices() const
returns vertex container for C++11 range-based for-loops
Definition: surface_mesh.h:1631
Halfedge opposite(Halfedge h) const
returns the opposite halfedge of h
Definition: surface_mesh.h:1260
Halfedge halfedge(Edge e, unsigned int i) const
returns the i'th halfedge of edge e. i has to be 0 or 1.
Definition: surface_mesh.h:1307
FaceContainer faces() const
returns face container for C++11 range-based for-loops
Definition: surface_mesh.h:1685
HalfedgeContainer halfedges() const
returns halfedge container for C++11 range-based for-loops
Definition: surface_mesh.h:1649
Face face(Halfedge h) const
returns the face incident to halfedge h
Definition: surface_mesh.h:1229
Vertex vertex(Edge e, unsigned int i) const
returns the i'th vertex of edge e. i has to be 0 or 1.
Definition: surface_mesh.h:1314
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
Definition: surface_mesh.h:134
Definition: surface_mesh.h:114
Definition: surface_mesh.h:104