Skip to content

Commit

Permalink
feat(mesh-io): python wasi mz3 support
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Dec 6, 2024
1 parent 968322e commit a4d3866
Show file tree
Hide file tree
Showing 14 changed files with 401 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from .free_surfer_ascii_write_mesh import free_surfer_ascii_write_mesh
from .free_surfer_binary_read_mesh import free_surfer_binary_read_mesh
from .free_surfer_binary_write_mesh import free_surfer_binary_write_mesh
from .mz3_read_mesh import mz3_read_mesh
from .mz3_write_mesh import mz3_write_mesh
from .obj_read_mesh import obj_read_mesh
from .obj_write_mesh import obj_write_mesh
from .off_read_mesh import off_read_mesh
Expand All @@ -27,6 +29,8 @@
from .wasm_zstd_read_mesh import wasm_zstd_read_mesh
from .wasm_zstd_write_mesh import wasm_zstd_write_mesh

from .mz3_read_point_set import mz3_read_point_set
from .mz3_write_point_set import mz3_write_point_set
from .obj_read_point_set import obj_read_point_set
from .obj_write_point_set import obj_write_point_set
from .off_read_point_set import off_read_point_set
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
('.byu', 'byu'),
('.fsa', 'free_surfer_ascii'),
('.fsb', 'free_surfer_binary'),
('.mz3', 'mz3'),
('.obj', 'obj'),
('.off', 'off'),
('.stl', 'stl'),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections import OrderedDict

extension_to_point_set_io = OrderedDict([
('.mz3', 'mz3'),
('.vtk', 'vtk_poly_data'),
('.obj', 'obj'),
('.off', 'off'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
'byu',
'free_surfer_ascii',
'free_surfer_binary',
'mz3',
'obj',
'obj',
'off',
'stl',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated file. To retain edits, remove this comment.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
PipelineInput,
Pipeline,
BinaryFile,
Mesh,
)

def mz3_read_mesh(
serialized_mesh: os.PathLike,
information_only: bool = False,
) -> Tuple[Any, Mesh]:
"""Read a mesh file format and convert it to the itk-wasm file format
:param serialized_mesh: Input mesh serialized in the file format
:type serialized_mesh: os.PathLike
:param information_only: Only read mesh metadata -- do not read pixel data.
:type information_only: bool
:return: Whether the input could be read. If false, the output mesh is not valid.
:rtype: Any
:return: Output mesh
:rtype: Mesh
"""
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-read-mesh.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.JsonCompatible),
PipelineOutput(InterfaceTypes.Mesh),
]

pipeline_inputs: List[PipelineInput] = [
PipelineInput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_mesh))),
]

args: List[str] = ['--memory-io',]
# Inputs
if not Path(serialized_mesh).exists():
raise FileNotFoundError("serialized_mesh does not exist")
args.append(str(PurePosixPath(serialized_mesh)))
# Outputs
could_read_name = '0'
args.append(could_read_name)

mesh_name = '1'
args.append(mesh_name)

# Options
input_count = len(pipeline_inputs)
if information_only:
args.append('--information-only')


outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = (
outputs[0].data,
outputs[1].data,
)
return result

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Generated file. To retain edits, remove this comment.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
PipelineInput,
Pipeline,
BinaryFile,
PointSet,
)

def mz3_read_point_set(
serialized_point_set: os.PathLike,
information_only: bool = False,
) -> Tuple[Any, PointSet]:
"""Read a point set file format and convert it to the itk-wasm file format
:param serialized_point_set: Input point set serialized in the file format
:type serialized_point_set: os.PathLike
:param information_only: Only read point set metadata -- do not read pixel data.
:type information_only: bool
:return: Whether the input could be read. If false, the output point set is not valid.
:rtype: Any
:return: Output point set
:rtype: PointSet
"""
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-read-point-set.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.JsonCompatible),
PipelineOutput(InterfaceTypes.PointSet),
]

pipeline_inputs: List[PipelineInput] = [
PipelineInput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_point_set))),
]

args: List[str] = ['--memory-io',]
# Inputs
if not Path(serialized_point_set).exists():
raise FileNotFoundError("serialized_point_set does not exist")
args.append(str(PurePosixPath(serialized_point_set)))
# Outputs
could_read_name = '0'
args.append(could_read_name)

