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

24% Statements 6/25
0% Branches 0/5
60% Functions 3/5
25% Lines 6/24

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                                                                                                              2x   2x                                 2x               2x   2x         1x          
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';
import { EPSILON } from 'vtk.js/Sources/Common/Core/Math/Constants';
 
export function projectDisplayToLine(
  x,
  y,
  lineOrigin,
  lineDirection,
  renderer,
  glRenderWindow
) {
  // if the active camera viewPlaneNormal and line direction are parallel, no change is allowed
  const dotProduct = Math.abs(
    vtkMath.dot(renderer.getActiveCamera().getViewPlaneNormal(), lineDirection)
  );
 
  if (1 - dotProduct < EPSILON) {
    return [];
  }
  const near = glRenderWindow.displayToWorld(x, y, 0, renderer);
  const far = glRenderWindow.displayToWorld(x, y, 1, renderer);
  const viewDir = [0, 0, 0];
  vtkMath.subtract(far, near, viewDir);
 
  const normal = [0, 0, 0];
  vtkMath.cross(lineDirection, viewDir, normal);
  vtkMath.cross(normal, viewDir, normal);
 
  const numerator = vtkMath.dot(
    [near[0] - lineOrigin[0], near[1] - lineOrigin[1], near[2] - lineOrigin[2]],
    normal
  );
  const denominator = vtkMath.dot(normal, lineDirection);
 
  const result = lineDirection.slice();
  if (denominator === 0) {
    // no change is allowed
    vtkMath.multiplyScalar(result, 0);
  } else {
    vtkMath.multiplyScalar(result, numerator / denominator);
  }
  vtkMath.add(lineOrigin, result, result);
 
  return result;
}
 
// ----------------------------------------------------------------------------
// vtkLineManipulator methods
// ----------------------------------------------------------------------------
 
function vtkLineManipulator(publicAPI, model) {
  // Set our className
  model.classHierarchy.push('vtkLineManipulator');
 
  publicAPI.handleEvent = (callData, glRenderWindow) => ({
    worldCoords: projectDisplayToLine(
      callData.position.x,
      callData.position.y,
      publicAPI.getOrigin(callData),
      publicAPI.getNormal(callData),
      callData.pokedRenderer,
      glRenderWindow
    ),
  });
}
 
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
 
function defaultValues(initialValues) {
  return {
    ...initialValues,
  };
}
 
// ----------------------------------------------------------------------------
 
export function extend(publicAPI, model, initialValues = {}) {
  vtkAbstractManipulator.extend(publicAPI, model, defaultValues(initialValues));
 
  vtkLineManipulator(publicAPI, model);
}
 
// ----------------------------------------------------------------------------
 
export const newInstance = macro.newInstance(extend, 'vtkLineManipulator');
 
// ----------------------------------------------------------------------------
 
export default { projectDisplayToLine, extend, newInstance };