All files / packages/tools/src/utilities/viewport jumpToWorld.ts

0% Statements 0/20
0% Branches 0/7
0% Functions 0/2
0% Lines 0/19

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                                                                                                                     
import { VolumeViewport } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
import { vec3 } from 'gl-matrix';
 
// Todo: merge this utility functionality with Crosshair _jump
/**
 * Uses the viewport's current camera to jump to a specific world coordinate
 * @param enabledElement - enabled element
 * @param jumpWorld - location in the world to jump to
 * @returns True if successful
 */
export default function jumpToWorld(
  viewport: Types.IVolumeViewport,
  jumpWorld: Types.Point3
): true | undefined {
  // if not instance of volumeViewport, return
  if (!(viewport instanceof VolumeViewport)) {
    return;
  }
 
  const { focalPoint } = viewport.getCamera();
 
  const delta: Types.Point3 = [0, 0, 0];
  vec3.sub(delta, jumpWorld, focalPoint);
 
  _applyShift(viewport, delta);
 
  return true;
}
 
function _applyShift(viewport, delta) {
  const camera = viewport.getCamera();
  const normal = camera.viewPlaneNormal;
 
  const dotProd = vec3.dot(delta, normal);
  const projectedDelta = vec3.fromValues(normal[0], normal[1], normal[2]);
 
  vec3.scale(projectedDelta, projectedDelta, dotProd);
 
  if (
    Math.abs(projectedDelta[0]) > 1e-3 ||
    Math.abs(projectedDelta[1]) > 1e-3 ||
    Math.abs(projectedDelta[2]) > 1e-3
  ) {
    const newFocalPoint: Types.Point3 = [0, 0, 0];
    const newPosition: Types.Point3 = [0, 0, 0];
 
    vec3.add(newFocalPoint, camera.focalPoint, projectedDelta);
    vec3.add(newPosition, camera.position, projectedDelta);
 
    viewport.setCamera({
      focalPoint: newFocalPoint,
      position: newPosition,
    });
    viewport.render();
  }
}