All files / packages/tools/src/tools/annotation PlanarFreehandROITool.ts

30.43% Statements 84/276
29.03% Branches 27/93
41.66% Functions 10/24
30.99% Lines 84/271

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 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021                                                                                                1x 1x   1x                                                                                                                                             6x 6x 6x                                                                                                                                                                                                                                                           6x       6x 6x 6x 6x 6x 6x   6x                           6x     2x 2x 2x 2x   2x       2x   2x         2x   2x   2x   2x                       6x                                               6x                                                               6x                                                                             6x                                             3x       3x 3x       3x                             3x     3x                                                                                                                                                       2x 2x   2x   2x     2x                           2x           2x           1x 1x   1x 1x   1x 1x 1x   1x               1x   1x 1x           1x 1x                                         1x     1x       1x               1x   1x                   1x   1x       1x                                                                     6x                                                                                                                                                                                                                                                                                                                                                                                                                                                                             6x           1x 1x   1x           1x 1x       1x 1x 1x                                                                                 1x                   1x   1x   1x             1x       1x       1x       1x       1x     1x    
import {
  CONSTANTS,
  getEnabledElement,
  VolumeViewport,
  utilities as csUtils,
} from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import { vec3 } from 'gl-matrix';
 
import { getCalibratedLengthUnitsAndScale } from '../../utilities/getCalibratedUnits';
import { math, roundNumber } from '../../utilities';
import { polyline } from '../../utilities/math';
import { filterAnnotationsForDisplay } from '../../utilities/planar';
import throttle from '../../utilities/throttle';
import { getViewportIdsWithToolToRender } from '../../utilities/viewportFilters';
import triggerAnnotationRenderForViewportIds from '../../utilities/triggerAnnotationRenderForViewportIds';
import registerDrawLoop from './planarFreehandROITool/drawLoop';
import registerEditLoopCommon from './planarFreehandROITool/editLoopCommon';
import registerClosedContourEditLoop from './planarFreehandROITool/closedContourEditLoop';
import registerOpenContourEditLoop from './planarFreehandROITool/openContourEditLoop';
import registerOpenContourEndEditLoop from './planarFreehandROITool/openContourEndEditLoop';
import registerRenderMethods from './planarFreehandROITool/renderMethods';
import type {
  EventTypes,
  ToolHandle,
  Annotation,
  Annotations,
  AnnotationStyle,
  PublicToolProps,
  ToolProps,
  SVGDrawingHelper,
  AnnotationRenderContext,
} from '../../types';
import { triggerAnnotationModified } from '../../stateManagement/annotation/helpers/state';
import { drawLinkedTextBox } from '../../drawingSvg';
import { PlanarFreehandROIAnnotation } from '../../types/ToolSpecificAnnotationTypes';
import { getTextBoxCoordsCanvas } from '../../utilities/drawing';
import { PlanarFreehandROICommonData } from '../../utilities/math/polyline/planarFreehandROIInternalTypes';
 
import { getLineSegmentIntersectionsCoordinates } from '../../utilities/math/polyline';
import pointInShapeCallback from '../../utilities/pointInShapeCallback';
import { isViewportPreScaled } from '../../utilities/viewport/isViewportPreScaled';
import { getModalityUnit } from '../../utilities/getModalityUnit';
import { BasicStatsCalculator } from '../../utilities/math/basic';
import calculatePerimeter from '../../utilities/contours/calculatePerimeter';
import ContourSegmentationBaseTool from '../base/ContourSegmentationBaseTool';
import { KeyboardBindings, ChangeTypes } from '../../enums';
 
const { pointCanProjectOnLine } = polyline;
const { EPSILON } = CONSTANTS;
 
const PARALLEL_THRESHOLD = 1 - EPSILON;
 
