All files / tools/src/utilities/contours findIslands.ts

0% Statements 0/16
0% Branches 0/14
0% Functions 0/1
0% Lines 0/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                                                                                                         
import type { Types } from '@cornerstonejs/core';
import isClosed from '../math/polyline/isClosed';
import { getSignedArea } from '../math/polyline';
 
/**
 * Finds closed contours (islands) that are smaller than the specified threshold.
 * This function identifies small closed polylines based on their area.
 *
 * @param polylines - Array of polylines to analyze
 * @param threshold - Minimum area threshold for identifying islands in cm2
 * @returns Array of polyline indexes that are islands (closed contours smaller than threshold)
 */
export default function findIslands(
  polylines: Types.Point2[][],
  threshold: number
): number[] {
  if (!polylines || polylines.length === 0) {
    return [];
  }
 
  if (threshold <= 0) {
    return [];
  }
 
  const islandIndexes: number[] = [];
 
  for (let i = 0; i < polylines.length; i++) {
    const polyline = polylines[i];
 
    // Skip empty or invalid polylines
    if (!polyline || polyline.length < 3) {
      continue;
    }
 
    // Check if the polyline is closed
    const isClosedPolyline = isClosed(polyline);
 
    if (isClosedPolyline) {
      // Calculate area for closed polylines (use absolute value since we only care about size)
      // Convert from mm² to cm² by dividing by 100 (1 cm² = 100 mm²)
      const area = Math.abs(getSignedArea(polyline)) / 100;
 
      // Identify closed contours that are smaller than the threshold (islands)
      if (area < threshold) {
        islandIndexes.push(i);
      }
    }
    // Open polylines are not considered islands, so we skip them
  }
 
  return islandIndexes;
}