From 037a213bb4cd1e5853df390b4a64d663efde2fe2 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 1 May 2018 15:06:56 +0200 Subject: [PATCH] STL-Exporter: fix division by zero in normalize method during update --- code/Exporter.cpp | 5 ++-- code/STLExporter.cpp | 8 +++--- include/assimp/Exporter.hpp | 8 +++++- include/assimp/GenericProperty.h | 48 ++++++++++++++++++-------------- test/unit/utSTLImportExport.cpp | 13 +++++++++ 5 files changed, 53 insertions(+), 29 deletions(-) diff --git a/code/Exporter.cpp b/code/Exporter.cpp index 53a623ecd..bd533f3b3 100644 --- a/code/Exporter.cpp +++ b/code/Exporter.cpp @@ -477,7 +477,7 @@ size_t Exporter::GetExportFormatCount() const { // ------------------------------------------------------------------------------------------------ const aiExportFormatDesc* Exporter::GetExportFormatDescription( size_t index ) const { if (index >= GetExportFormatCount()) { - return NULL; + return nullptr; } // Return from static storage if the requested index is built-in. @@ -539,8 +539,7 @@ bool ExportProperties::SetPropertyFloat(const char* szName, ai_real iValue) { // ------------------------------------------------------------------------------------------------ // Set a configuration property -bool ExportProperties :: SetPropertyString(const char* szName, const std::string& value) -{ +bool ExportProperties::SetPropertyString(const char* szName, const std::string& value) { return SetGenericProperty(mStringProperties, szName,value); } diff --git a/code/STLExporter.cpp b/code/STLExporter.cpp index 7b38125ef..e4a1dbb66 100644 --- a/code/STLExporter.cpp +++ b/code/STLExporter.cpp @@ -143,7 +143,7 @@ STLExporter::STLExporter(const char* _filename, const aiScene* pScene, bool expo // Export the assimp mesh const std::string name = "AssimpScene"; - mOutput << SolidToken << name << endl; + mOutput << SolidToken << " " << name << endl; for(unsigned int i = 0; i < pScene->mNumMeshes; ++i) { WriteMesh(pScene->mMeshes[ i ]); } @@ -169,7 +169,7 @@ void STLExporter::WritePointCloud(const std::string &name, const aiScene* pScene mOutput << " vertex " << v.x << " " << v.y << " " << v.z << endl; } } - mOutput << EndSolidToken << name << endl; + mOutput << EndSolidToken << " " << name << endl; } // ------------------------------------------------------------------------------------------------ @@ -185,7 +185,7 @@ void STLExporter::WriteMesh(const aiMesh* m) for(unsigned int a = 0; a < f.mNumIndices; ++a) { nor += m->mNormals[f.mIndices[a]]; } - nor.Normalize(); + nor.NormalizeSafe(); } mOutput << " facet normal " << nor.x << " " << nor.y << " " << nor.z << endl; mOutput << " outer loop" << endl; @@ -199,7 +199,7 @@ void STLExporter::WriteMesh(const aiMesh* m) } } -void STLExporter :: WriteMeshBinary(const aiMesh* m) +void STLExporter::WriteMeshBinary(const aiMesh* m) { for (unsigned int i = 0; i < m->mNumFaces; ++i) { const aiFace& f = m->mFaces[i]; diff --git a/include/assimp/Exporter.hpp b/include/assimp/Exporter.hpp index dfbac9371..3d1a9ea85 100644 --- a/include/assimp/Exporter.hpp +++ b/include/assimp/Exporter.hpp @@ -115,8 +115,14 @@ public: } }; -public: + /** + * @brief The class constructor. + */ Exporter(); + + /** + * @brief The class destructor. + */ ~Exporter(); // ------------------------------------------------------------------- diff --git a/include/assimp/GenericProperty.h b/include/assimp/GenericProperty.h index 96c74b4c4..7b75cb1c8 100644 --- a/include/assimp/GenericProperty.h +++ b/include/assimp/GenericProperty.h @@ -46,15 +46,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include #include "Hash.h" -#include +#include // ------------------------------------------------------------------------------------------------ template -inline bool SetGenericProperty(std::map< unsigned int, T >& list, - const char* szName, const T& value) -{ - ai_assert(NULL != szName); +inline +bool SetGenericProperty(std::map< unsigned int, T >& list, + const char* szName, const T& value) { + ai_assert(nullptr != szName); const uint32_t hash = SuperFastHash(szName); typename std::map::iterator it = list.find(hash); @@ -63,20 +63,22 @@ inline bool SetGenericProperty(std::map< unsigned int, T >& list, return false; } (*it).second = value; + return true; } // ------------------------------------------------------------------------------------------------ template -inline const T& GetGenericProperty(const std::map< unsigned int, T >& list, - const char* szName, const T& errorReturn) -{ - ai_assert(NULL != szName); +inline +const T& GetGenericProperty(const std::map< unsigned int, T >& list, + const char* szName, const T& errorReturn) { + ai_assert(nullptr != szName); const uint32_t hash = SuperFastHash(szName); typename std::map::const_iterator it = list.find(hash); - if (it == list.end()) + if (it == list.end()) { return errorReturn; + } return (*it).second; } @@ -85,16 +87,17 @@ inline const T& GetGenericProperty(const std::map< unsigned int, T >& list, // Special version for pointer types - they will be deleted when replaced with another value // passing NULL removes the whole property template -inline void SetGenericPropertyPtr(std::map< unsigned int, T* >& list, - const char* szName, T* value, bool* bWasExisting = NULL) -{ - ai_assert(NULL != szName); +inline +void SetGenericPropertyPtr(std::map< unsigned int, T* >& list, + const char* szName, T* value, bool* bWasExisting = nullptr ) { + ai_assert(nullptr != szName); const uint32_t hash = SuperFastHash(szName); typename std::map::iterator it = list.find(hash); if (it == list.end()) { - if (bWasExisting) + if (bWasExisting) { *bWasExisting = false; + } list.insert(std::pair( hash, value )); return; @@ -106,20 +109,23 @@ inline void SetGenericPropertyPtr(std::map< unsigned int, T* >& list, if (!value) { list.erase(it); } - if (bWasExisting) + if (bWasExisting) { *bWasExisting = true; + } } // ------------------------------------------------------------------------------------------------ template -inline bool HasGenericProperty(const std::map< unsigned int, T >& list, - const char* szName) -{ - ai_assert(NULL != szName); +inline +bool HasGenericProperty(const std::map< unsigned int, T >& list, + const char* szName) { + ai_assert(nullptr != szName); const uint32_t hash = SuperFastHash(szName); typename std::map::const_iterator it = list.find(hash); - if (it == list.end()) return false; + if (it == list.end()) { + return false; + } return true; } diff --git a/test/unit/utSTLImportExport.cpp b/test/unit/utSTLImportExport.cpp index aad6b0fa1..be73c5421 100644 --- a/test/unit/utSTLImportExport.cpp +++ b/test/unit/utSTLImportExport.cpp @@ -75,6 +75,17 @@ TEST_F( utSTLImporterExporter, test_with_two_solids ) { #ifndef ASSIMP_BUILD_NO_EXPORT +TEST_F(utSTLImporterExporter, exporterTest) { + Assimp::Importer importer; + const aiScene *scene = importer.ReadFile(ASSIMP_TEST_MODELS_DIR "/STL/Spider_ascii.stl", aiProcess_ValidateDataStructure); + + Assimp::Exporter mAiExporter; + mAiExporter.Export( scene, "stl", "spiderExport.stl" ); + + const aiScene *scene2 = importer.ReadFile("spiderExport.stl", aiProcess_ValidateDataStructure); + //EXPECT_NE(nullptr, scene2); +} + TEST_F(utSTLImporterExporter, test_export_pointclouds) { struct XYZ { float x, y, z; @@ -126,6 +137,8 @@ TEST_F(utSTLImporterExporter, test_export_pointclouds) { ExportProperties *properties = new ExportProperties; properties->SetPropertyBool(AI_CONFIG_EXPORT_POINT_CLOUDS, true); mAiExporter.Export(&scene, "stl", "testExport.stl", 0, properties ); + + delete properties; } #endif