Merge pull request #2117 from schuttejoe/master

Fix expensive memory allocation and memory copying occurring in obj…
pull/2122/head
Kim Kulling 2018-09-04 18:00:11 +02:00 committed by GitHub
commit ffda754d97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 22 deletions

View File

@ -211,12 +211,29 @@ void ObjFileImporter::CreateDataFromImport(const ObjFile::Model* pModel, aiScene
} }
if (pModel->m_Objects.size() > 0) { if (pModel->m_Objects.size() > 0) {
unsigned int meshCount = 0;
unsigned int childCount = 0;
for(size_t index = 0; index < pModel->m_Objects.size(); ++index) {
if(pModel->m_Objects[index]) {
++childCount;
meshCount += (unsigned int)pModel->m_Objects[index]->m_Meshes.size();
}
}
// Allocate space for the child nodes on the root node
pScene->mRootNode->mChildren = new aiNode*[ childCount ];
// Create nodes for the whole scene // Create nodes for the whole scene
std::vector<aiMesh*> MeshArray; std::vector<aiMesh*> MeshArray;
MeshArray.reserve(meshCount);
for (size_t index = 0; index < pModel->m_Objects.size(); ++index) { for (size_t index = 0; index < pModel->m_Objects.size(); ++index) {
createNodes(pModel, pModel->m_Objects[index], pScene->mRootNode, pScene, MeshArray); createNodes(pModel, pModel->m_Objects[index], pScene->mRootNode, pScene, MeshArray);
} }
ai_assert(pScene->mRootNode->mNumChildren == childCount);
// Create mesh pointer buffer for this scene // Create mesh pointer buffer for this scene
if (pScene->mNumMeshes > 0) { if (pScene->mNumMeshes > 0) {
pScene->mMeshes = new aiMesh*[MeshArray.size()]; pScene->mMeshes = new aiMesh*[MeshArray.size()];
@ -287,9 +304,8 @@ aiNode *ObjFileImporter::createNodes(const ObjFile::Model* pModel, const ObjFile
pNode->mName = pObject->m_strObjName; pNode->mName = pObject->m_strObjName;
// If we have a parent node, store it // If we have a parent node, store it
if( pParent != NULL ) { ai_assert( NULL != pParent );
appendChildToParentNode( pParent, pNode ); appendChildToParentNode( pParent, pNode );
}
for ( size_t i=0; i< pObject->m_Meshes.size(); ++i ) { for ( size_t i=0; i< pObject->m_Meshes.size(); ++i ) {
unsigned int meshId = pObject->m_Meshes[ i ]; unsigned int meshId = pObject->m_Meshes[ i ];
@ -442,8 +458,8 @@ void ObjFileImporter::createVertexArray(const ObjFile::Model* pModel,
pMesh->mNumVertices = numIndices; pMesh->mNumVertices = numIndices;
if (pMesh->mNumVertices == 0) { if (pMesh->mNumVertices == 0) {
throw DeadlyImportError( "OBJ: no vertices" ); throw DeadlyImportError( "OBJ: no vertices" );
} else if (pMesh->mNumVertices > AI_MAX_ALLOC(aiVector3D)) { } else if (pMesh->mNumVertices > AI_MAX_VERTICES) {
throw DeadlyImportError( "OBJ: Too many vertices, would run out of memory" ); throw DeadlyImportError( "OBJ: Too many vertices" );
} }
pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ]; pMesh->mVertices = new aiVector3D[ pMesh->mNumVertices ];
@ -770,25 +786,8 @@ void ObjFileImporter::appendChildToParentNode(aiNode *pParent, aiNode *pChild)
// Assign parent to child // Assign parent to child
pChild->mParent = pParent; pChild->mParent = pParent;
// If already children was assigned to the parent node, store them in a
std::vector<aiNode*> temp;
if (pParent->mChildren != NULL)
{
ai_assert( 0 != pParent->mNumChildren );
for (size_t index = 0; index < pParent->mNumChildren; index++)
{
temp.push_back(pParent->mChildren [ index ] );
}
delete [] pParent->mChildren;
}
// Copy node instances into parent node // Copy node instances into parent node
pParent->mNumChildren++; pParent->mNumChildren++;
pParent->mChildren = new aiNode*[ pParent->mNumChildren ];
for (size_t index = 0; index < pParent->mNumChildren-1; index++)
{
pParent->mChildren[ index ] = temp [ index ];
}
pParent->mChildren[ pParent->mNumChildren-1 ] = pChild; pParent->mChildren[ pParent->mNumChildren-1 ] = pChild;
} }