All files / packages/tools/src/tools/segmentation/strategies/utils getStrategyData.ts

41.02% Statements 16/39
22.72% Branches 5/22
100% Functions 1/1
41.02% Lines 16/39

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103          1x                 9x 9x   9x   9x     9x       9x 9x 9x 9x     9x 9x 9x                                                                                                 9x         9x       9x                    
import { cache, utilities } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import { isVolumeSegmentation } from './stackVolumeCheck';
import { LabelmapToolOperationDataStack } from '../../../../types';
 
const { VoxelManager } = utilities;
 
function getStrategyData({ operationData, viewport }) {
  let segmentationImageData, segmentationScalarData, imageScalarData;
  let imageDimensions: Types.Point3;
  let segmentationDimensions: Types.Point3;
  let imageVoxelManager;
  let segmentationVoxelManager;
 
  if (isVolumeSegmentation(operationData, viewport)) {
    const { volumeId, referencedVolumeId } = operationData;
 
    const segmentationVolume = cache.getVolume(volumeId);
 
    Iif (!segmentationVolume) {
      return;
    }
    segmentationVoxelManager = segmentationVolume.voxelManager;
 
    // we only need the referenceVolumeId if we do thresholding
    // but for other operations we don't need it so make it optional
    Eif (referencedVolumeId) {
      const imageVolume = cache.getVolume(referencedVolumeId);
      imageScalarData = imageVolume.getScalarData();
      imageDimensions = imageVolume.dimensions;
    }
 
    ({ imageData: segmentationImageData } = segmentationVolume);
    segmentationScalarData = segmentationVolume.getScalarData();
    segmentationDimensions = segmentationVolume.dimensions;
  } else E{
    const { imageIdReferenceMap, segmentationRepresentationUID } =
      operationData as LabelmapToolOperationDataStack;
 
    if (!imageIdReferenceMap) {
      return;
    }
 
    const currentImageId = viewport.getCurrentImageId();
    if (!currentImageId) {
      return;
    }
 
    // we know that the segmentationRepresentationUID is the name of the actor always
    // and always circle modifies the current imageId which in fact is the imageData
    // of that actor at that moment so we have the imageData already
    const actor = viewport.getActor(segmentationRepresentationUID);
    if (!actor) {
      return;
    }
    segmentationImageData = actor.actor.getMapper().getInputData();
    segmentationVoxelManager = segmentationImageData.voxelManager;
    const currentSegmentationImageId = imageIdReferenceMap.get(currentImageId);
 
    const segmentationImage = cache.getImage(currentSegmentationImageId);
    if (!segmentationImage) {
      return;
    }
    segmentationScalarData = segmentationImage.getPixelData?.();
 
    const image = cache.getImage(currentImageId);
    const imageData = image ? null : viewport.getImageData();
 
    // VERY IMPORTANT
    // This is the pixel data of the image that is being segmented in the cache
    // and we need to use this to for the modification
    imageScalarData = image?.getPixelData() || imageData.getScalarData();
    imageDimensions = image
      ? [image.columns, image.rows, 1]
      : imageData.dimensions;
    segmentationDimensions = [
      segmentationImage.columns,
      segmentationImage.rows,
      1,
    ];
    imageVoxelManager = image?.voxelManager;
  }
 
  segmentationVoxelManager ||= VoxelManager.createVolumeVoxelManager(
    segmentationDimensions,
    segmentationScalarData
  );
 
  imageVoxelManager ||=
    imageDimensions &&
    VoxelManager.createVolumeVoxelManager(imageDimensions, imageScalarData);
 
  return {
    segmentationImageData,
    segmentationScalarData,
    segmentationVoxelManager,
    imageScalarData,
    imageVoxelManager,
  };
}
 
export { getStrategyData };