All files / packages/tools/src/tools/displayTools SegmentationDisplayTool.ts

74% Statements 37/50
50% Branches 12/24
75% Functions 9/12
74% Lines 37/50

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 213 214 215 216 217 218 219 220 221 222 223 224                                                1x                                                             18x       15x   15x   15x       15x                                                                                           18x 25x   25x         25x   25x               25x   33x         33x 33x           25x   27x   27x   27x           27x     4x     27x   27x 35x           35x   27x       25x   25x 33x               4x       4x     4x                             27x 27x     27x   27x       1x    
import {
  getEnabledElementByIds,
  Types,
  utilities as csUtils,
} from '@cornerstonejs/core';
import Representations from '../../enums/SegmentationRepresentations';
import { config as segmentationConfig } from '../../stateManagement/segmentation';
import { setSegmentationVisibility } from '../../stateManagement/segmentation/config/segmentationVisibility';
import { getSegmentationRepresentations } from '../../stateManagement/segmentation/segmentationState';
import { getToolGroup } from '../../store/ToolGroupManager';
import { PublicToolProps, ToolProps } from '../../types';
import { BaseTool } from '../base';
 
import {
  SegmentationRepresentationConfig,
  ToolGroupSpecificRepresentation,
} from '../../types/SegmentationStateTypes';
import { surfaceDisplay } from './Surface';
import { contourDisplay } from './Contour';
import { labelmapDisplay } from './Labelmap';
import SegmentationRepresentations from '../../enums/SegmentationRepresentations';
import { addTool, state } from '../../store';
import PlanarFreehandContourSegmentationTool from '../annotation/PlanarFreehandContourSegmentationTool';
 
const planarContourToolName = PlanarFreehandContourSegmentationTool.toolName;
/**
 * In Cornerstone3DTools, displaying of segmentations are handled by the SegmentationDisplayTool.
 * Generally, any Segmentation can be viewed in various representations such as
 * labelmap (3d), contours, surface etc. As of now, Cornerstone3DTools only implements
 * Labelmap representation.
 *
 * SegmentationDisplayTool works at ToolGroup level, and is responsible for displaying the
 * segmentation representation for ALL viewports of a toolGroup, this way we can support complex
 * scenarios for displaying segmentations.
 *
 * Current Limitations:
 * - Only supports rendering of the volumetric segmentations in 3D space. (StackViewport segmentations are not supported yet)
 * - Labelmap representation is the only supported representation for now.
 *
 * Similar to other tools in Cornerstone3DTools, the SegmentationDisplayTool should
 * be added to the CornerstoneTools by calling cornerstoneTools.addTool(SegmentationDisplayTool)
 * and a toolGroup should be created for it using the ToolGroupManager API, finally
 * viewports information such as viewportId and renderingEngineId should be provided
 * to the toolGroup and the SegmentationDisplayTool should be set to be activated.
 *
 *
 */
class SegmentationDisplayTool extends BaseTool {
  static toolName;
  constructor(
    toolProps: PublicToolProps = {},
    defaultToolProps: ToolProps = {
      configuration: {},
    }
  ) {
    super(toolProps, defaultToolProps);
  }
 
  onSetToolEnabled(): void {
    const toolGroupId = this.toolGroupId;
    const toolGroupSegmentationRepresentations =
      getSegmentationRepresentations(toolGroupId);
 
    Eif (
      !toolGroupSegmentationRepresentations ||
      toolGroupSegmentationRepresentations.length === 0
    ) {
      return;
    }
 
    // for each segmentationData, make the visibility true
    toolGroupSegmentationRepresentations.forEach(
      (segmentationRepresentation) => {
        setSegmentationVisibility(
          toolGroupId,
          segmentationRepresentation.segmentationRepresentationUID,
          true
        );
      }
    );
  }
 
