28[[nodiscard]] std::optional<std::size_t> find_best_palette(
31 std::optional<std::size_t> best_idx;
32 double best_cost = std::numeric_limits<double>::max();
34 for (std::size_t i = 0; i < palettes.size(); ++i) {
35 const auto &palette = palettes[i];
46 if (metadata !=
nullptr) {
50 if (cost < best_cost) {
58 if (best_idx.has_value() && best_cost >=
static_cast<double>(tile.
color_count())) {
89 auto assign_tile = [&output, metadata](
const PackableTile &tile) ->
bool {
90 const auto maybe_best_idx = find_best_palette(tile, output.
palettes_, metadata);
92 if (maybe_best_idx.has_value()) {
94 output.
palettes_[maybe_best_idx.value()].add_tile(tile);
100 for (std::size_t i = 0; i < output.
palettes_.size(); ++i) {
110 for (std::size_t i = 0; i < output.
palettes_.size(); ++i) {
123 std::vector<PackableTile> tile_pool{};
124 for (
const auto &hint : input.
hints_) {
125 tile_pool.emplace_back(hint);
127 for (
const auto &tile : input.
tiles_) {
128 tile_pool.emplace_back(tile);
131 if (tile_pool.empty()) {
147 for (
const auto &tile : tile_pool) {
148 if (!assign_tile(tile)) {
149 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.
bool has_available_palette() const
Checks if there is at least one available palette that can be checked out.
std::size_t checkout()
Checks out the next available hardware palette index.
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_palettes, PalettePool &palette_pool, std::size_t palette_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::vector< PackedPalette > palettes_
The packed palettes with their colors and assigned tiles.
std::map< PackableTile::Id, std::size_t > tile_to_palette_
Maps tile IDs to their assigned hardware palette indices.