Skip to content

Commit

Permalink
fix(viewport-sync): Enable re-sync image slices in a different positi…
Browse files Browse the repository at this point in the history
…on when needed
  • Loading branch information
mateusfreira committed Apr 21, 2024
1 parent 65d419d commit 919602b
Showing 1 changed file with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { synchronizers, SynchronizerManager, Synchronizer } from '@cornerstonejs/tools';
import { getRenderingEngines } from '@cornerstonejs/core';
import { getRenderingEngines, utilities } from '@cornerstonejs/core';

import { pubSubServiceInterface, Types, ServicesManager } from '@ohif/core';


const EVENTS = {
TOOL_GROUP_CREATED: 'event::cornerstone::syncgroupservice:toolgroupcreated',
};
Expand Down Expand Up @@ -184,6 +185,11 @@ export default class SyncGroupService {
return;
}

// Only image slice synchronizer register spatial registration
if (this.isImageSliceSyncronizer(synchronizer)) {
this.unRegisterSpatialRegistration(synchronizer);
}

synchronizer.remove({
viewportId,
renderingEngineId,
Expand All @@ -198,4 +204,45 @@ export default class SyncGroupService {
}
});
}
/**
* Clean up the spatial registration metadata created by synchronizer
* This is needed to be able to re-sync images slices if needed
* @param synchronizer
*/
unRegisterSpatialRegistration(synchronizer: Synchronizer) {
const sourceViewports = synchronizer.getSourceViewports().map(vp => vp.viewportId);
const targetViewports = synchronizer.getTargetViewports().map(vp => vp.viewportId);

// Create an array of pair of viewports to remove from spatialRegistrationMetadataProvider
// All sourceViewports combined with all targetViewports
const toUnregister = sourceViewports
.map((sourceViewportId: string) => {
return targetViewports.map(targetViewportId => [targetViewportId, sourceViewportId]);
})
.reduce((acc, c) => acc.concat(c), []);

toUnregister.forEach(viewportIdPair => {
utilities.spatialRegistrationMetadataProvider.add(viewportIdPair, undefined);
});
}
/**
* Check if the synchronizer type is IMAGE_SLICE
* Need to convert to lowercase here because the types are lowercase
* e.g: synchronizerCreators
* @param synchronizer
*/
isImageSliceSyncronizer(synchronizer: Synchronizer) {
return this.getSynchronizerType(synchronizer).toLowerCase() === IMAGE_SLICE;
}
/**
* Returns the syncronizer type
* @param synchronizer
*/
getSynchronizerType(synchronizer: Synchronizer): string {
const synchronizerTypes = Object.keys(this.synchronizersByType);
const syncType = synchronizerTypes.find(syncType =>
this.getSynchronizersOfType(syncType).includes(synchronizer)
);
return syncType;
}
}

0 comments on commit 919602b

Please sign in to comment.