All files / Sources/IO/Misc/ITKPolyDataReader index.js

14.28% Statements 4/28
0% Branches 0/7
0% Functions 0/9
15.38% Lines 4/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91      1x 1x                                                                                                   1x                                                             1x          
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 };