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
41
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 SurfaceMesh* 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
81 // loop over all vertices
82 for (auto v : mesh->vertices()) {
83 std::cout << "incident vertices of vertex " << v << ": ";
84#ifdef USE_FOR_LOOP
85 // loop over all incident vertices
86 for (auto vv : mesh->vertices(v))
87 std::cout << vv << " ";
88#else // use circulator
89 SurfaceMesh::VertexAroundVertexCirculator cir = mesh->vertices(v);
91 do {
92 SurfaceMesh::Vertex vv = *cir;
93 std::cout << vv << " ";
94 ++cir;
95 } while (cir != end);
96#endif
97 std::cout << std::endl;
98 }
99
100 std::cout << "\n--------------------------------------\n";
101 std::cout << "The incident outgoing/ingoing edges of each vertex" << std::endl;
102 std::cout << "----------------------------------------\n";
103
104 // loop over all vertices
105 for (auto v : mesh->vertices()) {
106 std::cout << "incident outgoing/ingoing edges of vertex " << v << ": ";
107#ifdef USE_FOR_LOOP
108 // loop over all incident outgoing edges
109 for (auto h : mesh->halfedges(v))
110 std::cout << h << "/" << mesh->opposite(h) << " ";
111#else // use circulator
112 SurfaceMesh::HalfedgeAroundVertexCirculator cir = mesh->halfedges(v);
114 do {
115 SurfaceMesh::Halfedge h = *cir;
116 std::cout << h << "/" << mesh->opposite(h) << " ";
117 ++cir;
118 } while (cir != end);
119#endif
120 std::cout << std::endl;
121 }
122
123 std::cout << "\n--------------------------------------\n";
124 std::cout << "The incident faces of each vertex" << std::endl;
125 std::cout << "----------------------------------------\n";
126
127 // loop over all vertices
128 for (auto v : mesh->vertices()) {
129 std::cout << "incident faces of vertex " << v << ": ";
130#ifdef USE_FOR_LOOP
131 // loop over all incident faces
132 for (auto f : mesh->faces(v))
133 std::cout << f << " ";
134#else // use circulator
135 SurfaceMesh::FaceAroundVertexCirculator cir = mesh->faces(v);
137 do {
138 SurfaceMesh::Face f = *cir;
139 std::cout << f << " ";
140 ++cir;
141 } while (cir != end);
142#endif
143 std::cout << std::endl;
144 }
145
146 std::cout << "\n--------------------------------------\n";
147 std::cout << "The incident vertices of each face" << std::endl;
148 std::cout << "----------------------------------------\n";
149
150 // loop over all faces
151 for (auto f : mesh->faces()) {
152 std::cout << "incident vertices of face " << f << ": ";
153#ifdef USE_FOR_LOOP
154 // loop over all incident vertices
155 for (auto v : mesh->vertices(f))
156 std::cout << v << " ";
157#else // use circulator
158 SurfaceMesh::VertexAroundFaceCirculator cir = mesh->vertices(f);
160 do {
161 SurfaceMesh::Vertex v = *cir;
162 std::cout << v << " ";
163 ++cir;
164 } while (cir != end);
165#endif
166 std::cout << std::endl;
167 }
168
169 std::cout << "\n--------------------------------------\n";
170 std::cout << "The incident half-edges of each face" << std::endl;
171 std::cout << "----------------------------------------\n";
172
173 // loop over all faces
174 for (auto f : mesh->faces()) {
175 std::cout << "half-edges around face " << f << ": ";
176#ifdef USE_FOR_LOOP
177 // loop over all half-edges around the face
178 for (auto h : mesh->halfedges(f))
179 std::cout << h << " ";
180#else
181 SurfaceMesh::HalfedgeAroundFaceCirculator cir = mesh->halfedges(f);
183 do {
184 SurfaceMesh::Halfedge h = *cir;
185 std::cout << h << " ";
186 ++cir;
187 } while (cir != end);
188#endif
189 std::cout << std::endl;
190 }
191
192 std::cout << "\n--------------------------------------\n";
193 std::cout << "The two end points of each edge" << std::endl;
194 std::cout << "----------------------------------------\n";
195
196 // loop over all edges
197 for (auto e : mesh->edges()) {
198 std::cout << "the two end points of edge " << e << ": ";
199 SurfaceMesh::Vertex vs = mesh->vertex(e, 0);
200 std::cout << vs << " ";
201 SurfaceMesh::Vertex vt = mesh->vertex(e, 1);
202 std::cout << vt << " " << std::endl;
203 }
204
205 std::cout << "\n--------------------------------------\n";
206 std::cout << "The two faces connected by each edge" << std::endl;
207 std::cout << "----------------------------------------\n";
208
209 // loop over all edges
210 for (auto e : mesh->edges()) {
211 std::cout << "the two faces connected by edge " << e << ": ";
212 SurfaceMesh::Halfedge h0 = mesh->halfedge(e, 0);
213 if (mesh->is_border(h0))
214 std::cout << "NULL" << " ";
215 else
216 std::cout << mesh->face(h0) << " ";
217
218 SurfaceMesh::Halfedge h1 = mesh->halfedge(e, 1);
219 if (mesh->is_border(h1))
220 std::cout << "NULL" << " ";
221 else
222 std::cout << mesh->face(h1) << " ";
223
224 std::cout << std::endl;
225 }
226
227 // Delete the mesh (i.e., release memory)
228 delete mesh;
229
230 return EXIT_SUCCESS;
231}
232
A halfedge data structure for polygonal meshes of 2-manifold.
Definition surface_mesh.h:51
Definition collider.cpp:182
Vec< 3, float > vec3
A 3D point/vector of float type.
Definition types.h:44
void initialize(bool info_to_stdout, bool use_log_file, bool use_setting_file, const std::string &resource_dir)
Initialization of Easy3D.
Definition initializer.cpp:39
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