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

18.51% Statements 10/54
0% Branches 0/7
50% Functions 2/4
18.51% Lines 10/54

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                      8x   8x       8x                                                                                                                                                                                         1x         8x     8x 8x 8x     8x         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';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
// ----------------------------------------------------------------------------
// vtkMouseCameraTrackballPanManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseCameraTrackballPanManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseCameraTrackballPanManipulator');
 
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    model.previousPosition = position;
  };
 
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!position) {
      return;
    }
 
    const pos = position;
    const lastPos = model.previousPosition;
    model.previousPosition = position;
 
    const camera = renderer.getActiveCamera();
    const camPos = camera.getPosition();
    const fp = camera.getFocalPoint();
 
    if (camera.getParallelProjection()) {
      camera.orthogonalizeViewUp();
 
      const up = camera.getViewUp();
      const vpn = camera.getViewPlaneNormal();
 
      const right = [0, 0, 0];
 
      vtkMath.cross(vpn, up, right);
 
      // These are different because y is flipped.
      const height = interactor.getView().getViewportSize(renderer)[1];
      let dx = (pos.x - lastPos.x) / height;
      let dy = (lastPos.y - pos.y) / height;
 
      const scale = camera.getParallelScale();
      dx *= scale * 2.0;
      dy *= scale * 2.0;
 
      let tmp = right[0] * dx + up[0] * dy;
      camPos[0] += tmp;
      fp[0] += tmp;
      tmp = right[1] * dx + up[1] * dy;
      camPos[1] += tmp;
      fp[1] += tmp;
      tmp = right[2] * dx + up[2] * dy;
      camPos[2] += tmp;
      fp[2] += tmp;
      camera.setPosition(camPos[0], camPos[1], camPos[2]);
      camera.setFocalPoint(fp[0], fp[1], fp[2]);
    } else {
      const { center } = model;
      const style = interactor.getInteractorStyle();
      const focalDepth = style.computeWorldToDisplay(
        renderer,
        center[0],
        center[1],
        center[2]
      )[2];
      const worldPoint = style.computeDisplayToWorld(
        renderer,
        pos.x,
        pos.y,
        focalDepth
      );
      const lastWorldPoint = style.computeDisplayToWorld(
        renderer,
        lastPos.x,
        lastPos.y,
        focalDepth
      );
 
      const newCamPos = [
        camPos[0] + (lastWorldPoint[0] - worldPoint[0]),
        camPos[1] + (lastWorldPoint[1] - worldPoint[1]),
        camPos[2] + (lastWorldPoint[2] - worldPoint[2]),
      ];
 
      const newFp = [
        fp[0] + (lastWorldPoint[0] - worldPoint[0]),
        fp[1] + (lastWorldPoint[1] - worldPoint[1]),
        fp[2] + (lastWorldPoint[2] - worldPoint[2]),
      ];
 
      camera.setPosition(newCamPos[0], newCamPos[1], newCamPos[2]);
      camera.setFocalPoint(newFp[0], newFp[1], newFp[2]);
    }
 
    renderer.resetCameraClippingRange();
 
    if (interactor.getLightFollowCamera()) {
      renderer.updateLightsGeometryToFollowCamera();
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  macro.obj(publicAPI, model);
  vtkCompositeCameraManipulator.extend(publicAPI, model, initialValues);
  vtkCompositeMouseManipulator.extend(publicAPI, model, initialValues);
 
  // Object specific methods
  vtkMouseCameraTrackballPanManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseCameraTrackballPanManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };