ShelfBinPack.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /** @file ShelfBinPack.h
  2. @author Jukka Jylänki
  3. @brief Implements different bin packer algorithms that use the SHELF data structure.
  4. This work is released to Public Domain, do whatever you want with it.
  5. */
  6. #pragma once
  7. #include "GuillotineBinPack.h"
  8. #include "Rect.h"
  9. /** ShelfBinPack implements different bin packing algorithms that use the SHELF data structure. ShelfBinPack
  10. also uses GuillotineBinPack for the waste map if it is enabled. */
  11. class ShelfBinPack
  12. {
  13. public:
  14. /// Default ctor initializes a bin of size (0,0). Call Init() to init an instance.
  15. ShelfBinPack();
  16. ShelfBinPack(int width, int height, bool useWasteMap, bool allowRotate);
  17. /// Clears all previously packed rectangles and starts packing from scratch into a bin of the given size.
  18. void Init(int width, int height, bool useWasteMap, bool allowRotate);
  19. /// Defines different heuristic rules that can be used in the packing process.
  20. enum ShelfChoiceHeuristic
  21. {
  22. ShelfNextFit, ///< -NF: We always put the new rectangle to the last open shelf.
  23. ShelfFirstFit, ///< -FF: We test each rectangle against each shelf in turn and pack it to the first where it fits.
  24. ShelfBestAreaFit, ///< -BAF: Choose the shelf with smallest remaining shelf area.
  25. ShelfWorstAreaFit, ///< -WAF: Choose the shelf with the largest remaining shelf area.
  26. ShelfBestHeightFit, ///< -BHF: Choose the smallest shelf (height-wise) where the rectangle fits.
  27. ShelfBestWidthFit, ///< -BWF: Choose the shelf that has the least remaining horizontal shelf space available after packing.
  28. ShelfWorstWidthFit, ///< -WWF: Choose the shelf that will have most remainining horizontal shelf space available after packing.
  29. };
  30. /// Inserts a single rectangle into the bin. The packer might rotate the rectangle, in which case the returned
  31. /// struct will have the width and height values swapped.
  32. /// @param method The heuristic rule to use for choosing a shelf if multiple ones are possible.
  33. Rect Insert(int width, int height, ShelfChoiceHeuristic method);
  34. /// Computes the ratio of used surface area to the total bin area.
  35. float Occupancy() const;
  36. private:
  37. int binWidth;
  38. int binHeight;
  39. bool allowRotate;
  40. /// Stores the starting y-coordinate of the latest (topmost) shelf.
  41. int currentY;
  42. /// Tracks the total consumed surface area.
  43. unsigned long usedSurfaceArea;
  44. /// If true, the following GuillotineBinPack structure is used to recover the SHELF data structure from losing space.
  45. bool useWasteMap;
  46. GuillotineBinPack wasteMap;
  47. /// Describes a horizontal slab of space where rectangles may be placed.
  48. struct Shelf
  49. {
  50. /// The x-coordinate that specifies where the used shelf space ends.
  51. /// Space between [0, currentX[ has been filled with rectangles, [currentX, binWidth[ is still available for filling.
  52. int currentX;
  53. /// The y-coordinate of where this shelf starts, inclusive.
  54. int startY;
  55. /// Specifices the height of this shelf. The topmost shelf is "open" and its height may grow.
  56. int height;
  57. /// Lists all the rectangles in this shelf.
  58. Memc<Rect> usedRectangles;
  59. };
  60. Memc<Shelf> shelves;
  61. /// Parses through all rectangles added to the given shelf and adds the gaps between the rectangle tops and the shelf
  62. /// ceiling into the waste map. This is called only once when the shelf is being closed and a new one is opened.
  63. void MoveShelfToWasteMap(Shelf &shelf);
  64. /// Returns true if the rectangle of size width*height fits on the given shelf, possibly rotated.
  65. /// @param canResize If true, denotes that the shelf height may be increased to fit the object.
  66. bool FitsOnShelf(const Shelf &shelf, int width, int height, bool canResize) const;
  67. /// Measures and if desirable, flips width and height so that the rectangle fits the given shelf the best.
  68. /// @param width [in,out] The width of the rectangle.
  69. /// @param height [in,out] The height of the rectangle.
  70. void RotateToShelf(const Shelf &shelf, int &width, int &height) const;
  71. /// Adds the rectangle of size width*height into the given shelf, possibly rotated.
  72. /// @param newNode [out] The added rectangle will be returned here.
  73. void AddToShelf(Shelf &shelf, int width, int height, Rect &newNode);
  74. /// Returns true if there is still room in the bin to start a new shelf of the given height.
  75. bool CanStartNewShelf(int height) const;
  76. /// Creates a new shelf of the given starting height, which will become the topmost 'open' shelf.
  77. void StartNewShelf(int startingHeight);
  78. };