All files / packages/tools/src/utilities/boundingBox getBoundingBoxAroundShape.ts

100% Statements 28/28
50% Branches 1/2
100% Functions 2/2
100% Lines 28/28

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                              9x 9x 9x 9x 9x 9x   9x 34x 34x 34x 34x 34x 34x     9x 9x 9x 9x 9x 9x   9x   36x 9x 9x 9x 9x 9x 9x     9x                
import type { Types } from '@cornerstonejs/core';
 
/**
 * With a given vertices (points) coordinates in IJK, it calculates the minimum and maximum
 * coordinate in each axis, and returns them. If dimensions are provided it also
 * clip the min, max to the provided width, height and depth
 *
 * @param points - shape corner points coordinates (IJK)
 * @param dimensions - dimensions of the image
 * @returns [[xMin,xMax],[yMin,yMax], [zMin,zMax]]
 */
function getBoundingBoxAroundShape(
  points: Types.Point3[],
  dimensions?: Types.Point3
): [Types.Point2, Types.Point2, Types.Point2] {
  let xMin = Infinity;
  let xMax = 0;
  let yMin = Infinity;
  let yMax = 0;
  let zMin = Infinity;
  let zMax = 0;
 
  points.forEach((p) => {
    xMin = Math.min(p[0], xMin);
    xMax = Math.max(p[0], xMax);
    yMin = Math.min(p[1], yMin);
    yMax = Math.max(p[1], yMax);
    zMin = Math.min(p[2], zMin);
    zMax = Math.max(p[2], zMax);
  });
 
  xMin = Math.floor(xMin);
  xMax = Math.floor(xMax);
  yMin = Math.floor(yMin);
  yMax = Math.floor(yMax);
  zMin = Math.floor(zMin);
  zMax = Math.floor(zMax);
 
  Eif (dimensions) {
    // clip the min, max to the provided width, height and depth
    const [width, height, depth] = dimensions;
    xMin = Math.max(0, xMin);
    xMax = Math.min(width - 1, xMax);
    yMin = Math.max(0, yMin);
    yMax = Math.min(height - 1, yMax);
    zMin = Math.max(0, zMin);
    zMax = Math.min(depth - 1, zMax);
  }
 
  return [
    [xMin, xMax],
    [yMin, yMax],
    [zMin, zMax],
  ];
}
 
export default getBoundingBoxAroundShape;