All files / packages/tools/src/tools/segmentation SegmentSelectTool.ts

3.92% Statements 2/51
0% Branches 0/23
0% Functions 0/9
4% Lines 2/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                                                                1x                                                                                                                                                                                                                                                                                             1x    
import { getEnabledElement } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
import { BaseTool } from '../base';
import {
  PublicToolProps,
  ToolProps,
  EventTypes,
  ToolGroupSpecificRepresentation,
} from '../../types';
import { triggerSegmentationModified } from '../../stateManagement/segmentation/triggerSegmentationEvents';
import triggerAnnotationRenderForViewportIds from '../../utilities/triggerAnnotationRenderForViewportIds';
import { getActiveSegmentationRepresentation } from '../../stateManagement/segmentation/activeSegmentation';
import RepresentationTypes from '../../enums/SegmentationRepresentations';
import { setActiveSegmentIndex } from '../../stateManagement/segmentation/segmentIndex';
import {
  getHoveredContourSegmentationAnnotation,
  getSegmentAtLabelmapBorder,
  getSegmentAtWorldPoint,
} from '../../utilities/segmentation';
import { state } from '../../store';
import SegmentationRepresentations from '../../enums/SegmentationRepresentations';
 
/**
 * Represents a tool used for segment selection. It is used to select a segment
 * by hovering over it.
 *
 */
class SegmentSelectTool extends BaseTool {
  static toolName;
  private hoverTimer: ReturnType<typeof setTimeout> | null;
 
  static SelectMode = {
    Inside: 'Inside',
    Border: 'Border',
  };
 
  constructor(
    toolProps: PublicToolProps = {},
    defaultToolProps: ToolProps = {
      supportedInteractionTypes: ['Mouse', 'Touch'],
      configuration: {
        hoverTimeout: 100,
        mode: SegmentSelectTool.SelectMode.Border,
        searchRadius: 6, // search for border in a 6px radius
      },
    }
  ) {
    super(toolProps, defaultToolProps);
    this.hoverTimer = null;
  }
 
  mouseMoveCallback = (evt: EventTypes.InteractionEventType): boolean => {
    if (this.hoverTimer) {
      clearTimeout(this.hoverTimer);
    }
 
    this.hoverTimer = setTimeout(() => {
      this._setActiveSegment(evt);
      this.hoverTimer = null;
    }, this.configuration.hoverTimeout);
 
    return true;
  };
 
  onSetToolEnabled = (): void => {
    this.onSetToolActive();
  };
 
  onSetToolActive = (): void => {
    this.hoverTimer = null;
  };
 
  onSetToolDisabled = (): void => {
    this.hoverTimer = null;
  };
 
  _setActiveSegment(evt = {} as EventTypes.InteractionEventType): void {
    if (state.isInteractingWithTool) {
      return;
    }
 
    const { element, currentPoints } = evt.detail;
 
    const worldPoint = currentPoints.world;
 
    const enabledElement = getEnabledElement(element);
 
    if (!enabledElement) {
      return;
    }
 
    const { viewport } = enabledElement;
 
    const activeSegmentationReps = getActiveSegmentationRepresentation(
      this.toolGroupId
    );
 
    if (!activeSegmentationReps) {
      return;
    }
 
    const supportedTypes = [
      RepresentationTypes.Labelmap,
      RepresentationTypes.Contour,
    ];
 
    if (supportedTypes.includes(activeSegmentationReps.type)) {
      this._setActiveSegmentForType(
        activeSegmentationReps,
        worldPoint,
        viewport
      );
    } else {
      console.warn(
        'SegmentSelectTool does not support the current segmentation type.'
      );
    }
  }
 
  _setActiveSegmentForType(
    activeSegmentationReps: ToolGroupSpecificRepresentation,
    worldPoint: Types.Point3,
    viewport: Types.IStackViewport | Types.IVolumeViewport
  ): void {
    const imageDataInfo = viewport.getImageData();
 
    if (!imageDataInfo) {
      return;
    }
 
    const { segmentationId, type } = activeSegmentationReps;
 
    let hoveredSegmentIndex;
 
    if (this.configuration.mode === SegmentSelectTool.SelectMode.Inside) {
      hoveredSegmentIndex = getSegmentAtWorldPoint(segmentationId, worldPoint, {
        viewport,
      });
    } else {
      switch (type) {
        case SegmentationRepresentations.Labelmap:
          hoveredSegmentIndex = getSegmentAtLabelmapBorder(
            segmentationId,
            worldPoint,
            {
              viewport,
              searchRadius: this.configuration.searchRadius,
            }
          );
          break;
 
        case SegmentationRepresentations.Contour:
          hoveredSegmentIndex =
            getHoveredContourSegmentationAnnotation(segmentationId);
          break;
      }
    }
 
    // No need to select background
    if (!hoveredSegmentIndex || hoveredSegmentIndex === 0) {
      return;
    }
 
    setActiveSegmentIndex(segmentationId, hoveredSegmentIndex);
 
    const renderingEngine = viewport.getRenderingEngine();
    const viewportIds = renderingEngine.getViewports().map((v) => v.id);
 
    // update states
    triggerSegmentationModified(segmentationId);
    triggerAnnotationRenderForViewportIds(renderingEngine, viewportIds);
  }
}
 
SegmentSelectTool.toolName = 'SegmentSelectTool';
export default SegmentSelectTool;