All files / Sources/Interaction/Manipulators/MouseCameraTrackballZoomManipulator index.js

21.05% Statements 12/57
0% Branches 0/19
40% Functions 2/5
21.05% Lines 12/57

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                    24x   24x                           24x                                                                                       24x                                                   1x               24x     24x 24x 24x   24x     24x         1x                
import macro from 'vtk.js/Sources/macros';
import vtkCompositeCameraManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeCameraManipulator';
import vtkCompositeMouseManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeMouseManipulator';
 
// ----------------------------------------------------------------------------
// vtkMouseCameraTrackballZoomManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseCameraTrackballZoomManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseCameraTrackballZoomManipulator');
 
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    model.previousPosition = position;
    const size = interactor.getView().getViewportSize(renderer);
 
    const camera = renderer.getActiveCamera();
    const direction = model.flipDirection ? -1 : 1;
    if (camera.getParallelProjection()) {
      model.zoomScale = (1.5 / size[1]) * direction;
    } else {
      const range = camera.getClippingRange();
      model.zoomScale = 1.5 * (range[1] / size[1]) * direction;
    }
  };
 
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!position) {
      return;
    }
 
    const dy = model.previousPosition.y - position.y;
    const camera = renderer.getActiveCamera();
 
    if (camera.getParallelProjection()) {
      const k = dy * model.zoomScale;
      camera.setParallelScale((1.0 - k) * camera.getParallelScale());
    } else {
      const cameraPos = camera.getPosition();
      const cameraFp = camera.getFocalPoint();
      const norm = camera.getDirectionOfProjection();
      const k = dy * model.zoomScale;
 
      let tmp = k * norm[0];
      cameraPos[0] += tmp;
      cameraFp[0] += tmp;
 
      tmp = k * norm[1];
      cameraPos[1] += tmp;
      cameraFp[1] += tmp;
 
      tmp = k * norm[2];
      cameraPos[2] += tmp;
      cameraFp[2] += tmp;
 
      if (!camera.getFreezeFocalPoint()) {
        camera.setFocalPoint(cameraFp[0], cameraFp[1], cameraFp[2]);
      }
 
      camera.setPosition(cameraPos[0], cameraPos[1], cameraPos[2]);
      renderer.resetCameraClippingRange();
    }
 
    if (interactor.getLightFollowCamera()) {
      renderer.updateLightsGeometryToFollowCamera();
    }
 
    model.previousPosition = position;
  };
 
  publicAPI.onScroll = (interactor, renderer, delta) => {
    if (!delta) {
      return;
    }
 
    const camera = renderer.getActiveCamera();
 
    const dyf = 1 - delta / 10;
 
    if (camera.getParallelProjection()) {
      camera.setParallelScale(camera.getParallelScale() / dyf);
    } else {
      camera.dolly(dyf);
      renderer.resetCameraClippingRange();
    }
 
    if (interactor.getLightFollowCamera()) {
      renderer.updateLightsGeometryToFollowCamera();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  zoomScale: 0.0,
  flipDirection: false,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  macro.obj(publicAPI, model);
  vtkCompositeMouseManipulator.extend(publicAPI, model, initialValues);
  vtkCompositeCameraManipulator.extend(publicAPI, model, initialValues);
 
  macro.setGet(publicAPI, model, ['flipDirection']);
 
  // Object specific methods
  vtkMouseCameraTrackballZoomManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseCameraTrackballZoomManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };