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 | /** * Logical and set operations for polylines in Cornerstone3D's contour segmentation utilities. * * This module provides functions to perform union, subtraction, intersection, and XOR operations * on sets of polylines, where each polyline is associated with a view reference (PolylineInfoCanvas). * The logical operator pipeline is designed to propagate view references through all operations, * ensuring that annotations are placed in the correct view. UI responsiveness and controls are also * managed based on the selected operation. * * Key Types: * - SegmentInfo: Information about a segment (segmentationId, segmentIndex, label, color) * - OperatorOptions: Alias for SegmentInfo, used for operation options * - LogicalOperation: Enum for supported logical operations * - PolylineInfoWorld: { polyline: Point3[], viewReference } * - PolylineInfoCanvas: { polyline: Point2[], viewReference } * * Main Functions: * - add, subtract, intersect, xor: Perform the respective logical operation between two segments * - copy, deleteOperation: Copy or delete a segment * - applyLogicalOperation: Internal function to apply a logical operation and update the segmentation * * Helper Functions: * - getPolylinesInfoWorld: Gets world-space polylines and view references for a segment * - extractPolylinesInCanvasSpace: Converts world-space polylines to canvas space for two segments * - addSegmentInSegmentation: Adds a new segment to the segmentation object */ import type { Types } from '@cornerstonejs/core'; import { getAnnotation, removeAnnotation } from '../../stateManagement'; import type { ContourSegmentationData, ContourSegmentationAnnotation, } from '../../types'; import { convertContourPolylineToCanvasSpace, convertContourPolylineToWorld, } from './sharedOperations'; import addPolylinesToSegmentation from './addPolylinesToSegmentation'; import { getSegmentation } from '../../stateManagement/segmentation/getSegmentation'; import { copyContourSegment } from './copyAnnotation'; import { removeContourSegmentationAnnotation } from './removeContourSegmentationAnnotation'; import { getViewportAssociatedToSegmentation } from '../../stateManagement/segmentation/utilities/getViewportAssociatedToSegmentation'; import { unifyPolylineSets } from './polylineUnify'; import { subtractPolylineSets } from './polylineSubtract'; import { intersectPolylinesSets } from './polylineIntersect'; import { xorPolylinesSets } from './polylineXor'; import type { PolylineInfoWorld } from './polylineInfoTypes'; import { getViewReferenceFromAnnotation } from './getViewReferenceFromAnnotation'; export type SegmentInfo = { segmentationId: string; segmentIndex: number; label?: string; color?: string; }; export type OperatorOptions = SegmentInfo; export enum LogicalOperation { Union, Subtract, Intersect, XOR, Copy, Delete, } /** * Retrieves all polylines (in world space) and their view references for a given segment. * @param contourRepresentationData The contour segmentation data * @param segmentIndex The segment index * @returns Array of PolylineInfoWorld or undefined */ function getPolylinesInfoWorld( contourRepresentationData: ContourSegmentationData, segmentIndex: number ): PolylineInfoWorld[] | undefined { // loop over all annotations in the segment and flatten their polylines const polylinesInfo = []; const { annotationUIDsMap } = contourRepresentationData || {}; if (!annotationUIDsMap?.has(segmentIndex)) { return; } const annotationUIDs = annotationUIDsMap.get(segmentIndex); for (const annotationUID of annotationUIDs) { const annotation = getAnnotation( annotationUID ) as ContourSegmentationAnnotation; const { polyline } = annotation.data.contour; polylinesInfo.push({ polyline, viewReference: getViewReferenceFromAnnotation(annotation), }); } return polylinesInfo; } /** * Converts all polylines for two segments from world space to canvas space for a given viewport. * @param viewport The viewport to use for conversion * @param segment1 The first segment info * @param segment2 The second segment info * @returns Object with polyLinesInfoCanvas1 and polyLinesInfoCanvas2 arrays */ function extractPolylinesInCanvasSpace( viewport: Types.IViewport, segment1: SegmentInfo, segment2: SegmentInfo ) { const segmentation1 = getSegmentation(segment1.segmentationId); const segmentation2 = getSegmentation(segment2.segmentationId); if (!segmentation1 || !segmentation2) { return; } if ( !segmentation1.representationData.Contour || !segmentation2.representationData.Contour ) { return; } const polyLinesInfoWorld1 = getPolylinesInfoWorld( segmentation1.representationData.Contour, segment1.segmentIndex ); const polyLinesInfoWorld2 = getPolylinesInfoWorld( segmentation2.representationData.Contour, segment2.segmentIndex ); if (!polyLinesInfoWorld1 || !polyLinesInfoWorld2) { return; } const polyLinesInfoCanvas1 = polyLinesInfoWorld1.map( ({ polyline, viewReference }) => { return { polyline: convertContourPolylineToCanvasSpace(polyline, viewport), viewReference, }; } ); const polyLinesInfoCanvas2 = polyLinesInfoWorld2.map( ({ polyline, viewReference }) => { return { polyline: convertContourPolylineToCanvasSpace(polyline, viewport), viewReference, }; } ); return { polyLinesInfoCanvas1, polyLinesInfoCanvas2 }; } /** * Adds a new segment entry to the segmentation object. * @param segmentation The segmentation object * @param param1 Object with segmentIndex, label, and color */ function addSegmentInSegmentation( segmentation, { segmentIndex, label, color } ) { if (!segmentation?.segments) { return; } segmentation.segments[segmentIndex] = { active: false, locked: false, label, segmentIndex, cachedStats: {}, color, }; } /** * Removes all annotations for a given segment index from the segmentation. * This function iterates through the provided annotationUIDList, * retrieves each annotation, removes it from the state management, * and also removes the corresponding contour segmentation annotation. * After processing, it clears the annotationUIDList to avoid memory leaks or unintended reuse. * @param annotationUIDList */ function removeAnnotations(annotationUIDList: Set<string>) { annotationUIDList.forEach((annotationUID) => { const annotation = getAnnotation(annotationUID); removeAnnotation(annotationUID); removeContourSegmentationAnnotation( annotation as ContourSegmentationAnnotation ); }); annotationUIDList.clear(); // Clear the set after removal } /** * Applies a logical operation (union, subtract, intersect, xor) between two segments, * converts the result back to world space, and updates the segmentation. * @param segment1 The first segment info * @param segment2 The second segment info * @param options Operator options (target segment info) * @param operation The logical operation to perform */ function applyLogicalOperation( segment1: SegmentInfo, segment2: SegmentInfo, options: OperatorOptions, operation: LogicalOperation ) { const viewport = getViewportAssociatedToSegmentation(segment1.segmentationId); if (!viewport) { return; } const { polyLinesInfoCanvas1, polyLinesInfoCanvas2 } = extractPolylinesInCanvasSpace(viewport, segment1, segment2) || {}; if (!polyLinesInfoCanvas1 || !polyLinesInfoCanvas2) { return; } let polylinesMerged; switch (operation) { case LogicalOperation.Union: polylinesMerged = unifyPolylineSets( polyLinesInfoCanvas1, polyLinesInfoCanvas2 ); break; case LogicalOperation.Subtract: polylinesMerged = subtractPolylineSets( polyLinesInfoCanvas1, polyLinesInfoCanvas2 ); break; case LogicalOperation.Intersect: polylinesMerged = intersectPolylinesSets( polyLinesInfoCanvas1, polyLinesInfoCanvas2 ); break; case LogicalOperation.XOR: polylinesMerged = xorPolylinesSets( polyLinesInfoCanvas1, polyLinesInfoCanvas2 ); break; default: polylinesMerged = unifyPolylineSets( polyLinesInfoCanvas1, polyLinesInfoCanvas2 ); break; } // Convert merged polylines back to world space using their associated viewReference const polyLinesWorld = polylinesMerged.map(({ polyline, viewReference }) => { return { polyline: convertContourPolylineToWorld(polyline, viewport), viewReference, }; }); const resultSegment = options; const segmentation = getSegmentation(resultSegment.segmentationId); const segmentIndex = resultSegment.segmentIndex; const color = resultSegment.color; const label = resultSegment.label; const contourRepresentationData = segmentation.representationData .Contour as ContourSegmentationData; const { annotationUIDsMap } = contourRepresentationData; if (!annotationUIDsMap) { return; } if ( segment1.segmentationId === resultSegment.segmentationId && segment1.segmentIndex === segmentIndex ) { // If the segment being modified is the same as the result segment, // we need to remove the existing annotations for that segment // index before adding new ones. const existingAnnotationUIDs = annotationUIDsMap.get(segmentIndex); if (existingAnnotationUIDs) { removeAnnotations(existingAnnotationUIDs); } } // Add polylines to segmentation, passing viewReference for each addPolylinesToSegmentation( viewport, annotationUIDsMap, segmentation.segmentationId, polyLinesWorld, segmentIndex ); addSegmentInSegmentation(segmentation, { segmentIndex, color, label }); } /** * Performs a union (add) operation between two segments. * @param segment1 The first segment info * @param segment2 The second segment info * @param options Operator options (target segment info) */ export function add( segment1: SegmentInfo, segment2: SegmentInfo, options: OperatorOptions ) { applyLogicalOperation(segment1, segment2, options, LogicalOperation.Union); } /** * Performs a subtraction operation between two segments. * @param segment1 The first segment info * @param segment2 The second segment info * @param options Operator options (target segment info) */ export function subtract( segment1: SegmentInfo, segment2: SegmentInfo, options: OperatorOptions ) { applyLogicalOperation(segment1, segment2, options, LogicalOperation.Subtract); } /** * Performs an intersection operation between two segments. * @param segment1 The first segment info * @param segment2 The second segment info * @param options Operator options (target segment info) */ export function intersect( segment1: SegmentInfo, segment2: SegmentInfo, options: OperatorOptions ) { applyLogicalOperation( segment1, segment2, options, LogicalOperation.Intersect ); } /** * Performs an XOR operation between two segments. * @param segment1 The first segment info * @param segment2 The second segment info * @param options Operator options (target segment info) */ export function xor( segment1: SegmentInfo, segment2: SegmentInfo, options: OperatorOptions ) { applyLogicalOperation(segment1, segment2, options, LogicalOperation.XOR); } /** * Copies a segment to a new segment index or segmentation. * @param segment The source segment info * @param options The target segment info */ export function copy(segment: SegmentInfo, options: OperatorOptions) { copyContourSegment( segment.segmentationId, segment.segmentIndex, options.segmentationId, options.segmentIndex ); } /** * Deletes all annotations for a given segment from the segmentation. * @param segment The segment info */ export function deleteOperation(segment: SegmentInfo) { const segmentation = getSegmentation(segment.segmentationId); if (!segmentation) { console.log('No active segmentation detected'); return; } if (!segmentation.representationData.Contour) { console.log('No contour representation found'); return; } const representationData = segmentation.representationData.Contour; const { annotationUIDsMap } = representationData; if (!annotationUIDsMap) { console.log('No annotation map found'); return; } if (!annotationUIDsMap.has(segment.segmentIndex)) { console.log('Segmentation index has no annotations'); return; } const annotationUIDList = annotationUIDsMap.get(segment.segmentIndex); removeAnnotations(annotationUIDList); } |