Skip to content

Commit

Permalink
Merge pull request #1270 from thewtex/transform-io-high-level
Browse files Browse the repository at this point in the history
transform io high level
  • Loading branch information
thewtex authored Nov 12, 2024
2 parents e47e2c7 + 27c236e commit c41150f
Show file tree
Hide file tree
Showing 28 changed files with 583 additions and 198 deletions.
2 changes: 1 addition & 1 deletion include/itkWasmTransformIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class ITK_TEMPLATE_EXPORT WasmTransformIOTemplate : public TransformIOBaseTempla

/** Set the JSON representation of the image information. */
void
SetJSON(const TransformListJSON & json);
SetJSON(const TransformListJSON & json, bool inMemory=false);

/*-------- This part of the interfaces deals with writing data ----- */

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
const defaultImageTag = '20241107-53b18ede'
const defaultImageTag = '20241111-d4fcf16c'
export default defaultImageTag
70 changes: 38 additions & 32 deletions packages/mesh-io/typescript/src/mesh-io-index-node.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,41 @@
import vtkPolyDataReadMeshNode from './vtk-poly-data-read-mesh-node.js'
import vtkPolyDataWriteMeshNode from './vtk-poly-data-write-mesh-node.js'
import objReadMeshNode from './obj-read-mesh-node.js'
import objWriteMeshNode from './obj-write-mesh-node.js'
import stlReadMeshNode from './stl-read-mesh-node.js'
import stlWriteMeshNode from './stl-write-mesh-node.js'
import offReadMeshNode from './off-read-mesh-node.js'
import offWriteMeshNode from './off-write-mesh-node.js'
import wasmReadMeshNode from './wasm-read-mesh-node.js'
import wasmWriteMeshNode from './wasm-write-mesh-node.js'
import wasmZstdReadMeshNode from './wasm-zstd-read-mesh-node.js'
import wasmZstdWriteMeshNode from './wasm-zstd-write-mesh-node.js'
import swcReadMeshNode from './swc-read-mesh-node.js'
import swcWriteMeshNode from './swc-write-mesh-node.js'
import byuReadMeshNode from './byu-read-mesh-node.js'
import byuWriteMeshNode from './byu-write-mesh-node.js'
import freeSurferAsciiReadMeshNode from './free-surfer-ascii-read-mesh-node.js'
import freeSurferAsciiWriteMeshNode from './free-surfer-ascii-write-mesh-node.js'
import freeSurferBinaryReadMeshNode from './free-surfer-binary-read-mesh-node.js'
import freeSurferBinaryWriteMeshNode from './free-surfer-binary-write-mesh-node.js'
import vtkPolyDataReadMeshNode from "./vtk-poly-data-read-mesh-node.js";
import vtkPolyDataWriteMeshNode from "./vtk-poly-data-write-mesh-node.js";
import objReadMeshNode from "./obj-read-mesh-node.js";
import objWriteMeshNode from "./obj-write-mesh-node.js";
import stlReadMeshNode from "./stl-read-mesh-node.js";
import stlWriteMeshNode from "./stl-write-mesh-node.js";
import offReadMeshNode from "./off-read-mesh-node.js";
import offWriteMeshNode from "./off-write-mesh-node.js";
import wasmReadMeshNode from "./wasm-read-mesh-node.js";
import wasmWriteMeshNode from "./wasm-write-mesh-node.js";
import wasmZstdReadMeshNode from "./wasm-zstd-read-mesh-node.js";
import wasmZstdWriteMeshNode from "./wasm-zstd-write-mesh-node.js";
import swcReadMeshNode from "./swc-read-mesh-node.js";
import swcWriteMeshNode from "./swc-write-mesh-node.js";
import byuReadMeshNode from "./byu-read-mesh-node.js";
import byuWriteMeshNode from "./byu-write-mesh-node.js";
import freeSurferAsciiReadMeshNode from "./free-surfer-ascii-read-mesh-node.js";
import freeSurferAsciiWriteMeshNode from "./free-surfer-ascii-write-mesh-node.js";
import freeSurferBinaryReadMeshNode from "./free-surfer-binary-read-mesh-node.js";
import freeSurferBinaryWriteMeshNode from "./free-surfer-binary-write-mesh-node.js";

