Skip to content

Commit

Permalink
feat(mesh-io): add write-mesh-node
Browse files Browse the repository at this point in the history
  • Loading branch information
thewtex committed Nov 26, 2023
1 parent 03e7708 commit 9393607
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 29 deletions.
5 changes: 5 additions & 0 deletions packages/mesh-io/typescript/src/index-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ export type { ReadMeshOptions }
import readMeshNode from './read-mesh-node.js'
export { readMeshNode }

import WriteMeshOptions from './write-mesh-options.js'
export type { WriteMeshOptions }

import writeMeshNode from './write-mesh-node.js'
export { writeMeshNode }


import ByuReadMeshNodeResult from './byu-read-mesh-node-result.js'
Expand Down
71 changes: 71 additions & 0 deletions packages/mesh-io/typescript/src/write-mesh-node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import path from 'path'

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 WriteMeshOptions from './write-mesh-options.js'

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


/**
* Write a mesh to a serialized file format and from an the itk-wasm Mesh
*
* @param {Mesh} mesh - Input mesh
* @param {string} serializedMesh - Output mesh serialized in the file format.
* @param {WriteMeshOptions} options - options object
*
* @returns {void} - result object
*/
async function writeMeshNode(
mesh: Mesh,
serializedMesh: string,
options: WriteMeshOptions = {}
) : Promise<void> {
const absoluteFilePath = path.resolve(serializedMesh)
const mimeType = options.mimeType
const extension = getFileExtension(absoluteFilePath)

let inputMesh = mesh

let io = null
if (typeof mimeType !== 'undefined' && 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[1] !== null) {
let { couldWrite } = await (readerWriter[1] as Writer)(inputMesh, absoluteFilePath, { useCompression: options.useCompression, binaryFileType: options.binaryFileType })
if (couldWrite) {
return
}
}
}
}
if (io === null ) {
throw Error('Could not find IO for: ' + absoluteFilePath)
}
const readerWriter = meshIoIndexNode.get(io as string)

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

export default writeMeshNode
15 changes: 15 additions & 0 deletions packages/mesh-io/typescript/src/write-mesh-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
interface WriteMeshOptions {
/** Use compression when writing the mesh if the IO formt supports it. */
useCompression?: boolean

/** Use a binary file type in the written file, if supported */
binaryFileType?: boolean

/** Mime type of the output mesh file. */
mimeType?: string

/** Only write the mesh information, not the pixel data. */
informationOnly?: boolean
}

export default WriteMeshOptions
32 changes: 32 additions & 0 deletions packages/mesh-io/typescript/test/node/write-mesh-node-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import test from 'ava'
import path from 'path'

import { readMeshNode, writeMeshNode } from '../../dist/index-node.js'
import { IntTypes, FloatTypes, PixelTypes } from 'itk-wasm'

import { testInputPath, testOutputPath } from './common.js'

const testInputFilePath = path.resolve(testInputPath, 'cow.vtk')
const testOutputFilePath = path.resolve(testOutputPath, 'write-mesh-node-test-cow.vtk')

const verifyMesh = (t, mesh) => {
t.is(mesh.meshType.dimension, 3)
t.is(mesh.meshType.pointComponentType, FloatTypes.Float32)
t.is(mesh.meshType.cellComponentType, IntTypes.UInt32)
t.is(mesh.meshType.pointPixelType, PixelTypes.Scalar)
t.is(mesh.meshType.cellPixelType, PixelTypes.Scalar)
t.is(mesh.numberOfPoints, 2903)
t.is(mesh.numberOfCells, 3263)
}

test('writeMeshNode writes a file path on the local filesystem', async (t) => {
const mesh = await readMeshNode(testInputFilePath)
verifyMesh(t, mesh)

const useCompression = false
const binaryFileType = false
await writeMeshNode(mesh, testOutputFilePath, { useCompression, binaryFileType })

const meshBack = await readMeshNode(testOutputFilePath)
verifyMesh(t, meshBack)
})
29 changes: 0 additions & 29 deletions test/node/io/mesh/writeMeshLocalFileTest.js

This file was deleted.

0 comments on commit 9393607

Please sign in to comment.