I've recently started using OpenMesh on a project where I have to refine meshes. I need to use the face split(FaceHandle _fh, Point _p)
operation to insert a vertex at a triangle's centroid.
But when I use this method and try to grab the faces of the newly created vertex using a VertexFaceIterator, I always get invalid faces with vertex indexes such as (87, 87, -1) or (12, 12, -1). It seems as though OpenMesh isn't updating the meshes topology after a split.
My code looks something like this. faceStartIt
is what gives me these weird indexes.
typedef OpenMesh::TriMesh_ArrayKernelT<> TriMesh;
TriMesh::FaceIter triangleIt = mesh.faces_begin();
for( ; triangleIt != mesh.faces_end(); )
{
TriMesh::Point centroid = mesh.calc_face_centroid( *triangleIt );
if( hasToSplit( centroid ) )
{
TriMesh::VertexHandle centroidHandle = mesh.split( *triangleIt, centroid );
TriMesh::VertexFaceIter faceStartIt = mesh.vf_begin( centroidHandle );
TriMesh::VertexFaceIter faceEndIt = mesh.vf_end( centroidHandle );
TriMesh::VertexFaceIter faceIt = faceStartIt; //faceIt++;
for( ; faceIt != faceEndIt; ++faceIt )
{
// Do something for each face
}
}
else
{
++triangleIt;
}
}
I figured out the problem I was having. The split
method works fine, what I was doing wrong was inside the // Do something for each face
commentary. For each face I was doing edge flip operations, but references were being lost because a flip causes changes on later faces being evaluated. The solution was to in one iteration, after splitting a face, insert each edge I wanted flipped in a std::set
to guarantee they were unique. Then in another iteration do the actual flips on marked edges.