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

30.76% Statements 20/65
0% Branches 0/17
50% Functions 2/4
30.76% Lines 20/65

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                        1x   1x 1x 1x 1x 1x 1x 1x 1x 1x   1x       1x                                                                                                                                                                               1x               1x     1x 1x 1x 1x     1x         1x                
import { vec3, mat4 } from 'gl-matrix';
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';
 
// ----------------------------------------------------------------------------
// vtkMouseCameraAxisRotateManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseCameraAxisRotateManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseCameraAxisRotateManipulator');
 
  const negCameraDir = new Float64Array(3);
  const newCamPos = new Float64Array(3);
  const newFp = new Float64Array(3);
  const newViewUp = new Float64Array(3);
  const trans = new Float64Array(16);
  const rotation = new Float64Array(16);
  const v2 = new Float64Array(3);
  const centerNeg = new Float64Array(3);
  const negRotationAxis = new Float64Array(3);
 
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    model.previousPosition = position;
  };
 
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!position) {
      return;
    }
 
    const camera = renderer.getActiveCamera();
    const cameraPos = camera.getPosition();
    const cameraFp = camera.getFocalPoint();
    const cameraViewUp = camera.getViewUp();
    const cameraDirection = camera.getDirectionOfProjection();
    vec3.negate(negCameraDir, cameraDirection);
 
    mat4.identity(trans);
    mat4.identity(rotation);
 
    const { center, rotationFactor, rotationAxis } = model;
    vec3.negate(negRotationAxis, rotationAxis);
 
    const dx = model.previousPosition.x - position.x;
    const dy = model.previousPosition.y - position.y;
 
    const size = interactor.getView().getViewportSize(renderer);
 
    // Azimuth
    const azimuthDelta = vtkMath.radiansFromDegrees(
      ((360.0 * dx) / size[0]) * rotationFactor
    );
    mat4.rotate(rotation, rotation, azimuthDelta, rotationAxis);
 
    // Elevation
    vtkMath.cross(cameraDirection, cameraViewUp, v2);
    let elevationDelta = vtkMath.radiansFromDegrees(
      ((-360.0 * dy) / size[1]) * rotationFactor
    );
    // angle of camera to rotation axis on the positive or negative half,
    // relative to the origin.
    const angleToPosHalf = Math.acos(vec3.dot(negCameraDir, rotationAxis));
    const angleToNegHalf = Math.acos(vec3.dot(negCameraDir, negRotationAxis));
 
    // whether camera is in positive half of the rotation axis or neg half
    const inPosHalf = angleToPosHalf <= angleToNegHalf;
    const elevationToAxis = Math.min(angleToPosHalf, angleToNegHalf);
 
    if (model.useHalfAxis && !inPosHalf) {
      elevationDelta = Math.PI / 2 - angleToPosHalf;
    } else if (inPosHalf && elevationToAxis + elevationDelta < 0) {
      elevationDelta = -elevationToAxis;
      // } else if (!inPosHalf && elevationToAxis - elevationDelta < 0) {
    } else if (!inPosHalf && angleToPosHalf + elevationDelta > Math.PI) {
      elevationDelta = elevationToAxis;
    }
 
    mat4.rotate(rotation, rotation, elevationDelta, v2);
 
    // Translate from origin
    mat4.translate(trans, trans, center);
 
    // apply rotation
    mat4.multiply(trans, trans, rotation);
 
    // Translate to origin
    vec3.negate(centerNeg, center);
    mat4.translate(trans, trans, centerNeg);
 
    // Apply transformation to camera position, focal point, and view up
    vec3.transformMat4(newCamPos, cameraPos, trans);
    vec3.transformMat4(newFp, cameraFp, trans);
 
    vec3.transformMat4(newViewUp, cameraViewUp, rotation);
 
    camera.setPosition(newCamPos[0], newCamPos[1], newCamPos[2]);
    camera.setFocalPoint(newFp[0], newFp[1], newFp[2]);
    camera.setViewUp(newViewUp);
 
    renderer.resetCameraClippingRange();
 
    if (interactor.getLightFollowCamera()) {
      renderer.updateLightsGeometryToFollowCamera();
    }
 
    model.previousPosition = position;
  };
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
const DEFAULT_VALUES = {
  rotationAxis: [0, 0, 1],
  useHalfAxis: true,
};
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  Object.assign(model, DEFAULT_VALUES, initialValues);
 
  // Inheritance
  macro.obj(publicAPI, model);
  macro.setGet(publicAPI, model, ['rotationAxis', 'useHalfAxis']);
  vtkCompositeMouseManipulator.extend(publicAPI, model, initialValues);
  vtkCompositeCameraManipulator.extend(publicAPI, model, initialValues);
 
  // Object specific methods
  vtkMouseCameraAxisRotateManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseCameraAxisRotateManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };