All files / Sources/Widgets/Widgets3D/ImageCroppingWidget index.js

50% Statements 34/68
0% Branches 0/13
47.36% Functions 9/19
54.09% Lines 33/61

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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200                                                1x   1x   1x                     1x 1x 1x           1x                           1x                                                                   1x               1x         1x                                           1x       1x 1x 1x   8x     1x 1x 1x   12x     1x 1x 1x   6x             1x     1x 1x         1x                       1x   1x 1x           1x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkAbstractWidgetFactory from 'vtk.js/Sources/Widgets/Core/AbstractWidgetFactory';
import vtkPlaneManipulator from 'vtk.js/Sources/Widgets/Manipulators/PlaneManipulator';
import vtkLineManipulator from 'vtk.js/Sources/Widgets/Manipulators/LineManipulator';
import vtkSphereHandleRepresentation from 'vtk.js/Sources/Widgets/Representations/SphereHandleRepresentation';
import vtkCroppingOutlineRepresentation from 'vtk.js/Sources/Widgets/Representations/CroppingOutlineRepresentation';
 
import behavior from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/behavior';
import state from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/state';
 
import {
  AXES,
  transformVec3,
} from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers';
 
import { ViewTypes } from 'vtk.js/Sources/Widgets/Core/WidgetManager/Constants';
 
// ----------------------------------------------------------------------------
 
// ----------------------------------------------------------------------------
// Factory
// ----------------------------------------------------------------------------
 
function vtkImageCroppingWidget(publicAPI, model) {
  model.classHierarchy.push('vtkImageCroppingWidget');
 
  const superClass = { ...publicAPI };
 
  let stateSub = null;
 
  // --------------------------------------------------------------------------
 
  function setHandlesEnabled(label, flag) {
    model.widgetState.getStatesWithLabel(label).forEach((handle) => {
      handle.setVisible(flag);
    });
  }
 
  // Set the visibility of the three classes of handles: face, edge, corner
  publicAPI.setFaceHandlesEnabled = (flag) => setHandlesEnabled('faces', flag);
  publicAPI.setEdgeHandlesEnabled = (flag) => setHandlesEnabled('edges', flag);
  publicAPI.setCornerHandlesEnabled = (flag) =>
    setHandlesEnabled('corners', flag);
 
  // --------------------------------------------------------------------------
 
  // Copies the transforms and dimension of a vtkImageData
  publicAPI.copyImageDataDescription = (im) => {
    model.widgetState.setIndexToWorldT(...im.getIndexToWorld());
    model.widgetState.setWorldToIndexT(...im.getWorldToIndex());
 
    const dims = im.getDimensions();
    const planeState = model.widgetState.getCroppingPlanes();
    planeState.setPlanes([0, dims[0], 0, dims[1], 0, dims[2]]);
 
    publicAPI.modified();
  };
 
  // --------------------------------------------------------------------------
 
  // Updates handle positions based on cropping planes
  publicAPI.updateHandles = () => {
    const planes = model.widgetState.getCroppingPlanes().getPlanes();
    const midpts = [
      (planes[0] + planes[1]) / 2,
      (planes[2] + planes[3]) / 2,
      (planes[4] + planes[5]) / 2,
    ];
    const iAxis = [planes[0], midpts[0], planes[1]];
    const jAxis = [planes[2], midpts[1], planes[3]];
    const kAxis = [planes[4], midpts[2], planes[5]];
 
    const indexToWorldT = model.widgetState.getIndexToWorldT();
    const getAxis = (a) => AXES[a];
    for (let i = 0; i < 3; i++) {
      for (let j = 0; j < 3; j++) {
        for (let k = 0; k < 3; k++) {
          // skip center of box
          if (i !== 1 || j !== 1 || k !== 1) {
            const name = [i, j, k].map(getAxis).join('');
            const coord = transformVec3(
              [iAxis[i], jAxis[j], kAxis[k]],
              indexToWorldT
            );
 
            const [handle] = model.widgetState.getStatesWithLabel(name);
            handle.setOrigin(...coord);
          }
        }
      }
    }
  };
 
  // --------------------------------------------------------------------------
 
  publicAPI.delete = macro.chain(publicAPI.delete, () => {
    if (stateSub) {
      stateSub.unsubscribe();
    }
  });
 
  // --- Widget Requirement ---------------------------------------------------
 
  model.methodsToLink = ['scaleInPixels'];
 
  // Given a view type (geometry, slice, volume), return a description
  // of what representations to create and what widget state to pass
  // to the respective representations.
  publicAPI.getRepresentationsForViewType = (viewType) => {
    switch (viewType) {
      case ViewTypes.DEFAULT:
      case ViewTypes.GEOMETRY:
      case ViewTypes.SLICE:
      case ViewTypes.VOLUME:
      default:
        return [
          // Describes constructing a vtkSphereHandleRepresentation, and every
          // time the widget state updates, we will give the representation
          // a list of all handle states (which have the label "handles").
          { builder: vtkSphereHandleRepresentation, labels: ['handles'] },
          {
            builder: vtkCroppingOutlineRepresentation,
            // outline is defined by corner points
            labels: ['corners'],
          },
        ];
    }
  };
 
  // Update handle positions when cropping planes update
  stateSub = model.widgetState
    .getCroppingPlanes()
    .onModified(publicAPI.updateHandles);
 
  publicAPI.setCornerManipulator = (manipulator) => {
    superClass.setCornerManipulator(manipulator);
    model.widgetState
      .getStatesWithLabel('corners')
      .forEach((handle) => handle.setManipulator(manipulator));
  };
 
  publicAPI.setEdgeManipulator = (manipulator) => {
    superClass.setEdgeManipulator(manipulator);
    model.widgetState
      .getStatesWithLabel('edges')
      .forEach((handle) => handle.setManipulator(manipulator));
  };
 
  publicAPI.setFaceManipulator = (manipulator) => {
    superClass.setFaceManipulator(manipulator);
    model.widgetState
      .getStatesWithLabel('faces')
      .forEach((handle) => handle.setManipulator(manipulator));
  };
 
  // --------------------------------------------------------------------------
  // initialization
  // --------------------------------------------------------------------------
 
  publicAPI.setCornerManipulator(
    vtkPlaneManipulator.newInstance({ useCameraNormal: true })
  );
  publicAPI.setEdgeManipulator(vtkPlaneManipulator.newInstance());
  publicAPI.setFaceManipulator(vtkLineManipulator.newInstance());
}
 
// ----------------------------------------------------------------------------
 
const defaultValues = (initialValues) => ({
  // cornerManipulator: null,
  // edgeManipulator: null,
  // faceManipulator: null,
  behavior,
  widgetState: state(),
  ...initialValues,
});
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, defaultValues(initialValues));
 
  vtkAbstractWidgetFactory.extend(publicAPI, model, initialValues);
  macro.setGet(publicAPI, model, [
    'cornerManipulator',
    'edgeManipulator',
    'faceManipulator',
  ]);
 
  vtkImageCroppingWidget(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkImageCroppingWidget');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };