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

0% Statements 0/24
0% Branches 0/2
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                                                                                                                                                         
import type { Types } from '@cornerstonejs/core';
import { PlanarFreehandROIAnnotation } from '../../../types/ToolSpecificAnnotationTypes';
import { vec2 } from 'gl-matrix';
 
/**
 * Finds the length of the longest line from the midpoint of the line
 * that joins the start and end of the open contour, to the surface of the
 * open contour.
 */
export default function findOpenUShapedContourVectorToPeak(
  canvasPoints: Types.Point2[],
  viewport: Types.IStackViewport | Types.IVolumeViewport
): Types.Point3[] {
  // Find chord from first to last point.
  const first = canvasPoints[0];
  const last = canvasPoints[canvasPoints.length - 1];
 
  const firstToLastUnitVector = vec2.create();
 
  vec2.set(firstToLastUnitVector, last[0] - first[0], last[1] - first[1]);
  vec2.normalize(firstToLastUnitVector, firstToLastUnitVector);
 
  // Get the two possible normal vector to this vector
  // Note: Use the identity that the perpendicular line must have a gradient of
  // 1 / gradient of the line.
 
  const normalVector1 = vec2.create();
  const normalVector2 = vec2.create();
 
  vec2.set(normalVector1, -firstToLastUnitVector[1], firstToLastUnitVector[0]);
  vec2.set(normalVector2, firstToLastUnitVector[1], -firstToLastUnitVector[0]);
 
  // Find the center of the chord.
  const centerOfFirstToLast: Types.Point2 = [
    (first[0] + last[0]) / 2,
    (first[1] + last[1]) / 2,
  ];
 
  // Get furthest point.
 
  const furthest = {
    dist: 0,
    index: null,
  };
 
  for (let i = 0; i < canvasPoints.length; i++) {
    const canvasPoint = canvasPoints[i];
 
    const distance = vec2.dist(canvasPoint, <vec2>centerOfFirstToLast);
 
    if (distance > furthest.dist) {
      furthest.dist = distance;
      furthest.index = i;
    }
  }
 
  const toFurthest: [Types.Point2, Types.Point2] = [
    canvasPoints[furthest.index],
    centerOfFirstToLast,
  ];
  const toFurthestWorld = toFurthest.map(viewport.canvasToWorld);
 
  return toFurthestWorld;
}
 
export function findOpenUShapedContourVectorToPeakOnRender(
  enabledElement: Types.IEnabledElement,
  annotation: PlanarFreehandROIAnnotation
): Types.Point3[] {
  const { viewport } = enabledElement;
  const canvasPoints = annotation.data.contour.polyline.map(
    viewport.worldToCanvas
  );
 
  return findOpenUShapedContourVectorToPeak(canvasPoints, viewport);
}