All files / extensions/cornerstone/src/services/ViewportService Viewport.ts

58.88% Statements 53/90
29.03% Branches 9/31
70.58% Functions 24/34
59.55% Lines 53/89

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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366                                                                                                                                                                                                  55x 55x       55x                                                       153x 153x 153x 153x 153x 153x 153x   153x 153x 153x 153x                                                                       188x       235x       188x       153x       188x               346x       260x                     341x   341x   341x                                                               341x 341x 341x   341x   341x   341x       341x                 341x       529x       4710x       282x 282x       529x       235x 235x       295x       1598x       188x       188x       1114x               188x       138x               341x   341x 385x 385x                 385x   385x                   341x          
import {
  Types,
  Enums,
  getEnabledElementByViewportId,
  VolumeViewport,
  utilities,
} from '@cornerstonejs/core';
import { StackViewportData, VolumeViewportData } from '../../types/CornerstoneCacheService';
import getCornerstoneBlendMode from '../../utils/getCornerstoneBlendMode';
import getCornerstoneOrientation from '../../utils/getCornerstoneOrientation';
import getCornerstoneViewportType from '../../utils/getCornerstoneViewportType';
import JumpPresets from '../../utils/JumpPresets';
import { SyncGroup } from '../SyncGroupService/SyncGroupService';
 
export type InitialImageOptions = {
  index?: number;
  preset?: JumpPresets;
  useOnce?: boolean;
};
 
export type ViewportOptions = {
  id?: string;
  viewportType: Enums.ViewportType;
  toolGroupId: string;
  viewportId: string;
  // Presentation ID to store/load presentation state from
  presentationIds?: AppTypes.PresentationIds;
  orientation?: Enums.OrientationAxis;
  background?: Types.Point3;
  displayArea?: Types.DisplayArea;
  syncGroups?: SyncGroup[];
  initialImageOptions?: InitialImageOptions;
  rotation?: number;
  flipHorizontal?: boolean;
  viewReference?: Types.ViewReference;
  customViewportProps?: Record<string, unknown>;
  /*
   * Allows drag and drop of display sets not matching viewport options, but
   * doesn't show them initially.  Displays initially blank if no required match
   */
  allowUnmatchedView?: boolean;
};
 
export type PublicViewportOptions = {
  id?: string;
  viewportType?: string;
  toolGroupId?: string;
  presentationIds?: string[];
  viewportId?: string;
  orientation?: Enums.OrientationAxis;
  background?: Types.Point3;
  displayArea?: Types.DisplayArea;
  syncGroups?: SyncGroup[];
  rotation?: number;
  flipHorizontal?: boolean;
  initialImageOptions?: InitialImageOptions;
  customViewportProps?: Record<string, unknown>;
  allowUnmatchedView?: boolean;
};
 
export type DisplaySetSelector = {
  id?: string;
  options?: PublicDisplaySetOptions;
};
 
export type PublicDisplaySetOptions = {
  /** The display set options can have an id in order to distinguish
   * it from other similar items.
   */
  id?: string;
  voi?: VOI;
  voiInverted?: boolean;
  blendMode?: string;
  slabThickness?: number;
  colormap?: string;
  displayPreset?: string;
};
 
export type DisplaySetOptions = {
  id?: string;
  voi?: VOI;
  voiInverted: boolean;
  blendMode?: Enums.BlendModes;
  slabThickness?: number;
  colormap?: { name: string; opacity?: number };
  displayPreset?: string;
};
 
type VOI = {
  windowWidth: number;
  windowCenter: number;
};
 
export type DisplaySet = {
  displaySetInstanceUID: string;
};
 
const STACK = 'stack';
const DEFAULT_TOOLGROUP_ID = 'default';
 
