From 52c017b595af7f65d75b79bb1d4eeeb5acc19580 Mon Sep 17 00:00:00 2001 From: Max Vollmer Date: Mon, 25 Jun 2018 12:05:37 +0200 Subject: [PATCH 1/3] Added check to BaseImporter::SearchFileHeaderForToken making sure that a detected token is not in fact just a fraction of a longer token. Microsoft exported binary gltf files were detected as OBJ, because the "gltf " in the string "Microsoft GLTF Exporter 2.4.1.7" was detected as the token "f ". I added a new bool parameter to the method enabling this check. It's default false, and only ObjFileImporter sets it to true, so no other code should be affected. --- code/BaseImporter.cpp | 8 +++++++- code/ObjFileImporter.cpp | 2 +- include/assimp/BaseImporter.h | 3 ++- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/code/BaseImporter.cpp b/code/BaseImporter.cpp index 78979d078..c7d10865e 100644 --- a/code/BaseImporter.cpp +++ b/code/BaseImporter.cpp @@ -143,7 +143,8 @@ void BaseImporter::GetExtensionList(std::set& extensions) { const char** tokens, unsigned int numTokens, unsigned int searchBytes /* = 200 */, - bool tokensSol /* false */) + bool tokensSol /* false */, + bool noAlphaBeforeTokens /* false */) { ai_assert( nullptr != tokens ); ai_assert( 0 != numTokens ); @@ -193,6 +194,11 @@ void BaseImporter::GetExtensionList(std::set& extensions) { if( !r ) { continue; } + // We need to make sure that we didn't accidentially identify the end of another token as our token, + // e.g. in a previous version the "gltf " present in some gltf files was detected as "f " + if (noAlphaBeforeTokens && (r != buffer && isalpha(r[-1]))) { + continue; + } // We got a match, either we don't care where it is, or it happens to // be in the beginning of the file / line if (!tokensSol || r == buffer || r[-1] == '\r' || r[-1] == '\n') { diff --git a/code/ObjFileImporter.cpp b/code/ObjFileImporter.cpp index cbf2363a4..64eac6790 100644 --- a/code/ObjFileImporter.cpp +++ b/code/ObjFileImporter.cpp @@ -100,7 +100,7 @@ bool ObjFileImporter::CanRead( const std::string& pFile, IOSystem* pIOHandler , } else { // Check file Header static const char *pTokens[] = { "mtllib", "usemtl", "v ", "vt ", "vn ", "o ", "g ", "s ", "f " }; - return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9 ); + return BaseImporter::SearchFileHeaderForToken(pIOHandler, pFile, pTokens, 9, 200, false, true ); } } diff --git a/include/assimp/BaseImporter.h b/include/assimp/BaseImporter.h index 0c0fd110b..7546f9912 100644 --- a/include/assimp/BaseImporter.h +++ b/include/assimp/BaseImporter.h @@ -244,7 +244,8 @@ public: // static utilities const char** tokens, unsigned int numTokens, unsigned int searchBytes = 200, - bool tokensSol = false); + bool tokensSol = false, + bool noAlphaBeforeTokens = false); // ------------------------------------------------------------------- /** @brief Check whether a file has a specific file extension From 3c5c0a0df38161ed77903ffc092c8007c63ddabb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B6ber?= Date: Fri, 29 Jun 2018 09:55:49 +0200 Subject: [PATCH 2/3] Deactivate area based rejection of triangles in triangulation Use FindDegenerates post processing step for that. --- code/TriangulateProcess.cpp | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/code/TriangulateProcess.cpp b/code/TriangulateProcess.cpp index cc8b05c47..31239bb2d 100644 --- a/code/TriangulateProcess.cpp +++ b/code/TriangulateProcess.cpp @@ -485,21 +485,22 @@ bool TriangulateProcess::TriangulateMesh( aiMesh* pMesh) for(aiFace* f = last_face; f != curOut; ) { unsigned int* i = f->mIndices; - // drop dumb 0-area triangles - if (std::fabs(GetArea2D(temp_verts[i[0]],temp_verts[i[1]],temp_verts[i[2]])) < 1e-5f) { - ASSIMP_LOG_DEBUG("Dropping triangle with area 0"); - --curOut; + // drop dumb 0-area triangles - deactivated for now: + //FindDegenerates post processing step can do the same thing + //if (std::fabs(GetArea2D(temp_verts[i[0]],temp_verts[i[1]],temp_verts[i[2]])) < 1e-5f) { + // ASSIMP_LOG_DEBUG("Dropping triangle with area 0"); + // --curOut; - delete[] f->mIndices; - f->mIndices = NULL; + // delete[] f->mIndices; + // f->mIndices = nullptr; - for(aiFace* ff = f; ff != curOut; ++ff) { - ff->mNumIndices = (ff+1)->mNumIndices; - ff->mIndices = (ff+1)->mIndices; - (ff+1)->mIndices = NULL; - } - continue; - } + // for(aiFace* ff = f; ff != curOut; ++ff) { + // ff->mNumIndices = (ff+1)->mNumIndices; + // ff->mIndices = (ff+1)->mIndices; + // (ff+1)->mIndices = nullptr; + // } + // continue; + //} i[0] = idx[i[0]]; i[1] = idx[i[1]]; From 1feb0d1c60319110dc9364aa65f95e41e1e31487 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20L=C3=B6ber?= Date: Fri, 29 Jun 2018 15:47:41 +0200 Subject: [PATCH 3/3] Change order of PP steps to triangulate before searching for degenerates --- code/PostStepRegistry.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/code/PostStepRegistry.cpp b/code/PostStepRegistry.cpp index 2a5e211c1..6c25b9361 100644 --- a/code/PostStepRegistry.cpp +++ b/code/PostStepRegistry.cpp @@ -164,9 +164,6 @@ void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out) #if (!defined ASSIMP_BUILD_NO_OPTIMIZEGRAPH_PROCESS) out.push_back( new OptimizeGraphProcess()); #endif -#if (!defined ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS) - out.push_back( new FindDegeneratesProcess()); -#endif #ifndef ASSIMP_BUILD_NO_GENUVCOORDS_PROCESS out.push_back( new ComputeUVMappingProcess()); #endif @@ -179,6 +176,12 @@ void GetPostProcessingStepInstanceList(std::vector< BaseProcess* >& out) #if (!defined ASSIMP_BUILD_NO_TRIANGULATE_PROCESS) out.push_back( new TriangulateProcess()); #endif +#if (!defined ASSIMP_BUILD_NO_FINDDEGENERATES_PROCESS) + //find degenerates should run after triangulation (to sort out small + //generated triangles) but before sort by p types (in case there are lines + //and points generated and inserted into a mesh) + out.push_back( new FindDegeneratesProcess()); +#endif #if (!defined ASSIMP_BUILD_NO_SORTBYPTYPE_PROCESS) out.push_back( new SortByPTypeProcess()); #endif