Skip to content

Commit

Permalink
feat(compress-stringify): add poly_data_to_json
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Apr 11, 2024
1 parent 7f09871 commit fd70c01
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

from ._version import __version__

from .interface_type_json import image_to_json, json_to_image, mesh_to_json, json_to_mesh
from .interface_type_json import image_to_json, json_to_image, mesh_to_json, json_to_mesh, poly_data_to_json, json_to_poly_data
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def mesh_to_json(mesh: Mesh) -> str:
return json_str

def json_to_mesh(mesh_json: str) -> Mesh:
"""Convert a JSON string to an mesh
"""Convert a JSON string to a mesh
:param mesh_json: Input JSON string
:type mesh_json: str
Expand All @@ -85,11 +85,47 @@ def json_to_mesh(mesh_json: str) -> Mesh:
:rtype: itkwasm.Mesh
"""
mesh_dict = json.loads(mesh_json)
mesh_type = mesh_dict["meshType"]

for key in ["points", "pointData", "cells", "cellData"]:
if mesh_dict[key] is not None:
mesh_dict[key] = parse_string_decompress(mesh_dict[key].encode(), parse_string=True)

mesh = Mesh(**mesh_dict)
return mesh

def poly_data_to_json(poly_data: PolyData) -> str:
"""Convert an poly_data to a JSON string
:param poly_data: Input poly_data
:type poly_data: itkwasm.PolyData
:return: JSON string
:rtype: str
"""
poly_data_dict = asdict(poly_data)
level = 5

for key in ["points", "vertices", "lines", "polygons", "triangleStrips", "pointData", "cellData"]:
if poly_data_dict[key] is not None:
poly_data_dict[key] = compress_stringify(array_like_to_bytes(poly_data_dict[key]), compression_level=level, stringify=True).decode()

json_str = json.dumps(poly_data_dict)
return json_str

def json_to_poly_data(poly_data_json: str) -> PolyData:
"""Convert a JSON string to an poly_data
:param poly_data_json: Input JSON string
:type poly_data_json: str
:return: Output poly_data
:rtype: itkwasm.PolyData
"""
poly_data_dict = json.loads(poly_data_json)

for key in ["points", "vertices", "lines", "polygons", "triangleStrips", "pointData", "cellData"]:
if poly_data_dict[key] is not None:
poly_data_dict[key] = parse_string_decompress(poly_data_dict[key].encode(), parse_string=True)

poly_data = PolyData(**poly_data_dict)
return poly_data
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ dependencies = [
"itkwasm-image-io",
"itkwasm-compare-meshes",
"itkwasm-mesh-io",
"itkwasm-mesh-to-poly-data",
]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
json_to_image,
mesh_to_json,
json_to_mesh,
poly_data_to_json,
json_to_poly_data,
)


Expand Down Expand Up @@ -59,3 +61,33 @@ def test_mesh_to_json():
],
)
assert metrics['almostEqual']

def test_poly_data_to_json():
from itkwasm_mesh_io import read_mesh
from itkwasm_compare_meshes import compare_meshes
from itkwasm_mesh_to_poly_data import poly_data_to_mesh, mesh_to_poly_data

mesh_filepath = (
Path(__file__).parent
/ ".."
/ ".."
/ ".."
/ "test"
/ "data"
/ "input"
/ "cow.vtk"
)
mesh = read_mesh(mesh_filepath)
poly_data = mesh_to_poly_data(mesh)
poly_data_mesh = poly_data_to_mesh(poly_data)

poly_data_json = poly_data_to_json(poly_data)
json_poly_data = json_to_poly_data(poly_data_json)

json_mesh = poly_data_to_mesh(json_poly_data)
metrics, _, _, _ = compare_meshes(poly_data_mesh,
[
json_mesh,
],
)
assert metrics['almostEqual']

0 comments on commit fd70c01

Please sign in to comment.