SpatialSort.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /** Small helper classes to optimise finding vertizes close to a given location */
  2. #ifndef AI_SPATIALSORT_H_INC
  3. #define AI_SPATIALSORT_H_INC
  4. #include <vector>
  5. #include "../include/aiVector3D.h"
  6. namespace Assimp
  7. {
  8. // ------------------------------------------------------------------------------------------------
  9. /** A little helper class to quickly find all vertices in the epsilon environment of a given
  10. * position. Construct an instance with an array of positions. The class stores the given positions
  11. * by their indices and sorts them by their distance to an arbitrary chosen plane.
  12. * You can then query the instance for all vertices close to a given position in an average O(log n)
  13. * time, with O(n) worst case complexity when all vertices lay on the plane. The plane is chosen
  14. * so that it avoids common planes in usual data sets.
  15. */
  16. class SpatialSort
  17. {
  18. public:
  19. SpatialSort() {/* This is unintialized. This is evil. This is OK. */}
  20. /** Constructs a spatially sorted representation from the given position array.
  21. * Supply the positions in its layout in memory, the class will only refer to them
  22. * by index.
  23. * @param pPositions Pointer to the first position vector of the array.
  24. * @param pNumPositions Number of vectors to expect in that array.
  25. * @param pElementOffset Offset in bytes from the beginning of one vector in memory to the beginning of the next vector.
  26. */
  27. SpatialSort( const aiVector3D* pPositions, unsigned int pNumPositions, unsigned int pElementOffset);
  28. /** Destructor */
  29. ~SpatialSort();
  30. /** Returns an iterator for all positions close to the given position.
  31. * @param pPosition The position to look for vertices.
  32. * @param pRadius Maximal distance from the position a vertex may have to be counted in.
  33. * @param poResults The container to store the indices of the found positions. Will be emptied
  34. * by the call so it may contain anything.
  35. * @return An iterator to iterate over all vertices in the given area.
  36. */
  37. void FindPositions( const aiVector3D& pPosition, float pRadius, std::vector<unsigned int>& poResults) const;
  38. protected:
  39. /** Normal of the sorting plane, normalized. The center is always at (0, 0, 0) */
  40. aiVector3D mPlaneNormal;
  41. /** An entry in a spatially sorted position array. Consists of a vertex index,
  42. * its position and its precalculated distance from the reference plane */
  43. struct Entry
  44. {
  45. unsigned int mIndex; ///< The vertex referred by this entry
  46. aiVector3D mPosition; ///< Position
  47. float mDistance; ///< Distance of this vertex to the sorting plane
  48. Entry() { /** intentionally not initialized.*/ }
  49. Entry( unsigned int pIndex, const aiVector3D& pPosition, float pDistance)
  50. : mPosition( pPosition), mIndex( pIndex), mDistance( pDistance)
  51. { }
  52. bool operator < (const Entry& e) const { return mDistance < e.mDistance; }
  53. };
  54. // all positions, sorted by distance to the sorting plane
  55. std::vector<Entry> mPositions;
  56. };
  57. } // end of namespace Assimp
  58. #endif // AI_SPATIALSORT_H_INC