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

0% Statements 0/22
0% Branches 0/10
0% Functions 0/1
0% Lines 0/22

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                                                                                                                                         
import type {
  ContourSegmentationAnnotation,
  ContourSegmentationData,
} from '../../../types';
import { getSegmentation } from '../getSegmentation';
import { convertContourPolylineToCanvasSpace } from '../../../utilities/contourSegmentation';
import {
  getViewportsAssociatedToSegmentation,
  getViewportWithMatchingViewPlaneNormal,
} from './getViewportAssociatedToSegmentation';
import { getPolylinesMap } from './getPolylineMap';
import { getAnnotation } from '../../annotation/annotationState';
 
/**
 * Extracts segment polylines from a segmentation and converts them to canvas space coordinates.
 * This function retrieves all contour annotations for a specific segment and transforms their
 * world coordinates to canvas coordinates for rendering or processing.
 *
 * @param segmentationId - The unique identifier of the segmentation
 * @param segmentIndex - The index of the segment within the segmentation
 * @returns A map of annotation UIDs to their canvas space polylines, or undefined if extraction fails
 */
export function extractSegmentPolylines(
  segmentationId: string,
  segmentIndex: number
) {
  const viewports = getViewportsAssociatedToSegmentation(segmentationId);
 
  const segmentation = getSegmentation(segmentationId);
  if (!segmentation) {
    return;
  }
 
  if (!segmentation.representationData.Contour) {
    return;
  }
 
  const contourRepresentationData = segmentation.representationData
    .Contour as ContourSegmentationData;
  const { annotationUIDsMap } = contourRepresentationData;
  if (!annotationUIDsMap) {
    return;
  }
 
  if (!annotationUIDsMap.get(segmentIndex)) {
    return;
  }
 
  const polyLinesMap = getPolylinesMap(contourRepresentationData, segmentIndex);
  if (!polyLinesMap) {
    return;
  }
  const keys = Array.from(polyLinesMap?.keys());
  const polylinesCanvasMap = new Map();
 
  for (const key of keys) {
    const annotation = getAnnotation(key) as ContourSegmentationAnnotation;
    const viewport = getViewportWithMatchingViewPlaneNormal(
      viewports,
      annotation
    );
    polylinesCanvasMap.set(
      key,
      convertContourPolylineToCanvasSpace(polyLinesMap.get(key), viewport)
    );
  }
  return polylinesCanvasMap;
}