All files / tools/src/utilities/contourSegmentation polylineUnify.ts

0% Statements 0/52
0% Branches 0/20
0% Functions 0/5
0% Lines 0/46

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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130                                                                                                                                                                                                                                                                   
import type { Types } from '@cornerstonejs/core';
import * as math from '../math';
import {
  checkIntersection,
  convertContourPolylineToCanvasSpace,
} from './sharedOperations';
import arePolylinesIdentical from '../math/polyline/arePolylinesIdentical';
import type { PolylineInfoCanvas } from './polylineInfoTypes';
import type { ContourSegmentationAnnotation } from '../../types';
import { getViewReferenceFromAnnotation } from './getViewReferenceFromAnnotation';
import { areViewReferencesEqual } from './areViewReferencesEqual';
 
/**
 * Unifies two sets of polylines by merging unique polylines from both sets.
 * If a polyline from set B is not present in set A (by polyline and viewReference),
 * it is added to the result. The result contains all unique polylines from both sets.
 *
 * @param polylinesSetA The first set of PolylineInfoCanvas
 * @param polylinesSetB The second set of PolylineInfoCanvas
 * @returns Array of unique PolylineInfoCanvas from both sets
 */
export function unifyPolylineSets(
  polylinesSetA: PolylineInfoCanvas[],
  polylinesSetB: PolylineInfoCanvas[]
): PolylineInfoCanvas[] {
  const result: PolylineInfoCanvas[] = [];
  const processedFromA = new Set<number>();
  const processedFromB = new Set<number>();
  for (let i = 0; i < polylinesSetA.length; i++) {
    if (processedFromA.has(i)) {
      continue;
    }
    const polylineA = polylinesSetA[i];
    let merged = false;
    for (let j = 0; j < polylinesSetB.length; j++) {
      if (processedFromB.has(j)) {
        continue;
      }
      const polylineB = polylinesSetB[j];
      if (
        !areViewReferencesEqual(
          polylineA.viewReference,
          polylineB.viewReference
        )
      ) {
        continue; // Skip if view references are not equal
      }
      if (arePolylinesIdentical(polylineA.polyline, polylineB.polyline)) {
        result.push(polylineA);
        processedFromA.add(i);
        processedFromB.add(j);
        merged = true;
        break;
      }
      const intersection = checkIntersection(
        polylineA.polyline,
        polylineB.polyline
      );
      if (intersection.hasIntersection && !intersection.isContourHole) {
        const mergedPolyline = math.polyline.mergePolylines(
          polylineA.polyline,
          polylineB.polyline
        );
        result.push({
          polyline: mergedPolyline,
          viewReference: polylineA.viewReference,
        });
        processedFromA.add(i);
        processedFromB.add(j);
        merged = true;
        break;
      }
    }
    if (!merged) {
      result.push(polylineA);
      processedFromA.add(i);
    }
  }
  for (let j = 0; j < polylinesSetB.length; j++) {
    if (!processedFromB.has(j)) {
      result.push(polylinesSetB[j]);
    }
  }
  return result;
}
 
/**
 * Unifies multiple sets of polylines by progressively merging them.
 */
export function unifyMultiplePolylineSets(
  polylineSets: PolylineInfoCanvas[][]
): PolylineInfoCanvas[] {
  if (polylineSets.length === 0) {
    return [];
  }
  if (polylineSets.length === 1) {
    return [...polylineSets[0]];
  }
  let result = [...polylineSets[0]];
  for (let i = 1; i < polylineSets.length; i++) {
    result = unifyPolylineSets(result, polylineSets[i]);
  }
  return result;
}
 
/**
 * Unifies polylines from annotations by extracting their polylines and merging intersecting ones.
 */
export function unifyAnnotationPolylines(
  annotationsSetA: ContourSegmentationAnnotation[],
  annotationsSetB: ContourSegmentationAnnotation[],
  viewport: Types.IViewport
): PolylineInfoCanvas[] {
  const polylinesSetA = annotationsSetA.map((annotation) => ({
    polyline: convertContourPolylineToCanvasSpace(
      annotation.data.contour.polyline,
      viewport
    ),
    viewReference: getViewReferenceFromAnnotation(annotation),
  }));
  const polylinesSetB = annotationsSetB.map((annotation) => ({
    polyline: convertContourPolylineToCanvasSpace(
      annotation.data.contour.polyline,
      viewport
    ),
    viewReference: getViewReferenceFromAnnotation(annotation),
  }));
  return unifyPolylineSets(polylinesSetA, polylinesSetB);
}