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 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | 1x 6x 6x 6x 6x 6x 5x 1x 1x 1x 1x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 2x 2x 2x 10x 2x 6x 6x 6x 9x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { getEnabledElement, utilities as csUtils, Types, } from '@cornerstonejs/core'; import { ContourSegmentationAnnotation } from '../../../types/ContourSegmentationAnnotation'; import getViewportsForAnnotation from '../../../utilities/getViewportsForAnnotation'; import { math, triggerAnnotationRenderForViewportIds, } from '../../../utilities'; import { getViewportIdsWithToolToRender } from '../../../utilities/viewportFilters'; import { addAnnotation, removeAnnotation, getAllAnnotations, getChildAnnotations, addChildAnnotation, clearParentAnnotation, } from '../../../stateManagement/annotation/annotationState'; import { AnnotationCompletedEventType, ContourAnnotationCompletedEventDetail, } from '../../../types/EventTypes'; import * as contourUtils from '../../../utilities/contours'; import * as contourSegUtils from '../../../utilities/contourSegmentation'; import { ToolGroupManager, hasTool as cstHasTool } from '../../../store'; import { PlanarFreehandContourSegmentationTool } from '../../../tools'; import type { Annotation } from '../../../types'; import type { ContourAnnotation } from '../../../types/ContourAnnotation'; import { ContourWindingDirection } from '../../../types/ContourAnnotation'; import { triggerAnnotationModified } from '../../../stateManagement/annotation/helpers/state'; const DEFAULT_CONTOUR_SEG_TOOLNAME = 'PlanarFreehandContourSegmentationTool'; export default async function contourSegmentationCompletedListener( evt: AnnotationCompletedEventType ) { const sourceAnnotation = evt.detail .annotation as ContourSegmentationAnnotation; Iif (!contourSegUtils.isContourSegmentationAnnotation(sourceAnnotation)) { return; } const viewport = getViewport(sourceAnnotation); const contourSegmentationAnnotations = getValidContourSegmentationAnnotations( viewport, sourceAnnotation ); if (!contourSegmentationAnnotations.length) { return; } const sourcePolyline = convertContourPolylineToCanvasSpace( sourceAnnotation.data.contour.polyline, viewport ); const targetAnnotationInfo = findIntersectingContour( viewport, sourcePolyline, contourSegmentationAnnotations ); Eif (!targetAnnotationInfo) { return; } const { targetAnnotation, targetPolyline, isContourHole } = targetAnnotationInfo; if (isContourHole) { const { contourHoleProcessingEnabled = false } = evt.detail as ContourAnnotationCompletedEventDetail; // Do not create holes when contourHoleProcessingEnabled is `false` if (!contourHoleProcessingEnabled) { return; } createPolylineHole(viewport, targetAnnotation, sourceAnnotation); } else { combinePolylines( viewport, targetAnnotation, targetPolyline, sourceAnnotation, sourcePolyline ); } } function isFreehandContourSegToolRegisteredForViewport( viewport: Types.IViewport, silent = false ) { const { toolName } = PlanarFreehandContourSegmentationTool; const toolGroup = ToolGroupManager.getToolGroupForViewport( viewport.id, viewport.renderingEngineId ); let errorMessage; Iif (!toolGroup.hasTool(toolName)) { errorMessage = `Tool ${toolName} not added to ${toolGroup.id} toolGroup`; } else Iif (!toolGroup.getToolOptions(toolName)) { errorMessage = `Tool ${toolName} must be in active/passive state`; } Iif (errorMessage && !silent) { console.warn(errorMessage); } return !errorMessage; } function getViewport(annotation: Annotation) { const viewports = getViewportsForAnnotation(annotation); const viewportWithToolRegistered = viewports.find((viewport) => isFreehandContourSegToolRegisteredForViewport(viewport, true) ); // Returns the first viewport even if freehand contour segmentation is not // registered because it can be used to project the polyline to create holes. // Another verification is done before appending/removing contours which is // possible only when the tool is registered. return viewportWithToolRegistered ?? viewports[0]; } 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; } function getValidContourSegmentationAnnotations( viewport: Types.IViewport, sourceAnnotation: ContourSegmentationAnnotation ): ContourSegmentationAnnotation[] { const { annotationUID: sourceAnnotationUID } = sourceAnnotation; // Get all annotations and filter all contour segmentations locally const allAnnotations = getAllAnnotations(); return allAnnotations.filter( (targetAnnotation) => targetAnnotation.annotationUID && targetAnnotation.annotationUID !== sourceAnnotationUID && contourSegUtils.isContourSegmentationAnnotation(targetAnnotation) && contourSegUtils.areSameSegment(targetAnnotation, sourceAnnotation) && viewport.isReferenceViewable(targetAnnotation.metadata) ) as ContourSegmentationAnnotation[]; } /** * Finds other contours on the same slice which intersect the source polyline, * represented as canvas points. */ function findIntersectingContour( viewport: Types.IViewport, sourcePolyline: Types.Point2[], contourSegmentationAnnotations: ContourSegmentationAnnotation[] ): { targetAnnotation: ContourSegmentationAnnotation; targetPolyline: Types.Point2[]; isContourHole: boolean; } { const sourceAABB = math.polyline.getAABB(sourcePolyline); for (let i = 0; i < contourSegmentationAnnotations.length; i++) { const targetAnnotation = contourSegmentationAnnotations[i]; const targetPolyline = convertContourPolylineToCanvasSpace( targetAnnotation.data.contour.polyline, viewport ); const targetAABB = math.polyline.getAABB(targetPolyline); const aabbIntersect = math.aabb.intersectAABB(sourceAABB, targetAABB); const lineSegmentsIntersect = aabbIntersect && math.polyline.intersectPolyline(sourcePolyline, targetPolyline); const isContourHole = aabbIntersect && !lineSegmentsIntersect && math.polyline.containsPoints(targetPolyline, sourcePolyline); Iif (lineSegmentsIntersect || isContourHole) { return { targetAnnotation, targetPolyline, isContourHole }; } } } /** * Modifies the holeAnnotation to work as a contour hole in the targetAnnotation, * displayed on the given viewport. */ export function createPolylineHole( viewport: Types.IViewport, targetAnnotation: ContourSegmentationAnnotation, holeAnnotation: ContourSegmentationAnnotation ) { const { windingDirection: targetWindingDirection } = targetAnnotation.data.contour; const { windingDirection: holeWindingDirection } = holeAnnotation.data.contour; addChildAnnotation(targetAnnotation, holeAnnotation); contourSegUtils.removeContourSegmentationAnnotation(holeAnnotation); const { contour: holeContour } = holeAnnotation.data; const holePolyline = convertContourPolylineToCanvasSpace( holeContour.polyline, viewport ); // Calling `updateContourPolyline` method instead of reversing the polyline // locally because it is also responsible for checking/fixing the winding direction. contourUtils.updateContourPolyline( holeAnnotation, { points: holePolyline, closed: holeContour.closed, }, viewport ); const { element } = viewport; const enabledElement = getEnabledElement(element); const { renderingEngine } = enabledElement; // Updating a Spline contours, for example, should also update freehand contours const updatedToolNames = new Set([ DEFAULT_CONTOUR_SEG_TOOLNAME, targetAnnotation.metadata.toolName, holeAnnotation.metadata.toolName, ]); for (const toolName of updatedToolNames.values()) { const viewportIdsToRender = getViewportIdsWithToolToRender( element, toolName ); triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender); } } function getContourHolesData( viewport: Types.IViewport, annotation: ContourAnnotation ) { return getChildAnnotations(annotation).map((holeAnnotation) => { const polyline = convertContourPolylineToCanvasSpace( holeAnnotation.data.contour.polyline, viewport ); return { annotation: holeAnnotation, polyline }; }); } function combinePolylines( viewport: Types.IViewport, targetAnnotation: ContourSegmentationAnnotation, targetPolyline: Types.Point2[], sourceAnnotation: ContourSegmentationAnnotation, sourcePolyline: Types.Point2[] ) { if (!cstHasTool(PlanarFreehandContourSegmentationTool)) { console.warn( `${PlanarFreehandContourSegmentationTool.toolName} is not registered in cornerstone` ); return; } // Cannot append/remove an annotation if it will not be available on any viewport if (!isFreehandContourSegToolRegisteredForViewport(viewport)) { return; } const sourceStartPoint = sourcePolyline[0]; const mergePolylines = math.polyline.containsPoint( targetPolyline, sourceStartPoint ); const contourHolesData = getContourHolesData(viewport, targetAnnotation); const unassignedContourHolesSet = new Set(contourHolesData); const reassignedContourHolesMap = new Map(); const assignHoleToPolyline = (parentPolyline, holeData) => { let holes = reassignedContourHolesMap.get(parentPolyline); if (!holes) { holes = []; reassignedContourHolesMap.set(parentPolyline, holes); } holes.push(holeData); unassignedContourHolesSet.delete(holeData); }; const newPolylines = []; if (mergePolylines) { const mergedPolyline = math.polyline.mergePolylines( targetPolyline, sourcePolyline ); newPolylines.push(mergedPolyline); // Keep all holes because the contour can only grow when merging and there // is no chance for any hole to be removed Array.from(unassignedContourHolesSet.keys()).forEach((holeData) => assignHoleToPolyline(mergedPolyline, holeData) ); } else { const subtractedPolylines = math.polyline.subtractPolylines( targetPolyline, sourcePolyline ); subtractedPolylines.forEach((newPolyline) => { newPolylines.push(newPolyline); Array.from(unassignedContourHolesSet.keys()).forEach((holeData) => { const containsHole = math.polyline.containsPoints( newPolyline, holeData.polyline ); if (containsHole) { assignHoleToPolyline(newPolyline, holeData); unassignedContourHolesSet.delete(holeData); } }); }); } // Make sure the holes that will be added to the new annotation are not // associated to the target annotation that will be deleted Array.from(reassignedContourHolesMap.values()).forEach( (contourHolesDataArray) => contourHolesDataArray.forEach((contourHoleData) => clearParentAnnotation(contourHoleData.annotation) ) ); const { element } = viewport; const enabledElement = getEnabledElement(element); const { metadata, data } = targetAnnotation; const { handles, segmentation } = data; const { textBox } = handles; removeAnnotation(sourceAnnotation.annotationUID); removeAnnotation(targetAnnotation.annotationUID); for (let i = 0; i < newPolylines.length; i++) { const polyline = newPolylines[i]; const startPoint = viewport.canvasToWorld(polyline[0]); const endPoint = viewport.canvasToWorld(polyline[polyline.length - 1]); const newAnnotation: ContourSegmentationAnnotation = { metadata: { ...metadata, toolName: DEFAULT_CONTOUR_SEG_TOOLNAME, originalToolName: metadata.originalToolName || metadata.toolName, }, data: { cachedStats: {}, handles: { points: [startPoint, endPoint], textBox: textBox ? { ...textBox } : undefined, }, contour: { polyline: [], closed: true, }, spline: targetAnnotation.data.spline, segmentation: { ...segmentation, }, }, annotationUID: csUtils.uuidv4() as string, highlighted: true, invalidated: true, isLocked: false, isVisible: undefined, // Allow this object to be interpolated against the original interpolation // data. interpolationUID: targetAnnotation.interpolationUID, interpolationCompleted: targetAnnotation.interpolationCompleted, }; // Calling `updateContourPolyline` method instead of setting it locally // because it is also responsible for checking/fixing the winding direction. contourUtils.updateContourPolyline( newAnnotation, { points: polyline, closed: true, targetWindingDirection: ContourWindingDirection.Clockwise, }, viewport ); addAnnotation(newAnnotation, element); contourSegUtils.addContourSegmentationAnnotation(newAnnotation); triggerAnnotationModified(newAnnotation, viewport.element); reassignedContourHolesMap .get(polyline) ?.forEach((holeData) => addChildAnnotation(newAnnotation, holeData.annotation) ); } updateViewports(enabledElement, targetAnnotation, sourceAnnotation); } function updateViewports(enabledElement, targetAnnotation, sourceAnnotation) { const { viewport } = enabledElement; const { element } = viewport; const { renderingEngine } = enabledElement; const updatedTtoolNames = new Set([ DEFAULT_CONTOUR_SEG_TOOLNAME, targetAnnotation.metadata.toolName, sourceAnnotation.metadata.toolName, ]); for (const toolName of updatedTtoolNames.values()) { const viewportIdsToRender = getViewportIdsWithToolToRender( element, toolName ); triggerAnnotationRenderForViewportIds(renderingEngine, viewportIdsToRender); } return new Promise((resolve) => window.requestAnimationFrame(resolve)); } |