-
-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Miscellaneous fixes for stack image sync
- Loading branch information
1 parent
c49b833
commit 71b4827
Showing
13 changed files
with
451 additions
and
307 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 61 additions & 70 deletions
131
extensions/cornerstone/src/utils/stackSync/toggleStackImageSync.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,81 @@ | ||
import calculateViewportRegistrations from './calculateViewportRegistrations'; | ||
|
||
// [ { | ||
// synchronizerId: string, | ||
// viewports: [ { viewportId: string, renderingEngineId: string, index: number } , ...] | ||
// ]} | ||
let STACK_IMAGE_SYNC_GROUPS_INFO = []; | ||
|
||
export default function toggleStackImageSync({ toggledState, servicesManager, getEnabledElement }) { | ||
const disableSync = (syncName, servicesManager) => { | ||
const { syncGroupService, viewportGridService, displaySetService, cornerstoneViewportService } = | ||
servicesManager.services; | ||
const viewports = getViewports(viewportGridService, displaySetService); | ||
viewports.forEach(gridViewport => { | ||
const { viewportId } = gridViewport.viewportOptions; | ||
const viewport = cornerstoneViewportService.getCornerstoneViewport(viewportId); | ||
if (!viewport) { | ||
return; | ||
} | ||
syncGroupService.removeViewportFromSyncGroup( | ||
viewport.id, | ||
viewport.getRenderingEngine().id, | ||
syncName | ||
); | ||
}); | ||
}; | ||
|
||
if (!toggledState) { | ||
STACK_IMAGE_SYNC_GROUPS_INFO.forEach(syncGroupInfo => { | ||
const { viewports, synchronizerId } = syncGroupInfo; | ||
|
||
viewports.forEach(({ viewportId, renderingEngineId }) => { | ||
syncGroupService.removeViewportFromSyncGroup(viewportId, renderingEngineId, synchronizerId); | ||
}); | ||
}); | ||
|
||
return; | ||
} | ||
|
||
STACK_IMAGE_SYNC_GROUPS_INFO = []; | ||
|
||
// create synchronization groups and add viewports | ||
const { viewports } = viewportGridService.getState(); | ||
const getViewports = (viewportGridService, displaySetService) => { | ||
let { viewports } = viewportGridService.getState(); | ||
|
||
viewports = [...viewports.values()]; | ||
// filter empty viewports | ||
const viewportsArray = Array.from(viewports.values()) | ||
.filter(viewport => viewport.displaySetInstanceUIDs?.length) | ||
// filter reconstructable viewports | ||
.filter(viewport => { | ||
const { displaySetInstanceUIDs } = viewport; | ||
viewports = viewports.filter( | ||
viewport => viewport.displaySetInstanceUIDs && viewport.displaySetInstanceUIDs.length | ||
); | ||
|
||
for (const displaySetInstanceUID of displaySetInstanceUIDs) { | ||
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID); | ||
// filter reconstructable viewports | ||
viewports = viewports.filter(viewport => { | ||
const { displaySetInstanceUIDs } = viewport; | ||
|
||
return !!displaySet?.isReconstructable; | ||
} | ||
}); | ||
for (const displaySetInstanceUID of displaySetInstanceUIDs) { | ||
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID); | ||
|
||
const viewportsByOrientation = viewportsArray.reduce((acc, viewport) => { | ||
const { viewportId, viewportType } = viewport.viewportOptions; | ||
|
||
if (viewportType !== 'stack') { | ||
console.warn('Viewport is not a stack, cannot sync images yet'); | ||
return acc; | ||
} | ||
|
||
const { element } = cornerstoneViewportService.getViewportInfo(viewportId); | ||
const { viewport: csViewport, renderingEngineId } = getEnabledElement(element); | ||
const { viewPlaneNormal } = csViewport.getCamera(); | ||
|
||
// Should we round here? I guess so, but not sure how much precision we need | ||
const orientation = viewPlaneNormal.map(v => Math.round(v)).join(','); | ||
// TODO - add a better test than isReconstructable | ||
if (displaySet && displaySet.isReconstructable) { | ||
return true; | ||
} | ||
|
||
if (!acc[orientation]) { | ||
acc[orientation] = []; | ||
return false; | ||
} | ||
}); | ||
|
||
acc[orientation].push({ viewportId, renderingEngineId }); | ||
// viewports = viewports.filter(viewport => viewport.viewportOptions.viewportType === 'stack'); | ||
|
||
return acc; | ||
}, {}); | ||
console.log('viewports=', viewports); | ||
return viewports; | ||
}; | ||
|
||
// create synchronizer for each group | ||
Object.values(viewportsByOrientation).map(viewports => { | ||
let synchronizerId = viewports.map(({ viewportId }) => viewportId).join(','); | ||
const STACK_SYNC_NAME = 'stackImageSync'; | ||
|
||
synchronizerId = `imageSync_${synchronizerId}`; | ||
export default function toggleStackImageSync({ | ||
toggledState, | ||
servicesManager, | ||
viewports: providedViewports, | ||
}) { | ||
if (!toggledState) { | ||
return disableSync(STACK_SYNC_NAME, servicesManager); | ||
} | ||
|
||
calculateViewportRegistrations(viewports); | ||
console.log('Toggling stack image sync'); | ||
const { syncGroupService, viewportGridService, displaySetService, cornerstoneViewportService } = | ||
servicesManager.services; | ||
|
||
viewports.forEach(({ viewportId, renderingEngineId }) => { | ||
syncGroupService.addViewportToSyncGroup(viewportId, renderingEngineId, { | ||
type: 'stackimage', | ||
id: synchronizerId, | ||
source: true, | ||
target: true, | ||
}); | ||
}); | ||
const viewports = providedViewports || getViewports(viewportGridService, displaySetService); | ||
|
||
STACK_IMAGE_SYNC_GROUPS_INFO.push({ | ||
synchronizerId, | ||
viewports, | ||
// create synchronization group and add the viewports to it. | ||
viewports.forEach(gridViewport => { | ||
const { viewportId } = gridViewport.viewportOptions; | ||
const viewport = cornerstoneViewportService.getCornerstoneViewport(viewportId); | ||
if (!viewport) { | ||
return; | ||
} | ||
syncGroupService.addViewportToSyncGroup(viewportId, viewport.getRenderingEngine().id, { | ||
type: 'stackimage', | ||
id: STACK_SYNC_NAME, | ||
source: true, | ||
target: true, | ||
}); | ||
}); | ||
} |
Oops, something went wrong.