rect_pack.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // rect_pack.h - public domain - rectangle packing
  2. // Albert Kalchmair 2021
  3. //
  4. // Useful for e.g. packing rectangular textures into one or multiple atlases.
  5. //
  6. // LICENSE
  7. //
  8. // See end of file for license information.
  9. #pragma once
  10. #include <vector>
  11. namespace rect_pack {
  12. enum class Method {
  13. Best,
  14. Best_Skyline,
  15. Best_MaxRects,
  16. Skyline_BottomLeft,
  17. Skyline_BestFit,
  18. MaxRects_BestShortSideFit,
  19. MaxRects_BestLongSideFit,
  20. MaxRects_BestAreaFit,
  21. MaxRects_BottomLeftRule,
  22. MaxRects_ContactPointRule
  23. };
  24. struct Size {
  25. int id;
  26. int width;
  27. int height;
  28. };
  29. struct Rect {
  30. int id;
  31. int x;
  32. int y;
  33. int width;
  34. int height;
  35. bool rotated;
  36. };
  37. struct Sheet {
  38. int width;
  39. int height;
  40. std::vector<Rect> rects;
  41. };
  42. struct Settings {
  43. Method method;
  44. int max_sheets;
  45. bool power_of_two;
  46. bool square;
  47. bool allow_rotate;
  48. int align_width;
  49. int border_padding;
  50. int over_allocate;
  51. int min_width;
  52. int min_height;
  53. int max_width;
  54. int max_height;
  55. };
  56. std::vector<Sheet> pack(Settings settings, std::vector<Size> sizes);
  57. } // namespace
  58. /*
  59. ------------------------------------------------------------------------------
  60. This software is available under 2 licenses -- choose whichever you prefer.
  61. ------------------------------------------------------------------------------
  62. ALTERNATIVE A - MIT License
  63. Copyright (c) 2021 Albert Kalchmair
  64. Permission is hereby granted, free of charge, to any person obtaining a copy of
  65. this software and associated documentation files (the "Software"), to deal in
  66. the Software without restriction, including without limitation the rights to
  67. use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
  68. of the Software, and to permit persons to whom the Software is furnished to do
  69. so, subject to the following conditions:
  70. The above copyright notice and this permission notice shall be included in all
  71. copies or substantial portions of the Software.
  72. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  73. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  74. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  75. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  76. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  77. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  78. SOFTWARE.
  79. ------------------------------------------------------------------------------
  80. ALTERNATIVE B - Public Domain (www.unlicense.org)
  81. This is free and unencumbered software released into the public domain.
  82. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
  83. software, either in source code form or as a compiled binary, for any purpose,
  84. commercial or non-commercial, and by any means.
  85. In jurisdictions that recognize copyright laws, the author or authors of this
  86. software dedicate any and all copyright interest in the software to the public
  87. domain. We make this dedication for the benefit of the public at large and to
  88. the detriment of our heirs and successors. We intend this dedication to be an
  89. overt act of relinquishment in perpetuity of all present and future rights to
  90. this software under copyright law.
  91. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  92. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  93. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  94. AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  95. ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  96. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  97. ------------------------------------------------------------------------------
  98. */