All files / tools/src/utilities/spatial getViewportPlane.ts

6.25% Statements 1/16
0% Branches 0/11
0% Functions 0/3
6.25% Lines 1/16

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          128x                                                                                                  
import { vec3 } from 'gl-matrix';
import type { Types } from '@cornerstonejs/core';
import getViewportICamera from '../getViewportICamera';
import type { Plane } from './types';
 
const MIN_NORMAL_LENGTH = 1e-10;
 
function isFinitePoint3(point: Types.Point3 | undefined): boolean {
  return (
    Array.isArray(point) &&
    point.length === 3 &&
    point.every((v) => Number.isFinite(v))
  );
}
 
/**
 * Returns the slice plane currently displayed by a viewport, defined by the
 * camera view-plane normal and focal point. The returned normal is normalized.
 *
 * Returns null when the viewport has no valid camera (e.g. non-image
 * viewports, or viewports that have not been rendered yet).
 */
export default function getViewportPlane(
  viewport: Types.IViewport
): Plane | null {
  if (!viewport) {
    return null;
  }
 
  let camera;
  try {
    camera = getViewportICamera(viewport);
  } catch {
    return null;
  }
 
  const viewPlaneNormal = camera?.viewPlaneNormal as Types.Point3 | undefined;
  const focalPoint = camera?.focalPoint as Types.Point3 | undefined;
 
  if (!isFinitePoint3(viewPlaneNormal) || !isFinitePoint3(focalPoint)) {
    return null;
  }
 
  if (vec3.length(viewPlaneNormal) < MIN_NORMAL_LENGTH) {
    return null;
  }
 
  const normal = vec3.normalize(vec3.create(), viewPlaneNormal);
 
  return {
    normal: [normal[0], normal[1], normal[2]],
    point: [focalPoint[0], focalPoint[1], focalPoint[2]],
  };
}