All files / Sources/Filters/General/ImageSliceFilter index.js

34.48% Statements 10/29
0% Branches 0/5
66.66% Functions 2/3
34.48% Lines 10/29

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        1x               1x   1x                                                                                       1x             1x     1x     1x   1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkImageSliceFilter methods
// ----------------------------------------------------------------------------
 
function vtkImageSliceFilter(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageSliceFilter');
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
 
    if (!input) {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    const scalars = input.getPointData().getScalars();
 
    if (!scalars) {
      vtkErrorMacro('No scalars from input');
      return;
    }
 
    const datasetDefinition = input.get('extent', 'spacing', 'origin');
    datasetDefinition.extent[4] = model.sliceIndex;
    datasetDefinition.extent[5] = datasetDefinition.extent[4];
 
    const numberOfComponents = scalars.getNumberOfComponents();
    const sliceSize =
      (datasetDefinition.extent[1] - datasetDefinition.extent[0] + 1) *
      (datasetDefinition.extent[3] - datasetDefinition.extent[2] + 1) *
      numberOfComponents;
    const offset = sliceSize * model.sliceIndex;
    const sliceRawArray = scalars.getData().slice(offset, offset + sliceSize);
    const sliceArray = vtkDataArray.newInstance({
      name: scalars.getName(),
      numberOfComponents,
      values: sliceRawArray,
    });
 
    const output = vtkImageData.newInstance(datasetDefinition);
    output.getPointData().setScalars(sliceArray);
 
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  sliceIndex: 0,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Make this a VTK object
  macro.obj(publicAPI, model);
 
  // Also make it an algorithm with one input and one output
  macro.algo(publicAPI, model, 1, 1);
 
  macro.setGet(publicAPI, model, ['sliceIndex', 'orientation']);
 
  // Object specific methods
  vtkImageSliceFilter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageSliceFilter');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };