All files / packages/tools/src/utilities/math/polyline addCanvasPointsToArray.ts

4.16% Statements 1/24
0% Branches 0/2
0% Functions 0/1
4.54% Lines 1/22

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                1x                                                                                                            
import { getEnabledElement } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
import { vec2, vec3 } from 'gl-matrix';
import { PlanarFreehandROICommonData } from './planarFreehandROIInternalTypes';
 
/**
 * Adds one or more points to the array at a resolution defined by the underlying image.
 */
const addCanvasPointsToArray = (
  element: HTMLDivElement,
  canvasPoints: Types.Point2[],
  newCanvasPoint: Types.Point2,
  commonData: PlanarFreehandROICommonData
): number => {
  const { xDir, yDir, spacing } = commonData;
  const enabledElement = getEnabledElement(element);
  const { viewport } = enabledElement;
 
  const lastWorldPos = viewport.canvasToWorld(
    canvasPoints[canvasPoints.length - 1]
  );
  const newWorldPos = viewport.canvasToWorld(newCanvasPoint);
 
  const worldPosDiff = vec3.create();
 
  vec3.subtract(worldPosDiff, newWorldPos, lastWorldPos);
 
  const xDist = Math.abs(vec3.dot(worldPosDiff, xDir));
  const yDist = Math.abs(vec3.dot(worldPosDiff, yDir));
 
  const numPointsToAdd = Math.max(
    Math.floor(xDist / spacing[0]),
    Math.floor(yDist / spacing[0])
  );
 
  if (numPointsToAdd > 1) {
    const lastCanvasPoint = canvasPoints[canvasPoints.length - 1];
 
    const canvasDist = vec2.dist(lastCanvasPoint, newCanvasPoint);
 
    const canvasDir = vec2.create();
 
    vec2.subtract(canvasDir, newCanvasPoint, lastCanvasPoint);
 
    vec2.set(canvasDir, canvasDir[0] / canvasDist, canvasDir[1] / canvasDist);
 
    const distPerPoint = canvasDist / numPointsToAdd;
 
    for (let i = 1; i <= numPointsToAdd; i++) {
      canvasPoints.push([
        lastCanvasPoint[0] + distPerPoint * canvasDir[0] * i,
        lastCanvasPoint[1] + distPerPoint * canvasDir[1] * i,
      ]);
    }
  } else {
    canvasPoints.push(newCanvasPoint);
  }
 
  return numPointsToAdd;
};
 
export default addCanvasPointsToArray;