30[[nodiscard]] std::optional<std::size_t> find_best_palette(
33 std::optional<std::size_t> best_idx;
34 double best_cost = std::numeric_limits<double>::max();
36 for (std::size_t i = 0; i < palettes.size(); ++i) {
37 const auto &pal = palettes[i];
48 if (metadata !=
nullptr) {
52 if (cost < best_cost) {
60 if (best_idx.has_value() && best_cost >=
static_cast<double>(tile.
color_count())) {
91 auto assign_tile = [&output, metadata](
const PackableTile &tile) ->
bool {
92 const auto maybe_best_idx = find_best_palette(tile, output.
pals_, metadata);
94 if (maybe_best_idx.has_value()) {
96 output.
pals_[maybe_best_idx.value()].add_tile(tile);
102 for (std::size_t i = 0; i < output.
pals_.size(); ++i) {
104 output.
pals_[i].add_tile(tile);
112 for (std::size_t i = 0; i < output.
pals_.size(); ++i) {
114 output.
pals_[i].add_tile(tile);
125 std::vector<PackableTile> tile_pool{};
126 for (
const auto &hint : input.
hints_) {
127 tile_pool.emplace_back(hint);
129 for (
const auto &tile : input.
tiles_) {
130 tile_pool.emplace_back(tile);
133 if (tile_pool.empty()) {
151 for (
const auto &tile : tile_pool) {
152 if (!assign_tile(tile)) {
153 return FormattableError{
"Best Fusion: cannot assign tile - no palette has room."};
ChainableResult< PackingOutput > pack(const PackingInput &input) const override
Packs tiles into palettes using the Best Fusion algorithm.
A result type that maintains a chainable sequence of errors for debugging and error reporting.
Wraps a ColorSet with a tile ID for tracking during palette packing.
std::size_t color_count() const
const ColorSet & color_set() const
Manages allocation of hardware palette indexes with stack-based checkout semantics.
std::size_t checkout()
Checks out the next available hardware palette index.
bool has_available_pal() const
Checks if there is at least one available pal that can be checked out.
double compute_weighted_cost_in_palette_fast(const ColorSet &tile_colors, const PackedPalette &palette)
Computes the weighted cost of placing a tile in a palette using cached counts.
double compute_sharing_penalty(const PackableTile &tile, const PackedPalette &palette, const ShapeGroupMetadata &metadata, double sharing_weight=0.5)
Computes the sharing penalty for placing a tile in a palette.
std::vector< PackedPalette > initialize_packed_palettes(const std::set< PrefilledPalette > &prefilled_pals, PalettePool &pal_pool, std::size_t pal_capacity)
Initializes packed palettes from prefilled palettes.
Metrics for Bin Packing with Overlapping Items (Pagination problem).
The final palette assignments after a successful packing operation.
std::map< PackableTile::Id, std::size_t > tile_to_pal_
Maps tile IDs to their assigned hardware palette indices.
std::vector< PackedPalette > pals_
The packed palettes with their colors and assigned tiles.