"""領域の内容矩形を決めている塊を表示する: python3 jg_debug_region.py JG-E0007-Q04-stem-p02 [...]"""
import sys, json
from common import *
for rid in sys.argv[1:]:
    eid = rid[:8]
    seg = load_json(DATA / "segmentation" / f"{eid}.json")
    r = next(r for r in seg["regions"] if r["region_id"] == rid)
    pi = r["page_index"]; cr = r["content_rect"]
    page = load_json(DATA / "layout" / f"{eid}.json")["pages"][pi]
    print(rid, "rect", r["rect"], "content", cr, "note", r.get("note"))
    for d in page["drawings"]:
        x0, y0, x1, y1 = d
        inside = x0 >= cr[0] - 1 and x1 <= cr[2] + 1 and y0 >= cr[1] - 1 and y1 <= cr[3] + 1
        if inside and (abs(x0 - cr[0]) < 1 or abs(x1 - cr[2]) < 1 or abs(y0 - cr[1]) < 1 or abs(y1 - cr[3]) < 1):
            print("   edge blob", [round(v, 1) for v in d], "w", round(x1 - x0, 1), "h", round(y1 - y0, 1))
