All files / packages/tools/src/utilities getAnnotationNearPoint.ts

0% Statements 0/26
0% Branches 0/19
0% Functions 0/3
0% Lines 0/26

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                                                                                                                                                                                                                                                                     
import { getEnabledElement } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
import { AnnotationTool, BaseTool } from '../tools';
import { Annotation } from '../types';
import { getAnnotations } from '../stateManagement/annotation/annotationState';
import * as ToolGroupManager from '../store/ToolGroupManager';
 
/**
 * Get the annotation that is close to the provided canvas point, it will return
 * the first annotation that is found.
 *
 * @param element - The element to search for an annotation on.
 * @param canvasPoint - The canvasPoint on the page where the user clicked.
 * @param proximity - The distance from the canvasPoint to the annotation.
 * @returns The annotation for the element
 */
function getAnnotationNearPoint(
  element: HTMLDivElement,
  canvasPoint: Types.Point2,
  proximity = 5
): Annotation | null {
  // Todo: this function should return closest annotation, BUT, we are not using
  // the function anywhere.
  const enabledElement = getEnabledElement(element);
  if (!enabledElement) {
    throw new Error('getAnnotationNearPoint: enabledElement not found');
  }
 
  return getAnnotationNearPointOnEnabledElement(
    enabledElement,
    canvasPoint,
    proximity
  );
}
 
/**
 * "Find the annotation near the point on the enabled element." it will return the
 * first annotation that is found.
 *
 * @param enabledElement - The element that is currently active.
 * @param point - The point to search near.
 * @param proximity - The distance from the point that the annotation must
 * be within.
 * @returns A Annotation object.
 */
function getAnnotationNearPointOnEnabledElement(
  enabledElement: Types.IEnabledElement,
  point: Types.Point2,
  proximity: number
): Annotation | null {
  // Todo: this function should return closest annotation, BUT, we are not using
  // the function anywhere.
  const { renderingEngineId, viewportId } = enabledElement;
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  if (!toolGroup) {
    return null;
  }
 
  const { _toolInstances: tools } = toolGroup;
  for (const name in tools) {
    const found = findAnnotationNearPointByTool(
      tools[name],
      enabledElement,
      point,
      proximity
    );
    if (found) {
      return found;
    }
  }
 
  return null;
}
 
/**
 * For the provided toolClass, it will find the annotation that is near the point,
 * it will return the first annotation that is found.
 *
 * @param tool - AnnotationTool
 * @param enabledElement - The element that is currently active.
 * @param point - The point in the image where the user clicked.
 * @param proximity - The distance from the point that the tool must be
 * within to be considered "near" the point.
 * @returns The annotation object that is being returned is the annotation object that
 * is being used in the tool.
 */
function findAnnotationNearPointByTool(
  tool: AnnotationTool,
  enabledElement: Types.IEnabledElement,
  point: Types.Point2,
  proximity: number
): Annotation | null {
  // Todo: this function does not return closest annotation. It just returns
  // the first annotation that is found in the proximity. BUT, we are not using
  // the function anywhere.
  const { viewport } = enabledElement;
 
  const annotations = getAnnotations(
    (tool.constructor as typeof BaseTool).toolName,
    viewport?.element
  );
  const currentId = viewport?.getCurrentImageId?.();
  if (annotations?.length) {
    const { element } = enabledElement.viewport;
    for (const annotation of annotations) {
      const referencedImageId = annotation.metadata?.referencedImageId;
      if (
        (currentId && referencedImageId && currentId !== referencedImageId) ||
        !tool.isPointNearTool
      ) {
        continue;
      }
 
      if (
        tool.isPointNearTool(element, annotation, point, proximity, '') ||
        tool.getHandleNearImagePoint(element, annotation, point, proximity)
      ) {
        return annotation;
      }
    }
  }
  return null;
}
 
export { getAnnotationNearPoint, getAnnotationNearPointOnEnabledElement };