From 2acfc125c3dfc295136b81050425ac3e23d37654 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Tue, 14 Mar 2023 20:03:14 +0100 Subject: [PATCH] Refactoring: Make GeoUtils reusable --- code/CMakeLists.txt | 7 ++++ code/Common/Subdivision.cpp | 1 + code/Geometry/GeometryUtils.cpp | 40 +++++++++++++++++++ code/Geometry/GeometryUtils.h | 13 ++++++ code/PostProcessing/FindDegenerates.cpp | 38 +----------------- code/PostProcessing/FindDegenerates.h | 14 +++---- code/PostProcessing/FindInstancesProcess.cpp | 4 -- code/PostProcessing/FindInstancesProcess.h | 10 ++--- .../PostProcessing/FindInvalidDataProcess.cpp | 4 -- code/PostProcessing/FindInvalidDataProcess.h | 2 +- code/PostProcessing/FixNormalsStep.cpp | 15 +------ code/PostProcessing/FixNormalsStep.h | 7 ++-- .../GenBoundingBoxesProcess.cpp | 4 -- code/PostProcessing/GenBoundingBoxesProcess.h | 11 ++--- code/PostProcessing/GenFaceNormalsProcess.cpp | 8 ---- code/PostProcessing/GenFaceNormalsProcess.h | 17 ++++---- .../GenVertexNormalsProcess.cpp | 13 +++--- code/PostProcessing/GenVertexNormalsProcess.h | 2 +- 18 files changed, 96 insertions(+), 114 deletions(-) create mode 100644 code/Geometry/GeometryUtils.cpp create mode 100644 code/Geometry/GeometryUtils.h diff --git a/code/CMakeLists.txt b/code/CMakeLists.txt index a098f3e85..ba5415fe0 100644 --- a/code/CMakeLists.txt +++ b/code/CMakeLists.txt @@ -218,6 +218,12 @@ SET( CApi_SRCS ) SOURCE_GROUP(CApi FILES ${CApi_SRCS}) +SET(Geometry_SRCS + Geometry/GeometryUtils.h + Geometry/GeometryUtils.cpp +) +SOURCE_GROUP(Geometry FILES ${Geometry_SRCS}) + SET( STEPParser_SRCS AssetLib/STEPParser/STEPFileReader.h AssetLib/STEPParser/STEPFileReader.cpp @@ -1129,6 +1135,7 @@ SET( assimp_src ${Core_SRCS} ${CApi_SRCS} ${Common_SRCS} + ${Geometry_SRCS} ${Logging_SRCS} ${Exporter_SRCS} ${PostProcessing_SRCS} diff --git a/code/Common/Subdivision.cpp b/code/Common/Subdivision.cpp index ac4078b47..3aea5d4c5 100644 --- a/code/Common/Subdivision.cpp +++ b/code/Common/Subdivision.cpp @@ -53,6 +53,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include using namespace Assimp; + void mydummy() {} #ifdef _MSC_VER diff --git a/code/Geometry/GeometryUtils.cpp b/code/Geometry/GeometryUtils.cpp new file mode 100644 index 000000000..9b29af1dd --- /dev/null +++ b/code/Geometry/GeometryUtils.cpp @@ -0,0 +1,40 @@ +#include "GeometryUtils.h" + +#include + +namespace Assimp { + +ai_real GeometryUtils::heron( ai_real a, ai_real b, ai_real c ) { + ai_real s = (a + b + c) / 2; + ai_real area = pow((s * ( s - a ) * ( s - b ) * ( s - c ) ), (ai_real)0.5 ); + return area; +} + +ai_real GeometryUtils::distance3D( const aiVector3D &vA, aiVector3D &vB ) { + const ai_real lx = ( vB.x - vA.x ); + const ai_real ly = ( vB.y - vA.y ); + const ai_real lz = ( vB.z - vA.z ); + ai_real a = lx*lx + ly*ly + lz*lz; + ai_real d = pow( a, (ai_real)0.5 ); + + return d; +} + + + +ai_real GeometryUtils::calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ) { + ai_real area = 0; + + aiVector3D vA( mesh->mVertices[ face.mIndices[ 0 ] ] ); + aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] ); + aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] ); + + ai_real a( distance3D( vA, vB ) ); + ai_real b( distance3D( vB, vC ) ); + ai_real c( distance3D( vC, vA ) ); + area = heron( a, b, c ); + + return area; +} + +} // namespace Assimp diff --git a/code/Geometry/GeometryUtils.h b/code/Geometry/GeometryUtils.h new file mode 100644 index 000000000..2eb96926d --- /dev/null +++ b/code/Geometry/GeometryUtils.h @@ -0,0 +1,13 @@ +#include +#include + +namespace Assimp { + +class GeometryUtils { +public: + static ai_real heron( ai_real a, ai_real b, ai_real c ); + static ai_real distance3D( const aiVector3D &vA, aiVector3D &vB ); + static ai_real calculateAreaOfTriangle( const aiFace& face, aiMesh* mesh ); +}; + +} // namespace Assimp diff --git a/code/PostProcessing/FindDegenerates.cpp b/code/PostProcessing/FindDegenerates.cpp index 344979949..5874c17d2 100644 --- a/code/PostProcessing/FindDegenerates.cpp +++ b/code/PostProcessing/FindDegenerates.cpp @@ -45,6 +45,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "ProcessHelper.h" #include "FindDegenerates.h" +#include "Geometry/GeometryUtils.h" #include @@ -63,10 +64,6 @@ FindDegeneratesProcess::FindDegeneratesProcess() : // empty } -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -FindDegeneratesProcess::~FindDegeneratesProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. bool FindDegeneratesProcess::IsActive( unsigned int pFlags) const { @@ -132,37 +129,6 @@ static void updateSceneGraph(aiNode* pNode, const std::unordered_mapmVertices[ face.mIndices[ 0 ] ] ); - aiVector3D vB( mesh->mVertices[ face.mIndices[ 1 ] ] ); - aiVector3D vC( mesh->mVertices[ face.mIndices[ 2 ] ] ); - - ai_real a( distance3D( vA, vB ) ); - ai_real b( distance3D( vB, vC ) ); - ai_real c( distance3D( vC, vA ) ); - area = heron( a, b, c ); - - return area; -} - // ------------------------------------------------------------------------------------------------ // Executes the post processing step on the given imported mesh bool FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh) { @@ -218,7 +184,7 @@ bool FindDegeneratesProcess::ExecuteOnMesh( aiMesh* mesh) { if ( mConfigCheckAreaOfTriangle ) { if ( face.mNumIndices == 3 ) { - ai_real area = calculateAreaOfTriangle( face, mesh ); + ai_real area = GeometryUtils::calculateAreaOfTriangle( face, mesh ); if (area < ai_epsilon) { if ( mConfigRemoveDegenerates ) { remove_me[ a ] = true; diff --git a/code/PostProcessing/FindDegenerates.h b/code/PostProcessing/FindDegenerates.h index 6fe1e929b..55a9dd981 100644 --- a/code/PostProcessing/FindDegenerates.h +++ b/code/PostProcessing/FindDegenerates.h @@ -59,7 +59,7 @@ namespace Assimp { class ASSIMP_API FindDegeneratesProcess : public BaseProcess { public: FindDegeneratesProcess(); - ~FindDegeneratesProcess(); + ~FindDegeneratesProcess() = default; // ------------------------------------------------------------------- // Check whether step is active @@ -105,23 +105,19 @@ private: bool mConfigCheckAreaOfTriangle; }; -inline -void FindDegeneratesProcess::EnableInstantRemoval(bool enabled) { +inline void FindDegeneratesProcess::EnableInstantRemoval(bool enabled) { mConfigRemoveDegenerates = enabled; } -inline -bool FindDegeneratesProcess::IsInstantRemoval() const { +inline bool FindDegeneratesProcess::IsInstantRemoval() const { return mConfigRemoveDegenerates; } -inline -void FindDegeneratesProcess::EnableAreaCheck( bool enabled ) { +inline void FindDegeneratesProcess::EnableAreaCheck( bool enabled ) { mConfigCheckAreaOfTriangle = enabled; } -inline -bool FindDegeneratesProcess::isAreaCheckEnabled() const { +inline bool FindDegeneratesProcess::isAreaCheckEnabled() const { return mConfigCheckAreaOfTriangle; } diff --git a/code/PostProcessing/FindInstancesProcess.cpp b/code/PostProcessing/FindInstancesProcess.cpp index 07a0f66db..55974b1c3 100644 --- a/code/PostProcessing/FindInstancesProcess.cpp +++ b/code/PostProcessing/FindInstancesProcess.cpp @@ -58,10 +58,6 @@ FindInstancesProcess::FindInstancesProcess() : configSpeedFlag (false) {} -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -FindInstancesProcess::~FindInstancesProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. bool FindInstancesProcess::IsActive( unsigned int pFlags) const diff --git a/code/PostProcessing/FindInstancesProcess.h b/code/PostProcessing/FindInstancesProcess.h index b501d88d5..d1daeb1f4 100644 --- a/code/PostProcessing/FindInstancesProcess.h +++ b/code/PostProcessing/FindInstancesProcess.h @@ -107,14 +107,11 @@ inline bool CompareArrays(const aiColor4D* first, const aiColor4D* second, // --------------------------------------------------------------------------- /** @brief A post-processing steps to search for instanced meshes */ -class FindInstancesProcess : public BaseProcess -{ +class FindInstancesProcess : public BaseProcess { public: - FindInstancesProcess(); - ~FindInstancesProcess(); + ~FindInstancesProcess() = default; -public: // ------------------------------------------------------------------- // Check whether step is active in given flags combination bool IsActive( unsigned int pFlags) const; @@ -128,10 +125,9 @@ public: void SetupProperties(const Importer* pImp); private: - bool configSpeedFlag; - }; // ! end class FindInstancesProcess + } // ! end namespace Assimp #endif // !! AI_FINDINSTANCES_H_INC diff --git a/code/PostProcessing/FindInvalidDataProcess.cpp b/code/PostProcessing/FindInvalidDataProcess.cpp index c65208cbd..bb8e365a1 100644 --- a/code/PostProcessing/FindInvalidDataProcess.cpp +++ b/code/PostProcessing/FindInvalidDataProcess.cpp @@ -60,10 +60,6 @@ FindInvalidDataProcess::FindInvalidDataProcess() : // nothing to do here } -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -FindInvalidDataProcess::~FindInvalidDataProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. bool FindInvalidDataProcess::IsActive(unsigned int pFlags) const { diff --git a/code/PostProcessing/FindInvalidDataProcess.h b/code/PostProcessing/FindInvalidDataProcess.h index 5ea895c59..c5c7067a6 100644 --- a/code/PostProcessing/FindInvalidDataProcess.h +++ b/code/PostProcessing/FindInvalidDataProcess.h @@ -65,7 +65,7 @@ namespace Assimp { class ASSIMP_API FindInvalidDataProcess : public BaseProcess { public: FindInvalidDataProcess(); - ~FindInvalidDataProcess(); + ~FindInvalidDataProcess() = default; // ------------------------------------------------------------------- // diff --git a/code/PostProcessing/FixNormalsStep.cpp b/code/PostProcessing/FixNormalsStep.cpp index 3791bd35a..54ac05cc8 100644 --- a/code/PostProcessing/FixNormalsStep.cpp +++ b/code/PostProcessing/FixNormalsStep.cpp @@ -56,26 +56,15 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace Assimp; - -// ------------------------------------------------------------------------------------------------ -// Constructor to be privately used by Importer -FixInfacingNormalsProcess::FixInfacingNormalsProcess() = default; - -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -FixInfacingNormalsProcess::~FixInfacingNormalsProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. -bool FixInfacingNormalsProcess::IsActive( unsigned int pFlags) const -{ +bool FixInfacingNormalsProcess::IsActive( unsigned int pFlags) const { return (pFlags & aiProcess_FixInfacingNormals) != 0; } // ------------------------------------------------------------------------------------------------ // Executes the post processing step on the given imported data. -void FixInfacingNormalsProcess::Execute( aiScene* pScene) -{ +void FixInfacingNormalsProcess::Execute( aiScene* pScene) { ASSIMP_LOG_DEBUG("FixInfacingNormalsProcess begin"); bool bHas( false ); diff --git a/code/PostProcessing/FixNormalsStep.h b/code/PostProcessing/FixNormalsStep.h index b7d3ba386..ec546c987 100644 --- a/code/PostProcessing/FixNormalsStep.h +++ b/code/PostProcessing/FixNormalsStep.h @@ -49,8 +49,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. struct aiMesh; -namespace Assimp -{ +namespace Assimp { // --------------------------------------------------------------------------- /** The FixInfacingNormalsProcess tries to determine whether the normal @@ -59,8 +58,8 @@ namespace Assimp */ class FixInfacingNormalsProcess : public BaseProcess { public: - FixInfacingNormalsProcess(); - ~FixInfacingNormalsProcess(); + FixInfacingNormalsProcess() = default; + ~FixInfacingNormalsProcess() = default; // ------------------------------------------------------------------- /** Returns whether the processing step is present in the given flag field. diff --git a/code/PostProcessing/GenBoundingBoxesProcess.cpp b/code/PostProcessing/GenBoundingBoxesProcess.cpp index 52a0861e5..ca8e4d6d0 100644 --- a/code/PostProcessing/GenBoundingBoxesProcess.cpp +++ b/code/PostProcessing/GenBoundingBoxesProcess.cpp @@ -48,10 +48,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { -GenBoundingBoxesProcess::GenBoundingBoxesProcess() = default; - -GenBoundingBoxesProcess::~GenBoundingBoxesProcess() = default; - bool GenBoundingBoxesProcess::IsActive(unsigned int pFlags) const { return 0 != ( pFlags & aiProcess_GenBoundingBoxes ); } diff --git a/code/PostProcessing/GenBoundingBoxesProcess.h b/code/PostProcessing/GenBoundingBoxesProcess.h index 0b7591b6d..a880a0638 100644 --- a/code/PostProcessing/GenBoundingBoxesProcess.h +++ b/code/PostProcessing/GenBoundingBoxesProcess.h @@ -19,7 +19,7 @@ conditions are met: copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. - +s * Neither the name of the assimp team, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior @@ -54,15 +54,16 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { -/** Post-processing process to find axis-aligned bounding volumes for amm meshes - * used in a scene +/** + * @brief Post-processing process to find axis-aligned bounding volumes for amm meshes + * used in a scene. */ class ASSIMP_API GenBoundingBoxesProcess : public BaseProcess { public: /// The class constructor. - GenBoundingBoxesProcess(); + GenBoundingBoxesProcess() = default; /// The class destructor. - ~GenBoundingBoxesProcess(); + ~GenBoundingBoxesProcess() = default; /// Will return true, if aiProcess_GenBoundingBoxes is defined. bool IsActive(unsigned int pFlags) const override; /// The execution callback. diff --git a/code/PostProcessing/GenFaceNormalsProcess.cpp b/code/PostProcessing/GenFaceNormalsProcess.cpp index d3520d4b2..1d259ce22 100644 --- a/code/PostProcessing/GenFaceNormalsProcess.cpp +++ b/code/PostProcessing/GenFaceNormalsProcess.cpp @@ -54,14 +54,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. using namespace Assimp; -// ------------------------------------------------------------------------------------------------ -// Constructor to be privately used by Importer -GenFaceNormalsProcess::GenFaceNormalsProcess() = default; - -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -GenFaceNormalsProcess::~GenFaceNormalsProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. bool GenFaceNormalsProcess::IsActive(unsigned int pFlags) const { diff --git a/code/PostProcessing/GenFaceNormalsProcess.h b/code/PostProcessing/GenFaceNormalsProcess.h index c2f157e20..68e3fee0b 100644 --- a/code/PostProcessing/GenFaceNormalsProcess.h +++ b/code/PostProcessing/GenFaceNormalsProcess.h @@ -47,20 +47,17 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "Common/BaseProcess.h" #include -namespace Assimp -{ +namespace Assimp { // --------------------------------------------------------------------------- -/** The GenFaceNormalsProcess computes face normals for all faces of all meshes -*/ -class ASSIMP_API_WINONLY GenFaceNormalsProcess : public BaseProcess -{ +/** + * @brief The GenFaceNormalsProcess computes face normals for all faces of all meshes + */ +class ASSIMP_API_WINONLY GenFaceNormalsProcess : public BaseProcess { public: + GenFaceNormalsProcess() = default; + ~GenFaceNormalsProcess() = default; - GenFaceNormalsProcess(); - ~GenFaceNormalsProcess(); - -public: // ------------------------------------------------------------------- /** Returns whether the processing step is present in the given flag field. * @param pFlags The processing flags the importer was called with. A bitwise diff --git a/code/PostProcessing/GenVertexNormalsProcess.cpp b/code/PostProcessing/GenVertexNormalsProcess.cpp index 5b9033383..c8afac297 100644 --- a/code/PostProcessing/GenVertexNormalsProcess.cpp +++ b/code/PostProcessing/GenVertexNormalsProcess.cpp @@ -60,10 +60,6 @@ GenVertexNormalsProcess::GenVertexNormalsProcess() : // empty } -// ------------------------------------------------------------------------------------------------ -// Destructor, private as well -GenVertexNormalsProcess::~GenVertexNormalsProcess() = default; - // ------------------------------------------------------------------------------------------------ // Returns whether the processing step is present in the given flag field. bool GenVertexNormalsProcess::IsActive(unsigned int pFlags) const { @@ -109,10 +105,10 @@ void GenVertexNormalsProcess::Execute(aiScene *pScene) { // Executes the post processing step on the given imported data. bool GenVertexNormalsProcess::GenMeshVertexNormals(aiMesh *pMesh, unsigned int meshIndex) { if (nullptr != pMesh->mNormals) { - if (force_) - delete[] pMesh->mNormals; - else + if (!force_) { return false; + } + delete[] pMesh->mNormals; } // If the mesh consists of lines and/or points but not of @@ -144,8 +140,9 @@ bool GenVertexNormalsProcess::GenMeshVertexNormals(aiMesh *pMesh, unsigned int m const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]]; // Boolean XOR - if either but not both of these flags is set, then the winding order has // changed and the cross product to calculate the normal needs to be reversed - if (flippedWindingOrder_ != leftHanded_) + if (flippedWindingOrder_ != leftHanded_) { std::swap(pV2, pV3); + } const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe(); for (unsigned int i = 0; i < face.mNumIndices; ++i) { diff --git a/code/PostProcessing/GenVertexNormalsProcess.h b/code/PostProcessing/GenVertexNormalsProcess.h index 370bf42b1..3a15bf4a8 100644 --- a/code/PostProcessing/GenVertexNormalsProcess.h +++ b/code/PostProcessing/GenVertexNormalsProcess.h @@ -61,7 +61,7 @@ namespace Assimp { class ASSIMP_API GenVertexNormalsProcess : public BaseProcess { public: GenVertexNormalsProcess(); - ~GenVertexNormalsProcess(); + ~GenVertexNormalsProcess() = default; // ------------------------------------------------------------------- /** Returns whether the processing step is present in the given flag.