SGSpatialSort.h 5.7 KB

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