All files / packages/tools/src/tools/annotation/planarFreehandROITool openContourEndEditLoop.ts

8% Statements 2/25
0% Branches 0/4
50% Functions 1/2
8% Lines 2/25

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                          1x                                                                                                                                             8x          
import { getEnabledElement } from '@cornerstonejs/core';
import { state } from '../../../store';
import { Events } from '../../../enums';
import { hideElementCursor } from '../../../cursors/elementCursor';
import type {
  EventTypes,
  Annotation,
  ToolHandle,
  TextBoxHandle,
} from '../../../types';
import { PlanarFreehandROIAnnotation } from '../../../types/ToolSpecificAnnotationTypes';
import { polyline } from '../../../utilities/math';
 
const { getSubPixelSpacingAndXYDirections } = polyline;
 
/**
 * Activates the open contour end edit. This actually just sets up the state so
 * that the tool thinks we are mid draw, and then jumps into the drawing loop.
 */
function activateOpenContourEndEdit(
  evt: EventTypes.InteractionEventType,
  annotation: PlanarFreehandROIAnnotation,
  viewportIdsToRender: string[],
  handle: ToolHandle | null
): void {
  this.isDrawing = true;
 
  const eventDetail = evt.detail;
  const { element } = eventDetail;
  const enabledElement = getEnabledElement(element);
  const { viewport } = enabledElement;
 
  const { spacing, xDir, yDir } = getSubPixelSpacingAndXYDirections(
    viewport,
    this.configuration.subPixelResolution
  );
 
  const canvasPoints = annotation.data.contour.polyline.map(
    viewport.worldToCanvas
  );
  const handleIndexGrabbed = annotation.data.handles.activeHandleIndex;
 
  // If 0, invert point direction, if 1, keep point direction the same.
  // This is so we can just jump as into the state as if the annotation was just being drawn.
  if (handleIndexGrabbed === 0) {
    canvasPoints.reverse();
  }
 
  let movingTextBox = false;
  if ((handle as TextBoxHandle)?.worldPosition) {
    movingTextBox = true;
  }
 
  this.drawData = {
    canvasPoints: canvasPoints,
    polylineIndex: canvasPoints.length - 1,
  };
 
  this.commonData = {
    annotation,
    viewportIdsToRender,
    spacing,
    xDir,
    yDir,
    movingTextBox,
  };
 
  state.isInteractingWithTool = true;
 
  // Jump into drawing loop.
  element.addEventListener(Events.MOUSE_UP, this.mouseUpDrawCallback);
  element.addEventListener(Events.MOUSE_DRAG, this.mouseDragDrawCallback);
  element.addEventListener(Events.MOUSE_CLICK, this.mouseUpDrawCallback);
  element.addEventListener(Events.TOUCH_END, this.mouseUpDrawCallback);
  element.addEventListener(Events.TOUCH_DRAG, this.mouseDragDrawCallback);
  element.addEventListener(Events.TOUCH_TAP, this.mouseUpDrawCallback);
 
  hideElementCursor(element);
}
 
/**
 * Registers the open contour end edit loop to the tool instance.
 */
function registerOpenContourEndEditLoop(toolInstance): void {
  toolInstance.activateOpenContourEndEdit =
    activateOpenContourEndEdit.bind(toolInstance);
}
 
export default registerOpenContourEndEditLoop;