All files / tools/src/stateManagement/segmentation/utilities getAnnotationMapFromSegmentation.ts

0% Statements 0/21
0% Branches 0/9
0% Functions 0/7
0% Lines 0/21

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                                                                                                                                                                       
import type { Types } from '@cornerstonejs/core';
import { getAnnotation } from '../../annotation/annotationState';
import type * as ToolsTypes from '../../../types';
 
export type MapOptions = {
  segmentIndices?: number[];
  segmentationId?: string;
  viewport?: Types.IStackViewport | Types.IVolumeViewport;
};
 
export type AnnotationInfo = {
  polyline: Types.Point3[];
  isClosed: boolean;
  annotationUID: string;
  referencedImageId: string;
  holesPolyline: Types.Point3[][];
  holesUIDs: string[];
  holesClosed: boolean[];
};
 
export function getAnnotationMapFromSegmentation(
  contourRepresentationData: ToolsTypes.ContourSegmentationData,
  options: MapOptions = {}
) {
  const annotationMap = contourRepresentationData.annotationUIDsMap;
 
  const segmentIndices = options.segmentIndices?.length
    ? options.segmentIndices
    : Array.from(annotationMap.keys());
 
  const annotationUIDsInSegmentMap = new Map<number, unknown>();
  segmentIndices.forEach((index) => {
    const annotationUIDsInSegment = annotationMap.get(index);
 
    // Todo: there is a bug right now where the annotationUIDsInSegment has both
    // children and parent annotations, so we need to filter out the parent
    // annotations only
 
    let uids = Array.from(annotationUIDsInSegment);
 
    uids = uids.filter(
      (uid) =>
        !(getAnnotation(uid) as ToolsTypes.Annotation).parentAnnotationUID
    );
 
    const annotations = uids.map((uid) => {
      const annotation = getAnnotation(uid) as ToolsTypes.ContourAnnotation;
      const hasChildAnnotations = annotation.childAnnotationUIDs?.length;
      const childPolylinesInformation =
        hasChildAnnotations &&
        annotation.childAnnotationUIDs.map((childUID) => {
          const childAnnotation = getAnnotation(
            childUID
          ) as ToolsTypes.ContourAnnotation;
          return {
            polyline: childAnnotation.data.contour.polyline,
            isClosed: childAnnotation.data.contour.closed,
          };
        });
      const holesClosed =
        hasChildAnnotations &&
        childPolylinesInformation.map((childInfo) => childInfo.isClosed);
 
      const childPolylines =
        hasChildAnnotations &&
        childPolylinesInformation.map((childInfo) => childInfo.polyline);
 
      return {
        polyline: annotation.data.contour.polyline,
        isClosed: annotation.data.contour.closed,
        annotationUID: annotation.annotationUID,
        referencedImageId: annotation.metadata.referencedImageId,
        holesPolyline: childPolylines,
        holesUIDs: annotation.childAnnotationUIDs,
        holesClosed,
      };
    });
 
    annotationUIDsInSegmentMap.set(index, annotations);
  });
 
  return { segmentIndices, annotationUIDsInSegmentMap };
}