All files / packages/tools/src/stateManagement/segmentation/helpers normalizeSegmentationInput.ts

90.9% Statements 10/11
92.85% Branches 13/14
100% Functions 1/1
90.9% Lines 10/11

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                                20x   20x 20x     20x     20x       20x 3x       3x         3x         20x                                  
import { SegmentationRepresentations } from '../../../enums';
import {
  SegmentationPublicInput,
  Segmentation,
} from '../../../types/SegmentationStateTypes';
import type { ContourSegmentationData } from '../../../types/ContourTypes';
 
/**
 * It takes in a segmentation input and returns a segmentation with default values
 * @param segmentationInput - The input to the segmentation.
 * @returns A Segmentation object.
 * @internal
 */
function normalizeSegmentationInput(
  segmentationInput: SegmentationPublicInput
): Segmentation {
  const { segmentationId, representation } = segmentationInput;
  const isContourRepresentation =
    representation.type === SegmentationRepresentations.Contour;
  let data = representation.data ? { ...representation.data } : null;
 
  // Contour representation data is defined internally
  data = !data && isContourRepresentation ? {} : data;
 
  // Data cannot be undefined for labelmap and surface
  Iif (!data) {
    throw new Error('Segmentation representation data may not be undefined');
  }
 
  if (isContourRepresentation) {
    const contourData = <ContourSegmentationData>data;
 
    // geometryIds will be removed in a near future. It still exist in the
    // code for compatibility only but it is optional from now on.
    contourData.geometryIds = contourData.geometryIds ?? [];
 
    // Make sure annotationUIDsMap is defined because an empty contour is
    // created before adding contour annotations to the map. Also it prevents
    // breaking legacy code after moving from geometryIds to annotationUIDsMap.
    contourData.annotationUIDsMap = contourData.annotationUIDsMap ?? new Map();
  }
 
  // Todo: we should be able to let the user pass in non-default values for
  // cachedStats, label, activeSegmentIndex, etc.
  return {
    segmentationId,
    cachedStats: {},
    segmentLabels: {},
    label: null,
    segmentsLocked: new Set(),
    type: representation.type,
    activeSegmentIndex: 1,
    representationData: {
      [representation.type]: {
        ...data,
      },
    },
  };
}
 
export default normalizeSegmentationInput;