From dac16b2645a9bf5248980758f899eaaa496eedd7 Mon Sep 17 00:00:00 2001 From: Alireza Date: Tue, 12 Sep 2023 15:49:13 -0400 Subject: [PATCH] fix commands module --- .../src/commandsModule.ts | 55 +++++++++---------- .../HangingProtocolService.ts | 28 ++++++---- 2 files changed, 44 insertions(+), 39 deletions(-) diff --git a/extensions/cornerstone-dicom-seg/src/commandsModule.ts b/extensions/cornerstone-dicom-seg/src/commandsModule.ts index 0403166164..3d95c09c2c 100644 --- a/extensions/cornerstone-dicom-seg/src/commandsModule.ts +++ b/extensions/cornerstone-dicom-seg/src/commandsModule.ts @@ -179,21 +179,6 @@ const commandsModule = ({ segmentationService.hydrateSegmentation(segmentationId); }; - const boundFunctionMap = new Map(); - - async function createNewSegmentationWhenVolumeMounts({ viewportId, prevCamera, element }) { - const volumeViewport = cornerstoneViewportService.getCornerstoneViewport(viewportId); - volumeViewport.setCamera(prevCamera); - - await createSegmentationForVolume(); - - const boundFunction = boundFunctionMap.get(viewportId); - - if (boundFunction) { - element.removeEventListener(Enums.Events.VOLUME_VIEWPORT_NEW_VOLUME, boundFunction); - } - } - // the reference volume that is used to draw the segmentation. so check if the // volume exists in the cache (the target Viewport is already a volume viewport) const volumeExists = Array.from(cache._volumeCache.keys()).some(volumeId => @@ -218,22 +203,34 @@ const commandsModule = ({ return; } - const boundFunction = createNewSegmentationWhenVolumeMounts.bind(null, { - viewportId: viewportId, - prevCamera: prevCamera, - element: csViewport.element, - }); + const createNewSegmentationWhenVolumeMounts = async evt => { + const isTheActiveViewportVolumeMounted = evt.detail.volumeActors?.find(ac => + ac.uid.includes(referenceDisplaySetInstanceUID) + ); + + // Note: make sure to re-grab the viewport since it might have changed + // during the time it took for the volume to be mounted, for instance + // the stack viewport has been changed to a volume viewport + const volumeViewport = cornerstoneViewportService.getCornerstoneViewport(viewportId); + volumeViewport.setCamera(prevCamera); + + volumeViewport.element.removeEventListener( + Enums.Events.VOLUME_VIEWPORT_NEW_VOLUME, + createNewSegmentationWhenVolumeMounts + ); + + if (!isTheActiveViewportVolumeMounted) { + // it means it is one of those other updated viewports so just update the camera + return; + } - boundFunctionMap.set(targetViewportId, boundFunction); + await createSegmentationForVolume(); + }; - csViewport.element.addEventListener(Enums.Events.VOLUME_VIEWPORT_NEW_VOLUME, evt => { - if (evt.detail.volumeId.includes(referenceDisplaySetInstanceUID)) { - // only run it if the volumeId matches the referenceDisplaySetInstanceUID - // otherwise it might be other volumes that are not related to the segmentation - // or even the segmentation volume added itself - boundFunction(); - } - }); + csViewport.element.addEventListener( + Enums.Events.VOLUME_VIEWPORT_NEW_VOLUME, + createNewSegmentationWhenVolumeMounts + ); }); // Set the displaySets for the viewports that require to be updated diff --git a/platform/core/src/services/HangingProtocolService/HangingProtocolService.ts b/platform/core/src/services/HangingProtocolService/HangingProtocolService.ts index 3937fc93d4..c9a5587673 100644 --- a/platform/core/src/services/HangingProtocolService/HangingProtocolService.ts +++ b/platform/core/src/services/HangingProtocolService/HangingProtocolService.ts @@ -529,16 +529,24 @@ export default class HangingProtocolService extends PubSubService { } } else { // Clone each viewport to ensure independent objects - stage.viewports = stage.viewports.map((viewport, index) => ({ - ...viewport, - viewportOptions: { - ...(viewport.viewportOptions || defaultViewportOptions), - // Use 'default' for the first viewport, and either existing or new UUIDs for the rest. - viewportId: index === 0 ? 'default' : viewport.viewportOptions?.viewportId || uuidv4(), - }, - displaySets: viewport.displaySets || [], - })); + stage.viewports = stage.viewports.map((viewport, index) => { + const existingViewportId = viewport.viewportOptions?.viewportId; + return { + ...viewport, + viewportOptions: { + ...(viewport.viewportOptions || defaultViewportOptions), + // use provided viewportId when available, otherwise use default for first viewport + // and uuid for the rest + viewportId: existingViewportId + ? existingViewportId + : index === 0 + ? 'default' + : uuidv4(), + }, + displaySets: viewport.displaySets || [], + }; + }); stage.viewports.forEach(viewport => { viewport.displaySets.forEach(displaySet => { displaySet.options = displaySet.options || {}; @@ -1383,7 +1391,7 @@ export default class HangingProtocolService extends PubSubService { const matchingScores = []; let highestSeriesMatchingScore = 0; - console.log('ProtocolEngine::matchImages', studyMatchingRules, seriesMatchingRules); + // console.log('ProtocolEngine::matchImages', studyMatchingRules, seriesMatchingRules); const matchActiveOnly = this.protocol.numberOfPriorsReferenced === -1; this.studies.forEach((study, studyInstanceUIDsIndex) => { // Skip non-active if active only