4#ifndef GREM_RESOURCE_ATLAS_PACKER_HPP
5#define GREM_RESOURCE_ATLAS_PACKER_HPP
67 : resolution(options.initialResolution)
68 , padding(options.padding)
69 , alignment(options.alignment) {
112 bool resized =
false;
114 const uint32_t newRowBottom = (rows.empty()) ? uint32_t{0} : rows.back().top;
115 const uint32_t newRowHeight = paddedSize.
height + paddedSize.
height / uint32_t{8};
116 while (paddedSize.
width > resolution || newRowHeight > resolution - newRowBottom) {
118 throw std::length_error{
"Maximum atlas resolution exceeded."};
120 resolution *= GROWTH_FACTOR;
123 foundRow = rows.size();
124 rows.push_back(Row{.top = newRowBottom + newRowHeight, .width = 0});
126 const size_t rowIndex = *foundRow;
128 Row& row = rows[rowIndex];
129 const uint32_t rowBottom = (rowIndex == 0) ? uint32_t{0} : rows[rowIndex - 1].top;
130 const uint32_t x = row.width + padding;
131 const uint32_t y = rowBottom + padding;
133 row.width += paddedSize.
width;
136 .offset{.x =
static_cast<int32_t
>(x), .y =
static_cast<int32_t
>(y)},
151 static constexpr uint32_t GROWTH_FACTOR = 2;
152 static constexpr float MINIMUM_ROW_HEIGHT_RATIO = 0.7f;
160 for (
size_t rowIndex = 0; rowIndex < rows.
size(); ++rowIndex) {
161 const Row& row = rows[rowIndex];
162 const uint32_t rowHeight = (rowIndex == 0) ? row.top : row.top - rows[rowIndex - 1].top;
163 if (
const float heightRatio =
static_cast<float>(paddedSize.
height) /
static_cast<float>(rowHeight);
164 heightRatio >= MINIMUM_ROW_HEIGHT_RATIO && paddedSize.
height <= rowHeight && paddedSize.
width <= resolution - row.width) {
#define GREM_ASSERT(expression)
Definition assertions.hpp:77
size_type size() const noexcept
Definition Buffer.hpp:299
Optional value type that holds either a value or no value.
Definition Optional.hpp:58
uint32_t getResolution() const noexcept
Get the current required resolution of the atlas.
Definition AtlasPacker.hpp:146
AtlasPacker() noexcept
Construct an atlas packer with the default options.
Definition AtlasPacker.hpp:58
AtlasPacker(const AtlasPackerOptions &options)
Construct an atlas packer.
Definition AtlasPacker.hpp:66
InsertRectangleResult insertRectangle(Extent2D size)
Find and reserve a suitable space for a new axis-aligned rectangle to be inserted into the atlas.
Definition AtlasPacker.hpp:104
GREM_ALWAYS_INLINE constexpr bool isPowerOf2(UnsignedInteger value) noexcept
Check if a given integer is a power of 2, i.e. if the number appears in the infinite sequence 2^0,...
Definition fundamentals.hpp:781
Extent< 2 > Extent2D
Size of a 2D data region.
Definition extents.hpp:31
GREM_ALWAYS_INLINE constexpr UnsignedInteger roundUpToMultiple(UnsignedInteger value, UnsignedInteger n)
Get the smallest multiple of n that is not greater than a given value.
Definition fundamentals.hpp:822
Offset< 2 > Offset2D
Offset in a 2D data region.
Definition extents.hpp:21
uint32_t width
Horizontal extent of the region.
Definition extents.hpp:537
uint32_t height
Vertical extent of the region.
Definition extents.hpp:538
static constexpr T MAX
Definition fundamentals.hpp:29
Configuration options for an AtlasPacker.
Definition AtlasPacker.hpp:20
constexpr bool operator==(const AtlasPackerOptions &other) const =default
Compare these options to another set of options for equality.
uint32_t alignment
Align padded rectangles to a multiple of this number of pixels.
Definition AtlasPacker.hpp:38
uint32_t padding
Empty space to reserve between inserted rectangles, in pixels.
Definition AtlasPacker.hpp:31
uint32_t initialResolution
Initial width of the square atlas region, in pixels.
Definition AtlasPacker.hpp:26
Result of the insertRectangle() function.
Definition AtlasPacker.hpp:77
Offset2D offset
The offset, in pixels, from the bottom left of the atlas where the new rectangle was inserted.
Definition AtlasPacker.hpp:82
bool resized
Whether the atlas needed to grow in order to accommodate the new rectangle or not.
Definition AtlasPacker.hpp:89