All files / Sources/Interaction/Style/InteractorStyleImage index.js

26.08% Statements 36/138
10.76% Branches 7/65
41.17% Functions 7/17
26.08% Lines 36/138

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 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313                      5x     5x 5x                                           5x 5x                                                                       5x 5x                                 5x         5x         5x                                                             5x                                                                                                                 5x                                                                               5x 5x       5x 5x     5x     17x     5x   60x 5x 5x 2x   5x 17x     5x 5x         5x 5x               1x                                     5x     5x     5x 5x         5x         1x          
import macro from 'vtk.js/Sources/macros';
import vtkInteractorStyleTrackballCamera from 'vtk.js/Sources/Interaction/Style/InteractorStyleTrackballCamera';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
import { States } from 'vtk.js/Sources/Rendering/Core/InteractorStyle/Constants';
 
// ----------------------------------------------------------------------------
// vtkInteractorStyleImage methods
// ----------------------------------------------------------------------------
 
function vtkInteractorStyleImage(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkInteractorStyleImage');
 
  // Public API methods
  publicAPI.superHandleMouseMove = publicAPI.handleMouseMove;
  publicAPI.handleMouseMove = (callData) => {
    const pos = callData.position;
    const renderer = callData.pokedRenderer;
 
    switch (model.state) {
      case States.IS_WINDOW_LEVEL:
        publicAPI.windowLevel(renderer, pos);
        publicAPI.invokeInteractionEvent({ type: 'InteractionEvent' });
        break;
 
      case States.IS_SLICE:
        publicAPI.slice(renderer, pos);
        publicAPI.invokeInteractionEvent({ type: 'InteractionEvent' });
        break;
 
      default:
        break;
    }
    publicAPI.superHandleMouseMove(callData);
  };
 
  //----------------------------------------------------------------------------
  publicAPI.superHandleLeftButtonPress = publicAPI.handleLeftButtonPress;
  publicAPI.handleLeftButtonPress = (callData) => {
    const pos = callData.position;
 
    if (!callData.shiftKey && !callData.controlKey) {
      model.windowLevelStartPosition[0] = pos.x;
      model.windowLevelStartPosition[1] = pos.y;
      // Get the last (the topmost) image
      publicAPI.setCurrentImageNumber(model.currentImageNumber);
      const property = model.currentImageProperty;
      if (property) {
        model.windowLevelInitial[0] = property.getColorWindow();
        model.windowLevelInitial[1] = property.getColorLevel();
      }
      publicAPI.startWindowLevel();
    } else if (model.interactionMode === 'IMAGE3D' && callData.shiftKey) {
      // If ctrl+shift or alt+shift is held down, dolly the camera
      if (callData.controlKey || callData.altKey) {
        publicAPI.startDolly();
      } else {
        // If shift is held down, rotate
        publicAPI.startRotate();
      }
    } else if (
      model.interactionMode === 'IMAGE_SLICING' &&
      callData.controlKey
    ) {
      // If ctrl is held down in slicing mode, slice the image
      model.lastSlicePosition = pos.y;
      publicAPI.startSlice();
    } else {
      // The rest of the button + key combinations remain the same
      publicAPI.superHandleLeftButtonPress(callData);
    }
  };
 
  //--------------------------------------------------------------------------
  publicAPI.superHandleLeftButtonRelease = publicAPI.handleLeftButtonRelease;
  publicAPI.handleLeftButtonRelease = () => {
    switch (model.state) {
      case States.IS_WINDOW_LEVEL:
        publicAPI.endWindowLevel();
        break;
 
      case States.IS_SLICE:
        publicAPI.endSlice();
        break;
 
      default:
        publicAPI.superHandleLeftButtonRelease();
        break;
    }
  };
 
  //--------------------------------------------------------------------------
  publicAPI.handleStartMouseWheel = () => {
    publicAPI.startSlice();
  };
 
  //--------------------------------------------------------------------------
  publicAPI.handleEndMouseWheel = () => {
    publicAPI.endSlice();
  };
 
  //--------------------------------------------------------------------------
  publicAPI.handleMouseWheel = (callData) => {
    const camera = callData.pokedRenderer.getActiveCamera();
 
    let distance = camera.getDistance();
    distance += callData.spinY;
 
    // clamp the distance to the clipping range
    const range = camera.getClippingRange();
    if (distance < range[0]) {
      distance = range[0];
    }
    if (distance > range[1]) {
      distance = range[1];
    }
    camera.setDistance(distance);
    const props = callData.pokedRenderer
      .getViewProps()
      .filter((prop) => prop.isA('vtkImageSlice'));
    props.forEach((prop) => {
      if (prop.getMapper().isA('vtkImageResliceMapper')) {
        const p = prop.getMapper().getSlicePlane();
        if (p) {
          p.push(callData.spinY);
          p.modified();
          prop.getMapper().modified();
        }
      }
    });
  };
 
  //----------------------------------------------------------------------------
  publicAPI.windowLevel = (renderer, position) => {
    model.windowLevelCurrentPosition[0] = position.x;
    model.windowLevelCurrentPosition[1] = position.y;
    const rwi = model._interactor;
 
    if (model.currentImageProperty) {
      const size = rwi.getView().getViewportSize(renderer);
 
      const mWindow = model.windowLevelInitial[0];
      const level = model.windowLevelInitial[1];
 
      // Compute normalized delta
      let dx =
        ((model.windowLevelCurrentPosition[0] -
          model.windowLevelStartPosition[0]) *
          4.0) /
        size[0];
      let dy =
        ((model.windowLevelStartPosition[1] -
          model.windowLevelCurrentPosition[1]) *
          4.0) /
        size[1];
 
      // Scale by current values
      if (Math.abs(mWindow) > 0.01) {
        dx *= mWindow;
      } else {
        dx *= mWindow < 0 ? -0.01 : 0.01;
      }
      if (Math.abs(level) > 0.01) {
        dy *= level;
      } else {
        dy *= level < 0 ? -0.01 : 0.01;
      }
 
      // Abs so that direction does not flip
      if (mWindow < 0.0) {
        dx *= -1;
      }
      if (level < 0.0) {
        dy *= -1;
      }
 
      // Compute new mWindow level
      let newWindow = dx + mWindow;
      const newLevel = level - dy;
 
      if (newWindow < 0.01) {
        newWindow = 0.01;
      }
 
      model.currentImageProperty.setColorWindow(newWindow);
      model.currentImageProperty.setColorLevel(newLevel);
    }
  };
 
  //----------------------------------------------------------------------------
  publicAPI.slice = (renderer, position) => {
    const rwi = model._interactor;
 
    const dy = position.y - model.lastSlicePosition;
 
    const camera = renderer.getActiveCamera();
    const range = camera.getClippingRange();
    let distance = camera.getDistance();
 
    // scale the interaction by the height of the viewport
    let viewportHeight = 0.0;
    if (camera.getParallelProjection()) {
      viewportHeight = 2.0 * camera.getParallelScale();
    } else {
      const angle = vtkMath.radiansFromDegrees(camera.getViewAngle());
      viewportHeight = 2.0 * distance * Math.tan(0.5 * angle);
    }
 
    const size = rwi.getView().getSize();
    const delta = (dy * viewportHeight) / size[1];
    distance += delta;
 
    // clamp the distance to the clipping range
    if (distance < range[0]) {
      distance = range[0] + viewportHeight * 1e-3;
    }
    if (distance > range[1]) {
      distance = range[1] - viewportHeight * 1e-3;
    }
    camera.setDistance(distance);
 
    model.lastSlicePosition = position.y;
  };
 
  //----------------------------------------------------------------------------
  // This is a way of dealing with images as if they were layers.
  // It looks through the renderer's list of props and sets the
  // interactor ivars from the Nth image that it finds.  You can
  // also use negative numbers, i.e. -1 will return the last image,
  // -2 will return the second-to-last image, etc.
  publicAPI.setCurrentImageNumber = (i) => {
    Iif (i === null) {
      return;
    }
 
    const renderer = model._interactor.getCurrentRenderer();
    Iif (!renderer) {
      return;
    }
    model.currentImageNumber = i;
 
    function propMatch(j, prop, targetIndex) {
      return j === targetIndex && prop.getNestedPickable();
    }
 
    const props = renderer
      .getViewProps()
      .filter((prop) => prop.isA('vtkImageSlice'));
    let targetIndex = i;
    if (i < 0) {
      targetIndex += props.length;
    }
    const imageProp = props.find((prop, index) =>
      propMatch(index, prop, targetIndex)
    );
 
    if (imageProp) {
      publicAPI.setCurrentImageProperty(imageProp.getProperty());
    }
  };
 
  //----------------------------------------------------------------------------
  publicAPI.setCurrentImageProperty = (imageProperty) => {
    model.currentImageProperty = imageProperty;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  windowLevelStartPosition: [0, 0],
  windowLevelCurrentPosition: [0, 0],
  lastSlicePosition: 0,
  windowLevelInitial: [1.0, 0.5],
  // currentImageProperty: null,
  currentImageNumber: -1,
  interactionMode: 'IMAGE2D',
  xViewRightVector: [0, 1, 0],
  xViewUpVector: [0, 0, -1],
  yViewRightVector: [1, 0, 0],
  yViewUpVector: [0, 0, -1],
  zViewRightVector: [1, 0, 0],
  zViewUpVector: [0, 1, 0],
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  vtkInteractorStyleTrackballCamera.extend(publicAPI, model, initialValues);
 
  // Create get-set macros
  macro.setGet(publicAPI, model, ['interactionMode']);
  macro.get(publicAPI, model, ['currentImageProperty']);
 
  // For more macro methods, see "Sources/macros.js"
 
  // Object specific methods
  vtkInteractorStyleImage(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkInteractorStyleImage');
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };