Skip to content

Commit

Permalink
Merge pull request #1191 from thewtex/transform-io
Browse files Browse the repository at this point in the history
transform io
  • Loading branch information
thewtex authored Aug 11, 2024
2 parents 598bf1c + e5d17e2 commit 332bec1
Show file tree
Hide file tree
Showing 149 changed files with 5,713 additions and 192 deletions.
1 change: 1 addition & 0 deletions ITKKWStyleOverwrite.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ itkSupportInputMeshTypesTest\.cxx Namespace Disable
itkSupportInputMeshTypesMemoryIOTest\.cxx Namespace Disable
itkSupportInputPolyDataTypesTest\.cxx Namespace Disable
itkFloatTypesJSON\.h Namespace Disable
itkOutputTransformIO\.h Namespace Disable
1 change: 1 addition & 0 deletions docs/cxx/interface_types.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ itk-wasm execution pipelines support the following [interface types](https://git
- [Image](../typescript/interface_types/Image)
- [Mesh](../typescript/interface_types/Mesh)
- [PolyData](../typescript/interface_types/PolyData)
- [Transform](../typescript/interface_types/Transform)
- [JsonCompatible](../typescript/interface_types/JsonCompatible)

These interfaces types are supported in the [Emscripten interface](../typescript/browser_pipelines), [WASI](https://wasi.dev/) embedding interfaces, and native or virtual [filesystem IO](../introduction/file_formats/index). They are intended to be forward-compatible with the [WebAssembly Component Model](https://github.com/WebAssembly/component-model).
Expand Down
87 changes: 50 additions & 37 deletions include/itkTransformJSON.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <vector>

#include "itkCompositeTransformIOHelper.h"
#include "itkMetaDataDictionaryJSON.h"

#include "glaze/glaze.hpp"

Expand All @@ -36,12 +37,13 @@ namespace itk
Euler2D,
Euler3D,
Rigid2D,
Rigid3D,
Rigid3DPerspective,
VersorRigid3D,
Versor,
VersorRigid3D,
Scale,
ScaleLogarithmic,
ScaleSkewVersor3D,
Scale,
Similarity2D,
Similarity3D,
QuaternionRigid,
Expand All @@ -52,11 +54,11 @@ namespace itk
BSplineSmoothingOnUpdateDisplacementField,
ConstantVelocityField,
DisplacementField,
GaussianExponentialDiffeomorphic,
GaussianSmoothingOnUpdateDisplacementField,
GaussianSmoothingOnUpdateTimeVaryingVelocityField,
TimeVaryingVelocityField,
GaussianExponentialDiffeomorphic,
VelocityField,
TimeVaryingVelocityField,
GaussianSmoothingOnUpdateTimeVaryingVelocityField,
};

/** \class TransformTypeJSON
Expand Down Expand Up @@ -85,13 +87,15 @@ namespace itk
uint64_t numberOfFixedParameters{ 0 };
uint64_t numberOfParameters{ 0 };

std::string name { "transform" };
std::string name { "Transform" };

std::string inputSpaceName;
std::string outputSpaceName;

std::string fixedParameters;
std::string parameters;

MetadataJSON metadata;
};

/** \class TransformListJSON
Expand Down Expand Up @@ -166,6 +170,10 @@ auto transformListToTransformListJSON(std::list<typename TTransformBase::ConstPo
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Rigid2D;
}
else if (pString == "Rigid3DTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Rigid3D;
}
else if (pString == "Rigid3DPerspectiveTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Rigid3DPerspective;
Expand All @@ -178,6 +186,10 @@ auto transformListToTransformListJSON(std::list<typename TTransformBase::ConstPo
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Versor;
}
else if (pString == "ScaleTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Scale;
}
else if (pString == "ScaleLogarithmicTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::ScaleLogarithmic;
Expand All @@ -186,10 +198,6 @@ auto transformListToTransformListJSON(std::list<typename TTransformBase::ConstPo
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::ScaleSkewVersor3D;
}
else if (pString == "ScaleTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Scale;
}
else if (pString == "Similarity2DTransform")
{
transformJSON.transformType.transformParameterization = JSONTransformParameterizationEnum::Similarity2D;
Expand Down Expand Up @@ -315,6 +323,9 @@ auto transformListToTransformListJSON(std::list<typename TTransformBase::ConstPo
transformJSON.parameters = "data:application/vnd.itk.path,data/" + std::to_string(count) + "/parameters.raw";
}

auto dictionary = currentTransform->GetMetaDataDictionary();
metaDataDictionaryToJSON(dictionary, transformJSON.metadata);

transformListJSON.push_back(transformJSON);
++count;
}
Expand All @@ -327,33 +338,35 @@ auto transformListToTransformListJSON(std::list<typename TTransformBase::ConstPo
template <>
struct glz::meta<itk::JSONTransformParameterizationEnum> {
using enum itk::JSONTransformParameterizationEnum;
static constexpr auto value = glz::enumerate(Identity,
Composite,
Translation,
Euler2D,
Euler3D,
Rigid2D,
Rigid3DPerspective,
VersorRigid3D,
Versor,
ScaleLogarithmic,
ScaleSkewVersor3D,
Scale,
Similarity2D,
Similarity3D,
QuaternionRigid,
Affine,
ScalableAffine,
AzimuthElevationToCartesian,
BSpline,
BSplineSmoothingOnUpdateDisplacementField,
ConstantVelocityField,
DisplacementField,
GaussianExponentialDiffeomorphic,
GaussianSmoothingOnUpdateDisplacementField,
GaussianSmoothingOnUpdateTimeVaryingVelocityField,
TimeVaryingVelocityField,
VelocityField
static constexpr auto value = glz::enumerate(
Composite,
Identity,
Translation,
Euler2D,
Euler3D,
Rigid2D,
Rigid3D,
Rigid3DPerspective,
Versor,
VersorRigid3D,
ScaleLogarithmic,
ScaleSkewVersor3D,
Scale,
Similarity2D,
Similarity3D,
QuaternionRigid,
Affine,
ScalableAffine,
AzimuthElevationToCartesian,
BSpline,
BSplineSmoothingOnUpdateDisplacementField,
ConstantVelocityField,
DisplacementField,
GaussianSmoothingOnUpdateDisplacementField,
GaussianExponentialDiffeomorphic,
VelocityField,
TimeVaryingVelocityField,
GaussianSmoothingOnUpdateTimeVaryingVelocityField
);
};

Expand Down
15 changes: 11 additions & 4 deletions include/itkWasmImageToImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -206,14 +206,21 @@ WasmImageToImageFilter<TImage>
RegionType bufferedRegion;
RegionType largestRegion;
using SizeType = typename ImageType::SizeType;
SizeType largestSize;
SizeValueType totalSize = 1;
for (unsigned int i = 0; i < Dimension; ++i)
{
bufferedRegion.SetIndex(i, imageJSON.bufferedRegion.index[i]);
bufferedRegion.SetSize(i, imageJSON.bufferedRegion.size[i]);
if (imageJSON.bufferedRegion.size.size())
{
bufferedRegion.SetIndex(i, imageJSON.bufferedRegion.index[i]);
bufferedRegion.SetSize(i, imageJSON.bufferedRegion.size[i]);
}
else
{
bufferedRegion.SetIndex(i, 0);
bufferedRegion.SetSize(i, imageJSON.size[i]);
}
totalSize *= imageJSON.bufferedRegion.size[i];
largestRegion.SetSize(i, imageJSON.size[i]);
totalSize *= largestSize[i];
}
filter->SetBufferedRegion(bufferedRegion);
filter->SetLargestPossibleRegion(largestRegion);
Expand Down
4 changes: 2 additions & 2 deletions include/itkWasmMeshIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ namespace itk
* \brief Read and write the an itk::Mesh in a format for interfacing in WebAssembly (Wasm).
*
* This format is intended to facilitate data exchange in itk-wasm.
* It reads and writes an itk-wasm Mesh object where TypedArrays are
* It reads and writes an ITK-Wasm Mesh object where TypedArrays are
* replaced by binary files on the filesystem or in a CBOR file.
*
* The file extensions used are .iwm and .iwm.cbor.
*
*
* \ingroup IOFilters
* \ingroup WebAssemblyInterface
*/
Expand Down
12 changes: 4 additions & 8 deletions include/itkWasmTransformIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class ITK_TEMPLATE_EXPORT WasmTransformIOTemplate : public TransformIOBaseTempla
#if !defined(ITK_WRAPPING_PARSER)
/** Get the JSON representation of the mesh information. */
auto
GetJSON() -> TransformListJSON;
GetJSON(bool inMemory=false) -> TransformListJSON;
#endif

protected:
Expand All @@ -95,7 +95,7 @@ class ITK_TEMPLATE_EXPORT WasmTransformIOTemplate : public TransformIOBaseTempla
void
PrintSelf(std::ostream & os, Indent indent) const override;

auto
virtual auto
ReadTransformInformation() -> const TransformListJSON;
void
ReadFixedParameters(const TransformListJSON & json);
Expand All @@ -109,12 +109,8 @@ class ITK_TEMPLATE_EXPORT WasmTransformIOTemplate : public TransformIOBaseTempla
void
WriteParameters();

/** Reads in the transform information and populates the related buffers. */
void
ReadCBOR();
/** Writes the buffers into the CBOR item and the buffer out to disk. */
void
WriteCBOR();
void ReadCBOR(void * buffer = nullptr, unsigned char * cborBuffer = nullptr, size_t cborBufferLength = 0);
size_t WriteCBOR(const void * buffer = nullptr, unsigned char ** cborBuffer = nullptr, bool allocateCBORBuffer = false);

cbor_item_t * m_CBORRoot{ nullptr };

Expand Down
3 changes: 3 additions & 0 deletions include/itkWasmTransformToTransformFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,9 @@ WasmTransformToTransformFilter<TTransform>
transform->CopyInFixedParameters(fixedPtr, fixedPtr + transformJSON.numberOfFixedParameters);
ParametersValueType * paramsPtr = reinterpret_cast< ParametersValueType * >( std::strtoull(transformJSON.parameters.substr(35).c_str(), nullptr, 10) );
transform->CopyInParameters(paramsPtr, paramsPtr + transformJSON.numberOfParameters);

auto dictionary = transform->GetMetaDataDictionary();
jsonToMetaDataDictionary(transformJSON.metadata, dictionary);
}

