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 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 | 428x | import type { Types } from '@cornerstonejs/core'; import { utilities as csUtils, getEnabledElement } from '@cornerstonejs/core'; import type { ContourSegmentationAnnotation } from '../../types/ContourSegmentationAnnotation'; import { ContourWindingDirection } from '../../types/ContourAnnotation'; import * as math from '../math'; import updateContourPolyline from '../contours/updateContourPolyline'; import { addAnnotation, removeAnnotation, getChildAnnotations, addChildAnnotation, clearParentAnnotation, } from '../../stateManagement/annotation/annotationState'; import { addContourSegmentationAnnotation } from './addContourSegmentationAnnotation'; import { removeContourSegmentationAnnotation } from './removeContourSegmentationAnnotation'; import { triggerAnnotationModified } from '../../stateManagement/annotation/helpers/state'; import triggerAnnotationRenderForViewportIds from '../triggerAnnotationRenderForViewportIds'; import { getViewportIdsWithToolToRender } from '../viewportFilters'; import { hasToolByName, hasTool } from '../../store/addTool'; /** * Default tool name for contour segmentation operations. */ const DEFAULT_CONTOUR_SEG_TOOL_NAME = 'PlanarFreehandContourSegmentationTool'; /** * Processes multiple intersecting annotations with a source annotation. * This function handles the complex case where a source annotation intersects * with multiple target annotations, requiring multiple operations. * * @param viewport - The viewport context. * @param sourceAnnotation - The newly completed contour segmentation. * @param sourcePolyline - The polyline of the source annotation in canvas space. * @param intersectingContours - Array of intersecting contour information. */ function processMultipleIntersections( viewport: Types.IViewport, sourceAnnotation: ContourSegmentationAnnotation, sourcePolyline: Types.Point2[], intersectingContours: Array<{ targetAnnotation: ContourSegmentationAnnotation; targetPolyline: Types.Point2[]; isContourHole: boolean; }> ): void { // Separate hole operations from merge/subtract operations const holeOperations = intersectingContours.filter( (item) => item.isContourHole ); const mergeOperations = intersectingContours.filter( (item) => !item.isContourHole ); // Handle hole operations first (source becomes a hole in target annotations) if (holeOperations.length > 0) { // For hole operations, we only use the first target (largest containing contour) const primaryHoleTarget = holeOperations[0]; createPolylineHole( viewport, primaryHoleTarget.targetAnnotation, sourceAnnotation ); updateViewportsForAnnotations(viewport, [ sourceAnnotation, primaryHoleTarget.targetAnnotation, ]); return; } // Handle merge/subtract operations if (mergeOperations.length === 0) { return; } // Check if the necessary tool for creating new combined contours is registered. if (!hasToolByName(DEFAULT_CONTOUR_SEG_TOOL_NAME)) { console.warn( `${DEFAULT_CONTOUR_SEG_TOOL_NAME} is not registered in cornerstone. Cannot process multiple intersections.` ); return; } // Process each intersection sequentially processSequentialIntersections( viewport, sourceAnnotation, sourcePolyline, mergeOperations ); } /** * Processes intersections sequentially, combining or subtracting one at a time. */ function processSequentialIntersections( viewport: Types.IViewport, sourceAnnotation: ContourSegmentationAnnotation, sourcePolyline: Types.Point2[], mergeOperations: Array<{ targetAnnotation: ContourSegmentationAnnotation; targetPolyline: Types.Point2[]; isContourHole: boolean; }> ): void { const { element } = viewport; const allAnnotationsToRemove = [sourceAnnotation]; const allResultPolylines: Types.Point2[][] = []; const allHoles: Array<{ annotation: ContourSegmentationAnnotation; polyline: Types.Point2[]; }> = []; // Collect all holes from target annotations mergeOperations.forEach(({ targetAnnotation }) => { const holes = getContourHolesData(viewport, targetAnnotation); allHoles.push(...holes); allAnnotationsToRemove.push(targetAnnotation); }); // Determine operation type based on whether source start point is inside any target polyline const sourceStartPoint = sourcePolyline[0]; const shouldMerge = mergeOperations.some(({ targetPolyline }) => math.polyline.containsPoint(targetPolyline, sourceStartPoint) ); if (shouldMerge) { // Merge all polylines together let resultPolyline = sourcePolyline; mergeOperations.forEach(({ targetPolyline }) => { resultPolyline = math.polyline.mergePolylines( resultPolyline, targetPolyline ); }); allResultPolylines.push(resultPolyline); } else { // Subtract source from each target mergeOperations.forEach(({ targetPolyline }) => { const subtractedPolylines = math.polyline.subtractPolylines( targetPolyline, sourcePolyline ); allResultPolylines.push(...subtractedPolylines); }); } // Remove all original annotations allAnnotationsToRemove.forEach((annotation) => { removeAnnotation(annotation.annotationUID); removeContourSegmentationAnnotation(annotation); }); // Detach holes from old annotations allHoles.forEach((holeData) => clearParentAnnotation(holeData.annotation)); // Create new annotations from result polylines const baseAnnotation = mergeOperations[0].targetAnnotation; const newAnnotations: ContourSegmentationAnnotation[] = []; allResultPolylines.forEach((polyline) => { if (!polyline || polyline.length < 3) { console.warn( 'Skipping creation of new annotation due to invalid polyline:', polyline ); return; } const newAnnotation = createNewAnnotationFromPolyline( viewport, baseAnnotation, polyline ); addAnnotation(newAnnotation, element); addContourSegmentationAnnotation(newAnnotation); triggerAnnotationModified(newAnnotation, viewport.element); newAnnotations.push(newAnnotation); }); // Reassign holes to new annotations reassignHolesToNewAnnotations(viewport, allHoles, newAnnotations); updateViewportsForAnnotations(viewport, allAnnotationsToRemove); } /** * Creates a new annotation from a polyline, copying metadata from a base annotation. */ function createNewAnnotationFromPolyline( viewport: Types.IViewport, baseAnnotation: ContourSegmentationAnnotation, polyline: Types.Point2[] ): ContourSegmentationAnnotation { const startPointWorld = viewport.canvasToWorld(polyline[0]); const endPointWorld = viewport.canvasToWorld(polyline[polyline.length - 1]); const newAnnotation: ContourSegmentationAnnotation = { metadata: { ...baseAnnotation.metadata, toolName: DEFAULT_CONTOUR_SEG_TOOL_NAME, originalToolName: baseAnnotation.metadata.originalToolName || baseAnnotation.metadata.toolName, }, data: { cachedStats: {}, handles: { points: [startPointWorld, endPointWorld], textBox: baseAnnotation.data.handles.textBox ? { ...baseAnnotation.data.handles.textBox } : undefined, }, contour: { polyline: [], closed: true, }, spline: baseAnnotation.data.spline, segmentation: { ...baseAnnotation.data.segmentation, }, }, annotationUID: csUtils.uuidv4() as string, highlighted: true, invalidated: true, isLocked: false, isVisible: undefined, interpolationUID: baseAnnotation.interpolationUID, interpolationCompleted: baseAnnotation.interpolationCompleted, }; updateContourPolyline( newAnnotation, { points: polyline, closed: true, targetWindingDirection: ContourWindingDirection.Clockwise, }, viewport ); return newAnnotation; } /** * Reassigns holes to new annotations based on containment. */ function reassignHolesToNewAnnotations( viewport: Types.IViewport, holes: Array<{ annotation: ContourSegmentationAnnotation; polyline: Types.Point2[]; }>, newAnnotations: ContourSegmentationAnnotation[] ): void { holes.forEach((holeData) => { // Find which new annotation should contain this hole const parentAnnotation = newAnnotations.find((annotation) => { const parentPolyline = convertContourPolylineToCanvasSpace( annotation.data.contour.polyline, viewport ); return math.polyline.containsPoints(parentPolyline, holeData.polyline); }); if (parentAnnotation) { addChildAnnotation(parentAnnotation, holeData.annotation); } }); } /** * Helper function to get hole data from an annotation. */ function getContourHolesData( viewport: Types.IViewport, annotation: ContourSegmentationAnnotation ): Array<{ annotation: ContourSegmentationAnnotation; polyline: Types.Point2[]; }> { return getChildAnnotations(annotation).map((holeAnnotation) => { const contourHoleAnnotation = holeAnnotation as ContourSegmentationAnnotation; const polyline = convertContourPolylineToCanvasSpace( contourHoleAnnotation.data.contour.polyline, viewport ); return { annotation: contourHoleAnnotation, polyline }; }); } /** * Helper function to create a hole in a target annotation. */ function createPolylineHole( viewport: Types.IViewport, targetAnnotation: ContourSegmentationAnnotation, holeAnnotation: ContourSegmentationAnnotation ): void { addChildAnnotation(targetAnnotation, holeAnnotation); removeContourSegmentationAnnotation(holeAnnotation); const { contour: holeContour } = holeAnnotation.data; const holePolylineCanvas = convertContourPolylineToCanvasSpace( holeContour.polyline, viewport ); updateContourPolyline( holeAnnotation, { points: holePolylineCanvas, closed: holeContour.closed, targetWindingDirection: targetAnnotation.data.contour.windingDirection === ContourWindingDirection.Clockwise ? ContourWindingDirection.CounterClockwise : ContourWindingDirection.Clockwise, }, viewport ); } /** * Converts a 3D polyline to 2D canvas space. */ function convertContourPolylineToCanvasSpace( polyline: Types.Point3[], viewport: Types.IViewport ): Types.Point2[] { const numPoints = polyline.length; const projectedPolyline = new Array(numPoints); for (let i = 0; i < numPoints; i++) { projectedPolyline[i] = viewport.worldToCanvas(polyline[i]); } return projectedPolyline; } /** * Updates viewports for multiple annotations. */ function updateViewportsForAnnotations( viewport: Types.IViewport, annotations: ContourSegmentationAnnotation[] ): void { const { element } = viewport; const updatedToolNames = new Set([DEFAULT_CONTOUR_SEG_TOOL_NAME]); annotations.forEach((annotation) => { updatedToolNames.add(annotation.metadata.toolName); }); for (const toolName of updatedToolNames.values()) { if (hasToolByName(toolName)) { const viewportIdsToRender = getViewportIdsWithToolToRender( element, toolName ); triggerAnnotationRenderForViewportIds(viewportIdsToRender); } } } export { processMultipleIntersections }; |