All files / packages/tools/src/tools/displayTools/Labelmap validateLabelmap.ts

46.66% Statements 7/15
30% Branches 3/10
66.66% Functions 2/3
46.66% Lines 7/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                      17x 17x     17x       17x                                                           17x           17x     17x                          
import { cache } from '@cornerstonejs/core';
import { SegmentationPublicInput } from '../../../types/SegmentationStateTypes';
import {
  LabelmapSegmentationData,
  LabelmapSegmentationDataStack,
  LabelmapSegmentationDataVolume,
} from '../../../types/LabelmapTypes';
 
function validateRepresentationData(
  segmentationRepresentationData: LabelmapSegmentationData
): void {
  if ('volumeId' in segmentationRepresentationData) {
    segmentationRepresentationData =
      segmentationRepresentationData as LabelmapSegmentationDataVolume;
 
    const cachedVolume = cache.getVolume(
      segmentationRepresentationData.volumeId
    );
 
    Iif (!cachedVolume) {
      throw new Error(
        `volumeId of ${segmentationRepresentationData.volumeId} not found in cache, you should load and cache volume before adding segmentation`
      );
    }
  } else Eif ('imageIdReferenceMap' in segmentationRepresentationData) {
    segmentationRepresentationData =
      segmentationRepresentationData as LabelmapSegmentationDataStack;
 
    if (!segmentationRepresentationData.imageIdReferenceMap) {
      throw new Error(
        'The segmentationInput.representationData.imageIdReferenceMap is undefined, please provide a valid representationData.imageIdReferenceMap'
      );
    }
  } else {
    throw new Error(
      'The segmentationInput.representationData is undefined, please provide a valid representationData'
    );
  }
}
 
/**
 * Validates the public segmentation input.
 * Throws an error if the segmentation input is invalid.
 *
 * @param segmentationInput - The segmentation input to validate.
 */
export function validatePublic(
  segmentationInput: SegmentationPublicInput
): void {
  Iif (!segmentationInput.representation.data) {
    throw new Error(
      'The segmentationInput.representationData.data is undefined, please provide a valid representationData.data'
    );
  }
 
  const representationData = segmentationInput.representation
    .data as LabelmapSegmentationData;
 
  validateRepresentationData(representationData);
}
 
/**
 * Validates the given segmentation representation data.
 *
 * @param segmentationRepresentationData The segmentation representation data to validate.
 */
export function validate(
  segmentationRepresentationData: LabelmapSegmentationData
) {
  validateRepresentationData(segmentationRepresentationData);
}