Commit 53371f63 authored by Matthias Möller's avatar Matthias Möller

- fix crash, if obj file contains degenerated faces

- added unittest for this case

closes #2382

git-svn-id: http://www.openmesh.org/svnrepo/OpenMesh/trunk@1236 fdac6126-5c0c-442c-9429-916003d36597
parent bf21ed8a
...@@ -64,6 +64,10 @@ using std::isspace; ...@@ -64,6 +64,10 @@ using std::isspace;
#include <string.h> #include <string.h>
#endif #endif
#include <set>
#include <algorithm>
#include <functional>
//=== NAMESPACES ============================================================== //=== NAMESPACES ==============================================================
...@@ -94,6 +98,28 @@ void trimString( std::string& _string) { ...@@ -94,6 +98,28 @@ void trimString( std::string& _string) {
_string = _string.substr( start, end-start+1 ); _string = _string.substr( start, end-start+1 );
} }
//-----------------------------------------------------------------------------
template<typename Handle>
class HasSeen : public std::unary_function <Handle, bool>
{
public:
HasSeen () : seen_ () { }
bool operator ()(const Handle& i) const
{
return (!seen_.insert(i.idx()).second);
}
private:
mutable std::set<int> seen_;
};
// remove duplicated indices from one face
void remove_duplicated_vertices(BaseImporter::VHandles& _indices)
{
_indices.erase(std::remove_if(_indices.begin(),_indices.end(),HasSeen<BaseImporter::VHandles::value_type>()),_indices.end());
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
_OBJReader_:: _OBJReader_::
...@@ -575,7 +601,11 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt) ...@@ -575,7 +601,11 @@ read(std::istream& _in, BaseImporter& _bi, Options& _opt)
// note that add_face can possibly triangulate the faces, which is why we have to // note that add_face can possibly triangulate the faces, which is why we have to
// store the current number of faces first // store the current number of faces first
size_t n_faces = _bi.n_faces(); size_t n_faces = _bi.n_faces();
fh = _bi.add_face(faceVertices); remove_duplicated_vertices(faceVertices);
//A minimum of three vertices are required.
if (faceVertices.size() > 2)
fh = _bi.add_face(faceVertices);
if (!vhandles.empty() && fh.is_valid() ) if (!vhandles.empty() && fh.is_valid() )
_bi.add_face_texcoords(fh, vhandles[0], face_texcoords); _bi.add_face_texcoords(fh, vhandles[0], face_texcoords);
......
g cube
v 0.0 0.0 0.0
v 0.0 0.0 1.0
v 0.0 1.0 0.0
v 0.0 1.0 1.0
v 1.0 0.0 0.0
v 1.0 0.0 1.0
v 1.0 1.0 0.0
v 1.0 1.0 1.0
vn 0.0 0.0 1.0
vn 0.0 0.0 -1.0
vn 0.0 1.0 0.0
vn 0.0 -1.0 0.0
vn 1.0 0.0 0.0
vn -1.0 0.0 0.0
f 1//2 7//2 5//2
f 1//2 3//2 7//2
f 1//6 4//6 3//6
f 1//6 2//6 4//6
f 3//3 8//3 7//3
f 3//3 4//3 8//3
f 5//5 7//5 8//5
f 5//5 8//5 6//5
f 1//4 5//4 6//4
f 1//4 6//4 2//4
f 2//1 6//1 8//1 8//1 2//1
f 2//1 8//1 4//1
f 2//1 2//1 2//1
f 2//1 8//1 2//1
...@@ -47,6 +47,22 @@ TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJ) { ...@@ -47,6 +47,22 @@ TEST_F(OpenMeshReadWriteOBJ, LoadSimpleOBJ) {
EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!"; EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
} }
/*
* Just load a obj file of a cube with degenerated faces
*/
TEST_F(OpenMeshReadWriteOBJ, LoadDegeneratedOBJ) {
mesh_.clear();
bool ok = OpenMesh::IO::read_mesh(mesh_, "cube-minimal.obj");
EXPECT_TRUE(ok) << "Unable to load cube-minimal.obj";
EXPECT_EQ(8u , mesh_.n_vertices()) << "The number of loaded vertices is not correct!";
EXPECT_EQ(18u , mesh_.n_edges()) << "The number of loaded edges is not correct!";
EXPECT_EQ(12u , mesh_.n_faces()) << "The number of loaded faces is not correct!";
}
/* /*
* Just load a obj file of a cube and checks the halfedge and vertex normals * Just load a obj file of a cube and checks the halfedge and vertex normals
*/ */
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment