2
0

3DSSpatialSort.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /** @file Implementation of the helper class to quickly find vertices close to a given position */
  2. #include <algorithm>
  3. #include "3DSSpatialSort.h"
  4. using namespace Assimp;
  5. using namespace Assimp::Dot3DS;
  6. // ------------------------------------------------------------------------------------------------
  7. // Constructs a spatially sorted representation from the given position array.
  8. D3DSSpatialSorter::D3DSSpatialSorter( const aiVector3D* pPositions,
  9. unsigned int pNumPositions, unsigned int pElementOffset)
  10. {
  11. // define the reference plane. We choose some arbitrary vector away from all basic axises
  12. // in the hope that no model spreads all its vertices along this plane.
  13. mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f);
  14. mPlaneNormal.Normalize();
  15. // store references to all given positions along with their distance to the reference plane
  16. mPositions.reserve( pNumPositions);
  17. for( unsigned int a = 0; a < pNumPositions; a++)
  18. {
  19. const char* tempPointer = reinterpret_cast<const char*> (pPositions);
  20. const aiVector3D* vec = reinterpret_cast<const aiVector3D*> (tempPointer + a * pElementOffset);
  21. // store position by index and distance
  22. float distance = *vec * mPlaneNormal;
  23. mPositions.push_back( Entry( a, *vec, distance,0));
  24. }
  25. // now sort the array ascending by distance.
  26. std::sort( mPositions.begin(), mPositions.end());
  27. }
  28. // ------------------------------------------------------------------------------------------------
  29. D3DSSpatialSorter::D3DSSpatialSorter( const Dot3DS::Mesh* p_pcMesh)
  30. {
  31. // define the reference plane. We choose some arbitrary vector away from all basic axises
  32. // in the hope that no model spreads all its vertices along this plane.
  33. mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f);
  34. mPlaneNormal.Normalize();
  35. // store references to all given positions along with their distance to the reference plane
  36. mPositions.reserve( p_pcMesh->mPositions.size());
  37. for( std::vector<Dot3DS::Face>::const_iterator
  38. i = p_pcMesh->mFaces.begin();
  39. i != p_pcMesh->mFaces.end();++i)
  40. {
  41. // store position by index and distance
  42. float distance = p_pcMesh->mPositions[(*i).i1] * mPlaneNormal;
  43. mPositions.push_back( Entry( (*i).i1, p_pcMesh->mPositions[(*i).i1],
  44. distance, (*i).iSmoothGroup));
  45. // triangle vertex 2
  46. distance = p_pcMesh->mPositions[(*i).i2] * mPlaneNormal;
  47. mPositions.push_back( Entry( (*i).i2, p_pcMesh->mPositions[(*i).i2],
  48. distance, (*i).iSmoothGroup));
  49. // triangle vertex 3
  50. distance = p_pcMesh->mPositions[(*i).i3] * mPlaneNormal;
  51. mPositions.push_back( Entry( (*i).i3, p_pcMesh->mPositions[(*i).i3],
  52. distance, (*i).iSmoothGroup));
  53. }
  54. // now sort the array ascending by distance.
  55. std::sort( this->mPositions.begin(), this->mPositions.end());
  56. }
  57. // ------------------------------------------------------------------------------------------------
  58. // Destructor
  59. D3DSSpatialSorter::~D3DSSpatialSorter()
  60. {
  61. // nothing to do here, everything destructs automatically
  62. }
  63. // ------------------------------------------------------------------------------------------------
  64. // Returns an iterator for all positions close to the given position.
  65. void D3DSSpatialSorter::FindPositions( const aiVector3D& pPosition,
  66. uint32_t pSG,float pRadius, std::vector<unsigned int>& poResults) const
  67. {
  68. float dist = pPosition * mPlaneNormal;
  69. float minDist = dist - pRadius, maxDist = dist + pRadius;
  70. // clear the array in this strange fashion because a simple clear() would also deallocate
  71. // the array which we want to avoid
  72. poResults.erase( poResults.begin(), poResults.end());
  73. // quick check for positions outside the range
  74. if( mPositions.size() == 0)
  75. return;
  76. if( maxDist < mPositions.front().mDistance)
  77. return;
  78. if( minDist > mPositions.back().mDistance)
  79. return;
  80. // do a binary search for the minimal distance to start the iteration there
  81. unsigned int index = mPositions.size() / 2;
  82. unsigned int binaryStepSize = mPositions.size() / 4;
  83. while( binaryStepSize > 1)
  84. {
  85. if( mPositions[index].mDistance < minDist)
  86. index += binaryStepSize;
  87. else
  88. index -= binaryStepSize;
  89. binaryStepSize /= 2;
  90. }
  91. // depending on the direction of the last step we need to single step a bit back or forth
  92. // to find the actual beginning element of the range
  93. while( index > 0 && mPositions[index].mDistance > minDist)
  94. index--;
  95. while( index < (mPositions.size() - 1) && mPositions[index].mDistance < minDist)
  96. index++;
  97. // Mow start iterating from there until the first position lays outside of the distance range.
  98. // Add all positions inside the distance range within the given radius to the result aray
  99. if (0 == pSG)
  100. {
  101. std::vector<Entry>::const_iterator it = mPositions.begin() + index;
  102. float squareEpsilon = pRadius * pRadius;
  103. while( it->mDistance < maxDist)
  104. {
  105. if((it->mPosition - pPosition).SquareLength() < squareEpsilon)
  106. {
  107. poResults.push_back( it->mIndex);
  108. }
  109. ++it;
  110. if( it == mPositions.end())
  111. break;
  112. }
  113. }
  114. else
  115. {
  116. std::vector<Entry>::const_iterator it = mPositions.begin() + index;
  117. float squareEpsilon = pRadius * pRadius;
  118. while( it->mDistance < maxDist)
  119. {
  120. if((it->mPosition - pPosition).SquareLength() < squareEpsilon &&
  121. (it->mSmoothGroups & pSG || 0 == it->mSmoothGroups))
  122. {
  123. poResults.push_back( it->mIndex);
  124. }
  125. ++it;
  126. if( it == mPositions.end())
  127. break;
  128. }
  129. }
  130. }