feature(segmentation): Add contour tools and utilities in a side panel (#5517)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: Dan Rukas <dan.rukas@gmail.com>
This commit is contained in:
73 files changed
+2770
-435
No files matched your search
@@ -0,0 +1,74 @@
|
||||
import { utilities as csUtils } from '@cornerstonejs/core';
|
||||
import { SegmentationRepresentations } from '@cornerstonejs/tools/enums';
|
||||
import i18n from '@ohif/i18n';
|
||||
import { ServicesManager } from '@ohif/core';
|
||||
|
||||
function _createDefaultSegments(createInitialSegment?: boolean) {
|
||||
return createInitialSegment
|
||||
? {
|
||||
1: {
|
||||
label: `${i18n.t('Tools:Segment')} 1`,
|
||||
active: true,
|
||||
},
|
||||
}
|
||||
: {};
|
||||
}
|
||||
|
||||
type CreateSegmentationForViewportOptions = {
|
||||
displaySetInstanceUID?: string;
|
||||
label?: string;
|
||||
segmentationId?: string;
|
||||
createInitialSegment?: boolean;
|
||||
};
|
||||
|
||||
type CreateSegmentationForViewportParams = {
|
||||
viewportId: string;
|
||||
options?: CreateSegmentationForViewportOptions;
|
||||
segmentationType: SegmentationRepresentations;
|
||||
};
|
||||
|
||||
/**
|
||||
* Creates a segmentation for the active viewport
|
||||
*
|
||||
* The created segmentation will be registered as a display set and also added
|
||||
* as a segmentation representation to the viewport.
|
||||
*/
|
||||
export async function createSegmentationForViewport(
|
||||
servicesManager: ServicesManager,
|
||||
{ viewportId, options = {}, segmentationType }: CreateSegmentationForViewportParams
|
||||
): Promise<string> {
|
||||
const { viewportGridService, displaySetService, segmentationService } = servicesManager.services;
|
||||
const { viewports } = viewportGridService.getState();
|
||||
const targetViewportId = viewportId;
|
||||
|
||||
const viewport = viewports.get(targetViewportId);
|
||||
|
||||
// Todo: add support for multiple display sets
|
||||
const displaySetInstanceUID = options.displaySetInstanceUID || viewport.displaySetInstanceUIDs[0];
|
||||
|
||||
const segs = segmentationService.getSegmentations();
|
||||
|
||||
const label = options.label || `${i18n.t('Tools:Segmentation')} ${segs.length + 1}`;
|
||||
const segmentationId = options.segmentationId || `${csUtils.uuidv4()}`;
|
||||
|
||||
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID);
|
||||
|
||||
const segmentationCreationOptions = {
|
||||
label,
|
||||
segmentationId,
|
||||
segments: _createDefaultSegments(options.createInitialSegment),
|
||||
};
|
||||
|
||||
// This will create the segmentation and register it as a display set
|
||||
const generatedSegmentationId = await (segmentationType === SegmentationRepresentations.Labelmap
|
||||
? segmentationService.createLabelmapForDisplaySet(displaySet, segmentationCreationOptions)
|
||||
: segmentationService.createContourForDisplaySet(displaySet, segmentationCreationOptions));
|
||||
|
||||
// Also add the segmentation representation to the viewport
|
||||
await segmentationService.addSegmentationRepresentation(viewportId, {
|
||||
segmentationId,
|
||||
type: segmentationType,
|
||||
});
|
||||
|
||||
return generatedSegmentationId;
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import promptHydrationDialog, {
|
||||
HydrationSRResult,
|
||||
} from './promptHydrationDialog';
|
||||
import { getCenterExtent } from './getCenterExtent';
|
||||
import { createSegmentationForViewport } from './createSegmentationForViewport';
|
||||
|
||||
const utils = {
|
||||
handleSegmentChange,
|
||||
@@ -18,6 +19,7 @@ const utils = {
|
||||
setupSegmentationModifiedHandler,
|
||||
promptHydrationDialog,
|
||||
getCenterExtent,
|
||||
createSegmentationForViewport,
|
||||
};
|
||||
|
||||
export type { HydrationDialogProps, HydrationCallback, HydrationSRResult };
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
import { cache, metaData } from '@cornerstonejs/core';
|
||||
import { Types as cstTypes } from '@cornerstonejs/tools';
|
||||
|
||||
/**
|
||||
* Checks if a labelmap segmentation has exportable data
|
||||
* A label map is exportable if it has any pixel data and
|
||||
* it is referenced by a display set that is reconstructable.
|
||||
*
|
||||
* @param labelmap - The labelmap representation data
|
||||
* @param displaySetService - The display set service instance
|
||||
* @returns boolean - Whether the labelmap has exportable data
|
||||
*/
|
||||
export const hasExportableLabelMapData = (
|
||||
labelmap: cstTypes.LabelmapSegmentationData | undefined,
|
||||
displaySetService: any
|
||||
): boolean => {
|
||||
if (!labelmap) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const imageIds = labelmap?.imageIds;
|
||||
if (!imageIds?.length) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const hasPixelData = imageIds.some(imageId => {
|
||||
const pixelData = cache.getImage(imageId)?.getPixelData();
|
||||
if (!pixelData) {
|
||||
return false;
|
||||
}
|
||||
|
||||
for (let i = 0; i < pixelData.length; i++) {
|
||||
if (pixelData[i] !== 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!hasPixelData) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const referencedImageIds = labelmap?.referencedImageIds;
|
||||
|
||||
if (!referencedImageIds) {
|
||||
return false;
|
||||
}
|
||||
const firstImageId = referencedImageIds[0];
|
||||
const instance = metaData.get('instance', firstImageId);
|
||||
|
||||
if (!instance) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const SOPInstanceUID = instance.SOPInstanceUID || instance.SopInstanceUID;
|
||||
const SeriesInstanceUID = instance.SeriesInstanceUID;
|
||||
const displaySet = displaySetService.getDisplaySetForSOPInstanceUID(
|
||||
SOPInstanceUID,
|
||||
SeriesInstanceUID
|
||||
);
|
||||
|
||||
return displaySet?.isReconstructable;
|
||||
};
|
||||
|
||||
/**
|
||||
* Checks if a contour segmentation has exportable data
|
||||
* A contour is exportable if it has any contour annotation/geometry data.
|
||||
*
|
||||
* @param contour - The contour representation data
|
||||
* @returns boolean - Whether the contour has exportable data
|
||||
*/
|
||||
export const hasExportableContourData = (
|
||||
contour: cstTypes.ContourSegmentationData | undefined
|
||||
): boolean => {
|
||||
if (!contour) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const contourAnnotationUIDsMap = contour?.annotationUIDsMap;
|
||||
|
||||
if (!contourAnnotationUIDsMap) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return contourAnnotationUIDsMap.size > 0;
|
||||
};
|
||||
@@ -1,5 +1,7 @@
|
||||
import * as cornerstoneTools from '@cornerstonejs/tools';
|
||||
import { updateSegmentationStats } from './updateSegmentationStats';
|
||||
import { useSelectedSegmentationsForViewportStore } from '../stores';
|
||||
import { SegmentationRepresentations } from '@cornerstonejs/tools/enums';
|
||||
|
||||
/**
|
||||
* Sets up the handler for segmentation data modification events
|
||||
@@ -110,3 +112,49 @@ export function setupSegmentationModifiedHandler({ segmentationService }) {
|
||||
|
||||
return { unsubscribe };
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets up auto tab switching for when the first segmentation is added into the viewer.
|
||||
*/
|
||||
export function setUpSelectedSegmentationsForViewportHandler({ segmentationService }) {
|
||||
const selectedSegmentationsForViewportEvents = [
|
||||
segmentationService.EVENTS.SEGMENTATION_MODIFIED,
|
||||
segmentationService.EVENTS.SEGMENTATION_REPRESENTATION_MODIFIED,
|
||||
];
|
||||
|
||||
const unsubscribeSelectedSegmentationsForViewportEvents = selectedSegmentationsForViewportEvents
|
||||
.map(eventName =>
|
||||
segmentationService.subscribe(eventName, event => {
|
||||
const { viewportId } = event;
|
||||
|
||||
if (!viewportId) {
|
||||
return;
|
||||
}
|
||||
|
||||
const { selectedSegmentationsForViewport, setSelectedSegmentationsForViewport } =
|
||||
useSelectedSegmentationsForViewportStore.getState();
|
||||
|
||||
const representations = segmentationService.getSegmentationRepresentations(viewportId);
|
||||
|
||||
const activeRepresentation = representations.find(representation => representation.active);
|
||||
|
||||
const typeToSegmentationIdMap =
|
||||
selectedSegmentationsForViewport[viewportId] ??
|
||||
new Map<SegmentationRepresentations, string>();
|
||||
|
||||
if (activeRepresentation) {
|
||||
typeToSegmentationIdMap.set(
|
||||
activeRepresentation.type,
|
||||
activeRepresentation.segmentationId
|
||||
);
|
||||
} else {
|
||||
typeToSegmentationIdMap.clear();
|
||||
}
|
||||
|
||||
setSelectedSegmentationsForViewport(viewportId, typeToSegmentationIdMap);
|
||||
})
|
||||
)
|
||||
.map(subscription => subscription.unsubscribe);
|
||||
|
||||
return { unsubscribeSelectedSegmentationsForViewportEvents };
|
||||
}
|
||||
@@ -2,11 +2,13 @@ import { setUpSegmentationEventHandlers } from './setUpSegmentationEventHandlers
|
||||
import {
|
||||
setupSegmentationDataModifiedHandler,
|
||||
setupSegmentationModifiedHandler,
|
||||
setUpSelectedSegmentationsForViewportHandler,
|
||||
} from './segmentationHandlers';
|
||||
|
||||
jest.mock('./segmentationHandlers', () => ({
|
||||
setupSegmentationDataModifiedHandler: jest.fn(),
|
||||
setupSegmentationModifiedHandler: jest.fn(),
|
||||
setUpSelectedSegmentationsForViewportHandler: jest.fn(),
|
||||
}));
|
||||
|
||||
describe('setUpSegmentationEventHandlers', () => {
|
||||
@@ -38,6 +40,7 @@ describe('setUpSegmentationEventHandlers', () => {
|
||||
const mockUnsubscribeDataModified = jest.fn();
|
||||
const mockUnsubscribeModified = jest.fn();
|
||||
const mockUnsubscribeCreated = jest.fn();
|
||||
const mockUnsubscribeSelectedSegmentationsForViewportEvents = [jest.fn(), jest.fn()];
|
||||
|
||||
const defaultParameters = {
|
||||
servicesManager: mockServicesManager as unknown as AppTypes.ServicesManager,
|
||||
@@ -52,6 +55,10 @@ describe('setUpSegmentationEventHandlers', () => {
|
||||
(setupSegmentationModifiedHandler as jest.Mock).mockReturnValue({
|
||||
unsubscribe: mockUnsubscribeModified,
|
||||
});
|
||||
(setUpSelectedSegmentationsForViewportHandler as jest.Mock).mockReturnValue({
|
||||
unsubscribeSelectedSegmentationsForViewportEvents:
|
||||
mockUnsubscribeSelectedSegmentationsForViewportEvents,
|
||||
});
|
||||
mockSegmentationService.subscribe.mockReturnValue({
|
||||
unsubscribe: mockUnsubscribeCreated,
|
||||
});
|
||||
@@ -92,6 +99,7 @@ describe('setUpSegmentationEventHandlers', () => {
|
||||
mockUnsubscribeDataModified,
|
||||
mockUnsubscribeModified,
|
||||
mockUnsubscribeCreated,
|
||||
...mockUnsubscribeSelectedSegmentationsForViewportEvents,
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import {
|
||||
setUpSelectedSegmentationsForViewportHandler,
|
||||
setupSegmentationDataModifiedHandler,
|
||||
setupSegmentationModifiedHandler,
|
||||
} from './segmentationHandlers';
|
||||
@@ -28,7 +29,9 @@ export const setUpSegmentationEventHandlers = ({ servicesManager, commandsManage
|
||||
|
||||
const segmentation = segmentationService.getSegmentation(segmentationId);
|
||||
const label = segmentation.cachedStats.info;
|
||||
const imageIds = segmentation.representationData.Labelmap.imageIds;
|
||||
const imageIds =
|
||||
segmentation.representationData?.Labelmap?.imageIds ??
|
||||
segmentation.representationData?.Contour?.imageIds;
|
||||
|
||||
// Create a display set for the segmentation
|
||||
const segmentationDisplaySet = {
|
||||
@@ -50,10 +53,16 @@ export const setUpSegmentationEventHandlers = ({ servicesManager, commandsManage
|
||||
}
|
||||
);
|
||||
|
||||
const { unsubscribeSelectedSegmentationsForViewportEvents } =
|
||||
setUpSelectedSegmentationsForViewportHandler({
|
||||
segmentationService,
|
||||
});
|
||||
|
||||
const unsubscriptions = [
|
||||
unsubscribeSegmentationDataModifiedHandler,
|
||||
unsubscribeSegmentationModifiedHandler,
|
||||
unsubscribeSegmentationCreated,
|
||||
...unsubscribeSelectedSegmentationsForViewportEvents,
|
||||
];
|
||||
|
||||
return { unsubscriptions };
|
||||
|
||||
Reference in new issue
Block a user