Datasets:
Download code/robohome/src/tools/rplan/test_convert_rplan.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 27.2 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/rplan/test_convert_rplan.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/tools/rplan/test_convert_rplan.py
-
curl -L -o test_convert_rplan.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/tools/rplan/test_convert_rplan.py
27.2 kB
| import unittest | |
| import convert_rplan as cr | |
| def rect(x0, y0, x1, y1): | |
| return [[x0, y0], [x0, y1], [x1, y1], [x1, y0]] | |
| def record(rooms, name='7'): | |
| return dict(name=name, rType=[l for l, _ in rooms], rBoundary=[p for _, p in rooms]) | |
| def overlap_m(a, b): | |
| return (min(a[2], b[2]) - max(a[0], b[0])), (min(a[3], b[3]) - max(a[1], b[1])) | |
| # a flat with the living room as a central band: master and second bedroom on its left, kitchen and bath on its right, balcony below | |
| FLAT = [(0, rect(40, 0, 70, 90)), (1, rect(0, 0, 40, 45)), (7, rect(0, 45, 40, 90)), (2, rect(70, 0, 110, 30)), (3, rect(70, 30, 110, 60)), | |
| (9, rect(40, 90, 70, 105))] | |
| # the master bedroom opens onto the leg of an L-shaped living room and touches nothing else | |
| HALL = [(0, [[30, 0], [110, 0], [110, 40], [46, 40], [46, 90], [30, 90]]), (2, rect(110, 0, 140, 40)), (3, rect(46, 40, 78, 65)), | |
| (7, rect(78, 40, 140, 90)), (1, rect(0, 40, 30, 90))] | |
| # the only neighbour of the bathroom is the master bedroom (an en-suite bath), which the chain gives no door | |
| ENSUITE = [(0, rect(0, 0, 50, 70)), (1, rect(50, 0, 90, 70)), (3, rect(90, 0, 110, 70)), (2, rect(0, 70, 50, 100)), (7, rect(50, 70, 90, 100))] | |
| def touches(a, b, length=1.0): | |
| """True when two bounds_m rectangles share a wall at least `length` metres long.""" | |
| along_y = min(a[3], b[3]) - max(a[1], b[1]) if (abs(a[2] - b[0]) < 1e-6 or abs(b[2] - a[0]) < 1e-6) else 0 | |
| along_x = min(a[2], b[2]) - max(a[0], b[0]) if (abs(a[3] - b[1]) < 1e-6 or abs(b[3] - a[1]) < 1e-6) else 0 | |
| return max(along_x, along_y) >= length - 1e-6 | |
| class Rectangles(unittest.TestCase): | |
| def test_largest_rectangle_of_an_l_shape(self): | |
| l_shape = cr.Polygon([(0, 0), (10, 0), (10, 4), (4, 4), (4, 10), (0, 10)]) | |
| x0, y0, x1, y1 = cr.largest_rectangle(l_shape) | |
| self.assertEqual((x1 - x0) * (y1 - y0), 40) | |
| def test_a_rectangle_is_its_own_largest_rectangle(self): | |
| self.assertEqual(cr.largest_rectangle(cr.Polygon(rect(2, 3, 9, 8))), (2.0, 3.0, 9.0, 8.0)) | |
| self.assertEqual(cr.maximal_rectangles(cr.Polygon(rect(0, 0, 5, 5))), [(0, 0, 5, 5)]) | |
| def test_maximal_rectangles_of_l_t_and_plus_shapes(self): | |
| l_shape = cr.Polygon([(0, 0), (10, 0), (10, 4), (4, 4), (4, 10), (0, 10)]) | |
| self.assertEqual(sorted(cr.maximal_rectangles(l_shape)), [(0, 0, 4, 10), (0, 0, 10, 4)]) | |
| t_shape = cr.Polygon([(0, 0), (12, 0), (12, 4), (8, 4), (8, 10), (4, 10), (4, 4), (0, 4)]) | |
| self.assertEqual(sorted(cr.maximal_rectangles(t_shape)), [(0, 0, 12, 4), (4, 0, 8, 10)]) | |
| plus = cr.Polygon([(4, 0), (8, 0), (8, 4), (12, 4), (12, 8), (8, 8), (8, 12), (4, 12), (4, 8), (0, 8), (0, 4), (4, 4)]) | |
| self.assertEqual(sorted(cr.maximal_rectangles(plus)), [(0, 4, 12, 8), (4, 0, 8, 12)]) | |
| def test_maximal_rectangles_come_largest_first_and_are_limited(self): | |
| stairs = cr.Polygon([(0, 0), (9, 0), (9, 3), (6, 3), (6, 6), (3, 6), (3, 9), (0, 9)]) | |
| found = cr.maximal_rectangles(stairs, 2) | |
| self.assertEqual(len(found), 2) | |
| self.assertGreaterEqual((found[0][2] - found[0][0]) * (found[0][3] - found[0][1]), (found[1][2] - found[1][0]) * (found[1][3] - found[1][1])) | |
| def test_contact_overlap_counts_only_shared_wall(self): | |
| self.assertEqual(cr.contact_overlap([0, 0, 10, 10], [10, 4, 20, 30]), 6) | |
| self.assertEqual(cr.contact_overlap([0, 0, 10, 10], [3, 10, 8, 20]), 5) | |
| self.assertEqual(cr.contact_overlap([0, 0, 10, 10], [10, 10, 20, 20]), 0) # corner only | |
| self.assertEqual(cr.contact_overlap([0, 0, 10, 10], [12, 0, 20, 10]), 0) # a gap | |
| def test_the_chain_gives_no_door_between_kitchen_and_bath_or_bath_and_bedroom(self): | |
| geo = {'living': [0, 0, 20, 20], 'bath': [20, 0, 40, 20], 'master': [40, 0, 60, 20], 'kitchen': [20, 20, 40, 40]} | |
| types = {'living': 'living_room', 'bath': 'bathroom', 'master': 'master_bedroom', 'kitchen': 'kitchen'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'bath'}) | |
| geo['kitchen'] = [0, 20, 20, 40] | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'bath', 'kitchen'}) | |
| def test_a_bedroom_that_only_touches_another_bedroom_is_not_connected(self): | |
| geo = {'living': [0, 0, 40, 40], 'guest': [40, 0, 60, 40], 'bedroom_3': [60, 0, 80, 40]} | |
| types = {'living': 'living_room', 'guest': 'guest_bedroom', 'bedroom_3': 'guest_bedroom'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'guest'}) | |
| def test_a_balcony_needs_a_long_wall_to_a_bedroom_but_only_a_door_width_to_the_dining_room(self): | |
| types = {'living': 'living_room', 'dining': 'dining_room', 'balcony': 'balcony'} | |
| geo = {'living': [0, 0, 40, 40], 'dining': [40, 0, 80, 40], 'balcony': [80, 0, 100, 15]} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'dining', 'balcony'}) | |
| geo['balcony'] = [0, 40, 15, 55] # 15 px of wall against the living room: not enough for a balcony opening | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'dining'}) | |
| class DeadEnds(unittest.TestCase): | |
| """A bedroom or bathroom has one door and leads nowhere; the public rooms, the kitchen and a hallway hold several portals.""" | |
| def test_a_bathroom_does_not_connect_the_dining_room_to_the_living_room(self): | |
| geo = {'living': [0, 0, 40, 40], 'bath': [40, 0, 60, 40], 'dining': [60, 0, 100, 40]} | |
| types = {'living': 'living_room', 'bath': 'bathroom', 'dining': 'dining_room'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'bath'}) | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21, strict=False), {'living', 'bath', 'dining'}) | |
| def test_a_balcony_opens_onto_a_bedroom_that_is_connected(self): | |
| geo = {'living': [0, 0, 40, 40], 'master': [40, 0, 80, 40], 'balcony': [80, 0, 120, 40]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'balcony': 'balcony'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'master', 'balcony'}) | |
| def test_the_kitchen_connects_the_living_room_and_the_dining_room(self): | |
| geo = {'living': [0, 0, 40, 40], 'kitchen': [40, 0, 70, 40], 'dining': [70, 0, 110, 40]} | |
| types = {'living': 'living_room', 'kitchen': 'kitchen', 'dining': 'dining_room'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'kitchen', 'dining'}) | |
| def test_the_kitchen_does_not_lead_on_to_a_hallway(self): | |
| geo = {'living': [0, 0, 40, 40], 'kitchen': [40, 0, 70, 40], 'hallway': [70, 0, 90, 40]} | |
| types = {'living': 'living_room', 'kitchen': 'kitchen', 'hallway': 'hallway'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'kitchen'}) | |
| def test_a_hallway_leads_on_to_the_rooms_that_open_onto_it(self): | |
| geo = {'living': [0, 0, 40, 40], 'hallway': [40, 0, 60, 40], 'master': [60, 0, 100, 40]} | |
| types = {'living': 'living_room', 'hallway': 'hallway', 'master': 'master_bedroom'} | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'hallway', 'master'}) | |
| def test_repair_does_not_count_a_dead_end_as_a_connection(self): | |
| polygons = [cr.Polygon(rect(0, 0, 100, 60))] | |
| geo = {'living': [0, 0, 40, 40], 'master': [40, 0, 60, 40], 'dining': [60, 0, 100, 40]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'dining': 'dining_room'} | |
| self.assertEqual(cr.repair_contacts(geo, types, cr.footprint_cells(polygons), 11, 21), ([], ['dining'])) | |
| class Repair(unittest.TestCase): | |
| def test_a_room_on_the_hall_leg_grows_across_the_leg_to_reach_the_living_room(self): | |
| polygons = [cr.Polygon(v) for v in ([[30, 0], [110, 0], [110, 40], [46, 40], [46, 90], [30, 90]], rect(0, 40, 30, 90), rect(46, 40, 110, 90))] | |
| geo = {'living': [30, 0, 110, 40], 'master': [0, 40, 30, 90], 'guest': [46, 40, 110, 90]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'guest': 'guest_bedroom'} | |
| moves, stranded = cr.repair_contacts(geo, types, cr.footprint_cells(polygons), 11, 21) | |
| self.assertEqual(stranded, []) | |
| self.assertEqual(geo['master'], [0, 40, 41, 90]) # just far enough for an 11 px wall against the living room | |
| self.assertEqual(moves, ['master_grows_E_11_px_to_reach_living']) | |
| self.assertEqual(geo['living'], [30, 0, 110, 40]) # rectangles that already work are left alone | |
| def test_a_room_with_no_free_floor_to_grow_into_stays_stranded(self): | |
| polygons = [cr.Polygon(rect(0, 0, 40, 40)), cr.Polygon(rect(40, 0, 90, 40)), cr.Polygon(rect(90, 0, 110, 40))] | |
| geo = {'living': [0, 0, 40, 40], 'master': [40, 0, 90, 40], 'bath': [90, 0, 110, 40]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'bath': 'bathroom'} | |
| self.assertEqual(cr.repair_contacts(geo, types, cr.footprint_cells(polygons), 11, 21), ([], ['bath'])) | |
| def test_growth_is_capped(self): | |
| polygons = [cr.Polygon(rect(0, 0, 40, 20)), cr.Polygon(rect(0, 20, 20, 120)), cr.Polygon(rect(20, 20, 40, 120))] | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'guest': 'guest_bedroom'} | |
| capped = {'living': [0, 0, 40, 20], 'master': [0, 20, 20, 40], 'guest': [20, 100, 40, 120]} | |
| foot = cr.footprint_cells(polygons) | |
| self.assertEqual(cr.repair_contacts(dict(capped), types, foot, 11, 21, max_growth=2.0)[1], ['guest']) | |
| free_run = dict(capped) | |
| self.assertEqual(cr.repair_contacts(free_run, types, foot, 11, 21, max_growth=6.0)[1], []) | |
| self.assertEqual(free_run['guest'], [20, 20, 40, 120]) | |
| def test_the_living_room_takes_the_arm_of_its_polygon_that_the_other_rooms_open_onto(self): | |
| candidates = {'living': [[60, 20, 100, 80], [30, 50, 100, 80]], 'master': [[20, 80, 60, 120]]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'dining': 'dining_room'} | |
| options = [([60, 20, 100, 80], None, 'native', None), ([30, 50, 100, 80], None, 'native', None)] | |
| rects, chosen = cr._pick_rectangles(candidates, types, options, 0.1, lambda rid: 1.0, 11, 21) | |
| self.assertEqual(rects['living'], [30, 50, 100, 80]) | |
| self.assertEqual(chosen, options[1]) | |
| def test_a_room_takes_a_smaller_maximal_rectangle_when_only_that_one_touches_the_living_room(self): | |
| # the guest polygon is a 40 x 40 block set 10 px away from the living room plus a 10 x 20 neck that reaches it | |
| candidates = {'living': [[0, 0, 40, 40]], 'guest': [[50, 0, 90, 40], [40, 20, 90, 40]]} | |
| types = {'living': 'living_room', 'guest': 'guest_bedroom', 'dining': 'dining_room'} | |
| rects, _ = cr._pick_rectangles(candidates, types, [([0, 0, 40, 40], None, 'native', None)], 0.1, lambda rid: 1.5, 11, 21) | |
| self.assertEqual(rects['guest'], [40, 20, 90, 40]) | |
| def test_the_largest_rectangles_are_kept_when_nothing_is_wrong(self): | |
| candidates = {'living': [[0, 0, 40, 40]], 'guest': [[40, 0, 80, 40], [40, 0, 60, 60]]} | |
| types = {'living': 'living_room', 'guest': 'guest_bedroom', 'dining': 'dining_room'} | |
| rects, _ = cr._pick_rectangles(candidates, types, [([0, 0, 40, 40], None, 'native', None)], 0.1, lambda rid: 1.5, 11, 21) | |
| self.assertEqual(rects['guest'], [40, 0, 80, 40]) | |
| def test_footprint_raster_marks_the_cells_inside_the_polygons(self): | |
| foot = cr.footprint_cells([cr.Polygon(rect(1, 2, 4, 5))]) | |
| self.assertEqual(int(foot.sum()), 9) | |
| self.assertTrue(foot[2:5, 1:4].all()) | |
| class Conversion(unittest.TestCase): | |
| def test_a_plan_without_the_old_six_room_rule_converts(self): | |
| plan, reason = cr.convert(record(FLAT)) | |
| self.assertIsNone(reason, reason) | |
| types = {r['room_id']: r['room_type'] for r in plan['rooms']} | |
| self.assertEqual(types['dining'], 'dining_room') | |
| self.assertEqual(set(types), {'kitchen', 'dining', 'bath', 'master', 'living', 'guest', 'balcony'}) | |
| self.assertEqual(plan['rplan_conversion']['dining_source'], 'split_living') | |
| def test_no_balcony_is_fine(self): | |
| plan, reason = cr.convert(record(FLAT[:-1])) | |
| self.assertIsNone(reason, reason) | |
| self.assertNotIn('balcony', {r['room_id'] for r in plan['rooms']}) | |
| def test_missing_core_rooms_are_named(self): | |
| for dropped, reason in ((2, 'no_kitchen'), (3, 'no_bathroom'), (0, 'no_living_room')): | |
| rows = [(l, p) for l, p in FLAT if l != dropped] | |
| self.assertEqual(cr.convert(record(rows))[1], reason) | |
| self.assertEqual(cr.convert(record([(l, p) for l, p in FLAT if l not in (1, 7)]))[1], 'no_bedroom') | |
| def test_a_record_with_nan_vertices_is_named_not_crashed_on(self): | |
| self.assertEqual(cr.convert(dict(record(FLAT), corrupt=True))[1], 'corrupt_record_nan_vertices') | |
| def test_a_single_bedroom_flat_is_refused_unless_asked_for(self): | |
| rows = [(l, p) for l, p in FLAT if l != 7] | |
| self.assertEqual(cr.convert(record(rows))[1], 'no_second_bedroom') | |
| plan, reason = cr.convert(record(rows), require_guest=False) | |
| self.assertIsNone(reason, reason) | |
| self.assertNotIn('guest', {r['room_id'] for r in plan['rooms']}) | |
| def test_further_bedrooms_get_their_own_ids(self): | |
| rows = FLAT[:-1] + [(5, rect(70, 60, 110, 90)), (8, rect(40, 90, 110, 120))] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| ids = {r['room_id']: r['room_type'] for r in plan['rooms']} | |
| self.assertEqual({k: v for k, v in ids.items() if k.startswith('bedroom_')}, {'bedroom_3': 'guest_bedroom', 'bedroom_4': 'guest_bedroom'}) | |
| self.assertEqual(ids['master'], 'master_bedroom') | |
| self.assertEqual(ids['guest'], 'guest_bedroom') | |
| def test_a_study_becomes_a_further_bedroom_and_never_the_guest_while_a_real_bedroom_exists(self): | |
| rows = FLAT + [(6, rect(70, 60, 110, 90)), (10, rect(70, 90, 90, 98)), (11, rect(90, 90, 110, 98))] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| ids = {r['room_id']: r['room_type'] for r in plan['rooms']} | |
| self.assertEqual(ids['bedroom_3'], 'guest_bedroom') | |
| self.assertNotIn('study', ids) | |
| self.assertIn('study_used_as_bedroom', plan['rplan_conversion']['notes']) | |
| self.assertIn('entrance_not_represented', plan['rplan_conversion']['notes']) | |
| self.assertIn('storage_not_represented', plan['rplan_conversion']['notes']) | |
| def test_profiles_set_the_shortest_sides_by_group(self): | |
| self.assertEqual(cr.min_side_for(cr.PROFILES['balanced'], 'bedroom_3'), 2.5) | |
| self.assertEqual(cr.min_side_for(cr.PROFILES['balanced'], 'living'), 2.8) | |
| self.assertEqual(cr.min_side_for(cr.PROFILES['wide'], 'living'), 1.8) | |
| self.assertEqual(cr.min_side_for(dict(cr.MIN_SIDE_M), 'hallway_2'), cr.MIN_SIDE_M['hallway']) | |
| def test_a_bedroom_narrower_than_the_profile_is_refused_by_name(self): | |
| rows = [(1, rect(0, 0, 40, 45)) if l == 1 else (l, p) for l, p in FLAT] | |
| narrow = [(l, rect(16, 45, 40, 90)) if l == 7 else (l, p) for l, p in rows] # the guest bedroom becomes 24 px wide, about 2.4 m | |
| self.assertEqual(cr.convert(record(narrow), min_side=dict(cr.PROFILES['balanced']))[1], 'room_too_small_guest') | |
| self.assertIsNone(cr.convert(record(narrow), min_side=dict(cr.PROFILES['wide']))[1]) | |
| def test_a_study_is_the_guest_bedroom_of_a_one_bedroom_flat(self): | |
| rows = [(6 if l == 7 else l, p) for l, p in FLAT] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| self.assertEqual({r['room_id']: r['room_type'] for r in plan['rooms']}['guest'], 'guest_bedroom') | |
| def test_an_l_shaped_living_room_gives_its_dining_room(self): | |
| living = [[40, 0], [110, 0], [110, 30], [70, 30], [70, 90], [40, 90]] | |
| rows = [(0, living), (1, rect(0, 0, 40, 45)), (7, rect(0, 45, 40, 90)), (2, rect(70, 30, 110, 60)), (3, rect(70, 60, 110, 90)), (9, rect(40, 90, 70, 105))] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| self.assertEqual(plan['rplan_conversion']['dining_source'], 'living_leftover_rectangle') | |
| dining = next(r['bounds_m'] for r in plan['rooms'] if r['room_id'] == 'dining') | |
| living_room = next(r['bounds_m'] for r in plan['rooms'] if r['room_id'] == 'living') | |
| x, y = overlap_m(dining, living_room) | |
| self.assertFalse(x > 0.01 and y > 0.01) # the dining room does not overlap the living room | |
| def test_the_bigger_public_room_is_the_living_room(self): | |
| # the guest bedroom only touches the narrow arm of the L-shaped living room, so the arm becomes the living rectangle and the big block | |
| # the dining room; the two then trade names because the block is the larger one | |
| living = [[90, 0], [150, 0], [150, 80], [50, 80], [50, 55], [90, 55]] | |
| rows = [(0, living), (7, rect(20, 55, 50, 95)), (1, rect(50, 80, 110, 120)), (2, rect(150, 0, 180, 40)), (3, rect(150, 40, 180, 70))] | |
| plan, reason = cr.convert(record(rows), min_side={'default': 2.0}) | |
| self.assertIsNone(reason, reason) | |
| self.assertTrue(plan['rplan_conversion']['living_dining_swapped']) | |
| bounds = {r['room_id']: r['bounds_m'] for r in plan['rooms']} | |
| area = lambda b: (b[2] - b[0]) * (b[3] - b[1]) | |
| self.assertGreater(area(bounds['living']), area(bounds['dining'])) | |
| self.assertTrue(touches(bounds['guest'], bounds['dining'])) | |
| def test_the_rooms_do_not_trade_names_when_that_would_make_the_dining_room_an_overlong_strip(self): | |
| living = [[90, 0], [150, 0], [150, 80], [50, 80], [50, 55], [90, 55]] | |
| rows = [(0, living), (7, rect(20, 55, 50, 95)), (1, rect(50, 80, 110, 120)), (2, rect(150, 0, 180, 40)), (3, rect(150, 40, 180, 70))] | |
| plan, reason = cr.convert(record(rows), min_side={'default': 2.0}, max_aspect=3.0) # the strip is 4:1 | |
| self.assertIsNone(reason, reason) | |
| self.assertFalse(plan['rplan_conversion']['living_dining_swapped']) | |
| def test_nothing_is_swapped_when_the_living_room_is_already_the_bigger_one(self): | |
| plan, reason = cr.convert(record(FLAT)) | |
| self.assertIsNone(reason, reason) | |
| self.assertFalse(plan['rplan_conversion']['living_dining_swapped']) | |
| def test_a_narrow_leftover_falls_back_to_cutting_the_living_room(self): | |
| living = [[40, 0], [110, 0], [110, 6], [70, 6], [70, 90], [40, 90]] # the left-over is only 6 px deep | |
| rows = [(0, living), (1, rect(0, 0, 40, 45)), (7, rect(0, 45, 40, 90)), (2, rect(70, 6, 110, 36)), (3, rect(70, 36, 110, 66)), (9, rect(40, 90, 70, 105))] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| self.assertEqual(plan['rplan_conversion']['dining_source'], 'split_living') | |
| def test_rooms_never_overlap_and_the_scale_is_uniform(self): | |
| plan, _ = cr.convert(record(FLAT)) | |
| rooms = plan['rooms'] | |
| for i, a in enumerate(rooms): | |
| for b in rooms[i + 1:]: | |
| x, y = overlap_m(a['bounds_m'], b['bounds_m']) | |
| self.assertFalse(x > 0.01 and y > 0.01, (a['room_id'], b['room_id'])) | |
| self.assertLessEqual(max(r['bounds_m'][2] for r in rooms), cr.MAX_EXTENT_M + 1e-6) | |
| def test_plans_that_lost_too_much_area_can_be_filtered(self): | |
| notched_master = [[0, 0], [40, 0], [40, 30], [20, 30], [20, 45], [0, 45]] | |
| rows = [(l, notched_master if l == 1 else p) for l, p in FLAT] | |
| plan, reason = cr.convert(record(rows), min_retained=0.0) | |
| self.assertIsNone(reason, reason) | |
| self.assertLess(plan['rplan_conversion']['retained_area_fraction'], 1.0) | |
| self.assertTrue(cr.convert(record(rows), min_retained=0.999)[1].startswith('retained_area')) | |
| def test_a_fixed_scale_keeps_the_size_of_each_plan(self): | |
| small, _ = cr.convert(record(FLAT), fixed_scale=0.05, min_side={'default': 0.3, 'bath': 0.3, 'balcony': 0.3}, max_aspect=99) | |
| large, _ = cr.convert(record(FLAT), fixed_scale=0.10, min_side={'default': 0.5}, max_aspect=99) | |
| self.assertAlmostEqual(small['rplan_conversion']['scale_m_per_px'], 0.05) | |
| self.assertAlmostEqual(large['rplan_conversion']['scale_m_per_px'], 0.10) | |
| extent = lambda plan: max(r['bounds_m'][2] for r in plan['rooms']) | |
| self.assertAlmostEqual(extent(large) / extent(small), 2.0, places=1) | |
| def test_a_scale_range_stops_a_plan_from_being_stretched_to_the_target(self): | |
| free, _ = cr.convert(record(FLAT), scale_target=400.0, min_side={'default': 0.5}, max_aspect=99) | |
| bounded, _ = cr.convert(record(FLAT), scale_target=400.0, scale_range=(0.05, 0.09), min_side={'default': 0.5}, max_aspect=99) | |
| self.assertGreater(free['rplan_conversion']['scale_m_per_px'], 0.09) | |
| self.assertAlmostEqual(bounded['rplan_conversion']['scale_m_per_px'], 0.09) | |
| reached, _ = cr.convert(record(FLAT), scale_target=60.0, scale_range=(0.05, 0.5), min_side={'default': 0.5}, max_aspect=99) | |
| self.assertAlmostEqual(sum((r['bounds_m'][2] - r['bounds_m'][0]) * (r['bounds_m'][3] - r['bounds_m'][1]) for r in reached['rooms']), 60.0, delta=1.0) | |
| def test_a_tiny_room_is_refused(self): | |
| rows = [(l, rect(40, 90, 70, 92) if l == 9 else p) for l, p in FLAT] | |
| self.assertEqual(cr.convert(record(rows))[1], 'room_too_small_balcony') | |
| def test_a_very_long_thin_room_is_refused(self): | |
| rows = [(l, rect(33, 0, 40, 45) if l == 1 else p) for l, p in FLAT] | |
| self.assertEqual(cr.convert(record(rows), min_side={'default': 0.5})[1], 'room_too_elongated_master') | |
| class Hallways(unittest.TestCase): | |
| LIVING = cr.Polygon([(30, 0), (110, 0), (110, 40), (72, 40), (72, 120), (60, 120), (60, 40), (30, 40)]) # a bar with a long leg | |
| # a bar with a leg; the master and the guest bedroom open onto the leg and nothing else reaches the bar | |
| LEG = [(0, [[30, 0], [110, 0], [110, 40], [72, 40], [72, 120], [60, 120], [60, 40], [30, 40]]), (2, rect(72, 40, 110, 62)), (3, rect(30, 40, 60, 62)), | |
| (1, rect(30, 62, 60, 120)), (7, rect(72, 62, 102, 120))] | |
| def test_the_free_part_of_the_living_polygon_becomes_a_hallway_when_it_connects_rooms(self): | |
| geo = {'living': [30, 0, 110, 40], 'master': [30, 60, 60, 120], 'guest': [72, 60, 102, 120]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'guest': 'guest_bedroom'} | |
| self.assertEqual(cr.add_hallway(geo, types, self.LIVING, 11, 21, 10, 100), 'hallway') | |
| self.assertEqual(geo['hallway'], [60, 40, 72, 120]) | |
| self.assertEqual(types['hallway'], 'hallway') | |
| self.assertEqual(cr.reachable_rooms(geo, types, 11, 21), {'living', 'master', 'guest', 'hallway'}) | |
| def test_no_hallway_is_added_when_every_room_already_reaches_the_living_room(self): | |
| geo = {'living': [30, 0, 110, 40], 'master': [30, 40, 60, 100]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom'} | |
| self.assertIsNone(cr.add_hallway(geo, types, self.LIVING, 11, 21, 10, 100)) | |
| def test_a_hallway_that_is_too_narrow_is_not_added(self): | |
| geo = {'living': [30, 0, 110, 40], 'master': [30, 60, 60, 120], 'guest': [72, 60, 102, 120]} | |
| types = {'living': 'living_room', 'master': 'master_bedroom', 'guest': 'guest_bedroom'} | |
| self.assertIsNone(cr.add_hallway(geo, types, self.LIVING, 11, 21, 13, 100)) | |
| def test_a_flat_whose_bedrooms_open_onto_a_hall_leg_converts_with_a_hallway(self): | |
| plan, reason = cr.convert(record(self.LEG)) | |
| self.assertIsNone(reason, reason) | |
| rooms = {r['room_id']: r for r in plan['rooms']} | |
| self.assertEqual(rooms['hallway']['room_type'], 'hallway') | |
| self.assertEqual(plan['rplan_conversion']['access'], 'direct') | |
| self.assertNotIn(['hallway', 'living_room'], plan['open_room_type_pairs']) # a hallway is entered through a door | |
| self.assertEqual(plan['rooms'][0]['room_id'], 'hallway') # and listed first, see ROOM_ORDER | |
| for bedroom in ('master', 'guest'): | |
| self.assertTrue(touches(rooms[bedroom]['bounds_m'], rooms['hallway']['bounds_m']), bedroom) | |
| def test_without_hallways_the_same_flat_is_refused(self): | |
| self.assertTrue(cr.convert(record(self.LEG), hallways=False)[1].startswith('cannot_reach_living')) | |
| def test_flats_that_need_no_hallway_get_none(self): | |
| plan, _ = cr.convert(record(FLAT)) | |
| self.assertNotIn('hallway', {r['room_id'] for r in plan['rooms']}) | |
| self.assertNotIn(['hallway', 'living_room'], plan['open_room_type_pairs']) | |
| class AccessInConversion(unittest.TestCase): | |
| # a third bedroom that touches only the second bedroom | |
| BEHIND = [(0, rect(80, 0, 110, 90)), (1, rect(40, 0, 80, 45)), (7, rect(40, 45, 80, 90)), (5, rect(0, 45, 40, 90)), (2, rect(110, 0, 150, 30)), | |
| (3, rect(110, 30, 150, 60))] | |
| def test_a_bedroom_behind_another_bedroom_is_kept_but_marked_indirect(self): | |
| plan, reason = cr.convert(record(self.BEHIND)) | |
| self.assertIsNone(reason, reason) | |
| self.assertEqual(plan['rplan_conversion']['access'], 'indirect') | |
| def test_strict_access_refuses_it(self): | |
| self.assertEqual(cr.convert(record(self.BEHIND), strict_access=True)[1], 'cannot_reach_living_guest_bedroom') | |
| def test_a_balcony_that_only_touches_the_bathroom_is_left_out(self): | |
| rows = FLAT[:-1] + [(9, rect(110, 30, 125, 60))] | |
| plan, reason = cr.convert(record(rows)) | |
| self.assertIsNone(reason, reason) | |
| self.assertNotIn('balcony', {r['room_id'] for r in plan['rooms']}) | |
| self.assertIn('balcony_dropped_not_reachable', plan['rplan_conversion']['notes']) | |
| self.assertEqual(cr.convert(record(rows), drop_balcony=False)[1], 'cannot_reach_living_balcony') | |
| def test_a_flat_whose_rooms_all_touch_a_public_room_is_direct(self): | |
| plan, reason = cr.convert(record(FLAT)) | |
| self.assertIsNone(reason, reason) | |
| self.assertEqual(plan['rplan_conversion']['access'], 'direct') | |
| class ContactRepairInConversion(unittest.TestCase): | |
| def test_the_master_on_the_hall_leg_is_connected_by_growing(self): | |
| plan, reason = cr.convert(record(HALL)) | |
| self.assertIsNone(reason, reason) | |
| self.assertTrue(any(m.startswith('master_grows_E') for m in plan['rplan_conversion']['contact_repairs']), plan['rplan_conversion']) | |
| bounds = {r['room_id']: r['bounds_m'] for r in plan['rooms']} | |
| self.assertTrue(touches(bounds['master'], bounds['dining']) or touches(bounds['master'], bounds['living'])) | |
| def test_without_the_repair_the_master_floats_free(self): | |
| plan, reason = cr.convert(record(HALL), repair=False) | |
| self.assertIsNone(reason, reason) | |
| bounds = {r['room_id']: r['bounds_m'] for r in plan['rooms']} | |
| self.assertFalse(any(touches(bounds['master'], bounds[other], 0.5) for other in bounds if other != 'master')) | |
| def test_an_en_suite_bathroom_cannot_be_connected(self): | |
| self.assertEqual(cr.convert(record(ENSUITE))[1], 'cannot_reach_living_bathroom') | |
| if __name__ == '__main__': | |
| unittest.main() | |