Skip to content

Commit

Permalink
feat(mesh-io): add read-mesh-node
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 26, 2023
1 parent 273b8b6 commit 1b514a6
Show file tree
Hide file tree
Showing 22 changed files with 679 additions and 32 deletions.
2 changes: 1 addition & 1 deletion packages/mesh-io/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ set(meshios_WebAssemblyInterface itkWasmMeshIO itkWasmZstdMeshIO)
set(meshio_id_itkWasmMeshIO 8)
set(meshio_kebab_itkWasmMeshIO "wasm")
set(meshio_id_itkWasmZstdMeshIO 9)
set(meshio_kebab_itkWasmZstdMeshIO "wasm-ztd")
set(meshio_kebab_itkWasmZstdMeshIO "wasm-zstd")

set(ITK_NO_MESHIO_FACTORY_REGISTER_MANAGER 1)
set(MeshIOIndex_ARRAY "")
Expand Down
1 change: 1 addition & 0 deletions packages/mesh-io/typescript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"devDependencies": {
"@itk-wasm/image-io": "^0.2.0",
"@shoelace-style/shoelace": "^2.5.2",
"@types/mime-types": "^2.1.4",
"@types/node": "^20.2.5",
"ava": "^5.3.1",
"esbuild": "^0.19.5",
Expand Down
7 changes: 7 additions & 0 deletions packages/mesh-io/typescript/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 18 additions & 13 deletions packages/mesh-io/typescript/src/index-node.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
// Generated file. To retain edits, remove this comment.

import ReadMeshOptions from './read-mesh-options.js'
export type { ReadMeshOptions }

import readMeshNode from './read-mesh-node.js'
export { readMeshNode }



Expand Down Expand Up @@ -182,24 +187,24 @@ import wasmWriteMeshNode from './wasm-write-mesh-node.js'
export { wasmWriteMeshNode }


import WasmZtdReadMeshNodeResult from './wasm-ztd-read-mesh-node-result.js'
export type { WasmZtdReadMeshNodeResult }
import WasmZstdReadMeshNodeResult from './wasm-zstd-read-mesh-node-result.js'
export type { WasmZstdReadMeshNodeResult }

import WasmZtdReadMeshOptions from './wasm-ztd-read-mesh-options.js'
export type { WasmZtdReadMeshOptions }
import WasmZstdReadMeshOptions from './wasm-zstd-read-mesh-options.js'
export type { WasmZstdReadMeshOptions }

import wasmZtdReadMeshNode from './wasm-ztd-read-mesh-node.js'
export { wasmZtdReadMeshNode }
import wasmZstdReadMeshNode from './wasm-zstd-read-mesh-node.js'
export { wasmZstdReadMeshNode }


import WasmZtdWriteMeshNodeResult from './wasm-ztd-write-mesh-node-result.js'
export type { WasmZtdWriteMeshNodeResult }
import WasmZstdWriteMeshNodeResult from './wasm-zstd-write-mesh-node-result.js'
export type { WasmZstdWriteMeshNodeResult }

import WasmZtdWriteMeshOptions from './wasm-ztd-write-mesh-options.js'
export type { WasmZtdWriteMeshOptions }
import WasmZstdWriteMeshOptions from './wasm-zstd-write-mesh-options.js'
export type { WasmZstdWriteMeshOptions }

import wasmZtdWriteMeshNode from './wasm-ztd-write-mesh-node.js'
export { wasmZtdWriteMeshNode }
import wasmZstdWriteMeshNode from './wasm-zstd-write-mesh-node.js'
export { wasmZstdWriteMeshNode }

export type { JsonCompatible } from 'itk-wasm'
export type { Mesh } from 'itk-wasm'
20 changes: 20 additions & 0 deletions packages/mesh-io/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,26 @@ import wasmWriteMesh from './wasm-write-mesh.js'
export { wasmWriteMesh }


import WasmZstdReadMeshResult from './wasm-zstd-read-mesh-result.js'
export type { WasmZstdReadMeshResult }

import WasmZstdReadMeshOptions from './wasm-zstd-read-mesh-options.js'
export type { WasmZstdReadMeshOptions }

import wasmZstdReadMesh from './wasm-zstd-read-mesh.js'
export { wasmZstdReadMesh }


import WasmZstdWriteMeshResult from './wasm-zstd-write-mesh-result.js'
export type { WasmZstdWriteMeshResult }

import WasmZstdWriteMeshOptions from './wasm-zstd-write-mesh-options.js'
export type { WasmZstdWriteMeshOptions }

import wasmZstdWriteMesh from './wasm-zstd-write-mesh.js'
export { wasmZstdWriteMesh }


import WasmZtdReadMeshResult from './wasm-ztd-read-mesh-result.js'
export type { WasmZtdReadMeshResult }

Expand Down
35 changes: 35 additions & 0 deletions packages/mesh-io/typescript/src/mesh-io-index-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
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]],
])

export default meshIoIndexNode
72 changes: 72 additions & 0 deletions packages/mesh-io/typescript/src/read-mesh-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import path from 'path'
import mime from 'mime-types'

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 ReadMeshOptions from './read-mesh-options.js'

interface ReaderResult {
couldRead: boolean,
mesh: Mesh
}
interface ReaderOptions {
/** Only read image metadata -- do not read pixel data. */
informationOnly?: boolean
}
type Reader = (serializedMesh: string, options: ReaderOptions) => Promise<ReaderResult>


