Răsfoiți Sursa

Merge pull request #675 from turol/master

Fix OFFLoader to better handle certain malformed files
Kim Kulling 10 ani în urmă
părinte
comite
f1279f8f45
1 a modificat fișierele cu 29 adăugiri și 11 ștergeri
  1. 29 11
      code/OFFLoader.cpp

+ 29 - 11
code/OFFLoader.cpp

@@ -138,9 +138,15 @@ void OFFImporter::InternReadFile( const std::string& pFile,
         throw DeadlyImportError("OFF: There are no valid faces");
     }
 
-    pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes = 1 ];
-    aiMesh* mesh = pScene->mMeshes[0] = new aiMesh();
-    aiFace* faces = mesh->mFaces = new aiFace [mesh->mNumFaces = numFaces];
+    pScene->mNumMeshes = 1;
+    pScene->mMeshes = new aiMesh*[ pScene->mNumMeshes ];
+
+    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);
 
@@ -171,7 +177,8 @@ void OFFImporter::InternReadFile( const std::string& pFile,
             break;
         }
         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");
             --mesh->mNumFaces;
@@ -185,43 +192,54 @@ void OFFImporter::InternReadFile( const std::string& pFile,
         throw DeadlyImportError("OFF: There are no valid faces");
 
     // 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
-    buffer = old;faces = mesh->mFaces;
+    buffer = old;
+    faces = mesh->mFaces;
     for (unsigned int i = 0, p = 0; i< mesh->mNumFaces;)
     {
         if(!GetNextLine(buffer,line))break;
 
         unsigned int idx;
         sz = line;SkipSpaces(&sz);
-        if(!(idx = strtoul10(sz,&sz)) || idx > 9)
+        idx = strtoul10(sz,&sz);
+        if(!(idx) || idx > 9)
             continue;
 
         faces->mIndices = new unsigned int [faces->mNumIndices];
         for (unsigned int m = 0; m < faces->mNumIndices;++m)
         {
             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");
                 idx = numVertices-1;
             }
             faces->mIndices[m] = p++;
-            *verts++ = tempPositions[idx];
+            verts.push_back(tempPositions[idx]);
         }
         ++i;
         ++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
     pScene->mRootNode = new aiNode();
     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;
 
     // generate a default material
-    pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials = 1];
+    pScene->mNumMaterials = 1;
+    pScene->mMaterials = new aiMaterial*[pScene->mNumMaterials];
     aiMaterial* pcMat = new aiMaterial();
 
     aiColor4D clr(0.6f,0.6f,0.6f,1.0f);