3DSSpatialSort.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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/aiVector3D.h"
  39. #if (!defined AI_BUILD_NO_ASE_IMPORTER)
  40. # include "3DSHelper.h"
  41. #endif
  42. namespace Assimp
  43. {
  44. using namespace Dot3DS;
  45. // ------------------------------------------------------------------------------------------------
  46. /** Specialized version of SpatialSort to support smoothing groups
  47. * This is used in the .3ds loader
  48. */
  49. class D3DSSpatialSorter
  50. {
  51. public:
  52. D3DSSpatialSorter();
  53. // -------------------------------------------------------------------
  54. /** Construction from a given face array, handling smoothing groups properly
  55. */
  56. D3DSSpatialSorter(const std::vector<aiVector3D>& vPositions);
  57. // -------------------------------------------------------------------
  58. /** Add a face to the spatial sorter
  59. * @param pcFace Face to be added
  60. * @param vPositions Input position list
  61. */
  62. void AddFace(const Dot3DS::Face* pcFace,
  63. const std::vector<aiVector3D>& vPositions);
  64. // -------------------------------------------------------------------
  65. /** Prepare the spatial sorter for use
  66. */
  67. void Prepare();
  68. /** Destructor */
  69. ~D3DSSpatialSorter();
  70. // -------------------------------------------------------------------
  71. /** Returns an iterator for all positions close to the given position.
  72. * @param pPosition The position to look for vertices.
  73. * @param pSG Only included vertices with at least one shared smooth group
  74. * @param pRadius Maximal distance from the position a vertex may have to be counted in.
  75. * @param poResults The container to store the indices of the found positions. Will be emptied
  76. * by the call so it may contain anything.
  77. * @return An iterator to iterate over all vertices in the given area.
  78. */
  79. void FindPositions( const aiVector3D& pPosition, uint32_t pSG,
  80. float pRadius, std::vector<unsigned int>& poResults) const;
  81. protected:
  82. /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
  83. aiVector3D mPlaneNormal;
  84. // -------------------------------------------------------------------
  85. /** An entry in a spatially sorted position array. Consists of a vertex index,
  86. * its position and its precalculated distance from the reference plane */
  87. struct Entry
  88. {
  89. unsigned int mIndex; ///< The vertex referred by this entry
  90. aiVector3D mPosition; ///< Position
  91. uint32_t mSmoothGroups;
  92. float mDistance; ///< Distance of this vertex to the sorting plane
  93. Entry() { /** intentionally not initialized.*/ }
  94. Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance,uint32_t pSG)
  95. :
  96. mPosition( pPosition), mIndex( pIndex), mDistance( pDistance),mSmoothGroups (pSG)
  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