From fcff12b219963af6012f948edee9f6200afa84f2 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 5 May 2019 13:39:10 +0200 Subject: [PATCH 1/4] closes https://github.com/assimp/assimp/issues/2054: make empty bone validation optional. --- code/ASELoader.cpp | 2 +- code/ASEParser.cpp | 2 +- code/ASEParser.h | 10 +++++----- code/FBXConverter.cpp | 21 ++++++++++----------- code/FBXConverter.h | 4 +++- code/FBXImportSettings.h | 34 ++++++++++++++++++++-------------- code/FBXImporter.cpp | 3 ++- code/ValidateDataStructure.cpp | 26 ++++++++++---------------- 8 files changed, 52 insertions(+), 50 deletions(-) diff --git a/code/ASELoader.cpp b/code/ASELoader.cpp index 321e8548a..556e8ce66 100644 --- a/code/ASELoader.cpp +++ b/code/ASELoader.cpp @@ -1299,7 +1299,7 @@ void ASEImporter::BuildMaterialIndices() } } - // Dekete our temporary array + // Delete our temporary array delete[] pcIntMaterials; } diff --git a/code/ASEParser.cpp b/code/ASEParser.cpp index e8d6febc2..f21f49eeb 100644 --- a/code/ASEParser.cpp +++ b/code/ASEParser.cpp @@ -45,7 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @brief Implementation of the ASE parser class */ - +#pragma once #ifndef ASSIMP_BUILD_NO_ASE_IMPORTER #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER diff --git a/code/ASEParser.h b/code/ASEParser.h index b8c820632..0cfd5313c 100644 --- a/code/ASEParser.h +++ b/code/ASEParser.h @@ -188,10 +188,11 @@ struct Animation { } mRotationType, mScalingType, mPositionType; Animation() AI_NO_EXCEPT - : mRotationType (TRACK) - , mScalingType (TRACK) - , mPositionType (TRACK) - {} + : mRotationType (TRACK) + , mScalingType (TRACK) + , mPositionType (TRACK) { + // empty + } //! List of track rotation keyframes std::vector< aiQuatKey > akeyRotations; @@ -243,7 +244,6 @@ struct BaseNode { mTargetPosition.x = qnan; } - //! Name of the mesh std::string mName; diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index 0abfdafdc..8d7c087ca 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -76,7 +76,7 @@ namespace Assimp { #define CONVERT_FBX_TIME(time) static_cast(time) / 46186158000L - FBXConverter::FBXConverter(aiScene* out, const Document& doc) + FBXConverter::FBXConverter(aiScene* out, const Document& doc, bool removeEmptyBones) : defaultMaterialIndex() , lights() , cameras() @@ -89,7 +89,8 @@ namespace Assimp { , mNodeNames() , anim_fps() , out(out) - , doc(doc) { + , doc(doc) + , mRemoveEmptyBones( removeEmptyBones ) { // animations need to be converted first since this will // populate the node_anim_chain_bits map, which is needed // to determine which nodes need to be generated. @@ -1407,9 +1408,9 @@ namespace Assimp { const WeightIndexArray& indices = cluster->GetIndices(); - if (indices.empty()) { + /*if (indices.empty()) { continue; - } + }*/ const MatIndexArray& mats = geo.GetMaterialIndices(); @@ -1439,13 +1440,11 @@ namespace Assimp { if (index_out_indices.back() == no_index_sentinel) { index_out_indices.back() = out_indices.size(); - } if (no_mat_check) { out_indices.push_back(out_idx[i]); - } - else { + } else { // this extra lookup is in O(logn), so the entire algorithm becomes O(nlogn) const std::vector::iterator it = std::lower_bound( outputVertStartIndices->begin(), @@ -1465,10 +1464,10 @@ namespace Assimp { // if we found at least one, generate the output bones // XXX this could be heavily simplified by collecting the bone // data in a single step. - if (ok) { + //if (ok) { ConvertCluster(bones, model, *cluster, out_indices, index_out_indices, count_out_indices, node_global_transform); - } + //} } } catch (std::exception&) { @@ -3525,9 +3524,9 @@ void FBXConverter::SetShadingPropertiesRaw(aiMaterial* out_mat, const PropertyTa } // ------------------------------------------------------------------------------------------------ - void ConvertToAssimpScene(aiScene* out, const Document& doc) + void ConvertToAssimpScene(aiScene* out, const Document& doc, bool removeEmptyBones) { - FBXConverter converter(out, doc); + FBXConverter converter(out, doc, removeEmptyBones); } } // !FBX diff --git a/code/FBXConverter.h b/code/FBXConverter.h index 50637468b..fe4e916be 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -112,7 +112,7 @@ public: }; public: - FBXConverter(aiScene* out, const Document& doc); + FBXConverter(aiScene* out, const Document& doc, bool removeEmptyBones ); ~FBXConverter(); private: @@ -453,6 +453,8 @@ private: aiScene* const out; const FBX::Document& doc; + + bool mRemoveEmptyBones; }; } diff --git a/code/FBXImportSettings.h b/code/FBXImportSettings.h index d5e1c2060..eaf127382 100644 --- a/code/FBXImportSettings.h +++ b/code/FBXImportSettings.h @@ -53,19 +53,21 @@ namespace FBX { struct ImportSettings { ImportSettings() - : strictMode(true) - , readAllLayers(true) - , readAllMaterials(false) - , readMaterials(true) - , readTextures(true) - , readCameras(true) - , readLights(true) - , readAnimations(true) - , readWeights(true) - , preservePivots(true) - , optimizeEmptyAnimationCurves(true) - , useLegacyEmbeddedTextureNaming(false) - {} + : strictMode(true) + , readAllLayers(true) + , readAllMaterials(false) + , readMaterials(true) + , readTextures(true) + , readCameras(true) + , readLights(true) + , readAnimations(true) + , readWeights(true) + , preservePivots(true) + , optimizeEmptyAnimationCurves(true) + , useLegacyEmbeddedTextureNaming(false) + , removeEmptyBones( true ) { + // empty + } /** enable strict mode: @@ -141,8 +143,12 @@ struct ImportSettings bool optimizeEmptyAnimationCurves; /** use legacy naming for embedded textures eg: (*0, *1, *2) - **/ + */ bool useLegacyEmbeddedTextureNaming; + + /** Empty bones shall be removed + */ + bool removeEmptyBones; }; diff --git a/code/FBXImporter.cpp b/code/FBXImporter.cpp index 2cc8bffc2..60a19223c 100644 --- a/code/FBXImporter.cpp +++ b/code/FBXImporter.cpp @@ -140,6 +140,7 @@ void FBXImporter::SetupProperties(const Importer* pImp) settings.preservePivots = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_PRESERVE_PIVOTS, true); settings.optimizeEmptyAnimationCurves = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_OPTIMIZE_EMPTY_ANIMATION_CURVES, true); settings.useLegacyEmbeddedTextureNaming = pImp->GetPropertyBool(AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING, false); + settings.removeEmptyBones = pImp->GetPropertyBool(AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES, true); } // ------------------------------------------------------------------------------------------------ @@ -184,7 +185,7 @@ void FBXImporter::InternReadFile( const std::string& pFile, aiScene* pScene, IOS Document doc(parser,settings); // convert the FBX DOM to aiScene - ConvertToAssimpScene(pScene,doc); + ConvertToAssimpScene(pScene,doc, settings.removeEmptyBones); std::for_each(tokens.begin(),tokens.end(),Util::delete_fun()); } diff --git a/code/ValidateDataStructure.cpp b/code/ValidateDataStructure.cpp index 657b0361b..58f700b0e 100644 --- a/code/ValidateDataStructure.cpp +++ b/code/ValidateDataStructure.cpp @@ -46,8 +46,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * the data structure returned by Assimp. */ - - // internal headers #include "ValidateDataStructure.h" #include @@ -110,8 +108,8 @@ void ValidateDSProcess::ReportWarning(const char* msg,...) } // ------------------------------------------------------------------------------------------------ -inline int HasNameMatch(const aiString& in, aiNode* node) -{ +inline +int HasNameMatch(const aiString& in, aiNode* node) { int result = (node->mName == in ? 1 : 0 ); for (unsigned int i = 0; i < node->mNumChildren;++i) { result += HasNameMatch(in,node->mChildren[i]); @@ -121,9 +119,8 @@ inline int HasNameMatch(const aiString& in, aiNode* node) // ------------------------------------------------------------------------------------------------ template -inline void ValidateDSProcess::DoValidation(T** parray, unsigned int size, - const char* firstName, const char* secondName) -{ +inline +void ValidateDSProcess::DoValidation(T** parray, unsigned int size, const char* firstName, const char* secondName) { // validate all entries if (size) { @@ -516,13 +513,12 @@ void ValidateDSProcess::Validate( const aiMesh* pMesh) } // ------------------------------------------------------------------------------------------------ -void ValidateDSProcess::Validate( const aiMesh* pMesh, - const aiBone* pBone,float* afSum) -{ +void ValidateDSProcess::Validate( const aiMesh* pMesh, const aiBone* pBone,float* afSum) { this->Validate(&pBone->mName); if (!pBone->mNumWeights) { - ReportError("aiBone::mNumWeights is zero"); + // ReportError("aiBone::mNumWeights is zero"); + } // check whether all vertices affected by this bone are valid @@ -563,9 +559,6 @@ void ValidateDSProcess::Validate( const aiAnimation* pAnimation) else { ReportError("aiAnimation::mNumChannels is 0. At least one node animation channel must be there."); } - - // Animation duration is allowed to be zero in cases where the anim contains only a single key frame. - // if (!pAnimation->mDuration)this->ReportError("aiAnimation::mDuration is zero"); } // ------------------------------------------------------------------------------------------------ @@ -746,8 +739,9 @@ void ValidateDSProcess::Validate( const aiMaterial* pMaterial) "AI_MATKEY_SHININESS_STRENGTH key is 0.0"); } break; - default: ; - }; + default: + break; + } } if (AI_SUCCESS == aiGetMaterialFloat( pMaterial,AI_MATKEY_OPACITY,&fTemp) && (!fTemp || fTemp > 1.01)) { From 69ece762d13cb38be2147399d5d56d74aa0488f1 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 5 May 2019 16:00:57 +0200 Subject: [PATCH 2/4] fix the build: remove errorenous pragma statement. --- code/ASEParser.cpp | 1 - code/FBXConverter.cpp | 15 +++++++++------ code/ValidateDataStructure.cpp | 11 +++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/code/ASEParser.cpp b/code/ASEParser.cpp index f21f49eeb..14b962320 100644 --- a/code/ASEParser.cpp +++ b/code/ASEParser.cpp @@ -45,7 +45,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * @brief Implementation of the ASE parser class */ -#pragma once #ifndef ASSIMP_BUILD_NO_ASE_IMPORTER #ifndef ASSIMP_BUILD_NO_3DS_IMPORTER diff --git a/code/FBXConverter.cpp b/code/FBXConverter.cpp index 8d7c087ca..76b6c6b4e 100644 --- a/code/FBXConverter.cpp +++ b/code/FBXConverter.cpp @@ -388,6 +388,7 @@ namespace Assimp { break; default: ai_assert(false); + break; } } @@ -978,7 +979,9 @@ namespace Assimp { unsigned int epcount = 0; for (unsigned i = 0; i < indices.size(); i++) { - if (indices[i] < 0) epcount++; + if (indices[i] < 0) { + epcount++; + } } unsigned int pcount = static_cast( indices.size() ); unsigned int scount = out_mesh->mNumFaces = pcount - epcount; @@ -1408,9 +1411,9 @@ namespace Assimp { const WeightIndexArray& indices = cluster->GetIndices(); - /*if (indices.empty()) { + if (indices.empty() && mRemoveEmptyBones ) { continue; - }*/ + } const MatIndexArray& mats = geo.GetMaterialIndices(); @@ -1460,14 +1463,14 @@ namespace Assimp { } } } - + // if we found at least one, generate the output bones // XXX this could be heavily simplified by collecting the bone // data in a single step. - //if (ok) { + if (ok && mRemoveEmptyBones) { ConvertCluster(bones, model, *cluster, out_indices, index_out_indices, count_out_indices, node_global_transform); - //} + } } } catch (std::exception&) { diff --git a/code/ValidateDataStructure.cpp b/code/ValidateDataStructure.cpp index 58f700b0e..712fd6943 100644 --- a/code/ValidateDataStructure.cpp +++ b/code/ValidateDataStructure.cpp @@ -178,7 +178,8 @@ inline void ValidateDSProcess::DoValidationEx(T** parray, unsigned int size, // ------------------------------------------------------------------------------------------------ template inline -void ValidateDSProcess::DoValidationWithNameCheck(T** array, unsigned int size, const char* firstName, const char* secondName) { +void ValidateDSProcess::DoValidationWithNameCheck(T** array, unsigned int size, const char* firstName, + const char* secondName) { // validate all entries DoValidationEx(array,size,firstName,secondName); @@ -198,9 +199,8 @@ void ValidateDSProcess::DoValidationWithNameCheck(T** array, unsigned int size, // ------------------------------------------------------------------------------------------------ // Executes the post processing step on the given imported data. -void ValidateDSProcess::Execute( aiScene* pScene) -{ - this->mScene = pScene; +void ValidateDSProcess::Execute( aiScene* pScene) { + mScene = pScene; ASSIMP_LOG_DEBUG("ValidateDataStructureProcess begin"); // validate the node graph of the scene @@ -517,8 +517,7 @@ void ValidateDSProcess::Validate( const aiMesh* pMesh, const aiBone* pBone,float this->Validate(&pBone->mName); if (!pBone->mNumWeights) { - // ReportError("aiBone::mNumWeights is zero"); - + //ReportError("aiBone::mNumWeights is zero"); } // check whether all vertices affected by this bone are valid From 1324409c7b50ba8afa686facdb2b9468bd606bb5 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 5 May 2019 16:23:42 +0200 Subject: [PATCH 3/4] fix build: fix wrong function prototype. --- code/FBXConverter.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/code/FBXConverter.h b/code/FBXConverter.h index fe4e916be..a77cb4310 100644 --- a/code/FBXConverter.h +++ b/code/FBXConverter.h @@ -79,9 +79,10 @@ class Document; /** * Convert a FBX #Document to #aiScene * @param out Empty scene to be populated - * @param doc Parsed FBX document + * @param doc Parsed FBX document + * @param removeEmptyBones Will remove bones, which do not have any references to vertices. */ -void ConvertToAssimpScene(aiScene* out, const Document& doc); +void ConvertToAssimpScene(aiScene* out, const Document& doc, bool removeEmptyBones ); /** Dummy class to encapsulate the conversion process */ class FBXConverter { From eb1a2586c3e4ee5097b207b07feb5cfb61cd05bf Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 5 May 2019 17:44:15 +0200 Subject: [PATCH 4/4] add missing define in config.h.in --- include/assimp/config.h.in | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/include/assimp/config.h.in b/include/assimp/config.h.in index a37ff0b8c..50a5512e2 100644 --- a/include/assimp/config.h.in +++ b/include/assimp/config.h.in @@ -657,7 +657,11 @@ enum aiComponent */ #define AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING \ "AI_CONFIG_IMPORT_FBX_EMBEDDED_TEXTURES_LEGACY_NAMING" - + +// --------------------------------------------------------------------------- +#define AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES \ + "AI_CONFIG_IMPORT_REMOVE_EMPTY_BONES" + // --------------------------------------------------------------------------- /** @brief Set the vertex animation keyframe to be imported *