Skip to content

Commit

Permalink
fix commands module
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi committed Sep 12, 2023
1 parent 48f1d48 commit dac16b2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 39 deletions.
55 changes: 26 additions & 29 deletions extensions/cornerstone-dicom-seg/src/commandsModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 || {};
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit dac16b2

Please sign in to comment.