OpenMesh
Loading...
Searching...
No Matches
Using (custom) properties

This examples shows:

  • How to add and remove custom properties,
  • How to get and set the value of a custom property

In the last example we computed the barycenter of each vertex' neighborhood and stored it in an array. It would be more convenient and less error-prone if we could store this data in the mesh and let OpenMesh manage the data. It would be even more helpful if we could attach such properties dynamically to the mesh.

OpenMesh provides dynamic properties, which can be attached to each mesh entity (vertex, face, edge, halfedge, and the mesh itself). We distinguish between custom and standard properties. A custom property is any user-defined property and is accessed via the member function property(..) via a handle and an entity handle (e.g. VertexHandle). Whereas the standard properties are accessed via special member functions, e.g. the vertex position is accessed with point(..) and a vertex handle.

In this example we will store the cog-value (see previous example) in an additional vertex property instead of keeping it in a separate array. To do so we define first a so-called property handle with the desired type (MyMesh::Point) and register the handle at the mesh:

// this vertex property stores the computed centers of gravity
mesh.add_property(cogs);
Handle representing a vertex property.
Definition Property.hh:488


The mesh allocates enough memory to hold as many elements of type MyMesh::Point as number of vertices exist, and of course the mesh synchronizes all insert and delete operations on the vertices with the vertex properties.

Once the wanted property is registered we can use the property to calculate the barycenter of the neighborhood of each vertex v_it

for (vv_it=mesh.vv_iter( *v_it ); vv_it; ++vv_it)
{
mesh.property(cogs,*v_it) += mesh.point( *vv_it );
++valence;
}
mesh.property(cogs,*v_it) /= valence;


and finally set the new position for each vertex v_it

mesh.set_point( *v_it, mesh.property(cogs,*v_it) );


Below is the complete source code:

#include <iostream>
#include <vector>
// --------------------
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
int main(int argc, char **argv)
{
MyMesh mesh;
// check command line options
if (argc != 4)
{
std::cerr << "Usage: " << argv[0] << " #iterations infile outfile\n";
return 1;
}
// read mesh from stdin
if ( ! OpenMesh::IO::read_mesh(mesh, argv[2]) )
{
std::cerr << "Error: Cannot read mesh from " << argv[2] << std::endl;
return 1;
}
// this vertex property stores the computed centers of gravity
mesh.add_property(cogs);
// smoothing mesh argv[1] times
MyMesh::VertexIter v_it, v_end(mesh.vertices_end());
MyMesh::Scalar valence;
unsigned int i, N(atoi(argv[1]));
for (i=0; i < N; ++i)
{
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
{
mesh.property(cogs,*v_it).vectorize(0.0f);
valence = 0.0;
for (vv_it=mesh.vv_iter( *v_it ); vv_it; ++vv_it)
{
mesh.property(cogs,*v_it) += mesh.point( *vv_it );
++valence;
}
mesh.property(cogs,*v_it) /= valence;
}
for (v_it=mesh.vertices_begin(); v_it!=v_end; ++v_it)
if ( !mesh.is_boundary( *v_it ) )
mesh.set_point( *v_it, mesh.property(cogs,*v_it) );
}
// write mesh to stdout
if ( ! OpenMesh::IO::write_mesh(mesh, argv[3]) )
{
std::cerr << "Error: cannot write mesh to " << argv[3] << std::endl;
return 1;
}
return 0;
}
bool write_mesh(const Mesh &_mesh, const std::string &_filename, Options _opt=Options::Default, std::streamsize _precision=6)
Write a mesh to the file _filename.
Definition MeshIO.hh:199
bool read_mesh(Mesh &_mesh, const std::string &_filename)
Read a mesh from file _filename.
Definition MeshIO.hh:104
AttribKernel::Scalar Scalar
Definition PolyMeshT.hh:113
AttribKernel::VertexVertexIter VertexVertexIter
Definition PolyMeshT.hh:165
AttribKernel::Point Point
Definition PolyMeshT.hh:115
AttribKernel::VertexIter VertexIter
Definition PolyMeshT.hh:146
Triangle mesh based on the ArrayKernel.
Definition TriMesh_ArrayKernelT.hh:100

Project OpenMesh, ©  Computer Graphics Group, RWTH Aachen. Documentation generated using doxygen .