CalculateDistance method

pull/4130/head
Malcolm Tyrrell 2021-10-21 08:39:44 +01:00
parent 4ba99c1bb0
commit db0127859a
3 changed files with 21 additions and 9 deletions

View File

@ -87,6 +87,11 @@ void SpatialSort::Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
mFinalized = pFinalize; mFinalized = pFinalize;
} }
// ------------------------------------------------------------------------------------------------
ai_real SpatialSort::CalculateDistance(const aiVector3D &pPosition) const {
return (pPosition - mCentroid) * mPlaneNormal;
}
// ------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------
void SpatialSort::Finalize() { void SpatialSort::Finalize() {
const ai_real scale = 1.0f / mPositions.size(); const ai_real scale = 1.0f / mPositions.size();
@ -94,7 +99,7 @@ void SpatialSort::Finalize() {
mCentroid += scale * mPositions[i].mPosition; mCentroid += scale * mPositions[i].mPosition;
} }
for (unsigned int i = 0; i < mPositions.size(); i++) { 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()); std::sort(mPositions.begin(), mPositions.end());
mFinalized = true; mFinalized = true;
@ -125,7 +130,7 @@ void SpatialSort::Append(const aiVector3D *pPositions, unsigned int pNumPosition
void SpatialSort::FindPositions(const aiVector3D &pPosition, void SpatialSort::FindPositions(const aiVector3D &pPosition,
ai_real pRadius, std::vector<unsigned int> &poResults) const { ai_real pRadius, std::vector<unsigned int> &poResults) const {
ai_assert(mFinalized && "The SpatialSort object must be finalized before FindPositions can be called."); 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; const ai_real minDist = dist - pRadius, maxDist = dist + pRadius;
// clear the array // 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 // 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. // 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; const BinFloat maxDistBinary = minDistBinary + 2 * distanceToleranceInULPs;
// clear the array in this strange fashion because a simple clear() would also deallocate // clear the array in this strange fashion because a simple clear() would also deallocate

View File

@ -143,9 +143,19 @@ public:
ai_real pRadius) const; ai_real pRadius) const;
protected: 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; 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, /** An entry in a spatially sorted position array. Consists of a vertex index,
* its position and its pre-calculated distance from the reference plane */ * its position and its pre-calculated distance from the reference plane */
struct Entry { struct Entry {
@ -161,7 +171,7 @@ protected:
// empty // empty
} }
Entry(unsigned int pIndex, const aiVector3D &pPosition) : Entry(unsigned int pIndex, const aiVector3D &pPosition) :
mIndex(pIndex), mPosition(pPosition) { mIndex(pIndex), mPosition(pPosition), mDistance(std::numeric_limits<ai_real>::max()) {
// empty // empty
} }
@ -173,9 +183,6 @@ protected:
/// false until the Finalize method is called. /// false until the Finalize method is called.
bool mFinalized; bool mFinalized;
/// The centroid of the positions. Calculated in Finalize.
aiVector3D mCentroid;
}; };
} // end of namespace Assimp } // end of namespace Assimp

View File

@ -103,7 +103,7 @@ TEST_F(utSpatialSort, highlyDisplacedPositionsTest) {
SpatialSort sSort; SpatialSort sSort;
sSort.Fill(positions, totalNumPositions, sizeof(aiVector3D)); 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; const ai_real epsilon = 1.1f * step;
std::vector<unsigned int> indices; std::vector<unsigned int> indices;
// Iterate through the _interior_ points of the cube. // Iterate through the _interior_ points of the cube.