GridSectorizer.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. #ifndef _GRID_SECTORIZER_H
  11. #define _GRID_SECTORIZER_H
  12. //#define _USE_ORDERED_LIST
  13. #include "RakMemoryOverride.h"
  14. #ifdef _USE_ORDERED_LIST
  15. #include "DS_OrderedList.h"
  16. #else
  17. #include "DS_List.h"
  18. #endif
  19. class GridSectorizer
  20. {
  21. public:
  22. GridSectorizer();
  23. ~GridSectorizer();
  24. // _cellWidth, _cellHeight is the width and height of each cell in world units
  25. // minX, minY, maxX, maxY are the world dimensions (can be changed to dynamically allocate later if needed)
  26. void Init(const float _maxCellWidth, const float _maxCellHeight, const float minX, const float minY, const float maxX, const float maxY);
  27. // Adds a pointer to the grid with bounding rectangle dimensions
  28. void AddEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY);
  29. #ifdef _USE_ORDERED_LIST
  30. // Removes a pointer, as above
  31. void RemoveEntry(void *entry, const float minX, const float minY, const float maxX, const float maxY);
  32. // Adds and removes in one pass, more efficient than calling both functions consecutively
  33. void MoveEntry(void *entry, const float sourceMinX, const float sourceMinY, const float sourceMaxX, const float sourceMaxY,
  34. const float destMinX, const float destMinY, const float destMaxX, const float destMaxY);
  35. #endif
  36. // Adds to intersectionList all entries in a certain radius
  37. void GetEntries(DataStructures::List<void*>& intersectionList, const float minX, const float minY, const float maxX, const float maxY);
  38. void Clear(void);
  39. protected:
  40. int WorldToCellX(const float input) const;
  41. int WorldToCellY(const float input) const;
  42. int WorldToCellXOffsetAndClamped(const float input) const;
  43. int WorldToCellYOffsetAndClamped(const float input) const;
  44. // Returns true or false if a position crosses cells in the grid. If false, you don't need to move entries
  45. bool PositionCrossesCells(const float originX, const float originY, const float destinationX, const float destinationY) const;
  46. float cellOriginX, cellOriginY;
  47. float cellWidth, cellHeight;
  48. float invCellWidth, invCellHeight;
  49. float gridWidth, gridHeight;
  50. int gridCellWidthCount, gridCellHeightCount;
  51. // int gridWidth, gridHeight;
  52. #ifdef _USE_ORDERED_LIST
  53. DataStructures::OrderedList<void*, void*>* grid;
  54. #else
  55. DataStructures::List<void*>* grid;
  56. #endif
  57. };
  58. #endif
粤ICP备19079148号