All files / tools/src/utilities/contourSegmentation addPolylinesToSegmentation.ts

10% Statements 1/10
0% Branches 0/4
0% Functions 0/2
10% Lines 1/10

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                                        428x                                                                                              
/**
 * Adds polylines as contour segmentation annotations to a segmentation.
 *
 * Each polyline is associated with a view reference and is added as a new annotation
 * for the specified segment index. The function updates the annotationUIDsMap to include
 * the new annotation UIDs for the segment.
 *
 * @param viewport The Cornerstone3D viewport where the annotation will be added
 * @param annotationUIDsMap Map from segment index to set of annotation UIDs
 * @param segmentationId The ID of the segmentation
 * @param polylinesInfo Array of PolylineInfoWorld ({ polyline, viewReference }) to add
 * @param segmentIndex The segment index to which the polylines belong
 * @returns The updated annotationUIDsMap
 */
 
import type { Types } from '@cornerstonejs/core';
import { utilities } from '@cornerstonejs/core';
import { addAnnotation } from '../../stateManagement';
import type { PolylineInfoWorld } from './polylineInfoTypes';
 
const DEFAULT_CONTOUR_SEG_TOOLNAME = 'PlanarFreehandContourSegmentationTool';
 
export default function addPolylinesToSegmentation(
  viewport: Types.IViewport,
  annotationUIDsMap: Map<number, Set<string>>,
  segmentationId: string,
  polylinesInfo: PolylineInfoWorld[],
  segmentIndex: number
) {
  polylinesInfo.forEach(({ polyline, viewReference }) => {
    if (polyline.length < 3) {
      return;
    }
 
    const contourSegmentationAnnotation = {
      annotationUID: utilities.uuidv4(),
      data: {
        contour: {
          closed: true,
          polyline,
        },
        segmentation: {
          segmentationId,
          segmentIndex,
        },
        handles: {},
      },
      handles: {},
      highlighted: false,
      autoGenerated: false,
      invalidated: false,
      isLocked: false,
      isVisible: true,
      metadata: {
        toolName: DEFAULT_CONTOUR_SEG_TOOLNAME,
        ...viewReference,
      },
    };
 
    addAnnotation(contourSegmentationAnnotation, viewport.element);
 
    const currentSet = annotationUIDsMap?.get(segmentIndex) || new Set();
    currentSet.add(contourSegmentationAnnotation.annotationUID);
    annotationUIDsMap.set(segmentIndex, currentSet);
  });
  return annotationUIDsMap;
}