Merge pull request #675 from turol/master

Fix OFFLoader to better handle certain malformed files
pull/676/head
Kim Kulling 2015-10-28 20:10:20 +01:00
commit f1279f8f45
1 changed files with 29 additions and 11 deletions

View File

@ -138,9 +138,15 @@ void OFFImporter::InternReadFile( const std::string& pFile,
throw DeadlyImportError("OFF: There are no valid faces"); throw DeadlyImportError("OFF: There are no valid faces");
} }
pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = 1 ]; pScene->mNumMeshes = 1;
aiMesh* mesh = pScene->mMeshes[0] = new aiMesh(); pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
aiFace* faces = mesh->mFaces = new aiFace [mesh->mNumFaces = numFaces];
aiMesh* mesh = new aiMesh();
pScene->mMeshes[0] = mesh;
mesh->mNumFaces = numFaces;
aiFace* faces = new aiFace [mesh->mNumFaces];
mesh->mFaces = faces;
std::vector<aiVector3D> tempPositions(numVertices); std::vector<aiVector3D> tempPositions(numVertices);
@ -171,7 +177,8 @@ void OFFImporter::InternReadFile( const std::string& pFile,
break; break;
} }
sz = line;SkipSpaces(&sz); sz = line;SkipSpaces(&sz);
if(!(faces->mNumIndices = strtoul10(sz,&sz)) || faces->mNumIndices > 9) faces->mNumIndices = strtoul10(sz,&sz);
if(!(faces->mNumIndices) || faces->mNumIndices > 9)
{ {
DefaultLogger::get()->error("OFF: Faces with zero indices aren't allowed"); DefaultLogger::get()->error("OFF: Faces with zero indices aren't allowed");
--mesh->mNumFaces; --mesh->mNumFaces;
@ -185,43 +192,54 @@ void OFFImporter::InternReadFile( const std::string& pFile,
throw DeadlyImportError("OFF: There are no valid faces"); throw DeadlyImportError("OFF: There are no valid faces");
// allocate storage for the output vertices // allocate storage for the output vertices
aiVector3D* verts = mesh->mVertices = new aiVector3D[mesh->mNumVertices]; std::vector<aiVector3D> verts;
verts.reserve(mesh->mNumVertices);
// second: now parse all face indices // second: now parse all face indices
buffer = old;faces = mesh->mFaces; buffer = old;
faces = mesh->mFaces;
for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;) for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;)
{ {
if(!GetNextLine(buffer,line))break; if(!GetNextLine(buffer,line))break;
unsigned int idx; unsigned int idx;
sz = line;SkipSpaces(&sz); sz = line;SkipSpaces(&sz);
if(!(idx = strtoul10(sz,&sz)) || idx > 9) idx = strtoul10(sz,&sz);
if(!(idx) || idx > 9)
continue; continue;
faces->mIndices = new unsigned int [faces->mNumIndices]; faces->mIndices = new unsigned int [faces->mNumIndices];
for (unsigned int m = 0; m < faces->mNumIndices;++m) for (unsigned int m = 0; m < faces->mNumIndices;++m)
{ {
SkipSpaces(&sz); SkipSpaces(&sz);
if ((idx = strtoul10(sz,&sz)) >= numVertices) idx = strtoul10(sz,&sz);
if ((idx) >= numVertices)
{ {
DefaultLogger::get()->error("OFF: Vertex index is out of range"); DefaultLogger::get()->error("OFF: Vertex index is out of range");
idx = numVertices-1; idx = numVertices-1;
} }
faces->mIndices[m] = p++; faces->mIndices[m] = p++;
*verts++ = tempPositions[idx]; verts.push_back(tempPositions[idx]);
} }
++i; ++i;
++faces; ++faces;
} }
if (mesh->mNumVertices != verts.size()) {
throw DeadlyImportError("OFF: Vertex count mismatch");
}
mesh->mVertices = new aiVector3D[verts.size()];
memcpy(mesh->mVertices, &verts[0], verts.size() * sizeof(aiVector3D));
// generate the output node graph // generate the output node graph
pScene->mRootNode = new aiNode(); pScene->mRootNode = new aiNode();
pScene->mRootNode->mName.Set("<OFFRoot>"); pScene->mRootNode->mName.Set("<OFFRoot>");
pScene->mRootNode->mMeshes = new unsigned int [pScene->mRootNode->mNumMeshes = 1]; pScene->mRootNode->mNumMeshes = 1;
pScene->mRootNode->mMeshes = new unsigned int [pScene->mRootNode->mNumMeshes];
pScene->mRootNode->mMeshes[0] = 0; pScene->mRootNode->mMeshes[0] = 0;
// generate a default material // generate a default material
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = 1]; pScene->mNumMaterials = 1;
pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
aiMaterial* pcMat = new aiMaterial(); aiMaterial* pcMat = new aiMaterial();
aiColor4D clr(0.6f,0.6f,0.6f,1.0f); aiColor4D clr(0.6f,0.6f,0.6f,1.0f);