All files / tools/src/utilities/segmentation getReferenceVolumeForSegmentationVolume.ts

50% Statements 6/12
50% Branches 2/4
100% Functions 1/1
50% Lines 6/12

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                                551x   551x       551x           551x 551x                   551x    
import { cache } from '@cornerstonejs/core';
 
/**
 * Retrieves the reference volume associated with a segmentation volume.
 * This function first checks if the segmentation volume has a direct reference
 * to a volume via referencedVolumeId. If not, it attempts to find the reference
 * volume by looking up the first image in the segmentation's imageIds array,
 * then finding its referenced image and the volume containing that image.
 *
 * @param segmentationVolumeId - The ID of the segmentation volume
 * @returns The reference volume if found, or null if the segmentation volume
 * doesn't exist or no reference volume could be determined
 */
export function getReferenceVolumeForSegmentationVolume(
  segmentationVolumeId: string
) {
  const segmentationVolume = cache.getVolume(segmentationVolumeId);
 
  Iif (!segmentationVolume) {
    return null;
  }
 
  const referencedVolumeId = segmentationVolume.referencedVolumeId;
 
  let imageVolume;
 
  // we only need the referenceVolumeId if we do thresholding
  // but for other operations we don't need it so make it optional
  if (referencedVolumeId) {
    imageVolume = cache.getVolume(referencedVolumeId);
  } else E{
    // find the volume based on the imageIds
    const imageIds = segmentationVolume.imageIds;
    const image = cache.getImage(imageIds[0]);
    const referencedImageId = image.referencedImageId;
    const volumeInfo = cache.getVolumeContainingImageId(referencedImageId);
    imageVolume = volumeInfo?.volume;
  }
 
  return imageVolume;
}