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

21.32% Statements 29/136
0% Branches 0/25
10% Functions 2/20
21.8% Lines 29/133

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298                1x               1x   1x   1x                     1x                             1x                                                                                 1x                                                                 1x                                                             1x                                                                                 1x                           1x                                                     1x               1x 1x     1x 1x     1x   1x     1x     1x     1x   1x                 1x         1x     1x 1x 1x     1x         1x                
import vtkCompositeCameraManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeCameraManipulator';
import vtkCompositeMouseManipulator from 'vtk.js/Sources/Interaction/Manipulators/CompositeMouseManipulator';
import vtkInteractorStyleConstants from 'vtk.js/Sources/Rendering/Core/InteractorStyle/Constants';
import vtkMouseCameraUnicamRotateManipulator from 'vtk.js/Sources/Interaction/Manipulators/MouseCameraUnicamRotateManipulator';
 
import macro from 'vtk.js/Sources/macros';
import * as vtkMath from 'vtk.js/Sources/Common/Core/Math';
 
const { States } = vtkInteractorStyleConstants;
 
// ----------------------------------------------------------------------------
// vtkMouseCameraUnicamManipulator methods
// ----------------------------------------------------------------------------
 
function vtkMouseCameraUnicamManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkMouseCameraUnicamManipulator');
 
  model.state = States.IS_NONE;
 
  model.rotateManipulator = vtkMouseCameraUnicamRotateManipulator.newInstance({
    button: model.button,
    shift: model.shift,
    control: model.control,
    alt: model.alt,
    dragEnabled: model.dragEnabled,
    scrollEnabled: model.scrollEnabled,
    displayFocusSphereOnButtonDown: false,
  });
 
  //----------------------------------------------------------------------------
  const normalize = (position, interactor) => {
    const renderer = interactor.findPokedRenderer();
    const [width, height] = interactor.getView().getViewportSize(renderer);
 
    const nx = -1.0 + (2.0 * position.x) / width;
    const ny = -1.0 + (2.0 * position.y) / height;
 
    return { x: nx, y: ny };
  };
 
  // Given a 3D point & a vtkCamera, compute the vectors that extend
  // from the projection of the center of projection to the center of
  // the right-edge and the center of the top-edge onto the plane
  // containing the 3D point & with normal parallel to the camera's
  // projection plane.
  const getRightVAndUpV = (downPoint, interactor) => {
    // Compute the horizontal & vertical scaling ('scalex' and 'scaley')
    // factors as function of the down point & camera params.
    const camera = interactor.findPokedRenderer().getActiveCamera();
    const cameraPosition = camera.getPosition();
    const cameraToPointVec = [0, 0, 0];
 
    // Construct a vector from the viewing position to the picked point
    vtkMath.subtract(downPoint, cameraPosition, cameraToPointVec);
    if (camera.getParallelProjection()) {
      vtkMath.multiplyScalar(cameraToPointVec, camera.getParallelScale());
    }
 
    // Get shortest distance 'l' between the viewing position and
    // plane parallel to the projection plane that contains the 'downPoint'.
    const atV = camera.getViewPlaneNormal();
    vtkMath.normalize(atV);
    const l = vtkMath.dot(cameraToPointVec, atV);
    const viewAngle = vtkMath.radiansFromDegrees(camera.getViewAngle());
    const renderer = interactor.findPokedRenderer();
    const [width, height] = interactor.getView().getViewportSize(renderer);
 
    const scaleX = (width / height) * ((2 * l * Math.tan(viewAngle / 2)) / 2);
    const scaleY = (2 * l * Math.tan(viewAngle / 2)) / 2;
 
    // Construct the camera offset vector as function of delta mouse X & Y.
    const upV = camera.getViewUp();
    const rightV = [];
    vtkMath.cross(upV, atV, rightV);
    // (Make sure 'upV' is orthogonal to 'atV' & 'rightV')
    vtkMath.cross(atV, rightV, upV);
    vtkMath.normalize(rightV);
    vtkMath.normalize(upV);
 
    vtkMath.multiplyScalar(rightV, scaleX);
    vtkMath.multiplyScalar(upV, scaleY);
 
    return { rightV, upV };
  };
 
  //----------------------------------------------------------------------------
  const choose = (interactor, position) => {
    const normalizedPosition = normalize(position, interactor);
    const normalizedPreviousPosition = normalize(
      model.previousPosition,
      interactor
    );
    const delta = {
      x: normalizedPosition.x - normalizedPreviousPosition.x,
      y: normalizedPosition.y - normalizedPreviousPosition.y,
    };
    model.previousPosition = position;
 
    const deltaT = Date.now() / 1000 - model.time;
    model.dist += Math.sqrt(delta.x ** 2 + delta.y ** 2);
    const sDelta = {
      x: position.x - model.startPosition.x,
      y: position.y - model.startPosition.y,
    };
    const len = Math.sqrt(sDelta.x ** 2 + sDelta.y ** 2);
    if (Math.abs(sDelta.y) / len > 0.9 && deltaT > 0.05) {
      model.state = States.IS_DOLLY;
    } else if (deltaT >= 0.1 || model.dist >= 0.03) {
      if (Math.abs(sDelta.x) / len > 0.6) {
        model.state = States.IS_PAN;
      } else {
        model.state = States.IS_DOLLY;
      }
    }
  };
 
  //----------------------------------------------------------------------------
  // Transform mouse horizontal & vertical movements to a world
  // space offset for the camera that maintains pick correlation.
  const pan = (interactor, position) => {
    const renderer = interactor.findPokedRenderer();
    const normalizedPosition = normalize(position, interactor);
    const normalizedPreviousPosition = normalize(
      model.previousPosition,
      interactor
    );
 
    const delta = {
      x: normalizedPosition.x - normalizedPreviousPosition.x,
      y: normalizedPosition.y - normalizedPreviousPosition.y,
    };
 
    const camera = renderer.getActiveCamera();
 
    model.previousPosition = position;
 
    const { rightV, upV } = getRightVAndUpV(model.downPoint, interactor);
    const offset = [];
 
    for (let index = 0; index < 3; index++) {
      offset[index] = delta.x * rightV[index] + delta.y * upV[index];
    }
 
    camera.translate(...offset);
 
    renderer.resetCameraClippingRange();
    interactor.render();
  };
 
  //----------------------------------------------------------------------------
  const dolly = (interactor, position) => {
    const renderer = interactor.findPokedRenderer();
    const normalizedPosition = normalize(position, interactor);
    const normalizedPreviousPosition = normalize(
      model.previousPosition,
      interactor
    );
 
    const delta = {
      x: normalizedPosition.x - normalizedPreviousPosition.x,
      y: normalizedPosition.y - normalizedPreviousPosition.y,
    };
 
    const camera = renderer.getActiveCamera();
    const cameraPosition = camera.getPosition();
 
    // 1. Handle dollying
    if (camera.getParallelProjection()) {
      camera.zoom(1 - delta.y);
    } else {
      const offset1 = [];
      vtkMath.subtract(model.downPoint, cameraPosition, offset1);
      vtkMath.multiplyScalar(offset1, delta.y * -4);
 
      camera.translate(...offset1);
    }
 
    // 2. Now handle side-to-side panning
    const { rightV: offset2 } = getRightVAndUpV(model.downPoint, interactor);
 
    vtkMath.multiplyScalar(offset2, delta.x);
 
    camera.translate(...offset2);
 
    renderer.resetCameraClippingRange();
    interactor.render();
  };
 
  //----------------------------------------------------------------------------
  // Public API methods
  //----------------------------------------------------------------------------
  publicAPI.onButtonDown = (interactor, renderer, position) => {
    model.buttonPressed = true;
    model.startPosition = position;
    model.previousPosition = position;
    model.time = Date.now() / 1000.0;
    model.dist = 0;
 
    // Picking is delegated to the rotate manipulator
    model.rotateManipulator.onButtonDown(interactor, renderer, position);
 
    model.downPoint = model.rotateManipulator.getDownPoint();
  };
 
  //----------------------------------------------------------------------------
  publicAPI.onMouseMove = (interactor, renderer, position) => {
    if (!model.buttonPressed) {
      return;
    }
 
    if (model.rotateManipulator.getState() === States.IS_ROTATE) {
      model.rotateManipulator.onMouseMove(interactor, renderer, position);
    } else {
      switch (model.state) {
        case States.IS_NONE:
          choose(interactor, position);
          break;
        case States.IS_PAN:
          pan(interactor, position);
          break;
        case States.IS_DOLLY:
          dolly(interactor, position);
          break;
        default:
          break;
      }
    }
 
    model.previousPosition = position;
  };
 
  //--------------------------------------------------------------------------
  publicAPI.onButtonUp = (interactor) => {
    model.buttonPressed = false;
    if (model.state === States.IS_NONE) {
      model.rotateManipulator.onButtonUp(interactor);
    }
    model.state = States.IS_NONE;
  };
 
  publicAPI.getUseWorldUpVec = () => model.rotateManipulator.getUseWorldUpVec();
  publicAPI.setUseWorldUpVec = (useWorldUpVec) => {
    model.rotateManipulator.setUseWorldUpVec(useWorldUpVec);
  };
  publicAPI.getWorldUpVec = () => model.rotateManipulator.getWorldUpVec();
  publicAPI.setWorldUpVec = (x, y, z) => {
    model.rotateManipulator.setWorldUpVec(x, y, z);
  };
  publicAPI.getUseHardwareSelector = () =>
    model.rotateManipulator.getUseHardwareSelector();
  publicAPI.setUseHardwareSelector = (useHardwareSelector) => {
    model.rotateManipulator.setUseHardwareSelector(useHardwareSelector);
  };
  publicAPI.getFocusSphereColor = () => {
    model.rotateManipulator.getFocusSphereColor();
  };
  publicAPI.setFocusSphereColor = (r, g, b) => {
    model.rotateManipulator.setFocusSphereColor(r, g, b);
  };
  publicAPI.getFocusSphereRadiusFactor = () =>
    model.rotateManipulator.getFocusSphereRadiusFactor();
  publicAPI.setFocusSphereRadiusFactor = (focusSphereRadiusFactor) => {
    model.rotateManipulator.setFocusSphereRadiusFactor(focusSphereRadiusFactor);
  };
}
 
// ----------------------------------------------------------------------------
// 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
  vtkMouseCameraUnicamManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(
  extend,
  'vtkMouseCameraUnicamManipulator'
);
 
// ----------------------------------------------------------------------------
 
export default { newInstance, extend };