@ohif/extension-cornerstone 3.14.0-beta.10 → 3.14.0-beta.100
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/915.ohif-extension-cornerstone.umd.js +1 -1
- package/dist/ohif-extension-cornerstone.umd.js +1 -5
- package/package.json +33 -25
- package/src/Viewport/OHIFCornerstoneViewport.css +46 -0
- package/src/Viewport/OHIFCornerstoneViewport.tsx +502 -0
- package/src/Viewport/Overlays/CornerstoneOverlays.tsx +103 -0
- package/src/Viewport/Overlays/CustomizableViewportOverlay.css +25 -0
- package/src/Viewport/Overlays/CustomizableViewportOverlay.tsx +469 -0
- package/src/Viewport/Overlays/ViewportImageScrollbar.tsx +106 -0
- package/src/Viewport/Overlays/ViewportImageSliceLoadingIndicator.tsx +87 -0
- package/src/Viewport/Overlays/ViewportOrientationMarkers.css +41 -0
- package/src/Viewport/Overlays/ViewportOrientationMarkers.tsx +136 -0
- package/src/Viewport/Overlays/ViewportSliceProgressScrollbar/ViewportSliceProgressScrollbar.tsx +195 -0
- package/src/Viewport/Overlays/ViewportSliceProgressScrollbar/helpers.ts +46 -0
- package/src/Viewport/Overlays/ViewportSliceProgressScrollbar/hooks.ts +383 -0
- package/src/Viewport/Overlays/ViewportSliceProgressScrollbar/index.ts +1 -0
- package/src/Viewport/Overlays/ViewportSliceProgressScrollbar/types.ts +17 -0
- package/src/Viewport/Overlays/utils.ts +66 -0
- package/src/commandsModule.ts +2951 -0
- package/src/components/AccordionGroup/AccordionGroup.tsx +195 -0
- package/src/components/AccordionGroup/index.ts +4 -0
- package/src/components/ActiveViewportWindowLevel/ActiveViewportWindowLevel.tsx +23 -0
- package/src/components/ActiveViewportWindowLevel/index.js +1 -0
- package/src/components/AdvancedRenderingControls/AdvancedRenderingControls.tsx +149 -0
- package/src/components/AdvancedRenderingControls/index.ts +1 -0
- package/src/components/CinePlayer/CinePlayer.tsx +247 -0
- package/src/components/CinePlayer/index.ts +3 -0
- package/src/components/DicomUpload/DicomUpload.tsx +111 -0
- package/src/components/DicomUpload/DicomUploadProgress.tsx +396 -0
- package/src/components/DicomUpload/DicomUploadProgressItem.tsx +119 -0
- package/src/components/ExportSegmentationSubMenuItem.tsx +75 -0
- package/src/components/MeasurementItems.tsx +70 -0
- package/src/components/MeasurementTableNested.tsx +30 -0
- package/src/components/MeasurementsMenu.tsx +84 -0
- package/src/components/MeasurementsOrAdditionalFindings.tsx +82 -0
- package/src/components/ModalityLoadBadge/ModalityLoadBadge.tsx +171 -0
- package/src/components/ModalityLoadBadge/index.ts +1 -0
- package/src/components/NavigationComponent/NavigationComponent.tsx +138 -0
- package/src/components/NavigationComponent/index.ts +3 -0
- package/src/components/OHIFViewportActionCorners.tsx +94 -0
- package/src/components/OpacityMenu/OpacityMenu.tsx +54 -0
- package/src/components/OpacityMenu/OpacityMenuWrapper.tsx +85 -0
- package/src/components/OpacityMenu/index.ts +2 -0
- package/src/components/PanelAccordionTrigger.tsx +48 -0
- package/src/components/SegmentationToolConfig.tsx +71 -0
- package/src/components/SegmentationUtilityButton.tsx +61 -0
- package/src/components/SelectItemWithModality.tsx +29 -0
- package/src/components/SeriesMeasurements.tsx +90 -0
- package/src/components/SeriesSummaryFromDisplaySet.tsx +21 -0
- package/src/components/StudyMeasurements.tsx +88 -0
- package/src/components/StudyMeasurementsActions.tsx +75 -0
- package/src/components/StudySummaryFromMetadata.test.ts +47 -0
- package/src/components/StudySummaryFromMetadata.tsx +31 -0
- package/src/components/StudySummaryWithActions.tsx +14 -0
- package/src/components/ThresholdMenu/ThresholdMenu.tsx +104 -0
- package/src/components/ThresholdMenu/ThresholdMenuWrapper.tsx +85 -0
- package/src/components/ThresholdMenu/index.ts +2 -0
- package/src/components/TrackingStatus/TrackingStatus.tsx +32 -0
- package/src/components/TrackingStatus/index.ts +1 -0
- package/src/components/VOIManualControlMenu/VOIManualControlMenu.tsx +190 -0
- package/src/components/VOIManualControlMenu/VOIManualControlMenuWrapper.tsx +85 -0
- package/src/components/VOIManualControlMenu/index.ts +2 -0
- package/src/components/ViewportColorbar/ViewportColorbar.tsx +150 -0
- package/src/components/ViewportColorbar/ViewportColorbarsContainer.tsx +125 -0
- package/src/components/ViewportColorbar/index.ts +5 -0
- package/src/components/ViewportDataOverlaySettingMenu/ViewportDataOverlayMenu.tsx +538 -0
- package/src/components/ViewportDataOverlaySettingMenu/ViewportDataOverlayMenuWrapper.tsx +91 -0
- package/src/components/ViewportDataOverlaySettingMenu/utils.ts +118 -0
- package/src/components/ViewportOrientationMenu/ViewportOrientationMenu.tsx +249 -0
- package/src/components/ViewportOrientationMenu/ViewportOrientationMenuWrapper.tsx +29 -0
- package/src/components/ViewportWindowLevel/ViewportWindowLevel.tsx +261 -0
- package/src/components/ViewportWindowLevel/getViewportVolumeHistogram.ts +72 -0
- package/src/components/ViewportWindowLevel/histogramWorker.js +97 -0
- package/src/components/ViewportWindowLevel/index.js +1 -0
- package/src/components/ViewportWindowLevel/utils.ts +176 -0
- package/src/components/WindowLevelActionMenu/Colorbar.tsx +30 -0
- package/src/components/WindowLevelActionMenu/Colormap.tsx +121 -0
- package/src/components/WindowLevelActionMenu/VolumeLighting.tsx +82 -0
- package/src/components/WindowLevelActionMenu/VolumeRenderingOptions.tsx +38 -0
- package/src/components/WindowLevelActionMenu/VolumeRenderingPresets.tsx +36 -0
- package/src/components/WindowLevelActionMenu/VolumeRenderingPresetsContent.tsx +78 -0
- package/src/components/WindowLevelActionMenu/VolumeRenderingQuality.tsx +64 -0
- package/src/components/WindowLevelActionMenu/VolumeShade.tsx +47 -0
- package/src/components/WindowLevelActionMenu/VolumeShift.tsx +92 -0
- package/src/components/WindowLevelActionMenu/WindowLevel.tsx +157 -0
- package/src/components/WindowLevelActionMenu/WindowLevelActionMenu.tsx +102 -0
- package/src/components/WindowLevelActionMenu/WindowLevelActionMenuWrapper.tsx +131 -0
- package/src/components/WindowLevelActionMenu/defaultWindowLevelPresets.ts +23 -0
- package/src/components/WindowLevelActionMenu/index.ts +11 -0
- package/src/components/WindowLevelActionMenu/windowLevelPresetHotkeys.test.ts +19 -0
- package/src/components/index.ts +12 -0
- package/src/constants/index.ts +13 -0
- package/src/customizations/CustomDropdownMenuContent.tsx +106 -0
- package/src/customizations/CustomSegmentStatisticsHeader.tsx +140 -0
- package/src/customizations/captureViewportModalCustomization.tsx +175 -0
- package/src/customizations/colorbarCustomization.ts +93 -0
- package/src/customizations/gpuSettingsPresetCustomization.ts +8 -0
- package/src/customizations/layoutSelectorCustomization.ts +104 -0
- package/src/customizations/measurementsCustomization.ts +124 -0
- package/src/customizations/miscCustomization.ts +22 -0
- package/src/customizations/modalityColorMapCustomization.ts +23 -0
- package/src/customizations/segmentationHydrationCustomization.ts +16 -0
- package/src/customizations/segmentationPanelCustomization.tsx +119 -0
- package/src/customizations/segmentationToolbarCustomization.ts +964 -0
- package/src/customizations/toolGroupToolsCustomization.ts +204 -0
- package/src/customizations/toolbarButtonsCustomization.ts +805 -0
- package/src/customizations/viewportClickCommandsCustomization.ts +15 -0
- package/src/customizations/viewportDownloadWarningCustomization.tsx +6 -0
- package/src/customizations/viewportOverlayCustomization.tsx +46 -0
- package/src/customizations/viewportScrollbarCustomization.tsx +27 -0
- package/src/customizations/viewportToolsCustomization.ts +33 -0
- package/src/customizations/volumeRenderingCustomization.ts +14 -0
- package/src/customizations/windowLevelPresetsCustomization.ts +5 -0
- package/src/enums.ts +9 -0
- package/src/getCustomizationModule.tsx +54 -0
- package/src/getHangingProtocolModule.ts +47 -0
- package/src/getPanelModule.tsx +128 -0
- package/src/getSopClassHandlerModule.js +284 -0
- package/src/getToolbarModule.tsx +622 -0
- package/src/hooks/index.ts +8 -0
- package/src/hooks/useActiveViewportSegmentationRepresentations.ts +14 -0
- package/src/hooks/useMeasurementTracking.ts +132 -0
- package/src/hooks/useMeasurements.ts +104 -0
- package/src/hooks/useSegmentations.ts +147 -0
- package/src/hooks/useViewportDisplaySets.ts +284 -0
- package/src/hooks/useViewportHover.ts +77 -0
- package/src/hooks/useViewportRendering.tsx +868 -0
- package/src/hooks/useViewportSegmentations.ts +245 -0
- package/src/hps/fourUp.ts +117 -0
- package/src/hps/frameView.ts +1581 -0
- package/src/hps/main3D.ts +142 -0
- package/src/hps/mpr.ts +197 -0
- package/src/hps/mprAnd3DVolumeViewport.ts +124 -0
- package/src/hps/only3D.ts +70 -0
- package/src/hps/primary3D.ts +142 -0
- package/src/hps/primaryAxial.ts +114 -0
- package/src/id.js +5 -0
- package/src/index.tsx +350 -0
- package/src/init.tsx +445 -0
- package/src/initCineService.ts +75 -0
- package/src/initContextMenu.ts +94 -0
- package/src/initCornerstoneTools.js +188 -0
- package/src/initDoubleClick.ts +82 -0
- package/src/initMeasurementService.ts +576 -0
- package/src/initStudyPrefetcherService.ts +33 -0
- package/src/initWADOImageLoader.js +56 -0
- package/src/panels/PanelMeasurement.tsx +84 -0
- package/src/panels/PanelSegmentation.tsx +338 -0
- package/src/services/ColorbarService/ColorbarService.ts +242 -0
- package/src/services/ColorbarService/index.ts +2 -0
- package/src/services/CornerstoneCacheService/CornerstoneCacheService.ts +326 -0
- package/src/services/CornerstoneCacheService/index.js +3 -0
- package/src/services/SegmentationService/RTSTRUCT/mapROIContoursToRTStructData.ts +36 -0
- package/src/services/SegmentationService/SegmentationService.test.ts +3698 -0
- package/src/services/SegmentationService/SegmentationService.ts +2376 -0
- package/src/services/SegmentationService/backends/ISegmentationBackend.ts +102 -0
- package/src/services/SegmentationService/backends/ISegmentationServiceInternals.ts +44 -0
- package/src/services/SegmentationService/backends/LegacySegmentationBackend.ts +139 -0
- package/src/services/SegmentationService/backends/NextSegmentationBackend.ts +159 -0
- package/src/services/SegmentationService/backends/index.ts +4 -0
- package/src/services/SegmentationService/index.ts +3 -0
- package/src/services/SyncGroupService/SyncGroupService.ts +264 -0
- package/src/services/SyncGroupService/createHydrateSegmentationSynchronizer.ts +118 -0
- package/src/services/SyncGroupService/index.js +3 -0
- package/src/services/ToolGroupService/ToolGroupService.test.ts +83 -0
- package/src/services/ToolGroupService/ToolGroupService.ts +541 -0
- package/src/services/ToolGroupService/index.js +3 -0
- package/src/services/ViewedDataService/ViewedDataService.ts +58 -0
- package/src/services/ViewedDataService/index.ts +3 -0
- package/src/services/ViewportService/CornerstoneViewportService.ts +1814 -0
- package/src/services/ViewportService/IViewportService.ts +64 -0
- package/src/services/ViewportService/Viewport.ts +372 -0
- package/src/services/ViewportService/adapter/IViewportAdapter.contract.test.ts +467 -0
- package/src/services/ViewportService/adapter/IViewportAdapter.ts +179 -0
- package/src/services/ViewportService/adapter/LegacyViewportAdapter.ts +240 -0
- package/src/services/ViewportService/adapter/NextViewportAdapter.ts +241 -0
- package/src/services/ViewportService/adapter/getViewportAdapter.ts +62 -0
- package/src/services/ViewportService/adapter/index.ts +16 -0
- package/src/services/ViewportService/backends/IViewportBackend.ts +195 -0
- package/src/services/ViewportService/backends/IViewportOperations.ts +98 -0
- package/src/services/ViewportService/backends/IViewportServiceInternals.ts +47 -0
- package/src/services/ViewportService/backends/LegacyViewportBackend.ts +264 -0
- package/src/services/ViewportService/backends/LegacyViewportOperations.ts +272 -0
- package/src/services/ViewportService/backends/NextViewportBackend.ts +671 -0
- package/src/services/ViewportService/backends/NextViewportOperations.ts +169 -0
- package/src/services/ViewportService/backends/README.md +62 -0
- package/src/services/ViewportService/backends/dataIdRegistry.ts +121 -0
- package/src/services/ViewportService/backends/viewportOperations.ts +71 -0
- package/src/services/ViewportService/constants.ts +3 -0
- package/src/state.ts +34 -0
- package/src/stores/index.ts +5 -0
- package/src/stores/presentationUtils.ts +42 -0
- package/src/stores/useLutPresentationStore.ts +166 -0
- package/src/stores/usePositionPresentationStore.ts +172 -0
- package/src/stores/useSegmentationPresentationStore.test.ts +146 -0
- package/src/stores/useSegmentationPresentationStore.ts +417 -0
- package/src/stores/useSelectedSegmentationsForViewportStore.ts +85 -0
- package/src/stores/useSynchronizersStore.ts +84 -0
- package/src/synchronizers/frameViewSynchronizer.ts +52 -0
- package/src/tools/CalibrationLineTool.ts +97 -0
- package/src/tools/ImageOverlayViewerTool.tsx +279 -0
- package/src/tools/OverlayPlaneModuleProvider.ts +40 -0
- package/src/tools/WindowLevelTool.test.ts +55 -0
- package/src/tools/WindowLevelTool.ts +22 -0
- package/src/types/AppTypes.ts +70 -0
- package/src/types/Colorbar.ts +114 -0
- package/src/types/Colormap.ts +16 -0
- package/src/types/CornerstoneCacheService.ts +40 -0
- package/src/types/CornerstoneServices.ts +18 -0
- package/src/types/Presentation.ts +83 -0
- package/src/types/SelectedSegmentationByTypeMap.ts +3 -0
- package/src/types/ViewportPresets.ts +53 -0
- package/src/types/VolumeLighting.ts +7 -0
- package/src/types/VolumeRenderingConfig.ts +8 -0
- package/src/types/WindowLevel.ts +5 -0
- package/src/types/index.ts +10 -0
- package/src/utils/ActiveViewportBehavior.tsx +80 -0
- package/src/utils/CornerstoneViewportDownloadForm.tsx +319 -0
- package/src/utils/DicomFileUploader.ts +204 -0
- package/src/utils/JumpPresets.ts +14 -0
- package/src/utils/autoHydrateViewportTypes.ts +69 -0
- package/src/utils/colormaps.js +1597 -0
- package/src/utils/createSegmentationForViewport.ts +83 -0
- package/src/utils/dicomLoaderService.js +225 -0
- package/src/utils/ecgMetadata.test.ts +137 -0
- package/src/utils/ecgMetadata.ts +109 -0
- package/src/utils/findNearbyToolData.test.ts +125 -0
- package/src/utils/findNearbyToolData.ts +21 -0
- package/src/utils/generateSegmentationCSVReport.test.ts +324 -0
- package/src/utils/generateSegmentationCSVReport.ts +131 -0
- package/src/utils/getActiveViewportEnabledElement.test.ts +70 -0
- package/src/utils/getActiveViewportEnabledElement.ts +8 -0
- package/src/utils/getCenterExtent.test.ts +313 -0
- package/src/utils/getCenterExtent.ts +46 -0
- package/src/utils/getCornerstoneBlendMode.test.ts +59 -0
- package/src/utils/getCornerstoneBlendMode.ts +25 -0
- package/src/utils/getCornerstoneOrientation.test.ts +78 -0
- package/src/utils/getCornerstoneOrientation.ts +31 -0
- package/src/utils/getCornerstoneViewportType.test.ts +169 -0
- package/src/utils/getCornerstoneViewportType.ts +88 -0
- package/src/utils/getInterleavedFrames.test.ts +85 -0
- package/src/utils/getInterleavedFrames.ts +73 -0
- package/src/utils/getLegacyViewportType.ts +64 -0
- package/src/utils/getNthFrames.test.ts +145 -0
- package/src/utils/getNthFrames.ts +31 -0
- package/src/utils/getViewportEnabledElement.test.ts +83 -0
- package/src/utils/getViewportEnabledElement.ts +8 -0
- package/src/utils/getViewportOrientationFromImageOrientationPatient.test.ts +50 -0
- package/src/utils/getViewportOrientationFromImageOrientationPatient.ts +56 -0
- package/src/utils/hydrationUtils.test.ts +572 -0
- package/src/utils/hydrationUtils.ts +197 -0
- package/src/utils/imageSliceSync/calculateViewportRegistrations.ts +28 -0
- package/src/utils/imageSliceSync/toggleImageSliceSync.ts +101 -0
- package/src/utils/index.ts +29 -0
- package/src/utils/initGpuSettingsPreset.ts +51 -0
- package/src/utils/initViewTiming.test.ts +342 -0
- package/src/utils/initViewTiming.ts +52 -0
- package/src/utils/initWebWorkerProgressHandler.test.ts +381 -0
- package/src/utils/initWebWorkerProgressHandler.ts +110 -0
- package/src/utils/interleave.test.ts +304 -0
- package/src/utils/interleave.ts +32 -0
- package/src/utils/interleaveCenterLoader.test.ts +547 -0
- package/src/utils/interleaveCenterLoader.ts +143 -0
- package/src/utils/interleaveTopToBottom.test.ts +487 -0
- package/src/utils/interleaveTopToBottom.ts +132 -0
- package/src/utils/isAnyDisplaySetCommon.ts +18 -0
- package/src/utils/isDisplaySetOverlayable.ts +96 -0
- package/src/utils/isMeasurementWithinViewport.test.ts +340 -0
- package/src/utils/isMeasurementWithinViewport.ts +33 -0
- package/src/utils/isReferenceViewable.test.ts +581 -0
- package/src/utils/isReferenceViewable.ts +95 -0
- package/src/utils/loadDisplaySetData.ts +49 -0
- package/src/utils/measurementServiceMappings/Angle.ts +200 -0
- package/src/utils/measurementServiceMappings/ArrowAnnotate.ts +176 -0
- package/src/utils/measurementServiceMappings/Bidirectional.ts +193 -0
- package/src/utils/measurementServiceMappings/CircleROI.ts +227 -0
- package/src/utils/measurementServiceMappings/CobbAngle.ts +206 -0
- package/src/utils/measurementServiceMappings/EllipticalROI.ts +221 -0
- package/src/utils/measurementServiceMappings/Length.ts +206 -0
- package/src/utils/measurementServiceMappings/LivewireContour.ts +175 -0
- package/src/utils/measurementServiceMappings/PlanarFreehandROI.ts +228 -0
- package/src/utils/measurementServiceMappings/Probe.ts +192 -0
- package/src/utils/measurementServiceMappings/RectangleROI.ts +225 -0
- package/src/utils/measurementServiceMappings/SegmentBidirectional.ts +184 -0
- package/src/utils/measurementServiceMappings/SplineROI.ts +227 -0
- package/src/utils/measurementServiceMappings/UltrasoundDirectional.ts +214 -0
- package/src/utils/measurementServiceMappings/UltrasoundPleuraBLine.ts +179 -0
- package/src/utils/measurementServiceMappings/constants/supportedTools.js +21 -0
- package/src/utils/measurementServiceMappings/index.ts +3 -0
- package/src/utils/measurementServiceMappings/measurementServiceMappingsFactory.ts +324 -0
- package/src/utils/measurementServiceMappings/utils/getDisplayUnit.js +3 -0
- package/src/utils/measurementServiceMappings/utils/getHandlesFromPoints.js +14 -0
- package/src/utils/measurementServiceMappings/utils/getIsLocked.ts +5 -0
- package/src/utils/measurementServiceMappings/utils/getIsVisible.ts +6 -0
- package/src/utils/measurementServiceMappings/utils/getSOPInstanceAttributes.js +66 -0
- package/src/utils/measurementServiceMappings/utils/getValueDisplayString.js +12 -0
- package/src/utils/measurementServiceMappings/utils/index.ts +17 -0
- package/src/utils/measurementServiceMappings/utils/selection.ts +33 -0
- package/src/utils/mpr/getProtocolViewportStructureFromGridViewports.ts +74 -0
- package/src/utils/nextViewportPolicies.ts +41 -0
- package/src/utils/nextViewports.ts +157 -0
- package/src/utils/nthLoader.test.ts +317 -0
- package/src/utils/nthLoader.ts +78 -0
- package/src/utils/presentations/getViewportPresentations.test.ts +280 -0
- package/src/utils/presentations/getViewportPresentations.ts +151 -0
- package/src/utils/promptHydrationDialog.test.ts +490 -0
- package/src/utils/promptHydrationDialog.ts +197 -0
- package/src/utils/removeViewportSegmentationRepresentations.test.ts +129 -0
- package/src/utils/removeViewportSegmentationRepresentations.ts +17 -0
- package/src/utils/segmentUtils.test.ts +515 -0
- package/src/utils/segmentUtils.ts +46 -0
- package/src/utils/segmentationExportUtils.test.ts +100 -0
- package/src/utils/segmentationExportUtils.ts +171 -0
- package/src/utils/segmentationHandlers.test.ts +614 -0
- package/src/utils/segmentationHandlers.ts +164 -0
- package/src/utils/setUpAnnotationEventHandlers.ts +26 -0
- package/src/utils/setUpSegmentationEventHandlers.test.ts +372 -0
- package/src/utils/setUpSegmentationEventHandlers.ts +122 -0
- package/src/utils/toggleVOISliceSync.test.ts +606 -0
- package/src/utils/toggleVOISliceSync.ts +103 -0
- package/src/utils/transitions.test.ts +351 -0
- package/src/utils/transitions.ts +300 -0
- package/src/utils/updateSegmentationStats.test.ts +942 -0
- package/src/utils/updateSegmentationStats.ts +192 -0
- package/src/utils/viewportDataShape.ts +85 -0
|
@@ -0,0 +1,2376 @@
|
|
|
1
|
+
import {
|
|
2
|
+
cache,
|
|
3
|
+
Enums as csEnums,
|
|
4
|
+
eventTarget,
|
|
5
|
+
geometryLoader,
|
|
6
|
+
getEnabledElementByViewportId,
|
|
7
|
+
imageLoader,
|
|
8
|
+
Types as csTypes,
|
|
9
|
+
utilities as csUtils,
|
|
10
|
+
metaData,
|
|
11
|
+
} from '@cornerstonejs/core';
|
|
12
|
+
import { ViewportType } from '@cornerstonejs/core/enums';
|
|
13
|
+
import { isVolume3DViewportType } from '../../utils/getLegacyViewportType';
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
Enums as csToolsEnums,
|
|
17
|
+
segmentation as cstSegmentation,
|
|
18
|
+
Types as cstTypes,
|
|
19
|
+
annotation as cstAnnotation,
|
|
20
|
+
} from '@cornerstonejs/tools';
|
|
21
|
+
|
|
22
|
+
import { PubSubService, Types as OHIFTypes } from '@ohif/core';
|
|
23
|
+
import i18n from '@ohif/i18n';
|
|
24
|
+
|
|
25
|
+
import { VOLUME_LOADER_SCHEME } from '../../constants';
|
|
26
|
+
import { mapROIContoursToRTStructData } from './RTSTRUCT/mapROIContoursToRTStructData';
|
|
27
|
+
import { SegmentationPresentation, SegmentationPresentationItem } from '../../types/Presentation';
|
|
28
|
+
import { EasingFunctionEnum, EasingFunctionMap } from '../../utils/transitions';
|
|
29
|
+
import { ViewReference } from '@cornerstonejs/core/types';
|
|
30
|
+
import {
|
|
31
|
+
LegacySegmentationBackend,
|
|
32
|
+
NextSegmentationBackend,
|
|
33
|
+
type ISegmentationBackend,
|
|
34
|
+
type ISegmentationServiceInternals,
|
|
35
|
+
} from './backends';
|
|
36
|
+
// Sanctioned flag read: the SEG data shape (single- vs multi-layer) is fixed at
|
|
37
|
+
// load time, before any target viewport exists, so this one seg-backend dispatch
|
|
38
|
+
// cannot use a per-viewport capability check and reads the session flag instead.
|
|
39
|
+
import { isNextViewportsEnabled } from '../../utils/nextViewports';
|
|
40
|
+
import { isNextViewport } from '../ViewportService/adapter';
|
|
41
|
+
|
|
42
|
+
const { DefaultHistoryMemo } = csUtils.HistoryMemo;
|
|
43
|
+
|
|
44
|
+
const {
|
|
45
|
+
Labelmap: LABELMAP,
|
|
46
|
+
Contour: CONTOUR,
|
|
47
|
+
Surface: SURFACE,
|
|
48
|
+
} = csToolsEnums.SegmentationRepresentations;
|
|
49
|
+
|
|
50
|
+
const {
|
|
51
|
+
getLabelmapImageIds,
|
|
52
|
+
helpers: { convertStackToVolumeLabelmap },
|
|
53
|
+
state: { addColorLUT },
|
|
54
|
+
triggerSegmentationEvents: { triggerSegmentationRepresentationModified },
|
|
55
|
+
} = cstSegmentation;
|
|
56
|
+
|
|
57
|
+
export type SegmentRepresentation = {
|
|
58
|
+
segmentIndex: number;
|
|
59
|
+
color: csTypes.Color;
|
|
60
|
+
opacity: number;
|
|
61
|
+
visible: boolean;
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
export type SegmentationData = cstTypes.Segmentation;
|
|
65
|
+
|
|
66
|
+
export type SegmentationRepresentation = cstTypes.SegmentationRepresentation & {
|
|
67
|
+
viewportId: string;
|
|
68
|
+
id: string;
|
|
69
|
+
label: string;
|
|
70
|
+
fallbackLabel?: string;
|
|
71
|
+
styles: cstTypes.RepresentationStyle;
|
|
72
|
+
segments: {
|
|
73
|
+
[key: number]: SegmentRepresentation;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
export type SegmentationInfo = {
|
|
78
|
+
segmentation: SegmentationData;
|
|
79
|
+
representation?: SegmentationRepresentation;
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const EVENTS = {
|
|
83
|
+
SEGMENTATION_MODIFIED: 'event::segmentation_modified',
|
|
84
|
+
// fired when the segmentation is added
|
|
85
|
+
SEGMENTATION_ADDED: 'event::segmentation_added',
|
|
86
|
+
//
|
|
87
|
+
SEGMENTATION_DATA_MODIFIED: 'event::segmentation_data_modified',
|
|
88
|
+
// fired when the segmentation is removed
|
|
89
|
+
SEGMENTATION_REMOVED: 'event::segmentation_removed',
|
|
90
|
+
//
|
|
91
|
+
// fired when segmentation representation is added
|
|
92
|
+
SEGMENTATION_REPRESENTATION_MODIFIED: 'event::segmentation_representation_modified',
|
|
93
|
+
// fired when segmentation representation is removed
|
|
94
|
+
SEGMENTATION_REPRESENTATION_REMOVED: 'event::segmentation_representation_removed',
|
|
95
|
+
//
|
|
96
|
+
// LOADING EVENTS
|
|
97
|
+
// fired when the active segment is loaded in SEG or RTSTRUCT
|
|
98
|
+
SEGMENT_LOADING_COMPLETE: 'event::segment_loading_complete',
|
|
99
|
+
// loading completed for all segments
|
|
100
|
+
SEGMENTATION_LOADING_COMPLETE: 'event::segmentation_loading_complete',
|
|
101
|
+
// fired when a contour annotation cut merge process is completed
|
|
102
|
+
SEGMENTATION_ANNOTATION_CUT_MERGE_PROCESS_COMPLETED:
|
|
103
|
+
'event::annotation_cut_merge_process_completed',
|
|
104
|
+
SEGMENTATION_STYLE_MODIFIED: 'event::segmentation_style_modified',
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const VALUE_TYPES = {};
|
|
108
|
+
|
|
109
|
+
// A selected segment's highlight: animating, or finished and still selected.
|
|
110
|
+
type SegmentHighlight = {
|
|
111
|
+
segmentationId: string;
|
|
112
|
+
segmentIndex: number;
|
|
113
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
114
|
+
state: 'highlighting' | 'selected';
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
const segmentHighlightKey = (
|
|
118
|
+
segmentationId: string,
|
|
119
|
+
segmentIndex: number,
|
|
120
|
+
type: csToolsEnums.SegmentationRepresentations
|
|
121
|
+
) => `${segmentationId}:${segmentIndex}:${type}`;
|
|
122
|
+
|
|
123
|
+
class SegmentationService extends PubSubService implements ISegmentationServiceInternals {
|
|
124
|
+
static REGISTRATION = {
|
|
125
|
+
name: 'segmentationService',
|
|
126
|
+
altName: 'SegmentationService',
|
|
127
|
+
create: ({ servicesManager }: OHIFTypes.Extensions.ExtensionParams): SegmentationService => {
|
|
128
|
+
return new SegmentationService({ servicesManager });
|
|
129
|
+
},
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
private _segmentationIdToColorLUTIndexMap: Map<string, number>;
|
|
133
|
+
private _segmentationGroupStatsMap: Map<string, any>;
|
|
134
|
+
private readonly _legacySegBackend: ISegmentationBackend;
|
|
135
|
+
private readonly _nextSegBackend: ISegmentationBackend;
|
|
136
|
+
readonly servicesManager: AppTypes.ServicesManager;
|
|
137
|
+
// The highlighted segment's animation state, keyed by segmentHighlightKey. A
|
|
138
|
+
// segment that is not selected has no entry. Each highlight runs as a
|
|
139
|
+
// requestAnimationFrame loop that stops as soon as its entry is no longer current.
|
|
140
|
+
private _segmentHighlights = new Map<string, SegmentHighlight>();
|
|
141
|
+
readonly EVENTS = EVENTS;
|
|
142
|
+
|
|
143
|
+
constructor({ servicesManager }) {
|
|
144
|
+
super(EVENTS);
|
|
145
|
+
|
|
146
|
+
this._segmentationIdToColorLUTIndexMap = new Map();
|
|
147
|
+
|
|
148
|
+
this.servicesManager = servicesManager;
|
|
149
|
+
|
|
150
|
+
this._segmentationGroupStatsMap = new Map();
|
|
151
|
+
|
|
152
|
+
// Segmentation backend twins (mirror the viewport backend family). Routed PER
|
|
153
|
+
// VIEWPORT via _segBackend() using the adapter's isNextViewport predicate,
|
|
154
|
+
// because a flag-on session can mix native and legacy viewports. Both are
|
|
155
|
+
// constructed eagerly:
|
|
156
|
+
// per-viewport dispatch has no per-session flag to defer on, and the twins read
|
|
157
|
+
// state at call time (post-init), not at construction.
|
|
158
|
+
this._legacySegBackend = new LegacySegmentationBackend(this);
|
|
159
|
+
this._nextSegBackend = new NextSegmentationBackend();
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Picks the segmentation backend lane for a specific viewport: the native
|
|
164
|
+
* ("next") twin for a raw GenericViewport (PlanarViewport), the legacy twin
|
|
165
|
+
* otherwise. Mirrors viewportOperations' per-viewport dispatch.
|
|
166
|
+
*/
|
|
167
|
+
private _segBackend(viewport: csTypes.IViewport): ISegmentationBackend {
|
|
168
|
+
return isNextViewport(viewport) ? this._nextSegBackend : this._legacySegBackend;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
public onModeEnter(): void {
|
|
172
|
+
this._initSegmentationService();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
public onModeExit(): void {
|
|
176
|
+
this.destroy();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Retrieves a segmentation by its ID.
|
|
181
|
+
*
|
|
182
|
+
* @param segmentationId - The unique identifier of the segmentation to retrieve.
|
|
183
|
+
* @returns The segmentation object if found, or undefined if not found.
|
|
184
|
+
*
|
|
185
|
+
* @remarks
|
|
186
|
+
* This method directly accesses the cornerstone tools segmentation state to fetch
|
|
187
|
+
* the segmentation data. It's useful when you need to access specific properties
|
|
188
|
+
* or perform operations on a particular segmentation.
|
|
189
|
+
*/
|
|
190
|
+
public getSegmentation(segmentationId: string): cstTypes.Segmentation | undefined {
|
|
191
|
+
return cstSegmentation.state.getSegmentation(segmentationId);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Retrieves all segmentations from the cornerstone tools segmentation state.
|
|
196
|
+
*
|
|
197
|
+
* @returns An array of all segmentations currently stored in the state
|
|
198
|
+
*
|
|
199
|
+
* @remarks
|
|
200
|
+
* This is a convenience method that directly accesses the cornerstone tools
|
|
201
|
+
* segmentation state to get all available segmentations. It returns the raw
|
|
202
|
+
* segmentation objects without any additional processing or filtering.
|
|
203
|
+
*/
|
|
204
|
+
public getSegmentations(): cstTypes.Segmentation[] | [] {
|
|
205
|
+
return cstSegmentation.state.getSegmentations();
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
public getPresentation(viewportId: string): SegmentationPresentation {
|
|
209
|
+
const segmentationPresentations: SegmentationPresentation = [];
|
|
210
|
+
const segmentationsMap = new Map<string, SegmentationPresentationItem>();
|
|
211
|
+
|
|
212
|
+
const representations = this.getSegmentationRepresentations(viewportId);
|
|
213
|
+
for (const representation of representations) {
|
|
214
|
+
if (!representation) {
|
|
215
|
+
continue;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
const { segmentationId, type } = representation;
|
|
219
|
+
|
|
220
|
+
segmentationsMap.set(segmentationId, {
|
|
221
|
+
segmentationId,
|
|
222
|
+
type,
|
|
223
|
+
hydrated: true,
|
|
224
|
+
config: representation.config || {},
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Check inside the removedDisplaySetAndRepresentationMaps to see if any of the representations are not hydrated
|
|
229
|
+
// const hydrationMap = this._segmentationRepresentationHydrationMaps.get(presentationId);
|
|
230
|
+
|
|
231
|
+
// if (hydrationMap) {
|
|
232
|
+
// hydrationMap.forEach(rep => {
|
|
233
|
+
// segmentationsMap.set(rep.segmentationId, {
|
|
234
|
+
// segmentationId: rep.segmentationId,
|
|
235
|
+
// type: rep.type,
|
|
236
|
+
// hydrated: rep.hydrated,
|
|
237
|
+
// config: rep.config || {},
|
|
238
|
+
// });
|
|
239
|
+
// });
|
|
240
|
+
// }
|
|
241
|
+
|
|
242
|
+
// // Convert the Map to an array
|
|
243
|
+
segmentationPresentations.push(...segmentationsMap.values());
|
|
244
|
+
|
|
245
|
+
return segmentationPresentations;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
public getRepresentationsForSegmentation(
|
|
249
|
+
segmentationId: string
|
|
250
|
+
): { viewportId: string; representations: any[] }[] {
|
|
251
|
+
const representations =
|
|
252
|
+
cstSegmentation.state.getSegmentationRepresentationsBySegmentationId(segmentationId);
|
|
253
|
+
|
|
254
|
+
return representations;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Retrieves segmentation representations (labelmap, contour, surface) based on specified criteria.
|
|
259
|
+
*
|
|
260
|
+
* @param viewportId - The ID of the viewport.
|
|
261
|
+
* @param specifier - An object containing optional `segmentationId` and `type` to filter the representations.
|
|
262
|
+
* @returns An array of `SegmentationRepresentation` matching the criteria, or an empty array if none are found.
|
|
263
|
+
*
|
|
264
|
+
* @remarks
|
|
265
|
+
* This method filters the segmentation representations according to the provided `specifier`:
|
|
266
|
+
* - **No `segmentationId` or `type` provided**: Returns all representations associated with the given `viewportId`.
|
|
267
|
+
* - **Only `segmentationId` provided**: Returns all representations with that `segmentationId`, regardless of `viewportId`.
|
|
268
|
+
* - **Only `type` provided**: Returns all representations of that `type` associated with the given `viewportId`.
|
|
269
|
+
* - **Both `segmentationId` and `type` provided**: Returns representations matching both criteria, regardless of `viewportId`.
|
|
270
|
+
*/
|
|
271
|
+
public getSegmentationRepresentations(
|
|
272
|
+
viewportId: string,
|
|
273
|
+
specifier: {
|
|
274
|
+
segmentationId?: string;
|
|
275
|
+
type?: csToolsEnums.SegmentationRepresentations;
|
|
276
|
+
} = {}
|
|
277
|
+
): SegmentationRepresentation[] {
|
|
278
|
+
// Get all representations for the viewportId
|
|
279
|
+
const representations = cstSegmentation.state.getSegmentationRepresentations(
|
|
280
|
+
viewportId,
|
|
281
|
+
specifier
|
|
282
|
+
);
|
|
283
|
+
|
|
284
|
+
// Map to our SegmentationRepresentation type
|
|
285
|
+
const ohifRepresentations = representations.map(repr =>
|
|
286
|
+
this._toOHIFSegmentationRepresentation(viewportId, repr)
|
|
287
|
+
);
|
|
288
|
+
|
|
289
|
+
return ohifRepresentations;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
public destroy = () => {
|
|
293
|
+
eventTarget.removeEventListener(
|
|
294
|
+
csToolsEnums.Events.SEGMENTATION_MODIFIED,
|
|
295
|
+
this._onSegmentationModifiedFromSource
|
|
296
|
+
);
|
|
297
|
+
|
|
298
|
+
eventTarget.removeEventListener(
|
|
299
|
+
csToolsEnums.Events.SEGMENTATION_REMOVED,
|
|
300
|
+
this._onSegmentationRemovedFromSource
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
eventTarget.removeEventListener(
|
|
304
|
+
csToolsEnums.Events.SEGMENTATION_DATA_MODIFIED,
|
|
305
|
+
this._onSegmentationDataModifiedFromSource
|
|
306
|
+
);
|
|
307
|
+
|
|
308
|
+
eventTarget.removeEventListener(
|
|
309
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_MODIFIED,
|
|
310
|
+
this._onSegmentationRepresentationModifiedFromSource
|
|
311
|
+
);
|
|
312
|
+
|
|
313
|
+
eventTarget.removeEventListener(
|
|
314
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_ADDED,
|
|
315
|
+
this._onSegmentationRepresentationModifiedFromSource
|
|
316
|
+
);
|
|
317
|
+
|
|
318
|
+
eventTarget.removeEventListener(
|
|
319
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_REMOVED,
|
|
320
|
+
this._onSegmentationRepresentationRemovedFromSource
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
eventTarget.removeEventListener(
|
|
324
|
+
csToolsEnums.Events.SEGMENTATION_ADDED,
|
|
325
|
+
this._onSegmentationAddedFromSource
|
|
326
|
+
);
|
|
327
|
+
|
|
328
|
+
this.reset();
|
|
329
|
+
};
|
|
330
|
+
|
|
331
|
+
public async addSegmentationRepresentation(
|
|
332
|
+
viewportId: string,
|
|
333
|
+
{
|
|
334
|
+
segmentationId,
|
|
335
|
+
predecessorImageId,
|
|
336
|
+
type,
|
|
337
|
+
config,
|
|
338
|
+
suppressEvents = false,
|
|
339
|
+
}: {
|
|
340
|
+
segmentationId: string;
|
|
341
|
+
predecessorImageId?: string;
|
|
342
|
+
type?: csToolsEnums.SegmentationRepresentations;
|
|
343
|
+
config?: {
|
|
344
|
+
blendMode?: csEnums.BlendModes;
|
|
345
|
+
useSliceRendering?: boolean;
|
|
346
|
+
};
|
|
347
|
+
suppressEvents?: boolean;
|
|
348
|
+
}
|
|
349
|
+
): Promise<void> {
|
|
350
|
+
const segmentation = this.getSegmentation(segmentationId);
|
|
351
|
+
|
|
352
|
+
if (!segmentation) {
|
|
353
|
+
console.warn(
|
|
354
|
+
`addSegmentationRepresentation: segmentation "${segmentationId}" is not in state yet`
|
|
355
|
+
);
|
|
356
|
+
return;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (!segmentation.predecessorImageId && predecessorImageId) {
|
|
360
|
+
segmentation.predecessorImageId = predecessorImageId;
|
|
361
|
+
}
|
|
362
|
+
const csViewport = this.getAndValidateViewport(viewportId);
|
|
363
|
+
|
|
364
|
+
if (!csViewport) {
|
|
365
|
+
return;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
// A stale/invalid segmentationId yields no segmentation; fail fast with a clear
|
|
369
|
+
// message instead of dereferencing representationData deep inside the backend
|
|
370
|
+
// classification below.
|
|
371
|
+
if (!segmentation) {
|
|
372
|
+
throw new Error(
|
|
373
|
+
`SegmentationService: cannot add representation - segmentation "${segmentationId}" not found.`
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
const colorLUTIndex = this._segmentationIdToColorLUTIndexMap.get(segmentationId);
|
|
378
|
+
|
|
379
|
+
let isConverted = false;
|
|
380
|
+
|
|
381
|
+
const defaultRepresentationType: csToolsEnums.SegmentationRepresentations =
|
|
382
|
+
isVolume3DViewportType(csViewport) ? SURFACE : LABELMAP;
|
|
383
|
+
let representationTypeToUse = type || defaultRepresentationType;
|
|
384
|
+
|
|
385
|
+
if (representationTypeToUse === LABELMAP) {
|
|
386
|
+
({ representationTypeToUse, isConverted } = await this._segBackend(
|
|
387
|
+
csViewport
|
|
388
|
+
).classifyAndPrepareLabelmapAdd(
|
|
389
|
+
csViewport,
|
|
390
|
+
segmentation,
|
|
391
|
+
viewportId,
|
|
392
|
+
segmentationId,
|
|
393
|
+
representationTypeToUse
|
|
394
|
+
));
|
|
395
|
+
|
|
396
|
+
// Overlap precondition: an overlapping SEG is registered as multiple labelmap
|
|
397
|
+
// layers, but cornerstone only stacks them (slice rendering) when the viewport
|
|
398
|
+
// renders as a volume slice (VTK_VOLUME_SLICE) — i.e. an MPR/volume viewport. On
|
|
399
|
+
// a stack/acquisition viewport the render plan falls back to a single layer, so
|
|
400
|
+
// only the primary group is visible. Warn rather than fail silently.
|
|
401
|
+
const labelmapLayers = segmentation?.representationData?.[LABELMAP]?.labelmaps;
|
|
402
|
+
const isOverlapping = labelmapLayers && Object.keys(labelmapLayers).length > 1;
|
|
403
|
+
if (
|
|
404
|
+
isOverlapping &&
|
|
405
|
+
isNextViewport(csViewport) &&
|
|
406
|
+
!csUtils.viewportIsInVolumeMode(csViewport)
|
|
407
|
+
) {
|
|
408
|
+
console.warn(
|
|
409
|
+
`Overlapping segmentation ${segmentationId} has multiple labelmap layers, but ` +
|
|
410
|
+
`viewport ${viewportId} does not render as a volume slice (VTK_VOLUME_SLICE); ` +
|
|
411
|
+
`only the primary layer will be visible. Display the segmentation in an ` +
|
|
412
|
+
`MPR/volume layout to see all overlapping segments.`
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
await this._addSegmentationRepresentation(
|
|
418
|
+
viewportId,
|
|
419
|
+
segmentationId,
|
|
420
|
+
representationTypeToUse,
|
|
421
|
+
colorLUTIndex,
|
|
422
|
+
isConverted,
|
|
423
|
+
config
|
|
424
|
+
);
|
|
425
|
+
|
|
426
|
+
if (!suppressEvents) {
|
|
427
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED, { segmentationId });
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Creates an labelmap segmentation for a given display set
|
|
433
|
+
*
|
|
434
|
+
* @param displaySet - The display set to create the segmentation for.
|
|
435
|
+
* @param options - Optional parameters for creating the segmentation.
|
|
436
|
+
* @param options.segmentationId - Custom segmentation ID. If not provided, a UUID will be generated.
|
|
437
|
+
* @param options.FrameOfReferenceUID - Frame of reference UID for the segmentation.
|
|
438
|
+
* @param options.label - Label for the segmentation.
|
|
439
|
+
* @returns A promise that resolves to the created segmentation ID.
|
|
440
|
+
*/
|
|
441
|
+
public async createLabelmapForDisplaySet(
|
|
442
|
+
displaySet: AppTypes.DisplaySet,
|
|
443
|
+
options?: {
|
|
444
|
+
segmentationId?: string;
|
|
445
|
+
segments?: { [segmentIndex: number]: Partial<cstTypes.Segment> };
|
|
446
|
+
FrameOfReferenceUID?: string;
|
|
447
|
+
label?: string;
|
|
448
|
+
/** The caller invented the label, so the user has not chosen a name. */
|
|
449
|
+
labelIsGenerated?: boolean;
|
|
450
|
+
}
|
|
451
|
+
): Promise<string> {
|
|
452
|
+
return this._createSegmentationForDisplaySet(displaySet, LABELMAP, options);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
public async createContourForDisplaySet(
|
|
456
|
+
displaySet: AppTypes.DisplaySet,
|
|
457
|
+
options?: {
|
|
458
|
+
segmentationId?: string;
|
|
459
|
+
segments?: { [segmentIndex: number]: Partial<cstTypes.Segment> };
|
|
460
|
+
FrameOfReferenceUID?: string;
|
|
461
|
+
label?: string;
|
|
462
|
+
/** The caller invented the label, so the user has not chosen a name. */
|
|
463
|
+
labelIsGenerated?: boolean;
|
|
464
|
+
}
|
|
465
|
+
): Promise<string> {
|
|
466
|
+
return this._createSegmentationForDisplaySet(displaySet, CONTOUR, options);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
/**
|
|
470
|
+
* Private method to create segmentation for a display set with the specified type
|
|
471
|
+
*
|
|
472
|
+
* @param displaySet - The display set to create the segmentation for
|
|
473
|
+
* @param segmentationType - The type of segmentation (SegmentationRepresentations enum)
|
|
474
|
+
* @param options - Optional parameters for creating the segmentation
|
|
475
|
+
* @returns A promise that resolves to the created segmentation ID
|
|
476
|
+
*/
|
|
477
|
+
private async _createSegmentationForDisplaySet(
|
|
478
|
+
displaySet: AppTypes.DisplaySet,
|
|
479
|
+
segmentationType: SegmentationRepresentations,
|
|
480
|
+
options?: {
|
|
481
|
+
segmentationId?: string;
|
|
482
|
+
segments?: { [segmentIndex: number]: Partial<cstTypes.Segment> };
|
|
483
|
+
FrameOfReferenceUID?: string;
|
|
484
|
+
label?: string;
|
|
485
|
+
/** The caller invented the label, so the user has not chosen a name. */
|
|
486
|
+
labelIsGenerated?: boolean;
|
|
487
|
+
}
|
|
488
|
+
): Promise<string> {
|
|
489
|
+
// Todo: random does not makes sense, make this better, like
|
|
490
|
+
// labelmap 1, 2, 3 etc
|
|
491
|
+
const segmentationId = options?.segmentationId ?? `${csUtils.uuidv4()}`;
|
|
492
|
+
|
|
493
|
+
const isDynamicVolume = displaySet.isDynamicVolume;
|
|
494
|
+
|
|
495
|
+
let referenceImageIds = displaySet.imageIds;
|
|
496
|
+
if (isDynamicVolume) {
|
|
497
|
+
// get the middle timepoint for referenceImageIds
|
|
498
|
+
const timePoints = displaySet.dynamicVolumeInfo.timePoints;
|
|
499
|
+
const middleTimePoint = timePoints[Math.floor(timePoints.length / 2)];
|
|
500
|
+
referenceImageIds = middleTimePoint;
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
const derivedImages = await imageLoader.createAndCacheDerivedLabelmapImages(referenceImageIds);
|
|
504
|
+
|
|
505
|
+
const segs = this.getSegmentations();
|
|
506
|
+
const label = options?.label || `Segmentation ${segs.length + 1}`;
|
|
507
|
+
|
|
508
|
+
const segImageIds = derivedImages.map(image => image.imageId);
|
|
509
|
+
|
|
510
|
+
const segmentationPublicInput: cstTypes.SegmentationPublicInput = {
|
|
511
|
+
segmentationId,
|
|
512
|
+
representation: {
|
|
513
|
+
type: segmentationType,
|
|
514
|
+
data: {
|
|
515
|
+
imageIds: segImageIds,
|
|
516
|
+
// referencedVolumeId: this._getVolumeIdForDisplaySet(displaySet),
|
|
517
|
+
referencedImageIds: referenceImageIds,
|
|
518
|
+
},
|
|
519
|
+
},
|
|
520
|
+
config: {
|
|
521
|
+
label,
|
|
522
|
+
// Explicit, because `label` below always has a value by this point.
|
|
523
|
+
labelIsGenerated: options?.labelIsGenerated ?? !options?.label,
|
|
524
|
+
fallbackLabel: `S:${displaySet.SeriesNumber} ${displaySet.Modality}`,
|
|
525
|
+
segments:
|
|
526
|
+
options?.segments && Object.keys(options.segments).length > 0
|
|
527
|
+
? options.segments
|
|
528
|
+
: {
|
|
529
|
+
1: {
|
|
530
|
+
label: `${i18n.t('Segment')} 1`,
|
|
531
|
+
active: true,
|
|
532
|
+
},
|
|
533
|
+
},
|
|
534
|
+
cachedStats: {
|
|
535
|
+
info: `S${displaySet.SeriesNumber}: ${displaySet.SeriesDescription}`,
|
|
536
|
+
},
|
|
537
|
+
},
|
|
538
|
+
};
|
|
539
|
+
|
|
540
|
+
// Create a dedicated color LUT up front and remember its index so that every
|
|
541
|
+
// representation of this segmentation (one per viewport) reuses the same LUT.
|
|
542
|
+
// Otherwise each viewport would get its own default LUT copy and editing a
|
|
543
|
+
// segment color on one viewport would not be reflected on the others (the
|
|
544
|
+
// segment color appears to revert to the default when interacting elsewhere).
|
|
545
|
+
// The caller may pass the id of an existing segmentation, which this method
|
|
546
|
+
// updates rather than replaces; keep its LUT so representations already
|
|
547
|
+
// rendering it don't diverge from the ones created afterwards.
|
|
548
|
+
if (!this._segmentationIdToColorLUTIndexMap.has(segmentationId)) {
|
|
549
|
+
const colorLUTIndex = addColorLUT([[0, 0, 0, 0]] as csTypes.ColorLUT);
|
|
550
|
+
this._segmentationIdToColorLUTIndexMap.set(segmentationId, colorLUTIndex);
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
this.addOrUpdateSegmentation(segmentationPublicInput);
|
|
554
|
+
return segmentationId;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
public async createSegmentationForSEGDisplaySet(
|
|
558
|
+
segDisplaySet,
|
|
559
|
+
options: {
|
|
560
|
+
segmentationId?: string;
|
|
561
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
562
|
+
} = {
|
|
563
|
+
type: LABELMAP,
|
|
564
|
+
}
|
|
565
|
+
): Promise<string> {
|
|
566
|
+
const { type } = options;
|
|
567
|
+
let { segmentationId } = options;
|
|
568
|
+
const { labelMapImages } = segDisplaySet;
|
|
569
|
+
|
|
570
|
+
if (type !== LABELMAP) {
|
|
571
|
+
throw new Error('Only labelmap type is supported for SEG display sets right now');
|
|
572
|
+
}
|
|
573
|
+
|
|
574
|
+
if (!labelMapImages || !labelMapImages.length) {
|
|
575
|
+
throw new Error('SEG reading failed');
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
segmentationId = segmentationId ?? segDisplaySet.displaySetInstanceUID;
|
|
579
|
+
const referencedDisplaySetInstanceUID = segDisplaySet.referencedDisplaySetInstanceUID;
|
|
580
|
+
const referencedDisplaySet = this.servicesManager.services.displaySetService.getDisplaySetByUID(
|
|
581
|
+
referencedDisplaySetInstanceUID
|
|
582
|
+
);
|
|
583
|
+
|
|
584
|
+
const images = referencedDisplaySet.instances;
|
|
585
|
+
|
|
586
|
+
if (!images.length) {
|
|
587
|
+
throw new Error('No instances were provided for the referenced display set of the SEG');
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Use the same imageIds as SEG parse (_loadSegments stores these on segDisplaySet).
|
|
591
|
+
const imageIds =
|
|
592
|
+
segDisplaySet.referencedImageIds ||
|
|
593
|
+
(referencedDisplaySet.imageIds as string[] | undefined) ||
|
|
594
|
+
images.map(image => image.imageId);
|
|
595
|
+
|
|
596
|
+
if (!imageIds?.length) {
|
|
597
|
+
throw new Error('referencedDisplaySet has no imageIds for SEG');
|
|
598
|
+
}
|
|
599
|
+
const derivedImages = labelMapImages?.flat();
|
|
600
|
+
const derivedImageIds = derivedImages.map(image => image.imageId);
|
|
601
|
+
|
|
602
|
+
// Note: instance runtime props (frameNumber, imageId, url, ...) are
|
|
603
|
+
// intentionally non-enumerable, so this spread deliberately does NOT copy
|
|
604
|
+
// them — frameNumber must not be carried onto these derived image entries.
|
|
605
|
+
// Read such props off the original instance, never off a copy.
|
|
606
|
+
segDisplaySet.images = derivedImages.map(image => ({
|
|
607
|
+
...image,
|
|
608
|
+
...metaData.get('instance', image.referencedImageId),
|
|
609
|
+
}));
|
|
610
|
+
|
|
611
|
+
segDisplaySet.imageIds = derivedImageIds;
|
|
612
|
+
|
|
613
|
+
// We should parse the segmentation as separate slices to support overlapping segments.
|
|
614
|
+
// This parsing should occur in the CornerstoneJS library adapters.
|
|
615
|
+
// For now, we use the volume returned from the library and chop it here.
|
|
616
|
+
let firstSegmentedSliceImageId = null;
|
|
617
|
+
for (let i = 0; i < derivedImages.length; i++) {
|
|
618
|
+
const voxelManager = derivedImages[i].voxelManager as csTypes.IVoxelManager<number>;
|
|
619
|
+
const scalarData = voxelManager.getScalarData();
|
|
620
|
+
voxelManager.setScalarData(scalarData);
|
|
621
|
+
|
|
622
|
+
// Check if this slice has any non-zero voxels and we haven't found one yet
|
|
623
|
+
if (!firstSegmentedSliceImageId && scalarData.some(value => value !== 0)) {
|
|
624
|
+
firstSegmentedSliceImageId = derivedImages[i].referencedImageId;
|
|
625
|
+
}
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
// assign the first non zero voxel image id to the segDisplaySet
|
|
629
|
+
segDisplaySet.firstSegmentedSliceImageId = firstSegmentedSliceImageId;
|
|
630
|
+
|
|
631
|
+
const segmentsInfo = segDisplaySet.segMetadata.data;
|
|
632
|
+
|
|
633
|
+
const segments: { [segmentIndex: string]: cstTypes.Segment } = {};
|
|
634
|
+
const colorLUT = [];
|
|
635
|
+
|
|
636
|
+
segmentsInfo.forEach((segmentInfo, index) => {
|
|
637
|
+
if (index === 0) {
|
|
638
|
+
colorLUT.push([0, 0, 0, 0]);
|
|
639
|
+
return;
|
|
640
|
+
}
|
|
641
|
+
|
|
642
|
+
const {
|
|
643
|
+
SegmentedPropertyCategoryCodeSequence,
|
|
644
|
+
SegmentNumber,
|
|
645
|
+
SegmentLabel,
|
|
646
|
+
SegmentAlgorithmType,
|
|
647
|
+
SegmentAlgorithmName,
|
|
648
|
+
SegmentedPropertyTypeCodeSequence,
|
|
649
|
+
rgba,
|
|
650
|
+
} = segmentInfo;
|
|
651
|
+
|
|
652
|
+
colorLUT.push(rgba);
|
|
653
|
+
|
|
654
|
+
const segmentIndex = Number(SegmentNumber);
|
|
655
|
+
|
|
656
|
+
const centroid = segDisplaySet.centroids?.get(index);
|
|
657
|
+
const imageCentroidXYZ = centroid?.image || { x: 0, y: 0, z: 0 };
|
|
658
|
+
const worldCentroidXYZ = centroid?.world || { x: 0, y: 0, z: 0 };
|
|
659
|
+
|
|
660
|
+
segments[segmentIndex] = {
|
|
661
|
+
segmentIndex,
|
|
662
|
+
label: SegmentLabel || `Segment ${SegmentNumber}`,
|
|
663
|
+
locked: false,
|
|
664
|
+
active: false,
|
|
665
|
+
cachedStats: {
|
|
666
|
+
center: {
|
|
667
|
+
image: [imageCentroidXYZ.x, imageCentroidXYZ.y, imageCentroidXYZ.z],
|
|
668
|
+
world: [worldCentroidXYZ.x, worldCentroidXYZ.y, worldCentroidXYZ.z],
|
|
669
|
+
},
|
|
670
|
+
modifiedTime: segDisplaySet.SeriesDate,
|
|
671
|
+
category: SegmentedPropertyCategoryCodeSequence
|
|
672
|
+
? SegmentedPropertyCategoryCodeSequence.CodeMeaning
|
|
673
|
+
: '',
|
|
674
|
+
type: SegmentedPropertyTypeCodeSequence
|
|
675
|
+
? SegmentedPropertyTypeCodeSequence.CodeMeaning
|
|
676
|
+
: '',
|
|
677
|
+
algorithmType: SegmentAlgorithmType,
|
|
678
|
+
algorithmName: SegmentAlgorithmName,
|
|
679
|
+
},
|
|
680
|
+
};
|
|
681
|
+
});
|
|
682
|
+
|
|
683
|
+
const colorLUTIndex = addColorLUT(colorLUT);
|
|
684
|
+
this._segmentationIdToColorLUTIndexMap.set(segmentationId, colorLUTIndex);
|
|
685
|
+
|
|
686
|
+
// Build the segmentation input via the backend twin. At SEG-load there is no
|
|
687
|
+
// target viewport yet, so the lane is chosen by the session flag (the one
|
|
688
|
+
// viewport-less seg-backend dispatch): the next twin registers overlapping SEGs
|
|
689
|
+
// as multiple labelmap layers (slice rendering); the legacy twin keeps the single
|
|
690
|
+
// flattened layer (byte-identical).
|
|
691
|
+
const segBackend = isNextViewportsEnabled() ? this._nextSegBackend : this._legacySegBackend;
|
|
692
|
+
const seg = segBackend.assembleSegmentationDataForSEG({
|
|
693
|
+
segmentationId,
|
|
694
|
+
segDisplaySet,
|
|
695
|
+
derivedImageIds,
|
|
696
|
+
referencedImageIds: imageIds as string[],
|
|
697
|
+
label: segDisplaySet.SeriesDescription,
|
|
698
|
+
fallbackLabel: `S:${segDisplaySet.SeriesNumber} ${segDisplaySet.Modality}`,
|
|
699
|
+
segments,
|
|
700
|
+
});
|
|
701
|
+
|
|
702
|
+
segDisplaySet.isLoaded = true;
|
|
703
|
+
|
|
704
|
+
// Add the segmentation to cornerstone state BEFORE broadcasting that loading is
|
|
705
|
+
// complete. Subscribers (e.g. CornerstoneViewportService) react synchronously and
|
|
706
|
+
// call addSegmentationRepresentation, which now early-returns when the segmentation
|
|
707
|
+
// is not yet in cornerstone state. Broadcasting first would make that guard always
|
|
708
|
+
// fire on initial load, silently preventing the representation from being attached.
|
|
709
|
+
this.addOrUpdateSegmentation(seg);
|
|
710
|
+
|
|
711
|
+
this._broadcastEvent(EVENTS.SEGMENTATION_LOADING_COMPLETE, {
|
|
712
|
+
segmentationId,
|
|
713
|
+
segDisplaySet,
|
|
714
|
+
});
|
|
715
|
+
|
|
716
|
+
return segmentationId;
|
|
717
|
+
}
|
|
718
|
+
|
|
719
|
+
public async createSegmentationForRTDisplaySet(
|
|
720
|
+
rtDisplaySet,
|
|
721
|
+
options: {
|
|
722
|
+
segmentationId?: string;
|
|
723
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
724
|
+
} = {
|
|
725
|
+
type: CONTOUR,
|
|
726
|
+
}
|
|
727
|
+
): Promise<string> {
|
|
728
|
+
const { type } = options;
|
|
729
|
+
let { segmentationId } = options;
|
|
730
|
+
|
|
731
|
+
// Currently, only contour representation is supported for RT display
|
|
732
|
+
if (type !== CONTOUR) {
|
|
733
|
+
throw new Error('Only contour type is supported for RT display sets right now');
|
|
734
|
+
}
|
|
735
|
+
|
|
736
|
+
// Assign segmentationId if not provided
|
|
737
|
+
segmentationId = segmentationId ?? rtDisplaySet.displaySetInstanceUID;
|
|
738
|
+
const { structureSet } = rtDisplaySet;
|
|
739
|
+
|
|
740
|
+
if (!structureSet) {
|
|
741
|
+
throw new Error(
|
|
742
|
+
'To create the contours from RT displaySet, the displaySet should be loaded first. You can perform rtDisplaySet.load() before calling this method.'
|
|
743
|
+
);
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
const rtDisplaySetUID = rtDisplaySet.displaySetInstanceUID;
|
|
747
|
+
const referencedDisplaySet = this.servicesManager.services.displaySetService.getDisplaySetByUID(
|
|
748
|
+
rtDisplaySet.referencedDisplaySetInstanceUID
|
|
749
|
+
);
|
|
750
|
+
|
|
751
|
+
const referencedImageIdsWithGeometry = Array.from(structureSet.ReferencedSOPInstanceUIDsSet);
|
|
752
|
+
|
|
753
|
+
const referencedImageIds = referencedDisplaySet.imageIds;
|
|
754
|
+
// find the first image id that contains a referenced SOP instance UID
|
|
755
|
+
const firstSegmentedSliceImageId =
|
|
756
|
+
referencedImageIds?.find(imageId =>
|
|
757
|
+
referencedImageIdsWithGeometry.some(referencedId =>
|
|
758
|
+
imageId.includes(referencedId as string)
|
|
759
|
+
)
|
|
760
|
+
) || null;
|
|
761
|
+
|
|
762
|
+
rtDisplaySet.firstSegmentedSliceImageId = firstSegmentedSliceImageId;
|
|
763
|
+
|
|
764
|
+
if (!structureSet.ROIContours?.length) {
|
|
765
|
+
throw new Error(
|
|
766
|
+
'The structureSet does not contain any ROIContours. Please ensure the structureSet is loaded first.'
|
|
767
|
+
);
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
// Map ROI contours to RT Struct Data
|
|
771
|
+
const allRTStructData = mapROIContoursToRTStructData(structureSet, rtDisplaySetUID);
|
|
772
|
+
|
|
773
|
+
// Sort by segmentIndex for consistency
|
|
774
|
+
allRTStructData.sort((a, b) => a.segmentIndex - b.segmentIndex);
|
|
775
|
+
|
|
776
|
+
const geometryIds = allRTStructData.map(({ geometryId }) => geometryId);
|
|
777
|
+
|
|
778
|
+
// Initialize SegmentationPublicInput similar to SEG function
|
|
779
|
+
const segmentation: cstTypes.SegmentationPublicInput = {
|
|
780
|
+
segmentationId,
|
|
781
|
+
representation: {
|
|
782
|
+
type: CONTOUR,
|
|
783
|
+
data: {
|
|
784
|
+
geometryIds,
|
|
785
|
+
},
|
|
786
|
+
},
|
|
787
|
+
config: {
|
|
788
|
+
label: rtDisplaySet.SeriesDescription,
|
|
789
|
+
fallbackLabel: `S:${rtDisplaySet.SeriesNumber} ${rtDisplaySet.Modality}`,
|
|
790
|
+
},
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
const segments: { [segmentIndex: string]: cstTypes.Segment } = {};
|
|
794
|
+
let segmentsCachedStats = {};
|
|
795
|
+
|
|
796
|
+
// Create colorLUT array for RT structures
|
|
797
|
+
const colorLUT = [[0, 0, 0, 0]]; // First entry is transparent for index 0
|
|
798
|
+
|
|
799
|
+
// Process each segment similarly to the SEG function
|
|
800
|
+
for (let i = 0; i < allRTStructData.length; i++) {
|
|
801
|
+
const rtStructData = allRTStructData[i];
|
|
802
|
+
|
|
803
|
+
const { data, id, color, segmentIndex, geometryId, group } = rtStructData;
|
|
804
|
+
|
|
805
|
+
// Add the color to the colorLUT array
|
|
806
|
+
colorLUT.push(color);
|
|
807
|
+
|
|
808
|
+
try {
|
|
809
|
+
const geometry = await geometryLoader.createAndCacheGeometry(geometryId, {
|
|
810
|
+
geometryData: {
|
|
811
|
+
data,
|
|
812
|
+
id,
|
|
813
|
+
color,
|
|
814
|
+
frameOfReferenceUID: structureSet.frameOfReferenceUID,
|
|
815
|
+
segmentIndex,
|
|
816
|
+
},
|
|
817
|
+
type: csEnums.GeometryType.CONTOUR,
|
|
818
|
+
});
|
|
819
|
+
|
|
820
|
+
const contourSet = geometry.data as csTypes.IContourSet;
|
|
821
|
+
const centroid = contourSet.centroid;
|
|
822
|
+
|
|
823
|
+
segmentsCachedStats = {
|
|
824
|
+
center: { world: centroid },
|
|
825
|
+
modifiedTime: rtDisplaySet.SeriesDate, // Using SeriesDate as modifiedTime
|
|
826
|
+
};
|
|
827
|
+
|
|
828
|
+
segments[segmentIndex] = {
|
|
829
|
+
label: id,
|
|
830
|
+
segmentIndex,
|
|
831
|
+
cachedStats: segmentsCachedStats,
|
|
832
|
+
locked: false,
|
|
833
|
+
active: false,
|
|
834
|
+
group,
|
|
835
|
+
};
|
|
836
|
+
|
|
837
|
+
// Broadcast segment loading progress
|
|
838
|
+
const numInitialized = Object.keys(segmentsCachedStats).length;
|
|
839
|
+
const percentComplete = Math.round((numInitialized / allRTStructData.length) * 100);
|
|
840
|
+
this._broadcastEvent(EVENTS.SEGMENT_LOADING_COMPLETE, {
|
|
841
|
+
percentComplete,
|
|
842
|
+
numSegments: allRTStructData.length,
|
|
843
|
+
});
|
|
844
|
+
} catch (e) {
|
|
845
|
+
console.warn(`Error initializing contour for segment ${segmentIndex}:`, e);
|
|
846
|
+
continue; // Continue processing other segments even if one fails
|
|
847
|
+
}
|
|
848
|
+
}
|
|
849
|
+
|
|
850
|
+
// Create and register the colorLUT
|
|
851
|
+
const colorLUTIndex = addColorLUT(colorLUT);
|
|
852
|
+
this._segmentationIdToColorLUTIndexMap.set(segmentationId, colorLUTIndex);
|
|
853
|
+
|
|
854
|
+
// Assign processed segments to segmentation config
|
|
855
|
+
segmentation.config.segments = segments;
|
|
856
|
+
|
|
857
|
+
// Broadcast segmentation loading complete event
|
|
858
|
+
this._broadcastEvent(EVENTS.SEGMENTATION_LOADING_COMPLETE, {
|
|
859
|
+
segmentationId,
|
|
860
|
+
rtDisplaySet,
|
|
861
|
+
});
|
|
862
|
+
|
|
863
|
+
// Mark the RT display set as loaded
|
|
864
|
+
rtDisplaySet.isLoaded = true;
|
|
865
|
+
// Add or update the segmentation in the state
|
|
866
|
+
this.addOrUpdateSegmentation(segmentation);
|
|
867
|
+
|
|
868
|
+
return segmentationId;
|
|
869
|
+
}
|
|
870
|
+
|
|
871
|
+
/**
|
|
872
|
+
* Adds or updates a segmentation in the state
|
|
873
|
+
* @param segmentationId - The ID of the segmentation to add or update
|
|
874
|
+
* @param data - The data to add or update the segmentation with
|
|
875
|
+
*
|
|
876
|
+
* @remarks
|
|
877
|
+
* This method handles the addition or update of a segmentation in the state.
|
|
878
|
+
* If the segmentation already exists, it updates the existing segmentation.
|
|
879
|
+
* If the segmentation does not exist, it adds a new segmentation.
|
|
880
|
+
*/
|
|
881
|
+
public addOrUpdateSegmentation(
|
|
882
|
+
data: cstTypes.SegmentationPublicInput | Partial<cstTypes.Segmentation>
|
|
883
|
+
) {
|
|
884
|
+
const segmentationId = data.segmentationId;
|
|
885
|
+
const existingSegmentation = cstSegmentation.state.getSegmentation(segmentationId);
|
|
886
|
+
|
|
887
|
+
if (existingSegmentation) {
|
|
888
|
+
// Update the existing segmentation
|
|
889
|
+
this.updateSegmentationInSource(segmentationId, data as Partial<cstTypes.Segmentation>);
|
|
890
|
+
} else if (
|
|
891
|
+
'representation' in data &&
|
|
892
|
+
(data as cstTypes.SegmentationPublicInput).representation
|
|
893
|
+
) {
|
|
894
|
+
// Add a new segmentation
|
|
895
|
+
this.addSegmentationToSource(data as cstTypes.SegmentationPublicInput);
|
|
896
|
+
} else {
|
|
897
|
+
console.warn(
|
|
898
|
+
`addOrUpdateSegmentation: skipping add for ${segmentationId} — missing representation`
|
|
899
|
+
);
|
|
900
|
+
}
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
public setActiveSegmentation(viewportId: string, segmentationId: string): void {
|
|
904
|
+
cstSegmentation.activeSegmentation.setActiveSegmentation(viewportId, segmentationId);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
/**
|
|
908
|
+
* Gets the active segmentation for a viewport
|
|
909
|
+
* @param viewportId - The ID of the viewport to get the active segmentation for
|
|
910
|
+
* @returns The active segmentation object, or null if no segmentation is active
|
|
911
|
+
*
|
|
912
|
+
* @remarks
|
|
913
|
+
* This method retrieves the currently active segmentation for the specified viewport.
|
|
914
|
+
* The active segmentation is the one that is currently selected for editing operations.
|
|
915
|
+
* Returns null if no segmentation is active in the viewport.
|
|
916
|
+
*/
|
|
917
|
+
public getActiveSegmentation(viewportId: string): cstTypes.Segmentation | null {
|
|
918
|
+
return cstSegmentation.activeSegmentation.getActiveSegmentation(viewportId);
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
/**
|
|
922
|
+
* Gets the active segment from the active segmentation in a viewport
|
|
923
|
+
* @param viewportId - The ID of the viewport to get the active segment from
|
|
924
|
+
* @returns The active segment object, or undefined if no segment is active
|
|
925
|
+
*
|
|
926
|
+
* @remarks
|
|
927
|
+
* This method retrieves the currently active segment from the active segmentation
|
|
928
|
+
* in the specified viewport. The active segment is the one that is currently
|
|
929
|
+
* selected for editing operations. Returns undefined if no segment is active or
|
|
930
|
+
* if there is no active segmentation.
|
|
931
|
+
*/
|
|
932
|
+
public getActiveSegment(viewportId: string): cstTypes.Segment | undefined {
|
|
933
|
+
const activeSegmentation = this.getActiveSegmentation(viewportId);
|
|
934
|
+
|
|
935
|
+
if (!activeSegmentation) {
|
|
936
|
+
return;
|
|
937
|
+
}
|
|
938
|
+
|
|
939
|
+
const { segments } = activeSegmentation;
|
|
940
|
+
|
|
941
|
+
let activeSegment;
|
|
942
|
+
for (const segment of Object.values(segments)) {
|
|
943
|
+
if (segment.active) {
|
|
944
|
+
activeSegment = segment;
|
|
945
|
+
break;
|
|
946
|
+
}
|
|
947
|
+
}
|
|
948
|
+
|
|
949
|
+
return activeSegment;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
public hasCustomStyles(specifier: {
|
|
953
|
+
viewportId: string;
|
|
954
|
+
segmentationId: string;
|
|
955
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
956
|
+
}): boolean {
|
|
957
|
+
return cstSegmentation.config.style.hasCustomStyle(specifier);
|
|
958
|
+
}
|
|
959
|
+
|
|
960
|
+
public getStyle = (specifier: {
|
|
961
|
+
viewportId: string;
|
|
962
|
+
segmentationId: string;
|
|
963
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
964
|
+
segmentIndex?: number;
|
|
965
|
+
}) => {
|
|
966
|
+
const style = cstSegmentation.config.style.getStyle(specifier);
|
|
967
|
+
|
|
968
|
+
return style;
|
|
969
|
+
};
|
|
970
|
+
|
|
971
|
+
public setStyle = (
|
|
972
|
+
specifier: {
|
|
973
|
+
type: csToolsEnums.SegmentationRepresentations;
|
|
974
|
+
viewportId?: string;
|
|
975
|
+
segmentationId?: string;
|
|
976
|
+
segmentIndex?: number;
|
|
977
|
+
},
|
|
978
|
+
style: cstTypes.LabelmapStyle | cstTypes.ContourStyle | cstTypes.SurfaceStyle,
|
|
979
|
+
merge: boolean = true
|
|
980
|
+
) => {
|
|
981
|
+
cstSegmentation.config.style.setStyle(specifier, style, merge);
|
|
982
|
+
this._broadcastEvent(EVENTS.SEGMENTATION_STYLE_MODIFIED, {
|
|
983
|
+
specifier,
|
|
984
|
+
style,
|
|
985
|
+
merge,
|
|
986
|
+
});
|
|
987
|
+
};
|
|
988
|
+
|
|
989
|
+
public resetToGlobalStyle = () => {
|
|
990
|
+
cstSegmentation.config.style.resetToGlobalStyle();
|
|
991
|
+
};
|
|
992
|
+
|
|
993
|
+
public getNextAvailableSegmentIndex(segmentationId: string): number {
|
|
994
|
+
const csSegmentation = this.getCornerstoneSegmentation(segmentationId);
|
|
995
|
+
// grab the next available segment index based on the object keys,
|
|
996
|
+
// so basically get the highest segment index value + 1
|
|
997
|
+
|
|
998
|
+
const segmentKeys = Object.keys(csSegmentation.segments);
|
|
999
|
+
return segmentKeys.length === 0 ? 1 : Math.max(...segmentKeys.map(Number)) + 1;
|
|
1000
|
+
}
|
|
1001
|
+
|
|
1002
|
+
/**
|
|
1003
|
+
* Adds a new segment to the specified segmentation.
|
|
1004
|
+
* @param segmentationId - The ID of the segmentation to add the segment to.
|
|
1005
|
+
* @param viewportId: The ID of the viewport to add the segment to, it is used to get the representation, if it is not
|
|
1006
|
+
* provided, the first available representation for the segmentationId will be used.
|
|
1007
|
+
* @param config - An object containing the configuration options for the new segment.
|
|
1008
|
+
* - segmentIndex: (optional) The index of the segment to add. If not provided, the next available index will be used.
|
|
1009
|
+
* - properties: (optional) An object containing the properties of the new segment.
|
|
1010
|
+
* - label: (optional) The label of the new segment. If not provided, a default label will be used.
|
|
1011
|
+
* - color: (optional) The color of the new segment in RGB format. If not provided, a default color will be used.
|
|
1012
|
+
* - visibility: (optional) Whether the new segment should be visible. If not provided, the segment will be visible by default.
|
|
1013
|
+
* - isLocked: (optional) Whether the new segment should be locked for editing. If not provided, the segment will not be locked by default.
|
|
1014
|
+
* - active: (optional) Whether the new segment should be the active segment to be edited. If not provided, the segment will not be active by default.
|
|
1015
|
+
*/
|
|
1016
|
+
public addSegment(
|
|
1017
|
+
segmentationId: string,
|
|
1018
|
+
config: {
|
|
1019
|
+
segmentIndex?: number;
|
|
1020
|
+
label?: string;
|
|
1021
|
+
isLocked?: boolean;
|
|
1022
|
+
active?: boolean;
|
|
1023
|
+
color?: csTypes.Color; // Add color type
|
|
1024
|
+
visibility?: boolean; // Add visibility option
|
|
1025
|
+
cachedStats?: Record<string, unknown>;
|
|
1026
|
+
} = {}
|
|
1027
|
+
): void {
|
|
1028
|
+
if (config?.segmentIndex === 0) {
|
|
1029
|
+
throw new Error(i18n.t('Segment') + ' index 0 is reserved for "no label"');
|
|
1030
|
+
}
|
|
1031
|
+
|
|
1032
|
+
const csSegmentation = this.getCornerstoneSegmentation(segmentationId);
|
|
1033
|
+
|
|
1034
|
+
let segmentIndex = config.segmentIndex;
|
|
1035
|
+
if (!segmentIndex) {
|
|
1036
|
+
segmentIndex = this.getNextAvailableSegmentIndex(segmentationId);
|
|
1037
|
+
}
|
|
1038
|
+
|
|
1039
|
+
// update the segmentation
|
|
1040
|
+
if (!config.label) {
|
|
1041
|
+
config.label = `${i18n.t('Segment')} ${segmentIndex}`;
|
|
1042
|
+
}
|
|
1043
|
+
|
|
1044
|
+
const currentSegments = csSegmentation.segments;
|
|
1045
|
+
|
|
1046
|
+
cstSegmentation.updateSegmentations([
|
|
1047
|
+
{
|
|
1048
|
+
segmentationId,
|
|
1049
|
+
payload: {
|
|
1050
|
+
segments: {
|
|
1051
|
+
...currentSegments,
|
|
1052
|
+
[segmentIndex]: {
|
|
1053
|
+
...currentSegments[segmentIndex],
|
|
1054
|
+
segmentIndex,
|
|
1055
|
+
cachedStats: {},
|
|
1056
|
+
locked: false,
|
|
1057
|
+
...config,
|
|
1058
|
+
},
|
|
1059
|
+
},
|
|
1060
|
+
},
|
|
1061
|
+
},
|
|
1062
|
+
]);
|
|
1063
|
+
|
|
1064
|
+
this.setActiveSegment(segmentationId, segmentIndex);
|
|
1065
|
+
|
|
1066
|
+
// Apply additional configurations
|
|
1067
|
+
if (config.isLocked !== undefined) {
|
|
1068
|
+
this._setSegmentLockedStatus(segmentationId, segmentIndex, config.isLocked);
|
|
1069
|
+
}
|
|
1070
|
+
|
|
1071
|
+
// Get all viewports that have this segmentation
|
|
1072
|
+
const viewportIds = this.getViewportIdsWithSegmentation(segmentationId);
|
|
1073
|
+
|
|
1074
|
+
viewportIds.forEach(viewportId => {
|
|
1075
|
+
// Set color if provided
|
|
1076
|
+
if (config.color !== undefined) {
|
|
1077
|
+
this.setSegmentColor(viewportId, segmentationId, segmentIndex, config.color);
|
|
1078
|
+
}
|
|
1079
|
+
|
|
1080
|
+
// Set visibility if provided
|
|
1081
|
+
if (config.visibility !== undefined) {
|
|
1082
|
+
this.setSegmentVisibility(viewportId, segmentationId, segmentIndex, config.visibility);
|
|
1083
|
+
}
|
|
1084
|
+
});
|
|
1085
|
+
}
|
|
1086
|
+
|
|
1087
|
+
/**
|
|
1088
|
+
* Creates a memo that records the current state of a segment (segmentationId/segmentIndex)
|
|
1089
|
+
* so that undo can restore it via addSegment and redo can call removeSegment again
|
|
1090
|
+
* without recording history.
|
|
1091
|
+
*
|
|
1092
|
+
* @param segmentationId - The ID of the segmentation.
|
|
1093
|
+
* @param segmentIndex - The index of the segment (must still exist when called).
|
|
1094
|
+
* @param _options - Reserved (e.g. deleting) for future use.
|
|
1095
|
+
* @returns A Memo with restoreMemo(undo): undo => addSegment, redo => removeSegment (skipRecordingHistory).
|
|
1096
|
+
*/
|
|
1097
|
+
public createSegmentIndexMemo(
|
|
1098
|
+
segmentationId: string,
|
|
1099
|
+
segmentIndex: number,
|
|
1100
|
+
_options?: { deleting?: boolean }
|
|
1101
|
+
): csTypes.Memo | null {
|
|
1102
|
+
const csSegmentation = this.getCornerstoneSegmentation(segmentationId);
|
|
1103
|
+
const segment = csSegmentation?.segments?.[segmentIndex];
|
|
1104
|
+
if (!segment) {
|
|
1105
|
+
return null;
|
|
1106
|
+
}
|
|
1107
|
+
|
|
1108
|
+
let color: csTypes.Color | undefined;
|
|
1109
|
+
let visibility: boolean | undefined;
|
|
1110
|
+
const viewportIds = this.getViewportIdsWithSegmentation(segmentationId);
|
|
1111
|
+
if (viewportIds.length > 0) {
|
|
1112
|
+
const firstViewportId = viewportIds[0];
|
|
1113
|
+
const representations = this.getSegmentationRepresentations(firstViewportId, {
|
|
1114
|
+
segmentationId,
|
|
1115
|
+
});
|
|
1116
|
+
const repType = representations[0]?.type ?? LABELMAP;
|
|
1117
|
+
color = this.getSegmentColor(firstViewportId, segmentationId, segmentIndex);
|
|
1118
|
+
visibility = cstSegmentation.config.visibility.getSegmentIndexVisibility(
|
|
1119
|
+
firstViewportId,
|
|
1120
|
+
{ segmentationId, type: repType },
|
|
1121
|
+
segmentIndex
|
|
1122
|
+
);
|
|
1123
|
+
}
|
|
1124
|
+
|
|
1125
|
+
const segmentState = {
|
|
1126
|
+
segmentIndex,
|
|
1127
|
+
label: segment.label,
|
|
1128
|
+
isLocked: segment.locked,
|
|
1129
|
+
active: segment.active,
|
|
1130
|
+
color,
|
|
1131
|
+
visibility,
|
|
1132
|
+
};
|
|
1133
|
+
|
|
1134
|
+
const service = this;
|
|
1135
|
+
const memo: csTypes.Memo = {
|
|
1136
|
+
id: csUtils.uuidv4(),
|
|
1137
|
+
operationType: 'segmentIndex',
|
|
1138
|
+
restoreMemo(undo?: boolean) {
|
|
1139
|
+
if (undo === true) {
|
|
1140
|
+
service.addSegment(segmentationId, segmentState);
|
|
1141
|
+
} else {
|
|
1142
|
+
// Redo: remove the segment via cornerstone without recording history
|
|
1143
|
+
cstSegmentation.removeSegment(segmentationId, segmentIndex, { recordHistory: false });
|
|
1144
|
+
}
|
|
1145
|
+
},
|
|
1146
|
+
};
|
|
1147
|
+
return memo;
|
|
1148
|
+
}
|
|
1149
|
+
|
|
1150
|
+
/**
|
|
1151
|
+
* Removes a segment from a segmentation and updates the active segment index if necessary.
|
|
1152
|
+
*
|
|
1153
|
+
* @param segmentationId - The ID of the segmentation containing the segment to remove.
|
|
1154
|
+
* @param segmentIndex - The index of the segment to remove.
|
|
1155
|
+
* @param options - Optional. skipRecordingHistory: if true, do not push undo memo (used when redoing).
|
|
1156
|
+
*
|
|
1157
|
+
* @remarks
|
|
1158
|
+
* This method performs the following actions:
|
|
1159
|
+
* 1. Clears the segment value in the Cornerstone segmentation.
|
|
1160
|
+
* 2. Updates all related segmentation representations to remove the segment.
|
|
1161
|
+
* 3. If the removed segment was the active segment, it updates the active segment index.
|
|
1162
|
+
*
|
|
1163
|
+
*/
|
|
1164
|
+
public removeSegment(
|
|
1165
|
+
segmentationId: string,
|
|
1166
|
+
segmentIndex: number,
|
|
1167
|
+
options?: { skipRecordingHistory?: boolean }
|
|
1168
|
+
): void {
|
|
1169
|
+
let memo;
|
|
1170
|
+
if (!options?.skipRecordingHistory) {
|
|
1171
|
+
memo = this.createSegmentIndexMemo(segmentationId, segmentIndex, { deleting: true });
|
|
1172
|
+
DefaultHistoryMemo.startGroupRecording();
|
|
1173
|
+
cstSegmentation.removeSegment(segmentationId, segmentIndex, { recordHistory: true });
|
|
1174
|
+
DefaultHistoryMemo.push(memo);
|
|
1175
|
+
DefaultHistoryMemo.endGroupRecording();
|
|
1176
|
+
} else {
|
|
1177
|
+
cstSegmentation.removeSegment(segmentationId, segmentIndex, { recordHistory: false });
|
|
1178
|
+
}
|
|
1179
|
+
}
|
|
1180
|
+
|
|
1181
|
+
public setSegmentVisibility(
|
|
1182
|
+
viewportId: string,
|
|
1183
|
+
segmentationId: string,
|
|
1184
|
+
segmentIndex: number,
|
|
1185
|
+
isVisible: boolean,
|
|
1186
|
+
type?: csToolsEnums.SegmentationRepresentations
|
|
1187
|
+
): void {
|
|
1188
|
+
this._setSegmentVisibility(viewportId, segmentationId, segmentIndex, isVisible, type);
|
|
1189
|
+
}
|
|
1190
|
+
|
|
1191
|
+
/**
|
|
1192
|
+
* Sets the locked status of a segment in a segmentation.
|
|
1193
|
+
*
|
|
1194
|
+
* @param segmentationId - The ID of the segmentation containing the segment.
|
|
1195
|
+
* @param segmentIndex - The index of the segment to set the locked status for.
|
|
1196
|
+
* @param isLocked - The new locked status of the segment.
|
|
1197
|
+
*
|
|
1198
|
+
* @remarks
|
|
1199
|
+
* This method updates the locked status of a specific segment within a segmentation.
|
|
1200
|
+
* A locked segment cannot be modified or edited.
|
|
1201
|
+
*/
|
|
1202
|
+
public setSegmentLocked(segmentationId: string, segmentIndex: number, isLocked: boolean): void {
|
|
1203
|
+
this._setSegmentLockedStatus(segmentationId, segmentIndex, isLocked);
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Toggles the locked state of a segment in a segmentation.
|
|
1208
|
+
* @param segmentationId - The ID of the segmentation.
|
|
1209
|
+
* @param segmentIndex - The index of the segment to toggle.
|
|
1210
|
+
*/
|
|
1211
|
+
public toggleSegmentLocked(segmentationId: string, segmentIndex: number): void {
|
|
1212
|
+
const isLocked = cstSegmentation.segmentLocking.isSegmentIndexLocked(
|
|
1213
|
+
segmentationId,
|
|
1214
|
+
segmentIndex
|
|
1215
|
+
);
|
|
1216
|
+
this._setSegmentLockedStatus(segmentationId, segmentIndex, !isLocked);
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
public toggleSegmentVisibility(
|
|
1220
|
+
viewportId: string,
|
|
1221
|
+
segmentationId: string,
|
|
1222
|
+
segmentIndex: number,
|
|
1223
|
+
type: csToolsEnums.SegmentationRepresentations
|
|
1224
|
+
): void {
|
|
1225
|
+
const isVisible = cstSegmentation.config.visibility.getSegmentIndexVisibility(
|
|
1226
|
+
viewportId,
|
|
1227
|
+
{
|
|
1228
|
+
segmentationId,
|
|
1229
|
+
type,
|
|
1230
|
+
},
|
|
1231
|
+
segmentIndex
|
|
1232
|
+
);
|
|
1233
|
+
this._setSegmentVisibility(viewportId, segmentationId, segmentIndex, !isVisible, type);
|
|
1234
|
+
}
|
|
1235
|
+
|
|
1236
|
+
/**
|
|
1237
|
+
* Sets the color of a specific segment in a segmentation.
|
|
1238
|
+
*
|
|
1239
|
+
* @param viewportId - The ID of the viewport containing the segmentation
|
|
1240
|
+
* @param segmentationId - The ID of the segmentation containing the segment
|
|
1241
|
+
* @param segmentIndex - The index of the segment to set the color for
|
|
1242
|
+
* @param color - The new color to apply to the segment as an array of RGBA values
|
|
1243
|
+
*
|
|
1244
|
+
* @remarks
|
|
1245
|
+
* This method updates the color of a specific segment within a segmentation.
|
|
1246
|
+
* The color parameter should be an array of 4 numbers representing RGBA values.
|
|
1247
|
+
*/
|
|
1248
|
+
public setSegmentColor(
|
|
1249
|
+
viewportId: string,
|
|
1250
|
+
segmentationId: string,
|
|
1251
|
+
segmentIndex: number,
|
|
1252
|
+
color: csTypes.Color
|
|
1253
|
+
): void {
|
|
1254
|
+
const segmentationRepresentations = this.getSegmentationRepresentations(viewportId, {
|
|
1255
|
+
segmentationId,
|
|
1256
|
+
});
|
|
1257
|
+
const { colorLUTIndex } = segmentationRepresentations[0];
|
|
1258
|
+
this._segmentationIdToColorLUTIndexMap.set(segmentationId, colorLUTIndex);
|
|
1259
|
+
|
|
1260
|
+
cstSegmentation.config.color.setSegmentIndexColor(
|
|
1261
|
+
viewportId,
|
|
1262
|
+
segmentationId,
|
|
1263
|
+
segmentIndex,
|
|
1264
|
+
color
|
|
1265
|
+
);
|
|
1266
|
+
}
|
|
1267
|
+
|
|
1268
|
+
/**
|
|
1269
|
+
* Gets the current color of a specific segment in a segmentation.
|
|
1270
|
+
*
|
|
1271
|
+
* @param viewportId - The ID of the viewport containing the segmentation
|
|
1272
|
+
* @param segmentationId - The ID of the segmentation containing the segment
|
|
1273
|
+
* @param segmentIndex - The index of the segment to get the color for
|
|
1274
|
+
* @returns An array of 4 numbers representing the RGBA color values of the segment
|
|
1275
|
+
*
|
|
1276
|
+
* @remarks
|
|
1277
|
+
* This method retrieves the current color of a specific segment within a segmentation.
|
|
1278
|
+
* The returned color is an array of 4 numbers representing RGBA values.
|
|
1279
|
+
*/
|
|
1280
|
+
public getSegmentColor(viewportId: string, segmentationId: string, segmentIndex: number) {
|
|
1281
|
+
return cstSegmentation.config.color.getSegmentIndexColor(
|
|
1282
|
+
viewportId,
|
|
1283
|
+
segmentationId,
|
|
1284
|
+
segmentIndex
|
|
1285
|
+
);
|
|
1286
|
+
}
|
|
1287
|
+
|
|
1288
|
+
/**
|
|
1289
|
+
* Gets the labelmap volume for a segmentation
|
|
1290
|
+
* @param segmentationId - The ID of the segmentation to get the labelmap volume for
|
|
1291
|
+
* @returns The labelmap volume for the segmentation, or null if not found
|
|
1292
|
+
*
|
|
1293
|
+
* @remarks
|
|
1294
|
+
* This method retrieves the labelmap volume data for a specific segmentation.
|
|
1295
|
+
* The labelmap volume contains the actual segmentation data in the form of a 3D volume.
|
|
1296
|
+
* Returns null if the segmentation does not have valid labelmap volume data.
|
|
1297
|
+
*/
|
|
1298
|
+
public getLabelmapVolume(segmentationId: string) {
|
|
1299
|
+
const csSegmentation = cstSegmentation.state.getSegmentation(segmentationId);
|
|
1300
|
+
const labelmapData = csSegmentation.representationData[
|
|
1301
|
+
LABELMAP
|
|
1302
|
+
] as cstTypes.LabelmapToolOperationDataVolume;
|
|
1303
|
+
|
|
1304
|
+
if (!labelmapData || !labelmapData.volumeId) {
|
|
1305
|
+
return null;
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
const { volumeId } = labelmapData;
|
|
1309
|
+
const labelmapVolume = cache.getVolume(volumeId);
|
|
1310
|
+
|
|
1311
|
+
return labelmapVolume;
|
|
1312
|
+
}
|
|
1313
|
+
|
|
1314
|
+
/**
|
|
1315
|
+
* Sets the label for a specific segment in a segmentation
|
|
1316
|
+
* @param segmentationId - The ID of the segmentation containing the segment
|
|
1317
|
+
* @param segmentIndex - The index of the segment to set the label for
|
|
1318
|
+
* @param label - The new label to apply to the segment
|
|
1319
|
+
*
|
|
1320
|
+
* @remarks
|
|
1321
|
+
* This method updates the text label of a specific segment within a segmentation.
|
|
1322
|
+
* The label is used to identify and describe the segment in the UI.
|
|
1323
|
+
*/
|
|
1324
|
+
public setSegmentLabel(segmentationId: string, segmentIndex: number, label: string) {
|
|
1325
|
+
this._setSegmentLabel(segmentationId, segmentIndex, label);
|
|
1326
|
+
}
|
|
1327
|
+
|
|
1328
|
+
/**
|
|
1329
|
+
* Sets the active segment for a segmentation
|
|
1330
|
+
* @param segmentationId - The ID of the segmentation containing the segment
|
|
1331
|
+
* @param segmentIndex - The index of the segment to set as active
|
|
1332
|
+
*
|
|
1333
|
+
* @remarks
|
|
1334
|
+
* This method updates which segment is considered "active" within a segmentation.
|
|
1335
|
+
* The active segment is typically highlighted and available for editing operations.
|
|
1336
|
+
*/
|
|
1337
|
+
public setActiveSegment(segmentationId: string, segmentIndex: number): void {
|
|
1338
|
+
this._setActiveSegment(segmentationId, segmentIndex);
|
|
1339
|
+
}
|
|
1340
|
+
|
|
1341
|
+
/**
|
|
1342
|
+
* Controls whether inactive segmentations should be rendered in a viewport
|
|
1343
|
+
* @param viewportId - The ID of the viewport to update
|
|
1344
|
+
* @param renderInactive - Whether inactive segmentations should be rendered
|
|
1345
|
+
*
|
|
1346
|
+
* @remarks
|
|
1347
|
+
* This method configures if segmentations that are not currently active
|
|
1348
|
+
* should still be visible in the specified viewport. This can be useful
|
|
1349
|
+
* for comparing or viewing multiple segmentations simultaneously.
|
|
1350
|
+
*/
|
|
1351
|
+
public setRenderInactiveSegmentations(viewportId: string, renderInactive: boolean): void {
|
|
1352
|
+
cstSegmentation.config.style.setRenderInactiveSegmentations(viewportId, renderInactive);
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
/**
|
|
1356
|
+
* Gets whether inactive segmentations are being rendered for a viewport
|
|
1357
|
+
* @param viewportId - The ID of the viewport to check
|
|
1358
|
+
* @returns boolean indicating if inactive segmentations are rendered
|
|
1359
|
+
*
|
|
1360
|
+
* @remarks
|
|
1361
|
+
* This method retrieves the current rendering state for inactive segmentations
|
|
1362
|
+
* in the specified viewport. Returns true if inactive segmentations are visible.
|
|
1363
|
+
*/
|
|
1364
|
+
public getRenderInactiveSegmentations(viewportId: string): boolean {
|
|
1365
|
+
return cstSegmentation.config.style.getRenderInactiveSegmentations(viewportId);
|
|
1366
|
+
}
|
|
1367
|
+
/**
|
|
1368
|
+
* Sets statistics for a group of segmentations
|
|
1369
|
+
* @param segmentationIds - Array of segmentation IDs that form the group
|
|
1370
|
+
* @param stats - Statistics object containing metrics for the segmentation group
|
|
1371
|
+
*
|
|
1372
|
+
* @remarks
|
|
1373
|
+
* This method stores statistical data for a group of related segmentations.
|
|
1374
|
+
* The stats are stored using a composite key created from the sorted and joined
|
|
1375
|
+
*/
|
|
1376
|
+
public setSegmentationGroupStats(segmentationIds: string[], stats: any) {
|
|
1377
|
+
const groupId = this.getGroupId(segmentationIds);
|
|
1378
|
+
this._segmentationGroupStatsMap.set(groupId, stats);
|
|
1379
|
+
}
|
|
1380
|
+
|
|
1381
|
+
/**
|
|
1382
|
+
* Gets statistics for a group of segmentations
|
|
1383
|
+
* @param segmentationIds - Array of segmentation IDs that form the group
|
|
1384
|
+
* @returns The stored statistics object for the segmentation group if found, undefined otherwise
|
|
1385
|
+
*/
|
|
1386
|
+
public getSegmentationGroupStats(segmentationIds: string[]) {
|
|
1387
|
+
const groupId = this.getGroupId(segmentationIds);
|
|
1388
|
+
return this._segmentationGroupStatsMap.get(groupId);
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
private getGroupId(segmentationIds: string[]) {
|
|
1392
|
+
return segmentationIds.sort().join(',');
|
|
1393
|
+
}
|
|
1394
|
+
|
|
1395
|
+
/**
|
|
1396
|
+
* Toggles the visibility of a segmentation in the state, and broadcasts the event.
|
|
1397
|
+
* Note: this method does not update the segmentation state in the source. It only
|
|
1398
|
+
* updates the state, and there should be separate listeners for that.
|
|
1399
|
+
* @param ids segmentation ids
|
|
1400
|
+
*/
|
|
1401
|
+
public toggleSegmentationRepresentationVisibility = (
|
|
1402
|
+
viewportId: string,
|
|
1403
|
+
{
|
|
1404
|
+
segmentationId,
|
|
1405
|
+
type,
|
|
1406
|
+
}: { segmentationId: string; type: csToolsEnums.SegmentationRepresentations }
|
|
1407
|
+
): void => {
|
|
1408
|
+
this._toggleSegmentationRepresentationVisibility(viewportId, segmentationId, type);
|
|
1409
|
+
};
|
|
1410
|
+
|
|
1411
|
+
public getViewportIdsWithSegmentation = (segmentationId: string): string[] => {
|
|
1412
|
+
const viewportIds = cstSegmentation.state.getViewportIdsWithSegmentation(segmentationId);
|
|
1413
|
+
return viewportIds;
|
|
1414
|
+
};
|
|
1415
|
+
|
|
1416
|
+
/**
|
|
1417
|
+
* Clears segmentation representations from the viewport.
|
|
1418
|
+
* Unlike removeRepresentationsFromViewport, this doesn't update
|
|
1419
|
+
* removed display set and representation maps.
|
|
1420
|
+
* We track removed segmentations manually to avoid re-adding them
|
|
1421
|
+
* when the display set is added again.
|
|
1422
|
+
* @param viewportId - The viewport ID to clear segmentation representations from.
|
|
1423
|
+
*/
|
|
1424
|
+
public clearSegmentationRepresentations(viewportId: string): void {
|
|
1425
|
+
this.removeRepresentationsFromViewport(viewportId);
|
|
1426
|
+
}
|
|
1427
|
+
|
|
1428
|
+
/**
|
|
1429
|
+
* Completely removes a segmentation from the state
|
|
1430
|
+
* @param segmentationId - The ID of the segmentation to remove.
|
|
1431
|
+
*/
|
|
1432
|
+
public remove(segmentationId: string): void {
|
|
1433
|
+
cstSegmentation.state.removeSegmentation(segmentationId);
|
|
1434
|
+
}
|
|
1435
|
+
|
|
1436
|
+
public removeAllSegmentations(): void {
|
|
1437
|
+
cstSegmentation.state.removeAllSegmentations();
|
|
1438
|
+
}
|
|
1439
|
+
|
|
1440
|
+
/**
|
|
1441
|
+
* Removes segmentation representations from the viewport.
|
|
1442
|
+
* @param viewportId - The viewport id to remove the segmentation representations from.
|
|
1443
|
+
* @param specifier - The specifier to remove the segmentation representations.
|
|
1444
|
+
*
|
|
1445
|
+
* @remarks
|
|
1446
|
+
* If no specifier is provided, all segmentation representations for the viewport are removed.
|
|
1447
|
+
* If a segmentationId specifier is provided, only the segmentation representation with the specified segmentationId and type are removed.
|
|
1448
|
+
* If a type specifier is provided, only the segmentation representation with the specified type are removed.
|
|
1449
|
+
* If both a segmentationId and type specifier are provided, only the segmentation representation with the specified segmentationId and type are removed.
|
|
1450
|
+
*/
|
|
1451
|
+
public removeRepresentationsFromViewport(
|
|
1452
|
+
viewportId: string,
|
|
1453
|
+
specifier: {
|
|
1454
|
+
segmentationId?: string;
|
|
1455
|
+
type?: csToolsEnums.SegmentationRepresentations;
|
|
1456
|
+
} = {}
|
|
1457
|
+
): void {
|
|
1458
|
+
cstSegmentation.removeSegmentationRepresentations(viewportId, specifier);
|
|
1459
|
+
}
|
|
1460
|
+
|
|
1461
|
+
/**
|
|
1462
|
+
* Jumps to the next slice that contains the specified segment in the viewport.
|
|
1463
|
+
* For labelmaps, it jumps to the segment center. For contours, it cycles through
|
|
1464
|
+
* all slices that contain contour data for the segment.
|
|
1465
|
+
*
|
|
1466
|
+
* @param segmentationId - The ID of the segmentation
|
|
1467
|
+
* @param segmentIndex - The index of the segment to jump to
|
|
1468
|
+
* @param viewportId - Optional viewport ID. If not provided, applies to all viewports with this segmentation
|
|
1469
|
+
* @param highlightAlpha - Alpha value for highlighting (0-1)
|
|
1470
|
+
* @param highlightSegment - Whether to highlight the segment after jumping
|
|
1471
|
+
* @param animationLength - Length of highlight animation in milliseconds
|
|
1472
|
+
* @param highlightHideOthers - Whether to hide other segments during highlight
|
|
1473
|
+
* @param animationFunctionType - The easing function to use for animation
|
|
1474
|
+
*/
|
|
1475
|
+
public jumpToSegmentNext(
|
|
1476
|
+
segmentationId: string,
|
|
1477
|
+
segmentIndex: number,
|
|
1478
|
+
forViewportId?: string,
|
|
1479
|
+
direction = 1,
|
|
1480
|
+
highlightAlpha = 0.9,
|
|
1481
|
+
highlightSegment = true,
|
|
1482
|
+
animationLength = 750,
|
|
1483
|
+
highlightHideOthers = false,
|
|
1484
|
+
animationFunctionType: EasingFunctionEnum = EasingFunctionEnum.EASE_IN_OUT
|
|
1485
|
+
): void {
|
|
1486
|
+
const viewportIds = forViewportId
|
|
1487
|
+
? [forViewportId]
|
|
1488
|
+
: this.getViewportIdsWithSegmentation(segmentationId);
|
|
1489
|
+
|
|
1490
|
+
viewportIds.forEach(viewportId => {
|
|
1491
|
+
const representations = this.getSegmentationRepresentations(viewportId, {
|
|
1492
|
+
segmentationId,
|
|
1493
|
+
});
|
|
1494
|
+
|
|
1495
|
+
if (!representations || representations.length === 0) {
|
|
1496
|
+
return;
|
|
1497
|
+
}
|
|
1498
|
+
|
|
1499
|
+
const representation = representations[0];
|
|
1500
|
+
const { type } = representation;
|
|
1501
|
+
|
|
1502
|
+
// For contours, check if we have a segment center.
|
|
1503
|
+
const center =
|
|
1504
|
+
type === CONTOUR ? this._getSegmentCenter(segmentationId, segmentIndex) : undefined;
|
|
1505
|
+
const canUseSegmentCenter = type !== CONTOUR || !!center;
|
|
1506
|
+
if (canUseSegmentCenter) {
|
|
1507
|
+
this.jumpToSegmentCenter(
|
|
1508
|
+
segmentationId,
|
|
1509
|
+
segmentIndex,
|
|
1510
|
+
viewportId,
|
|
1511
|
+
highlightAlpha,
|
|
1512
|
+
highlightSegment,
|
|
1513
|
+
animationLength,
|
|
1514
|
+
highlightHideOthers,
|
|
1515
|
+
animationFunctionType,
|
|
1516
|
+
center
|
|
1517
|
+
);
|
|
1518
|
+
return;
|
|
1519
|
+
}
|
|
1520
|
+
|
|
1521
|
+
const { viewport } = getEnabledElementByViewportId(viewportId);
|
|
1522
|
+
if (!viewport) {
|
|
1523
|
+
return;
|
|
1524
|
+
}
|
|
1525
|
+
|
|
1526
|
+
const viewRefs = this._getContourViewReferences(segmentationId, viewport, segmentIndex);
|
|
1527
|
+
if (!viewRefs) {
|
|
1528
|
+
return;
|
|
1529
|
+
}
|
|
1530
|
+
|
|
1531
|
+
// Get the current slice index
|
|
1532
|
+
const currentSliceIndex = viewport.getCurrentImageIdIndex();
|
|
1533
|
+
let nearestSliceIndex = null;
|
|
1534
|
+
let loopSliceIndex = null;
|
|
1535
|
+
|
|
1536
|
+
for (const [sliceIndex] of viewRefs.entries()) {
|
|
1537
|
+
// Track loop index for wraparound (smallest for forward, largest for backward)
|
|
1538
|
+
if (direction > 0) {
|
|
1539
|
+
if (loopSliceIndex === null || sliceIndex < loopSliceIndex) {
|
|
1540
|
+
loopSliceIndex = sliceIndex;
|
|
1541
|
+
}
|
|
1542
|
+
} else {
|
|
1543
|
+
if (loopSliceIndex === null || sliceIndex > loopSliceIndex) {
|
|
1544
|
+
loopSliceIndex = sliceIndex;
|
|
1545
|
+
}
|
|
1546
|
+
}
|
|
1547
|
+
|
|
1548
|
+
if (direction > 0) {
|
|
1549
|
+
// Forward direction: find nearest slice after current
|
|
1550
|
+
if (sliceIndex <= currentSliceIndex) {
|
|
1551
|
+
continue;
|
|
1552
|
+
}
|
|
1553
|
+
if (nearestSliceIndex === null || sliceIndex < nearestSliceIndex) {
|
|
1554
|
+
nearestSliceIndex = sliceIndex;
|
|
1555
|
+
}
|
|
1556
|
+
} else {
|
|
1557
|
+
// Backward direction: find nearest slice before current
|
|
1558
|
+
if (sliceIndex >= currentSliceIndex) {
|
|
1559
|
+
continue;
|
|
1560
|
+
}
|
|
1561
|
+
if (nearestSliceIndex === null || sliceIndex > nearestSliceIndex) {
|
|
1562
|
+
nearestSliceIndex = sliceIndex;
|
|
1563
|
+
}
|
|
1564
|
+
}
|
|
1565
|
+
}
|
|
1566
|
+
|
|
1567
|
+
// Wraparound: if no slice found in direction, use loop index
|
|
1568
|
+
nearestSliceIndex = nearestSliceIndex ?? loopSliceIndex;
|
|
1569
|
+
if (nearestSliceIndex === null) {
|
|
1570
|
+
return;
|
|
1571
|
+
}
|
|
1572
|
+
const viewRef = viewRefs.get(nearestSliceIndex);
|
|
1573
|
+
viewport.setViewReference(viewRef);
|
|
1574
|
+
viewport.render();
|
|
1575
|
+
});
|
|
1576
|
+
}
|
|
1577
|
+
|
|
1578
|
+
/**
|
|
1579
|
+
* Jumps the viewport to the center of hte given segment.
|
|
1580
|
+
* Only works for labelmaps, and may result in not showing any contours.
|
|
1581
|
+
*/
|
|
1582
|
+
public jumpToSegmentCenter(
|
|
1583
|
+
segmentationId: string,
|
|
1584
|
+
segmentIndex: number,
|
|
1585
|
+
viewportId?: string,
|
|
1586
|
+
highlightAlpha = 0.9,
|
|
1587
|
+
highlightSegment = true,
|
|
1588
|
+
animationLength = 750,
|
|
1589
|
+
highlightHideOthers = false,
|
|
1590
|
+
animationFunctionType: EasingFunctionEnum = EasingFunctionEnum.EASE_IN_OUT,
|
|
1591
|
+
center?: { image?: csTypes.Point3; world: csTypes.Point3 }
|
|
1592
|
+
): void {
|
|
1593
|
+
const resolvedCenter = center ?? this._getSegmentCenter(segmentationId, segmentIndex);
|
|
1594
|
+
if (!resolvedCenter) {
|
|
1595
|
+
console.warn('No center found for segmentation', segmentationId, segmentIndex);
|
|
1596
|
+
return;
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1599
|
+
const { world } = resolvedCenter as { world: csTypes.Point3 };
|
|
1600
|
+
|
|
1601
|
+
// need to find which viewports are displaying the segmentation
|
|
1602
|
+
const viewportIds = viewportId
|
|
1603
|
+
? [viewportId]
|
|
1604
|
+
: this.getViewportIdsWithSegmentation(segmentationId);
|
|
1605
|
+
|
|
1606
|
+
viewportIds.forEach(viewportId => {
|
|
1607
|
+
const { viewport } = getEnabledElementByViewportId(viewportId);
|
|
1608
|
+
if (!viewport) {
|
|
1609
|
+
return;
|
|
1610
|
+
}
|
|
1611
|
+
|
|
1612
|
+
// Recenter via the backend twin: legacy jumpToWorld, or native setViewReference
|
|
1613
|
+
// (a native PlanarViewport has no jumpToWorld). Skip the highlight when the
|
|
1614
|
+
// recenter did not happen, matching the previous guarded behavior.
|
|
1615
|
+
const didJump = this._segBackend(viewport).jumpToSegmentCenter(viewport, world);
|
|
1616
|
+
|
|
1617
|
+
didJump &&
|
|
1618
|
+
highlightSegment &&
|
|
1619
|
+
this.highlightSegment(
|
|
1620
|
+
segmentationId,
|
|
1621
|
+
segmentIndex,
|
|
1622
|
+
viewportId,
|
|
1623
|
+
highlightAlpha,
|
|
1624
|
+
animationLength,
|
|
1625
|
+
highlightHideOthers,
|
|
1626
|
+
animationFunctionType
|
|
1627
|
+
);
|
|
1628
|
+
});
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
public highlightSegment(
|
|
1632
|
+
segmentationId: string,
|
|
1633
|
+
segmentIndex: number,
|
|
1634
|
+
viewportId?: string,
|
|
1635
|
+
alpha = 0.9,
|
|
1636
|
+
animationLength = 750,
|
|
1637
|
+
hideOthers = true,
|
|
1638
|
+
animationFunctionType: EasingFunctionEnum = EasingFunctionEnum.EASE_IN_OUT
|
|
1639
|
+
): void {
|
|
1640
|
+
const csSegmentation = this.getCornerstoneSegmentation(segmentationId);
|
|
1641
|
+
|
|
1642
|
+
this._endSegmentHighlightsOtherThan(segmentationId, segmentIndex);
|
|
1643
|
+
|
|
1644
|
+
const viewportIds = viewportId
|
|
1645
|
+
? [viewportId]
|
|
1646
|
+
: this.getViewportIdsWithSegmentation(segmentationId);
|
|
1647
|
+
|
|
1648
|
+
viewportIds.forEach(viewportId => {
|
|
1649
|
+
const segmentationRepresentation = this.getSegmentationRepresentations(viewportId, {
|
|
1650
|
+
segmentationId,
|
|
1651
|
+
});
|
|
1652
|
+
|
|
1653
|
+
const representation = segmentationRepresentation[0];
|
|
1654
|
+
const { type } = representation;
|
|
1655
|
+
const segments = csSegmentation.segments;
|
|
1656
|
+
|
|
1657
|
+
// Viewports showing the same representation type share the segment's style, and
|
|
1658
|
+
// so one animation. Selecting the segment again while it animates continues it.
|
|
1659
|
+
const key = segmentHighlightKey(segmentationId, segmentIndex, type);
|
|
1660
|
+
if (this._segmentHighlights.get(key)?.state === 'highlighting') {
|
|
1661
|
+
return;
|
|
1662
|
+
}
|
|
1663
|
+
const highlight: SegmentHighlight = {
|
|
1664
|
+
segmentationId,
|
|
1665
|
+
segmentIndex,
|
|
1666
|
+
type,
|
|
1667
|
+
state: 'highlighting',
|
|
1668
|
+
};
|
|
1669
|
+
|
|
1670
|
+
const highlightFn =
|
|
1671
|
+
type === LABELMAP ? this._highlightLabelmap.bind(this) : this._highlightContour.bind(this);
|
|
1672
|
+
|
|
1673
|
+
const adjustedAlpha = type === LABELMAP ? alpha : 1 - alpha;
|
|
1674
|
+
|
|
1675
|
+
highlightFn(
|
|
1676
|
+
segmentIndex,
|
|
1677
|
+
adjustedAlpha,
|
|
1678
|
+
hideOthers,
|
|
1679
|
+
segments,
|
|
1680
|
+
viewportId,
|
|
1681
|
+
animationLength,
|
|
1682
|
+
representation,
|
|
1683
|
+
animationFunctionType,
|
|
1684
|
+
highlight
|
|
1685
|
+
);
|
|
1686
|
+
// Registered only once the animation is set up, so a highlight that throws
|
|
1687
|
+
// does not leave the segment looking as if it were still animating.
|
|
1688
|
+
this._segmentHighlights.set(key, highlight);
|
|
1689
|
+
});
|
|
1690
|
+
}
|
|
1691
|
+
|
|
1692
|
+
/**
|
|
1693
|
+
* Deselects every highlighted segment but the given one. A segment still
|
|
1694
|
+
* animating has its temporary style reset now, not on its loop's next frame,
|
|
1695
|
+
* so a highlight that starts right after reads the segment's real style.
|
|
1696
|
+
*/
|
|
1697
|
+
private _endSegmentHighlightsOtherThan(segmentationId: string, segmentIndex: number): void {
|
|
1698
|
+
this._segmentHighlights.forEach((highlight, key) => {
|
|
1699
|
+
if (highlight.segmentationId === segmentationId && highlight.segmentIndex === segmentIndex) {
|
|
1700
|
+
return;
|
|
1701
|
+
}
|
|
1702
|
+
this._segmentHighlights.delete(key);
|
|
1703
|
+
if (highlight.state === 'highlighting') {
|
|
1704
|
+
this._resetSegmentHighlightStyle(highlight);
|
|
1705
|
+
}
|
|
1706
|
+
});
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
private _isCurrentSegmentHighlight(highlight: SegmentHighlight): boolean {
|
|
1710
|
+
const { segmentationId, segmentIndex, type } = highlight;
|
|
1711
|
+
return (
|
|
1712
|
+
this._segmentHighlights.get(segmentHighlightKey(segmentationId, segmentIndex, type)) ===
|
|
1713
|
+
highlight
|
|
1714
|
+
);
|
|
1715
|
+
}
|
|
1716
|
+
|
|
1717
|
+
/**
|
|
1718
|
+
* Wraps a highlight's animation frame so it runs only while the highlight is
|
|
1719
|
+
* current. A frame that throws ends the highlight, so the segment can be
|
|
1720
|
+
* highlighted again, and resets the temporary style an earlier frame may have
|
|
1721
|
+
* applied, since nothing else will once the highlight is forgotten.
|
|
1722
|
+
*/
|
|
1723
|
+
private _segmentHighlightFrame(
|
|
1724
|
+
highlight: SegmentHighlight,
|
|
1725
|
+
frame: FrameRequestCallback
|
|
1726
|
+
): FrameRequestCallback {
|
|
1727
|
+
return (time: number) => {
|
|
1728
|
+
if (!this._isCurrentSegmentHighlight(highlight)) {
|
|
1729
|
+
return;
|
|
1730
|
+
}
|
|
1731
|
+
try {
|
|
1732
|
+
frame(time);
|
|
1733
|
+
} catch (error) {
|
|
1734
|
+
const { segmentationId, segmentIndex, type } = highlight;
|
|
1735
|
+
this._segmentHighlights.delete(segmentHighlightKey(segmentationId, segmentIndex, type));
|
|
1736
|
+
if (this.getCornerstoneSegmentation(segmentationId)) {
|
|
1737
|
+
try {
|
|
1738
|
+
this._resetSegmentHighlightStyle(highlight);
|
|
1739
|
+
} catch {
|
|
1740
|
+
// The frame's error is the one to report.
|
|
1741
|
+
}
|
|
1742
|
+
}
|
|
1743
|
+
throw error;
|
|
1744
|
+
}
|
|
1745
|
+
};
|
|
1746
|
+
}
|
|
1747
|
+
|
|
1748
|
+
private _completeSegmentHighlight(highlight: SegmentHighlight): void {
|
|
1749
|
+
highlight.state = 'selected';
|
|
1750
|
+
this._resetSegmentHighlightStyle(highlight);
|
|
1751
|
+
}
|
|
1752
|
+
|
|
1753
|
+
private _resetSegmentHighlightStyle({ segmentationId, segmentIndex, type }: SegmentHighlight) {
|
|
1754
|
+
cstSegmentation.config.style.setStyle({ segmentationId, segmentIndex, type }, {}, false);
|
|
1755
|
+
}
|
|
1756
|
+
|
|
1757
|
+
private getAndValidateViewport(viewportId: string) {
|
|
1758
|
+
const csViewport =
|
|
1759
|
+
this.servicesManager.services.cornerstoneViewportService.getCornerstoneViewport(viewportId);
|
|
1760
|
+
if (!csViewport) {
|
|
1761
|
+
console.warn(`Viewport with id ${viewportId} not found.`);
|
|
1762
|
+
return null;
|
|
1763
|
+
}
|
|
1764
|
+
return csViewport;
|
|
1765
|
+
}
|
|
1766
|
+
|
|
1767
|
+
/**
|
|
1768
|
+
* Sets the visibility of a segmentation representation.
|
|
1769
|
+
*
|
|
1770
|
+
* @param viewportId - The ID of the viewport.
|
|
1771
|
+
* @param segmentationId - The ID of the segmentation.
|
|
1772
|
+
* @param isVisible - The new visibility state.
|
|
1773
|
+
*/
|
|
1774
|
+
private _setSegmentationRepresentationVisibility(
|
|
1775
|
+
viewportId: string,
|
|
1776
|
+
segmentationId: string,
|
|
1777
|
+
type: csToolsEnums.SegmentationRepresentations,
|
|
1778
|
+
isVisible: boolean
|
|
1779
|
+
): void {
|
|
1780
|
+
const representations = this.getSegmentationRepresentations(viewportId, {
|
|
1781
|
+
segmentationId,
|
|
1782
|
+
type,
|
|
1783
|
+
});
|
|
1784
|
+
const representation = representations[0];
|
|
1785
|
+
|
|
1786
|
+
if (!representation) {
|
|
1787
|
+
console.debug(
|
|
1788
|
+
'No segmentation representation found for the given viewportId and segmentationId'
|
|
1789
|
+
);
|
|
1790
|
+
return;
|
|
1791
|
+
}
|
|
1792
|
+
|
|
1793
|
+
cstSegmentation.config.visibility.setSegmentationRepresentationVisibility(
|
|
1794
|
+
viewportId,
|
|
1795
|
+
{
|
|
1796
|
+
segmentationId,
|
|
1797
|
+
type,
|
|
1798
|
+
},
|
|
1799
|
+
isVisible
|
|
1800
|
+
);
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
// Labelmap-add classification (determineViewportAndSegmentationType +
|
|
1804
|
+
// handleViewportConversion + the stack/volume case handlers) now lives in the
|
|
1805
|
+
// segmentation backend twins (backends/{Legacy,Next}SegmentationBackend), routed
|
|
1806
|
+
// per viewport via _segBackend(). The legacy twin reaches the viewport-recreation
|
|
1807
|
+
// and data-volume-conversion helpers below through ISegmentationServiceInternals.
|
|
1808
|
+
|
|
1809
|
+
private async _addSegmentationRepresentation(
|
|
1810
|
+
viewportId: string,
|
|
1811
|
+
segmentationId: string,
|
|
1812
|
+
representationType: csToolsEnums.SegmentationRepresentations,
|
|
1813
|
+
colorLUTIndex: number,
|
|
1814
|
+
isConverted: boolean,
|
|
1815
|
+
config?: {
|
|
1816
|
+
blendMode?: csEnums.BlendModes;
|
|
1817
|
+
useSliceRendering?: boolean;
|
|
1818
|
+
}
|
|
1819
|
+
): Promise<void> {
|
|
1820
|
+
const representation = {
|
|
1821
|
+
type: representationType,
|
|
1822
|
+
segmentationId,
|
|
1823
|
+
config: { colorLUTOrIndex: colorLUTIndex, ...config },
|
|
1824
|
+
};
|
|
1825
|
+
|
|
1826
|
+
const addRepresentation = () =>
|
|
1827
|
+
cstSegmentation.addSegmentationRepresentations(viewportId, [representation]);
|
|
1828
|
+
|
|
1829
|
+
if (isConverted) {
|
|
1830
|
+
const { viewportGridService } = this.servicesManager.services;
|
|
1831
|
+
await new Promise<void>(resolve => {
|
|
1832
|
+
const { unsubscribe } = viewportGridService.subscribe(
|
|
1833
|
+
viewportGridService.EVENTS.GRID_STATE_CHANGED,
|
|
1834
|
+
() => {
|
|
1835
|
+
addRepresentation();
|
|
1836
|
+
unsubscribe();
|
|
1837
|
+
resolve();
|
|
1838
|
+
}
|
|
1839
|
+
);
|
|
1840
|
+
});
|
|
1841
|
+
} else {
|
|
1842
|
+
addRepresentation();
|
|
1843
|
+
}
|
|
1844
|
+
}
|
|
1845
|
+
public async handleVolumeViewport(
|
|
1846
|
+
viewport: csTypes.IVolumeViewport,
|
|
1847
|
+
segmentation: SegmentationData,
|
|
1848
|
+
isVolumeSegmentation: boolean
|
|
1849
|
+
): Promise<void> {
|
|
1850
|
+
if (isVolumeSegmentation) {
|
|
1851
|
+
return; // Volume Labelmap on Volume Viewport is natively supported
|
|
1852
|
+
}
|
|
1853
|
+
|
|
1854
|
+
const frameOfReferenceUID = viewport.getFrameOfReferenceUID();
|
|
1855
|
+
const imageIds = getLabelmapImageIds(segmentation.segmentationId);
|
|
1856
|
+
const segImage = cache.getImage(imageIds[0]);
|
|
1857
|
+
|
|
1858
|
+
if (segImage?.FrameOfReferenceUID === frameOfReferenceUID) {
|
|
1859
|
+
await convertStackToVolumeLabelmap(segmentation);
|
|
1860
|
+
}
|
|
1861
|
+
}
|
|
1862
|
+
|
|
1863
|
+
public async convertStackToVolumeViewport(viewport: csTypes.IViewport): Promise<boolean> {
|
|
1864
|
+
const { viewportGridService, cornerstoneViewportService } = this.servicesManager.services;
|
|
1865
|
+
const state = viewportGridService.getState();
|
|
1866
|
+
const gridViewport = state.viewports.get(viewport.id);
|
|
1867
|
+
|
|
1868
|
+
const prevViewPresentation = viewport.getViewPresentation();
|
|
1869
|
+
const prevViewReference = viewport.getViewReference();
|
|
1870
|
+
const stackViewport = cornerstoneViewportService.getCornerstoneViewport(viewport.id);
|
|
1871
|
+
const { element } = stackViewport;
|
|
1872
|
+
|
|
1873
|
+
const volumeViewportNewVolumeHandler = () => {
|
|
1874
|
+
const volumeViewport = cornerstoneViewportService.getCornerstoneViewport(viewport.id);
|
|
1875
|
+
volumeViewport.setViewPresentation(prevViewPresentation);
|
|
1876
|
+
volumeViewport.setViewReference(prevViewReference);
|
|
1877
|
+
volumeViewport.render();
|
|
1878
|
+
|
|
1879
|
+
element.removeEventListener(
|
|
1880
|
+
csEnums.Events.VOLUME_VIEWPORT_NEW_VOLUME,
|
|
1881
|
+
volumeViewportNewVolumeHandler
|
|
1882
|
+
);
|
|
1883
|
+
};
|
|
1884
|
+
|
|
1885
|
+
element.addEventListener(
|
|
1886
|
+
csEnums.Events.VOLUME_VIEWPORT_NEW_VOLUME,
|
|
1887
|
+
volumeViewportNewVolumeHandler
|
|
1888
|
+
);
|
|
1889
|
+
|
|
1890
|
+
viewportGridService.setDisplaySetsForViewport({
|
|
1891
|
+
viewportId: viewport.id,
|
|
1892
|
+
displaySetInstanceUIDs: gridViewport.displaySetInstanceUIDs,
|
|
1893
|
+
viewportOptions: {
|
|
1894
|
+
...gridViewport.viewportOptions,
|
|
1895
|
+
viewportType: ViewportType.ORTHOGRAPHIC,
|
|
1896
|
+
},
|
|
1897
|
+
});
|
|
1898
|
+
|
|
1899
|
+
return true;
|
|
1900
|
+
}
|
|
1901
|
+
|
|
1902
|
+
public async attemptStackToVolumeConversion(
|
|
1903
|
+
viewport: csTypes.IStackViewport,
|
|
1904
|
+
segmentation: SegmentationData,
|
|
1905
|
+
viewportId: string,
|
|
1906
|
+
segmentationId: string
|
|
1907
|
+
): Promise<boolean> {
|
|
1908
|
+
const imageIds = getLabelmapImageIds(segmentation.segmentationId);
|
|
1909
|
+
const frameOfReferenceUID = viewport.getFrameOfReferenceUID();
|
|
1910
|
+
const segImage = cache.getImage(imageIds[0]);
|
|
1911
|
+
|
|
1912
|
+
if (
|
|
1913
|
+
segImage?.FrameOfReferenceUID &&
|
|
1914
|
+
frameOfReferenceUID &&
|
|
1915
|
+
segImage.FrameOfReferenceUID === frameOfReferenceUID
|
|
1916
|
+
) {
|
|
1917
|
+
const isConverted = await this.convertStackToVolumeViewport(viewport);
|
|
1918
|
+
triggerSegmentationRepresentationModified(viewportId, segmentationId, LABELMAP);
|
|
1919
|
+
|
|
1920
|
+
return isConverted;
|
|
1921
|
+
}
|
|
1922
|
+
|
|
1923
|
+
// Frame-of-reference mismatch (or missing): no conversion happened. Return an
|
|
1924
|
+
// explicit boolean so the Promise<boolean> contract holds for callers.
|
|
1925
|
+
return false;
|
|
1926
|
+
}
|
|
1927
|
+
|
|
1928
|
+
private addSegmentationToSource(segmentationPublicInput: cstTypes.SegmentationPublicInput) {
|
|
1929
|
+
cstSegmentation.addSegmentations([segmentationPublicInput]);
|
|
1930
|
+
}
|
|
1931
|
+
|
|
1932
|
+
private updateSegmentationInSource(
|
|
1933
|
+
segmentationId: string,
|
|
1934
|
+
payload: Partial<cstTypes.Segmentation>
|
|
1935
|
+
) {
|
|
1936
|
+
cstSegmentation.updateSegmentations([{ segmentationId, payload }]);
|
|
1937
|
+
}
|
|
1938
|
+
|
|
1939
|
+
private _toOHIFSegmentationRepresentation(
|
|
1940
|
+
viewportId: string,
|
|
1941
|
+
csRepresentation: cstTypes.SegmentationRepresentation
|
|
1942
|
+
): SegmentationRepresentation {
|
|
1943
|
+
const { segmentationId, type, active, visible } = csRepresentation;
|
|
1944
|
+
const { colorLUTIndex } = csRepresentation;
|
|
1945
|
+
|
|
1946
|
+
const segmentsRepresentations: { [segmentIndex: number]: SegmentRepresentation } = {};
|
|
1947
|
+
|
|
1948
|
+
const segmentation = cstSegmentation.state.getSegmentation(segmentationId);
|
|
1949
|
+
|
|
1950
|
+
if (!segmentation) {
|
|
1951
|
+
throw new Error(`Segmentation with ID ${segmentationId} not found.`);
|
|
1952
|
+
}
|
|
1953
|
+
|
|
1954
|
+
const segmentIds = Object.keys(segmentation.segments);
|
|
1955
|
+
|
|
1956
|
+
for (const segmentId of segmentIds) {
|
|
1957
|
+
const segmentIndex = parseInt(segmentId, 10);
|
|
1958
|
+
|
|
1959
|
+
const color = cstSegmentation.config.color.getSegmentIndexColor(
|
|
1960
|
+
viewportId,
|
|
1961
|
+
segmentationId,
|
|
1962
|
+
segmentIndex
|
|
1963
|
+
);
|
|
1964
|
+
|
|
1965
|
+
const isVisible = cstSegmentation.config.visibility.getSegmentIndexVisibility(
|
|
1966
|
+
viewportId,
|
|
1967
|
+
{
|
|
1968
|
+
segmentationId,
|
|
1969
|
+
type,
|
|
1970
|
+
},
|
|
1971
|
+
segmentIndex
|
|
1972
|
+
);
|
|
1973
|
+
|
|
1974
|
+
segmentsRepresentations[segmentIndex] = {
|
|
1975
|
+
color,
|
|
1976
|
+
segmentIndex,
|
|
1977
|
+
opacity: color[3],
|
|
1978
|
+
visible: isVisible,
|
|
1979
|
+
};
|
|
1980
|
+
}
|
|
1981
|
+
|
|
1982
|
+
const styles = cstSegmentation.config.style.getStyle({
|
|
1983
|
+
viewportId,
|
|
1984
|
+
segmentationId,
|
|
1985
|
+
type,
|
|
1986
|
+
});
|
|
1987
|
+
|
|
1988
|
+
const id = `${segmentationId}-${type}-${viewportId}`;
|
|
1989
|
+
|
|
1990
|
+
return {
|
|
1991
|
+
id: id,
|
|
1992
|
+
segmentationId,
|
|
1993
|
+
label: segmentation.label,
|
|
1994
|
+
fallbackLabel: segmentation.fallbackLabel,
|
|
1995
|
+
active,
|
|
1996
|
+
type,
|
|
1997
|
+
visible,
|
|
1998
|
+
segments: segmentsRepresentations,
|
|
1999
|
+
styles,
|
|
2000
|
+
viewportId,
|
|
2001
|
+
colorLUTIndex,
|
|
2002
|
+
config: {},
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
2005
|
+
|
|
2006
|
+
private _initSegmentationService() {
|
|
2007
|
+
eventTarget.addEventListener(
|
|
2008
|
+
csToolsEnums.Events.SEGMENTATION_MODIFIED,
|
|
2009
|
+
this._onSegmentationModifiedFromSource
|
|
2010
|
+
);
|
|
2011
|
+
|
|
2012
|
+
eventTarget.addEventListener(
|
|
2013
|
+
csToolsEnums.Events.SEGMENTATION_REMOVED,
|
|
2014
|
+
this._onSegmentationRemovedFromSource
|
|
2015
|
+
);
|
|
2016
|
+
|
|
2017
|
+
eventTarget.addEventListener(
|
|
2018
|
+
csToolsEnums.Events.SEGMENTATION_DATA_MODIFIED,
|
|
2019
|
+
this._onSegmentationDataModifiedFromSource
|
|
2020
|
+
);
|
|
2021
|
+
|
|
2022
|
+
eventTarget.addEventListener(
|
|
2023
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_MODIFIED,
|
|
2024
|
+
this._onSegmentationRepresentationModifiedFromSource
|
|
2025
|
+
);
|
|
2026
|
+
|
|
2027
|
+
eventTarget.addEventListener(
|
|
2028
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_ADDED,
|
|
2029
|
+
this._onSegmentationRepresentationModifiedFromSource
|
|
2030
|
+
);
|
|
2031
|
+
|
|
2032
|
+
eventTarget.addEventListener(
|
|
2033
|
+
csToolsEnums.Events.SEGMENTATION_REPRESENTATION_REMOVED,
|
|
2034
|
+
this._onSegmentationRepresentationRemovedFromSource
|
|
2035
|
+
);
|
|
2036
|
+
|
|
2037
|
+
eventTarget.addEventListener(
|
|
2038
|
+
csToolsEnums.Events.SEGMENTATION_ADDED,
|
|
2039
|
+
this._onSegmentationAddedFromSource
|
|
2040
|
+
);
|
|
2041
|
+
|
|
2042
|
+
eventTarget.addEventListener(
|
|
2043
|
+
csToolsEnums.Events.ANNOTATION_CUT_MERGE_PROCESS_COMPLETED,
|
|
2044
|
+
this._onAnnotationCutMergeProcessCompletedFromSource
|
|
2045
|
+
);
|
|
2046
|
+
}
|
|
2047
|
+
|
|
2048
|
+
private getCornerstoneSegmentation(segmentationId: string) {
|
|
2049
|
+
return cstSegmentation.state.getSegmentation(segmentationId);
|
|
2050
|
+
}
|
|
2051
|
+
|
|
2052
|
+
private _highlightLabelmap(
|
|
2053
|
+
segmentIndex: number,
|
|
2054
|
+
alpha: number,
|
|
2055
|
+
hideOthers: boolean,
|
|
2056
|
+
segments: cstTypes.Segment[],
|
|
2057
|
+
viewportId: string,
|
|
2058
|
+
animationLength: number,
|
|
2059
|
+
representation: cstTypes.SegmentationRepresentation,
|
|
2060
|
+
animationFunctionType: EasingFunctionEnum,
|
|
2061
|
+
highlight: SegmentHighlight
|
|
2062
|
+
) {
|
|
2063
|
+
const { segmentationId } = representation;
|
|
2064
|
+
const newSegmentSpecificConfig = {
|
|
2065
|
+
fillAlpha: alpha,
|
|
2066
|
+
};
|
|
2067
|
+
|
|
2068
|
+
if (hideOthers) {
|
|
2069
|
+
throw new Error('hideOthers is not working right now');
|
|
2070
|
+
}
|
|
2071
|
+
|
|
2072
|
+
const { fillAlpha } = this.getStyle({
|
|
2073
|
+
viewportId,
|
|
2074
|
+
segmentationId,
|
|
2075
|
+
type: LABELMAP,
|
|
2076
|
+
segmentIndex,
|
|
2077
|
+
}) as cstTypes.LabelmapStyle;
|
|
2078
|
+
|
|
2079
|
+
let startTime: number = null;
|
|
2080
|
+
const animation = this._segmentHighlightFrame(highlight, (timestamp: number) => {
|
|
2081
|
+
if (startTime === null) {
|
|
2082
|
+
startTime = timestamp;
|
|
2083
|
+
}
|
|
2084
|
+
|
|
2085
|
+
const elapsed = timestamp - startTime;
|
|
2086
|
+
const progress = Math.min(elapsed / animationLength, 1);
|
|
2087
|
+
|
|
2088
|
+
const easingFunction = EasingFunctionMap.get(animationFunctionType);
|
|
2089
|
+
|
|
2090
|
+
cstSegmentation.config.style.setStyle(
|
|
2091
|
+
{
|
|
2092
|
+
segmentationId,
|
|
2093
|
+
segmentIndex,
|
|
2094
|
+
type: LABELMAP,
|
|
2095
|
+
},
|
|
2096
|
+
{
|
|
2097
|
+
fillAlpha: easingFunction(progress, fillAlpha),
|
|
2098
|
+
}
|
|
2099
|
+
);
|
|
2100
|
+
|
|
2101
|
+
if (progress < 1) {
|
|
2102
|
+
requestAnimationFrame(animation);
|
|
2103
|
+
} else {
|
|
2104
|
+
this._completeSegmentHighlight(highlight);
|
|
2105
|
+
}
|
|
2106
|
+
});
|
|
2107
|
+
|
|
2108
|
+
requestAnimationFrame(animation);
|
|
2109
|
+
}
|
|
2110
|
+
|
|
2111
|
+
private _highlightContour(
|
|
2112
|
+
segmentIndex: number,
|
|
2113
|
+
alpha: number,
|
|
2114
|
+
hideOthers: boolean,
|
|
2115
|
+
segments: cstTypes.Segment[],
|
|
2116
|
+
viewportId: string,
|
|
2117
|
+
animationLength: number,
|
|
2118
|
+
representation: cstTypes.SegmentationRepresentation,
|
|
2119
|
+
animationFunctionType: EasingFunctionEnum,
|
|
2120
|
+
highlight: SegmentHighlight
|
|
2121
|
+
) {
|
|
2122
|
+
const { segmentationId } = representation;
|
|
2123
|
+
const startTime = performance.now();
|
|
2124
|
+
|
|
2125
|
+
const prevStyle = cstSegmentation.config.style.getStyle({
|
|
2126
|
+
type: CONTOUR,
|
|
2127
|
+
}) as cstTypes.ContourStyle;
|
|
2128
|
+
|
|
2129
|
+
const prevOutlineWidth = prevStyle.outlineWidth;
|
|
2130
|
+
|
|
2131
|
+
const animate = this._segmentHighlightFrame(highlight, (currentTime: number) => {
|
|
2132
|
+
const progress = (currentTime - startTime) / animationLength;
|
|
2133
|
+
if (progress >= 1) {
|
|
2134
|
+
this._completeSegmentHighlight(highlight);
|
|
2135
|
+
return;
|
|
2136
|
+
}
|
|
2137
|
+
|
|
2138
|
+
const OUTLINE_ANIMATION_SCALE_FACTOR = 5;
|
|
2139
|
+
const easingFunction = EasingFunctionMap.get(animationFunctionType);
|
|
2140
|
+
|
|
2141
|
+
cstSegmentation.config.style.setStyle(
|
|
2142
|
+
{
|
|
2143
|
+
segmentationId,
|
|
2144
|
+
segmentIndex,
|
|
2145
|
+
type: CONTOUR,
|
|
2146
|
+
},
|
|
2147
|
+
{
|
|
2148
|
+
outlineWidth: easingFunction(progress, prevOutlineWidth, OUTLINE_ANIMATION_SCALE_FACTOR),
|
|
2149
|
+
}
|
|
2150
|
+
);
|
|
2151
|
+
|
|
2152
|
+
requestAnimationFrame(animate);
|
|
2153
|
+
});
|
|
2154
|
+
|
|
2155
|
+
requestAnimationFrame(animate);
|
|
2156
|
+
}
|
|
2157
|
+
|
|
2158
|
+
private _toggleSegmentationRepresentationVisibility = (
|
|
2159
|
+
viewportId: string,
|
|
2160
|
+
segmentationId: string,
|
|
2161
|
+
type: csToolsEnums.SegmentationRepresentations
|
|
2162
|
+
): void => {
|
|
2163
|
+
const segmentsHidden = cstSegmentation.config.visibility.getHiddenSegmentIndices(viewportId, {
|
|
2164
|
+
segmentationId,
|
|
2165
|
+
type,
|
|
2166
|
+
});
|
|
2167
|
+
|
|
2168
|
+
const currentVisibility = segmentsHidden.size === 0;
|
|
2169
|
+
this._setSegmentationRepresentationVisibility(
|
|
2170
|
+
viewportId,
|
|
2171
|
+
segmentationId,
|
|
2172
|
+
type,
|
|
2173
|
+
!currentVisibility
|
|
2174
|
+
);
|
|
2175
|
+
};
|
|
2176
|
+
|
|
2177
|
+
private _setActiveSegment(segmentationId: string, segmentIndex: number) {
|
|
2178
|
+
cstSegmentation.segmentIndex.setActiveSegmentIndex(segmentationId, segmentIndex);
|
|
2179
|
+
}
|
|
2180
|
+
|
|
2181
|
+
private _getVolumeIdForDisplaySet(displaySet) {
|
|
2182
|
+
const volumeLoaderSchema = displaySet.volumeLoaderSchema ?? VOLUME_LOADER_SCHEME;
|
|
2183
|
+
|
|
2184
|
+
return `${volumeLoaderSchema}:${displaySet.displaySetInstanceUID}`;
|
|
2185
|
+
}
|
|
2186
|
+
|
|
2187
|
+
private _getSegmentCenter(
|
|
2188
|
+
segmentationId: string,
|
|
2189
|
+
segmentIndex: number
|
|
2190
|
+
): { image?: csTypes.Point3; world: csTypes.Point3 } | undefined {
|
|
2191
|
+
const segmentation = this.getSegmentation(segmentationId);
|
|
2192
|
+
|
|
2193
|
+
if (!segmentation) {
|
|
2194
|
+
return;
|
|
2195
|
+
}
|
|
2196
|
+
|
|
2197
|
+
const { segments } = segmentation;
|
|
2198
|
+
|
|
2199
|
+
const { cachedStats } = segments[segmentIndex];
|
|
2200
|
+
|
|
2201
|
+
if (cachedStats?.center) {
|
|
2202
|
+
const { center } = cachedStats;
|
|
2203
|
+
|
|
2204
|
+
return center as { image: csTypes.Point3; world: csTypes.Point3 };
|
|
2205
|
+
}
|
|
2206
|
+
|
|
2207
|
+
if (cachedStats?.namedStats?.center) {
|
|
2208
|
+
return {
|
|
2209
|
+
world: cachedStats.namedStats.center.value,
|
|
2210
|
+
};
|
|
2211
|
+
}
|
|
2212
|
+
}
|
|
2213
|
+
|
|
2214
|
+
private _setSegmentLockedStatus(segmentationId: string, segmentIndex: number, isLocked: boolean) {
|
|
2215
|
+
cstSegmentation.segmentLocking.setSegmentIndexLocked(segmentationId, segmentIndex, isLocked);
|
|
2216
|
+
}
|
|
2217
|
+
|
|
2218
|
+
private _setSegmentVisibility(
|
|
2219
|
+
viewportId: string,
|
|
2220
|
+
segmentationId: string,
|
|
2221
|
+
segmentIndex: number,
|
|
2222
|
+
isVisible: boolean,
|
|
2223
|
+
type?: csToolsEnums.SegmentationRepresentations
|
|
2224
|
+
) {
|
|
2225
|
+
cstSegmentation.config.visibility.setSegmentIndexVisibility(
|
|
2226
|
+
viewportId,
|
|
2227
|
+
{
|
|
2228
|
+
segmentationId,
|
|
2229
|
+
type,
|
|
2230
|
+
},
|
|
2231
|
+
segmentIndex,
|
|
2232
|
+
isVisible
|
|
2233
|
+
);
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
private _setSegmentLabel(segmentationId: string, segmentIndex: number, segmentLabel: string) {
|
|
2237
|
+
const segmentation = this.getCornerstoneSegmentation(segmentationId);
|
|
2238
|
+
const { segments } = segmentation;
|
|
2239
|
+
|
|
2240
|
+
segments[segmentIndex].label = segmentLabel;
|
|
2241
|
+
|
|
2242
|
+
cstSegmentation.updateSegmentations([
|
|
2243
|
+
{
|
|
2244
|
+
segmentationId,
|
|
2245
|
+
payload: {
|
|
2246
|
+
segments,
|
|
2247
|
+
},
|
|
2248
|
+
},
|
|
2249
|
+
]);
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2252
|
+
private _onSegmentationDataModifiedFromSource = evt => {
|
|
2253
|
+
const { segmentationId } = evt.detail;
|
|
2254
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_DATA_MODIFIED, {
|
|
2255
|
+
segmentationId,
|
|
2256
|
+
});
|
|
2257
|
+
};
|
|
2258
|
+
|
|
2259
|
+
private _onSegmentationRepresentationModifiedFromSource = evt => {
|
|
2260
|
+
const { segmentationId, viewportId } = evt.detail;
|
|
2261
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED, {
|
|
2262
|
+
segmentationId,
|
|
2263
|
+
viewportId,
|
|
2264
|
+
});
|
|
2265
|
+
};
|
|
2266
|
+
|
|
2267
|
+
private _onSegmentationRepresentationRemovedFromSource = evt => {
|
|
2268
|
+
const { segmentationId, viewportId } = evt.detail;
|
|
2269
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_REPRESENTATION_REMOVED, {
|
|
2270
|
+
segmentationId,
|
|
2271
|
+
viewportId,
|
|
2272
|
+
});
|
|
2273
|
+
};
|
|
2274
|
+
|
|
2275
|
+
private _onSegmentationModifiedFromSource = (
|
|
2276
|
+
evt: cstTypes.EventTypes.SegmentationModifiedEventType
|
|
2277
|
+
) => {
|
|
2278
|
+
const { segmentationId } = evt.detail;
|
|
2279
|
+
|
|
2280
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_MODIFIED, {
|
|
2281
|
+
segmentationId,
|
|
2282
|
+
});
|
|
2283
|
+
};
|
|
2284
|
+
|
|
2285
|
+
private _onSegmentationAddedFromSource = (
|
|
2286
|
+
evt: cstTypes.EventTypes.SegmentationAddedEventType
|
|
2287
|
+
) => {
|
|
2288
|
+
const { segmentationId } = evt.detail;
|
|
2289
|
+
|
|
2290
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_ADDED, {
|
|
2291
|
+
segmentationId,
|
|
2292
|
+
});
|
|
2293
|
+
};
|
|
2294
|
+
|
|
2295
|
+
private _onSegmentationRemovedFromSource = (
|
|
2296
|
+
evt: cstTypes.EventTypes.SegmentationRemovedEventType
|
|
2297
|
+
) => {
|
|
2298
|
+
const { segmentationId } = evt.detail;
|
|
2299
|
+
|
|
2300
|
+
this._segmentHighlights.forEach((highlight, key) => {
|
|
2301
|
+
if (highlight.segmentationId === segmentationId) {
|
|
2302
|
+
this._segmentHighlights.delete(key);
|
|
2303
|
+
}
|
|
2304
|
+
});
|
|
2305
|
+
|
|
2306
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_REMOVED, {
|
|
2307
|
+
segmentationId,
|
|
2308
|
+
});
|
|
2309
|
+
};
|
|
2310
|
+
|
|
2311
|
+
private _onAnnotationCutMergeProcessCompletedFromSource = evt => {
|
|
2312
|
+
const { segmentationId } = evt.detail;
|
|
2313
|
+
this._broadcastEvent(this.EVENTS.SEGMENTATION_ANNOTATION_CUT_MERGE_PROCESS_COMPLETED, {
|
|
2314
|
+
segmentationId,
|
|
2315
|
+
});
|
|
2316
|
+
};
|
|
2317
|
+
|
|
2318
|
+
/**
|
|
2319
|
+
* Gets slice indices for contour segmentations by checking view references from annotation metadata
|
|
2320
|
+
* @private
|
|
2321
|
+
*/
|
|
2322
|
+
protected _getContourViewReferences(
|
|
2323
|
+
segmentationId: string,
|
|
2324
|
+
viewport: csTypes.IViewport,
|
|
2325
|
+
segmentIndex?: number
|
|
2326
|
+
): Map<number, ViewReference> | undefined {
|
|
2327
|
+
const segmentation = cstSegmentation.state.getSegmentation(segmentationId);
|
|
2328
|
+
const contourData = segmentation.representationData[CONTOUR];
|
|
2329
|
+
|
|
2330
|
+
if (!contourData || !contourData.annotationUIDsMap) {
|
|
2331
|
+
return;
|
|
2332
|
+
}
|
|
2333
|
+
|
|
2334
|
+
const viewReferences = new Map<number, ViewReference>();
|
|
2335
|
+
// Iterate through the annotationUIDsMap
|
|
2336
|
+
contourData.annotationUIDsMap.forEach((annotationUIDs, currentSegmentIndex) => {
|
|
2337
|
+
// Filter by segment index if specified
|
|
2338
|
+
if (segmentIndex !== undefined && currentSegmentIndex !== segmentIndex) {
|
|
2339
|
+
return;
|
|
2340
|
+
}
|
|
2341
|
+
// Process each annotation UID
|
|
2342
|
+
annotationUIDs.forEach(annotationUID => {
|
|
2343
|
+
const annotation = cstAnnotation.state.getAnnotation(annotationUID);
|
|
2344
|
+
|
|
2345
|
+
if (!annotation?.metadata) {
|
|
2346
|
+
return;
|
|
2347
|
+
}
|
|
2348
|
+
const { metadata } = annotation;
|
|
2349
|
+
// Check if the viewport can view this annotation's view reference with navigation
|
|
2350
|
+
const isViewable = viewport.isReferenceViewable(metadata, {
|
|
2351
|
+
withNavigation: true,
|
|
2352
|
+
});
|
|
2353
|
+
|
|
2354
|
+
if (!isViewable) {
|
|
2355
|
+
return;
|
|
2356
|
+
}
|
|
2357
|
+
|
|
2358
|
+
const { sliceIndex } = metadata;
|
|
2359
|
+
if (sliceIndex === undefined) {
|
|
2360
|
+
console.warn("Can't find slice index:", metadata);
|
|
2361
|
+
return;
|
|
2362
|
+
}
|
|
2363
|
+
|
|
2364
|
+
viewReferences.set(sliceIndex, metadata);
|
|
2365
|
+
});
|
|
2366
|
+
});
|
|
2367
|
+
|
|
2368
|
+
if (viewReferences.size === 0) {
|
|
2369
|
+
return;
|
|
2370
|
+
}
|
|
2371
|
+
return viewReferences;
|
|
2372
|
+
}
|
|
2373
|
+
}
|
|
2374
|
+
|
|
2375
|
+
export default SegmentationService;
|
|
2376
|
+
export { EVENTS, VALUE_TYPES };
|