Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_106_SurfaceMesh_Property

Storing a per-face attribute (the face normal) on a SurfaceMesh through a named property.

Overview

This console tutorial applies the property system from Tutorial_102_PointCloud_Property to a surface mesh, using faces instead of points. On the tetrahedron from Tutorial_104 we create a per-face property "f:normal", fill it with each face's normal, and print the results. The same pattern works for per-vertex ("v:") and per-edge ("e:") properties — only the add_*_property / handle type changes.

The technique

A property is a parallel array with one slot per element, indexed by the element handle, so a FaceProperty<vec3> stores one normal per face. The normal of a planar face is the unit vector perpendicular to its plane. For a triangle with corners p0, p1, p2 it is normalize((p1 - p0) x (p2 - p0)): the cross product of two edge vectors gives a perpendicular whose direction follows the face winding (right-hand rule), and normalizing makes it unit length. SurfaceMesh::compute_face_normal() does this for you and also handles general (non-triangular) polygons robustly. Normals are the basis of lighting, shading, offsetting and many geometry-processing operations, which is why meshes routinely cache them as a property.

Easy3D API

Running it

Console-only. It prints one line per face, e.g. "normal of face f0: ...", for the four faces of the tetrahedron. The mesh is heap-allocated and deleted at the end to release its memory.

1
27#include <easy3d/core/surface_mesh.h>
28#include <easy3d/util/initializer.h>
29
71using namespace easy3d;
72
73// Rebuilds the tetrahedron from Tutorial_104 so this example is self-contained.
74SurfaceMesh *old_mesh_from_previous_example() {
75 // Create a surface mesh
76 auto mesh = new SurfaceMesh;
77
78 // Add 4 vertices
79 SurfaceMesh::Vertex v0 = mesh->add_vertex(vec3(0, 0, 0));
80 SurfaceMesh::Vertex v1 = mesh->add_vertex(vec3(1, 0, 0));
81 SurfaceMesh::Vertex v2 = mesh->add_vertex(vec3(0, 1, 0));
82 SurfaceMesh::Vertex v3 = mesh->add_vertex(vec3(0, 0, 1));
83
84 // Add 4 triangular faces
85 mesh->add_triangle(v0, v1, v3);
86 mesh->add_triangle(v1, v2, v3);
87 mesh->add_triangle(v2, v0, v3);
88 mesh->add_triangle(v0, v2, v1);
89
90 return mesh;
91}
92
93int main(int argc, char **argv) {
94 // initialize Easy3D.
95 initialize();
96
97 // Create a mesh
98 SurfaceMesh *mesh = old_mesh_from_previous_example();
99
100 // We add a per-face property "f:normal" storing the normal of each face
101 SurfaceMesh::FaceProperty<vec3> normals = mesh->add_face_property<vec3>("f:normal");
102
103 // for each face, we access the face normal and print it.
104 for (auto f : mesh->faces()) {
105 // We use the built-in function of SurfaceMesh compute_face_normal().
106 // Of course, you can write your own function to compute the normal of
107 // a face (the normalized cross product of two consecutive edge vectors).
108 normals[f] = mesh->compute_face_normal(f);
109 std::cout << "normal of face " << f << ": " << normals[f] << std::endl;
110 }
111
112 // Delete the mesh (i.e., release memory)
113 delete mesh;
114
115 return EXIT_SUCCESS;
116}
Face property of type T.
Definition surface_mesh.h:363
A halfedge data structure for polygonal meshes of 2-manifold topology.
Definition surface_mesh.h:51
Definition collider.cpp:182
This type represents a vertex (internally it is basically an index).
Definition surface_mesh.h:135
See also
SurfaceMesh, Tutorial_102_PointCloud_Property, Tutorial_104_SurfaceMesh, Tutorial_114_PolyMesh_Property