All files / tools/src/utilities/math/polyline arePolylinesIdentical.ts

0% Statements 0/40
0% Branches 0/20
0% Functions 0/1
0% Lines 0/35

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                                                                                                                                                                     
import type { Types } from '@cornerstonejs/core';
import { pointsAreEqual } from './robustSegmentIntersection';
 
/**
 * Checks if two polylines are identical (same points in same or reverse order)
 * This function handles various cases:
 * - Identical order (same points in same sequence)
 * - Reverse order (same shape, opposite winding direction)
 * - Cyclic shifts (same polyline starting from different vertex)
 *
 * @param poly1 - First polyline coordinates
 * @param poly2 - Second polyline coordinates
 * @returns true if polylines represent the same geometric shape
 */
export default function arePolylinesIdentical(
  poly1: Types.Point2[],
  poly2: Types.Point2[]
): boolean {
  if (poly1.length !== poly2.length) {
    return false;
  }
 
  const len = poly1.length;
  if (len === 0) {
    return true;
  }
 
  // Check for identical order
  let identicalForward = true;
  for (let i = 0; i < len; i++) {
    if (!pointsAreEqual(poly1[i], poly2[i])) {
      identicalForward = false;
      break;
    }
  }
 
  if (identicalForward) {
    return true;
  }
 
  // Check for reverse order (same shape, opposite winding)
  let identicalReverse = true;
  for (let i = 0; i < len; i++) {
    if (!pointsAreEqual(poly1[i], poly2[len - 1 - i])) {
      identicalReverse = false;
      break;
    }
  }
 
  if (identicalReverse) {
    return true;
  }
 
  // Check for cyclic shifts (same starting point at different indices)
  for (let offset = 1; offset < len; offset++) {
    // Forward direction with offset
    let cyclicForward = true;
    for (let i = 0; i < len; i++) {
      if (!pointsAreEqual(poly1[i], poly2[(i + offset) % len])) {
        cyclicForward = false;
        break;
      }
    }
    if (cyclicForward) {
      return true;
    }
 
    // Reverse direction with offset
    let cyclicReverse = true;
    for (let i = 0; i < len; i++) {
      if (!pointsAreEqual(poly1[i], poly2[(len - 1 - i + offset) % len])) {
        cyclicReverse = false;
        break;
      }
    }
    if (cyclicReverse) {
      return true;
    }
  }
 
  return false;
}