All files / packages/core/src/utilities getImageSliceDataForVolumeViewport.ts

88% Statements 22/25
60% Branches 6/10
100% Functions 2/2
85.71% Lines 18/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                          233x   233x 233x   233x       233x   233x     233x       233x       233x 233x   233x     233x     233x 233x     233x 16x 217x       233x              
import { ImageSliceData, IVolumeViewport, VolumeActor } from '../types';
import getSliceRange from './getSliceRange';
import getTargetVolumeAndSpacingInNormalDir from './getTargetVolumeAndSpacingInNormalDir';
 
/**
 * It calculates the number of slices and the current slice index for a given
 * Volume viewport
 * @param viewport - volume viewport
 * @returns An object with two properties: numberOfSlices and imageIndex.
 */
function getImageSliceDataForVolumeViewport(
  viewport: IVolumeViewport
): ImageSliceData {
  const camera = viewport.getCamera();
 
  const { spacingInNormalDirection, imageVolume } =
    getTargetVolumeAndSpacingInNormalDir(viewport, camera);
 
  Iif (!imageVolume) {
    return;
  }
 
  const { viewPlaneNormal, focalPoint } = camera;
 
  const actorEntry = viewport
    .getActors()
    .find(
      (a) =>
        a.referenceId === imageVolume.volumeId || a.uid === imageVolume.volumeId
    );
 
  Iif (!actorEntry) {
    console.warn('No actor found for with actorUID of', imageVolume.volumeId);
  }
 
  const volumeActor = actorEntry.actor as VolumeActor;
  const sliceRange = getSliceRange(volumeActor, viewPlaneNormal, focalPoint);
 
  const { min, max, current } = sliceRange;
 
  // calculate number of steps from min to max with current normal spacing in direction
  const numberOfSlices = Math.round((max - min) / spacingInNormalDirection) + 1;
 
  // calculate the imageIndex based on min, max, current
  let imageIndex = ((current - min) / (max - min)) * numberOfSlices;
  imageIndex = Math.floor(imageIndex);
 
  // Clamp imageIndex
  if (imageIndex > numberOfSlices - 1) {
    imageIndex = numberOfSlices - 1;
  } else Iif (imageIndex < 0) {
    imageIndex = 0;
  }
 
  return {
    numberOfSlices,
    imageIndex,
  };
}
 
export default getImageSliceDataForVolumeViewport;