From 6758e6f3ce96cace67dab9b026a7efcdc4d434cd Mon Sep 17 00:00:00 2001 From: Alireza Date: Wed, 21 Feb 2024 11:28:03 -0500 Subject: [PATCH] Refactor viewport presentations --- .../CornerstoneViewportService.ts | 71 ++++++++++++++----- .../cornerstone/src/types/Presentation.ts | 23 +++--- 2 files changed, 68 insertions(+), 26 deletions(-) diff --git a/extensions/cornerstone/src/services/ViewportService/CornerstoneViewportService.ts b/extensions/cornerstone/src/services/ViewportService/CornerstoneViewportService.ts index a14916e0f09..96050311c73 100644 --- a/extensions/cornerstone/src/services/ViewportService/CornerstoneViewportService.ts +++ b/extensions/cornerstone/src/services/ViewportService/CornerstoneViewportService.ts @@ -10,6 +10,7 @@ import { VolumeViewport3D, cache, Enums as csEnums, + BaseVolumeViewport, } from '@cornerstonejs/core'; import { utilities as csToolsUtils, Enums as csToolsEnums } from '@cornerstonejs/tools'; @@ -166,7 +167,10 @@ class CornerstoneViewportService extends PubSubService implements IViewportServi * @param presentations - The presentations to apply to the viewport. */ public setPresentations(viewportId: string, presentations?: Presentations): void { - const viewport = this.getCornerstoneViewport(viewportId); + const viewport = this.getCornerstoneViewport(viewportId) as + | Types.IStackViewport + | Types.IVolumeViewport; + if (!viewport) { return; } @@ -177,17 +181,29 @@ class CornerstoneViewportService extends PubSubService implements IViewportServi const { lutPresentation, positionPresentation } = presentations; if (lutPresentation) { - viewport.setProperties(lutPresentation); + const { presentation } = lutPresentation; + + if (viewport instanceof BaseVolumeViewport) { + Object.entries(presentation).forEach( + ([volumeId, properties]: [string, Types.ViewportProperties]) => { + viewport.setProperties(properties, volumeId); + } + ); + } else { + viewport.setProperties(presentation); + } } + if (positionPresentation) { - viewport.setCamera(positionPresentation); + const { viewPlaneNormal, viewUp, zoom, pan } = positionPresentation.presentation; + viewport.setCamera({ viewPlaneNormal, viewUp }); - if (positionPresentation.zoom !== undefined) { - viewport.setZoom(positionPresentation.zoom); + if (zoom !== undefined) { + viewport.setZoom(zoom); } - if (positionPresentation.pan !== undefined) { - viewport.setPan(positionPresentation.pan); + if (pan !== undefined) { + viewport.setPan(pan); } } } @@ -225,11 +241,13 @@ class CornerstoneViewportService extends PubSubService implements IViewportServi return { id: positionPresentationId, viewportType: viewportInfo.getViewportType(), - initialImageIndex, - viewPlaneNormal, - viewUp, - zoom, - pan, + presentation: { + initialImageIndex, + viewUp, + viewPlaneNormal, + zoom, + pan, + }, }; } @@ -252,21 +270,38 @@ class CornerstoneViewportService extends PubSubService implements IViewportServi const { lutPresentationId } = presentationIds; - const csViewport = this.getCornerstoneViewport(viewportId); + const csViewport = this.getCornerstoneViewport(viewportId) as + | Types.IStackViewport + | Types.IVolumeViewport; + if (!csViewport) { return; } - const properties = csViewport.getProperties(); - if (properties.isComputedVOI) { - delete properties.voiRange; - delete properties.VOILUTFunction; + const cleanProperties = properties => { + if (properties.isComputedVOI) { + delete properties.voiRange; + delete properties.VOILUTFunction; + } + return properties; + }; + + const presentation = + csViewport instanceof BaseVolumeViewport + ? new Map() + : cleanProperties(csViewport.getProperties()); + + if (presentation instanceof Map) { + csViewport.getActors().forEach(({ uid: volumeId }) => { + const properties = cleanProperties(csViewport.getProperties(volumeId)); + presentation.set(volumeId, properties); + }); } return { id: lutPresentationId, viewportType: viewportInfo.getViewportType(), - ...properties, + presentation, }; } diff --git a/extensions/cornerstone/src/types/Presentation.ts b/extensions/cornerstone/src/types/Presentation.ts index 8a841808f46..57737cc44dd 100644 --- a/extensions/cornerstone/src/types/Presentation.ts +++ b/extensions/cornerstone/src/types/Presentation.ts @@ -1,24 +1,31 @@ import type { Types } from '@cornerstonejs/core'; /** - * Represents a position presentation in a viewport. + * Represents a position presentation in a viewport. This is basically + * viewport specific camera position and zoom, and not the display set */ export type PositionPresentation = { id: string; viewportType: string; - initialImageIndex: number; - viewUp: Types.Point3; - viewPlaneNormal: Types.Point3; - zoom?: number; - pan?: Types.Point2; + presentation: { + initialImageIndex: number; + viewUp: Types.Point3; + viewPlaneNormal: Types.Point3; + zoom?: number; + pan?: Types.Point2; + }; }; /** - * Represents a LUT presentation in a stack viewport. + * Represents a LUT presentation in a viewport, and is really related + * to displaySets and not the viewport itself. So that is why it can + * be an object with volumeId keys, or a single object with the properties + * itself */ -export interface LutPresentation extends Types.StackViewportProperties { +export interface LutPresentation { id: string; viewportType: string; + presentation: Record | Types.ViewportProperties; } /**