All files / packages/tools/src/utilities/rectangleROITool isAxisAlignedRectangle.ts

100% Statements 14/14
75% Branches 3/4
100% Functions 4/4
100% Lines 13/13

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      1x   1x 1x 1x   1x                 8x           8x             8x 8x       8x   48x           8x         48x        
import { vec3 } from 'gl-matrix';
import { utilities as csUtils } from '@cornerstonejs/core';
 
const { isEqual } = csUtils;
 
const iAxis = vec3.fromValues(1, 0, 0);
const jAxis = vec3.fromValues(0, 1, 0);
const kAxis = vec3.fromValues(0, 0, 1);
 
const axisList = [iAxis, jAxis, kAxis];
 
/**
 * Determines whether a given rectangle in a 3D space (defined by its corner
 * points in IJK coordinates) is aligned with the IJK axes.
 * @param rectangleCornersIJK - The corner points of the rectangle in IJK coordinates
 * @returns True if the rectangle is aligned with the IJK axes, false otherwise
 */
function isAxisAlignedRectangle(rectangleCornersIJK) {
  const rectangleVec1 = vec3.subtract(
    vec3.create(),
    rectangleCornersIJK[0],
    rectangleCornersIJK[1]
  );
 
  const rectangleVec2 = vec3.subtract(
    vec3.create(),
    rectangleCornersIJK[0],
    rectangleCornersIJK[2]
  );
 
  // Calculate the angles with IJK axes for both vectors
  const anglesVec1 = calculateAnglesWithAxes(rectangleVec1, axisList);
  const anglesVec2 = calculateAnglesWithAxes(rectangleVec2, axisList);
 
  // Check if all angles are aligned (0, 90, 180, or 270 degrees)
  // we could do csUtils.isEqual(angle % 90, 0) but this is more explicit for reading
  const isAligned = [...anglesVec1, ...anglesVec2].every(
    (angle) =>
      isEqual(angle, 0) ||
      isEqual(angle, 90) ||
      isEqual(angle, 180) ||
      isEqual(angle, 270)
  );
 
  return isAligned;
}
 
// Function to calculate angle with IJK axes
function calculateAnglesWithAxes(vec, axes) {
  return axes.map((axis) => (vec3.angle(vec, axis) * 180) / Math.PI);
}
 
export { isAxisAlignedRectangle };