Files
ohif-viewer/extensions/cornerstone/src/services/SyncGroupService/createHydrateSegmentationSynchronizer.ts
T

104 lines
3.6 KiB
TypeScript
Raw Normal View History

import { Enums as CoreEnums, Types, getEnabledElementByViewportId } from '@cornerstonejs/core';
2024-11-06 07:15:27 -05:00
import {
SynchronizerManager,
Synchronizer,
Enums,
Types as ToolsTypes,
} from '@cornerstonejs/tools';
2025-12-15 19:19:43 +05:30
import { isAnyDisplaySetCommon } from '../../utils/isAnyDisplaySetCommon';
2024-11-06 07:15:27 -05:00
const { createSynchronizer } = SynchronizerManager;
const { SEGMENTATION_REPRESENTATION_MODIFIED } = Enums.Events;
const { BlendModes } = CoreEnums;
2024-11-06 07:15:27 -05:00
export default function createHydrateSegmentationSynchronizer(
synchronizerName: string,
{ servicesManager, ...options }: { servicesManager: AppTypes.ServicesManager; options }
): Synchronizer {
const stackImageSynchronizer = createSynchronizer(
synchronizerName,
SEGMENTATION_REPRESENTATION_MODIFIED,
(synchronizerInstance, sourceViewport, targetViewport, sourceEvent) => {
return segmentationRepresentationModifiedCallback(
2024-11-06 07:15:27 -05:00
synchronizerInstance,
sourceViewport,
targetViewport,
sourceEvent,
{ servicesManager, options }
);
},
2024-11-06 07:15:27 -05:00
{
eventSource: 'eventTarget',
}
);
return stackImageSynchronizer;
}
2025-12-15 19:19:43 +05:30
/**
* This method will add the segmentation representation to any target viewports having:
*
* 1. the same FrameOfReferenceUID (FOR) as the segmentation representation, or
* 2. a shared DisplaySet with the source viewport when no FOR is present.
*/
2024-11-06 07:15:27 -05:00
const segmentationRepresentationModifiedCallback = async (
synchronizerInstance: Synchronizer,
sourceViewport: Types.IViewportId,
targetViewport: Types.IViewportId,
sourceEvent: Event,
{ servicesManager, options }: { servicesManager: AppTypes.ServicesManager; options: unknown }
) => {
const event = sourceEvent as ToolsTypes.EventTypes.SegmentationRepresentationModifiedEventType;
const { segmentationId, type: segmentationRepresentationType } = event.detail;
2025-12-15 19:19:43 +05:30
const { segmentationService, cornerstoneViewportService } = servicesManager.services;
2024-11-06 07:15:27 -05:00
const targetViewportId = targetViewport.viewportId;
2025-12-15 19:19:43 +05:30
const sourceViewportId = sourceViewport.viewportId;
2024-11-06 07:15:27 -05:00
const { viewport } = getEnabledElementByViewportId(targetViewportId);
2025-12-15 19:19:43 +05:30
const sourceViewportInfo = cornerstoneViewportService.getViewportInfo(sourceViewportId);
const targetViewportInfo = cornerstoneViewportService.getViewportInfo(targetViewportId);
const sourceDisplaySetUIDs = extractDisplaySetUIDs(sourceViewportInfo);
const targetDisplaySetUIDs = extractDisplaySetUIDs(targetViewportInfo);
2024-11-06 07:15:27 -05:00
2025-12-15 19:19:43 +05:30
const sharedDisplaySetExists = isAnyDisplaySetCommon(sourceDisplaySetUIDs, targetDisplaySetUIDs);
2024-11-06 07:15:27 -05:00
2025-12-15 19:19:43 +05:30
if (!sharedDisplaySetExists && !viewport.getFrameOfReferenceUID()) {
2024-11-06 07:15:27 -05:00
return;
}
const targetViewportRepresentation = segmentationService.getSegmentationRepresentations(
targetViewportId,
{ segmentationId }
);
if (targetViewportRepresentation.length > 0) {
return;
}
// Ensure the segmentation representation aligns with the target viewport type.
const type: Enums.SegmentationRepresentations =
viewport.type === CoreEnums.ViewportType.VOLUME_3D
? Enums.SegmentationRepresentations.Surface
: ((segmentationRepresentationType as Enums.SegmentationRepresentations) ??
Enums.SegmentationRepresentations.Labelmap);
2024-11-06 07:15:27 -05:00
await segmentationService.addSegmentationRepresentation(targetViewportId, {
segmentationId,
type,
config: {
blendMode:
viewport?.getBlendMode?.() === 1 ? BlendModes.LABELMAP_EDGE_PROJECTION_BLEND : undefined,
},
2024-11-06 07:15:27 -05:00
});
};
2025-12-15 19:19:43 +05:30
/**
* Extracts the displaySetInstanceUIDs from a viewportInfo.
*/
function extractDisplaySetUIDs(viewportInfo) {
return viewportInfo.getViewportData().data.map(ds => ds.displaySetInstanceUID);
}