++count;
Expand Down
11 changes: 6 additions & 5 deletions model/itk-wasm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -644,12 +644,13 @@ enums:
Euler2D:
Euler3D:
Rigid2D:
Rigid3D:
Rigid3DPerspective:
VersorRigid3D:
Versor:
VersorRigid3D:
Scale:
ScaleLogarithmic:
ScaleSkewVersor3D:
Scale:
Similarity2D:
Similarity3D:
QuaternionRigid:
Expand All @@ -660,8 +661,8 @@ enums:
BSplineSmoothingOnUpdateDisplacementField:
ConstantVelocityField:
DisplacementField:
GaussianExponentialDiffeomorphic:
GaussianSmoothingOnUpdateDisplacementField:
GaussianSmoothingOnUpdateTimeVaryingVelocityField:
TimeVaryingVelocityField:
GaussianExponentialDiffeomorphic:
VelocityField:
TimeVaryingVelocityField:
GaussianSmoothingOnUpdateTimeVaryingVelocityField:
6 changes: 5 additions & 1 deletion packages/core/python/itkwasm/itkwasm/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
"""itkwasm: Python interface to itk-wasm WebAssembly modules."""

__version__ = "1.0b175"
__version__ = "1.0b178"

from .interface_types import InterfaceTypes
from .image import Image, ImageType, ImageRegion
from .pointset import PointSet, PointSetType
from .mesh import Mesh, MeshType
from .transform import Transform, TransformType, TransformParameterizations
from .polydata import PolyData, PolyDataType
from .binary_file import BinaryFile
from .binary_stream import BinaryStream
Expand Down Expand Up @@ -42,6 +43,9 @@
"MeshType",
"PolyData",
"PolyDataType",
"Transform",
"TransformType",
"TransformParameterizations",
"BinaryFile",
"BinaryStream",
"TextFile",
Expand Down
20 changes: 10 additions & 10 deletions packages/core/python/itkwasm/itkwasm/interface_types.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from enum import Enum


