All files / core/src/RenderingEngine/GenericViewport/ECG ecgProjectionPresentation.ts

3.7% Statements 1/27
0% Branches 0/23
0% Functions 0/3
3.7% Lines 1/27

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                  128x                                                                                                                                                                          
import type { Point2, ViewPresentation } from '../../../types';
import type { ViewPresentationSelector } from '../../../types/IViewport';
import type { ProjectionWriteOptions } from '../ViewportProjectionTypes';
import { normalizeECGViewState } from './ecgViewportCamera';
import type { ECGProjectionSnapshot } from './ECGProjectionTypes';
import type { ECGViewState } from './ECGViewportTypes';
 
const DEFAULT_SELECTOR: Required<
  Pick<ViewPresentationSelector, 'zoom' | 'pan'>
> = {
  zoom: true,
  pan: 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 an ECG projection snapshot to the public view-presentation shape.
 */
export function getECGProjectionPresentation(
  snapshot: ECGProjectionSnapshot,
  selector: ViewPresentationSelector | undefined
): ViewPresentation {
  const target: ViewPresentation = {};
  const ecgSelector = selector ?? DEFAULT_SELECTOR;
  const zoom = snapshot.presentation.zoom ?? 1;
 
  if (ecgSelector.zoom) {
    target.zoom = zoom;
  }
 
  if (ecgSelector.pan) {
    const pan = snapshot.presentation.rawPan;
 
    target.pan = [pan[0] / zoom, pan[1] / zoom];
  }
 
  return target;
}
 
/**
 * Converts an ECG view-presentation patch back into semantic ECG view state.
 */
export function withECGProjectionPresentation(
  snapshot: ECGProjectionSnapshot,
  presentation: Partial<ViewPresentation>,
  options: ProjectionWriteOptions = {}
): ECGViewState {
  const viewState = snapshot.viewState;
  const hasZoom = hasOwn(presentation, 'zoom');
  const hasPan = hasOwn(presentation, 'pan');
  const nextZoom = Math.max(
    presentation.zoom ?? snapshot.presentation.zoom ?? viewState.scale ?? 1,
    0.001
  );
  let resolvedView = snapshot.resolvedView;
  let nextViewState = normalizeECGViewState(viewState);
 
  if (hasZoom) {
    if (resolvedView) {
      resolvedView = resolvedView.withZoom(nextZoom, options.anchorCanvas);
      nextViewState = normalizeECGViewState(resolvedView.state.viewState);
    } else {
      nextViewState = normalizeECGViewState({
        ...nextViewState,
        scale: nextZoom,
        scaleMode: 'fit',
      });
    }
  }
 
  if (hasPan && presentation.pan) {
    const targetPan: Point2 = [
      presentation.pan[0] * nextZoom,
      presentation.pan[1] * nextZoom,
    ];
 
    if (resolvedView) {
      nextViewState = normalizeECGViewState(
        resolvedView.withPan(targetPan).state.viewState
      );
    }
  }
 
  return nextViewState;
}