/**
 * PlanarFreehandROITool lets you draw annotations that define an arbitrarily drawn region.
 * You can use the PlanarFreehandROITool in all perpendicular views (axial, sagittal, coronal),
 * support for oblique views is possible, but not yet supported, due to the implementation of
 * `getSubPixelSpacingAndXYDirections`.
 *
 * The resulting annotation's data and metadata (the
 * state of the viewport while drawing was happening) will get added to the
 * ToolState manager and can be accessed from the ToolState by calling getAnnotations
 * or similar methods.
 *
 * PlanarFreehandROITool annotation can be smoothed on drawing completion. This is a configured based approach.
 * The smoothing process uses b-spline algorithm and consider 4 configurations properties:
 * - smoothing.smoothOnAdd: to tell whether it should be smoothed or not (for editing it is considered the property smoothOnEdit) (default: false)
 * - smoothing.smoothOnEdit: to tell whether it should be smoothed or not when editing (default: false)
 * - smoothing.knotsRatioPercentageOnAdd: percentage of points from Segment that are likely to be considered knots during smoothing (for editing it is considered the property knotsRatioPercentageOnEdit) ( default: 40)
 * - smoothing.knotsRatioPercentageOnEdit: same as knotsRatioPercentageOnAdd but applicable only when editing the tool (default: 40)
 *
 * So, with that said the smoothing might occur when:
 * - drawing is done (i.e mouse is released) and smoothing.smoothOnAdd is true. smoothing algorithm uses knotsRatioPercentageOnAdd
 * - edit drawing is done (i.e mouse is released) and smoothing.smoothOnEdit is true. smoothing algorithm uses knotsRatioPercentageOnEdit and its only applied to changed segment
 * smoothing does not occur when:
 * - smoothing.smoothOnAdd is false and drawing is completed
 * - smoothing.smoothOnEdit is false and edit is completed
 * - drawing still happening (editing or not)
 *
 * The result of smoothing will be removal of some of the outliers
 * Changing tool configuration (see below) you can fine-tune the smoothing process by changing knotsRatioPercentageOnAdd and knotsRatioPercentageOnEdit value, which smaller values produces a more agressive smoothing.
 * A smaller value of knotsRatioPercentageOnAdd/knotsRatioPercentageOnEdit produces a more aggressive smoothing.
 *
 * ```js
 * cornerstoneTools.addTool(PlanarFreehandROITool)
 *
 * const toolGroup = ToolGroupManager.createToolGroup('toolGroupId')
 *
 * toolGroup.addTool(PlanarFreehandROITool.toolName)
 *
 * toolGroup.addViewport('viewportId', 'renderingEngineId')
 *
 * toolGroup.setToolActive(PlanarFreehandROITool.toolName, {
 *   bindings: [
 *    {
 *       mouseButton: MouseBindings.Primary, // Left Click
 *     },
 *   ],
 * })
 *
 * // set smoothing aggressiveness while adding new annotation (ps: this does not change if smoothing is ON or OFF)
 * toolGroup.setToolConfiguration(PlanarFreehandROITool.toolName, {
 *   smoothing: { knotsRatioPercentageOnAdd: 30 },
 * });
 *
 * // set smoothing to be ON while editing only
 * toolGroup.setToolConfiguration(PlanarFreehandROITool.toolName, {
 *   smoothing: { smoothOnAdd: false, smoothOnEdit: true  },
 * });
 * ```
 *
 *
 * Read more in the Docs section of the website.
 */
 
class PlanarFreehandROITool extends ContourSegmentationBaseTool {
  static toolName;
 
  public touchDragCallback: any;
  public mouseDragCallback: any;
  _throttledCalculateCachedStats: any;
  private commonData?: PlanarFreehandROICommonData;
  isDrawing = false;
  isEditingClosed = false;
  isEditingOpen = false;
 
  protected activateDraw: (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation,
    viewportIdsToRender: string[]
  ) => void;
  private activateClosedContourEdit: (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation,
    viewportIdsToRender: string[]
  ) => void;
  private activateOpenContourEdit: (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation,
    viewportIdsToRender: string[]
  ) => void;
  private activateOpenContourEndEdit: (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation,
    viewportIdsToRender: string[],
    handle: ToolHandle | null
  ) => void;
  private cancelDrawing: (element: HTMLDivElement) => void;
  private cancelClosedContourEdit: (element: HTMLDivElement) => void;
  private cancelOpenContourEdit: (element: HTMLDivElement) => void;
 
  private renderContour: (
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper,
    annotation: PlanarFreehandROIAnnotation
  ) => void;
  private renderContourBeingDrawn: (
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper,
    annotation: PlanarFreehandROIAnnotation
  ) => void;
  private renderClosedContourBeingEdited: (
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper,
    annotation: PlanarFreehandROIAnnotation
  ) => void;
  private renderOpenContourBeingEdited: (
    enabledElement: Types.IEnabledElement,
    svgDrawingHelper: SVGDrawingHelper,
    annotation: PlanarFreehandROIAnnotation
  ) => void;
 
