From fd70c015b9877f8aa3569e30cf94ec2dcf579f3f Mon Sep 17 00:00:00 2001 From: Matt McCormick Date: Wed, 3 Apr 2024 21:17:54 -0400 Subject: [PATCH] feat(compress-stringify): add poly_data_to_json --- .../itkwasm_compress_stringify/__init__.py | 2 +- .../interface_type_json.py | 40 ++++++++++++++++++- .../itkwasm-compress-stringify/pyproject.toml | 1 + .../test/test_interface_type_json.py | 32 +++++++++++++++ 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/__init__.py b/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/__init__.py index a7b3f03d5..bb252324b 100644 --- a/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/__init__.py +++ b/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/__init__.py @@ -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 \ No newline at end of file +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 \ No newline at end of file diff --git a/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/interface_type_json.py b/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/interface_type_json.py index 5db89fcf7..48274b9b3 100644 --- a/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/interface_type_json.py +++ b/packages/compress-stringify/python/itkwasm-compress-stringify/itkwasm_compress_stringify/interface_type_json.py @@ -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 @@ -85,7 +85,6 @@ 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: @@ -93,3 +92,40 @@ def json_to_mesh(mesh_json: str) -> Mesh: 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 diff --git a/packages/compress-stringify/python/itkwasm-compress-stringify/pyproject.toml b/packages/compress-stringify/python/itkwasm-compress-stringify/pyproject.toml index 8db0d13a1..7b71a809d 100644 --- a/packages/compress-stringify/python/itkwasm-compress-stringify/pyproject.toml +++ b/packages/compress-stringify/python/itkwasm-compress-stringify/pyproject.toml @@ -55,6 +55,7 @@ dependencies = [ "itkwasm-image-io", "itkwasm-compare-meshes", "itkwasm-mesh-io", + "itkwasm-mesh-to-poly-data", ] diff --git a/packages/compress-stringify/python/itkwasm-compress-stringify/test/test_interface_type_json.py b/packages/compress-stringify/python/itkwasm-compress-stringify/test/test_interface_type_json.py index 99fb74032..41d4e7660 100644 --- a/packages/compress-stringify/python/itkwasm-compress-stringify/test/test_interface_type_json.py +++ b/packages/compress-stringify/python/itkwasm-compress-stringify/test/test_interface_type_json.py @@ -5,6 +5,8 @@ json_to_image, mesh_to_json, json_to_mesh, + poly_data_to_json, + json_to_poly_data, ) @@ -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']