Easy3D 3.0.0
Loading...
Searching...
No Matches
Tutorial_114_PolyMesh_Property

Storing per-element attributes on a PolyMesh (a polyhedral / volumetric mesh).

Overview

This console tutorial attaches a custom attribute to every face of a polyhedral mesh and reads it back. It reuses the single-tetrahedron mesh built in Tutorial_112_PolyMesh and computes the centroid (geometric center) of each of its four triangular faces. The same pattern works for per-vertex, per-edge, and per-cell properties — only the element type and the "v:" / "e:" / "f:" / "c:" name prefix change.

The technique

A PolyMesh is Easy3D's volumetric data structure: unlike a SurfaceMesh (which only has a boundary surface), it also represents the interior as 3D cells — here a single tetrahedron. It exposes four element types: vertices, edges, faces, and cells. As with all Easy3D models, attributes are stored as named properties: one contiguous array per attribute, indexed by element handle. The face centroid is simply the arithmetic mean of the face's corner positions, c = (p0 + p1 + ... + pn-1) / n, a basic quantity used everywhere in geometry processing (e.g., for rendering, integration, or locating a face).

Easy3D API

Running it

Console-only. It prints one line per face giving the face handle and its computed centroid, e.g. "center of f0: ...". The mesh is heap-allocated and explicitly deleted at the end.

1
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
29
72using namespace easy3d;
73
74// Rebuilds the single-tetrahedron mesh from Tutorial_112_PolyMesh so this example is self-contained.
75PolyMesh *old_mesh_from_previous_example() {
76 // Create an empty polyhedral mesh.
77 auto mesh = new PolyMesh;
78
79 // Add the four corner vertices of the tetrahedron (all lie in the XZ plane, y = 0).
80 auto v0 = mesh->add_vertex(vec3(-1.0, 0.0, 0.0));
81 auto v1 = mesh->add_vertex(vec3(0.0, 0.0, 1.0));
82 auto v2 = mesh->add_vertex(vec3(1.0, 0.0, 0.0));
83 auto v3 = mesh->add_vertex(vec3(0.0, 0.0, -1.0));
84
85 // Add the single tetrahedral cell bounded by the four vertices. This also creates its faces.
86 mesh->add_tetra(v0, v1, v2, v3);
87
88 return mesh;
89}
90
91int main(int argc, char **argv) {
92 // Bootstrap Easy3D (logging, resource directory, ...). Must come first.
93 initialize();
94
95 // Create the mesh (the tetrahedron from the previous tutorial).
96 PolyMesh *mesh = old_mesh_from_previous_example();
97
98 // Create a per-face property named "f:center" holding one vec3 per face. The "f:" prefix marks
99 // it as a face property; add_face_property<T>() returns a typed accessor indexed by face handle.
100 PolyMesh::FaceProperty<vec3> center = mesh->add_face_property<vec3>("f:center");
101
102 // For each face, compute its centroid: the average of its corner positions.
103 for (auto f : mesh->faces()) {
104 vec3 c(0, 0, 0);
105 // vertices(f) yields the corner vertices of face f; position(v) gives each corner's xyz.
106 for (auto v : mesh->vertices(f))
107 c += mesh->position(v);
108 center[f] = c / mesh->vertices(f).size(); // divide by the number of corners to get the mean
109 }
110
111 // Read the property back: print the centroid stored for each face.
112 for (auto f : mesh->faces()) {
113 std::cout << "center of " << f << ": " << center[f] << std::endl;
114 }
115
116 // Delete the mesh (i.e., release memory).
117 delete mesh;
118
119 return EXIT_SUCCESS;
120}
Face property of type T.
Definition poly_mesh.h:423
Data structure representing a polyhedral mesh.
Definition poly_mesh.h:50
Definition collider.cpp:182
See also
PolyMesh, Tutorial_112_PolyMesh, Tutorial_113_PolyMesh_Connectivity, Tutorial_106_SurfaceMesh_Property