All files / packages/tools/src/utilities/planarFreehandROITool smoothAnnotation.ts

0% Statements 0/23
0% Branches 0/19
0% Functions 0/2
0% Lines 0/23

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                                                                                                                                                                                   
import { Types } from '@cornerstonejs/core';
import { PlanarFreehandROITool } from '../../tools';
import { ToolGroupManager } from '../../store';
import { PlanarFreehandROIAnnotation } from '../../types/ToolSpecificAnnotationTypes';
import interpolateSegmentPoints from './interpolation/interpolateSegmentPoints';
 
function shouldPreventInterpolation(
  enabledElement: Types.IEnabledElement,
  annotation: PlanarFreehandROIAnnotation,
  knotsRatioPercentage: number
): boolean {
  if (!annotation?.data?.polyline || knotsRatioPercentage <= 0) {
    return true;
  }
 
  if (!enabledElement.viewport) {
    return true;
  }
 
  const { renderingEngineId, viewportId, FrameOfReferenceUID } = enabledElement;
  const toolGroup = ToolGroupManager.getToolGroupForViewport(
    viewportId,
    renderingEngineId
  );
 
  if (annotation.metadata.FrameOfReferenceUID !== FrameOfReferenceUID) {
    return true;
  }
 
  if (!toolGroup) {
    return true;
  }
 
  const toolInstance = toolGroup.getToolInstance(annotation.metadata.toolName);
 
  // strategy to prevent non PlanarFreehandTool
  if (!(toolInstance instanceof PlanarFreehandROITool)) {
    return true;
  }
 
  return (
    toolInstance.isDrawing ||
    toolInstance.isEditingOpen ||
    toolInstance.isEditingClosed
  );
}
/**
 * Interpolates a given annotation from a given enabledElement.
 * It mutates annotation param.
 * The param knotsRatioPercentage defines the percentage of points to be considered as knots on the interpolation process.
 * Interpolation will be skipped in case: annotation is not present in enabledElement (or there is no toolGroup associated with it), related tool is being modified.
 */
export default function smoothAnnotation(
  enabledElement: Types.IEnabledElement,
  annotation: PlanarFreehandROIAnnotation,
  knotsRatioPercentage: number
): boolean {
  // prevent running while there is any tool annotation being modified
  if (
    shouldPreventInterpolation(enabledElement, annotation, knotsRatioPercentage)
  ) {
    return false;
  }
 
  const { viewport } = enabledElement;
  // use only 2 dimensions on interpolation (what visually matters),
  // otherwise a 3d interpolation might have a totally different output as it consider one more dimension.
  const canvasPoints = annotation.data.contour.polyline.map(
    viewport.worldToCanvas
  );
  const interpolatedCanvasPoints = <Types.Point2[]>(
    interpolateSegmentPoints(
      canvasPoints,
      0,
      canvasPoints.length,
      knotsRatioPercentage
    )
  );
 
  if (interpolatedCanvasPoints === canvasPoints) {
    return false;
  }
 
  annotation.data.contour.polyline = interpolatedCanvasPoints.map(
    viewport.canvasToWorld
  );
 
  return true;
}