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

14.7% Statements 10/68
0% Branches 0/29
33.33% Functions 2/6
15.62% Lines 10/64

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 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134        1x             1x   1x                                                                                                                                                                                     1x               1x     1x     1x   1x     1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkDataArray from 'vtk.js/Sources/Common/Core/DataArray';
import vtkImageData from 'vtk.js/Sources/Common/DataModel/ImageData';
 
const { vtkErrorMacro } = macro;
 
// ----------------------------------------------------------------------------
// vtkImageOutlineFilter methods
// ----------------------------------------------------------------------------
 
function vtkImageOutlineFilter(publicAPI, model) {
  model.classHierarchy.push('vtkImageOutlineFilter');
 
  publicAPI.requestData = (inData, outData) => {
    // implement requestData
    const input = inData[0];
    if (!input || input.getClassName() !== 'vtkImageData') {
      vtkErrorMacro('Invalid or missing input');
      return;
    }
 
    const output = vtkImageData.newInstance(
      input.get('spacing', 'origin', 'direction')
    );
 
    const getIndex = (point, dims) =>
      point[0] + point[1] * dims[0] + point[2] * dims[0] * dims[1];
 
    const getIJK = (index, dims) => {
      const ijk = [0, 0, 0];
      ijk[0] = index % dims[0];
      ijk[1] = Math.floor(index / dims[0]) % dims[1];
      ijk[2] = Math.floor(index / (dims[0] * dims[1]));
      return ijk;
    };
    const dims = input.getDimensions();
    output.setDimensions(dims);
    output.computeTransforms();
    const values = new Uint8Array(input.getNumberOfPoints());
    const inputDataArray = input.getPointData().getScalars().getData();
    let kernelX = 0; // default K slicing mode
    let kernelY = 1;
    if (model.slicingMode === 1) {
      kernelX = 0;
      kernelY = 2;
    } else if (model.slicingMode === 0) {
      kernelX = 1;
      kernelY = 2;
    }
    inputDataArray.forEach((el, index) => {
      if (el !== model.background) {
        const ijk = getIJK(index, dims);
        let isBorder = false;
        for (let x = -1; x <= 1 && !isBorder; x++) {
          for (let y = -1; y <= 1 && !isBorder; y++) {
            let dx = x;
            let dy = y;
            let dz = 0;
            if (model.slicingMode === 1) {
              dx = x;
              dy = 0;
              dz = y;
            } else if (model.slicingMode === 0) {
              dx = 0;
              dy = y;
              dz = x;
            }
            const evalX = ijk[kernelX] + dx;
            const evalY = ijk[kernelY] + dy;
            // check boundaries
            if (
              evalX >= 0 &&
              evalX < dims[kernelX] &&
              evalY >= 0 &&
              evalY < dims[kernelY]
            ) {
              const hoodValue =
                inputDataArray[
                  getIndex([ijk[0] + dx, ijk[1] + dy, ijk[2] + dz], dims)
                ];
              if (hoodValue !== el) isBorder = true;
            }
          }
        }
        if (isBorder) values[index] = el;
        else values[index] = model.background;
      } else {
        values[index] = model.background;
      }
    });
 
    const dataArray = vtkDataArray.newInstance({
      numberOfComponents: 1,
      values,
    });
    output.getPointData().setScalars(dataArray);
    outData[0] = output;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  slicingMode: 2,
  background: 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, ['slicingMode', 'background']);
 
  // Object specific methods
  vtkImageOutlineFilter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageOutlineFilter');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };