All files / packages/tools/src/tools/displayTools/Contour contourDisplay.ts

58.62% Statements 17/29
50% Branches 8/16
75% Functions 3/4
58.62% Lines 17/29

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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212                                    1x                                 3x       3x         3x   3x                                             4x 4x   4x       4x   4x                                               4x   4x 4x                                                                                                                                                                 3x   3x       3x   3x                                  
import {
  getEnabledElementByIds,
  Types,
  BaseVolumeViewport,
} from '@cornerstonejs/core';
 
import Representations from '../../../enums/SegmentationRepresentations';
import * as SegmentationState from '../../../stateManagement/segmentation/segmentationState';
import { getToolGroup } from '../../../store/ToolGroupManager';
import {
  SegmentationRepresentationConfig,
  ToolGroupSpecificRepresentation,
} from '../../../types/SegmentationStateTypes';
import { addOrUpdateVTKContourSets } from './vtkContour/addOrUpdateVTKContourSets';
import removeContourFromElement from './removeContourFromElement';
import { deleteConfigCache } from './vtkContour/contourConfigCache';
import { polySeg } from '../../../stateManagement/segmentation';
 
let polySegConversionInProgress = false;
 
/**
 * It removes a segmentation representation from the tool group's viewports and
 * from the segmentation state
 * @param toolGroupId - The toolGroupId of the toolGroup that the
 * segmentationRepresentation belongs to.
 * @param segmentationRepresentationUID - This is the unique identifier
 * for the segmentation representation.
 * @param renderImmediate - If true, the viewport will be rendered
 * immediately after the segmentation representation is removed.
 */
function removeSegmentationRepresentation(
  toolGroupId: string,
  segmentationRepresentationUID: string,
  renderImmediate = false
): void {
  _removeContourFromToolGroupViewports(
    toolGroupId,
    segmentationRepresentationUID
  );
  SegmentationState.removeSegmentationRepresentation(
    toolGroupId,
    segmentationRepresentationUID
  );
 
  deleteConfigCache(segmentationRepresentationUID);
 
  Iif (renderImmediate) {
    const viewportsInfo = getToolGroup(toolGroupId).getViewportsInfo();
    viewportsInfo.forEach(({ viewportId, renderingEngineId }) => {
      const enabledElement = getEnabledElementByIds(
        viewportId,
        renderingEngineId
      );
      enabledElement.viewport.render();
    });
  }
}
 
/**
 * It renders the contour sets for the given segmentation
 * @param viewport - The viewport object
 * @param representation - ToolGroupSpecificRepresentation
 * @param toolGroupConfig - This is the configuration object for the tool group
 */
async function render(
  viewport: Types.IVolumeViewport,
  representationConfig: ToolGroupSpecificRepresentation,
  toolGroupConfig: SegmentationRepresentationConfig
): Promise<void> {
  const { segmentationId } = representationConfig;
  const segmentation = SegmentationState.getSegmentation(segmentationId);
 
  Iif (!segmentation) {
    return;
  }
 
  let contourData = segmentation.representationData[Representations.Contour];
 
  Iif (
    !contourData &&
    polySeg.canComputeRequestedRepresentation(
      representationConfig.segmentationRepresentationUID
    ) &&
    !polySegConversionInProgress
  ) {
    polySegConversionInProgress = true;
 
    contourData = await polySeg.computeAndAddContourRepresentation(
      segmentationId,
      {
        segmentationRepresentationUID:
          representationConfig.segmentationRepresentationUID,
        viewport,
      }
    );
  }
 
  // From here to below it is basically the legacy geometryId based
  // contour rendering via vtkActors that has some bugs for display,
  // as it sometimes appear and sometimes not, and it is not clear.
  // We have moved to the new SVG based contours via our annotation tools
  // check out annotationUIDsMap in the ContourSegmentationData type
  const { geometryIds } = contourData;
 
  Eif (!geometryIds?.length || !(viewport instanceof BaseVolumeViewport)) {
    return;
  }
 
  // add the contour sets to the viewport
  addOrUpdateVTKContourSets(
    viewport,
    geometryIds,
    representationConfig,
    toolGroupConfig
  );
 
  /**
   * The following logic could be added if we want to support the use case
   * where the contour representation data is initiated using annotations
   * in the state from the get-go , and not when the user draws a contour.
   */
  // if (contourData?.points?.length) {
  //   // contourData = createAnnotationsFromPoints(contourData.points);
  //   const contourSegmentationAnnotation = {
  //     annotationUID: csUtils.uuidv4(),
  //     data: {
  //       contour: {
  //         closed: true,
  //         polyline: contourData.points,
  //       },
  //       segmentation: {
  //         segmentationId,
  //         segmentIndex: 1, // Todo
  //         segmentationRepresentationUID:
  //           representationConfig.segmentationRepresentationUID,
  //       },
  //     },
  //     highlighted: false,
  //     invalidated: false,
  //     isLocked: false,
  //     isVisible: true,
  //     metadata: {
  //       toolName: 'PlanarFreehandContourSegmentationTool',
  //       FrameOfReferenceUID: viewport.getFrameOfReferenceUID(),
  //       viewPlaneNormal: viewport.getCamera().viewPlaneNormal,
  //     },
  //   };
 
  //   addAnnotation(contourSegmentationAnnotation, viewport.element);
  // } else if (
  //   !contourData &&
  //   polySeg.canComputeRequestedRepresentation(
  //     representationConfig.segmentationRepresentationUID
  //   )
  // ) {
  // contourData = await polySeg.computeAndAddContourRepresentation(
  //   segmentationId,
  //   {
  //     segmentationRepresentationUID:
  //       representationConfig.segmentationRepresentationUID,
  //     viewport,
  //   }
  // );
  // }
 
  // if (contourData?.geometryIds?.length) {
  //   handleVTKContour({
  //     viewport,
  //     representationConfig,
  //     toolGroupConfig,
  //     geometryIds: contourData.geometryIds,
  //   });
  // } else if (contourData.annotationUIDsMap?.size) {
  //   handleContourAnnotationSegmentation({
  //     viewport,
  //     representationConfig,
  //     toolGroupConfig,
  //     annotationUIDsMap: contourData.annotationUIDsMap,
  //   });
  // }
}
 
function _removeContourFromToolGroupViewports(
  toolGroupId: string,
  segmentationRepresentationUID: string
): void {
  const toolGroup = getToolGroup(toolGroupId);
 
  Iif (toolGroup === undefined) {
    throw new Error(`ToolGroup with ToolGroupId ${toolGroupId} does not exist`);
  }
 
  const { viewportsInfo } = toolGroup;
 
  for (const viewportInfo of viewportsInfo) {
    const { viewportId, renderingEngineId } = viewportInfo;
    const enabledElement = getEnabledElementByIds(
      viewportId,
      renderingEngineId
    );
    removeContourFromElement(
      enabledElement.viewport.element,
      segmentationRepresentationUID
    );
  }
}
 
export default {
  render,
  removeSegmentationRepresentation,
};