Easy3D 2.6.1
Loading...
Searching...
No Matches
Tutorial_105_SurfaceMesh_Connectivity/main.cpp

This example shows how to access the adjacency information of a surface mesh, i.e.,

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