All files / packages/tools/src/utilities scroll.ts

62.96% Statements 17/27
25% Branches 2/8
50% Functions 1/2
57.14% Lines 12/21

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                                                                            1x 1x 1x 1x   1x           1x   1x       1x 1x           1x                 1x       1x    
import {
  StackViewport,
  Types,
  VolumeViewport,
  utilities as csUtils,
} from '@cornerstonejs/core';
import { ScrollOptions } from '../types';
 
/**
 * It scrolls one slice in the Stack or Volume Viewport, it uses the options provided
 * to determine the slice to scroll to. For Stack Viewport, it scrolls in the 1 or -1
 * direction, for Volume Viewport, it uses the camera and focal point to determine the
 * slice to scroll to based on the spacings.
 * @param viewport - The viewport in which to scroll
 * @param options - Options to use for scrolling, including direction, invert, and volumeId
 * @returns
 */
export default function scroll(
  viewport: Types.IStackViewport | Types.IVolumeViewport,
  options: ScrollOptions
): void {
  const { type: viewportType } = viewport;
  const { volumeId, delta } = options;
 
  if (viewport instanceof StackViewport) {
    viewport.scroll(delta, options.debounceLoading);
  } else if (viewport instanceof VolumeViewport) {
    scrollVolume(viewport, volumeId, delta);
  } else {
    throw new Error(`Not implemented for Viewport Type: ${viewportType}`);
  }
}
 
export function scrollVolume(
  viewport: VolumeViewport,
  volumeId: string,
  delta: number
) {
  const camera = viewport.getCamera();
  const { focalPoint, viewPlaneNormal, position } = camera;
  const { spacingInNormalDirection, imageVolume } =
    csUtils.getTargetVolumeAndSpacingInNormalDir(viewport, camera, volumeId);
 
  Iif (!imageVolume) {
    throw new Error(
      `Could not find image volume with id ${volumeId} in the viewport`
    );
  }
 
  const actorEntry = viewport.getActor(imageVolume.volumeId);
 
  Iif (!actorEntry) {
    console.warn('No actor found for with actorUID of', imageVolume.volumeId);
  }
 
  const volumeActor = actorEntry.actor as Types.VolumeActor;
  const sliceRange = csUtils.getSliceRange(
    volumeActor,
    viewPlaneNormal,
    focalPoint
  );
 
  const { newFocalPoint, newPosition } = csUtils.snapFocalPointToSlice(
    focalPoint,
    position,
    sliceRange,
    viewPlaneNormal,
    spacingInNormalDirection,
    delta
  );
 
  viewport.setCamera({
    focalPoint: newFocalPoint,
    position: newPosition,
  });
  viewport.render();
}