SpatialSort.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2025, assimp team
  5. All rights reserved.
  6. Redistribution and use of this software in source and binary forms,
  7. with or without modification, are permitted provided that the
  8. following conditions are met:
  9. * Redistributions of source code must retain the above
  10. copyright notice, this list of conditions and the
  11. following disclaimer.
  12. * Redistributions in binary form must reproduce the above
  13. copyright notice, this list of conditions and the
  14. following disclaimer in the documentation and/or other
  15. materials provided with the distribution.
  16. * Neither the name of the assimp team, nor the names of its
  17. contributors may be used to endorse or promote products
  18. derived from this software without specific prior
  19. written permission of the assimp team.
  20. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. ----------------------------------------------------------------------
  32. */
  33. /** Small helper classes to optimise finding vertizes close to a given location */
  34. #pragma once
  35. #ifndef AI_SPATIALSORT_H_INC
  36. #define AI_SPATIALSORT_H_INC
  37. #ifdef __GNUC__
  38. #pragma GCC system_header
  39. #endif
  40. #include <assimp/types.h>
  41. #include <vector>
  42. #include <limits>
  43. namespace Assimp {
  44. // ------------------------------------------------------------------------------------------------
  45. /** A little helper class to quickly find all vertices in the epsilon environment of a given
  46. * position. Construct an instance with an array of positions. The class stores the given positions
  47. * by their indices and sorts them by their distance to an arbitrary chosen plane.
  48. * You can then query the instance for all vertices close to a given position in an average O(log n)
  49. * time, with O(n) worst case complexity when all vertices lay on the plane. The plane is chosen
  50. * so that it avoids common planes in usual data sets. */
  51. // ------------------------------------------------------------------------------------------------
  52. class ASSIMP_API SpatialSort {
  53. public:
  54. SpatialSort();
  55. // ------------------------------------------------------------------------------------
  56. /** Constructs a spatially sorted representation from the given position array.
  57. * Supply the positions in its layout in memory, the class will only refer to them
  58. * by index.
  59. * @param pPositions Pointer to the first position vector of the array.
  60. * @param pNumPositions Number of vectors to expect in that array.
  61. * @param pElementOffset Offset in bytes from the beginning of one vector in memory
  62. * to the beginning of the next vector. */
  63. SpatialSort(const aiVector3D *pPositions, unsigned int pNumPositions,
  64. unsigned int pElementOffset);
  65. /** Destructor */
  66. ~SpatialSort() = default;
  67. // ------------------------------------------------------------------------------------
  68. /** Sets the input data for the SpatialSort. This replaces existing data, if any.
  69. * The new data receives new indices in ascending order.
  70. *
  71. * @param pPositions Pointer to the first position vector of the array.
  72. * @param pNumPositions Number of vectors to expect in that array.
  73. * @param pElementOffset Offset in bytes from the beginning of one vector in memory
  74. * to the beginning of the next vector.
  75. * @param pFinalize Specifies whether the SpatialSort's internal representation
  76. * is finalized after the new data has been added. Finalization is
  77. * required in order to use #FindPosition() or #GenerateMappingTable().
  78. * If you don't finalize yet, you can use #Append() to add data from
  79. * other sources.*/
  80. void Fill(const aiVector3D *pPositions, unsigned int pNumPositions,
  81. unsigned int pElementOffset,
  82. bool pFinalize = true);
  83. // ------------------------------------------------------------------------------------
  84. /** Same as #Fill(), except the method appends to existing data in the #SpatialSort. */
  85. void Append(const aiVector3D *pPositions, unsigned int pNumPositions,
  86. unsigned int pElementOffset,
  87. bool pFinalize = true);
  88. // ------------------------------------------------------------------------------------
  89. /** Finalize the spatial hash data structure. This can be useful after
  90. * multiple calls to #Append() with the pFinalize parameter set to false.
  91. * This is finally required before one of #FindPositions() and #GenerateMappingTable()
  92. * can be called to query the spatial sort.*/
  93. void Finalize();
  94. // ------------------------------------------------------------------------------------
  95. /** Returns an iterator for all positions close to the given position.
  96. * @param pPosition The position to look for vertices.
  97. * @param pRadius Maximal distance from the position a vertex may have to be counted in.
  98. * @param poResults The container to store the indices of the found positions.
  99. * Will be emptied by the call so it may contain anything.
  100. * @return An iterator to iterate over all vertices in the given area.*/
  101. void FindPositions(const aiVector3D &pPosition, ai_real pRadius,
  102. std::vector<unsigned int> &poResults) const;
  103. // ------------------------------------------------------------------------------------
  104. /** Fills an array with indices of all positions identical to the given position. In
  105. * opposite to FindPositions(), not an epsilon is used but a (very low) tolerance of
  106. * four floating-point units.
  107. * @param pPosition The position to look for vertices.
  108. * @param poResults The container to store the indices of the found positions.
  109. * Will be emptied by the call so it may contain anything.*/
  110. void FindIdenticalPositions(const aiVector3D &pPosition,
  111. std::vector<unsigned int> &poResults) const;
  112. // ------------------------------------------------------------------------------------
  113. /** Compute a table that maps each vertex ID referring to a spatially close
  114. * enough position to the same output ID. Output IDs are assigned in ascending order
  115. * from 0...n.
  116. * @param fill Will be filled with numPositions entries.
  117. * @param pRadius Maximal distance from the position a vertex may have to
  118. * be counted in.
  119. * @return Number of unique vertices (n). */
  120. unsigned int GenerateMappingTable(std::vector<unsigned int> &fill,
  121. ai_real pRadius) const;
  122. protected:
  123. /** Return the distance to the sorting plane. */
  124. ai_real CalculateDistance(const aiVector3D &pPosition) const;
  125. protected:
  126. /** Normal of the sorting plane, normalized.
  127. */
  128. aiVector3D mPlaneNormal;
  129. /** The centroid of the positions, which is used as a point on the sorting plane
  130. * when calculating distance. This value is calculated in Finalize.
  131. */
  132. aiVector3D mCentroid;
  133. /** An entry in a spatially sorted position array. Consists of a vertex index,
  134. * its position and its pre-calculated distance from the reference plane */
  135. struct Entry {
  136. unsigned int mIndex; ///< The vertex referred by this entry
  137. aiVector3D mPosition; ///< Position
  138. /// Distance of this vertex to the sorting plane. This is set by Finalize.
  139. ai_real mDistance;
  140. Entry() AI_NO_EXCEPT
  141. : mIndex(std::numeric_limits<unsigned int>::max()),
  142. mPosition(),
  143. mDistance(std::numeric_limits<ai_real>::max()) {
  144. // empty
  145. }
  146. Entry(unsigned int pIndex, const aiVector3D &pPosition) :
  147. mIndex(pIndex), mPosition(pPosition), mDistance(std::numeric_limits<ai_real>::max()) {
  148. // empty
  149. }
  150. bool operator<(const Entry &e) const { return mDistance < e.mDistance; }
  151. };
  152. // all positions, sorted by distance to the sorting plane
  153. std::vector<Entry> mPositions;
  154. /// false until the Finalize method is called.
  155. bool mFinalized;
  156. };
  157. } // end of namespace Assimp
  158. #endif // AI_SPATIALSORT_H_INC