All files / extensions/cornerstone/src/utils getViewportOrientationFromImageOrientationPatient.ts

100% Statements 14/14
100% Branches 12/12
100% Functions 1/1
100% Lines 14/14

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    34x                                       34x   77x 5x     72x 72x 72x     72x       38x     34x       1x     33x       28x     5x    
import { CONSTANTS, utilities } from '@cornerstonejs/core';
 
const { MPR_CAMERA_VALUES } = CONSTANTS;
 
/**
 * Determines the viewport orientation (axial, sagittal, or coronal) based on the image orientation patient values.
 * This is done by comparing the view vectors with predefined MPR camera values.
 *
 * @param imageOrientationPatient - Array of 6 numbers representing the image orientation patient values.
 * The first 3 numbers represent the direction cosines of the first row and the second 3 numbers
 * represent the direction cosines of the first column.
 *
 * @returns The viewport orientation as a string ('axial', 'sagittal', 'coronal') or undefined if
 * the orientation cannot be determined or if the input is invalid.
 *
 * @example
 * ```typescript
 * const orientation = getViewportOrientationFromImageOrientationPatient([1,0,0,0,1,0]);
 * console.debug(orientation); // 'axial'
 * ```
 */
export const getViewportOrientationFromImageOrientationPatient = (
  imageOrientationPatient: number[]
): string | undefined => {
  if (!imageOrientationPatient || imageOrientationPatient.length !== 6) {
    return undefined;
  }
 
  const viewRight = imageOrientationPatient.slice(0, 3);
  const viewDown = imageOrientationPatient.slice(3, 6);
  const viewUp = [-viewDown[0], -viewDown[1], -viewDown[2]];
 
  // Compare vectors with MPR camera values using utilities.isEqual
  if (
    utilities.isEqual(viewRight, MPR_CAMERA_VALUES.axial.viewRight) &&
    utilities.isEqual(viewUp, MPR_CAMERA_VALUES.axial.viewUp)
  ) {
    return 'axial';
  }
 
  if (
    utilities.isEqual(viewRight, MPR_CAMERA_VALUES.sagittal.viewRight) &&
    utilities.isEqual(viewUp, MPR_CAMERA_VALUES.sagittal.viewUp)
  ) {
    return 'sagittal';
  }
 
  if (
    utilities.isEqual(viewRight, MPR_CAMERA_VALUES.coronal.viewRight) &&
    utilities.isEqual(viewUp, MPR_CAMERA_VALUES.coronal.viewUp)
  ) {
    return 'coronal';
  }
 
  return undefined;
};