All files / extensions/cornerstone/src/services/ViewportService/adapter LegacyViewportAdapter.ts

45.2% Statements 33/73
43.9% Branches 18/41
60.86% Functions 14/23
45.2% Lines 33/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241                                                                                                182x                 601x         783x   460x   322x       1x         20130x               322x                     19752x               425x                   22015x                   858x       3585x 2946x     639x 639x 435x   639x 254x   385x                                                               20562x                 2270x 1542x   728x 728x 486x                           1684x 1326x   358x 358x 166x   192x 192x                                                                      
import { Enums, Types as CoreTypes } from '@cornerstonejs/core';
import {
  getLegacyViewportType,
  isOrthographicViewportType,
  isStackViewportType,
  isVolumeViewportType,
} from '../../../utils/getLegacyViewportType';
import type {
  IViewportAdapter,
  ViewportColormap,
  ViewportPresentation,
  ViewportShape,
  ViewportViewState,
  VOIRange,
} from './IViewportAdapter';
 
/**
 * Structural view of the legacy StackViewport/VolumeViewport surface used by
 * the adapter. Optional-chained because different legacy families expose
 * different subsets (e.g. only volume viewports have getAllVolumeIds).
 * Deliberately NOT an intersection with CoreTypes.IViewport: the adapter
 * contract types these members with the next-shaped signatures (e.g. getCamera
 * as a plain record), and IViewport's own declarations would win otherwise.
 */
type LegacyViewport = {
  getProperties?: (dataId?: string) => ViewportPresentation | undefined;
  setProperties?: (props: ViewportPresentation, dataId?: string) => void;
  getCamera?: () => Record<string, unknown> | undefined;
  setCamera?: (patch: Record<string, unknown>) => void;
  getAllVolumeIds?: () => string[];
  getImageData?: (volumeId?: string) => {
    imageData?: { get: (key: string) => { voxelManager?: unknown } | undefined };
  };
  getActors?: () => Array<{ referencedId?: string }>;
  isInAcquisitionPlane?: () => boolean;
  getViewReference?: () => CoreTypes.ViewReference | undefined;
  setViewReference?: (ref: CoreTypes.ViewReference) => void;
  getViewPresentation?: () => unknown;
  setViewPresentation?: (presentation: unknown) => void;
  getCurrentImageId?: () => string;
  setStack?: (imageIds: string[]) => Promise<unknown>;
  setVolumes?: (volumes: Array<{ volumeId: string }>) => Promise<unknown>;
};
 
/**
 * Opacity slider gamma the legacy fusion rendering expects: the slider value is
 * applied through a 1/5 curve (native renders a linear blend and uses gamma 1).
 */
export const LEGACY_OPACITY_GAMMA = 1 / 5;
 
/**
 * Legacy lane of IViewportAdapter — adapts the StackViewport/VolumeViewport
 * surface (getCamera/getProperties/volumeIds) to the next-shaped contract.
 * Deleting the legacy path deletes this file. Instantiated only by
 * `getViewportAdapter`.
 */
export class LegacyViewportAdapter implements IViewportAdapter {
  constructor(private readonly viewport: LegacyViewport) {}
 
  // ---- classification ----
 
  getShape(): ViewportShape {
    switch (getLegacyViewportType(this.viewport)) {
      case Enums.ViewportType.STACK:
        return 'stack';
      case Enums.ViewportType.ORTHOGRAPHIC:
        return 'volume';
      case Enums.ViewportType.VOLUME_3D:
        return 'volume3d';
      default:
        return 'unknown';
    }
  }
 
  isVolumeRendering(): boolean {
    return isOrthographicViewportType(this.viewport);
  }
 
  canReorientInPlace(): boolean {
    return isOrthographicViewportType(this.viewport);
  }
 
  isInAcquisitionPlane(): boolean {
    return !!this.viewport.isInAcquisitionPlane?.();
  }
 
  hasContent(): boolean {
    const actorEntries = this.viewport.getActors?.();
    return !!actorEntries && actorEntries.length > 0;
  }
 
  // ---- view geometry ----
 
  getViewState(): ViewportViewState {
    return this.viewport.getCamera?.() ?? {};
  }
 
  setViewState(patch: ViewportViewState): void {
    this.viewport.setCamera?.(patch);
  }
 
  getViewPlaneNormal(): CoreTypes.Point3 | undefined {
    return this.viewport.getCamera?.()?.viewPlaneNormal as CoreTypes.Point3 | undefined;
  }
 
  getFocalPoint(): CoreTypes.Point3 | undefined {
    return this.viewport.getCamera?.()?.focalPoint as CoreTypes.Point3 | undefined;
  }
 
  // ---- per-display-set appearance ----
 
