#include "mapping.h"

namespace freling {

std::vector<BoundingBox> map_to_origin(
    const std::vector<BoundingBox>& origin_bboxes,
    const std::vector<BoundingBox>& packed_bboxes,
    const std::vector<BoundingBox>& a2_bboxes) {
    std::vector<BoundingBox> mapped_bboxes(a2_bboxes.size());
    for (int i = 0, i_max = a2_bboxes.size(); i < i_max; ++i) {
        const auto& a2_box = a2_bboxes[i];
        for (int j = 0, j_max = packed_bboxes.size(); j < j_max; ++j) {
            const auto& packed_box = packed_bboxes[j];
            if (packed_box.contains(a2_box.x, a2_box.y)) {
                const auto& origin_box = origin_bboxes[j];
                auto& mapped_box = mapped_bboxes[i];
                mapped_box.width = a2_box.width;
                mapped_box.height = a2_box.height;
                mapped_box.x = a2_box.x - packed_box.x + origin_box.x;
                mapped_box.y = a2_box.y - packed_box.y + origin_box.y;
                break;
            }
        }
    }
    return mapped_bboxes;
}

}  // namespace freling
