SGSpatialSort.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. ---------------------------------------------------------------------------
  3. Open Asset Import Library (assimp)
  4. ---------------------------------------------------------------------------
  5. Copyright (c) 2006-2025, assimp team
  6. All rights reserved.
  7. Redistribution and use of this software in source and binary forms,
  8. with or without modification, are permitted provided that the following
  9. conditions are met:
  10. * Redistributions of source code must retain the above
  11. copyright notice, this list of conditions and the
  12. following disclaimer.
  13. * Redistributions in binary form must reproduce the above
  14. copyright notice, this list of conditions and the
  15. following disclaimer in the documentation and/or other
  16. materials provided with the distribution.
  17. * Neither the name of the assimp team, nor the names of its
  18. contributors may be used to endorse or promote products
  19. derived from this software without specific prior
  20. written permission of the assimp team.
  21. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. ---------------------------------------------------------------------------
  33. */
  34. /** @file Implementation of the helper class to quickly find
  35. vertices close to a given position. Special implementation for
  36. the 3ds loader handling smooth groups correctly */
  37. #include <assimp/SGSpatialSort.h>
  38. using namespace Assimp;
  39. // ------------------------------------------------------------------------------------------------
  40. SGSpatialSort::SGSpatialSort() {
  41. // define the reference plane. We choose some arbitrary vector away from all basic axes
  42. // in the hope that no model spreads all its vertices along this plane.
  43. mPlaneNormal.Set( 0.8523f, 0.34321f, 0.5736f);
  44. mPlaneNormal.Normalize();
  45. }
  46. // ------------------------------------------------------------------------------------------------
  47. void SGSpatialSort::Add(const aiVector3D& vPosition, unsigned int index,
  48. unsigned int smoothingGroup)
  49. {
  50. // store position by index and distance
  51. float distance = vPosition * mPlaneNormal;
  52. mPositions.emplace_back( index, vPosition,
  53. distance, smoothingGroup);
  54. }
  55. // ------------------------------------------------------------------------------------------------
  56. void SGSpatialSort::Prepare()
  57. {
  58. // now sort the array ascending by distance.
  59. std::sort( this->mPositions.begin(), this->mPositions.end());
  60. }
  61. // ------------------------------------------------------------------------------------------------
  62. // Returns an iterator for all positions close to the given position.
  63. void SGSpatialSort::FindPositions( const aiVector3D& pPosition,
  64. uint32_t pSG,
  65. float pRadius,
  66. std::vector<unsigned int>& poResults,
  67. bool exactMatch /*= false*/) const
  68. {
  69. float dist = pPosition * mPlaneNormal;
  70. float minDist = dist - pRadius, maxDist = dist + pRadius;
  71. // clear the array
  72. poResults.clear();
  73. // quick check for positions outside the range
  74. if( mPositions.empty() )
  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 = (unsigned int)mPositions.size() / 2;
  82. unsigned int binaryStepSize = (unsigned int)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 array
  99. float squareEpsilon = pRadius * pRadius;
  100. std::vector<Entry>::const_iterator it = mPositions.begin() + index;
  101. std::vector<Entry>::const_iterator end = mPositions.end();
  102. if (exactMatch)
  103. {
  104. while( it->mDistance < maxDist)
  105. {
  106. if((it->mPosition - pPosition).SquareLength() < squareEpsilon && it->mSmoothGroups == pSG)
  107. {
  108. poResults.push_back( it->mIndex);
  109. }
  110. ++it;
  111. if( end == it )break;
  112. }
  113. }
  114. else
  115. {
  116. // if the given smoothing group is 0, we'll return all surrounding vertices
  117. if (!pSG)
  118. {
  119. while( it->mDistance < maxDist)
  120. {
  121. if((it->mPosition - pPosition).SquareLength() < squareEpsilon)
  122. poResults.push_back( it->mIndex);
  123. ++it;
  124. if( end == it)break;
  125. }
  126. }
  127. else while( it->mDistance < maxDist)
  128. {
  129. if((it->mPosition - pPosition).SquareLength() < squareEpsilon &&
  130. (it->mSmoothGroups & pSG || !it->mSmoothGroups))
  131. {
  132. poResults.push_back( it->mIndex);
  133. }
  134. ++it;
  135. if( end == it)break;
  136. }
  137. }
  138. }