import json

# 读取你的occupancy.json和2D map.json
with open('test/839919/occupancy.json') as f:
    meta = json.load(f)
scale = meta['scale']
x_min, y_min = meta['min'][:2]

with open('2D_Semantic_Map_839919.json') as f:
    objects_2d = json.load(f)

# 转为A*地图API输入参数格式
object_layouts = []
for inst in objects_2d:
    label = inst['category_label']
    bbox = inst['bbox']  # [xmin, ymin, xmax, ymax] in pixel
    mask_area = inst['area']
    instance_id = inst.get('instance_id', '')

    xmin, ymin, xmax, ymax = bbox
    # 左上原点，左下物理空间注意y要对应图的方向。通常y像素增大对应物理y增大
    obj_x = x_min + xmin * scale
    obj_y = y_min + ymin * scale
    width = (xmax - xmin) * scale
    height = (ymax - ymin) * scale
    # 2D物体模型的传给map_rep为：obj_name, pos(x, y, 0), size(width, height, ...), ...
    obj_info = {
        "name": f"{label}_{instance_id}".replace(" ", "_"),
        "category": label,
        "pixel_bbox": bbox,                  # [xmin, ymin, xmax, ymax]
        "world_bbox": [obj_x, obj_y, width, height],      # [物理左下x, 左下y, width, height]
        "pixel_xywh": inst['bbox_xywh'],
        "area_pixel": mask_area,
    }
    object_layouts.append(obj_info)

# 现在object_layouts里每个就是A*地图API add的标准参数字典！
with open('Map_Object_Layouts_839919.json', 'w') as f:
    json.dump(object_layouts, f, indent=2)