SGSpatialSort.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. Open Asset Import Library (assimp)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2016, 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 optimize finding vertices close to a given location
  34. */
  35. #ifndef AI_D3DSSPATIALSORT_H_INC
  36. #define AI_D3DSSPATIALSORT_H_INC
  37. #include <assimp/types.h>
  38. #include <vector>
  39. #include <stdint.h>
  40. namespace Assimp {
  41. // ----------------------------------------------------------------------------------
  42. /** Specialized version of SpatialSort to support smoothing groups
  43. * This is used in by the 3DS, ASE and LWO loaders. 3DS and ASE share their
  44. * normal computation code in SmoothingGroups.inl, the LWO loader has its own
  45. * implementation to handle all details of its file format correctly.
  46. */
  47. // ----------------------------------------------------------------------------------
  48. class SGSpatialSort
  49. {
  50. public:
  51. SGSpatialSort();
  52. // -------------------------------------------------------------------
  53. /** Construction from a given face array, handling smoothing groups
  54. * properly
  55. */
  56. explicit SGSpatialSort(const std::vector<aiVector3D>& vPositions);
  57. // -------------------------------------------------------------------
  58. /** Add a vertex to the spatial sort
  59. * @param vPosition Vertex position to be added
  60. * @param index Index of the vrtex
  61. * @param smoothingGroup SmoothingGroup for this vertex
  62. */
  63. void Add(const aiVector3D& vPosition, unsigned int index,
  64. unsigned int smoothingGroup);
  65. // -------------------------------------------------------------------
  66. /** Prepare the spatial sorter for use. This step runs in O(logn)
  67. */
  68. void Prepare();
  69. /** Destructor */
  70. ~SGSpatialSort();
  71. // -------------------------------------------------------------------
  72. /** Returns an iterator for all positions close to the given position.
  73. * @param pPosition The position to look for vertices.
  74. * @param pSG Only included vertices with at least one shared smooth group
  75. * @param pRadius Maximal distance from the position a vertex may have
  76. * to be counted in.
  77. * @param poResults The container to store the indices of the found
  78. * positions. Will be emptied by the call so it may contain anything.
  79. * @param exactMatch Specifies whether smoothing groups are bit masks
  80. * (false) or integral values (true). In the latter case, a vertex
  81. * cannot belong to more than one smoothing group.
  82. * @return An iterator to iterate over all vertices in the given area.
  83. */
  84. // -------------------------------------------------------------------
  85. void FindPositions( const aiVector3D& pPosition, uint32_t pSG,
  86. float pRadius, std::vector<unsigned int>& poResults,
  87. bool exactMatch = false) const;
  88. protected:
  89. /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
  90. aiVector3D mPlaneNormal;
  91. // -------------------------------------------------------------------
  92. /** An entry in a spatially sorted position array. Consists of a
  93. * vertex index, its position and its precalculated distance from
  94. * the reference plane */
  95. // -------------------------------------------------------------------
  96. struct Entry
  97. {
  98. unsigned int mIndex; ///< The vertex referred by this entry
  99. aiVector3D mPosition; ///< Position
  100. uint32_t mSmoothGroups;
  101. float mDistance; ///< Distance of this vertex to the sorting plane
  102. Entry() { /** intentionally not initialized.*/ }
  103. Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance,uint32_t pSG)
  104. :
  105. mIndex( pIndex),
  106. mPosition( pPosition),
  107. mSmoothGroups (pSG),
  108. mDistance( pDistance)
  109. { }
  110. bool operator < (const Entry& e) const { return mDistance < e.mDistance; }
  111. };
  112. // all positions, sorted by distance to the sorting plane
  113. std::vector<Entry> mPositions;
  114. };
  115. } // end of namespace Assimp
  116. #endif // AI_SPATIALSORT_H_INC