const meshIoIndexNode = new Map([
['vtk', [vtkPolyDataReadMeshNode, vtkPolyDataWriteMeshNode]],
['obj', [objReadMeshNode, objWriteMeshNode]],
['stl', [stlReadMeshNode, stlWriteMeshNode]],
['off', [offReadMeshNode, offWriteMeshNode]],
['wasm', [wasmReadMeshNode, wasmWriteMeshNode]],
['wasm-zst', [wasmZstdReadMeshNode, wasmZstdWriteMeshNode]],
['swc', [swcReadMeshNode, swcWriteMeshNode]],
['byu', [byuReadMeshNode, byuWriteMeshNode]],
['free-surfer-ascii', [freeSurferAsciiReadMeshNode, freeSurferAsciiWriteMeshNode]],
['free-surfer-binary', [freeSurferBinaryReadMeshNode, freeSurferBinaryWriteMeshNode]],
])
["vtk", [vtkPolyDataReadMeshNode, vtkPolyDataWriteMeshNode]],
["obj", [objReadMeshNode, objWriteMeshNode]],
["stl", [stlReadMeshNode, stlWriteMeshNode]],
["off", [offReadMeshNode, offWriteMeshNode]],
["wasm", [wasmReadMeshNode, wasmWriteMeshNode]],
["wasmZstd", [wasmZstdReadMeshNode, wasmZstdWriteMeshNode]],
["swc", [swcReadMeshNode, swcWriteMeshNode]],
["byu", [byuReadMeshNode, byuWriteMeshNode]],
[
"freeSurferAscii",
[freeSurferAsciiReadMeshNode, freeSurferAsciiWriteMeshNode],
],
[
"freeSurferBinary",
[freeSurferBinaryReadMeshNode, freeSurferBinaryWriteMeshNode],
],
]);

export default meshIoIndexNode
export default meshIoIndexNode;
64 changes: 32 additions & 32 deletions packages/mesh-io/typescript/src/mesh-io-index.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
import vtkPolyDataReadMesh from './vtk-poly-data-read-mesh.js'
import vtkPolyDataWriteMesh from './vtk-poly-data-write-mesh.js'
import objReadMesh from './obj-read-mesh.js'
import objWriteMesh from './obj-write-mesh.js'
import stlReadMesh from './stl-read-mesh.js'
import stlWriteMesh from './stl-write-mesh.js'
import offReadMesh from './off-read-mesh.js'
import offWriteMesh from './off-write-mesh.js'
import wasmReadMesh from './wasm-read-mesh.js'
import wasmWriteMesh from './wasm-write-mesh.js'
import wasmZstdReadMesh from './wasm-zstd-read-mesh.js'
import wasmZstdWriteMesh from './wasm-zstd-write-mesh.js'
import swcReadMesh from './swc-read-mesh.js'
import swcWriteMesh from './swc-write-mesh.js'
import byuReadMesh from './byu-read-mesh.js'
import byuWriteMesh from './byu-write-mesh.js'
import freeSurferAsciiReadMesh from './free-surfer-ascii-read-mesh.js'
import freeSurferAsciiWriteMesh from './free-surfer-ascii-write-mesh.js'
import freeSurferBinaryReadMesh from './free-surfer-binary-read-mesh.js'
import freeSurferBinaryWriteMesh from './free-surfer-binary-write-mesh.js'
import vtkPolyDataReadMesh from "./vtk-poly-data-read-mesh.js";
import vtkPolyDataWriteMesh from "./vtk-poly-data-write-mesh.js";
import objReadMesh from "./obj-read-mesh.js";
import objWriteMesh from "./obj-write-mesh.js";
import stlReadMesh from "./stl-read-mesh.js";
import stlWriteMesh from "./stl-write-mesh.js";
import offReadMesh from "./off-read-mesh.js";
import offWriteMesh from "./off-write-mesh.js";
import wasmReadMesh from "./wasm-read-mesh.js";
import wasmWriteMesh from "./wasm-write-mesh.js";
import wasmZstdReadMesh from "./wasm-zstd-read-mesh.js";
import wasmZstdWriteMesh from "./wasm-zstd-write-mesh.js";
import swcReadMesh from "./swc-read-mesh.js";
import swcWriteMesh from "./swc-write-mesh.js";
import byuReadMesh from "./byu-read-mesh.js";
import byuWriteMesh from "./byu-write-mesh.js";
import freeSurferAsciiReadMesh from "./free-surfer-ascii-read-mesh.js";
import freeSurferAsciiWriteMesh from "./free-surfer-ascii-write-mesh.js";
import freeSurferBinaryReadMesh from "./free-surfer-binary-read-mesh.js";
import freeSurferBinaryWriteMesh from "./free-surfer-binary-write-mesh.js";