/**
* Read a mesh file format and convert it to the itk-wasm file format
*
* @param {string} serializedMesh - Path to input mesh serialized in the file format
* @param {ReadMeshOptions} options - options to cast resulting mesh type or to only read mesh metadata
*
* @returns {Promise<Mesh>} - Mesh result
*/
async function readMeshNode(
serializedMesh: string,
options: ReadMeshOptions = {}
) : Promise<Mesh> {

const absoluteFilePath = path.resolve(serializedMesh)
const mimeType = mime.lookup(absoluteFilePath)
const extension = getFileExtension(absoluteFilePath)

let io = null
if (mimeType && mimeToMeshIo.has(mimeType)) {
io = mimeToMeshIo.get(mimeType)
} else if (extensionToMeshIo.has(extension)) {
io = extensionToMeshIo.get(extension)
} else {
for (const readerWriter of meshIoIndexNode.values()) {
if (readerWriter[0] !== null) {
let { couldRead, mesh } = await (readerWriter[0] as Reader)(absoluteFilePath, { informationOnly: options.informationOnly })
if (couldRead) {
return mesh
}
}
}
}
if (io === null ) {
throw Error('Could not find IO for: ' + absoluteFilePath)
}
const readerWriter = meshIoIndexNode.get(io as string)

const reader = (readerWriter as Array<Reader>)[0]
let { couldRead, mesh } = await reader(absoluteFilePath, { informationOnly: options.informationOnly })
if (!couldRead) {
throw Error('Could not read: ' + absoluteFilePath)
}

return mesh
}

export default readMeshNode
6 changes: 6 additions & 0 deletions packages/mesh-io/typescript/src/read-mesh-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
interface ReadMeshOptions {
/** Only read mesh metadata -- do not read pixel data. */
informationOnly?: boolean
}

export default ReadMeshOptions
14 changes: 14 additions & 0 deletions packages/mesh-io/typescript/src/wasm-zstd-read-mesh-node-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Generated file. To retain edits, remove this comment.

import { JsonCompatible, Mesh } from 'itk-wasm'

interface WasmZstdReadMeshNodeResult {
/** Whether the input could be read. If false, the output mesh is not valid. */
couldRead: JsonCompatible

/** Output mesh */
mesh: Mesh

}

export default WasmZstdReadMeshNodeResult
78 changes: 78 additions & 0 deletions packages/mesh-io/typescript/src/wasm-zstd-read-mesh-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Generated file. To retain edits, remove this comment.

import {
JsonCompatible,
Mesh,
InterfaceTypes,
PipelineOutput,
PipelineInput,
runPipelineNode
} from 'itk-wasm'

import WasmZstdReadMeshOptions from './wasm-zstd-read-mesh-options.js'
import WasmZstdReadMeshNodeResult from './wasm-zstd-read-mesh-node-result.js'

import path from 'path'

/**
* Read a mesh file format and convert it to the itk-wasm file format
*
* @param {string} serializedMesh - Input mesh serialized in the file format
* @param {WasmZstdReadMeshOptions} options - options object
*
* @returns {Promise<WasmZstdReadMeshNodeResult>} - result object
*/
async function wasmZstdReadMeshNode(
serializedMesh: string,
options: WasmZstdReadMeshOptions = {}
) : Promise<WasmZstdReadMeshNodeResult> {

const mountDirs: Set<string> = new Set()

const desiredOutputs: Array<PipelineOutput> = [
{ type: InterfaceTypes.JsonCompatible },
{ type: InterfaceTypes.Mesh },
]

mountDirs.add(path.dirname(serializedMesh as string))
const inputs: Array<PipelineInput> = [
]

const args = []
// Inputs
const serializedMeshName = serializedMesh
args.push(serializedMeshName)
mountDirs.add(path.dirname(serializedMeshName))

// Outputs
const couldReadName = '0'
args.push(couldReadName)

const meshName = '1'
args.push(meshName)

// Options
args.push('--memory-io')
if (typeof options.informationOnly !== "undefined") {
options.informationOnly && args.push('--information-only')
}

const pipelinePath = path.join(path.dirname(import.meta.url.substring(7)), 'pipelines', 'wasm-zstd-read-mesh')

const {
returnValue,
stderr,
outputs
} = await runPipelineNode(pipelinePath, args, desiredOutputs, inputs, mountDirs)
if (returnValue !== 0 && stderr !== "") {
throw new Error(stderr)
}

const result = {
couldRead: outputs[0]?.data as JsonCompatible,
mesh: outputs[1]?.data as Mesh,
}
return result
}

export default wasmZstdReadMeshNode
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// Generated file. To retain edits, remove this comment.

interface WasmZstdReadMeshOptions {
/** Only read image metadata -- do not read pixel data. */
informationOnly?: boolean

}

export default WasmZstdReadMeshOptions
17 changes: 17 additions & 0 deletions packages/mesh-io/typescript/src/wasm-zstd-read-mesh-result.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Generated file. To retain edits, remove this comment.

import { JsonCompatible, Mesh } from 'itk-wasm'

interface WasmZstdReadMeshResult {
/** WebWorker used for computation */
webWorker: Worker | null

/** Whether the input could be read. If false, the output mesh is not valid. */
couldRead: JsonCompatible

/** Output mesh */
mesh: Mesh

}

export default WasmZstdReadMeshResult
Loading

0 comments on commit 1b514a6

Please sign in to comment.