All files / Sources/Widgets/Manipulators/TrackballManipulator index.js

28.12% Statements 9/32
0% Branches 0/1
50% Functions 3/6
28.12% Lines 9/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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                                                                                                              1x   1x 1x   1x                               1x                     1x               1x   1x         1x          
import { mat4, vec3 } from 'gl-matrix';
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
import vtkAbstractManipulator from 'vtk.js/Sources/Widgets/Manipulators/AbstractManipulator';
 
export function trackballRotate(
  prevX,
  prevY,
  curX,
  curY,
  origin,
  direction,
  renderer,
  glRenderWindow
) {
  const dx = curX - prevX;
  const dy = curY - prevY;
 
  const camera = renderer.getActiveCamera();
  const viewUp = camera.getViewUp();
  const dop = camera.getDirectionOfProjection();
 
  const size = renderer
    .getRenderWindow()
    .getInteractor()
    .getView()
    .getViewportSize(renderer);
  const xdeg = (360.0 * dx) / size[0];
  const ydeg = (360.0 * dy) / size[1];
 
  const newDirection = new Float64Array([
    direction[0],
    direction[1],
    direction[2],
  ]);
 
  const xDisplayAxis = viewUp;
  const yDisplayAxis = [0, 0, 0];
  vtkMath.cross(dop, viewUp, yDisplayAxis);
 
  const rot = mat4.identity(new Float64Array(16));
  mat4.rotate(rot, rot, vtkMath.radiansFromDegrees(xdeg), xDisplayAxis);
  mat4.rotate(rot, rot, vtkMath.radiansFromDegrees(-ydeg), yDisplayAxis);
 
  vec3.transformMat4(newDirection, newDirection, rot);
  return newDirection;
}
 
// ----------------------------------------------------------------------------
// vtkTrackballManipulator methods
// ----------------------------------------------------------------------------
 
function vtkTrackballManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkTrackballManipulator');
 
  let prevX = 0;
  let prevY = 0;
 
  publicAPI.handleEvent = (callData, glRenderWindow) => {
    const newDirection = trackballRotate(
      prevX,
      prevY,
      callData.position.x,
      callData.position.y,
      publicAPI.getOrigin(callData),
      publicAPI.getNormal(callData),
      callData.pokedRenderer,
      glRenderWindow
    );
    prevX = callData.position.x;
    prevY = callData.position.y;
    return { worldCoords: newDirection };
  };
 
  publicAPI.reset = (callData) => {
    prevX = callData.position.x;
    prevY = callData.position.y;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
function defaultValues(initialValues) {
  return {
    ...initialValues,
  };
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  vtkAbstractManipulator.extend(publicAPI, model, defaultValues(initialValues));
 
  vtkTrackballManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkTrackballManipulator');
 
// ----------------------------------------------------------------------------
 
export default { trackballRotate, extend, newInstance };