const meshIoIndex = new Map([
['vtk', [vtkPolyDataReadMesh, vtkPolyDataWriteMesh]],
['obj', [objReadMesh, objWriteMesh]],
['stl', [stlReadMesh, stlWriteMesh]],
['off', [offReadMesh, offWriteMesh]],
['wasm', [wasmReadMesh, wasmWriteMesh]],
['wasm-zst', [wasmZstdReadMesh, wasmZstdWriteMesh]],
['swc', [swcReadMesh, swcWriteMesh]],
['byu', [byuReadMesh, byuWriteMesh]],
['free-surfer-ascii', [freeSurferAsciiReadMesh, freeSurferAsciiWriteMesh]],
['free-surfer-binary', [freeSurferBinaryReadMesh, freeSurferBinaryWriteMesh]],
])
["vtk", [vtkPolyDataReadMesh, vtkPolyDataWriteMesh]],
["obj", [objReadMesh, objWriteMesh]],
["stl", [stlReadMesh, stlWriteMesh]],
["off", [offReadMesh, offWriteMesh]],
["wasm", [wasmReadMesh, wasmWriteMesh]],
["wasmZstd", [wasmZstdReadMesh, wasmZstdWriteMesh]],
["swc", [swcReadMesh, swcWriteMesh]],
["byu", [byuReadMesh, byuWriteMesh]],
["freeSurferAscii", [freeSurferAsciiReadMesh, freeSurferAsciiWriteMesh]],
["freeSurferBinary", [freeSurferBinaryReadMesh, freeSurferBinaryWriteMesh]],
]);

export default meshIoIndex
export default meshIoIndex;
73 changes: 41 additions & 32 deletions packages/mesh-io/typescript/src/write-mesh-node.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
import path from 'path'
import path from "path";

import {
Mesh,
getFileExtension,
} from 'itk-wasm'
import { Mesh, getFileExtension } from "itk-wasm";

import mimeToMeshIo from './mime-to-mesh-io.js'
import extensionToMeshIo from './extension-to-mesh-io.js'
import meshIoIndexNode from './mesh-io-index-node.js'
import mimeToMeshIo from "./mime-to-mesh-io.js";
import extensionToMeshIo from "./extension-to-mesh-io.js";
import meshIoIndexNode from "./mesh-io-index-node.js";

import WriteMeshOptions from './write-mesh-options.js'
import WriteMeshOptions from "./write-mesh-options.js";

interface WriterOptions {
useCompression?: boolean
binaryFileType?: boolean
useCompression?: boolean;
binaryFileType?: boolean;
}
interface WriterResult {
couldWrite: boolean
couldWrite: boolean;
}
type Writer = (mesh: Mesh, serializedImage: string, options: WriterOptions) => Promise<WriterResult>

type Writer = (
mesh: Mesh,
serializedMesh: string,
options: WriterOptions
) => Promise<WriterResult>;