  getPresentation(dataId?: string): ViewportPresentation {
    return (dataId ? this.viewport.getProperties?.(dataId) : this.viewport.getProperties?.()) ?? {};
  }
 
  setPresentation(props: ViewportPresentation, dataId?: string): void {
    this.viewport.setProperties?.(props, dataId);
  }
 
  getDefaultVOIRange(): VOIRange | undefined {
    // Legacy getProperties always returns the applied VOI; there is no separate
    // computed-default accessor.
    return undefined;
  }
 
  getColormap(displaySetInstanceUID: string): ViewportColormap | undefined {
    if (isStackViewportType(this.viewport)) {
      return this.viewport.getProperties?.()?.colormap;
    }
 
    const actorEntries = this.viewport.getActors?.();
    const actorEntry = actorEntries?.find(entry =>
      entry.referencedId?.includes(displaySetInstanceUID)
    );
    if (!actorEntry) {
      return undefined;
    }
    return this.viewport.getProperties?.(actorEntry.referencedId)?.colormap;
  }
 
  setLayerOpacity(displaySetInstanceUID: string, opacity: number): boolean {
    Iif (!isVolumeViewportType(this.viewport)) {
      return false;
    }
    const volumeId = this.getDataIdForDisplaySet(displaySetInstanceUID);
    Iif (!volumeId) {
      return false;
    }
 
    // Merge the opacity into the current colormap so its name/threshold persist.
    const currentColormap = this.viewport.getProperties?.(volumeId)?.colormap ?? {};
    this.viewport.setProperties?.({ colormap: { ...currentColormap, opacity } }, volumeId);
    return true;
  }
 
  setLayerThreshold(displaySetInstanceUID: string, threshold: number): boolean {
    Iif (!isVolumeViewportType(this.viewport)) {
      return false;
    }
    const volumeId = this.getDataIdForDisplaySet(displaySetInstanceUID);
    Iif (!volumeId) {
      return false;
    }
 
    this.viewport.setProperties?.({ colormap: { threshold } }, volumeId);
    return true;
  }
 
  getOpacityGamma(): number {
    return LEGACY_OPACITY_GAMMA;
  }
 
  // ---- data addressing ----
 
  getDataIdForDisplaySet(displaySetInstanceUID: string): string | undefined {
    // Multi-volume viewports address a layer by the volumeId that embeds the
    // display set UID; single-actor viewports (stack) address the active layer
    // implicitly (undefined).
    if (typeof this.viewport.getAllVolumeIds !== 'function') {
      return undefined;
    }
    const volumeIds = this.viewport.getAllVolumeIds() || [];
    return volumeIds.length > 0
      ? (volumeIds.find(id => id.includes(displaySetInstanceUID)) ?? undefined)
      : undefined;
  }
 
  getVolumeIds(): string[] {
    Iif (typeof this.viewport.getAllVolumeIds !== 'function') {
      return [];
    }
    return this.viewport.getAllVolumeIds() || [];
  }
 
  getVoxelManagerForDisplaySet(
    displaySetInstanceUID: string
  ): { getRange?: () => [number, number]; [key: string]: unknown } | undefined {
    if (!isVolumeViewportType(this.viewport)) {
      return undefined;
    }
    const volumeId = this.getDataIdForDisplaySet(displaySetInstanceUID);
    if (!volumeId) {
      return undefined;
    }
    const imageData = this.viewport.getImageData?.(volumeId);
    return imageData?.imageData?.get('voxelManager')?.voxelManager as
      | { getRange?: () => [number, number]; [key: string]: unknown }
      | undefined;
  }
 
  // ---- capture ----
 
  async copyDisplayedContentTo(target: CoreTypes.IViewport): Promise<void> {
    const targetViewport = target as unknown as LegacyViewport;
    const viewRef = this.viewport.getViewReference?.();
 
    // - properties: VOI, colormap, interpolation, etc.
    // - viewPresentation: flip/rotate/zoom presentation state (preserves flip/rotate)
    const properties = this.viewport.getProperties?.();
    const viewPresentation = this.viewport.getViewPresentation?.();
 
    if (isStackViewportType(targetViewport)) {
      const imageId = this.viewport.getCurrentImageId?.();
      await targetViewport.setStack?.([imageId]);
    } else Iif (isVolumeViewportType(targetViewport)) {
      const volumeIds = this.getVolumeIds();
      await targetViewport.setVolumes?.([{ volumeId: volumeIds[0] }]);
    }
 
    Iif (viewPresentation && targetViewport.setViewPresentation) {
      targetViewport.setViewPresentation(viewPresentation);
    }
 
    targetViewport.setProperties?.(properties);
 
    Iif (viewRef && targetViewport.setViewReference) {
      targetViewport.setViewReference(viewRef);
    }
  }
}