* Fix: Segmentation "Hide All" fails when switching from 2D to 3D four-up viewport * Update based on the review comment on segmentation load in 3d fourup Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * fix: simplify the conditional logic * fix: playwright test * fix: playwright test * fix: render timeout in test --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> Co-authored-by: Belbin-GK <150322972+Belbin-GK@users.noreply.github.com> Co-authored-by: Devu Jayalekshmi <devu.jayalekshmi@trenser.com> Co-authored-by: Bill Wallace <wayfarer3130@gmail.com>
119 lines
4.1 KiB
TypeScript
119 lines
4.1 KiB
TypeScript
import { Enums as CoreEnums, Types, getEnabledElementByViewportId } from '@cornerstonejs/core';
|
|
import {
|
|
SynchronizerManager,
|
|
Synchronizer,
|
|
Enums,
|
|
Types as ToolsTypes,
|
|
} from '@cornerstonejs/tools';
|
|
|
|
import { isAnyDisplaySetCommon } from '../../utils/isAnyDisplaySetCommon';
|
|
import { isVolume3DViewportType } from '../../utils/getLegacyViewportType';
|
|
|
|
const { createSynchronizer } = SynchronizerManager;
|
|
const { SEGMENTATION_REPRESENTATION_MODIFIED } = Enums.Events;
|
|
const { BlendModes } = CoreEnums;
|
|
|
|
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(
|
|
synchronizerInstance,
|
|
sourceViewport,
|
|
targetViewport,
|
|
sourceEvent,
|
|
{ servicesManager, options }
|
|
);
|
|
},
|
|
{
|
|
eventSource: 'eventTarget',
|
|
}
|
|
);
|
|
|
|
return stackImageSynchronizer;
|
|
}
|
|
|
|
/**
|
|
* 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.
|
|
*/
|
|
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;
|
|
const { segmentationService, cornerstoneViewportService } = servicesManager.services;
|
|
|
|
const targetViewportId = targetViewport.viewportId;
|
|
const sourceViewportId = sourceViewport.viewportId;
|
|
|
|
const { viewport } = getEnabledElementByViewportId(targetViewportId);
|
|
const sourceViewportInfo = cornerstoneViewportService.getViewportInfo(sourceViewportId);
|
|
const targetViewportInfo = cornerstoneViewportService.getViewportInfo(targetViewportId);
|
|
|
|
const sourceDisplaySetUIDs = extractDisplaySetUIDs(sourceViewportInfo);
|
|
const targetDisplaySetUIDs = extractDisplaySetUIDs(targetViewportInfo);
|
|
|
|
const sharedDisplaySetExists = isAnyDisplaySetCommon(sourceDisplaySetUIDs, targetDisplaySetUIDs);
|
|
|
|
const targetFrameOfReferenceUID = viewport.getFrameOfReferenceUID();
|
|
const sourceFrameOfReferenceUID =
|
|
getEnabledElementByViewportId(sourceViewportId)?.viewport?.getFrameOfReferenceUID();
|
|
|
|
if (!sharedDisplaySetExists && !targetFrameOfReferenceUID) {
|
|
return;
|
|
}
|
|
|
|
if (!sharedDisplaySetExists && targetFrameOfReferenceUID !== sourceFrameOfReferenceUID) {
|
|
return;
|
|
}
|
|
|
|
const targetViewportRepresentation = segmentationService.getSegmentationRepresentations(
|
|
targetViewportId,
|
|
{ segmentationId }
|
|
);
|
|
|
|
if (targetViewportRepresentation.length > 0) {
|
|
return;
|
|
}
|
|
|
|
// Ensure the segmentation representation aligns with the target viewport type
|
|
const is3D = isVolume3DViewportType(viewport);
|
|
const requestedRepresentation =
|
|
segmentationRepresentationType as Enums.SegmentationRepresentations;
|
|
const { Surface, Labelmap } = Enums.SegmentationRepresentations;
|
|
|
|
const type: Enums.SegmentationRepresentations = is3D
|
|
? Surface
|
|
: requestedRepresentation && requestedRepresentation !== Surface
|
|
? requestedRepresentation
|
|
: Labelmap;
|
|
|
|
await segmentationService.addSegmentationRepresentation(targetViewportId, {
|
|
segmentationId,
|
|
type,
|
|
config: {
|
|
blendMode:
|
|
viewport?.getBlendMode?.() === 1 ? BlendModes.LABELMAP_EDGE_PROJECTION_BLEND : undefined,
|
|
},
|
|
});
|
|
};
|
|
|
|
/**
|
|
* Extracts the displaySetInstanceUIDs from a viewportInfo.
|
|
*/
|
|
function extractDisplaySetUIDs(viewportInfo) {
|
|
return viewportInfo.getViewportData().data.map(ds => ds.displaySetInstanceUID);
|
|
}
|