- 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
This commit is contained in:
Matthias Möller
2015-03-09 09:56:41 +00:00
parent bf21ed8ad0
commit 53371f63e1
3 changed files with 76 additions and 1 deletions

View File

@@ -64,6 +64,10 @@ using std::isspace;
#include <string.h>
#endif
#include <set>
#include <algorithm>
#include <functional>
//=== NAMESPACES ==============================================================
@@ -94,6 +98,28 @@ void trimString( std::string& _string) {
_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_::
@@ -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
// store the current number of faces first
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() )
_bi.add_face_texcoords(fh, vhandles[0], face_texcoords);