2015-05-19 03:48:29 +00:00
|
|
|
/*
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
Open Asset Import Library (assimp)
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
|
2020-01-20 13:53:12 +00:00
|
|
|
Copyright (c) 2006-2020, assimp team
|
2018-01-28 18:42:05 +00:00
|
|
|
|
2015-05-19 03:48:29 +00:00
|
|
|
All rights reserved.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
Redistribution and use of this software in source and binary forms,
|
|
|
|
with or without modification, are permitted provided that the following
|
2015-05-19 03:48:29 +00:00
|
|
|
conditions are met:
|
|
|
|
|
|
|
|
* Redistributions of source code must retain the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer.
|
|
|
|
|
|
|
|
* Redistributions in binary form must reproduce the above
|
|
|
|
copyright notice, this list of conditions and the
|
|
|
|
following disclaimer in the documentation and/or other
|
|
|
|
materials provided with the distribution.
|
|
|
|
|
|
|
|
* 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
|
|
|
|
written permission of the assimp team.
|
|
|
|
|
2015-05-19 03:52:10 +00:00
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
2015-05-19 03:52:10 +00:00
|
|
|
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
2015-05-19 03:48:29 +00:00
|
|
|
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
2015-05-19 03:52:10 +00:00
|
|
|
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
2015-05-19 03:48:29 +00:00
|
|
|
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
2015-05-19 03:52:10 +00:00
|
|
|
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
2015-05-19 03:48:29 +00:00
|
|
|
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
---------------------------------------------------------------------------
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @file Implementation of the helper class to quickly find vertices close to a given position */
|
|
|
|
|
2018-01-06 00:18:33 +00:00
|
|
|
#include <assimp/SpatialSort.h>
|
2016-06-06 20:04:29 +00:00
|
|
|
#include <assimp/ai_assert.h>
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
using namespace Assimp;
|
|
|
|
|
|
|
|
// CHAR_BIT seems to be defined under MVSC, but not under GCC. Pray that the correct value is 8.
|
|
|
|
#ifndef CHAR_BIT
|
2020-04-26 06:59:52 +00:00
|
|
|
#define CHAR_BIT 8
|
2015-05-19 03:48:29 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-26 17:14:30 +00:00
|
|
|
const aiVector3D PlaneInit(0.8523f, 0.34321f, 0.5736f);
|
2020-02-16 14:37:20 +00:00
|
|
|
|
2020-04-26 17:14:30 +00:00
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
// Constructs a spatially sorted representation from the given position array.
|
|
|
|
// define the reference plane. We choose some arbitrary vector away from all basic axises
|
|
|
|
// in the hope that no model spreads all its vertices along this plane.
|
|
|
|
SpatialSort::SpatialSort(const aiVector3D *pPositions, unsigned int pNumPositions, unsigned int pElementOffset) :
|
|
|
|
mPlaneNormal(PlaneInit) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mPlaneNormal.Normalize();
|
2020-04-26 06:59:52 +00:00
|
|
|
Fill(pPositions, pNumPositions, pElementOffset);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
SpatialSort::SpatialSort() :
|
|
|
|
mPlaneNormal(PlaneInit) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mPlaneNormal.Normalize();
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Destructor
|
2020-04-26 06:59:52 +00:00
|
|
|
SpatialSort::~SpatialSort() {
|
|
|
|
// empty
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
void SpatialSort::Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
|
|
|
|
unsigned int pElementOffset,
|
|
|
|
bool pFinalize /*= true */) {
|
2015-05-19 03:57:13 +00:00
|
|
|
mPositions.clear();
|
2020-04-26 06:59:52 +00:00
|
|
|
Append(pPositions, pNumPositions, pElementOffset, pFinalize);
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
void SpatialSort::Finalize() {
|
|
|
|
std::sort(mPositions.begin(), mPositions.end());
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
void SpatialSort::Append(const aiVector3D *pPositions, unsigned int pNumPositions,
|
|
|
|
unsigned int pElementOffset,
|
|
|
|
bool pFinalize /*= true */) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// store references to all given positions along with their distance to the reference plane
|
|
|
|
const size_t initial = mPositions.size();
|
2020-04-26 06:59:52 +00:00
|
|
|
mPositions.reserve(initial + (pFinalize ? pNumPositions : pNumPositions * 2));
|
|
|
|
for (unsigned int a = 0; a < pNumPositions; a++) {
|
|
|
|
const char *tempPointer = reinterpret_cast<const char *>(pPositions);
|
|
|
|
const aiVector3D *vec = reinterpret_cast<const aiVector3D *>(tempPointer + a * pElementOffset);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// store position by index and distance
|
2016-07-15 02:53:49 +00:00
|
|
|
ai_real distance = *vec * mPlaneNormal;
|
2020-04-26 06:59:52 +00:00
|
|
|
mPositions.push_back(Entry(static_cast<unsigned int>(a + initial), *vec, distance));
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pFinalize) {
|
|
|
|
// now sort the array ascending by distance.
|
|
|
|
Finalize();
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
// Returns an iterator for all positions close to the given position.
|
2020-04-26 06:59:52 +00:00
|
|
|
void SpatialSort::FindPositions(const aiVector3D &pPosition,
|
|
|
|
ai_real pRadius, std::vector<unsigned int> &poResults) const {
|
2016-07-15 02:53:49 +00:00
|
|
|
const ai_real dist = pPosition * mPlaneNormal;
|
|
|
|
const ai_real minDist = dist - pRadius, maxDist = dist + pRadius;
|
2015-05-19 03:48:29 +00:00
|
|
|
|
2017-06-06 09:37:02 +00:00
|
|
|
// clear the array
|
|
|
|
poResults.clear();
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// quick check for positions outside the range
|
2020-04-26 06:59:52 +00:00
|
|
|
if (mPositions.size() == 0)
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2020-04-26 06:59:52 +00:00
|
|
|
if (maxDist < mPositions.front().mDistance)
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
2020-04-26 06:59:52 +00:00
|
|
|
if (minDist > mPositions.back().mDistance)
|
2015-05-19 03:57:13 +00:00
|
|
|
return;
|
|
|
|
|
|
|
|
// do a binary search for the minimal distance to start the iteration there
|
|
|
|
unsigned int index = (unsigned int)mPositions.size() / 2;
|
|
|
|
unsigned int binaryStepSize = (unsigned int)mPositions.size() / 4;
|
2020-04-26 06:59:52 +00:00
|
|
|
while (binaryStepSize > 1) {
|
|
|
|
if (mPositions[index].mDistance < minDist)
|
2015-05-19 03:57:13 +00:00
|
|
|
index += binaryStepSize;
|
|
|
|
else
|
|
|
|
index -= binaryStepSize;
|
|
|
|
|
|
|
|
binaryStepSize /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// depending on the direction of the last step we need to single step a bit back or forth
|
|
|
|
// to find the actual beginning element of the range
|
2020-04-26 06:59:52 +00:00
|
|
|
while (index > 0 && mPositions[index].mDistance > minDist)
|
2015-05-19 03:57:13 +00:00
|
|
|
index--;
|
2020-04-26 06:59:52 +00:00
|
|
|
while (index < (mPositions.size() - 1) && mPositions[index].mDistance < minDist)
|
2015-05-19 03:57:13 +00:00
|
|
|
index++;
|
|
|
|
|
|
|
|
// Mow start iterating from there until the first position lays outside of the distance range.
|
|
|
|
// Add all positions inside the distance range within the given radius to the result aray
|
|
|
|
std::vector<Entry>::const_iterator it = mPositions.begin() + index;
|
2020-04-26 06:59:52 +00:00
|
|
|
const ai_real pSquared = pRadius * pRadius;
|
|
|
|
while (it->mDistance < maxDist) {
|
|
|
|
if ((it->mPosition - pPosition).SquareLength() < pSquared)
|
|
|
|
poResults.push_back(it->mIndex);
|
2015-05-19 03:57:13 +00:00
|
|
|
++it;
|
2020-04-26 06:59:52 +00:00
|
|
|
if (it == mPositions.end())
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// that's it
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-04-26 06:59:52 +00:00
|
|
|
// Binary, signed-integer representation of a single-precision floating-point value.
|
|
|
|
// IEEE 754 says: "If two floating-point numbers in the same format are ordered then they are
|
|
|
|
// ordered the same way when their bits are reinterpreted as sign-magnitude integers."
|
|
|
|
// This allows us to convert all floating-point numbers to signed integers of arbitrary size
|
|
|
|
// and then use them to work with ULPs (Units in the Last Place, for high-precision
|
|
|
|
// computations) or to compare them (integer comparisons are faster than floating-point
|
|
|
|
// comparisons on many platforms).
|
|
|
|
typedef ai_int BinFloat;
|
|
|
|
|
|
|
|
// --------------------------------------------------------------------------------------------
|
|
|
|
// Converts the bit pattern of a floating-point number to its signed integer representation.
|
|
|
|
BinFloat ToBinary(const ai_real &pValue) {
|
|
|
|
|
|
|
|
// If this assertion fails, signed int is not big enough to store a float on your platform.
|
|
|
|
// Please correct the declaration of BinFloat a few lines above - but do it in a portable,
|
|
|
|
// #ifdef'd manner!
|
|
|
|
static_assert(sizeof(BinFloat) >= sizeof(ai_real), "sizeof(BinFloat) >= sizeof(ai_real)");
|
|
|
|
|
|
|
|
#if defined(_MSC_VER)
|
|
|
|
// If this assertion fails, Visual C++ has finally moved to ILP64. This means that this
|
|
|
|
// code has just become legacy code! Find out the current value of _MSC_VER and modify
|
|
|
|
// the #if above so it evaluates false on the current and all upcoming VC versions (or
|
|
|
|
// on the current platform, if LP64 or LLP64 are still used on other platforms).
|
|
|
|
static_assert(sizeof(BinFloat) == sizeof(ai_real), "sizeof(BinFloat) == sizeof(ai_real)");
|
|
|
|
|
|
|
|
// This works best on Visual C++, but other compilers have their problems with it.
|
|
|
|
const BinFloat binValue = reinterpret_cast<BinFloat const &>(pValue);
|
|
|
|
//::memcpy(&binValue, &pValue, sizeof(pValue));
|
|
|
|
//return binValue;
|
|
|
|
#else
|
|
|
|
// On many compilers, reinterpreting a float address as an integer causes aliasing
|
|
|
|
// problems. This is an ugly but more or less safe way of doing it.
|
|
|
|
union {
|
|
|
|
ai_real asFloat;
|
|
|
|
BinFloat asBin;
|
|
|
|
} conversion;
|
|
|
|
conversion.asBin = 0; // zero empty space in case sizeof(BinFloat) > sizeof(float)
|
|
|
|
conversion.asFloat = pValue;
|
|
|
|
const BinFloat binValue = conversion.asBin;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// floating-point numbers are of sign-magnitude format, so find out what signed number
|
|
|
|
// representation we must convert negative values to.
|
|
|
|
// See http://en.wikipedia.org/wiki/Signed_number_representations.
|
|
|
|
|
|
|
|
// Two's complement?
|
2020-04-26 17:14:30 +00:00
|
|
|
bool DefaultValue = ((-42 == (~42 + 1)) && (binValue & 0x80000000));
|
|
|
|
bool OneComplement = ((-42 == ~42) && (binValue & 0x80000000));
|
|
|
|
bool SignedMagnitude = ((-42 == (42 | (-0))) && (binValue & 0x80000000));
|
|
|
|
|
|
|
|
if (DefaultValue)
|
2020-04-26 06:59:52 +00:00
|
|
|
return BinFloat(1 << (CHAR_BIT * sizeof(BinFloat) - 1)) - binValue;
|
|
|
|
// One's complement?
|
2020-04-26 17:14:30 +00:00
|
|
|
else if (OneComplement)
|
2020-04-26 06:59:52 +00:00
|
|
|
return BinFloat(-0) - binValue;
|
|
|
|
// Sign-magnitude?
|
2020-04-26 17:14:30 +00:00
|
|
|
else if (SignedMagnitude) // -0 = 1000... binary
|
|
|
|
return binValue;
|
|
|
|
else
|
2020-04-26 06:59:52 +00:00
|
|
|
return binValue;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2016-04-03 00:38:00 +00:00
|
|
|
// Fills an array with indices of all positions identical to the given position. In opposite to
|
2015-05-19 03:48:29 +00:00
|
|
|
// FindPositions(), not an epsilon is used but a (very low) tolerance of four floating-point units.
|
2020-04-26 06:59:52 +00:00
|
|
|
void SpatialSort::FindIdenticalPositions(const aiVector3D &pPosition, std::vector<unsigned int> &poResults) const {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Epsilons have a huge disadvantage: they are of constant precision, while floating-point
|
|
|
|
// values are of log2 precision. If you apply e=0.01 to 100, the epsilon is rather small, but
|
|
|
|
// if you apply it to 0.001, it is enormous.
|
|
|
|
|
|
|
|
// The best way to overcome this is the unit in the last place (ULP). A precision of 2 ULPs
|
|
|
|
// tells us that a float does not differ more than 2 bits from the "real" value. ULPs are of
|
2017-01-08 16:41:10 +00:00
|
|
|
// logarithmic precision - around 1, they are 1*(2^24) and around 10000, they are 0.00125.
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// For standard C math, we can assume a precision of 0.5 ULPs according to IEEE 754. The
|
|
|
|
// incoming vertex positions might have already been transformed, probably using rather
|
|
|
|
// inaccurate SSE instructions, so we assume a tolerance of 4 ULPs to safely identify
|
|
|
|
// identical vertex positions.
|
|
|
|
static const int toleranceInULPs = 4;
|
|
|
|
// An interesting point is that the inaccuracy grows linear with the number of operations:
|
|
|
|
// multiplying to numbers, each inaccurate to four ULPs, results in an inaccuracy of four ULPs
|
|
|
|
// plus 0.5 ULPs for the multiplication.
|
|
|
|
// To compute the distance to the plane, a dot product is needed - that is a multiplication and
|
|
|
|
// an addition on each number.
|
|
|
|
static const int distanceToleranceInULPs = toleranceInULPs + 1;
|
|
|
|
// The squared distance between two 3D vectors is computed the same way, but with an additional
|
|
|
|
// subtraction.
|
|
|
|
static const int distance3DToleranceInULPs = distanceToleranceInULPs + 1;
|
|
|
|
|
|
|
|
// 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.
|
2020-04-26 06:59:52 +00:00
|
|
|
const BinFloat minDistBinary = ToBinary(pPosition * mPlaneNormal) - distanceToleranceInULPs;
|
2015-05-19 03:57:13 +00:00
|
|
|
const BinFloat maxDistBinary = minDistBinary + 2 * distanceToleranceInULPs;
|
|
|
|
|
|
|
|
// clear the array in this strange fashion because a simple clear() would also deallocate
|
2015-05-19 03:48:29 +00:00
|
|
|
// the array which we want to avoid
|
2020-04-26 06:59:52 +00:00
|
|
|
poResults.resize(0);
|
2015-05-19 03:57:13 +00:00
|
|
|
|
|
|
|
// do a binary search for the minimal distance to start the iteration there
|
|
|
|
unsigned int index = (unsigned int)mPositions.size() / 2;
|
|
|
|
unsigned int binaryStepSize = (unsigned int)mPositions.size() / 4;
|
2020-04-26 06:59:52 +00:00
|
|
|
while (binaryStepSize > 1) {
|
2015-05-19 03:57:13 +00:00
|
|
|
// Ugly, but conditional jumps are faster with integers than with floats
|
2020-04-26 06:59:52 +00:00
|
|
|
if (minDistBinary > ToBinary(mPositions[index].mDistance))
|
2015-05-19 03:57:13 +00:00
|
|
|
index += binaryStepSize;
|
|
|
|
else
|
|
|
|
index -= binaryStepSize;
|
|
|
|
|
|
|
|
binaryStepSize /= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
// depending on the direction of the last step we need to single step a bit back or forth
|
|
|
|
// to find the actual beginning element of the range
|
2020-04-26 06:59:52 +00:00
|
|
|
while (index > 0 && minDistBinary < ToBinary(mPositions[index].mDistance))
|
2015-05-19 03:57:13 +00:00
|
|
|
index--;
|
2020-04-26 06:59:52 +00:00
|
|
|
while (index < (mPositions.size() - 1) && minDistBinary > ToBinary(mPositions[index].mDistance))
|
2015-05-19 03:57:13 +00:00
|
|
|
index++;
|
|
|
|
|
|
|
|
// Now start iterating from there until the first position lays outside of the distance range.
|
2018-02-13 19:36:22 +00:00
|
|
|
// Add all positions inside the distance range within the tolerance to the result array
|
2015-05-19 03:57:13 +00:00
|
|
|
std::vector<Entry>::const_iterator it = mPositions.begin() + index;
|
2020-04-26 06:59:52 +00:00
|
|
|
while (ToBinary(it->mDistance) < maxDistBinary) {
|
|
|
|
if (distance3DToleranceInULPs >= ToBinary((it->mPosition - pPosition).SquareLength()))
|
2015-05-19 03:57:13 +00:00
|
|
|
poResults.push_back(it->mIndex);
|
|
|
|
++it;
|
2020-04-26 06:59:52 +00:00
|
|
|
if (it == mPositions.end())
|
2015-05-19 03:57:13 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// that's it
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
2020-04-26 06:59:52 +00:00
|
|
|
unsigned int SpatialSort::GenerateMappingTable(std::vector<unsigned int> &fill, ai_real pRadius) const {
|
|
|
|
fill.resize(mPositions.size(), UINT_MAX);
|
2016-07-15 02:53:49 +00:00
|
|
|
ai_real dist, maxDist;
|
2015-05-19 03:57:13 +00:00
|
|
|
|
2020-04-26 06:59:52 +00:00
|
|
|
unsigned int t = 0;
|
|
|
|
const ai_real pSquared = pRadius * pRadius;
|
2015-05-19 03:57:13 +00:00
|
|
|
for (size_t i = 0; i < mPositions.size();) {
|
|
|
|
dist = mPositions[i].mPosition * mPlaneNormal;
|
|
|
|
maxDist = dist + pRadius;
|
|
|
|
|
|
|
|
fill[mPositions[i].mIndex] = t;
|
2020-04-26 06:59:52 +00:00
|
|
|
const aiVector3D &oldpos = mPositions[i].mPosition;
|
|
|
|
for (++i; i < fill.size() && mPositions[i].mDistance < maxDist && (mPositions[i].mPosition - oldpos).SquareLength() < pSquared; ++i) {
|
2015-05-19 03:57:13 +00:00
|
|
|
fill[mPositions[i].mIndex] = t;
|
|
|
|
}
|
|
|
|
++t;
|
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#ifdef ASSIMP_BUILD_DEBUG
|
|
|
|
|
2015-05-19 03:57:13 +00:00
|
|
|
// debug invariant: mPositions[i].mIndex values must range from 0 to mPositions.size()-1
|
|
|
|
for (size_t i = 0; i < fill.size(); ++i) {
|
2020-04-26 06:59:52 +00:00
|
|
|
ai_assert(fill[i] < mPositions.size());
|
2015-05-19 03:57:13 +00:00
|
|
|
}
|
2015-05-19 03:48:29 +00:00
|
|
|
|
|
|
|
#endif
|
2015-05-19 03:57:13 +00:00
|
|
|
return t;
|
2015-05-19 03:48:29 +00:00
|
|
|
}
|