-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
123 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
32
packages/mesh-io/typescript/test/node/write-mesh-node-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}) |
This file was deleted.
Oops, something went wrong.