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

0% Statements 0/55
0% Branches 0/48
0% Functions 0/6
0% Lines 0/55

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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183                                                                                                                                                                                                                                                                                                                                                                             
import { vec2 } from 'gl-matrix';
import type { Types } from '@cornerstonejs/core';
 
/**
 * Orientation algoritm to determine if two lines cross.
 * Credit and details: geeksforgeeks.org/check-if-two-given-line-segments-intersect/
 */
 
/**
 * Checks whether the line (`p1`,`q1`) intersects any of the other lines in the
 * `points`, and returns the first value.
 */
function getFirstIntersectionWithPolyline(
  points: Types.Point2[],
  p1: Types.Point2,
  q1: Types.Point2,
  closed = true
): Types.Point2 | undefined {
  let initialI;
  let j;
 
  if (closed) {
    j = points.length - 1;
    initialI = 0;
  } else {
    j = 0;
    initialI = 1;
  }
 
  for (let i = initialI; i < points.length; i++) {
    const p2 = points[j];
    const q2 = points[i];
 
    if (doesIntersect(p1, q1, p2, q2)) {
      return [j, i];
    }
 
    j = i;
  }
}
 
/**
 * Checks whether the line (`p1`,`q1`) intersects any of the other lines in the
 * `points`, and returns the closest value.
 */
function getClosestIntersectionWithPolyline(
  points: Types.Point2[],
  p1: Types.Point2,
  q1: Types.Point2,
  closed = true
): { segment: Types.Point2; distance: number } | undefined {
  let initialI;
  let j;
 
  if (closed) {
    j = points.length - 1;
    initialI = 0;
  } else {
    j = 0;
    initialI = 1;
  }
 
  const intersections = [];
 
  for (let i = initialI; i < points.length; i++) {
    const p2 = points[j];
    const q2 = points[i];
 
    if (doesIntersect(p1, q1, p2, q2)) {
      intersections.push([j, i]);
    }
 
    j = i;
  }
 
  if (intersections.length === 0) {
    return;
  }
 
  // Find intersection closest to the start point
  const distances = [];
 
  intersections.forEach((intersection) => {
    const intersectionPoints = [
      points[intersection[0]],
      points[intersection[1]],
    ];
 
    const midpoint = [
      (intersectionPoints[0][0] + intersectionPoints[1][0]) / 2,
      (intersectionPoints[0][1] + intersectionPoints[1][1]) / 2,
    ];
 
    distances.push(vec2.distance(<vec2>midpoint, p1));
  });
 
  const minDistance = Math.min(...distances);
  const indexOfMinDistance = distances.indexOf(minDistance);
 
  return {
    segment: intersections[indexOfMinDistance],
    distance: minDistance,
  };
}
 
/**
 * Checks whether the line (`p1`,`q1`) intersects the line (`p2`,`q2`) via an orientation algorithm.
 */
function doesIntersect(
  p1: Types.Point2,
  q1: Types.Point2,
  p2: Types.Point2,
  q2: Types.Point2
): boolean {
  let result = false;
 
  const orient = [
    orientation(p1, q1, p2),
    orientation(p1, q1, q2),
    orientation(p2, q2, p1),
    orientation(p2, q2, q1),
  ];
 
  // General Case
  if (orient[0] !== orient[1] && orient[2] !== orient[3]) {
    return true;
  }
 
  // Special Cases
  if (orient[0] === 0 && onSegment(p1, p2, q1)) {
    // If p1, q1 and p2 are colinear and p2 lies on segment p1q1
    result = true;
  } else if (orient[1] === 0 && onSegment(p1, q2, q1)) {
    // If p1, q1 and p2 are colinear and q2 lies on segment p1q1
    result = true;
  } else if (orient[2] === 0 && onSegment(p2, p1, q2)) {
    // If p2, q2 and p1 are colinear and p1 lies on segment p2q2
    result = true;
  } else if (orient[3] === 0 && onSegment(p2, q1, q2)) {
    // If p2, q2 and q1 are colinear and q1 lies on segment p2q2
    result = true;
  }
 
  return result;
}
 
/**
 * Checks the orientation of 3 points, returns a 0, 1 or 2 based on
 * the orientation of the points.
 */
function orientation(
  p: Types.Point2,
  q: Types.Point2,
  r: Types.Point2
): number {
  const orientationValue =
    (q[1] - p[1]) * (r[0] - q[0]) - (q[0] - p[0]) * (r[1] - q[1]);
 
  if (orientationValue === 0) {
    return 0; // Colinear
  }
 
  return orientationValue > 0 ? 1 : 2;
}
 
/**
 * Checks if point `q` lies on the segment (`p`,`r`).
 */
function onSegment(p: Types.Point2, q: Types.Point2, r: Types.Point2): boolean {
  if (
    q[0] <= Math.max(p[0], r[0]) &&
    q[0] >= Math.min(p[0], r[0]) &&
    q[1] <= Math.max(p[1], r[1]) &&
    q[1] >= Math.min(p[1], r[1])
  ) {
    return true;
  }
 
  return false;
}
 
export { getFirstIntersectionWithPolyline, getClosestIntersectionWithPolyline };