  constructor(
    toolProps: PublicToolProps = {},
    defaultToolProps: ToolProps = {
      supportedInteractionTypes: ['Mouse', 'Touch'],
      configuration: {
        shadow: true,
        preventHandleOutsideImage: false,
        /**
         * Specify which modifier key is used to add a hole to a contour. The
         * modifier must be pressed when the first point of a new contour is added.
         */
        contourHoleAdditionModifierKey: KeyboardBindings.Shift,
        alwaysRenderOpenContourHandles: {
          // When true, always render end points when you have an open contour, rather
          // than just rendering a line.
          enabled: false,
          // When enabled, use this radius to draw the endpoints whilst not hovering.
          radius: 2,
        },
        allowOpenContours: true,
        // Proximity in canvas coordinates used to join contours.
        closeContourProximity: 10,
        // The proximity at which we fallback to the simplest grabbing logic for
        // determining what index of the contour to start editing.
        checkCanvasEditFallbackProximity: 6,
        // For closed contours, make them clockwise
        // This can be useful if contours are compared between slices, eg for
        // interpolation, and does not cause problems otherwise so defaulting to true.
        makeClockWise: true,
        // The relative distance that points should be dropped along the polyline
        // in units of the image pixel spacing. A value of 1 means that nodes must
        // be placed no closed than the image spacing apart. A value of 4 means that 4
        // nodes should be placed within the space of one image pixel size. A higher
        // value gives more finesse to the tool/smoother lines, but the value cannot
        // be infinite as the lines become very computationally expensive to draw.
        subPixelResolution: 4,
        /**
         * Smoothing is used to remove jagged irregularities in the polyline,
         * as opposed to interpolation, which is used to create new polylines
         * between existing polylines.
         */
        smoothing: {
          smoothOnAdd: false,
          smoothOnEdit: false, // used for edit only
          knotsRatioPercentageOnAdd: 40,
          knotsRatioPercentageOnEdit: 40,
        },
        /**
         * Interpolation is the creation of new segmentations in between the
         * existing segmentations/indices.  Note that this does not apply to
         * ROI values, since those annotations are individual annotations, not
         * connected in any way to each other, whereas segmentations are intended
         * to be connected 2d + 1 dimension (time or space or other) volumes.
         */
        interpolation: {
          enabled: false,
          // Callback to update the annotation or perform other action when the
          // interpolation is complete.
          onInterpolationComplete: null,
        },
        /**
         * The polyline may get processed in order to reduce the number of points
         * for better performance and storage.
         */
        decimate: {
          enabled: false,
          /** A maximum given distance 'epsilon' to decide if a point should or
           * shouldn't be added the resulting polyline which will have a lower
           * number of points for higher `epsilon` values.
           */
          epsilon: 0.1,
        },
        calculateStats: true,
        getTextLines: defaultGetTextLines,
        statsCalculator: BasicStatsCalculator,
      },
    }
  ) {
    super(toolProps, defaultToolProps);
 
    // Register event loops and rendering logic, which are stored in different
    // Files due to their complexity/size.
    registerDrawLoop(this);
    registerEditLoopCommon(this);
    registerClosedContourEditLoop(this);
    registerOpenContourEditLoop(this);
    registerOpenContourEndEditLoop(this);
    registerRenderMethods(this);
 
    this._throttledCalculateCachedStats = throttle(
      this._calculateCachedStats,
      100,
      { trailing: true }
    );
  }
 
  /**
   * Based on the current position of the mouse and the current image, creates
   * a `PlanarFreehandROIAnnotation` and stores it in the annotationManager.
   *
   * @param evt - `EventTypes.NormalizedMouseEventType`
   * @returns The `PlanarFreehandROIAnnotation` object.
   */
  addNewAnnotation = (
    evt: EventTypes.InteractionEventType
  ): PlanarFreehandROIAnnotation => {
    const eventDetail = evt.detail;
    const { element } = eventDetail;
    const enabledElement = getEnabledElement(element);
    const { renderingEngine } = enabledElement;
 
    const annotation = this.createAnnotation(
      evt
    ) as PlanarFreehandROIAnnotation;
 
    this.addAnnotation(annotation, element);
 
    const viewportIdsToRender = getViewportIdsWithToolToRender(
      element,
      this.getToolName()
    );
 
    this.activateDraw(evt, annotation, viewportIdsToRender);
 
    evt.preventDefault();
 
    triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender);
 
