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

63.73% Statements 116/182
38% Branches 19/50
76.92% Functions 10/13
63.73% Lines 116/182

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 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542                                                      1x                   2x   2x 2x 2x 2x 2x   2x     2x         2x           2x                 2x   2x 2x 2x 2x 2x 2x   2x           2x   2x 2x 2x 2x 2x 2x   2x               8x 8x 8x 8x 8x 8x                 8x 8x   8x 8x   8x   8x   8x 8x     8x         8x                               8x   8x     2x   6x             6x       8x                                                                                 2x   2x 2x   2x         2x       2x 2x 2x     2x             2x   2x       2x                   2x   2x 2x     2x 2x 2x   2x   2x   2x               2x 2x   2x 2x   2x             2x                                                                                                                                                                                   8x 8x 8x 8x   8x 8x   8x             8x 6x     2x   2x                     2x 2x 2x 2x     2x             2x   2x 2x       2x         2x         2x   2x   2x                                                                   2x                       2x             2x   2x                                     2x             8x 8x   8x 8x   8x   8x   8x 8x 8x   8x 8x        
import { getEnabledElement, utilities } from '@cornerstonejs/core';
import {
  resetElementCursor,
  hideElementCursor,
} from '../../../cursors/elementCursor';
import { Events } from '../../../enums';
import { EventTypes } from '../../../types';
import { state } from '../../../store';
import { vec3 } from 'gl-matrix';
import {
  shouldSmooth,
  getInterpolatedPoints,
} from '../../../utilities/planarFreehandROITool/smoothPoints';
import getMouseModifierKey from '../../../eventDispatchers/shared/getMouseModifier';
import triggerAnnotationRenderForViewportIds from '../../../utilities/triggerAnnotationRenderForViewportIds';
import { triggerContourAnnotationCompleted } from '../../../stateManagement/annotation/helpers/state';
import { PlanarFreehandROIAnnotation } from '../../../types/ToolSpecificAnnotationTypes';
import findOpenUShapedContourVectorToPeak from './findOpenUShapedContourVectorToPeak';
import { polyline } from '../../../utilities/math';
import { removeAnnotation } from '../../../stateManagement/annotation/annotationState';
import { ContourWindingDirection } from '../../../types/ContourAnnotation';
 
const {
  addCanvasPointsToArray,
  pointsAreWithinCloseContourProximity,
  getFirstLineSegmentIntersectionIndexes,
  getSubPixelSpacingAndXYDirections,
} = polyline;
 
/**
 * Activates the contour drawing event loop.
 */
function activateDraw(
  evt: EventTypes.InteractionEventType,
  annotation: PlanarFreehandROIAnnotation,
  viewportIdsToRender: string[]
): void {
  this.isDrawing = true;
 
  const eventDetail = evt.detail;
  const { currentPoints, element } = eventDetail;
  const canvasPos = currentPoints.canvas;
  const enabledElement = getEnabledElement(element);
  const { viewport } = enabledElement;
  const contourHoleProcessingEnabled =
    getMouseModifierKey(evt.detail.event) ===
    this.configuration.contourHoleAdditionModifierKey;
 
  const { spacing, xDir, yDir } = getSubPixelSpacingAndXYDirections(
    viewport,
    this.configuration.subPixelResolution
  );
 
  this.drawData = {
    canvasPoints: [canvasPos],
    polylineIndex: 0,
    contourHoleProcessingEnabled,
  };
 
  this.commonData = {
    annotation,
    viewportIdsToRender,
    spacing,
    xDir,
    yDir,
    movingTextBox: false,
  };
 
  state.isInteractingWithTool = true;
 
  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);
}
/**
 * Dectivates and cleans up the contour drawing event loop.
 */
function deactivateDraw(element: HTMLDivElement): void {
  state.isInteractingWithTool = false;
 
  element.removeEventListener(Events.MOUSE_UP, this.mouseUpDrawCallback);
  element.removeEventListener(Events.MOUSE_DRAG, this.mouseDragDrawCallback);
  element.removeEventListener(Events.MOUSE_CLICK, this.mouseUpDrawCallback);
  element.removeEventListener(Events.TOUCH_END, this.mouseUpDrawCallback);
  element.removeEventListener(Events.TOUCH_DRAG, this.mouseDragDrawCallback);
  element.removeEventListener(Events.TOUCH_TAP, this.mouseUpDrawCallback);
 
  resetElementCursor(element);
}
 
/**
 * Adds points to a set of preview canvas points of the contour being created.
 * Checks if crossing of lines means early completion and editing needs to be started.
 */
