SpatialSort.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. /** @file Implementation of the helper class to quickly find vertices close to a given position */
  2. #include <algorithm>
  3. #include "SpatialSort.h"
  4. using namespace Assimp;
  5. // ------------------------------------------------------------------------------------------------
  6. // Constructs a spatially sorted representation from the given position array.
  7. SpatialSort::SpatialSort( const aiVector3D* pPositions, unsigned int pNumPositions, unsigned int pElementOffset)
  8. {
  9. // define the reference plane. We choose some arbitrary vector away from all basic axises
  10. // in the hope that no model spreads all its vertices along this plane.
  11. mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f);
  12. mPlaneNormal.Normalize();
  13. // store references to all given positions along with their distance to the reference plane
  14. mPositions.reserve( pNumPositions);
  15. for( unsigned int a = 0; a < pNumPositions; a++)
  16. {
  17. const char* tempPointer = reinterpret_cast<const char*> (pPositions);
  18. const aiVector3D* vec = reinterpret_cast<const aiVector3D*> (tempPointer + a * pElementOffset);
  19. // store position by index and distance
  20. float distance = *vec * mPlaneNormal;
  21. mPositions.push_back( Entry( a, *vec, distance));
  22. }
  23. // now sort the array ascending by distance.
  24. std::sort( mPositions.begin(), mPositions.end());
  25. }
  26. // ------------------------------------------------------------------------------------------------
  27. // Destructor
  28. SpatialSort::~SpatialSort()
  29. {
  30. // nothing to do here, everything destructs automatically
  31. }
  32. // ------------------------------------------------------------------------------------------------
  33. // Returns an iterator for all positions close to the given position.
  34. void SpatialSort::FindPositions( const aiVector3D& pPosition, float pRadius, std::vector<unsigned int>& poResults) const
  35. {
  36. float dist = pPosition * mPlaneNormal;
  37. float minDist = dist - pRadius, maxDist = dist + pRadius;
  38. // clear the array in this strange fashion because a simple clear() would also deallocate
  39. // the array which we want to avoid
  40. poResults.erase( poResults.begin(), poResults.end());
  41. // quick check for positions outside the range
  42. if( mPositions.size() == 0)
  43. return;
  44. if( maxDist < mPositions.front().mDistance)
  45. return;
  46. if( minDist > mPositions.back().mDistance)
  47. return;
  48. // do a binary search for the minimal distance to start the iteration there
  49. unsigned int index = mPositions.size() / 2;
  50. unsigned int binaryStepSize = mPositions.size() / 4;
  51. while( binaryStepSize > 1)
  52. {
  53. if( mPositions[index].mDistance < minDist)
  54. index += binaryStepSize;
  55. else
  56. index -= binaryStepSize;
  57. binaryStepSize /= 2;
  58. }
  59. // depending on the direction of the last step we need to single step a bit back or forth
  60. // to find the actual beginning element of the range
  61. while( index > 0 && mPositions[index].mDistance > minDist)
  62. index--;
  63. while( index < (mPositions.size() - 1) && mPositions[index].mDistance < minDist)
  64. index++;
  65. // Mow start iterating from there until the first position lays outside of the distance range.
  66. // Add all positions inside the distance range within the given radius to the result aray
  67. std::vector<Entry>::const_iterator it = mPositions.begin() + index;
  68. float squareEpsilon = pRadius * pRadius;
  69. while( it->mDistance < maxDist)
  70. {
  71. if( (it->mPosition - pPosition).SquareLength() < squareEpsilon)
  72. poResults.push_back( it->mIndex);
  73. ++it;
  74. if( it == mPositions.end())
  75. break;
  76. }
  77. // that's it
  78. }