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

0% Statements 0/15
0% Branches 0/14
0% Functions 0/3
0% Lines 0/15

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                                                                                                                                                   
import {
  getEnabledElement,
  StackViewport,
  VolumeViewport,
  utilities as csUtils,
  Types,
} from '@cornerstonejs/core';
import JumpToSliceOptions from '../../types/JumpToSliceOptions';
import clip from '../clip';
import scroll from '../scroll';
 
/**
 * It uses the imageIndex in the Options to scroll to the slice that is intended.
 * It works for both Stack and Volume viewports. In VolumeViewports, the imageIndex
 * should be given with respect to the index in the 3D image in the view direction
 * (i.e. the index of the slice in Axial, Sagittal, Coronal, or Oblique).
 *
 * @param element - the HTML Div element scrolling inside
 * @param options - the options used for jumping to a slice
 * @returns Promise that resolves to ImageIdIndex
 */
async function jumpToSlice(
  element: HTMLDivElement,
  options = {} as JumpToSliceOptions
): Promise<void> {
  const { imageIndex, debounceLoading } = options;
  const enabledElement = getEnabledElement(element);
 
  if (!enabledElement) {
    throw new Error('Element has been disabled');
  }
 
  const { viewport } = enabledElement;
 
  const { imageIndex: currentImageIndex, numberOfSlices } = _getImageSliceData(
    viewport,
    debounceLoading
  );
 
  const imageIndexToJump = _getImageIndexToJump(numberOfSlices, imageIndex);
  const delta = imageIndexToJump - currentImageIndex;
 
  scroll(viewport, { delta, debounceLoading });
}
 
function _getImageSliceData(
  viewport: Types.IStackViewport | Types.IVolumeViewport,
  debounceLoading?: boolean
): Types.ImageSliceData {
  if (viewport instanceof StackViewport) {
    return {
      numberOfSlices: viewport.getImageIds().length,
      imageIndex: debounceLoading
        ? viewport.getTargetImageIdIndex()
        : viewport.getCurrentImageIdIndex(),
    };
  } else if (viewport instanceof VolumeViewport) {
    return csUtils.getImageSliceDataForVolumeViewport(viewport);
  } else {
    throw new Error('Unsupported viewport type');
  }
}
 
function _getImageIndexToJump(
  numberOfSlices: number,
  imageIndex: number
): number {
  const lastSliceIndex = numberOfSlices - 1;
 
  return clip(imageIndex, 0, lastSliceIndex);
}
 
export default jumpToSlice;