// Return true if the data contains the given display set UID OR the imageId
// if it is a composite object.
const dataContains = ({ data, displaySetUID, imageId, viewport }): boolean => {
  Iif (imageId && data.isCompositeStack && data.imageIds) {
    return !!data.imageIds.find(dataId => dataId === imageId);
  }
 
  Iif (imageId && (data.volumeId || viewport instanceof VolumeViewport)) {
    const isAcquisition = !!viewport.getCurrentImageId();
 
    Iif (!isAcquisition) {
      return false;
    }
 
    const imageURI = utilities.imageIdToURI(imageId);
    const hasImageId = viewport.hasImageURI(imageURI);
 
    Iif (hasImageId) {
      return true;
    }
  }
 
  Iif (data.displaySetInstanceUID === displaySetUID) {
    return true;
  }
 
  return false;
};
 
class ViewportInfo {
  private viewportId = '';
  private element: HTMLDivElement;
  private viewportOptions: ViewportOptions;
  private displaySetOptions: Array<DisplaySetOptions>;
  private viewportData: StackViewportData | VolumeViewportData;
  private renderingEngineId: string;
  private viewReference: Types.ViewReference;
 
  constructor(viewportId: string) {
    this.viewportId = viewportId;
    this.setPublicViewportOptions({});
    this.setPublicDisplaySetOptions([{}]);
  }
 
  /**
   * Return true if the viewport contains the given display set UID,
   * OR if it is a composite stack and contains the given imageId
   */
  public contains(displaySetUID: string, imageId: string): boolean {
    Iif (!this.viewportData?.data) {
      return false;
    }
 
    const { viewport } = getEnabledElementByViewportId(this.viewportId) || {};
 
    Iif (this.viewportData.data.length) {
      return !!this.viewportData.data.find(data =>
        dataContains({ data, displaySetUID, imageId, viewport })
      );
    }
 
    return dataContains({
      data: this.viewportData.data,
      displaySetUID,
      imageId,
      viewport,
    });
  }
 
  public destroy = (): void => {
    this.element = null;
    this.viewportData = null;
    this.viewportOptions = null;
    this.displaySetOptions = null;
  };
 
  public setRenderingEngineId(renderingEngineId: string): void {
    this.renderingEngineId = renderingEngineId;
  }
 
  public getRenderingEngineId(): string {
    return this.renderingEngineId;
  }
 
  public setViewportId(viewportId: string): void {
    this.viewportId = viewportId;
  }
 
  public setElement(element: HTMLDivElement): void {
    this.element = element;
  }
 
  public setViewportData(viewportData: StackViewportData | VolumeViewportData): void {
    this.viewportData = viewportData;
  }
 
  public getViewportData(): StackViewportData | VolumeViewportData {
    return this.viewportData;
  }
 
  public getElement(): HTMLDivElement {
    return this.element;
  }
 
  public getViewportId(): string {
    return this.viewportId;
  }
 
  public getViewReference(): Types.ViewReference {
    return this.viewportOptions?.viewReference;
  }
 
  public setPublicDisplaySetOptions(
    publicDisplaySetOptions: PublicDisplaySetOptions[] | DisplaySetSelector[]
  ): Array<DisplaySetOptions> {
    // map the displaySetOptions and check if they are undefined then set them to default values
    const displaySetOptions = this.mapDisplaySetOptions(publicDisplaySetOptions);
 
    this.setDisplaySetOptions(displaySetOptions);
 
    return this.displaySetOptions;
  }
 
  public hasDisplaySet(displaySetInstanceUID: string): boolean {
    // Todo: currently this does not work for non image & referenceImage displaySets.
    // Since SEG and other derived displaySets are loaded in a different way, and not
    // via cornerstoneViewportService
    let viewportData = this.getViewportData();
 
    Iif (
      viewportData.viewportType === Enums.ViewportType.ORTHOGRAPHIC ||
      viewportData.viewportType === Enums.ViewportType.VOLUME_3D
    ) {
      viewportData = viewportData as VolumeViewportData;
      return viewportData.data.some(
        ({ displaySetInstanceUID: dsUID }) => dsUID === displaySetInstanceUID
      );
    }
 
    viewportData = viewportData as StackViewportData;
    return viewportData.data.displaySetInstanceUID === displaySetInstanceUID;
  }
 
