| 1 |
import re |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pycolmap |
| 5 |
|
| 6 |
CAMERA_MODELS = Path(__file__).parents[1] / "viewer_src" / "camera_models.ts" |
| 7 |
|
| 8 |
|
| 9 |
def parse_array(source: str, name: str) -> list[str]: |
| 10 |
match = re.search( |
| 11 |
rf"export const {name} = \[(.*?)\] as const;", source, re.DOTALL |
| 12 |
) |
| 13 |
if match is None: |
| 14 |
raise AssertionError(f"Could not find {name} in {CAMERA_MODELS}") |
| 15 |
return [ |
| 16 |
value.strip().strip('"') |
| 17 |
for value in match.group(1).split(",") |
| 18 |
if value.strip() |
| 19 |
] |
| 20 |
|
| 21 |
|
| 22 |
def main() -> None: |
| 23 |
source = CAMERA_MODELS.read_text(encoding="utf-8") |
| 24 |
names = parse_array(source, "CAMERA_MODEL_NAMES") |
| 25 |
param_counts = [ |
| 26 |
int(value) for value in parse_array(source, "CAMERA_MODEL_PARAM_COUNTS") |
| 27 |
] |
| 28 |
expected = list(zip(names, param_counts, strict=True)) |
| 29 |
|
| 30 |
actual: list[tuple[str, int] | None] = [None] * len(expected) |
| 31 |
for name, model in pycolmap.CameraModelId.__members__.items(): |
| 32 |
if name == "INVALID": |
| 33 |
continue |
| 34 |
camera = pycolmap.Camera.create_from_model_id(1, model, 1.0, 100, 100) |
| 35 |
model_id = int(model.value) |
| 36 |
if model_id >= len(actual): |
| 37 |
raise AssertionError( |
| 38 |
f"Viewer is missing pycolmap camera model {name} ({model_id})" |
| 39 |
) |
| 40 |
actual[model_id] = (name, len(camera.params)) |
| 41 |
|
| 42 |
message = ( |
| 43 |
"Viewer camera model registry differs from pycolmap:\n" |
| 44 |
f"viewer={expected}\n" |
| 45 |
f"pycolmap={actual}" |
| 46 |
) |
| 47 |
assert actual == expected, message |
| 48 |
|
| 49 |
|
| 50 |
if __name__ == "__main__": |
| 51 |
main() |