| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- import json
- import math
- import pytest
- from core.calibration import CalibrationMapper
- def test_calibration_mapper_offset_scale(tmp_path):
- calib = {
- "pan_offset": 200.0,
- "pan_scale_x": 104.0,
- "pan_scale_y": 0.0,
- "tilt_offset": -10.0,
- "tilt_scale_x": 0.0,
- "tilt_scale_y": 30.0,
- }
- path = tmp_path / "calib.json"
- path.write_text(json.dumps(calib), encoding="utf-8")
- mapper = CalibrationMapper(
- str(path),
- {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
- )
- assert mapper.is_loaded()
- # visual pan=0 (center, x=0.5) -> pan=200+104*0.5=252
- device_pan, device_tilt = mapper.visual_to_device(0, 7)
- assert device_pan == pytest.approx(252.0)
- # visual tilt=7 (center, y=0.5) -> tilt=-10+30*0.5=5
- assert device_tilt == pytest.approx(5.0)
- # visual pan=30 (x=0.6667) -> pan=200+104*0.6667=269.3
- device_pan, _ = mapper.visual_to_device(30, 7)
- assert device_pan == pytest.approx(269.333, abs=0.01)
- def test_calibration_mapper_lookup(tmp_path):
- calib = {
- "pan_lookup": [[0.0, 200.0], [0.5, 250.0], [1.0, 300.0]],
- "tilt_lookup": [[0.0, 10.0], [0.5, 0.0], [1.0, -10.0]],
- }
- path = tmp_path / "calib.json"
- path.write_text(json.dumps(calib), encoding="utf-8")
- mapper = CalibrationMapper(
- str(path),
- {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
- )
- # visual pan=0 -> x=0.5 -> pan=250
- device_pan, device_tilt = mapper.visual_to_device(0, 7)
- assert device_pan == pytest.approx(250.0)
- assert device_tilt == pytest.approx(0.0)
- def test_calibration_mapper_missing_file():
- mapper = CalibrationMapper(
- "/nonexistent/calib.json",
- {"pan_range": (-90, 90), "pan_center": 0, "tilt_range": (-5, 20), "tilt_center": 7},
- )
- assert not mapper.is_loaded()
- device_pan, device_tilt = mapper.visual_to_device(30, 5)
- assert device_pan == pytest.approx(30.0)
- assert device_tilt == pytest.approx(5.0)
- def test_calibration_mapper_flips_and_mount_type(tmp_path):
- calib = {
- "pan_offset": 200.0,
- "pan_scale_x": 100.0,
- "pan_scale_y": 0.0,
- "tilt_offset": 0.0,
- "tilt_scale_x": 0.0,
- "tilt_scale_y": 50.0,
- "mount_type": "ceiling",
- "pan_flip": True,
- "tilt_flip": False,
- }
- path = tmp_path / "calib.json"
- path.write_text(json.dumps(calib), encoding="utf-8")
- mapper = CalibrationMapper(str(path), {})
- assert mapper.mount_type == "ceiling"
- assert mapper.pan_flip is True
- assert mapper.tilt_flip is False
|