Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow meshes to be exported #516

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions examples/02_maps_and_templates/005_access_surface_maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,12 @@
# first one from the list. Now let us fetch a specific variant and also the
# hemisphere fragment
mesh = mp.fetch(variant="inflated", fragment="left")

# The returned structure is a dictionary of three numpy arrays representing the
# vertices, faces, and labels respectively. Each vertex defines a 3D surface
mesh
# The returned structure is a Surface consiting of three numpy arrays representing the
# vertices, faces, and vertex_labels respectively. Each vertex defines a 3D surface
# point, while the faces are triplets of indices into the list of vertices,
# defining surface triangles. The labels provide the label index associated with
# each vertex.
print(mesh.keys())

# %%
# Most meshes are shipped with a color map which we can fetch from the map
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ nilearn
typing-extensions; python_version < "3.8"
filelock
ebrains-drive >= 0.6.0
trimesh
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,6 @@ def find_version():
'typing-extensions; python_version < "3.8"',
"filelock",
"ebrains-drive >= 0.6.0",
"trimesh",
],
)
3 changes: 3 additions & 0 deletions siibra/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
# limitations under the License.
"""Constants, functions, and classes used commonly across siibra."""

from .surface import wrap_return_surface

import os
import re
from enum import Enum
Expand Down Expand Up @@ -677,6 +679,7 @@ def is_mesh(structure: Union[list, dict]):
return False


@wrap_return_surface()
def merge_meshes(meshes: list, labels: list = None):
# merge a list of meshes into one
# if meshes have no labels, a list of labels of the
Expand Down
1 change: 1 addition & 0 deletions siibra/surface/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .common import Surface, wrap_return_surface
77 changes: 77 additions & 0 deletions siibra/surface/common.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from typing import List, Union, IO
import numpy as np
import trimesh
from io import BytesIO
from functools import wraps


class Surface:
def __init__(
self,
vertices: List[List[float]] = None,
faces: List[List[int]] = None,
vertex_labels: List[List[Union[float, int]]] = None,
**kwargs,
) -> None:
"""
Describes a common interface to surface, where vertices
faces are defined, and optionally, vertices labels, face labels
can be defined.

Parameters
----------
vertices (List[List[float]] or ndarray): verticies triplet
faces: (List[List[int]] or ndarray): faces triplet
vertex_labels: (Labels)
"""

self.vertices = np.array(
kwargs.get("verts", []) if vertices is None else vertices
)
self.faces = np.array([] if faces is None else faces, dtype=np.uint64)

vertex_labels = (
kwargs.get("labels", []) if vertex_labels is None else vertex_labels
)
self.vertex_labels = np.array(vertex_labels)

def __contains__(self, spec):
self.faces
if spec in ("verts", "faces"):
return True
if spec == "labels":
return len(self.vertex_labels) > 0
return False

def __getitem__(self, spec: str):
if spec == "verts":
return self.vertices
if spec == "faces":
return self.faces
if spec == "labels":
if len(self.vertex_labels) > 0:
return self.vertex_labels
raise IndexError(f"spec {spec!r} not found in class surface")

def export(self, export_dest: Union[str, IO]):
mesh = trimesh.Trimesh(self.vertices, self.faces)
mesh.export(export_dest)

def to_bytes(self):
io = BytesIO()
mesh = trimesh.Trimesh(self.vertices, self.faces)
mesh.export(io, file_obj="obj")
io.seek(0)
return io.read()


def wrap_return_surface():
def outer(fn):
@wraps(fn)
def inner(*args, **kwargs):
return_val = fn(*args, **kwargs)
return Surface(**return_val)

return inner

return outer
2 changes: 2 additions & 0 deletions siibra/volumes/providers/neuroglancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from ...commons import logger, MapType, merge_meshes
from ...retrieval import requests, cache
from ...locations import boundingbox as _boundingbox
from ...surface import wrap_return_surface

from neuroglancer_scripts.precomputed_io import get_IO_for_existing_dataset
from neuroglancer_scripts.accessor import get_accessor_for_url
Expand Down Expand Up @@ -552,6 +553,7 @@ def _get_fragment_info(self, meshindex: int) -> Dict[str, Tuple[str, ]]:

return result

@wrap_return_surface()
def _fetch_fragment(self, url: str, transform_nm: np.ndarray):
r = requests.HttpRequest(url, func=lambda b: BytesIO(b))
(vertices_vox, triangles_vox) = read_precomputed_mesh(r.data)
Expand Down
5 changes: 2 additions & 3 deletions siibra/volumes/volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,9 +346,8 @@ def fetch(
try:
if fmt == "gii-label":
tpl = self.space.get_template(variant=kwargs.get('variant'))
mesh = tpl.fetch(**kwargs)
labels = self._providers[fmt].fetch(**fwd_args)
result = dict(**mesh, **labels)
result = tpl.fetch(format='mesh', **kwargs)
result.vertex_labels = self._providers[fmt].fetch(**fwd_args).get('labels')
else:
result = self._providers[fmt].fetch(**fwd_args)
except requests.SiibraHttpRequestError as e:
Expand Down
Loading