#!/usr/bin/env python3 """Draw converted RPLAN plans (room rectangles, doors and windows the chain would cut) as one contact sheet so they can be looked at. draw_plans.py --library screened.json --out sheet.png [--count 12] [--seed 1] [--ids rplan_graph2plan_1,...] [--cols 3] Needs matplotlib. The doors/windows are drawn from the plan's own `doors` / `windows` when present (a library straight from convert_rplan.py has neither: only the rectangles are drawn).""" import argparse import json import random import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt from matplotlib.patches import Rectangle COLOUR = {'living_room': '#f2e6c9', 'dining_room': '#f6d6a8', 'kitchen': '#c5e0b4', 'bathroom': '#b7d7e8', 'master_bedroom': '#f4b6c2', 'guest_bedroom': '#e2c2ee', 'balcony': '#d9d9d9', 'hallway': '#fff2b3'} def draw(ax, plan): for room in plan['rooms']: x0, y0, x1, y1 = room['bounds_m'] ax.add_patch(Rectangle((x0, y0), x1 - x0, y1 - y0, fc=COLOUR.get(room['room_type'], '#eeeeee'), ec='k', lw=1.2)) ax.text((x0 + x1) / 2, (y0 + y1) / 2, '%s\n%.1f m2' % (room['room_id'], (x1 - x0) * (y1 - y0)), ha='center', va='center', fontsize=6) for window in plan.get('windows', []): lo, hi = window['window_interval_m'] line = window['wall_line_m'] xs, ys = ([line, line], [lo, hi]) if window['wall_orientation'] == 'V' else ([lo, hi], [line, line]) ax.plot(xs, ys, color='#1f77b4', lw=3) for door in plan.get('doors', []): lo, hi = door['door_interval_m'] line = door['wall_line_m'] xs, ys = ([line, line], [lo, hi]) if door['wall_orientation'] == 'V' else ([lo, hi], [line, line]) ax.plot(xs, ys, color='#d62728', lw=3) xs = [v for r in plan['rooms'] for v in (r['bounds_m'][0], r['bounds_m'][2])] ys = [v for r in plan['rooms'] for v in (r['bounds_m'][1], r['bounds_m'][3])] ax.set_xlim(min(xs) - 0.3, max(xs) + 0.3) ax.set_ylim(max(ys) + 0.3, min(ys) - 0.3) ax.set_aspect('equal') ax.set_xticks([]) ax.set_yticks([]) conv = plan.get('rplan_conversion', {}) area = sum((r['bounds_m'][2] - r['bounds_m'][0]) * (r['bounds_m'][3] - r['bounds_m'][1]) for r in plan['rooms']) ax.set_title('%s | %.0f m2 | %d rooms | %s' % (plan['plan_id'].replace('rplan_graph2plan_', ''), area, len(plan['rooms']), conv.get('access', '')), fontsize=7) def main(): ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) ap.add_argument('--library', required=True) ap.add_argument('--out', required=True) ap.add_argument('--count', type=int, default=12) ap.add_argument('--seed', type=int, default=1) ap.add_argument('--ids', help='comma separated plan ids to draw instead of a random draw') ap.add_argument('--cols', type=int, default=3) args = ap.parse_args() plans = json.load(open(args.library))['plans'] if args.ids: wanted = args.ids.split(',') chosen = [p for p in plans if p['plan_id'] in wanted] else: chosen = random.Random(args.seed).sample(plans, min(args.count, len(plans))) rows = (len(chosen) + args.cols - 1) // args.cols fig, axes = plt.subplots(rows, args.cols, figsize=(4.2 * args.cols, 4.0 * rows), squeeze=False) for ax in axes.ravel(): ax.axis('off') for ax, plan in zip(axes.ravel(), chosen): ax.axis('on') draw(ax, plan) fig.tight_layout() fig.savefig(args.out, dpi=75) print('drew %d plans' % len(chosen)) if __name__ == '__main__': main()