point_set_name = '1'
args.append(point_set_name)

# Options
input_count = len(pipeline_inputs)
if information_only:
args.append('--information-only')


outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = (
outputs[0].data,
outputs[1].data,
)
return result

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Generated file. To retain edits, remove this comment.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
PipelineInput,
Pipeline,
Mesh,
BinaryFile,
)

def mz3_write_mesh(
mesh: Mesh,
serialized_mesh: str,
information_only: bool = False,
use_compression: bool = False,
binary_file_type: bool = False,
) -> Tuple[Any]:
"""Write an itk-wasm file format converted to an mesh file format
:param mesh: Input mesh
:type mesh: Mesh
:param serialized_mesh: Output mesh
:type serialized_mesh: str
:param information_only: Only write mesh metadata -- do not write pixel data.
:type information_only: bool
:param use_compression: Use compression in the written file, if supported
:type use_compression: bool
:param binary_file_type: Use a binary file type in the written file, if supported
:type binary_file_type: bool
:return: Whether the input could be written. If false, the output mesh is not valid.
:rtype: Any
"""
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-write-mesh.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.JsonCompatible),
PipelineOutput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_mesh))),
]

pipeline_inputs: List[PipelineInput] = [
PipelineInput(InterfaceTypes.Mesh, mesh),
]

args: List[str] = ['--memory-io',]
# Inputs
args.append('0')
# Outputs
could_write_name = '0'
args.append(could_write_name)

serialized_mesh_name = str(PurePosixPath(serialized_mesh))
args.append(serialized_mesh_name)

# Options
input_count = len(pipeline_inputs)
if information_only:
args.append('--information-only')

if use_compression:
args.append('--use-compression')

if binary_file_type:
args.append('--binary-file-type')


outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data
return result

Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# Generated file. To retain edits, remove this comment.

from pathlib import Path, PurePosixPath
import os
from typing import Dict, Tuple, Optional, List, Any

from importlib_resources import files as file_resources

_pipeline = None

from itkwasm import (
InterfaceTypes,
PipelineOutput,
PipelineInput,
Pipeline,
PointSet,
BinaryFile,
)

def mz3_write_point_set(
point_set: PointSet,
serialized_point_set: str,
information_only: bool = False,
use_compression: bool = False,
binary_file_type: bool = False,
) -> Tuple[Any]:
"""Write an ITK-Wasm file format converted to a point set file format
:param point_set: Input point set
:type point_set: PointSet
:param serialized_point_set: Output point set
:type serialized_point_set: str
:param information_only: Only write point set metadata -- do not write pixel data.
:type information_only: bool
:param use_compression: Use compression in the written file, if supported
:type use_compression: bool
:param binary_file_type: Use a binary file type in the written file, if supported
:type binary_file_type: bool
:return: Whether the input could be written. If false, the output mesh is not valid.
:rtype: Any
"""
global _pipeline
if _pipeline is None:
_pipeline = Pipeline(file_resources('itkwasm_mesh_io_wasi').joinpath(Path('wasm_modules') / Path('mz3-write-point-set.wasi.wasm')))

pipeline_outputs: List[PipelineOutput] = [
PipelineOutput(InterfaceTypes.JsonCompatible),
PipelineOutput(InterfaceTypes.BinaryFile, BinaryFile(PurePosixPath(serialized_point_set))),
]

pipeline_inputs: List[PipelineInput] = [
PipelineInput(InterfaceTypes.PointSet, point_set),
]

args: List[str] = ['--memory-io',]
# Inputs
args.append('0')
# Outputs
could_write_name = '0'
args.append(could_write_name)

serialized_point_set_name = str(PurePosixPath(serialized_point_set))
args.append(serialized_point_set_name)

# Options
input_count = len(pipeline_inputs)
if information_only:
args.append('--information-only')

if use_compression:
args.append('--use-compression')

if binary_file_type:
args.append('--binary-file-type')


outputs = _pipeline.run(args, pipeline_outputs, pipeline_inputs)

result = outputs[0].data
return result

Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
point_set_io_index = [
'vtk_poly_data',
'mz3',
'obj',
'off',
'wasm',
Expand Down
Loading

0 comments on commit a4d3866

Please sign in to comment.