This small example shows how to remove faces and vertices from a mesh.
We basically use the geometry created in First Steps - Building a cube.
If we want our mesh class to be able to remove vertices, faces or edges we have to extend the default traits for our mesh class. Vertices, faces and (half-)edges need the OpenMesh::Attributes::Status attribute which is used to hold the flag "deleted" if an element is deleted.
Instead of defining the required attributes via traits on compile-time, it is possible to request attributes dynamically on run-time by requesting them. In this example, we want to delete faces, edges and vertices, therefore requesting the status attribute is required.
- Note
- You have to request attributes before you use them, if you don't enable them via traits.
mesh.request_face_status();
mesh.request_edge_status();
mesh.request_vertex_status();
After having created the geometry of the cube one can delete faces and vertices by simply calling delete_vertices() (delete_faces() or delete_edges() respectively).
Note that there is actually no way to directly delete halfedges since they are automatically affected when the parent edge is marked as deleted!
The status whether an element is marked as deleted can be requested by
mesh.status(handle).deleted();
where handle is either a vertex-, edge- or face-handle.
In this example we delete all faces except one and the corresponding vertices. The code looks like this
mesh.delete_face(fhandle[0], false);
mesh.delete_face(fhandle[2], false);
mesh.delete_face(fhandle[3], false);
mesh.delete_face(fhandle[4], false);
mesh.delete_face(fhandle[5], false);
mesh.delete_vertex(vhandle[0], false);
mesh.delete_vertex(vhandle[1], false);
mesh.delete_vertex(vhandle[2], false);
mesh.delete_vertex(vhandle[3], false);
Now the deleted faces and vertices are marked as "deleted" internally. Call garbage_collection() to definitely remove them from memory.
mesh.garbage_collection();
The full source code of the example:
#include <iostream>
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh>
#include <OpenMesh/Core/System/config.h>
#include <OpenMesh/Core/Mesh/Status.hh>
{
};
int main()
{
mesh.request_face_status();
mesh.request_edge_status();
mesh.request_vertex_status();
std::vector<MyMesh::VertexHandle> tmp_face_vhandles;
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[0]);
tmp_face_vhandles.push_back(vhandle[1]);
tmp_face_vhandles.push_back(vhandle[2]);
tmp_face_vhandles.push_back(vhandle[3]);
fhandle[0] = mesh.add_face(tmp_face_vhandles);
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[7]);
tmp_face_vhandles.push_back(vhandle[6]);
tmp_face_vhandles.push_back(vhandle[5]);
tmp_face_vhandles.push_back(vhandle[4]);
fhandle[1] = mesh.add_face(tmp_face_vhandles);
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[1]);
tmp_face_vhandles.push_back(vhandle[0]);
tmp_face_vhandles.push_back(vhandle[4]);
tmp_face_vhandles.push_back(vhandle[5]);
fhandle[2] = mesh.add_face(tmp_face_vhandles);
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[2]);
tmp_face_vhandles.push_back(vhandle[1]);
tmp_face_vhandles.push_back(vhandle[5]);
tmp_face_vhandles.push_back(vhandle[6]);
fhandle[3] = mesh.add_face(tmp_face_vhandles);
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[3]);
tmp_face_vhandles.push_back(vhandle[2]);
tmp_face_vhandles.push_back(vhandle[6]);
tmp_face_vhandles.push_back(vhandle[7]);
fhandle[4] = mesh.add_face(tmp_face_vhandles);
tmp_face_vhandles.clear();
tmp_face_vhandles.push_back(vhandle[0]);
tmp_face_vhandles.push_back(vhandle[3]);
tmp_face_vhandles.push_back(vhandle[7]);
tmp_face_vhandles.push_back(vhandle[4]);
fhandle[5] = mesh.add_face(tmp_face_vhandles);
mesh.delete_face(fhandle[0], false);
mesh.delete_face(fhandle[2], false);
mesh.delete_face(fhandle[3], false);
mesh.delete_face(fhandle[4], false);
mesh.delete_face(fhandle[5], false);
mesh.delete_vertex(vhandle[0], false);
mesh.delete_vertex(vhandle[1], false);
mesh.delete_vertex(vhandle[2], false);
mesh.delete_vertex(vhandle[3], false);
mesh.garbage_collection();
try {
std::cerr << "Cannot write mesh to file 'output.off'" << std::endl;
return 1;
}
}
catch( std::exception& x )
{
std::cerr << x.what() << 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:190
Polygonal mesh based on the ArrayKernel.
Definition: PolyMesh_ArrayKernelT.hh:96
Kernel::VertexHandle VertexHandle
Handle for referencing the corresponding item.
Definition: PolyMeshT.hh:136
SmartVertexHandle add_vertex(const Point _p)
Alias for new_vertex(const Point&).
Definition: PolyMeshT.hh:238
Kernel::FaceHandle FaceHandle
Scalar type.
Definition: PolyMeshT.hh:139
Kernel::Point Point
Coordinate type.
Definition: PolyMeshT.hh:112
Base class for all traits.
Definition: Traits.hh:122