All files / packages/tools/src/store filterMoveableAnnotationTools.ts

93.75% Statements 15/16
75% Branches 9/12
100% Functions 2/2
91.66% Lines 11/12

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                                                68x 68x               68x   68x 16x 16x       16x               16x 14x       14x         68x    
import type { Types } from '@cornerstonejs/core';
 
import {
  ToolAnnotationPair,
  ToolAnnotationsPair,
} from '../types/InternalToolTypes';
 
/**
 * Filters an array of tools with annotations, returning the first annotation
 * for each tool that is moveable and at the mouse location. It results in
 * one annotation per tool.
 *
 *
 * @param element - The HTML element
 * @param ToolAndAnnotations - The input tool array.
 * @param canvasCoords - The coordinates of the mouse position.
 * @param interactionType - The type of interaction that is taking place.
 * @returns The filtered array containing ToolAndAnnotation
 */
export default function filterMoveableAnnotationTools(
  element: HTMLDivElement,
  ToolAndAnnotations: ToolAnnotationsPair[],
  canvasCoords: Types.Point2,
  interactionType = 'mouse'
): ToolAnnotationPair[] {
  const proximity = interactionType === 'touch' ? 36 : 6;
 
  // TODO - This could get pretty expensive pretty quickly. We don't want to fetch the camera
  // And do world to canvas on each coord.
 
  // We want to produce a matrix from canvas to world for the viewport and just do a matrix operation on each handle.
  // This could still be expensive for ROIs, but we probably shouldn't have "handles" for them anyway.
 
  const moveableAnnotationTools = [];
 
  ToolAndAnnotations.forEach(({ tool, annotations }) => {
    for (const annotation of annotations) {
      Iif (annotation.isLocked || !annotation.isVisible) {
        continue;
      }
 
      const near = tool.isPointNearTool(
        element,
        annotation,
        canvasCoords,
        proximity,
        interactionType
      );
 
      if (near) {
        moveableAnnotationTools.push({
          tool,
          annotation,
        });
        break;
      }
    }
  });
 
  return moveableAnnotationTools;
}