All files / utils/test addMockContourSegmentation.ts

0% Statements 0/18
0% Branches 0/6
0% Functions 0/3
0% Lines 0/18

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 85                                                                                                                                                                         
import { utilities as csUtils } from '@cornerstonejs/core';
import { annotation, utilities as cstUtils } from '@cornerstonejs/tools';
 
export function addMockContourSegmentation({
  segmentationId,
  contours,
  viewport,
}) {
  contours = Array.isArray(contours) ? contours : [contours];
 
  contours.forEach((contour) => {
    const {
      segmentIndex = 1,
      radius = 100,
      resolution = 100,
      centerOffset = [0, 0],
    } = contour;
 
    // get the circle annotation that would draw if we pick the center
    // as the circle center and the radius as the distance from the center
    // to the edge of the viewport
 
    const centerInCanvas = [
      viewport.canvas.width / 2 + centerOffset[0],
      viewport.canvas.height / 2 + centerOffset[1],
    ];
    const radiusInCanvas = radius;
 
    const polyline = Array.from(Array(resolution).keys()).map((i) => {
      const angle = (i * 2 * Math.PI) / resolution;
      const x = centerInCanvas[0] + radiusInCanvas * Math.cos(angle);
      const y = centerInCanvas[1] + radiusInCanvas * Math.sin(angle);
 
      const world = viewport.canvasToWorld([x, y]);
 
      return world;
    });
 
    const FrameOfReferenceUID = viewport.getFrameOfReferenceUID();
    const camera = viewport.getCamera();
    const contourSegmentationAnnotation = {
      annotationUID: csUtils.uuidv4(),
      data: {
        contour: {
          closed: true,
          polyline,
        },
        segmentation: {
          segmentationId,
          segmentIndex,
        },
        handles: {},
      },
      handles: {},
      highlighted: false,
      autoGenerated: false,
      invalidated: false,
      isLocked: false,
      isVisible: true,
      metadata: {
        referencedImageId: viewport.getCurrentImageId(),
        toolName: 'PlanarFreehandContourSegmentationTool',
        FrameOfReferenceUID: FrameOfReferenceUID,
        viewPlaneNormal: camera.viewPlaneNormal,
      },
    };
 
    const annotationGroupSelector = viewport.element;
 
    annotation.state.addAnnotation(
      contourSegmentationAnnotation,
      annotationGroupSelector
    );
 
    cstUtils.contourSegmentation.addContourSegmentationAnnotation(
      contourSegmentationAnnotation
    );
 
    cstUtils.triggerAnnotationRenderForViewportIds(
      viewport.getRenderingEngine(),
      [viewport.id]
    );
  });
}