Skip to content

Commit

Permalink
Refactor viewport presentations
Browse files Browse the repository at this point in the history
  • Loading branch information
sedghi committed Feb 21, 2024
1 parent 6535ed1 commit 6758e6f
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
VolumeViewport3D,
cache,
Enums as csEnums,
BaseVolumeViewport,
} from '@cornerstonejs/core';

import { utilities as csToolsUtils, Enums as csToolsEnums } from '@cornerstonejs/tools';
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Expand Down Expand Up @@ -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,
},
};
}

Expand All @@ -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,
};
}

Expand Down
23 changes: 15 additions & 8 deletions extensions/cornerstone/src/types/Presentation.ts
Original file line number Diff line number Diff line change
@@ -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<string, Types.ViewportProperties> | Types.ViewportProperties;
}

/**
Expand Down

0 comments on commit 6758e6f

Please sign in to comment.