Skip to content
This repository has been archived by the owner on Aug 29, 2024. It is now read-only.

feat(auto hydration): Auto hydration of derived series #1

Merged
merged 5 commits into from
Oct 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ohif-idc-mode",
"version": "0.0.1",
"version": "0.0.2",
"description": "OHIF IDC Mode",
"author": "IDC",
"license": "MIT",
Expand Down
26 changes: 24 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { hotkeys } from '@ohif/core';
import toolbarButtons from './toolbarButtons.js';
import { id } from './id.js';
import initToolGroups from './initToolGroups.js';
import loadDerivedDisplaySets from './loadDerivedDisplaySets';
import { eventTarget, EVENTS } from '@cornerstonejs/core';

// Allow this mode by excluding non-imaging modalities such as SR, SEG
// Also, SM is not a simple imaging modalities, so exclude it.
Expand Down Expand Up @@ -65,6 +67,7 @@ const extensionDependencies = {

function modeFactory() {
let _activatePanelTriggersSubscriptions = [];
let boundedLoadDerivedDisplaySets;
return {
// TODO: We're using this as a route segment
// We should not be.
Expand All @@ -76,15 +79,23 @@ function modeFactory() {
onModeInit: ({ extensionManager }) => {
extensionManager.setActiveDataSource('idc-dicomweb');
},
onModeEnter: ({ servicesManager, extensionManager, commandsManager }) => {
onModeEnter: ({ servicesManager, extensionManager, commandsManager, appConfig }) => {
const {
measurementService,
toolbarService,
toolGroupService,
panelService,
segmentationService,
} = servicesManager.services;
appConfig.disableEditing = true;
appConfig.minDisplaySetsToRunHP = 10;

boundedLoadDerivedDisplaySets = loadDerivedDisplaySets.bind(
null,
servicesManager,
extensionManager,
commandsManager
);
measurementService.clearMeasurements();

// Init Default and SR ToolGroups
Expand Down Expand Up @@ -155,16 +166,27 @@ function modeFactory() {
// },
// ]),
// ];
eventTarget.addEventListener(
EVENTS.STACK_VIEWPORT_NEW_STACK,
boundedLoadDerivedDisplaySets
);
},
onModeExit: ({ servicesManager }) => {
onModeExit: ({ servicesManager, appConfig }) => {
const {
toolGroupService,
syncGroupService,
toolbarService,
segmentationService,
cornerstoneViewportService,
} = servicesManager.services;
appConfig.disableEditing = false;
appConfig.minDisplaySetsToRunHP = undefined;


eventTarget.removeEventListener(
EVENTS.STACK_VIEWPORT_NEW_STACK,
boundedLoadDerivedDisplaySets
);
_activatePanelTriggersSubscriptions.forEach(sub => sub.unsubscribe());
_activatePanelTriggersSubscriptions = [];

Expand Down
143 changes: 143 additions & 0 deletions src/loadDerivedDisplaySets.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
import { hydrateRTDisplaySet } from '@ohif/extension-cornerstone-dicom-rt';
import { hydrateStructuredReport } from '@ohif/extension-cornerstone-dicom-sr';
import { cache as cs3DCache, volumeLoader } from '@cornerstonejs/core';

const delay = 0;

function getDerivedSequences(displaySetUID, servicesManager) {
const { displaySetService } = servicesManager.services;

const displaySetCache = displaySetService.getDisplaySetCache();
const derivedDisplaySets = [...displaySetCache.values()].filter(ds => {
if (ds?.getReferenceDisplaySet) {
ds?.getReferenceDisplaySet();
}
return (
(ds?.referencedDisplaySetInstanceUID === displaySetUID ||
ds.Modality === 'SR') &&
!ds?.isHydrated
);
});
return derivedDisplaySets;
}

export default function loadDerivedDisplaySets(
servicesManager,
extensionManager,
commandsManager,
evt
) {
async function checkVolume(displaySet, imageIds) {
const VOLUME_LOADER_SCHEME = 'cornerstoneStreamingImageVolume';
const volumeLoaderSchema =
displaySet.volumeLoaderSchema ?? VOLUME_LOADER_SCHEME;

const volumeId = `${volumeLoaderSchema}:${displaySet.displaySetInstanceUID}`;

let volume = cs3DCache.getVolume(volumeId);

if (!volume) {
volume = await volumeLoader.createAndCacheVolume(volumeId, { imageIds });
}
}

const {
userAuthenticationService,
segmentationService,
displaySetService,
viewportGridService,
} = servicesManager.services;

const { viewports } = viewportGridService.getState();
const { viewportId, imageIds } = evt.detail;
const mainViewport = viewports.get(viewportId);
if (mainViewport) {
if (mainViewport.displaySetInstanceUIDs.length === 1) {
const mainDisplaySet = displaySetService.getDisplaySetByUID(
mainViewport.displaySetInstanceUIDs[0]
);
const derivedDisplaySets = getDerivedSequences(
mainViewport.displaySetInstanceUIDs[0],
servicesManager
);

derivedDisplaySets.forEach(async displaySet => {
const headers = userAuthenticationService.getAuthorizationHeader();
await checkVolume(mainDisplaySet, imageIds);
await displaySet.load({ headers });
const derivedDisplaySetInstanceUID = displaySet.displaySetInstanceUID;

if (displaySet.Modality === 'SEG') {
let segmentationId = null;

// We need the hydration to notify panels about the new segmentation added
const suppressEvents = false;

segmentationId = await segmentationService.createSegmentationForSEGDisplaySet(
displaySet,
segmentationId,
suppressEvents
);
setTimeout(() => {
if (
!mainViewport.displaySetInstanceUIDs.includes(
derivedDisplaySetInstanceUID
)
) {
mainViewport.displaySetInstanceUIDs.push(
derivedDisplaySetInstanceUID
);
}
commandsManager.runCommand('loadSegmentationDisplaySetsForViewport', {
displaySets: [displaySet],
viewportId,
});
}, delay);
} else if (displaySet.Modality === 'RTSTRUCT') {
let segmentationId = null;

// We need the hydration to notify panels about the new segmentation added
const suppressEvents = false;

segmentationId = await segmentationService.createSegmentationForRTDisplaySet(
displaySet,
segmentationId,
suppressEvents
);
setTimeout(() => {
if (
!mainViewport.displaySetInstanceUIDs.includes(
derivedDisplaySetInstanceUID
)
) {
mainViewport.displaySetInstanceUIDs.push(
derivedDisplaySetInstanceUID
);
}
hydrateRTDisplaySet({
rtDisplaySet: displaySet,
viewportId,
servicesManager,
});
}, delay);
} else if (displaySet.Modality === 'SR') {
setTimeout(() => {
if (
!mainViewport.displaySetInstanceUIDs.includes(
derivedDisplaySetInstanceUID
)
) {
mainViewport.displaySetInstanceUIDs.push(
derivedDisplaySetInstanceUID
);
}
hydrateStructuredReport(
{ servicesManager, extensionManager },
derivedDisplaySetInstanceUID
);
}, delay);
}
});
}
}
}