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

0% Statements 0/62
0% Branches 0/37
0% Functions 0/8
0% Lines 0/59

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                                                                                                                                                                                                                                                                                             
import macro from 'vtk.js/Sources/macros';
 
import {
  AXES,
  transformVec3,
  rotateVec3,
  handleTypeFromName,
} from 'vtk.js/Sources/Widgets/Widgets3D/ImageCroppingWidget/helpers';
 
export default function widgetBehavior(publicAPI, model) {
  model._isDragging = false;
 
  publicAPI.setDisplayCallback = (callback) =>
    model.representations[0].setDisplayCallback(callback);
 
  publicAPI.handleLeftButtonPress = () => {
    if (
      !model.activeState ||
      !model.activeState.getActive() ||
      !model.pickable
    ) {
      return macro.VOID;
    }
    if (model.dragable) {
      model._isDragging = true;
      model._apiSpecificRenderWindow.setCursor('grabbing');
      model._interactor.requestAnimation(publicAPI);
    }
    return macro.EVENT_ABORT;
  };
 
  publicAPI.handleMouseMove = (callData) => {
    if (model._isDragging) {
      return publicAPI.handleEvent(callData);
    }
    return macro.VOID;
  };
 
  publicAPI.handleLeftButtonRelease = () => {
    if (
      !model.activeState ||
      !model.activeState.getActive() ||
      !model.pickable
    ) {
      return macro.VOID;
    }
 
    if (model._isDragging) {
      model._isDragging = false;
      model._interactor.cancelAnimation(publicAPI);
      model.widgetState.deactivate();
    }
 
    return macro.EVENT_ABORT;
  };
 
  publicAPI.handleEvent = (callData) => {
    if (model.pickable && model.activeState && model.activeState.getActive()) {
      const manipulator = model.activeState.getManipulator();
      if (manipulator) {
        const name = model.activeState.getName();
        const type = handleTypeFromName(name);
        const index = name.split('').map((l) => AXES.indexOf(l));
        const planes = model.widgetState.getCroppingPlanes().getPlanes();
        const indexToWorldT = model.widgetState.getIndexToWorldT();
 
        let worldCoords = [];
 
        if (type === 'corners') {
          // manipulator should be a plane manipulator
          worldCoords = manipulator.handleEvent(
            callData,
            model._apiSpecificRenderWindow
          ).worldCoords;
        }
 
        if (type === 'faces') {
          // constraint axis is line defined by the index and center point.
          // Since our index point is defined inside a box [0, 2, 0, 2, 0, 2],
          // center point is [1, 1, 1].
          const constraintAxis = [1 - index[0], 1 - index[1], 1 - index[2]];
 
          // get center of current crop box
          const center = [
            (planes[0] + planes[1]) / 2,
            (planes[2] + planes[3]) / 2,
            (planes[4] + planes[5]) / 2,
          ];
 
          // manipulator should be a line manipulator
          manipulator.setHandleOrigin(transformVec3(center, indexToWorldT));
          manipulator.setHandleNormal(
            rotateVec3(constraintAxis, indexToWorldT)
          );
          worldCoords = manipulator.handleEvent(
            callData,
            model._apiSpecificRenderWindow
          ).worldCoords;
        }
 
        if (type === 'edges') {
          // constrain to a plane with a normal parallel to the edge
          const edgeAxis = index.map((a) => (a === 1 ? a : 0));
 
          manipulator.setHandleNormal(rotateVec3(edgeAxis, indexToWorldT));
          worldCoords = manipulator.handleEvent(
            callData,
            model._apiSpecificRenderWindow
          ).worldCoords;
        }
 
        if (worldCoords.length) {
          // transform worldCoords to indexCoords, and then update the croppingPlanes() state with setPlanes().
          const worldToIndexT = model.widgetState.getWorldToIndexT();
          const indexCoords = transformVec3(worldCoords, worldToIndexT);
 
          for (let i = 0; i < 3; i++) {
            if (index[i] === 0) {
              planes[i * 2] = indexCoords[i];
            } else if (index[i] === 2) {
              planes[i * 2 + 1] = indexCoords[i];
            }
          }
 
          model.activeState.setOrigin(...worldCoords);
          model.widgetState.getCroppingPlanes().setPlanes(...planes);
 
          return macro.EVENT_ABORT;
        }
      }
    }
    return macro.VOID;
  };
 
  // --------------------------------------------------------------------------
  // initialization
  // --------------------------------------------------------------------------
 
  model._camera = model._renderer.getActiveCamera();
 
  model.classHierarchy.push('vtkImageCroppingWidgetProp');
}