  /**
   *
   * @param viewportOptionsEntry - the base values for the options
   * @param viewportTypeDisplaySet  - allows overriding the viewport type
   */
  public setPublicViewportOptions(
    viewportOptionsEntry: PublicViewportOptions,
    viewportTypeDisplaySet?: string
  ): ViewportOptions {
    const ohifViewportType = viewportTypeDisplaySet || viewportOptionsEntry.viewportType || STACK;
    const { presentationIds } = viewportOptionsEntry;
    let { toolGroupId = DEFAULT_TOOLGROUP_ID } = viewportOptionsEntry;
    // Just assign the orientation for any viewport type and let the viewport deal with it
    const orientation = getCornerstoneOrientation(viewportOptionsEntry.orientation);
 
    const viewportType = getCornerstoneViewportType(ohifViewportType);
 
    Iif (!toolGroupId) {
      toolGroupId = DEFAULT_TOOLGROUP_ID;
    }
 
    this.setViewportOptions({
      ...viewportOptionsEntry,
      viewportId: this.viewportId,
      viewportType: viewportType as Enums.ViewportType,
      orientation,
      toolGroupId,
      presentationIds,
    });
 
    return this.viewportOptions;
  }
 
  public setViewportOptions(viewportOptions: ViewportOptions): void {
    this.viewportOptions = viewportOptions;
  }
 
  public getViewportOptions(): ViewportOptions {
    return this.viewportOptions;
  }
 
  public getPresentationIds(): AppTypes.PresentationIds | null {
    const { presentationIds } = this.viewportOptions;
    return presentationIds;
  }
 
  public setDisplaySetOptions(displaySetOptions: Array<DisplaySetOptions>): void {
    this.displaySetOptions = displaySetOptions;
  }
 
  public getSyncGroups(): SyncGroup[] {
    this.viewportOptions.syncGroups ||= [];
    return this.viewportOptions.syncGroups;
  }
 
  public getDisplaySetOptions(): Array<DisplaySetOptions> {
    return this.displaySetOptions;
  }
 
  public getViewportType(): Enums.ViewportType {
    return this.viewportOptions.viewportType || Enums.ViewportType.STACK;
  }
 
  public getToolGroupId(): string {
    return this.viewportOptions.toolGroupId;
  }
 
  public getBackground(): Types.Point3 {
    return this.viewportOptions.background || [0, 0, 0];
  }
 
  public getOrientation(): Enums.OrientationAxis {
    return this.viewportOptions.orientation;
  }
 
  public setOrientation(orientation: Enums.OrientationAxis): void {
    this.viewportOptions.orientation = orientation;
  }
 
  public getDisplayArea(): Types.DisplayArea {
    return this.viewportOptions.displayArea;
  }
 
  public getInitialImageOptions(): InitialImageOptions {
    return this.viewportOptions.initialImageOptions;
  }
 
  // Handle incoming public display set options or a display set select
  // with a contained options.
  private mapDisplaySetOptions(
    options: PublicDisplaySetOptions[] | DisplaySetSelector[] = [{}]
  ): Array<DisplaySetOptions> {
    const displaySetOptions: Array<DisplaySetOptions> = [];
 
    options.forEach(item => {
      let option = item?.options || item;
      Iif (!option) {
        option = {
          blendMode: undefined,
          slabThickness: undefined,
          colormap: undefined,
          voi: {},
          voiInverted: false,
        };
      }
      const blendMode = getCornerstoneBlendMode(option.blendMode);
 
      displaySetOptions.push({
        voi: option.voi,
        voiInverted: option.voiInverted,
        colormap: option.colormap,
        slabThickness: option.slabThickness,
        blendMode,
        displayPreset: option.displayPreset,
      });
    });
 
    return displaySetOptions;
  }
}
 
export default ViewportInfo;