File size: 4,051 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
"""Exercise the installed selector with real closed/open GLB source assets."""
import copy
import hashlib
from pathlib import Path
import sys
import tempfile
import unittest
from unittest.mock import patch

ROOT = Path(__file__).resolve().parents[1]
CODE = ROOT / 'source/scripts/internal/scene_generation/real_assets'
sys.path.insert(0, str(CODE))

import numpy as np
import trimesh
from asset_collision_qualification import CollisionQualifier
from eligible_candidate_request import select_domain


class Reader:
    def consume(self, *args, **kwargs):
        return {}, None


class CandidateAdmissionTests(unittest.TestCase):
    def setUp(self):
        self.folder = tempfile.TemporaryDirectory()
        self.addCleanup(self.folder.cleanup)
        self.root = Path(self.folder.name)
        self.qualifier = CollisionQualifier(self.root / 'cache')
        self.entity = {'entity_id': 'master__bed', 'room_id': 'master', 'role': 'bed'}

    def candidate(self, key, name, *, open_mesh=False, source_class='bed.n.01'):
        mesh = trimesh.creation.box(extents=[1., 2., .5])
        if open_mesh:
            mesh.update_faces(np.arange(len(mesh.faces)-1))
        path = self.root / (key + '.glb')
        mesh.export(path)
        sha = hashlib.sha256(path.read_bytes()).hexdigest()
        frame = {'canonical_front_axis': '+Y', 'units': 'm', 'up_axis': 'Z'}
        return dict(asset_id=key, pool_candidate_key=key, category='bed',
            normalized_category='bed', display_name=name, dimensions_m=[1., 2., .5],
            canonical_front_axis='+Y', units='m', up_axis='Z',
            source_geometry_probe_key='fixture', probe_implementation_sha256='0'*64,
            source_dependency_sha256={str(path): sha}, prepared_source_frame_contract=frame,
            provisional_geometry_dimensions=False, canonical_path=str(path), runtime_path=str(path),
            asset_sha256=sha, runtime_sha256=sha, canonical_transform=np.eye(4).reshape(-1).tolist(),
            source_semantic_evidence={'authored_source': {'source_name': name,
                'wordnet_synset': source_class, 'source_category': 'bed'}})

    def select(self, candidates, declared):
        rule = {'entity_id': self.entity['entity_id'], 'candidate_asset_ids': declared}
        with patch('eligible_candidate_request.source_semantic_evidence',
                   side_effect=lambda reader, c: c.get('source_semantic_evidence', {})):
            return select_domain(self.entity, rule, {'mode': 'pool'},
                {c['pool_candidate_key']: c for c in candidates}, [self.entity], {},
                Reader(), self.qualifier)

    def test_wrong_beds_and_open_collision_asset_fall_back_to_real_closed_adult_bed(self):
        candidates = [self.candidate('01-lounger', 'Loris Outdoor Lounger', source_class='chaise_longue.n.01'),
            self.candidate('02-toddler', 'Toddler Bed'),
            self.candidate('03-open', 'Adult Bed', open_mesh=True),
            self.candidate('04-backup', 'Adult Bed')]
        before = copy.deepcopy(candidates)
        admitted, rejected = self.select(candidates, [c['asset_id'] for c in candidates[:3]])
        self.assertEqual([c['asset_id'] for _, c in admitted], ['04-backup'])
        self.assertEqual(candidates, before)
        self.assertEqual(len(rejected), 3)
        self.assertTrue(any(r['reason'].startswith('collision_representation_unqualified:') for r in rejected))
        self.assertEqual(admitted[0][1]['collision_qualification']['representation'], 'source_meshes')
        self.assertEqual(admitted[0][1]['dimensions_m'], [1., 2., .5])

    def test_exhaustion_never_reinserts_a_known_wrong_or_unqualified_bed(self):
        candidates = [self.candidate('01-toddler', 'Toddler Bed'),
                      self.candidate('02-open', 'Adult Bed', open_mesh=True)]
        admitted, rejected = self.select(candidates, [c['asset_id'] for c in candidates])
        self.assertEqual(admitted, [])
        self.assertEqual(len(rejected), 2)


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