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 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 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/ */ function getAllIntersectionsWithPolyline( points: Types.Point2[], p1: Types.Point2, q1: Types.Point2, closed = true ): Types.Point2[] { let initialI; let j; const intersections: Types.Point2[] = []; 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)) { intersections.push([j, i]); } j = i; } return intersections; } /** * Returns all intersections points * between a line and a polyline */ function getIntersectionCoordinatesWithPolyline( points: Types.Point2[], p1: Types.Point2, q1: Types.Point2, closed = true ): Types.Point2[] { const result = []; const polylineIndexes = getAllIntersectionsWithPolyline( points, p1, q1, closed ); for (let i = 0; i < polylineIndexes.length; i++) { const p2 = points[polylineIndexes[i][0]]; const q2 = points[polylineIndexes[i][1]]; const intersection = getIntersection(p1, q1, p2, q2); result.push(intersection); } return result; } /** * 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; } /** * Gets the intersection between the line (`p1`,`q1`) and the line (`p2`,`q2`) * http://jsfiddle.net/justin_c_rounds/Gd2S2/light/ * https://en.wikipedia.org/wiki/Line%E2%80%93line_intersection#Given_two_points_on_each_line */ function getIntersection( p1: Types.Point2, q1: Types.Point2, p2: Types.Point2, q2: Types.Point2 ): Types.Point2 { const denominator = (q2[1] - p2[1]) * (q1[0] - p1[0]) - (q2[0] - p2[0]) * (q1[1] - p1[1]); if (denominator == 0) { return; } let a = p1[1] - p2[1]; let b = p1[0] - p2[0]; const numerator1 = (q2[0] - p2[0]) * a - (q2[1] - p2[1]) * b; const numerator2 = (q1[0] - p1[0]) * a - (q1[1] - p1[1]) * b; a = numerator1 / denominator; b = numerator2 / denominator; const resultX = p1[0] + a * (q1[0] - p1[0]); const resultY = p1[1] + a * (q1[1] - p1[1]); return [resultX, resultY]; } export { getAllIntersectionsWithPolyline, getFirstIntersectionWithPolyline, getClosestIntersectionWithPolyline, getIntersectionCoordinatesWithPolyline, }; |