    return annotation;
  };
 
  /**
   * Begins an edit of an open contour, when the mouse has selected a handle
   * (end) of the open contour.
   *
   * @param evt - `EventTypes.MouseDownEventType`
   * @param annotation - `PlanarFreehandROIAnnotation` annotation.
   * @param handle - The handle index, 0 for the start and 1 for the end.
   * @param interactionType - interaction type (mouse, touch)
   */
  handleSelectedCallback = (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation,
    handle: ToolHandle
  ): void => {
    const eventDetail = evt.detail;
    const { element } = eventDetail;
 
    const viewportIdsToRender = getViewportIdsWithToolToRender(
      element,
      this.getToolName()
    );
 
    this.activateOpenContourEndEdit(
      evt,
      annotation,
      viewportIdsToRender,
      handle
    );
  };
 
  /**
   * Edits the open or closed contour when the line is grabbed and dragged.
   */
  toolSelectedCallback = (
    evt: EventTypes.InteractionEventType,
    annotation: PlanarFreehandROIAnnotation
  ): void => {
    const eventDetail = evt.detail;
    const { element } = eventDetail;
 
    const viewportIdsToRender = getViewportIdsWithToolToRender(
      element,
      this.getToolName()
    );
 
    if (annotation.data.contour.closed) {
      this.activateClosedContourEdit(evt, annotation, viewportIdsToRender);
    } else {
      this.activateOpenContourEdit(evt, annotation, viewportIdsToRender);
    }
 
    evt.preventDefault();
  };
 
  /**
   * Returns if the canvas point is near the line of the given annotation in the
   * provided element or not. A proximity is passed to the function to determine the
   * proximity of the point to the annotation in number of pixels.
   *
   * @param element - HTML Element
   * @param annotation - The `PlanarFreehandROIAnnotation`.
   * @param canvasCoords - Canvas coordinates
   * @param proximity - Proximity to tool to consider
   * @returns Boolean, whether the canvas point is near tool
   */
  isPointNearTool = (
    element: HTMLDivElement,
    annotation: PlanarFreehandROIAnnotation,
    canvasCoords: Types.Point2,
    proximity: number
  ): boolean => {
    const enabledElement = getEnabledElement(element);
    const { viewport } = enabledElement;
 
    const { polyline: points } = annotation.data.contour;
 
    // NOTE: It is implemented this way so that we do not double calculate
    // points when number crunching adjacent line segments.
    let previousPoint = viewport.worldToCanvas(points[0]);
 
    for (let i = 1; i < points.length; i++) {
      const p1 = previousPoint;
      const p2 = viewport.worldToCanvas(points[i]);
      const canProject = pointCanProjectOnLine(canvasCoords, p1, p2, proximity);
 
      if (canProject) {
        return true;
      }
 
      previousPoint = p2;
    }
 
    if (!annotation.data.contour.closed) {
      // Contour is open, don't check last point to first point.
      return false;
    }
 
    // check last point to first point
    const pStart = viewport.worldToCanvas(points[0]);
    const pEnd = viewport.worldToCanvas(points[points.length - 1]);
 
    return pointCanProjectOnLine(canvasCoords, pStart, pEnd, proximity);
  };
 
  public cancel = (element: HTMLDivElement): void => {
    const isDrawing = this.isDrawing;
    const isEditingOpen = this.isEditingOpen;
    const isEditingClosed = this.isEditingClosed;
 
    if (isDrawing) {
      this.cancelDrawing(element);
    } else if (isEditingOpen) {
      this.cancelOpenContourEdit(element);
    } else if (isEditingClosed) {
      this.cancelClosedContourEdit(element);
    }
  };
 
  /**
   * @override We need to override this method as the tool doesn't always have
   * `handles`, which means `filterAnnotationsForDisplay` fails inside
   * `filterAnnotationsWithinSlice`.
   */
  public filterInteractableAnnotationsForElement(
    element: HTMLDivElement,
    annotations: Annotations
  ): Annotations | undefined {
    Iif (!annotations || !annotations.length) {
      return;
    }
 
    const enabledElement = getEnabledElement(element);
    const { viewport } = enabledElement;
 
    let annotationsToDisplay;
 
    Iif (viewport instanceof VolumeViewport) {
      const camera = viewport.getCamera();
 
      const { spacingInNormalDirection } =
        csUtils.getTargetVolumeAndSpacingInNormalDir(viewport, camera);
 
      // Get data with same normal and within the same slice
      annotationsToDisplay = this.filterAnnotationsWithinSlice(
        annotations,
        camera,
        spacingInNormalDirection
      );
    } else {
      // Use the default `filterAnnotationsForDisplay` utility, as the stack
      // path doesn't require handles.
      annotationsToDisplay = filterAnnotationsForDisplay(viewport, annotations);
    }
 
    return annotationsToDisplay;
  }
 
  /**
   * Altered version of the `utilities.planar.filterAnnotationsWithinSlice`,
   * which uses the polyline position rather than the handle. As the polyline is
   * always present.
   */
  private filterAnnotationsWithinSlice(
    annotations: Annotations,
    camera: Types.ICamera,
    spacingInNormalDirection: number
  ): Annotations {
    const { viewPlaneNormal } = camera;
 
    const annotationsWithParallelNormals = annotations.filter(
      (td: Annotation) => {
        const annotationViewPlaneNormal = td.metadata.viewPlaneNormal;
 
        const isParallel =
          Math.abs(vec3.dot(viewPlaneNormal, annotationViewPlaneNormal)) >
          PARALLEL_THRESHOLD;
 
        return annotationViewPlaneNormal && isParallel;
      }
    ) as PlanarFreehandROIAnnotation[];
 
    // No in plane annotations.
    if (!annotationsWithParallelNormals.length) {
      return [];
    }
 
    // Annotation should be within the slice, which means that it should be between
    // camera's focalPoint +/- spacingInNormalDirection.
 
    const halfSpacingInNormalDirection = spacingInNormalDirection / 2;
    const { focalPoint } = camera;
 
    const annotationsWithinSlice = [];
 
    for (const annotation of annotationsWithParallelNormals) {
      const data = annotation.data;
      const point = data.contour.polyline[0];
 
      if (!annotation.isVisible) {
        continue;
      }
 
      // A = point
      // B = focal point
      // P = normal
 
      // B-A dot P  => Distance in the view direction.
      // this should be less than half the slice distance.
 
      const dir = vec3.create();
 
      vec3.sub(dir, focalPoint, point);
 
      const dot = vec3.dot(dir, viewPlaneNormal);
 
      if (Math.abs(dot) < halfSpacingInNormalDirection) {
        annotationsWithinSlice.push(annotation);
      }
    }
 
    return annotationsWithinSlice;
  }
 
  protected isContourSegmentationTool(): boolean {
    // Disable contour segmentation behavior because it shall be activated only
    // for PlanarFreehandContourSegmentationTool
    return false;
  }
 
  protected createAnnotation(evt: EventTypes.InteractionEventType): Annotation {
    const worldPos = evt.detail.currentPoints.world;
    const contourAnnotation = super.createAnnotation(evt);
 
    const onInterpolationComplete = (annotation) => {
      // Clear out the handles because they aren't used for straight freeform
      annotation.data.handles.points.length = 0;
    };
 
    const annotation = <PlanarFreehandROIAnnotation>csUtils.deepMerge(
      contourAnnotation,
      {
        data: {
          contour: {
            polyline: [<Types.Point3>[...worldPos]],
          },
          label: '',
          cachedStats: {},
        },
        onInterpolationComplete,
      }
    );
 
    return annotation;
  }
 
  protected getAnnotationStyle(context) {
    // This method exists only because `super` cannot be called from
    // _getRenderingOptions() which is in an external file.
    return super.getAnnotationStyle(context);
  }
 
  protected renderAnnotationInstance(
    renderContext: AnnotationRenderContext
  ): boolean {
    const { enabledElement, targetId, svgDrawingHelper } = renderContext;
    const annotation = renderContext.annotation as PlanarFreehandROIAnnotation;
 
    let renderStatus = false;
    const { viewport, renderingEngine } = enabledElement;
 
    const isDrawing = this.isDrawing;
    const isEditingOpen = this.isEditingOpen;
    const isEditingClosed = this.isEditingClosed;
 
    Iif (!(isDrawing || isEditingOpen || isEditingClosed)) {
      // No annotations are currently being modified, so we can just use the
      // render contour method to render all of them
      this.renderContour(enabledElement, svgDrawingHelper, annotation);
    } else {
      // The active annotation will need special rendering treatment. Render all
      // other annotations not being interacted with using the standard renderContour
      // rendering path.
      const activeAnnotationUID = this.commonData.annotation.annotationUID;
 
      if (annotation.annotationUID === activeAnnotationUID) {
        Iif (isDrawing) {
          this.renderContourBeingDrawn(
            enabledElement,
            svgDrawingHelper,
            annotation
          );
        } else if (isEditingClosed) {
          this.renderClosedContourBeingEdited(
            enabledElement,
            svgDrawingHelper,
            annotation
          );
        } else Eif (isEditingOpen) {
          this.renderOpenContourBeingEdited(
            enabledElement,
            svgDrawingHelper,
            annotation
          );
        } else {
          throw new Error(
            `Unknown ${this.getToolName()} annotation rendering state`
          );
        }
      } else E{
        this.renderContour(enabledElement, svgDrawingHelper, annotation);
      }
 
      // Todo: return boolean flag for each rendering route in the planar tool.
      renderStatus = true;
    }
 
    Iif (!this.configuration.calculateStats) {
      return;
    }
 
    this._calculateStatsIfActive(
      annotation,
      targetId,
      viewport,
      renderingEngine,
      enabledElement
    );
 
    this._renderStats(annotation, viewport, enabledElement, svgDrawingHelper);
 
    return renderStatus;
  }
 
  _calculateStatsIfActive(
    annotation: PlanarFreehandROIAnnotation,
    targetId: string,
    viewport,
    renderingEngine,
    enabledElement
  ) {
    const activeAnnotationUID = this.commonData?.annotation.annotationUID;
 
    Eif (
      annotation.annotationUID === activeAnnotationUID &&
      !this.commonData?.movingTextBox
    ) {
      return;
    }
 
    if (!this.commonData?.movingTextBox) {
      const { data } = annotation;
      if (
        !data.cachedStats[targetId] ||
        data.cachedStats[targetId].areaUnit == null
      ) {
        data.cachedStats[targetId] = {
          Modality: null,
          area: null,
          max: null,
          mean: null,
          stdDev: null,
          areaUnit: null,
        };
 
        this._calculateCachedStats(
          annotation,
          viewport,
          renderingEngine,
          enabledElement
        );
      } else if (annotation.invalidated) {
        this._throttledCalculateCachedStats(
          annotation,
          viewport,
          renderingEngine,
          enabledElement
        );
      }
    }
  }
 
  private _calculateCachedStats = (
    annotation,
    viewport,
    renderingEngine,
    enabledElement
  ) => {
    const { data } = annotation;
    const { cachedStats } = data;
    const { polyline: points, closed } = data.contour;
 
    const targetIds = Object.keys(cachedStats);
 
    for (let i = 0; i < targetIds.length; i++) {
      const targetId = targetIds[i];
      const image = this.getTargetIdImage(targetId, renderingEngine);
 
      // If image does not exists for the targetId, skip. This can be due
      // to various reasons such as if the target was a volumeViewport, and
      // the volumeViewport has been decached in the meantime.
      if (!image) {
        continue;
      }
 
      const { imageData, metadata } = image;
      const canvasCoordinates = points.map((p) => viewport.worldToCanvas(p));
 
      // Using an arbitrary start point (canvasPoint), calculate the
      // mm spacing for the canvas in the X and Y directions.
      const canvasPoint = canvasCoordinates[0];
      const originalWorldPoint = viewport.canvasToWorld(canvasPoint);
      const deltaXPoint = viewport.canvasToWorld([
        canvasPoint[0] + 1,
        canvasPoint[1],
      ]);
      const deltaYPoint = viewport.canvasToWorld([
        canvasPoint[0],
        canvasPoint[1] + 1,
      ]);
 
      const deltaInX = vec3.distance(originalWorldPoint, deltaXPoint);
      const deltaInY = vec3.distance(originalWorldPoint, deltaYPoint);
 
      const worldPosIndex = csUtils.transformWorldToIndex(imageData, points[0]);
      worldPosIndex[0] = Math.floor(worldPosIndex[0]);
      worldPosIndex[1] = Math.floor(worldPosIndex[1]);
      worldPosIndex[2] = Math.floor(worldPosIndex[2]);
 
      let iMin = worldPosIndex[0];
      let iMax = worldPosIndex[0];
 
      let jMin = worldPosIndex[1];
      let jMax = worldPosIndex[1];
 
      let kMin = worldPosIndex[2];
      let kMax = worldPosIndex[2];
 
      for (let j = 1; j < points.length; j++) {
        const worldPosIndex = csUtils.transformWorldToIndex(
          imageData,
          points[j]
        );
        worldPosIndex[0] = Math.floor(worldPosIndex[0]);
        worldPosIndex[1] = Math.floor(worldPosIndex[1]);
        worldPosIndex[2] = Math.floor(worldPosIndex[2]);
        iMin = Math.min(iMin, worldPosIndex[0]);
        iMax = Math.max(iMax, worldPosIndex[0]);
 
        jMin = Math.min(jMin, worldPosIndex[1]);
        jMax = Math.max(jMax, worldPosIndex[1]);
 
        kMin = Math.min(kMin, worldPosIndex[2]);
        kMax = Math.max(kMax, worldPosIndex[2]);
      }
 
      const worldPosIndex2 = csUtils.transformWorldToIndex(
        imageData,
        points[1]
      );
      worldPosIndex2[0] = Math.floor(worldPosIndex2[0]);
      worldPosIndex2[1] = Math.floor(worldPosIndex2[1]);
      worldPosIndex2[2] = Math.floor(worldPosIndex2[2]);
 
      const { scale, areaUnits } = getCalibratedLengthUnitsAndScale(
        image,
        () => {
          const polyline = data.contour.polyline;
          const numPoints = polyline.length;
          const projectedPolyline = new Array(numPoints);
 
          for (let i = 0; i < numPoints; i++) {
            projectedPolyline[i] = viewport.worldToCanvas(polyline[i]);
          }
 
          const {
            maxX: canvasMaxX,
            maxY: canvasMaxY,
            minX: canvasMinX,
            minY: canvasMinY,
          } = math.polyline.getAABB(projectedPolyline);
 
          const topLeftBBWorld = viewport.canvasToWorld([
            canvasMinX,
            canvasMinY,
          ]);
 
          const topLeftBBIndex = csUtils.transformWorldToIndex(
            imageData,
            topLeftBBWorld
          );
 
          const bottomRightBBWorld = viewport.canvasToWorld([
            canvasMaxX,
            canvasMaxY,
          ]);
 
          const bottomRightBBIndex = csUtils.transformWorldToIndex(
            imageData,
            bottomRightBBWorld
          );
 
          return [topLeftBBIndex, bottomRightBBIndex];
        }
      );
      let area = polyline.getArea(canvasCoordinates) / scale / scale;
      // Convert from canvas_pixels ^2 to mm^2
      area *= deltaInX * deltaInY;
 
      // Expand bounding box
      const iDelta = 0.01 * (iMax - iMin);
      const jDelta = 0.01 * (jMax - jMin);
      const kDelta = 0.01 * (kMax - kMin);
 
      iMin = Math.floor(iMin - iDelta);
      iMax = Math.ceil(iMax + iDelta);
      jMin = Math.floor(jMin - jDelta);
      jMax = Math.ceil(jMax + jDelta);
      kMin = Math.floor(kMin - kDelta);
      kMax = Math.ceil(kMax + kDelta);
 
      const boundsIJK = [
        [iMin, iMax],
        [jMin, jMax],
        [kMin, kMax],
      ] as [Types.Point2, Types.Point2, Types.Point2];
 
      const worldPosEnd = imageData.indexToWorld([iMax, jMax, kMax]);
      const canvasPosEnd = viewport.worldToCanvas(worldPosEnd);
 
      let curRow = 0;
      let intersections = [];
      let intersectionCounter = 0;
      const pointsInShape = pointInShapeCallback(
        imageData,
        (pointLPS, pointIJK) => {
          let result = true;
          const point = viewport.worldToCanvas(pointLPS);
          if (point[1] != curRow) {
            intersectionCounter = 0;
            curRow = point[1];
            intersections = getLineSegmentIntersectionsCoordinates(
              canvasCoordinates,
              point,
              [canvasPosEnd[0], point[1]]
            );
            intersections.sort(
              (function (index) {
                return function (a, b) {
                  return a[index] === b[index]
                    ? 0
                    : a[index] < b[index]
                    ? -1
                    : 1;
                };
              })(0)
            );
          }
          if (intersections.length && point[0] > intersections[0][0]) {
            intersections.shift();
            intersectionCounter++;
          }
          if (intersectionCounter % 2 === 0) {
            result = false;
          }
          return result;
        },
        this.configuration.statsCalculator.statsCallback,
        boundsIJK
      );
 
      const modalityUnitOptions = {
        isPreScaled: isViewportPreScaled(viewport, targetId),
        isSuvScaled: this.isSuvScaled(
          viewport,
          targetId,
          annotation.metadata.referencedImageId
        ),
      };
 
      const modalityUnit = getModalityUnit(
        metadata.Modality,
        annotation.metadata.referencedImageId,
        modalityUnitOptions
      );
 
      const stats = this.configuration.statsCalculator.getStatistics();
 
      cachedStats[targetId] = {
        Modality: metadata.Modality,
        area,
        perimeter: calculatePerimeter(canvasCoordinates, closed),
        mean: stats.mean?.value,
        max: stats.max?.value,
        stdDev: stats.stdDev?.value,
        statsArray: stats.array,
        pointsInShape: pointsInShape,
        areaUnit: areaUnits,
        modalityUnit,
      };
    }
 
    triggerAnnotationModified(
      annotation,
      enabledElement.viewport.element,
      ChangeTypes.StatsUpdated
    );
 
    annotation.invalidated = false;
 
    return cachedStats;
  };
 
  private _renderStats = (
    annotation,
    viewport,
    enabledElement,
    svgDrawingHelper
  ) => {
    const { data } = <PlanarFreehandROIAnnotation>annotation;
    const targetId = this.getTargetId(viewport);
 
    const styleSpecifier: AnnotationStyle.StyleSpecifier = {
      toolGroupId: this.toolGroupId,
      toolName: this.getToolName(),
      viewportId: enabledElement.viewport.id,
    };
 
    const options = this.getLinkedTextBoxStyle(styleSpecifier, annotation);
    Iif (!options.visibility) {
      return;
    }
 
    const textLines = this.configuration.getTextLines(data, targetId);
    Eif (!textLines || textLines.length === 0) {
      return;
    }
 
    const canvasCoordinates = data.contour.polyline.map((p) =>
      viewport.worldToCanvas(p)
    );
    if (!data.handles.textBox.hasMoved) {
      const canvasTextBoxCoords = getTextBoxCoordsCanvas(canvasCoordinates);
 
      data.handles.textBox.worldPosition =
        viewport.canvasToWorld(canvasTextBoxCoords);
    }
 
    const textBoxPosition = viewport.worldToCanvas(
      data.handles.textBox.worldPosition
    );
 
    const textBoxUID = '1';
    const boundingBox = drawLinkedTextBox(
      svgDrawingHelper,
      annotation.annotationUID ?? '',
      textBoxUID,
      textLines,
      textBoxPosition,
      canvasCoordinates,
      {},
      options
    );
 
    const { x: left, y: top, width, height } = boundingBox;
 
    data.handles.textBox.worldBoundingBox = {
      topLeft: viewport.canvasToWorld([left, top]),
      topRight: viewport.canvasToWorld([left + width, top]),
      bottomLeft: viewport.canvasToWorld([left, top + height]),
      bottomRight: viewport.canvasToWorld([left + width, top + height]),
    };
  };
}
 
function defaultGetTextLines(data, targetId): string[] {
  const cachedVolumeStats = data.cachedStats[targetId];
  const {
    area,
    mean,
    stdDev,
    perimeter,
    max,
    isEmptyArea,
    areaUnit,
    modalityUnit,
  } = cachedVolumeStats || {};
 
  const textLines: string[] = [];
 
  Iif (area) {
    const areaLine = isEmptyArea
      ? `Area: Oblique not supported`
      : `Area: ${roundNumber(area)} ${areaUnit}`;
    textLines.push(areaLine);
  }
 
  Iif (mean) {
    textLines.push(`Mean: ${roundNumber(mean)} ${modalityUnit}`);
  }
 
  Iif (max) {
    textLines.push(`Max: ${roundNumber(max)} ${modalityUnit}`);
  }
 
  Iif (stdDev) {
    textLines.push(`Std Dev: ${roundNumber(stdDev)} ${modalityUnit}`);
  }
 
  Iif (perimeter) {
    textLines.push(`Perimeter: ${roundNumber(perimeter)} ${modalityUnit}`);
  }
 
  return textLines;
}
 
PlanarFreehandROITool.toolName = 'PlanarFreehandROI';
export default PlanarFreehandROITool;