File size: 9,404 Bytes
870b85a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
"""Repair constraints must coexist with the WQZ native solver policies."""
import copy
import json
from pathlib import Path
import tempfile
import unittest
from unittest.mock import patch

from engine.tests import test_native_repair_constraints as fixtures


joint = fixtures.joint


class FusionNativeTests(unittest.TestCase):
    def setUp(self):
        temp = tempfile.TemporaryDirectory()
        self.addCleanup(temp.cleanup)
        self.output = Path(temp.name)
        self.entity = fixtures.row()
        self.manifest = dict(entities=[self.entity], 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='Fusion native solver regression', _sha256='d' * 64,
            furniture=[dict(entity_id='a', candidate_asset_ids=[self.entity['asset_id']])],
            search=dict(grid_step_m=.4, max_nodes_per_room=1000, max_seconds_per_room=5),
            support_region_policy='legacy_vertex', floor_cover_policy='legacy',
            functional_layout_policy='legacy')

    def solve(self, request):
        return joint.solve(self.manifest, [fixtures.candidate(self.entity)], request, self.output)

    def test_fixed_pose_preserves_wqz_door_motion_evidence(self):
        fixed = dict(self.entity, center_xy_m=[1.237, 2.719], yaw_deg=37.125, base_z_m=.031)
        self.request['search_constraints'] = fixtures.constraints(fixed_entities=[fixed])
        motion = [dict(portal_id='door', kind='native_door_motion',
                       bounds_xyz_m=[[0., 0., 0.], [.1, .1, 2.]])]
        shell = self.output / 'shell.json'
        shell.write_text(json.dumps(dict(floor_plan_sha256=joint.fingerprint(self.manifest['floor_plan']),
            window_glazing=[], door_swing_keepouts=[], door_motion_volumes=motion)))
        before = copy.deepcopy((self.manifest, self.request))
        with patch.dict('os.environ', {'RHG_WINDOW_GEOMETRY': str(shell)}):
            chosen, report = self.solve(self.request)
        for key in ('center_xy_m', 'yaw_deg', 'base_z_m', 'dimensions_m'):
            self.assertEqual(chosen['a'][1][key], fixed[key])
        self.assertEqual(report['structural_constraint_evidence']['door_motion_volumes'], motion)
        self.assertTrue(report['search_constraints']['enforced'])
        self.assertEqual((self.manifest, self.request), before)

    def test_empty_constraints_preserve_unconstrained_wqz_search(self):
        plain, plain_report = self.solve(self.request)
        constrained, constrained_report = self.solve(dict(
            self.request, search_constraints=fixtures.constraints()))
        self.assertEqual(constrained, plain)
        self.assertEqual(constrained_report['selected'], plain_report['selected'])
        self.assertNotIn('search_constraints', plain_report)
        self.assertTrue(constrained_report['search_constraints']['enforced'])

    def test_saved_semantic_role_requires_matching_native_source_evidence(self):
        from bind_fixed_layout import semantic_key
        entity = dict(self.entity, role='vase', entity_id='decor', asset_pool_key='vase',
            binding_semantic_key='bowl', category='bowl', asset_sha256='a' * 64,
            source_dimensions_m=list(self.entity['dimensions_m']))
        self.assertEqual(semantic_key(entity), 'bowl')
        self.assertEqual(semantic_key(dict(entity, category='lamp')), 'vase')
        self.assertEqual(semantic_key(dict(entity, source_dimensions_m=[1., 1., 1.])), 'vase')
        self.assertEqual(semantic_key(dict(entity, asset_sha256=None)), 'vase')
        self.assertEqual(semantic_key(dict(entity, binding_semantic_key='unknown')), 'vase')
        self.assertEqual(semantic_key(dict(entity, role='carpet', binding_semantic_key=None)), 'carpet')

    def bind_repaired_pose(self, original, pose):
        import bind_fixed_layout as binding
        source_scene = self.output / 'source_scene'
        source_scene.mkdir()
        source = self.output / 'native.glb'
        source.write_bytes(b'binding serialization fixture')
        parents = [dict(fixtures.row(eid, (4., 4.)), role=role)
                   for eid, role in (('old_rug', 'rug'), ('table', 'desk'), ('new_rug', 'rug'))]
        entities = [*parents, original]
        manifest = dict(self.manifest, scene_id='repair-binding', entities=entities)
        before = json.dumps(manifest)
        for name in ('composition_manifest.runtime.json', 'SceneManifest.v16.json'):
            (source_scene / name).write_text(before)
        candidates = [fixtures.candidate(entity, canonical_path=str(source), category=entity['role'])
                      for entity in entities]
        choices = {entity['entity_id']: (candidate, dict(entity, wall_anchor='S'), None)
                   for entity, candidate in zip(entities, candidates)}
        choices[original['entity_id']] = (candidates[-1], dict(pose, wall_anchor='S'), None)
        request = dict(self.request, furniture=[dict(entity_id=entity['entity_id'],
            candidate_asset_ids=[entity['asset_id']]) for entity in entities])
        request_path = self.output / 'bind-request.json'
        request_path.write_text(json.dumps(request))
        report = dict(domain_sizes={entity['entity_id']: 1 for entity in entities},
                      relations=[], effective_policies={})
        output = self.output / 'bound'
        with patch.dict('os.environ', {'RHG_PREPARED_ASSET_POOL': 'synthetic',
                'RHG_JOINT_REQUEST': str(request_path)}, clear=True), patch(
                'prepared_asset_pool.load_candidates', return_value=(candidates,
                    {'prepared_pool': {'sha256': 'f' * 64}})), patch(
                'joint_layout.solve', return_value=(choices, report)), patch.object(
                binding, 'correct_demonstrated_surface_edges'):
            binding.bind_scene(source_scene, output, self.output, self.output)
        for name in ('composition_manifest.runtime.json', 'SceneManifest.v16.json'):
            self.assertEqual((source_scene / name).read_text(), before)
        return [next(entity for entity in json.loads((output / name).read_text())['entities']
                     if entity['entity_id'] == original['entity_id'])
                for name in ('composition_manifest.runtime.json', 'SceneManifest.v16.json')]

    def stale_rug_entity(self):
        return dict(self.entity, support_entity_id='old_rug', base_z_m=.03,
            derived_support_relation=dict(source='actual_floor_cover_mesh_and_furniture_bottom_contacts',
                authored_support_parent_entity_id=None, added_support_parent_entity_id='old_rug'),
            floor_cover_ancestor_height_delta_m=.03,
            floor_cover_contact_evidence=[dict(floor_cover_entity_id='old_rug')])

    def test_bind_scene_drops_old_rug_parent_when_repaired_pose_is_on_floor(self):
        original = self.stale_rug_entity()
        pose = dict(self.entity, center_xy_m=[1., 1.], base_z_m=0.)
        for bound in self.bind_repaired_pose(original, pose):
            self.assertNotIn('support_entity_id', bound)
            for field in ('derived_support_relation', 'floor_cover_ancestor_height_delta_m',
                          'floor_cover_contact_evidence'):
                self.assertNotIn(field, bound)
            self.assertEqual(bound['center_xy_m'], pose['center_xy_m'])
            self.assertEqual(bound['base_z_m'], 0.)

    def test_bind_scene_clears_old_rug_metadata_without_dropping_authored_parent(self):
        original = dict(self.stale_rug_entity(), supported_by_entity_id='table')
        pose = dict(self.entity, supported_by_entity_id='table', base_z_m=.7)
        for bound in self.bind_repaired_pose(original, pose):
            self.assertEqual(bound['supported_by_entity_id'], 'table')
            self.assertNotIn('support_entity_id', bound)
            self.assertNotIn('derived_support_relation', bound)
            self.assertNotIn('floor_cover_contact_evidence', bound)
            self.assertNotIn('floor_cover_ancestor_height_delta_m', bound)

    def test_bind_scene_restores_authored_parent_recorded_before_old_rug(self):
        original = self.stale_rug_entity()
        original['derived_support_relation']['authored_support_parent_entity_id'] = 'table'
        pose = dict(self.entity, support_entity_id='table', base_z_m=.7)
        for bound in self.bind_repaired_pose(original, pose):
            self.assertEqual(bound['support_entity_id'], 'table')
            self.assertNotIn('derived_support_relation', bound)

    def test_bind_scene_replaces_old_rug_metadata_with_current_pose_evidence(self):
        original = self.stale_rug_entity()
        pose = dict(self.stale_rug_entity(), support_entity_id='new_rug', base_z_m=.02,
            floor_cover_ancestor_height_delta_m=.02,
            floor_cover_contact_evidence=[dict(floor_cover_entity_id='new_rug')])
        pose['derived_support_relation']['added_support_parent_entity_id'] = 'new_rug'
        for bound in self.bind_repaired_pose(original, pose):
            for field in ('support_entity_id', 'derived_support_relation',
                          'floor_cover_ancestor_height_delta_m', 'floor_cover_contact_evidence'):
                self.assertEqual(bound[field], pose[field])


if __name__ == '__main__':
    unittest.main()