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

5.88% Statements 1/17
0% Branches 0/4
0% Functions 0/2
6.66% Lines 1/15

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      1x                                                                                                  
import { utilities } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
const epsilon = 1e-6;
 
/**
 * Projects a polyline from 3D to 2D by reducing one dimension.
 *
 * @param polyline - The polyline to be projected.
 * @returns An object containing the shared dimension index and the projected polyline in 2D.
 * @throws Error if a shared dimension index cannot be found for the polyline.
 */
export function projectTo2D(polyline: Types.Point3[]) {
  // We need to reduce one dimension to 2D, so basically
  // we need to find the dimension index that is shared by all points
  // Use the first three points, two is enough but three is more robust
  let sharedDimensionIndex;
 
  const testPoints = utilities.getRandomSampleFromArray(polyline, 50);
 
  for (let i = 0; i < 3; i++) {
    if (
      testPoints.every(
        (point, index, array) => Math.abs(point[i] - array[0][i]) < epsilon
      )
    ) {
      sharedDimensionIndex = i;
      break;
    }
  }
 
  if (sharedDimensionIndex === undefined) {
    throw new Error(
      'Cannot find a shared dimension index for polyline, probably oblique plane'
    );
  }
 
  // convert polyline list and point to 2D
  const points2D = [] as Types.Point2[];
 
  const firstDim = (sharedDimensionIndex + 1) % 3;
  const secondDim = (sharedDimensionIndex + 2) % 3;
 
  for (let i = 0; i < polyline.length; i++) {
    points2D.push([polyline[i][firstDim], polyline[i][secondDim]]);
  }
 
  return {
    sharedDimensionIndex,
    projectedPolyline: points2D,
  };
}