class InterfaceTypes(Enum):
TextFile = "InterfaceTextFile"
BinaryFile = "InterfaceBinaryFile"
TextStream = "InterfaceTextStream"
BinaryStream = "InterfaceBinaryStream"
Image = "InterfaceImage"
Mesh = "InterfaceMesh"
PolyData = "InterfacePolyData"
JsonCompatible = "InterfaceJsonCompatible"
class InterfaceTypes(str, Enum):
TextFile = "TextFile"
BinaryFile = "BinaryFile"
TextStream = "TextStream"
BinaryStream = "BinaryStream"
Image = "Image"
Mesh = "Mesh"
PolyData = "PolyData"
Transform = "Transform"
JsonCompatible = "JsonCompatible"
2 changes: 2 additions & 0 deletions packages/core/python/itkwasm/itkwasm/mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Mesh:
numberOfCellPixels: int = 0
cellData: Optional[ArrayLike] = None

metadata: Dict = field(default_factory=dict)

def __post_init__(self):
if isinstance(self.meshType, dict):
self.meshType = MeshType(**self.meshType)
2 changes: 2 additions & 0 deletions packages/core/python/itkwasm/itkwasm/pointset.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class PointSet:
numberOfPointPixels: int = 0
pointData: Optional[ArrayLike] = None

metadata: Dict = field(default_factory=dict)

def __post_init__(self):
if isinstance(self.pointSetType, dict):
self.pointSetType = PointSetType(**self.pointSetType)
Loading

0 comments on commit 332bec1

Please sign in to comment.