  onSetToolDisabled(): void {
    const toolGroupId = this.toolGroupId;
    const toolGroupSegmentationRepresentations =
      getSegmentationRepresentations(toolGroupId);
 
    if (
      !toolGroupSegmentationRepresentations ||
      toolGroupSegmentationRepresentations.length === 0
    ) {
      return;
    }
 
    // for each segmentationData, make the visibility false
    toolGroupSegmentationRepresentations.forEach(
      (segmentationRepresentation) => {
        setSegmentationVisibility(
          toolGroupId,
          segmentationRepresentation.segmentationRepresentationUID,
          false
        );
      }
    );
  }
 
  /**
   * It is used to trigger the render for each segmentations in the toolGroup.
   * Based on the segmentation representation type, it will call the corresponding
   * render function.
   *
   * @param toolGroupId - the toolGroupId
   */
  renderSegmentation = (toolGroupId: string): void => {
    const toolGroup = getToolGroup(toolGroupId);
 
    Iif (!toolGroup) {
      return;
    }
 
    const toolGroupSegmentationRepresentations =
      getSegmentationRepresentations(toolGroupId);
 
    Iif (
      !toolGroupSegmentationRepresentations ||
      toolGroupSegmentationRepresentations.length === 0
    ) {
      return;
    }
 
    // toolGroup Viewports
    const toolGroupViewports = toolGroup.viewportsInfo.map(
      ({ renderingEngineId, viewportId }) => {
        const enabledElement = getEnabledElementByIds(
          viewportId,
          renderingEngineId
        );
 
        Eif (enabledElement) {
          return enabledElement.viewport;
        }
      }
    );
 
    // Render each segmentationData, in each viewport in the toolGroup
    const segmentationRenderList = toolGroupSegmentationRepresentations.map(
      (representation: ToolGroupSpecificRepresentation) => {
        const config = this._getMergedRepresentationsConfig(toolGroupId);
 
        const viewportsRenderList = [];
 
        const renderers = {
          [Representations.Labelmap]: labelmapDisplay,
          [Representations.Contour]: contourDisplay,
          [Representations.Surface]: surfaceDisplay,
        };
 
        if (representation.type === SegmentationRepresentations.Contour) {
          // if the representation is contour we need to make sure
          // that the planarFreeHandTool is added to the toolGroup
          this.addPlanarFreeHandToolIfAbsent(toolGroupId);
        }
 
        const display = renderers[representation.type];
 
        for (const viewport of toolGroupViewports) {
          const renderedViewport = display.render(
            viewport as Types.IVolumeViewport,
            representation,
            config
          );
 
          viewportsRenderList.push(renderedViewport);
        }
        return viewportsRenderList;
      }
    );
 
    Promise.allSettled(segmentationRenderList).then(() => {
      // for all viewports in the toolGroup trigger a re-render
      toolGroupViewports.forEach((viewport) => {
        viewport.render();
      });
    });
  };
 
  addPlanarFreeHandToolIfAbsent(toolGroupId) {
    // if it is contour we should check if the toolGroup and more importantly
    // the cornerstoneTools have the planarFreeHandTool added
    Iif (!(planarContourToolName in state.tools)) {
      addTool(PlanarFreehandContourSegmentationTool);
    }
 
    const toolGroup = getToolGroup(toolGroupId);
 
    // check if toolGroup has this tool
    Iif (!toolGroup.hasTool(planarContourToolName)) {
      toolGroup.addTool(planarContourToolName);
      toolGroup.setToolPassive(planarContourToolName);
    }
  }
 
  /**
   * Merge the toolGroup specific configuration with the default global configuration
   * @param toolGroupId
   * @returns
   */
  _getMergedRepresentationsConfig(
    toolGroupId: string
  ): SegmentationRepresentationConfig {
    const toolGroupConfig =
      segmentationConfig.getToolGroupSpecificConfig(toolGroupId);
    const globalConfig = segmentationConfig.getGlobalConfig();
 
    // merge two configurations and override the global config
    const mergedConfig = csUtils.deepMerge(globalConfig, toolGroupConfig);
 
    return mergedConfig;
  }
}
 
SegmentationDisplayTool.toolName = 'SegmentationDisplay';
export default SegmentationDisplayTool;