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

43.75% Statements 14/32
0% Branches 0/9
28.57% Functions 2/7
43.75% Lines 14/32

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                                        1x     1x 1x 1x   1x                                         1x           1x                     1x         1x     1x 1x 1x     1x         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 vtkMouseCameraTrackballRotateManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballRotateManipulator';
import vtkMouseCameraTrackballRollManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraTrackballRollManipulator';
 
function max(x, y) {
  return x < y ? y : x;
}
 
function sqr(x) {
  return x * x;
}
 
// ----------------------------------------------------------------------------
// vtkMouseCameraTrackballMultiRotateManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseCameraTrackballMultiRotateManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseCameraTrackballMultiRotateManipulator');
 
  const rotateManipulator =
    vtkMouseCameraTrackballRotateManipulator.newInstance();
  const rollManipulator = vtkMouseCameraTrackballRollManipulator.newInstance();
  let currentManipulator = null;
 
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    const viewSize = interactor.getView().getViewportSize(renderer);
    const viewCenter = [0.5 * viewSize[0], 0.5 * viewSize[1]];
    const rotateRadius = 0.9 * max(viewCenter[0], viewCenter[1]);
    const dist2 =
      sqr(viewCenter[0] - position.x) + sqr(viewCenter[1] - position.y);
 
    if (rotateRadius * rotateRadius > dist2) {
      currentManipulator = rotateManipulator;
    } else {
      currentManipulator = rollManipulator;
    }
 
    currentManipulator.setButton(publicAPI.getButton());
    currentManipulator.setShift(publicAPI.getShift());
    currentManipulator.setControl(publicAPI.getControl());
    currentManipulator.setCenter(publicAPI.getCenter());
 
    currentManipulator.onButtonDown(interactor, position);
  };
 
  publicAPI.onButtonUp = (interactor) => {
    if (currentManipulator) {
      currentManipulator.onButtonUp(interactor);
    }
  };
 
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (currentManipulator) {
      currentManipulator.onMouseMove(interactor, renderer, position);
    }
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {};
 
// ----------------------------------------------------------------------------
 
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);
 
  // Object specific methods
  vtkMouseCameraTrackballMultiRotateManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseCameraTrackballMultiRotateManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };