Datasets:
Download code/robohome/src/engine/tests/test_native_repair_constraints.py from RoboParty/RoboHome: direct link, hf CLI and curl.
- Browser
- Download file 21.7 kB
-
https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/tests/test_native_repair_constraints.py
- Command line
-
hf download hf://datasets/RoboParty/RoboHome/code/robohome/src/engine/tests/test_native_repair_constraints.py
-
curl -L -o test_native_repair_constraints.py https://huggingface.co/datasets/RoboParty/RoboHome/resolve/main/code/robohome/src/engine/tests/test_native_repair_constraints.py
21.7 kB
| """Synthetic native-search contracts, not imported-mesh physics evidence.""" | |
| import copy | |
| import importlib | |
| import json | |
| import math | |
| from pathlib import Path | |
| import sys | |
| import tempfile | |
| import unittest | |
| from unittest.mock import patch | |
| import engine as opt3 | |
| ROOT = Path(next(iter(opt3.__path__))) | |
| NATIVE = ROOT / 'source/scripts/internal/scene_generation/real_assets' | |
| sys.path.insert(0, str(NATIVE)) | |
| joint = importlib.import_module( | |
| 'engine.source.scripts.internal.scene_generation.real_assets.joint_layout') | |
| IDENTITY = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1] | |
| def row(eid='a', center=(2., 2.), **extra): | |
| return dict(entity_id=eid, room_id='room', role='lamp', asset_id='lamp-' + eid, | |
| center_xy_m=list(center), yaw_deg=0., base_z_m=0., | |
| dimensions_m=[.4, .4, .7], **extra) | |
| def candidate(entity, **changes): | |
| result = dict(asset_id=entity['asset_id'], dimensions_m=list(entity['dimensions_m']), | |
| canonical_front_axis='+Y', units='m', up_axis='Z', category='lamp', | |
| canonical_transform=list(IDENTITY), asset_sha256='a' * 64, | |
| runtime_sha256='b' * 64, pool_candidate_key='record-' + entity['entity_id'], | |
| source_geometry_probe_key='synthetic-probe', | |
| probe_implementation_sha256='synthetic-implementation', | |
| source_dependency_sha256={'synthetic': 'c' * 64}, | |
| provisional_geometry_dimensions=False, | |
| prepared_source_frame_contract=dict(canonical_front_axis='+Y', units='m', up_axis='Z')) | |
| result.update(changes) | |
| return result | |
| def constraints(**changes): | |
| result = dict(schema='rhg.search-constraints.v1', fixed_entities=[], | |
| asset_locks=[], nogoods=[], reservations=[]) | |
| result.update(changes) | |
| return result | |
| class NativeRepairTests(unittest.TestCase): | |
| def test_saved_native_binding_preserves_specialized_semantic_role(self): | |
| from bind_fixed_layout import semantic_key | |
| entity = dict(row(), role='vase', entity_id='counter_decor', | |
| asset_pool_key='vase', layout_slot_id='vase', binding_semantic_key='bowl', | |
| category='bowl', source_dimensions_m=[.4, .4, .7], asset_sha256='a' * 64) | |
| self.assertEqual(semantic_key(entity), 'bowl') | |
| entity.pop('asset_sha256') | |
| self.assertEqual(semantic_key(entity), 'vase') | |
| def test_attribute_record_identity_is_distinct_from_pool_identity(self): | |
| native = candidate(row()) | |
| lock = dict(row(), pool_candidate_key=native['pool_candidate_key'], | |
| style_attribute_consumption=dict(pool_candidate_key=native['pool_candidate_key'], | |
| record_id='attribute-release-record')) | |
| helper = self.helper(constraints(fixed_entities=[lock])) | |
| self.assertIsNone(helper.candidate_error('a', native)) | |
| def setUp(self): | |
| self.temp = tempfile.TemporaryDirectory() | |
| self.addCleanup(self.temp.cleanup) | |
| self.output = Path(self.temp.name) | |
| self.a, self.b = row(), row('b', (4., 4.)) | |
| self.manifest = dict(entities=[self.a, self.b], floor_plan=dict( | |
| rooms=[dict(room_id='room', bounds_m=[0., 0., 6., 6.])], | |
| wall_height_m=3., wall_thickness_m=.2)) | |
| self.request = dict(schema='rhg.scene_request.v1', mode='pool', | |
| task_text='Synthetic native solver contract fixture', _sha256='d' * 64, | |
| furniture=[dict(entity_id=e['entity_id'], candidate_asset_ids=[e['asset_id']]) | |
| for e in self.manifest['entities']], | |
| search=dict(grid_step_m=.4, max_nodes_per_room=10000, max_seconds_per_room=10), | |
| support_region_policy='legacy_vertex', floor_cover_policy='legacy', | |
| functional_layout_policy='legacy', search_constraints=constraints()) | |
| def solve(self, candidates=None): | |
| return joint.solve(self.manifest, candidates or [candidate(self.a), candidate(self.b)], | |
| self.request, self.output) | |
| def helper(self, data): | |
| try: | |
| module = importlib.import_module( | |
| 'engine.source.scripts.internal.scene_generation.real_assets.repair_constraints') | |
| except ModuleNotFoundError as error: | |
| self.fail('Native repair constraint validation is missing: ' + str(error)) | |
| return module.RepairConstraints(dict(self.request, search_constraints=data), self.manifest) | |
| def test_fixed_pose_is_actual_singleton_and_preserves_inputs(self): | |
| fixed = dict(self.a, center_xy_m=[1.237, 2.719], yaw_deg=37.125, base_z_m=.031) | |
| self.request['search_constraints'] = constraints(fixed_entities=[fixed]) | |
| before = copy.deepcopy((self.manifest, self.request)) | |
| chosen, report = self.solve() | |
| for key in ('center_xy_m', 'yaw_deg', 'base_z_m', 'dimensions_m'): | |
| self.assertEqual(chosen['a'][1][key], fixed[key]) | |
| self.assertEqual((self.manifest, self.request), before) | |
| enforcement = report['search_constraints'] | |
| self.assertTrue(enforcement['enforced']) | |
| self.assertEqual(enforcement['request_sha256'], self.request['_sha256']) | |
| self.assertEqual(enforcement['fixed_entity_ids'], ['a']) | |
| self.assertEqual(enforcement['consumed_counts']['fixed_entities'], 1) | |
| def test_wall_mount_axis_selection_retains_fixed_pose_record(self): | |
| parent = dict(self.a, wall_anchor='S', center_xy_m=[2., .5], _original_center=[2., .5], | |
| contract_yaw_deg=0., _original_contract_yaw=0.) | |
| child = dict(self.b, wall_anchor='S', layout_relation='wall_mounted', | |
| supported_by_entity_id='a', center_xy_m=[2., .305], base_z_m=1.4) | |
| self.request['search_constraints'] = constraints(fixed_entities=[child]) | |
| helper = self.helper(self.request['search_constraints']) | |
| with patch('wall_mount_layout.align', side_effect=lambda pose, room, plan, inner, *args: (pose, inner)): | |
| poses = joint.positions(child, candidate(child), self.manifest['floor_plan']['rooms'][0], | |
| self.manifest['floor_plan'], {'a': parent}, self.request, constraints=helper) | |
| self.assertTrue(poses) | |
| self.assertTrue(all(p['center_xy_m'] == child['center_xy_m'] for p in poses)) | |
| def test_fixed_native_dimensions_and_asset_must_match(self): | |
| for change in (dict(asset_id='absent'), dict(dimensions_m=[.5, .4, .7])): | |
| with self.subTest(change=change): | |
| self.request['search_constraints'] = constraints(fixed_entities=[dict(self.a, **change)]) | |
| with self.assertRaises((ValueError, joint.JointLayoutFailure)): | |
| self.solve() | |
| def test_fixed_pose_cannot_bypass_collision_or_room_bounds(self): | |
| for fixed in ([self.a, dict(self.b, center_xy_m=self.a['center_xy_m'])], | |
| [dict(self.a, center_xy_m=[-.5, 2.])], | |
| [dict(self.a, base_z_m=2.9)]): | |
| with self.subTest(fixed=fixed): | |
| self.request['search_constraints'] = constraints(fixed_entities=fixed) | |
| with self.assertRaises(joint.JointLayoutFailure): | |
| self.solve() | |
| def test_fixed_entities_are_obstacles_before_mutable_entities(self): | |
| self.b['center_xy_m'] = list(self.a['center_xy_m']) | |
| self.request['search_constraints'] = constraints(fixed_entities=[self.b]) | |
| visited = [] | |
| original = joint.positions | |
| def recording(entity, *args, **kwargs): | |
| visited.append(entity['entity_id']) | |
| return original(entity, *args, **kwargs) | |
| with patch.object(joint, 'positions', side_effect=recording): | |
| chosen, _ = self.solve() | |
| self.assertEqual(visited[0], 'b') | |
| self.assertEqual(chosen['b'][1]['center_xy_m'], self.b['center_xy_m']) | |
| self.assertFalse(joint.overlap_3d(chosen['a'][1], chosen['b'][1])) | |
| def test_asset_locks_intersect_original_domain_for_mutable_entities(self): | |
| original = candidate(self.a) | |
| lock = dict(self.a, asset_sha256=original['asset_sha256'], | |
| runtime_sha256=original['runtime_sha256'], | |
| pool_candidate_key=original['pool_candidate_key'], | |
| canonical_transform=list(IDENTITY)) | |
| wrong = candidate(self.a, pool_candidate_key='000-first', asset_sha256='f' * 64) | |
| self.request['search_constraints'] = constraints(asset_locks=[lock, self.b]) | |
| declared = copy.deepcopy(self.request['furniture']) | |
| chosen, report = self.solve([wrong, original, candidate(self.b)]) | |
| self.assertEqual(chosen['a'][0]['pool_candidate_key'], original['pool_candidate_key']) | |
| self.assertEqual(report['domain_sizes']['a'], 1) | |
| self.assertEqual(report['search_constraints']['consumed_counts']['asset_locks'], 2) | |
| self.assertEqual(self.request['furniture'], declared) | |
| def test_asset_lock_cannot_expand_declared_request_domain(self): | |
| lock = dict(self.a, asset_id='not-declared') | |
| self.request['search_constraints'] = constraints(asset_locks=[lock, self.b]) | |
| with self.assertRaises((ValueError, joint.JointLayoutFailure)): | |
| self.solve([candidate(lock), candidate(self.a), candidate(self.b)]) | |
| def test_joint_nogood_does_not_ban_each_member(self): | |
| self.request['search_constraints'] = constraints( | |
| fixed_entities=[self.a], nogoods=[dict(code='bad_pair', entities=[self.a, self.b])]) | |
| chosen, _ = self.solve() | |
| self.assertEqual(chosen['a'][1]['center_xy_m'], self.a['center_xy_m']) | |
| self.assertNotEqual(chosen['b'][1]['center_xy_m'], self.b['center_xy_m']) | |
| def test_cross_room_nogood_is_checked_at_assignment_completion(self): | |
| self.b['room_id'] = 'other' | |
| self.manifest['floor_plan']['rooms'].append(dict(room_id='other', bounds_m=[0., 0., 6., 6.])) | |
| self.request['search_constraints'] = constraints( | |
| nogoods=[dict(code='bad_pair', entities=[self.a, self.b])]) | |
| chosen, _ = self.solve() | |
| self.assertFalse(all(chosen[e['entity_id']][1]['center_xy_m'] == e['center_xy_m'] | |
| and chosen[e['entity_id']][1]['yaw_deg'] == e['yaw_deg'] | |
| for e in (self.a, self.b))) | |
| def test_reservation_changes_the_native_search(self): | |
| self.request['search_constraints'] = constraints(reservations=[dict( | |
| room_id='room', bounds_xy_m=[1.7, 1.7, 2.3, 2.3], purpose='access')]) | |
| chosen, _ = self.solve() | |
| self.assertFalse(1.5 < chosen['a'][1]['center_xy_m'][0] < 2.5 | |
| and 1.5 < chosen['a'][1]['center_xy_m'][1] < 2.5) | |
| def test_floor_cover_completion_cannot_lift_a_fixed_entity(self): | |
| self.request['floor_cover_policy'] = 'measured_support' | |
| self.request['search_constraints'] = constraints(fixed_entities=[self.a]) | |
| def elevate(assigned, room_id, plan): | |
| updated = copy.deepcopy(assigned) | |
| updated['a']['base_z_m'] += .03 | |
| return updated, None | |
| with patch('floor_cover_layout.resolve', side_effect=elevate): | |
| with self.assertRaises(joint.JointLayoutFailure): | |
| self.solve() | |
| def test_floor_cover_rug_branch_checks_every_updated_entity(self): | |
| rug = dict(self.b, role='rug', dimensions_m=[1.5, 1.5, .01]) | |
| self.manifest['entities'][1] = rug | |
| self.request['floor_cover_policy'] = 'measured_support' | |
| self.request['search_constraints'] = constraints(fixed_entities=[self.a]) | |
| def elevate(assigned, room_id, plan): | |
| updated = copy.deepcopy(assigned) | |
| updated['a']['base_z_m'] += .03 | |
| return updated, None | |
| with patch('floor_cover_layout.resolve', side_effect=elevate) as resolver: | |
| with self.assertRaises(joint.JointLayoutFailure): | |
| self.solve([candidate(self.a), candidate(rug, category='rug')]) | |
| self.assertTrue(any('b' in call.args[0] for call in resolver.call_args_list)) | |
| def test_asset_lock_rejects_malformed_native_candidate_dimensions(self): | |
| helper = self.helper(constraints(asset_locks=[self.a, self.b])) | |
| for dimensions in ([.4, .4], [.4, .4, math.nan], [.4, .4, .7, 1.], [True, .4, .7]): | |
| with self.subTest(dimensions=dimensions): | |
| self.assertTrue(helper.candidate_error('a', candidate(self.a, dimensions_m=dimensions))) | |
| def test_source_identity_uses_native_binding_runtime_defaults(self): | |
| source = candidate(self.a, canonical_path='/synthetic/source.glb') | |
| source.pop('runtime_sha256') | |
| source.pop('canonical_transform') | |
| lock = dict(self.a, canonical_path=source['canonical_path'], runtime_path=source['canonical_path'], | |
| asset_sha256=source['asset_sha256'], runtime_sha256=source['asset_sha256'], | |
| canonical_transform=list(IDENTITY)) | |
| helper = self.helper(constraints(asset_locks=[lock, self.b])) | |
| self.assertIsNone(helper.candidate_error('a', source)) | |
| def test_fixed_supported_child_respects_dependency_and_native_support_height(self): | |
| self.b.update(center_xy_m=[2., 2.], support_entity_id='a', base_z_m=1.2) | |
| self.a['dimensions_m'] = [1., 1., .7] | |
| support = importlib.import_module('source_support') | |
| footprint = [[-.2, -.2], [.2, -.2], [.2, .2], [-.2, .2]] | |
| child = candidate(self.b, source_support_geometry=dict( | |
| schema=support.SUPPORT_GEOMETRY_SCHEMA, producer_sha256=support.geometry_signature(), | |
| bottom_band_m=support.SOURCE_BOTTOM_BAND_M, visual_footprint_polygon_xy_m=footprint, | |
| bottom_vertices_xyz_m=[[*p, 0.] for p in footprint])) | |
| parent = candidate(self.a, source_support_plane_offset_m=.7) | |
| self.request['search_constraints'] = constraints(fixed_entities=[self.a, self.b]) | |
| visited = [] | |
| original = joint.positions | |
| def recording(entity, *args, **kwargs): | |
| visited.append(entity['entity_id']) | |
| return original(entity, *args, **kwargs) | |
| with patch.object(joint, 'positions', side_effect=recording): | |
| with self.assertRaises(joint.JointLayoutFailure): | |
| self.solve([parent, child]) | |
| self.assertEqual(visited[:2], ['a', 'b']) | |
| def test_repair_of_derived_floor_cover_parent_has_no_artificial_support_cycle(self): | |
| self.a.update(support_entity_id='b', base_z_m=.01, derived_support_relation=dict( | |
| source='actual_floor_cover_mesh_and_furniture_bottom_contacts', | |
| authored_support_parent_entity_id=None, added_support_parent_entity_id='b')) | |
| rug = dict(self.b, role='rug', dimensions_m=[1.5, 1.5, .01], _layout_parent_id='a') | |
| self.manifest['entities'][1] = rug | |
| self.request['floor_cover_policy'] = 'measured_support' | |
| self.request['search_constraints'] = constraints(fixed_entities=[self.a, rug]) | |
| def recompute_contact(assigned, room_id, plan): | |
| self.assertEqual(assigned['a']['base_z_m'], 0.) | |
| self.assertNotIn('support_entity_id', assigned['a']) | |
| updated = copy.deepcopy(assigned) | |
| updated['a'].update(base_z_m=.01, support_entity_id='b', | |
| derived_support_relation=copy.deepcopy(self.a['derived_support_relation'])) | |
| return updated, None | |
| original = copy.deepcopy(self.manifest) | |
| with patch('floor_cover_layout.resolve', side_effect=recompute_contact): | |
| chosen, report = self.solve([candidate(self.a), candidate(rug, category='rug')]) | |
| self.assertEqual(chosen['a'][1]['base_z_m'], .01) | |
| self.assertEqual(report['original_entities'], original['entities']) | |
| self.assertEqual(self.manifest, original) | |
| def test_validation_rejects_bad_shapes_unknown_ids_and_nonfinite_values(self): | |
| bad = [None, [], dict(schema='unsupported'), constraints(fixed_entities={}), | |
| constraints(fixed_entities=[dict(self.a, entity_id='unknown')]), | |
| constraints(fixed_entities=[self.a, self.a]), | |
| constraints(fixed_entities=[dict(self.a, dimensions_m=[1, 1])]), | |
| constraints(fixed_entities=[dict(self.a, dimensions_m=[1, -1, 1])]), | |
| constraints(fixed_entities=[dict(self.a, yaw_deg=math.nan)]), | |
| constraints(fixed_entities=[dict(self.a, center_xy_m=[math.inf, 0])]), | |
| constraints(nogoods=[dict(code='bad', entities=[])]), | |
| constraints(nogoods=[dict(code='bad', entities=[self.a, self.a])]), | |
| constraints(reservations=[dict(room_id='unknown', purpose='access', bounds_xy_m=[0, 0, 1, 1])]), | |
| constraints(reservations=[dict(room_id='room', purpose='access', bounds_xy_m=[1, 0, 0, 1])]), | |
| constraints(reservations=[dict(room_id='room', purpose='access', bounds_xy_m=[0, 0, 1, 1], | |
| exempt_entity_ids=['unknown'])]), | |
| constraints(reservations=[dict(room_id='room', purpose='access', bounds_xy_m=[0, 0, 1, 1], | |
| support_entity_id='unknown')]), | |
| constraints(reservations=[dict(room_id='room', purpose='access', bounds_xy_m=[0, 0, 1, 1], | |
| z_range_m=[1, 1])])] | |
| for data in bad: | |
| with self.subTest(data=data), self.assertRaises(ValueError): | |
| self.helper(data) | |
| def test_nogood_tolerance_and_full_assignment_semantics(self): | |
| helper = self.helper(constraints(nogoods=[dict(code='bad_pair', entities=[self.a, self.b])])) | |
| self.assertEqual(helper.assignment_errors({'a': self.a}), []) | |
| self.assertTrue(helper.assignment_errors({'a': self.a, 'b': self.b})) | |
| close = dict(self.b, center_xy_m=[4. + .5e-6, 4.]) | |
| self.assertTrue(helper.assignment_errors({'a': self.a, 'b': close})) | |
| apart = dict(self.b, center_xy_m=[4. + 2e-6, 4.]) | |
| self.assertEqual(helper.assignment_errors({'a': self.a, 'b': apart}), []) | |
| self.assertEqual(helper.assignment_errors({'a': self.a, 'b': dict(self.b, asset_id='other')}), []) | |
| def test_reservation_uses_obb_not_aabb_and_honors_height_and_exemption(self): | |
| pose = dict(self.a, center_xy_m=[0., 0.], dimensions_m=[2., .1, .7], yaw_deg=45.) | |
| reservation = dict(room_id='room', bounds_xy_m=[.5, -.65, .65, -.5], purpose='access') | |
| self.assertEqual(self.helper(constraints(reservations=[reservation])).assignment_errors({'a': pose}), []) | |
| reservation['bounds_xy_m'] = [.5, .5, .65, .65] | |
| self.assertTrue(self.helper(constraints(reservations=[reservation])).assignment_errors({'a': pose})) | |
| reservation['z_range_m'] = [1., 2.] | |
| self.assertEqual(self.helper(constraints(reservations=[reservation])).assignment_errors({'a': pose}), []) | |
| pose['base_z_m'] = 1. | |
| self.assertTrue(self.helper(constraints(reservations=[reservation])).assignment_errors({'a': pose})) | |
| reservation['exempt_entity_ids'] = ['a'] | |
| self.assertEqual(self.helper(constraints(reservations=[reservation])).assignment_errors({'a': pose}), []) | |
| def test_support_local_reservation_follows_assigned_parent(self): | |
| reservation = dict(room_id='room', support_entity_id='a', bounds_xy_m=[.8, -.2, 1.2, .2], | |
| purpose='support_access', exempt_entity_ids=['a']) | |
| helper = self.helper(constraints(reservations=[reservation])) | |
| parent = dict(self.a, center_xy_m=[2., 2.], yaw_deg=90.) | |
| child = dict(self.b, center_xy_m=[2., 3.]) | |
| self.assertEqual(helper.assignment_errors({'b': child}), []) | |
| self.assertTrue(helper.assignment_errors({'a': parent, 'b': child})) | |
| self.assertEqual(helper.assignment_errors({'a': dict(parent, yaw_deg=0.), 'b': child}), []) | |
| def test_surface_reservation_only_checks_the_declared_supports_children(self): | |
| reservation = dict(room_id='room', support_entity_id='a', bounds_xy_m=[-.2, -.2, .2, .2], | |
| purpose='target', occupancy_scope='supported_children') | |
| helper = self.helper(constraints(reservations=[reservation])) | |
| self.assertEqual(helper.assignment_errors({'a': self.a}), []) | |
| child = dict(self.b, center_xy_m=self.a['center_xy_m'], supported_by_entity_id='a') | |
| self.assertTrue(helper.assignment_errors({'a': self.a, 'b': child})) | |
| child.pop('supported_by_entity_id') | |
| self.assertEqual(helper.assignment_errors({'a': self.a, 'b': child}), []) | |
| def test_asset_lock_validates_source_record_aliases_and_native_dimensions(self): | |
| native = candidate(self.a) | |
| lock = dict(self.a, real_asset_binding=dict(record_id=native['pool_candidate_key']), | |
| source_dimensions_m=list(self.a['dimensions_m']), asset_sha256=native['asset_sha256']) | |
| helper = self.helper(constraints(asset_locks=[lock, self.b])) | |
| self.assertIsNone(helper.candidate_error('a', native)) | |
| for changes in (dict(pool_candidate_key='wrong'), dict(dimensions_m=[.5, .4, .7]), | |
| dict(asset_sha256='wrong')): | |
| with self.subTest(changes=changes): | |
| self.assertTrue(helper.candidate_error('a', dict(native, **changes))) | |
| def test_load_request_validates_constraints_before_search(self): | |
| path = self.output / 'request.json' | |
| request = dict(self.request, search_constraints=constraints( | |
| fixed_entities=[dict(self.a, center_xy_m=[0., math.nan])])) | |
| path.write_text(json.dumps(request)) | |
| with self.assertRaises(ValueError): | |
| joint.load_request(path) | |
| if __name__ == '__main__': | |
| unittest.main() | |