function mouseDragDrawCallback(evt: EventTypes.InteractionEventType): void {
  const eventDetail = evt.detail;
  const { currentPoints, element } = eventDetail;
  const worldPos = currentPoints.world;
  const canvasPos = currentPoints.canvas;
  const enabledElement = getEnabledElement(element);
  const { renderingEngine, viewport } = enabledElement;
 
  const {
    annotation,
    viewportIdsToRender,
    xDir,
    yDir,
    spacing,
    movingTextBox,
  } = this.commonData;
  const { polylineIndex, canvasPoints } = this.drawData;
 
  const lastCanvasPoint = canvasPoints[canvasPoints.length - 1];
  const lastWorldPoint = viewport.canvasToWorld(lastCanvasPoint);
 
  const worldPosDiff = vec3.create();
 
  vec3.subtract(worldPosDiff, worldPos, lastWorldPoint);
 
  const xDist = Math.abs(vec3.dot(worldPosDiff, xDir));
  const yDist = Math.abs(vec3.dot(worldPosDiff, yDir));
 
  // Check that we have moved at least one voxel in each direction.
  Iif (xDist <= spacing[0] && yDist <= spacing[1]) {
    // Haven't changed world point enough, don't render
    return;
  }
 
  Iif (movingTextBox) {
    this.isDrawing = false;
 
    // Drag mode - Move the text boxes world position
    const { deltaPoints } = eventDetail as EventTypes.MouseDragEventDetail;
    const worldPosDelta = deltaPoints.world;
 
    const { textBox } = annotation.data.handles;
    const { worldPosition } = textBox;
 
    worldPosition[0] += worldPosDelta[0];
    worldPosition[1] += worldPosDelta[1];
    worldPosition[2] += worldPosDelta[2];
 
    textBox.hasMoved = true;
  } else {
    const crossingIndex = this.findCrossingIndexDuringCreate(evt);
 
    if (crossingIndex !== undefined) {
      // If we have crossed our drawing line, create a closed contour and then
      // start an edit.
      this.applyCreateOnCross(evt, crossingIndex);
    } else {
      const numPointsAdded = addCanvasPointsToArray(
        element,
        canvasPoints,
        canvasPos,
        this.commonData
      );
 
      this.drawData.polylineIndex = polylineIndex + numPointsAdded;
    }
  }
 
  triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
}
 
/**
 * Completes the contour on mouse up. If the `allowOpenContours` configuration
 * option is `true`, checks if we should create an open or closed contour.
 * If the `allowOpenContours` configuration option is `false`, always creates a
 * closed contour.
 */
function mouseUpDrawCallback(evt: EventTypes.InteractionEventType): void {
  const { allowOpenContours } = this.configuration;
  const { canvasPoints, contourHoleProcessingEnabled } = this.drawData;
  const firstPoint = canvasPoints[0];
  const lastPoint = canvasPoints[canvasPoints.length - 1];
  const eventDetail = evt.detail;
  const { element } = eventDetail;
 
  if (
    allowOpenContours &&
    !pointsAreWithinCloseContourProximity(
      firstPoint,
      lastPoint,
      this.configuration.closeContourProximity
    )
  ) {
    this.completeDrawOpenContour(element, { contourHoleProcessingEnabled });
  } else {
    this.completeDrawClosedContour(element, { contourHoleProcessingEnabled });
  }
}
 
/**
 * Completes the contour being drawn, creating a closed contour annotation. It will return true if contour is completed or false in case contour drawing is halted.
 */
function completeDrawClosedContour(
  element: HTMLDivElement,
  options: {
    contourHoleProcessingEnabled: boolean;
    minPointsToSave: number;
  }
): boolean {
  this.removeCrossedLinesOnCompleteDraw();
 
  const { canvasPoints } = this.drawData;
  const { contourHoleProcessingEnabled, minPointsToSave } = options ?? {};
 
  Iif (minPointsToSave && canvasPoints.length < minPointsToSave) {
    return false;
  }
 
  // check and halt if necessary the drawing process, last chance to complete drawing and fire events.
  Iif (this.haltDrawing(element, canvasPoints)) {
    return false;
  }
 
  const { annotation, viewportIdsToRender } = this.commonData;
  const enabledElement = getEnabledElement(element);
  const { viewport, renderingEngine } = enabledElement;
 
  // Convert annotation to world coordinates
  addCanvasPointsToArray(
    element,
    canvasPoints,
    canvasPoints[0],
    this.commonData
  );
  // Remove last point which will be a duplicate now.
  canvasPoints.pop();
 
  const updatedPoints = shouldSmooth(this.configuration, annotation)
    ? getInterpolatedPoints(this.configuration, canvasPoints)
    : canvasPoints;
 
  this.updateContourPolyline(
    annotation,
    {
      points: updatedPoints,
      closed: true,
      targetWindingDirection: ContourWindingDirection.Clockwise,
    },
    viewport
  );
 
  const { textBox } = annotation.data.handles;
 
  Eif (!textBox?.hasMoved) {
    triggerContourAnnotationCompleted(annotation, contourHoleProcessingEnabled);
  }
 
  this.isDrawing = false;
  this.drawData = undefined;
  this.commonData = undefined;
 
  triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
  this.deactivateDraw(element);
 
  return true;
}
 
