Easy3D 2.5.3
Tutorial_106_SurfaceMesh_Property
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 create and access properties defined on a surface mesh.
35// We use per-face properties as example, you should be able to do similarly for per-edge/vertex properties also.
36
37
38SurfaceMesh* old_mesh_from_previous_example() {
39 // Create a surface mesh
40 auto mesh = new SurfaceMesh;
41
42 // Add 4 vertices
43 SurfaceMesh::Vertex v0 = mesh->add_vertex(vec3(0, 0, 0));
44 SurfaceMesh::Vertex v1 = mesh->add_vertex(vec3(1, 0, 0));
45 SurfaceMesh::Vertex v2 = mesh->add_vertex(vec3(0, 1, 0));
46 SurfaceMesh::Vertex v3 = mesh->add_vertex(vec3(0, 0, 1));
47
48 // Add 4 triangular faces
49 mesh->add_triangle(v0, v1, v3);
50 mesh->add_triangle(v1, v2, v3);
51 mesh->add_triangle(v2, v0, v3);
52 mesh->add_triangle(v0, v2, v1);
53
54 return mesh;
55}
56
57int main(int argc, char** argv) {
58 // initialize Easy3D.
59 initialize();
60
61 // The mesh created from the previous tutorial.
62 SurfaceMesh* mesh = old_mesh_from_previous_example();
63
64 // We add a per-face property "f:normal" storing the normal of each face
65 SurfaceMesh::FaceProperty<vec3> normals = mesh->add_face_property<vec3>("f:normal");
66
67 // for each face, we access the face normal and print it.
68 for (auto f : mesh->faces()) {
69 // We use the built-in function of SurfaceMesh compute_face_normal().
70 // Of course, you can write your own function to compute the normal of
71 // a face (the normalized cross product of two consecutive edge vectors).
72 normals[f] = mesh->compute_face_normal(f);
73 std::cout << "normal of face " << f << ": " << normals[f] << std::endl;
74 }
75
76 delete mesh;
77
78 return EXIT_SUCCESS;
79}
80
Definition: surface_mesh.h:257
A halfedge data structure for polygonal meshes of 2-manifold.
Definition: surface_mesh.h:52
FaceContainer faces() const
returns face container for C++11 range-based for-loops
Definition: surface_mesh.h:1685
vec3 compute_face_normal(Face f) const
compute normal vector of face f.
Definition: surface_mesh.cpp:1035
FaceProperty< T > add_face_property(const std::string &name, const T t=T())
Definition: surface_mesh.h:1395
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