From db0127859aeb27add83afc9440f41e1a0a8db6db Mon Sep 17 00:00:00 2001 From: Malcolm Tyrrell Date: Thu, 21 Oct 2021 08:39:44 +0100 Subject: [PATCH] CalculateDistance method --- code/Common/SpatialSort.cpp | 11 ++++++++--- include/assimp/SpatialSort.h | 17 ++++++++++++----- test/unit/Common/utSpatialSort.cpp | 2 +- 3 files changed, 21 insertions(+), 9 deletions(-) diff --git a/code/Common/SpatialSort.cpp b/code/Common/SpatialSort.cpp index 08a7c55df..9b96887ac 100644 --- a/code/Common/SpatialSort.cpp +++ b/code/Common/SpatialSort.cpp @@ -87,6 +87,11 @@ void SpatialSort::Fill(const aiVector3D *pPositions, unsigned int pNumPositions, mFinalized = pFinalize; } +// ------------------------------------------------------------------------------------------------ +ai_real SpatialSort::CalculateDistance(const aiVector3D &pPosition) const { + return (pPosition - mCentroid) * mPlaneNormal; +} + // ------------------------------------------------------------------------------------------------ void SpatialSort::Finalize() { const ai_real scale = 1.0f / mPositions.size(); @@ -94,7 +99,7 @@ void SpatialSort::Finalize() { mCentroid += scale * mPositions[i].mPosition; } for (unsigned int i = 0; i < mPositions.size(); i++) { - mPositions[i].mDistance = (mPositions[i].mPosition - mCentroid) * mPlaneNormal; + mPositions[i].mDistance = CalculateDistance(mPositions[i].mPosition); } std::sort(mPositions.begin(), mPositions.end()); mFinalized = true; @@ -125,7 +130,7 @@ void SpatialSort::Append(const aiVector3D *pPositions, unsigned int pNumPosition void SpatialSort::FindPositions(const aiVector3D &pPosition, ai_real pRadius, std::vector &poResults) const { ai_assert(mFinalized && "The SpatialSort object must be finalized before FindPositions can be called."); - const ai_real dist = (pPosition - mCentroid) * mPlaneNormal; + const ai_real dist = CalculateDistance(pPosition); const ai_real minDist = dist - pRadius, maxDist = dist + pRadius; // clear the array @@ -266,7 +271,7 @@ void SpatialSort::FindIdenticalPositions(const aiVector3D &pPosition, std::vecto // Convert the plane distance to its signed integer representation so the ULPs tolerance can be // applied. For some reason, VC won't optimize two calls of the bit pattern conversion. - const BinFloat minDistBinary = ToBinary((pPosition - mCentroid) * mPlaneNormal) - distanceToleranceInULPs; + const BinFloat minDistBinary = ToBinary(CalculateDistance(pPosition)) - distanceToleranceInULPs; const BinFloat maxDistBinary = minDistBinary + 2 * distanceToleranceInULPs; // clear the array in this strange fashion because a simple clear() would also deallocate diff --git a/include/assimp/SpatialSort.h b/include/assimp/SpatialSort.h index 39f863b15..c0c4a0292 100644 --- a/include/assimp/SpatialSort.h +++ b/include/assimp/SpatialSort.h @@ -143,9 +143,19 @@ public: ai_real pRadius) const; protected: - /** Normal of the sorting plane, normalized. */ + /** Return the distance to the sorting plane. */ + ai_real CalculateDistance(const aiVector3D &pPosition) const; + +protected: + /** Normal of the sorting plane, normalized. + */ aiVector3D mPlaneNormal; + /** The centroid of the positions, which is used as a point on the sorting plane + * when calculating distance. This value is calculated in Finalize. + */ + aiVector3D mCentroid; + /** An entry in a spatially sorted position array. Consists of a vertex index, * its position and its pre-calculated distance from the reference plane */ struct Entry { @@ -161,7 +171,7 @@ protected: // empty } Entry(unsigned int pIndex, const aiVector3D &pPosition) : - mIndex(pIndex), mPosition(pPosition) { + mIndex(pIndex), mPosition(pPosition), mDistance(std::numeric_limits::max()) { // empty } @@ -173,9 +183,6 @@ protected: /// false until the Finalize method is called. bool mFinalized; - - /// The centroid of the positions. Calculated in Finalize. - aiVector3D mCentroid; }; } // end of namespace Assimp diff --git a/test/unit/Common/utSpatialSort.cpp b/test/unit/Common/utSpatialSort.cpp index 8ebae76e8..f0b694c7d 100644 --- a/test/unit/Common/utSpatialSort.cpp +++ b/test/unit/Common/utSpatialSort.cpp @@ -103,7 +103,7 @@ TEST_F(utSpatialSort, highlyDisplacedPositionsTest) { SpatialSort sSort; sSort.Fill(positions, totalNumPositions, sizeof(aiVector3D)); - // Enough to pick up a point and its 6 immediate neighbors, but not any other point. + // Enough to find a point and its 6 immediate neighbors, but not any other point. const ai_real epsilon = 1.1f * step; std::vector indices; // Iterate through the _interior_ points of the cube.