/**
 * If lines are crossed during the draw loop, remove the points drawn over the
 * crossing.
 */
function removeCrossedLinesOnCompleteDraw(): void {
  const { canvasPoints } = this.drawData;
  const numPoints = canvasPoints.length;
 
  const endToStart = [canvasPoints[0], canvasPoints[numPoints - 1]];
  const canvasPointsMinusEnds = canvasPoints.slice(0, -1).slice(1);
 
  const lineSegment = getFirstLineSegmentIntersectionIndexes(
    canvasPointsMinusEnds,
    endToStart[0],
    endToStart[1],
    false
  );
 
  Iif (lineSegment) {
    const indexToRemoveUpTo = lineSegment[1];
 
    // It is better to remove the last point when the user just moved the mouse
    // cursor back than deleting the entire contour
    if (indexToRemoveUpTo === 1) {
      this.drawData.canvasPoints = canvasPoints.splice(1);
    } else {
      this.drawData.canvasPoints = canvasPoints.splice(0, indexToRemoveUpTo);
    }
  }
}
 
/**
 * Completes the contour being drawn, creating an open contour annotation. It will return true if contour is completed or false in case contour drawing is halted.
 */
function completeDrawOpenContour(
  element: HTMLDivElement,
  options: {
    contourHoleProcessingEnabled: boolean;
  }
): boolean {
  const { canvasPoints } = this.drawData;
  const { contourHoleProcessingEnabled } = options ?? {};
 
  // check and halt if necessary the drawing process, last chance to complete drawing and fire events.
  if (this.haltDrawing(element, canvasPoints)) {
    return false;
  }
 
  const { annotation, viewportIdsToRender } = this.commonData;
  const enabledElement = getEnabledElement(element);
  const { viewport, renderingEngine } = enabledElement;
 
  const updatedPoints = shouldSmooth(this.configuration, annotation)
    ? getInterpolatedPoints(this.configuration, canvasPoints)
    : canvasPoints;
 
  // Note: -> This is pretty expensive and may not scale well with hundreds of
  // contours. A future optimisation if we use this for segmentation is to re-do
  // this rendering with the GPU rather than SVG.
 
  this.updateContourPolyline(
    annotation,
    {
      points: updatedPoints,
      closed: false,
    },
    viewport
  );
 
  const { textBox } = annotation.data.handles;
  const worldPoints = annotation.data.contour.polyline;
 
  // Add the first and last points to the list of handles. These means they
  // will render handles on mouse hover.
  annotation.data.handles.points = [
    worldPoints[0],
    worldPoints[worldPoints.length - 1],
  ];
 
  // If the annotation is an open U-shaped annotation, find the annotation vector.
  if (annotation.data.isOpenUShapeContour) {
    annotation.data.openUShapeContourVectorToPeak =
      findOpenUShapedContourVectorToPeak(canvasPoints, viewport);
  }
 
  if (!textBox.hasMoved) {
    triggerContourAnnotationCompleted(annotation, contourHoleProcessingEnabled);
  }
 
  this.isDrawing = false;
  this.drawData = undefined;
  this.commonData = undefined;
 
  triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
  this.deactivateDraw(element);
 
  return true;
}
 
/**
 * Searches for a crossing of the contour during create. If found, returns the
 * index of the point just before the lines cross.
 */
function findCrossingIndexDuringCreate(
  evt: EventTypes.InteractionEventType
): number | undefined {
  // Note as we super sample the added points, we need to check the whole last mouse move, not the points
  const eventDetail = evt.detail;
  const { currentPoints, lastPoints } = eventDetail;
  const canvasPos = currentPoints.canvas;
  const lastCanvasPoint = lastPoints.canvas;
 
  const { canvasPoints } = this.drawData;
  const pointsLessLastOne = canvasPoints.slice(0, -1);
 
  const lineSegment = getFirstLineSegmentIntersectionIndexes(
    pointsLessLastOne,
    canvasPos,
    lastCanvasPoint,
    false
  );
 
  if (lineSegment === undefined) {
    return;
  }
 
  const crossingIndex = lineSegment[0];
 
  return crossingIndex;
}
 
/**
 * On crossing of the draw line, create a closed contour, and then start an edit
 * since this occurs during a mouse drag.
 */
