I've been following the OpenMesh tutorial First Steps - Building a Cube with a few modifications, I'm using a TriMesh instead of a PolyMesh and am building a pyramid instead of a cube.
Somehow, I'm getting the error PolymeshT::add_face:complex edge
for my second and third faces. These faces should be between the points (0,0,0), (0,1,0) and (0,0,1) and the points (0,0,0), (0,0,1), and (1,0,0).
Two edges already exist when each face is constructed (0,0,0) to (0,1,0) and (0,0,0) to (0,0,1), but I should be able to create faces where some of the edges are already existing, shouldn't I?
Solutions I've tried so far
I can't spot anything else that I'm doing differently than the tutorial.
#include <OpenMesh/Core/IO/MeshIO.hh>
#include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh>
typedef OpenMesh::TriMesh_ArrayKernelT<> MyTriMesh;
// Make a pyramid
int main()
{
MyTriMesh tin;
// generate vertices
MyTriMesh::VertexHandle vhandle[4];
vhandle[0] = tin.add_vertex(MyTriMesh::Point(0, 0, 0));
vhandle[1] = tin.add_vertex(MyTriMesh::Point(0, 1, 0));
vhandle[2] = tin.add_vertex(MyTriMesh::Point(1, 0, 0));
vhandle[3] = tin.add_vertex(MyTriMesh::Point(0, 0, 1));
// generate (trianglar) faces
std::vector<MyTriMesh::VertexHandle> face_vhandles;
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[2]);
tin.add_face(face_vhandles);
printf("Vertices: %u\nEdges: %u\nTriangles: %u\n",
tin.n_vertices(), tin.n_edges(), tin.n_faces());
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[3]);
tin.add_face(face_vhandles);
printf("Vertices: %u\nEdges: %u\nTriangles: %u\n",
tin.n_vertices(), tin.n_edges(), tin.n_faces());
face_vhandles.clear();
face_vhandles.push_back(vhandle[0]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
tin.add_face(face_vhandles);
printf("Vertices: %u\nEdges: %u\nTriangles: %u\n",
tin.n_vertices(), tin.n_edges(), tin.n_faces());
face_vhandles.clear();
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[2]);
tin.add_face(face_vhandles);
printf("Vertices: %u\nEdges: %u\nTriangles: %u\n",
tin.n_vertices(), tin.n_edges(), tin.n_faces());
}
This error results from some of the faces' vertices being added in the wrong order.
OpenMesh uses a halfedge structure for describing the 3d structure of the mesh. Halfedges are directional edges between vertices. This allows the vertices on a face to be transversed by following the halfedges belonging to the face. However, for this reason the order in which vertices are added to a face is very important.
Typically, vertices should always be added in a counter-clockwise order. This results in the halfedges of adjacent faces pointing in opposite directions as in the picture below left. If the ordering of the vertices is not consistant, there is ambiguity as to which edge follows the halfedge, the bottom edge of 'A' or the bottom edge of 'B', as in the picture below right.
In the code from the question, faces 1 and 4 are ordered counter-clockwise and faces 2 and 3 are ordered clockwise. The simple fix is to switch the first and third vertices for these two faces.
face_vhandles.clear();
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[1]);
face_vhandles.push_back(vhandle[0]);
tin.add_face(face_vhandles);
face_vhandles.clear();
face_vhandles.push_back(vhandle[2]);
face_vhandles.push_back(vhandle[3]);
face_vhandles.push_back(vhandle[0]);
tin.add_face(face_vhandles);