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

83.33% Statements 20/24
50% Branches 4/8
100% Functions 1/1
80% Lines 16/20

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                          79x   79x 79x   79x       79x 79x   79x       79x 79x   79x     79x     79x 79x     79x   79x       79x              
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.getActor(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
  Iif (imageIndex > numberOfSlices - 1) {
    imageIndex = numberOfSlices - 1;
  } else Iif (imageIndex < 0) {
    imageIndex = 0;
  }
 
  return {
    numberOfSlices,
    imageIndex,
  };
}
 
export default getImageSliceDataForVolumeViewport;