function applyCreateOnCross(
  evt: EventTypes.InteractionEventType,
  crossingIndex: number
): void {
  const eventDetail = evt.detail;
  const { element } = eventDetail;
  const { canvasPoints, contourHoleProcessingEnabled } = this.drawData;
  const { annotation, viewportIdsToRender } = this.commonData;
 
  // Add points between the end point and crossing point
  addCanvasPointsToArray(
    element,
    canvasPoints,
    canvasPoints[crossingIndex],
    this.commonData
  );
  // Remove last point which will be a duplicate now.
  canvasPoints.pop();
 
  const remainingPoints = canvasPoints.slice(crossingIndex);
  const newArea = polyline.getArea(remainingPoints);
 
  // User just moved the mouse back and the last points must be removed
  // otherwise the entire contour drawn would be lost.
  Iif (utilities.isEqual(newArea, 0)) {
    canvasPoints.splice(crossingIndex + 1);
    return;
  }
 
  canvasPoints.splice(0, crossingIndex);
 
  // There is no contour with less than 3 points.
  // It's possible to have a contour with very few points after removing
  // crossed lines which is not enough to save as a contour.
  const options = { contourHoleProcessingEnabled, minPointsToSave: 3 };
 
  Eif (this.completeDrawClosedContour(element, options)) {
    // pos complete operation
    this.activateClosedContourEdit(evt, annotation, viewportIdsToRender);
  }
}
 
/**
 * Completes the contour on a cancel method call during the draw loop.
 */
function cancelDrawing(element: HTMLElement) {
  const { allowOpenContours } = this.configuration;
  const { canvasPoints, contourHoleProcessingEnabled } = this.drawData;
  const firstPoint = canvasPoints[0];
  const lastPoint = canvasPoints[canvasPoints.length - 1];
 
  if (
    allowOpenContours &&
    !pointsAreWithinCloseContourProximity(
      firstPoint,
      lastPoint,
      this.configuration.closeContourProximity
    )
  ) {
    this.completeDrawOpenContour(element, { contourHoleProcessingEnabled });
  } else {
    this.completeDrawClosedContour(element, { contourHoleProcessingEnabled });
  }
}
 
/**
 * Tell whether a drawing should be halted or not. It will be true when canvas points is less than the minimum required.
 */
function shouldHaltDrawing(
  canvasPoints: any,
  subPixelResolution: number
): boolean {
  const minPoints = Math.max(
    /**
     * The number of points to span 3 voxels in length, this is a realistically
     * smallest open contour one could reasonably define (2 voxels should probably be a line).
     */
    subPixelResolution * 3,
    /**
     * Minimum 3 points, there are other annotations for one point (probe)
     * or 2 points (line), so this comes only from a mistake in practice.
     */
    3
  );
  return canvasPoints.length < minPoints;
}
 
/**
 * Check and halt a drawing for a given event. It returns true in case drawing is halted, otherswise false.
 */
function haltDrawing(element: HTMLDivElement, canvasPoints: any): boolean {
  const { subPixelResolution } = this.configuration;
 
  Iif (shouldHaltDrawing(canvasPoints, subPixelResolution)) {
    // Remove annotation instead of completing it.
    const { annotation, viewportIdsToRender } = this.commonData;
    const enabledElement = getEnabledElement(element);
    const { renderingEngine } = enabledElement;
 
    removeAnnotation(annotation.annotationUID);
 
    this.isDrawing = false;
    this.drawData = undefined;
    this.commonData = undefined;
 
    triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
    this.deactivateDraw(element);
 
    return true;
  }
 
  return false;
}
 
/**
 * Registers the contour drawing loop to the tool instance.
 */
function registerDrawLoop(toolInstance): void {
  toolInstance.activateDraw = activateDraw.bind(toolInstance);
  toolInstance.deactivateDraw = deactivateDraw.bind(toolInstance);
 
  toolInstance.applyCreateOnCross = applyCreateOnCross.bind(toolInstance);
  toolInstance.findCrossingIndexDuringCreate =
    findCrossingIndexDuringCreate.bind(toolInstance);
  toolInstance.completeDrawOpenContour =
    completeDrawOpenContour.bind(toolInstance);
  toolInstance.removeCrossedLinesOnCompleteDraw =
    removeCrossedLinesOnCompleteDraw.bind(toolInstance);
  toolInstance.mouseDragDrawCallback = mouseDragDrawCallback.bind(toolInstance);
  toolInstance.mouseUpDrawCallback = mouseUpDrawCallback.bind(toolInstance);
  toolInstance.completeDrawClosedContour =
    completeDrawClosedContour.bind(toolInstance);
  toolInstance.cancelDrawing = cancelDrawing.bind(toolInstance);
  toolInstance.haltDrawing = haltDrawing.bind(toolInstance);
}
 
export default registerDrawLoop;