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 | import { utilities } from '@cornerstonejs/core'; import type { ContourSegmentationAnnotation } from '../../types'; import { addAnnotation, getAnnotation } from '../../stateManagement'; import { getViewportAssociatedToSegmentation } from '../../stateManagement/segmentation/utilities/getViewportAssociatedToSegmentation'; import { getToolGroupForViewport } from '../../store/ToolGroupManager'; import { getAnnotationsUIDMapFromSegmentation } from '../../stateManagement/segmentation/utilities/getAnnotationsUIDMapFromSegmentation'; /** * Creates a deep copy of a contour segmentation annotation, assigning it to a new segmentation and segment index. * Copies polyline and handle points, and generates a new annotation UID. * * @param annotation - The annotation to copy * @param segmentationId - The target segmentation ID * @param segmentIndex - The target segment index * @returns A new ContourSegmentationAnnotation object */ export function copyAnnotation( annotation: ContourSegmentationAnnotation, segmentationId: string, segmentIndex: number ): ContourSegmentationAnnotation { const newAnnotation: ContourSegmentationAnnotation = { annotationUID: utilities.uuidv4(), data: { contour: { closed: true, polyline: [], }, segmentation: { segmentationId, segmentIndex, }, handles: {}, }, handles: {}, highlighted: false, autoGenerated: false, invalidated: false, isLocked: false, isVisible: true, metadata: { ...annotation.metadata, toolName: annotation.metadata.toolName, }, }; newAnnotation.data.segmentation.segmentationId = segmentationId; newAnnotation.data.segmentation.segmentIndex = segmentIndex; // Copy specific attributes if (annotation.data.contour?.polyline) { newAnnotation.data.contour.polyline = [...annotation.data.contour.polyline]; } if (annotation.data.handles?.points) { newAnnotation.data.handles.points = annotation.data.handles.points.map( (point) => [...point] ); } return newAnnotation; } /** * Copies all contour annotations from a segment in one segmentation to a segment in another segmentation. * Handles copying of child annotations and spline objects if present. * * @param segmentationId - Source segmentation ID * @param segmentIndex - Source segment index * @param targetSegmentationId - Target segmentation ID * @param targetSegmentIndex - Target segment index */ export function copyContourSegment( segmentationId: string, segmentIndex: number, targetSegmentationId: string, targetSegmentIndex: number ) { const annotationUIDsMap = getAnnotationsUIDMapFromSegmentation(segmentationId); const targetAnnotationUIDsMap = getAnnotationsUIDMapFromSegmentation(targetSegmentationId); if (!annotationUIDsMap || !targetAnnotationUIDsMap) { return; } if (!annotationUIDsMap?.has(segmentIndex)) { return; } const annotationUIDs = annotationUIDsMap.get(segmentIndex); const viewport = getViewportAssociatedToSegmentation(targetSegmentationId); if (!viewport) { return; } const toolGroup = getToolGroupForViewport(viewport.id); const copyContourAnnotation = (annotation: ContourSegmentationAnnotation) => { const newAnnotation = copyAnnotation( annotation, targetSegmentationId, targetSegmentIndex ); if (toolGroup) { const instance = toolGroup.getToolInstance(annotation.metadata.toolName); if (instance) { // Properly test if the function isSplineAnnotation exists in instance if ( typeof instance.isSplineAnnotation === 'function' && instance.isSplineAnnotation(annotation) ) { instance.createSplineObjectFromType( newAnnotation, (annotation.data.spline as { type: string }).type ); } } } addAnnotation(newAnnotation, viewport.element); newAnnotationsUID.add(newAnnotation.annotationUID); return newAnnotation; }; const newAnnotationsUID = new Set<string>(); for (const annotationUID of annotationUIDs) { const annotation = getAnnotation( annotationUID ) as ContourSegmentationAnnotation; const newAnnotation = copyContourAnnotation(annotation); if (annotation?.childAnnotationUIDs) { newAnnotation.childAnnotationUIDs = []; for (const childAnnotationUID of annotation.childAnnotationUIDs) { const childAnnotation = getAnnotation( childAnnotationUID ) as ContourSegmentationAnnotation; const newChildAnnotation = copyContourAnnotation(childAnnotation); newChildAnnotation.parentAnnotationUID = newAnnotation.annotationUID; newAnnotation.childAnnotationUIDs.push( newChildAnnotation.annotationUID ); } } } targetAnnotationUIDsMap.set(targetSegmentIndex, newAnnotationsUID); } |