All files / core/src/RenderingEngine/GenericViewport/WSI wsiProjectionPresentation.ts

5% Statements 1/20
0% Branches 0/21
0% Functions 0/3
5% Lines 1/20

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                128x                                                                                                                                                          
import type { ViewPresentation } from '../../../types';
import type { ViewPresentationSelector } from '../../../types/IViewport';
import type { ProjectionWriteOptions } from '../ViewportProjectionTypes';
import type { WSIProjectionSnapshot } from './WSIProjectionTypes';
import type { WSIViewState } from './WSIViewportTypes';
 
const DEFAULT_SELECTOR: Required<
  Pick<ViewPresentationSelector, 'rotation' | 'zoom'>
> = {
  rotation: true,
  zoom: true,
};
 
/**
 * Narrows a presentation patch to fields explicitly provided by the caller.
 */
function hasOwn<T extends object, K extends PropertyKey>(
  obj: T,
  key: K
): obj is T & Record<K, unknown> {
  return Object.prototype.hasOwnProperty.call(obj, key);
}
 
/**
 * Converts a WSI projection snapshot to the public view-presentation shape.
 */
export function getWSIProjectionPresentation(
  snapshot: WSIProjectionSnapshot,
  selector: ViewPresentationSelector | undefined
): ViewPresentation {
  const target: ViewPresentation = {};
  const wsiSelector = selector ?? DEFAULT_SELECTOR;
 
  if (wsiSelector.zoom) {
    target.zoom = snapshot.presentation.zoom;
  }
 
  if (wsiSelector.rotation) {
    target.rotation = snapshot.presentation.rotation;
  }
 
  return target;
}
 
/**
 * Converts a WSI view-presentation patch back into semantic WSI view state.
 */
export function withWSIProjectionPresentation(
  snapshot: WSIProjectionSnapshot,
  presentation: Partial<ViewPresentation>,
  options: ProjectionWriteOptions = {}
): WSIViewState {
  const hasZoom = hasOwn(presentation, 'zoom');
  const hasRotation = hasOwn(presentation, 'rotation');
  const nextZoom = Math.max(
    presentation.zoom ??
      snapshot.presentation.zoom ??
      snapshot.viewState.zoom ??
      1,
    0.001
  );
  let nextViewState: WSIViewState = {
    ...snapshot.viewState,
    centerIndex: snapshot.viewState.centerIndex
      ? [snapshot.viewState.centerIndex[0], snapshot.viewState.centerIndex[1]]
      : undefined,
  };
 
  if (hasZoom) {
    if (snapshot.resolvedView) {
      nextViewState = {
        ...snapshot.resolvedView.withZoom(nextZoom, options.anchorCanvas).state
          .viewState,
      };
    } else {
      nextViewState.zoom = nextZoom;
    }
  }
 
  if (hasRotation) {
    nextViewState.rotation = presentation.rotation ?? 0;
  }
 
  return nextViewState;
}