/**
* Write a mesh to a serialized file format and from an the itk-wasm Mesh
Expand All @@ -34,38 +34,47 @@ async function writeMeshNode(
mesh: Mesh,
serializedMesh: string,
options: WriteMeshOptions = {}
) : Promise<void> {
const absoluteFilePath = path.resolve(serializedMesh)
const mimeType = options.mimeType
const extension = getFileExtension(absoluteFilePath)
): Promise<void> {
const absoluteFilePath = path.resolve(serializedMesh);
const mimeType = options.mimeType;
const extension = getFileExtension(absoluteFilePath);

let inputMesh = mesh
let inputMesh = mesh;

let io = null
if (typeof mimeType !== 'undefined' && mimeToMeshIo.has(mimeType)) {
io = mimeToMeshIo.get(mimeType)
let io = null;
if (typeof mimeType !== "undefined" && mimeToMeshIo.has(mimeType)) {
io = mimeToMeshIo.get(mimeType);
} else if (extensionToMeshIo.has(extension)) {
io = extensionToMeshIo.get(extension)
io = extensionToMeshIo.get(extension);
} else {
for (const readerWriter of meshIoIndexNode.values()) {
if (readerWriter[1] !== null) {
let { couldWrite } = await (readerWriter[1] as Writer)(inputMesh, absoluteFilePath, { useCompression: options.useCompression, binaryFileType: options.binaryFileType })
let { couldWrite } = await (readerWriter[1] as Writer)(
inputMesh,
absoluteFilePath,
{
useCompression: options.useCompression,
binaryFileType: options.binaryFileType,
}
);
if (couldWrite) {
return
return;
}
}
}
}
if (io === null ) {
throw Error('Could not find IO for: ' + absoluteFilePath)
if (io === null) {
throw Error("Could not find IO for: " + absoluteFilePath);
}
const readerWriter = meshIoIndexNode.get(io as string)
const readerWriter = meshIoIndexNode.get(io as string);

const writer = (readerWriter as Array<Writer>)[1]
let { couldWrite } = await writer(inputMesh, absoluteFilePath, { useCompression: options.useCompression })
const writer = (readerWriter as Array<Writer>)[1];
let { couldWrite } = await writer(inputMesh, absoluteFilePath, {
useCompression: options.useCompression,
});
if (!couldWrite) {
throw Error('Could not write: ' + absoluteFilePath)
throw Error("Could not write: " + absoluteFilePath);
}
}

export default writeMeshNode
export default writeMeshNode;
6 changes: 3 additions & 3 deletions packages/transform-io/itkInputTransformIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ bool lexical_cast(const std::string &input, InputTransformIO<TParametersValueTyp
}

auto wasmTransformIO = WasmTransformIOType::New();
wasmTransformIO->SetJSON(transformListJSON);
constexpr bool inMemory = true;
wasmTransformIO->SetJSON(transformListJSON, inMemory);

auto wasmTransformIOBase = WasmTransformIOBaseType::New();
wasmTransformIOBase->SetTransformIO(wasmTransformIO, false);
wasmTransformIOBase->SetJSON(json);
wasmTransformIOBase->SetTransformIO(wasmTransformIO, inMemory);

inputTransformIO.Set(wasmTransformIOBase);
#else
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 3 additions & 1 deletion packages/transform-io/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,14 @@
"author": "",
"license": "Apache-2.0",
"dependencies": {
"itk-wasm": "workspace:^"
"itk-wasm": "workspace:^",
"mime-types": "^2.1.35"
},
"devDependencies": {
"@itk-wasm/demo-app": "workspace:*",
"@itk-wasm/transform-io-build": "workspace:*",
"@types/node": "^20.2.5",
"@types/mime-types": "^2.1.4",
"ava": "^6.1.3",
"esbuild": "^0.19.8",
"shx": "^0.3.4",
Expand Down
12 changes: 12 additions & 0 deletions packages/transform-io/typescript/src/extension-to-transform-io.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
const extensionToTransformIo = new Map([
["h5", "hdf5"],
["hdf5", "hdf5"],
["txt", "txt"],
["mat", "mat"],
["xfm", "mnc"],
["iwt", "wasm"],
["iwt.cbor", "wasm"],
["iwt.cbor.zst", "wasm-zstd"],
]);

export default extensionToTransformIo;
Loading

0 comments on commit c41150f

Please sign in to comment.