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 | import { volumeLoader, utilities as csUtils, eventTarget, cache, } from '@cornerstonejs/core'; import { Events, SegmentationRepresentations } from '../../enums'; import addSegmentationRepresentations from './addSegmentationRepresentations'; import { triggerSegmentationRender } from '../../utilities/segmentation'; import { getSegmentation } from './segmentationState'; import { LabelmapSegmentationDataStack } from '../../types/LabelmapTypes'; import { triggerSegmentationDataModified } from './triggerSegmentationEvents'; async function computeVolumeSegmentationFromStack({ imageIdReferenceMap, options, }: { imageIdReferenceMap: Map<string, string>; options?: { volumeId?: string; }; }): Promise<{ volumeId: string }> { const segmentationImageIds = Array.from(imageIdReferenceMap.values()); const additionalDetails = { imageIdReferenceMap, }; const volumeId = options?.volumeId ?? csUtils.uuidv4(); await volumeLoader.createAndCacheVolumeFromImages( volumeId, segmentationImageIds, { additionalDetails, } ); return { volumeId }; } /** * Converts a stack-based segmentation to a volume-based segmentation. * * @param params - The parameters for the conversion. * @param params.segmentationId - The segmentationId to convert. * @param [params.options] - The conversion options. * @param params.options.toolGroupId - The new toolGroupId to use for the segmentation. * @param [params.options.volumeId] - the new volumeId to use for the segmentation. If not provided, a new ID will be generated. * @param [params.options.newSegmentationId] - the new segmentationId to use for the segmentation. If not provided, a new ID will be generated. * @param [params.options.removeOriginal] - Whether or not to remove the original segmentation. Defaults to true. * * @returns A promise that resolves when the conversion is complete. */ async function convertStackToVolumeSegmentation({ segmentationId, options, }: { segmentationId: string; options?: { toolGroupId: string; volumeId?: string; removeOriginal?: boolean; }; }): Promise<void> { const segmentation = getSegmentation(segmentationId); const data = segmentation.representationData .LABELMAP as LabelmapSegmentationDataStack; const { volumeId } = await computeVolumeSegmentationFromStack({ imageIdReferenceMap: data.imageIdReferenceMap, options, }); await updateSegmentationState({ segmentationId, toolGroupId: options.toolGroupId, options, volumeId, }); } // This function is responsible for updating the segmentation state async function updateSegmentationState({ segmentationId, toolGroupId, volumeId, options, }: { segmentationId: string; toolGroupId: string; volumeId: string; options?: { removeOriginal?: boolean; }; }): Promise<void> { const segmentation = getSegmentation(segmentationId); if (options?.removeOriginal) { const data = segmentation.representationData .LABELMAP as LabelmapSegmentationDataStack; const imageIdReferenceMap = data.imageIdReferenceMap; Array.from(imageIdReferenceMap.values()).forEach((imageId) => { cache.removeImageLoadObject(imageId); }); segmentation.representationData.LABELMAP = { volumeId, }; } else { segmentation.representationData.LABELMAP = { ...segmentation.representationData.LABELMAP, volumeId, }; } await addSegmentationRepresentations(toolGroupId, [ { segmentationId, type: SegmentationRepresentations.Labelmap, }, ]); triggerSegmentationRender(toolGroupId); // Note: It is crucial to trigger the data modified event. This ensures that the // old texture is updated to the GPU, especially in scenarios where it may not be getting updated. eventTarget.addEventListenerOnce(Events.SEGMENTATION_RENDERED, () => triggerSegmentationDataModified(segmentationId) ); } export { convertStackToVolumeSegmentation, computeVolumeSegmentationFromStack }; |