All files / core/src/RenderingEngine/GenericViewport/Planar planarCpuViewportMath.ts

8.33% Statements 1/12
0% Branches 0/16
0% Functions 0/2
8.33% Lines 1/12

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            128x                                                                                                                              
import type { CPUFallbackViewport, IImage, Point2 } from '../../../types';
import {
  getPlanarScaleRatio,
  type PlanarScaleInput,
} from './planarCameraScale';
 
const EPSILON = 1e-6;
 
export function resolvePlanarCpuImageDisplayedArea(
  image: IImage
): CPUFallbackViewport['displayedArea'] {
  return {
    tlhc: {
      x: 1,
      y: 1,
    },
    brhc: {
      x: Math.max(image.columns, 1),
      y: Math.max(image.rows, 1),
    },
    rowPixelSpacing: image.rowPixelSpacing ?? 1,
    columnPixelSpacing: image.columnPixelSpacing ?? 1,
    presentationSizeMode: 'NONE',
  };
}
 
export function resolvePlanarCpuViewportScale(args: {
  canvas: HTMLCanvasElement;
  parallelScale?: number;
  rowPixelSpacing: number;
  columnPixelSpacing: number;
  presentationScale?: PlanarScaleInput;
}): number | Point2 {
  const {
    canvas,
    columnPixelSpacing,
    parallelScale,
    presentationScale,
    rowPixelSpacing,
  } = args;
  const worldHeight = Math.max((parallelScale ?? 1) * 2, EPSILON);
  const worldToCanvasScale = canvas.height / worldHeight;
  const scaleRatio = getPlanarScaleRatio(presentationScale);
 
  if (Math.abs(scaleRatio - 1) > EPSILON) {
    const safeCanvasHeight = Math.max(canvas.height, 1);
    const safeCanvasWidth = Math.max(canvas.width, 1);
    const worldWidth =
      worldHeight * (safeCanvasWidth / safeCanvasHeight) * (1 / scaleRatio);
 
    return [
      Math.max(
        (safeCanvasWidth * (columnPixelSpacing || 1)) /
          Math.max(worldWidth, EPSILON),
        EPSILON
      ),
      Math.max(
        (safeCanvasHeight * (rowPixelSpacing || 1)) / worldHeight,
        EPSILON
      ),
    ];
  }
 
  return Math.max(
    Math.min(rowPixelSpacing || 1, columnPixelSpacing || 1) *
      worldToCanvasScale,
    EPSILON
  );
}