All files / tools/src/utilities/spatial translateViewportAlongNormal.ts

0% Statements 0/16
0% Branches 0/12
0% Functions 0/1
0% Lines 0/16

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                                                                                                                                             
import { vec3 } from 'gl-matrix';
import { utilities as csUtils } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import getViewportICamera from '../getViewportICamera';
import { navigatePlanarViewportToPoint } from '../genericViewportToolHelpers';
 
/**
 * Moves a viewport camera (position and focal point) along its view-plane
 * normal by the given signed distance in world units. Pan, zoom and
 * orientation are untouched.
 *
 * Native (generic) viewports have no free camera write; they are navigated by
 * view reference instead, which snaps to the nearest slice.
 *
 * Returns true when the camera was moved.
 */
export default function translateViewportAlongNormal(
  viewport: Types.IViewport,
  distance: number
): boolean {
  if (!viewport || !Number.isFinite(distance) || distance === 0) {
    return false;
  }
 
  // getViewportICamera can throw for viewports without a valid/rendered
  // camera; guard it the same way getViewportPlane does so a single bad
  // viewport cannot abort a caller iterating over several of them.
  let camera;
  try {
    camera = getViewportICamera(viewport);
  } catch {
    return false;
  }
 
  const viewPlaneNormal = camera?.viewPlaneNormal;
  const focalPoint = camera?.focalPoint;
  const position = camera?.position;
 
  if (!viewPlaneNormal || !focalPoint || !position) {
    return false;
  }
 
  const newFocalPoint = vec3.scaleAndAdd(
    vec3.create(),
    focalPoint,
    viewPlaneNormal,
    distance
  );
  const newPosition = vec3.scaleAndAdd(
    vec3.create(),
    position,
    viewPlaneNormal,
    distance
  );
 
  if (csUtils.isGenericViewport(viewport)) {
    return navigatePlanarViewportToPoint(viewport, [
      newFocalPoint[0],
      newFocalPoint[1],
      newFocalPoint[2],
    ]);
  }
 
  viewport.setCamera({
    focalPoint: [newFocalPoint[0], newFocalPoint[1], newFocalPoint[2]],
    position: [newPosition[0], newPosition[1], newPosition[2]],
  });
 
  return true;
}