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

16.43% Statements 12/73
0% Branches 0/29
28.57% Functions 2/7
17.39% Lines 12/69

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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181        1x               2x       2x                 2x                                                                                                                                                                                                                   2x                                       1x             2x     2x     2x     2x     2x         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;
 
// ----------------------------------------------------------------------------
// vtkImageCropFilter methods
// ----------------------------------------------------------------------------
 
function vtkImageCropFilter(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkImageCropFilter');
 
  // --------------------------------------------------------------------------
 
  publicAPI.reset = () => {
    const data = publicAPI.getInputData();
    if (data) {
      publicAPI.setCroppingPlanes(...data.getExtent());
    }
  };
 
  // --------------------------------------------------------------------------
 
  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 extent = input.getExtent();
    const cropped =
      model.croppingPlanes && model.croppingPlanes.length === 6
        ? extent.map((e, i) => {
            if (i % 2 === 0) {
              // min plane
              return Math.max(e, Math.round(model.croppingPlanes[i]));
            }
            // max plane
            return Math.min(e, Math.round(model.croppingPlanes[i]));
          })
        : extent.slice();
 
    if (
      cropped[0] === extent[0] &&
      cropped[1] === extent[1] &&
      cropped[2] === extent[2] &&
      cropped[3] === extent[3] &&
      cropped[4] === extent[4] &&
      cropped[5] === extent[5]
    ) {
      const sameAsInput = vtkImageData.newInstance();
      sameAsInput.shallowCopy(input); // Force new mtime
      outData[0] = sameAsInput;
      return;
    }
 
    // reorder if needed
    for (let i = 0; i < 3; ++i) {
      if (cropped[i * 2] > cropped[i * 2 + 1]) {
        [cropped[i * 2], cropped[i * 2 + 1]] = [
          cropped[i * 2 + 1],
          cropped[i * 2],
        ];
      }
    }
 
    // restrict crop bounds based on extent bounds
    for (let i = 0; i < 6; i += 2) {
      // min case
      cropped[i] = Math.max(cropped[i], extent[i]);
      // max case
      cropped[i + 1] = Math.min(cropped[i + 1], extent[i + 1]);
    }
 
    const numberOfComponents = scalars.getNumberOfComponents();
    const componentSize =
      (cropped[1] - cropped[0] + 1) *
      (cropped[3] - cropped[2] + 1) *
      (cropped[5] - cropped[4] + 1) *
      numberOfComponents;
    const scalarsData = scalars.getData();
 
    const dims = input.getDimensions();
    const jStride = numberOfComponents * dims[0];
    const kStride = numberOfComponents * dims[0] * dims[1];
    const beginOffset = (cropped[0] - extent[0]) * numberOfComponents;
    const stripSize = (cropped[1] - cropped[0] + 1) * numberOfComponents; // +1 because subarray end is exclusive
 
    // crop image
    const croppedArray = new scalarsData.constructor(componentSize);
    let index = 0;
    for (let k = cropped[4]; k <= cropped[5]; ++k) {
      for (let j = cropped[2]; j <= cropped[3]; ++j) {
        const begin =
          beginOffset + (j - extent[2]) * jStride + (k - extent[4]) * kStride;
        const end = begin + stripSize;
        const slice = scalarsData.subarray(begin, end);
        croppedArray.set(slice, index);
        index += slice.length;
      }
    }
    const outImage = vtkImageData.newInstance({
      extent: cropped,
      origin: input.getOrigin(),
      direction: input.getDirection(),
      spacing: input.getSpacing(),
    });
 
    const croppedScalars = vtkDataArray.newInstance({
      name: scalars.getName(),
      numberOfComponents,
      values: croppedArray,
    });
 
    outImage.getPointData().setScalars(croppedScalars);
 
    outData[0] = outImage;
  };
 
  publicAPI.isResetAvailable = () => {
    if (model.croppingPlanes == null || model.croppingPlanes.length === 0) {
      return false;
    }
    const data = publicAPI.getInputData();
    if (data) {
      const originalExtent = data.getExtent();
      const findDifference = originalExtent.find(
        (v, i) => Math.abs(model.croppingPlanes[i] - v) > Number.EPSILON
      );
      return findDifference !== undefined;
    }
    return false;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  // croppingPlanes: null,
};
 
// ----------------------------------------------------------------------------
 
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);
 
  // no orientation support yet
  macro.setGetArray(publicAPI, model, ['croppingPlanes'], 6);
 
  // Object specific methods
  vtkImageCropFilter(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageCropFilter');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };