Easy3D 2.5.3
Tutorial_104_SurfaceMesh
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/core/surface_mesh_builder.h>
29#include <easy3d/util/initializer.h>
30
31
32using namespace easy3d;
33
34
35// This example shows how to
36// - construct a mesh from its vertices and known connectivity
37
38
39int main(int argc, char** argv) {
40 // initialize Easy3D.
41 initialize();
42
43 // Easy3D provides two options to construct a surface mesh.
44 // - Option 1: use the add_vertex() and add_[face/triangle/quad]() functions of SurfaceMesh. You can only choose
45 // this option if you are sure that the mesh is manifold.
46 // - Option 2: use the SurfaceMeshBuilder that can resolve non-manifoldness during the construction of a mesh. This
47 // is the default option in Easy3D and client code is highly recommended to use SurfaceMeshBuilder.
48
49 // You can easily change an option.
50 const int option = 2;
51
52 // In this example, we create a surface mesh representing a tetrahedron (i.e., 4 triangle faces, 4 vertices).
53 //
54 // v0
55 // /|\
56 // / | \
57 // / | \
58 // v1 /_ _|_ _\ v2
59 // \ | /
60 // \ | /
61 // \ | /
62 // v3
63 //
64 const std::vector<vec3> points = {
65 vec3(0, 0, 0),
66 vec3(1, 0, 0),
67 vec3(0, 1, 0),
68 vec3(0, 0, 1)
69 };
70
71 // Create a surface mesh
72 SurfaceMesh mesh;
73
74 if (option == 1) { // Option 1: use the built-in functions of SurfaceMesh.
75 // Add vertices
76 SurfaceMesh::Vertex v0 = mesh.add_vertex(points[0]);
77 SurfaceMesh::Vertex v1 = mesh.add_vertex(points[1]);
78 SurfaceMesh::Vertex v2 = mesh.add_vertex(points[2]);
79 SurfaceMesh::Vertex v3 = mesh.add_vertex(points[3]);
80 // Add faces
81 mesh.add_triangle(v0, v1, v3);
82 mesh.add_triangle(v1, v2, v3);
83 mesh.add_triangle(v2, v0, v3);
84 mesh.add_triangle(v0, v2, v1);
85 }
86
87 else if (option == 2) { // Option 2: use SurfaceMeshBuilder.
88 // Add vertices
89 SurfaceMeshBuilder builder(&mesh);
90 builder.begin_surface();
91 SurfaceMesh::Vertex v0 = builder.add_vertex(vec3(0, 0, 0));
92 SurfaceMesh::Vertex v1 = builder.add_vertex(vec3(1, 0, 0));
93 SurfaceMesh::Vertex v2 = builder.add_vertex(vec3(0, 1, 0));
94 SurfaceMesh::Vertex v3 = builder.add_vertex(vec3(0, 0, 1));
95 // Add faces
96 builder.add_triangle(v0, v1, v3);
97 builder.add_triangle(v1, v2, v3);
98 builder.add_triangle(v2, v0, v3);
99 builder.add_triangle(v0, v2, v1);
100 builder.end_surface(false);
101 }
102 else
103 LOG(ERROR) << "option must be 1 or 2";
104
105 std::cout << "#face: " << mesh.n_faces() << std::endl;
106 std::cout << "#vertex: " << mesh.n_vertices() << std::endl;
107 std::cout << "#edge: " << mesh.n_edges() << std::endl;
108
109 return EXIT_SUCCESS;
110}
111
A helper class for constructing manifold surface mesh models.
Definition: surface_mesh_builder.h:56
void end_surface(bool log_issues=true)
Finalize surface construction. Must be called at the end of the surface construction and used in pair...
Definition: surface_mesh_builder.cpp:69
Face add_triangle(Vertex v1, Vertex v2, Vertex v3)
Add a new triangle face connecting vertices v1, v2, and v3.
Definition: surface_mesh_builder.cpp:355
Vertex add_vertex(const vec3 &p)
Add a vertex to the mesh.
Definition: surface_mesh_builder.cpp:228
void begin_surface()
Begin surface construction. Must be called at the beginning of the surface construction and used in p...
Definition: surface_mesh_builder.cpp:54
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
unsigned int n_faces() const
returns number of faces in the mesh
Definition: surface_mesh.h:1076
unsigned int n_edges() const
returns number of edges in the mesh
Definition: surface_mesh.h:1074
Face add_triangle(Vertex v1, Vertex v2, Vertex v3)
Definition: surface_mesh.cpp:491
Vertex add_vertex(const vec3 &p)
add a new vertex with position p
Definition: surface_mesh.h:1034
unsigned int n_vertices() const
returns number of vertices in the mesh
Definition: surface_mesh.h:1070
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:104