65 lines
3.5 KiB
Python
65 lines
3.5 KiB
Python
"""Synthetic geometry checks; these do not qualify semantic accuracy."""
|
|
import json
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
import numpy as np
|
|
from PIL import Image
|
|
from atlas import fuse
|
|
from mapping import CLASSES, camera, unproject, write_json, verify_phase, digest
|
|
from validation import fingerprint, filename
|
|
|
|
|
|
class MappingChecks(unittest.TestCase):
|
|
def test_camera_roundtrip(self):
|
|
for projection in ['pinhole', 'ortho']:
|
|
spec = dict(eye=[3, 4, 8], target=[10, -2, 1], size=[800, 600], fov_degrees=90, width_m=40, projection=projection)
|
|
view, k = camera(spec)
|
|
pixels = np.array([[0, 0], [200, 500], [799, 599]])
|
|
xyz = unproject(pixels, np.array([3, 8, 20]), view, k, projection)
|
|
local = xyz @ view[:3, :3].T + view[:3, 3]
|
|
uv = local[:, :2] / (local[:, 2:] if projection == 'pinhole' else 1)
|
|
np.testing.assert_allclose(uv * np.diag(k)[:2] + k[:2, 2] - .5, pixels, atol=1e-8)
|
|
|
|
def test_invalid_camera(self):
|
|
with self.assertRaises(ValueError):
|
|
camera(dict(eye=[0, 0, 0], target=[0, 0, 1], size=[100, 100], fov_degrees=90))
|
|
|
|
def test_paths_and_phase_integrity(self):
|
|
for name in ['../secret', '/absolute', 'sub/file', 'sub\\file', '..']:
|
|
with self.assertRaises(ValueError): filename(name)
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
p=Path(directory);(p/'data').write_text('original')
|
|
write_json(p/'receipt.json', {'inputs':fingerprint({'a':1}), 'files':{'data':digest(p/'data')}})
|
|
verify_phase(p,'receipt.json',fingerprint({'a':1}))
|
|
with self.assertRaises(ValueError): verify_phase(p,'receipt.json',fingerprint({'a':2}))
|
|
(p/'data').write_text('changed')
|
|
with self.assertRaises(ValueError): verify_phase(p,'receipt.json',fingerprint({'a':1}))
|
|
|
|
def test_unknown_holes_and_occlusion(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
output=Path(directory)
|
|
job={'bounds_xy':[[-3,-3],[3,3]],'cell_m':1,'source':{},'model':{}, 'views':[
|
|
{'id':'top','projection':'ortho','eye':[0,0,10],'target':[0,0,0],'up':[0,1,0],'size':[6,6],'width_m':6},
|
|
{'id':'front','eye':[0,0,10],'target':[0,0,0],'up':[0,1,0],'size':[6,6],'fov_degrees':33.39848846798724}]}
|
|
for spec in job['views']:
|
|
view,k=camera(spec)
|
|
np.savez(output/(spec['id']+'.geometry.npz'),depth=np.full((6,6),10),alpha=np.ones((6,6)),view=view,intrinsics=k)
|
|
labels=np.full((6,6),13,np.uint8);labels[2:4,2:4]=255
|
|
Image.fromarray(labels).save(output/'front.labels.png');np.save(output/'front.confidence.npy',np.ones((6,6)))
|
|
result=fuse(job,output,unproject,CLASSES,write_json)
|
|
self.assertEqual(result['coverage']['accepted_cells'],32)
|
|
self.assertEqual(len(result['features']),1)
|
|
self.assertEqual(len(result['features'][0]['geometry']['coordinates']),2)
|
|
import ezdxf
|
|
drawing=ezdxf.readfile(output/'urban-map.dxf')
|
|
self.assertEqual(drawing.units,6)
|
|
self.assertEqual(len(drawing.modelspace().query('POLYLINE')),2)
|
|
view,k=camera(job['views'][1])
|
|
np.savez(output/'front.geometry.npz',depth=np.full((6,6),5),alpha=np.ones((6,6)),view=view,intrinsics=k)
|
|
blocked=fuse(job,output,unproject,CLASSES,write_json)
|
|
self.assertEqual(blocked['coverage']['accepted_cells'],0)
|
|
self.assertEqual(blocked['features'],[])
|
|
|
|
if __name__=='__main__': unittest.main()
|