ITKPolyDataReader

Introduction

The vtkITKPolyDataReader aims to read a ITK file format.

Methods

extend

Method used to decorate a given object (publicAPI+model) with vtkITKPolyDataReader characteristics.

Argument Type Required Description
publicAPI Yes object on which methods will be bounds (public)
model Yes object on which data structure will be bounds (protected)
initialValues IITKPolyDataReaderInitialValues No (default: {})

getArrayName

Get the array name.

getFileName

Get the filename.

newInstance

Method used to create a new instance of vtkITKPolyDataReader

Argument Type Required Description
initialValues IITKPolyDataReaderInitialValues No for pre-setting some of its content

parseAsArrayBuffer

Parse data as array buffer.

Argument Type Required Description
arrayBuffer ArrayBuffer Yes The array buffer to parse.

requestData

Argument Type Required Description
inData Yes
outData Yes

setArrayName

Set the array name.

Argument Type Required Description
arrayName String Yes The name of the array.

setFileName

Set the filename.

Argument Type Required Description
filename String Yes

setReadPolyDataArrayBufferFromITK

Argument Type Required Description
fn any Yes

Source

index.d.ts
import { vtkAlgorithm, vtkObject } from '../../../interfaces';

/**
*
*/
export interface IITKPolyDataReaderInitialValues {
fileName?: string;
arrayName?: string;
}

type vtkITKPolyDataReaderBase = vtkObject &
Omit<
vtkAlgorithm,
| 'getInputData'
| 'setInputData'
| 'setInputConnection'
| 'getInputConnection'
| 'addInputConnection'
| 'addInputData'
>;

export interface vtkITKPolyDataReader extends vtkITKPolyDataReaderBase {
/**
* Get the array name.
*/
getArrayName(): string;

/**
* Get the filename.
*/
getFileName(): string;

/**
* Parse data as array buffer.
* @param {ArrayBuffer} arrayBuffer The array buffer to parse.
*/
parseAsArrayBuffer(arrayBuffer: ArrayBuffer): void;

/**
*
* @param inData
* @param outData
*/
requestData(inData: any, outData: any): void;

/**
* Set the array name.
* @param {String} arrayName The name of the array.
*/
setArrayName(arrayName: string): boolean;

/**
* Set the filename.
* @param {String} filename
*/
setFileName(filename: string): boolean;
}

/**
* Method used to decorate a given object (publicAPI+model) with vtkITKPolyDataReader characteristics.
*
* @param publicAPI object on which methods will be bounds (public)
* @param model object on which data structure will be bounds (protected)
* @param {IITKPolyDataReaderInitialValues} [initialValues] (default: {})
*/
export function extend(
publicAPI: object,
model: object,
initialValues?: IITKPolyDataReaderInitialValues
): void;

/**
* Method used to create a new instance of vtkITKPolyDataReader
* @param {IITKPolyDataReaderInitialValues} [initialValues] for pre-setting some of its content
*/
export function newInstance(
initialValues?: IITKPolyDataReaderInitialValues
): vtkITKPolyDataReader;

/**
*
* @param {*} fn
*/
export function setReadPolyDataArrayBufferFromITK(fn: any): Promise<any>;

/**
* The vtkITKPolyDataReader aims to read a ITK file format.
*/
export declare const vtkITKPolyDataReader: {
newInstance: typeof newInstance;
extend: typeof extend;
setReadPolyDataArrayBufferFromITK: typeof setReadPolyDataArrayBufferFromITK;
};
export default vtkITKPolyDataReader;
index.js
import macro from 'vtk.js/Sources/macros';
import vtk from 'vtk.js/Sources/vtk';

let readPolyDataArrayBuffer = null;
let resultPreprocessor = (result) => result;

function setReadPolyDataArrayBufferFromITK(fn) {
readPolyDataArrayBuffer = fn;

// first arg is a webworker if reuse is desired.
readPolyDataArrayBuffer = (...args) => fn(null, ...args);

// an object is now passed out which includes a webworker which we
// should terminate
resultPreprocessor = ({ webWorker, polyData }) => {
webWorker.terminate();
return polyData;
};
}

// ----------------------------------------------------------------------------
// vtkITKPolyDataReader methods
// ----------------------------------------------------------------------------

function vtkITKPolyDataReader(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkITKPolyDataReader');

// Returns a promise to signal when polyData is ready
publicAPI.parseAsArrayBuffer = (arrayBuffer) => {
if (!arrayBuffer || arrayBuffer === model.rawDataBuffer) {
return Promise.resolve();
}

model.rawDataBuffer = arrayBuffer;

return readPolyDataArrayBuffer(arrayBuffer, model.fileName)
.then(resultPreprocessor)
.then((polyData) => {
model.output[0] = vtk(polyData);

publicAPI.modified();
});
};

publicAPI.requestData = (inData, outData) => {
publicAPI.parseAsArrayBuffer(model.rawDataBuffer, model.fileName);
};
}

// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------

const DEFAULT_VALUES = {
fileName: '',
// If null/undefined a unique array will be generated
arrayName: null,
};

// ----------------------------------------------------------------------------

export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);

// Build VTK API
macro.obj(publicAPI, model);
macro.algo(publicAPI, model, 0, 1);
macro.setGet(publicAPI, model, ['fileName', 'arrayName']);

// vtkITKPolyDataReader methods
vtkITKPolyDataReader(publicAPI, model);

// Check that ITK function has been injected
if (!readPolyDataArrayBuffer) {
console.error(`
// Dependency needs to be added inside your project
import readPolyDataArrayBuffer from 'itk/readPolyDataArrayBuffer';
vtkITKPolyDataReader.setReadPolyDataArrayBufferFromITK(readPolyDataArrayBuffer);
`);
}
}

// ----------------------------------------------------------------------------

export const newInstance = macro.newInstance(extend, 'vtkITKPolyDataReader');

// ----------------------------------------------------------------------------

export default { newInstance, extend, setReadPolyDataArrayBufferFromITK };