Subtract the centroid

pull/4130/head
Malcolm Tyrrell 2021-10-20 15:08:08 +01:00
parent 4d10f5d133
commit c644f9d719
3 changed files with 47 additions and 6 deletions

View File

@ -89,8 +89,12 @@ void SpatialSort::Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
// ------------------------------------------------------------------------------------------------
void SpatialSort::Finalize() {
const ai_real scale = 1.0f / mPositions.size();
for (unsigned int i = 0; i < mPositions.size(); i++) {
mPositions[i].mDistance = mPositions[i].mPosition * mPlaneNormal;
mCentroid += scale * mPositions[i].mPosition;
}
for (unsigned int i = 0; i < mPositions.size(); i++) {
mPositions[i].mDistance = (mPositions[i].mPosition - mCentroid) * mPlaneNormal;
}
std::sort(mPositions.begin(), mPositions.end());
mFinalized = true;
@ -121,7 +125,7 @@ void SpatialSort::Append(const aiVector3D *pPositions, unsigned int pNumPosition
void SpatialSort::FindPositions(const aiVector3D &pPosition,
ai_real pRadius, std::vector<unsigned int> &poResults) const {
ai_assert(mFinalized && "The SpatialSort object must be finalized before FindPositions can be called.");
const ai_real dist = pPosition * mPlaneNormal;
const ai_real dist = (pPosition - mCentroid) * mPlaneNormal;
const ai_real minDist = dist - pRadius, maxDist = dist + pRadius;
// clear the array
@ -262,7 +266,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 * mPlaneNormal) - distanceToleranceInULPs;
const BinFloat minDistBinary = ToBinary((pPosition - mCentroid) * mPlaneNormal) - distanceToleranceInULPs;
const BinFloat maxDistBinary = minDistBinary + 2 * distanceToleranceInULPs;
// clear the array in this strange fashion because a simple clear() would also deallocate
@ -312,7 +316,7 @@ unsigned int SpatialSort::GenerateMappingTable(std::vector<unsigned int> &fill,
unsigned int t = 0;
const ai_real pSquared = pRadius * pRadius;
for (size_t i = 0; i < mPositions.size();) {
dist = mPositions[i].mPosition * mPlaneNormal;
dist = (mPositions[i].mPosition - mCentroid) * mPlaneNormal;
maxDist = dist + pRadius;
fill[mPositions[i].mIndex] = t;

View File

@ -143,7 +143,7 @@ public:
ai_real pRadius) const;
protected:
/** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
/** Normal of the sorting plane, normalized. */
aiVector3D mPlaneNormal;
/** An entry in a spatially sorted position array. Consists of a vertex index,
@ -171,8 +171,11 @@ protected:
// all positions, sorted by distance to the sorting plane
std::vector<Entry> mPositions;
// False until the Finalize method is called.
/// false until the Finalize method is called.
bool mFinalized;
/// The centroid of the positions. Calculated in Finalize.
aiVector3D mCentroid;
};
} // end of namespace Assimp

View File

@ -82,3 +82,37 @@ TEST_F(utSpatialSort, findPositionsTest) {
sSort.FindPositions(vecs[0], 0.01f, indices);
EXPECT_EQ(1u, indices.size());
}
TEST_F(utSpatialSort, highlyDisplacedPositionsTest) {
constexpr unsigned int verticesPerAxis = 10;
constexpr ai_real step = 0.001f;
constexpr ai_real offset = 5000.0f - (0.5f * verticesPerAxis * step);
constexpr unsigned int totalNumPositions = verticesPerAxis * verticesPerAxis * verticesPerAxis;
aiVector3D* positions = new aiVector3D[totalNumPositions];
for (unsigned int x = 0; x < verticesPerAxis; ++x) {
for (unsigned int y = 0; y < verticesPerAxis; ++y) {
for (unsigned int z = 0; z < verticesPerAxis; ++z) {
const unsigned int index = (x * verticesPerAxis * verticesPerAxis) + (y * verticesPerAxis) + z;
positions[index] = aiVector3D(offset + (x * step), offset + (y * step), offset + (z * step));
}
}
}
SpatialSort sSort;
sSort.Fill(positions, totalNumPositions, sizeof(aiVector3D));
// Enough to pick up the neighboring vertices, but not the neighbors' neighbors.
const ai_real epsilon = 1.1f * step;
std::vector<unsigned int> indices;
// Iterate through the interior points of the cube.
for (unsigned int x = 1; x < verticesPerAxis - 1; ++x) {
for (unsigned int y = 1; y < verticesPerAxis - 1; ++y) {
for (unsigned int z = 1; z < verticesPerAxis - 1; ++z) {
const unsigned int index = (x * verticesPerAxis * verticesPerAxis) + (y * verticesPerAxis) + z;
sSort.FindPositions(positions[index], epsilon, indices);
ASSERT_EQ(7u, indices.size());
}
}
}
delete[] positions;
}