"""Continuous furniture wall runs in rectangular RPLAN rooms (all distances in metres). Use native shell topology for screening. ``approximate=True`` is the conversion estimate: full living/dining/balcony contacts are open, other public contacts carry a centred one metre door. No windows are predicted in that mode. """ DOOR_MARGIN_M = 0.1 ASSUMED_DOOR_WIDTH_M = 1.0 LIVING_SOFA_MIN_M = 2.4 LIVING_TV_MIN_M = 1.6 BEDROOM_MIN_M = 2.0 DINING_WALL_MIN_M = 1.2 DINING_CENTER_MIN_SIDE_M = 2.6 IRREGULAR_LIVING_AREA_RATIO = 0.85 EPS = 1e-6 OPEN_TYPE_PAIRS = {frozenset(p) for p in ( ('living_room', 'dining_room'), ('living_room', 'kitchen'), ('dining_room', 'kitchen'), ('living_room', 'balcony'), ('master_bedroom', 'balcony'), ('guest_bedroom', 'balcony'))} FORBIDDEN_DOOR_PAIRS = {frozenset(p) for p in ( ('kitchen', 'bathroom'), ('master_bedroom', 'guest_bedroom'), ('bathroom', 'master_bedroom'), ('bathroom', 'guest_bedroom'))} def _shared_wall(a, b): for orient, edges_a, edges_b, span in ( ('V', (0, 2), (2, 0), (1, 3)), ('H', (1, 3), (3, 1), (0, 2))): lo, hi = max(a[span[0]], b[span[0]]), min(a[span[1]], b[span[1]]) if hi <= lo + EPS: continue for i, j in zip(edges_a, edges_b): if abs(a[i] - b[j]) <= EPS: return dict(wall_orientation=orient, wall_line_m=a[i], overlap_interval_m=[lo, hi]) return None def _inferred_adjacencies(plan, approximate): rooms = plan['rooms'] open_pairs = OPEN_TYPE_PAIRS | {frozenset(p) for p in plan.get('open_room_type_pairs', [])} for i, a in enumerate(rooms): for b in rooms[i + 1:]: rec = _shared_wall(a['bounds_m'], b['bounds_m']) if rec is None: continue pair = frozenset((a['room_type'], b['room_type'])) rec['room_ids'] = [a['room_id'], b['room_id']] is_open = pair in open_pairs if approximate and 'living_room' in pair: is_open = bool(pair & {'dining_room', 'balcony'}) if is_open: rec.update(open_connection=True, open_interval_m=rec['overlap_interval_m']) yield rec elif approximate and pair & {'living_room', 'dining_room', 'hallway'} and pair not in FORBIDDEN_DOOR_PAIRS: lo, hi = rec['overlap_interval_m'] mid = (lo + hi) / 2 rec['door_interval_m'] = [max(lo, mid - ASSUMED_DOOR_WIDTH_M / 2), min(hi, mid + ASSUMED_DOOR_WIDTH_M / 2)] yield rec def _free_intervals(lo, hi, occupied): cursor, free = lo, [] for start, end in sorted(occupied): start, end = max(lo, start), min(hi, end) if end <= start: continue if start > cursor + EPS: free.append([cursor, start]) cursor = max(cursor, end) if cursor < hi - EPS: free.append([cursor, hi]) return [[round(a, 6), round(b, 6)] for a, b in free] def _longest(intervals): return round(max((b - a for a, b in intervals), default=0.0), 6) def check_usable_walls(plan, *, approximate=False): """Return JSON-safe ``passed``, ``failures`` and per-room ``walls`` diagnostics. Door/open intervals have 0.1 m clearance at each end; windows only shorten windowless runs. Native adjacency records with no portal remain solid. An explicit door for a room pair takes precedence over its topology door. N/S follow the native coordinate convention: N is y1 and S is y0. Rooms without a furniture threshold are measured but never rejected. """ rooms, measured = {r['room_id']: r for r in plan['rooms']}, {} for rid, room in rooms.items(): x0, y0, x1, y1 = room['bounds_m'] walls = {} for side, orient, line, lo, hi in ( ('W', 'V', x0, y0, y1), ('E', 'V', x1, y0, y1), ('S', 'H', y0, x0, x1), ('N', 'H', y1, x0, x1)): walls[side] = dict(wall_orientation=orient, wall_line_m=line, interval_m=[lo, hi], occupied=[], windows=[]) measured[rid] = dict(room_type=room['room_type'], walls=walls) def add(rec, interval, kind, margin=0.0): for rid in rec.get('room_ids', [rec.get('room_id')]): if rid not in measured: continue walls = measured[rid]['walls'] choices = [s for s in walls if walls[s]['wall_orientation'] == rec['wall_orientation']] side = min(choices, key=lambda s: abs(walls[s]['wall_line_m'] - rec['wall_line_m'])) wall = walls[side] lo, hi = wall['interval_m'] start, end = max(lo, interval[0] - margin), min(hi, interval[1] + margin) if end > start + EPS: wall['windows' if kind == 'window' else 'occupied'].append( dict(kind=kind, interval_m=[round(start, 6), round(end, 6)], room_ids=rec.get('room_ids', [rid]))) doors = plan.get('doors', []) explicit_pairs = {frozenset(d.get('room_ids', [])) for d in doors} adjs = list(_inferred_adjacencies(plan, approximate)) if approximate or 'topology_adjacencies' not in plan else plan['topology_adjacencies'] for rec in adjs: if rec.get('balcony_opening_suppressed_v16_21p'): continue if rec.get('open_connection'): interval = rec.get('open_interval_m') or rec.get('overlap_interval_m') if interval: kind = 'balcony_opening' if any(rooms[r]['room_type'] == 'balcony' for r in rec['room_ids'] if r in rooms) else 'open' add(rec, interval, kind, DOOR_MARGIN_M) elif rec.get('door_interval_m') and frozenset(rec.get('room_ids', [])) not in explicit_pairs: add(rec, rec['door_interval_m'], 'door', DOOR_MARGIN_M) for rec in doors: add(rec, rec['door_interval_m'], 'door', DOOR_MARGIN_M) for rec in plan.get('windows', []): add(rec, rec['window_interval_m'], 'window') failures = [] for rid, result in measured.items(): walls = result['walls'] for wall in walls.values(): occupied = [r['interval_m'] for r in wall['occupied']] wall['free_intervals_m'] = _free_intervals(*wall['interval_m'], occupied) wall['windowless_intervals_m'] = _free_intervals(*wall['interval_m'], occupied + [r['interval_m'] for r in wall['windows']]) wall['longest_m'] = _longest(wall['free_intervals_m']) wall['windowless_longest_m'] = _longest(wall['windowless_intervals_m']) result['max_usable_wall_m'] = max(w['longest_m'] for w in walls.values()) result['max_windowless_wall_m'] = max(w['windowless_longest_m'] for w in walls.values()) rtype = result['room_type'] result['required'] = rtype in ('living_room', 'dining_room') or 'bedroom' in rtype result['passed'] = True if rtype == 'living_room': pairs = [(a, b) for a in walls for b in walls if a != b] valid = [(a, b) for a, b in pairs if walls[a]['longest_m'] + EPS >= LIVING_SOFA_MIN_M and walls[b]['windowless_longest_m'] + EPS >= LIVING_TV_MIN_M] a, b = max(valid or pairs, key=lambda p: (walls[p[0]]['longest_m'], walls[p[1]]['windowless_longest_m'])) result.update(passed=bool(valid), sofa_side=a, tv_side=b, sofa_wall_m=walls[a]['longest_m'], tv_wall_m=walls[b]['windowless_longest_m']) elif 'bedroom' in rtype: result['passed'] = result['max_usable_wall_m'] + EPS >= BEDROOM_MIN_M elif rtype == 'dining_room': x0, y0, x1, y1 = rooms[rid]['bounds_m'] result['passed'] = (result['max_usable_wall_m'] + EPS >= DINING_WALL_MIN_M or min(x1 - x0, y1 - y0) + EPS >= DINING_CENTER_MIN_SIDE_M) if not result['passed']: failures.append('no_usable_wall:' + rid) return dict(passed=not failures, failures=sorted(failures), rooms=measured, mode='approximate' if approximate else 'native')