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
- PolyMesh: the polyhedral mesh class. add_vertex() adds a corner; add_tetra(v0,v1,v2,v3) adds a tetrahedral cell from four vertices.
- PolyMesh::add_face_property<T>(name): creates a per-face property of type T and returns a FaceProperty<T> accessor; values are read/written with center[f].
- PolyMesh::faces(): a range of all face handles. PolyMesh::vertices(f): the corner vertices of face f. PolyMesh::position(v): the 3D coordinates of vertex v.
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.
27#include <easy3d/core/poly_mesh.h>
28#include <easy3d/util/initializer.h>
75PolyMesh *old_mesh_from_previous_example() {
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));
86 mesh->add_tetra(v0, v1, v2, v3);
91int main(
int argc,
char **argv) {
96 PolyMesh *mesh = old_mesh_from_previous_example();
103 for (
auto f : mesh->faces()) {
106 for (
auto v : mesh->vertices(f))
107 c += mesh->position(v);
108 center[f] = c / mesh->vertices(f).size();
112 for (
auto f : mesh->faces()) {
113 std::cout <<
"center of " << f <<
": " << center[f] << std::endl;
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