All files / core/src/RenderingEngine/GenericViewport/Video videoProjectionPresentation.ts

3.33% Statements 1/30
0% Branches 0/41
0% Functions 0/3
3.33% Lines 1/30

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                  128x                                                                                                                                                                                                    
import type { Point2, ViewPresentation } from '../../../types';
import type { ViewPresentationSelector } from '../../../types/IViewport';
import type { ProjectionWriteOptions } from '../ViewportProjectionTypes';
import { normalizeVideoViewState } from './videoViewportCamera';
import type { VideoProjectionSnapshot } from './VideoProjectionTypes';
import type { VideoViewState } from './VideoViewportTypes';
 
const DEFAULT_SELECTOR: Required<
  Pick<ViewPresentationSelector, 'rotation' | 'zoom' | 'pan'>
> = {
  rotation: true,
  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 a Video projection snapshot to the public view-presentation shape.
 */
export function getVideoProjectionPresentation(
  snapshot: VideoProjectionSnapshot,
  selector: ViewPresentationSelector | undefined
): ViewPresentation {
  const target: ViewPresentation = {};
  const videoSelector = selector ?? DEFAULT_SELECTOR;
  const zoom = snapshot.presentation.zoom ?? 1;
 
  if (videoSelector.rotation) {
    target.rotation = snapshot.presentation.rotation ?? 0;
  }
 
  if (videoSelector.zoom) {
    target.zoom = zoom;
  }
 
  if (videoSelector.pan) {
    const pan = snapshot.presentation.rawPan;
 
    target.pan = [pan[0] / zoom, pan[1] / zoom];
  }
 
  return target;
}
 
/**
 * Converts a Video view-presentation patch back into semantic Video view state.
 */
export function withVideoProjectionPresentation(
  snapshot: VideoProjectionSnapshot,
  presentation: Partial<ViewPresentation>,
  options: ProjectionWriteOptions = {}
): VideoViewState {
  const viewState = snapshot.viewState ?? {};
  const hasZoom = hasOwn(presentation, 'zoom');
  const hasPan = hasOwn(presentation, 'pan');
  const hasRotation = hasOwn(presentation, 'rotation');
  const nextZoom = Math.max(
    presentation.zoom ?? snapshot.presentation.zoom ?? viewState.scale ?? 1,
    0.001
  );
  let resolvedView = snapshot.resolvedView;
  let nextViewState: VideoViewState = normalizeVideoViewState({
    ...viewState,
    ...(hasRotation ? { rotation: presentation.rotation ?? 0 } : {}),
  });
 
  if (hasZoom) {
    if (resolvedView) {
      resolvedView = resolvedView.withZoom(nextZoom, options.anchorCanvas);
      nextViewState = normalizeVideoViewState({
        ...resolvedView.state?.viewState,
        ...(hasRotation ? { rotation: presentation.rotation ?? 0 } : {}),
      });
    } else {
      nextViewState = normalizeVideoViewState({
        ...nextViewState,
        scale: nextZoom,
        scaleMode: 'fit',
      });
    }
  }
 
  if (hasPan && presentation.pan) {
    const targetPan: Point2 = [
      presentation.pan[0] * nextZoom,
      presentation.pan[1] * nextZoom,
    ];
 
    if (resolvedView) {
      nextViewState = normalizeVideoViewState({
        ...resolvedView.withPan(targetPan).state?.viewState,
        ...(hasRotation ? { rotation: presentation.rotation ?? 0 } : {}),
      });
    }
  }
 
  return nextViewState;
}