forked from OHIF/Viewers
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(StackSync): Miscellaneous fixes for stack image sync (OHIF#3663)
- Loading branch information
1 parent
147cb3f
commit 949a156
Showing
26 changed files
with
425 additions
and
286 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
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
136 changes: 63 additions & 73 deletions
136
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,80 @@ | ||
import calculateViewportRegistrations from './calculateViewportRegistrations'; | ||
const STACK_SYNC_NAME = 'stackImageSync'; | ||
|
||
// [ { | ||
// synchronizerId: string, | ||
// viewports: [ { viewportId: string, renderingEngineId: string, index: number } , ...] | ||
// ]} | ||
let STACK_IMAGE_SYNC_GROUPS_INFO = []; | ||
export default function toggleStackImageSync({ | ||
toggledState, | ||
servicesManager, | ||
viewports: providedViewports, | ||
}) { | ||
if (!toggledState) { | ||
return disableSync(STACK_SYNC_NAME, servicesManager); | ||
} | ||
|
||
export default function toggleStackImageSync({ toggledState, servicesManager, getEnabledElement }) { | ||
const { syncGroupService, viewportGridService, displaySetService, cornerstoneViewportService } = | ||
servicesManager.services; | ||
|
||
if (!toggledState) { | ||
STACK_IMAGE_SYNC_GROUPS_INFO.forEach(syncGroupInfo => { | ||
const { viewports, synchronizerId } = syncGroupInfo; | ||
const viewports = providedViewports || getReconstructableStackViewports(viewportGridService, displaySetService); | ||
|
||
viewports.forEach(({ viewportId, renderingEngineId }) => { | ||
syncGroupService.removeViewportFromSyncGroup(viewportId, renderingEngineId, synchronizerId); | ||
}); | ||
// 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, | ||
}); | ||
}); | ||
} | ||
|
||
return; | ||
} | ||
|
||
STACK_IMAGE_SYNC_GROUPS_INFO = []; | ||
function disableSync(syncName, servicesManager) { | ||
const { syncGroupService, viewportGridService, displaySetService, cornerstoneViewportService } = | ||
servicesManager.services; | ||
const viewports = getReconstructableStackViewports(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 | ||
); | ||
}); | ||
}; | ||
|
||
// create synchronization groups and add viewports | ||
const { viewports } = viewportGridService.getState(); | ||
/** | ||
* Gets the consistent spacing stack viewport types, which are the ones which | ||
* can be navigated using the stack image sync right now. | ||
*/ | ||
function getReconstructableStackViewports(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); | ||
|
||
return !!displaySet?.isReconstructable; | ||
} | ||
}); | ||
// filter reconstructable viewports | ||
viewports = viewports.filter(viewport => { | ||
const { displaySetInstanceUIDs } = viewport; | ||
|
||
const viewportsByOrientation = viewportsArray.reduce((acc, viewport) => { | ||
const { viewportId, viewportType } = viewport.viewportOptions; | ||
for (const displaySetInstanceUID of displaySetInstanceUIDs) { | ||
const displaySet = displaySetService.getDisplaySetByUID(displaySetInstanceUID); | ||
|
||
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 }); | ||
|
||
return acc; | ||
}, {}); | ||
|
||
// create synchronizer for each group | ||
Object.values(viewportsByOrientation).map(viewports => { | ||
let synchronizerId = viewports.map(({ viewportId }) => viewportId).join(','); | ||
|
||
synchronizerId = `imageSync_${synchronizerId}`; | ||
|
||
calculateViewportRegistrations(viewports); | ||
|
||
viewports.forEach(({ viewportId, renderingEngineId }) => { | ||
syncGroupService.addViewportToSyncGroup(viewportId, renderingEngineId, { | ||
type: 'stackimage', | ||
id: synchronizerId, | ||
source: true, | ||
target: true, | ||
}); | ||
}); | ||
|
||
STACK_IMAGE_SYNC_GROUPS_INFO.push({ | ||
synchronizerId, | ||
viewports, | ||
}); | ||
}); | ||
} | ||
return viewports; | ||
}; |
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
Oops, something went wrong.