You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

171 lines
7.6 KiB

  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2012, 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. #ifndef AI_SPATIALSORT_H_INC
  35. #define AI_SPATIALSORT_H_INC
  36. #include <vector>
  37. #include "../include/assimp/types.h"
  38. namespace Assimp
  39. {
  40. // ------------------------------------------------------------------------------------------------
  41. /** A little helper class to quickly find all vertices in the epsilon environment of a given
  42. * position. Construct an instance with an array of positions. The class stores the given positions
  43. * by their indices and sorts them by their distance to an arbitrary chosen plane.
  44. * You can then query the instance for all vertices close to a given position in an average O(log n)
  45. * time, with O(n) worst case complexity when all vertices lay on the plane. The plane is chosen
  46. * so that it avoids common planes in usual data sets. */
  47. // ------------------------------------------------------------------------------------------------
  48. class SpatialSort
  49. {
  50. public:
  51. SpatialSort();
  52. // ------------------------------------------------------------------------------------
  53. /** Constructs a spatially sorted representation from the given position array.
  54. * Supply the positions in its layout in memory, the class will only refer to them
  55. * by index.
  56. * @param pPositions Pointer to the first position vector of the array.
  57. * @param pNumPositions Number of vectors to expect in that array.
  58. * @param pElementOffset Offset in bytes from the beginning of one vector in memory
  59. * to the beginning of the next vector. */
  60. SpatialSort( const aiVector3D* pPositions, unsigned int pNumPositions,
  61. unsigned int pElementOffset);
  62. /** Destructor */
  63. ~SpatialSort();
  64. public:
  65. // ------------------------------------------------------------------------------------
  66. /** Sets the input data for the SpatialSort. This replaces existing data, if any.
  67. * The new data receives new indices in ascending order.
  68. *
  69. * @param pPositions Pointer to the first position vector of the array.
  70. * @param pNumPositions Number of vectors to expect in that array.
  71. * @param pElementOffset Offset in bytes from the beginning of one vector in memory
  72. * to the beginning of the next vector.
  73. * @param pFinalize Specifies whether the SpatialSort's internal representation
  74. * is finalized after the new data has been added. Finalization is
  75. * required in order to use #FindPosition() or #GenerateMappingTable().
  76. * If you don't finalize yet, you can use #Append() to add data from
  77. * other sources.*/
  78. void Fill( const aiVector3D* pPositions, unsigned int pNumPositions,
  79. unsigned int pElementOffset,
  80. bool pFinalize = true);
  81. // ------------------------------------------------------------------------------------
  82. /** Same as #Fill(), except the method appends to existing data in the #SpatialSort. */
  83. void Append( const aiVector3D* pPositions, unsigned int pNumPositions,
  84. unsigned int pElementOffset,
  85. bool pFinalize = true);
  86. // ------------------------------------------------------------------------------------
  87. /** Finalize the spatial hash data structure. This can be useful after
  88. * multiple calls to #Append() with the pFinalize parameter set to false.
  89. * This is finally required before one of #FindPositions() and #GenerateMappingTable()
  90. * can be called to query the spatial sort.*/
  91. void Finalize();
  92. // ------------------------------------------------------------------------------------
  93. /** Returns an iterator for all positions close to the given position.
  94. * @param pPosition The position to look for vertices.
  95. * @param pRadius Maximal distance from the position a vertex may have to be counted in.
  96. * @param poResults The container to store the indices of the found positions.
  97. * Will be emptied by the call so it may contain anything.
  98. * @return An iterator to iterate over all vertices in the given area.*/
  99. void FindPositions( const aiVector3D& pPosition, float pRadius,
  100. std::vector<unsigned int>& poResults) const;
  101. // ------------------------------------------------------------------------------------
  102. /** Fills an array with indices of all positions indentical to the given position. In
  103. * opposite to FindPositions(), not an epsilon is used but a (very low) tolerance of
  104. * four floating-point units.
  105. * @param pPosition The position to look for vertices.
  106. * @param poResults The container to store the indices of the found positions.
  107. * Will be emptied by the call so it may contain anything.*/
  108. void FindIdenticalPositions( const aiVector3D& pPosition,
  109. std::vector<unsigned int>& poResults) const;
  110. // ------------------------------------------------------------------------------------
  111. /** Compute a table that maps each vertex ID referring to a spatially close
  112. * enough position to the same output ID. Output IDs are assigned in ascending order
  113. * from 0...n.
  114. * @param fill Will be filled with numPositions entries.
  115. * @param pRadius Maximal distance from the position a vertex may have to
  116. * be counted in.
  117. * @return Number of unique vertices (n). */
  118. unsigned int GenerateMappingTable(std::vector<unsigned int>& fill,
  119. float pRadius) const;
  120. protected:
  121. /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
  122. aiVector3D mPlaneNormal;
  123. /** An entry in a spatially sorted position array. Consists of a vertex index,
  124. * its position and its precalculated distance from the reference plane */
  125. struct Entry
  126. {
  127. unsigned int mIndex; ///< The vertex referred by this entry
  128. aiVector3D mPosition; ///< Position
  129. float mDistance; ///< Distance of this vertex to the sorting plane
  130. Entry() { /** intentionally not initialized.*/ }
  131. Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance)
  132. : mIndex( pIndex), mPosition( pPosition), mDistance( pDistance)
  133. { }
  134. bool operator < (const Entry& e) const { return mDistance < e.mDistance; }
  135. };
  136. // all positions, sorted by distance to the sorting plane
  137. std::vector<Entry> mPositions;
  138. };
  139. } // end of namespace Assimp
  140. #endif // AI_SPATIALSORT_H_INC