|
51 | 51 | from pathlib import Path |
52 | 52 | from shutil import copyfileobj |
53 | 53 | from tempfile import TemporaryDirectory |
54 | | -from typing import BinaryIO, Union, cast, overload |
| 54 | +from typing import BinaryIO, Literal, Union, cast, overload |
55 | 55 | from zipfile import ZIP_DEFLATED, ZipFile |
56 | 56 | from zlib import Z_BEST_SPEED |
57 | 57 |
|
|
61 | 61 | from zipp import Path as ZipPath |
62 | 62 |
|
63 | 63 | import webknossos._nml as wknml |
| 64 | +from webknossos.geometry.mag import Mag, MagLike |
64 | 65 |
|
65 | | -from ..client.api_client.models import ApiAnnotation |
| 66 | +from ..client.api_client.models import ( |
| 67 | + ApiAdHocMeshInfo, |
| 68 | + ApiAnnotation, |
| 69 | + ApiPrecomputedMeshInfo, |
| 70 | +) |
66 | 71 | from ..dataset import ( |
67 | 72 | SEGMENTATION_CATEGORY, |
68 | 73 | DataFormat, |
@@ -1522,3 +1527,55 @@ def save(self, path: str | PathLike) -> None: |
1522 | 1527 | raise NotImplementedError( |
1523 | 1528 | "Remote annotations cannot be saved. Changes are applied ." |
1524 | 1529 | ) |
| 1530 | + |
| 1531 | + def download_mesh( |
| 1532 | + self, |
| 1533 | + segment_id: int, |
| 1534 | + output_dir: PathLike | str, |
| 1535 | + tracing_id: str, |
| 1536 | + mesh_file_name: str | None = None, |
| 1537 | + lod: int = 0, |
| 1538 | + mapping_name: str | None = None, |
| 1539 | + mapping_type: Literal["agglomerate", "json"] | None = None, |
| 1540 | + mag: MagLike | None = None, |
| 1541 | + seed_position: Vec3Int | None = None, |
| 1542 | + token: str | None = None, |
| 1543 | + ) -> UPath: |
| 1544 | + from ..client.context import _get_context |
| 1545 | + |
| 1546 | + context = _get_context() |
| 1547 | + tracingstore = context.get_tracingstore_api_client() |
| 1548 | + mesh_info: ApiAdHocMeshInfo | ApiPrecomputedMeshInfo |
| 1549 | + if mesh_file_name is not None: |
| 1550 | + mesh_info = ApiPrecomputedMeshInfo( |
| 1551 | + lod=lod, |
| 1552 | + mesh_file_name=mesh_file_name, |
| 1553 | + segment_id=segment_id, |
| 1554 | + mapping_name=mapping_name, |
| 1555 | + ) |
| 1556 | + else: |
| 1557 | + assert mag is not None, "mag is required for downloading ad-hoc mesh" |
| 1558 | + assert seed_position is not None, ( |
| 1559 | + "seed_position is required for downloading ad-hoc mesh" |
| 1560 | + ) |
| 1561 | + mesh_info = ApiAdHocMeshInfo( |
| 1562 | + lod=lod, |
| 1563 | + segment_id=segment_id, |
| 1564 | + mapping_name=mapping_name, |
| 1565 | + mapping_type=mapping_type, |
| 1566 | + mag=Mag(mag).to_tuple(), |
| 1567 | + seed_position=seed_position.to_tuple(), |
| 1568 | + ) |
| 1569 | + file_path: UPath |
| 1570 | + mesh_download = tracingstore.annotation_download_mesh( |
| 1571 | + mesh=mesh_info, |
| 1572 | + tracing_id=tracing_id, |
| 1573 | + token=token, |
| 1574 | + ) |
| 1575 | + file_path = UPath(output_dir) / f"{tracing_id}_{segment_id}.stl" |
| 1576 | + file_path.parent.mkdir(parents=True, exist_ok=True) |
| 1577 | + |
| 1578 | + with file_path.open("wb") as f: |
| 1579 | + for chunk in mesh_download: |
| 1580 | + f.write(chunk) |
| 1581 | + return file_path |
0 commit comments