SGSpatialSort.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Open Asset Import Library (ASSIMP)
  3. ----------------------------------------------------------------------
  4. Copyright (c) 2006-2008, ASSIMP Development 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 Development 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. */
  35. #ifndef AI_D3DSSPATIALSORT_H_INC
  36. #define AI_D3DSSPATIALSORT_H_INC
  37. #include <vector>
  38. #include "../include/aiTypes.h"
  39. namespace Assimp
  40. {
  41. // ------------------------------------------------------------------------------------------------
  42. /** Specialized version of SpatialSort to support smoothing groups
  43. * This is used in the .3ds loader
  44. */
  45. class SGSpatialSort
  46. {
  47. public:
  48. SGSpatialSort();
  49. // -------------------------------------------------------------------
  50. /** Construction from a given face array, handling smoothing groups properly
  51. */
  52. SGSpatialSort(const std::vector<aiVector3D>& vPositions);
  53. // -------------------------------------------------------------------
  54. /** Add a vertex to the spatial sort
  55. * @param vPosition Vertex position to be added
  56. * @param index Index of the vrtex
  57. * @param smoothingGroup SmoothingGroup for this vertex
  58. */
  59. void Add(const aiVector3D& vPosition, unsigned int index,
  60. unsigned int smoothingGroup);
  61. // -------------------------------------------------------------------
  62. /** Prepare the spatial sorter for use
  63. */
  64. void Prepare();
  65. /** Destructor */
  66. ~SGSpatialSort();
  67. // -------------------------------------------------------------------
  68. /** Returns an iterator for all positions close to the given position.
  69. * @param pPosition The position to look for vertices.
  70. * @param pSG Only included vertices with at least one shared smooth group
  71. * @param pRadius Maximal distance from the position a vertex may have to be counted in.
  72. * @param poResults The container to store the indices of the found positions. Will be emptied
  73. * by the call so it may contain anything.
  74. * @return An iterator to iterate over all vertices in the given area.
  75. */
  76. void FindPositions( const aiVector3D& pPosition, uint32_t pSG,
  77. float pRadius, std::vector<unsigned int>& poResults) const;
  78. protected:
  79. /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
  80. aiVector3D mPlaneNormal;
  81. // -------------------------------------------------------------------
  82. /** An entry in a spatially sorted position array. Consists of a vertex index,
  83. * its position and its precalculated distance from the reference plane */
  84. struct Entry
  85. {
  86. unsigned int mIndex; ///< The vertex referred by this entry
  87. aiVector3D mPosition; ///< Position
  88. uint32_t mSmoothGroups;
  89. float mDistance; ///< Distance of this vertex to the sorting plane
  90. Entry() { /** intentionally not initialized.*/ }
  91. Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance,uint32_t pSG)
  92. :
  93. mIndex( pIndex),
  94. mPosition( pPosition),
  95. mSmoothGroups (pSG),
  96. mDistance( pDistance)
  97. { }
  98. bool operator < (const Entry& e) const { return mDistance < e.mDistance; }
  99. };
  100. // all positions, sorted by distance to the sorting plane
  101. std::vector<Entry> mPositions;
  102. };
  103. } // end of namespace Assimp
  104. #endif // AI_SPATIALSORT_H_INC