All files / packages/tools/src/tools/segmentation/strategies fillCircle.ts

39.47% Statements 15/38
57.14% Branches 4/7
28.57% Functions 2/7
37.83% Lines 14/37

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 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177                                1x   1x                                                                                                                                                 1x   1x 1x 1x   1x 1x         1x         8410x                           1x                 1x                                   1x               1x                                      
import { vec3 } from 'gl-matrix';
import { utilities as csUtils } from '@cornerstonejs/core';
import type { Types } from '@cornerstonejs/core';
 
import {
  getCanvasEllipseCorners,
  precalculatePointInEllipse,
} from '../../../utilities/math/ellipse';
import { getBoundingBoxAroundShapeIJK } from '../../../utilities/boundingBox';
import BrushStrategy from './BrushStrategy';
import type { Composition, InitializedOperationData } from './BrushStrategy';
import type { CanvasCoordinates } from '../../../types';
import { StrategyCallbacks } from '../../../enums';
import compositions from './compositions';
import { pointInSphere } from '../../../utilities/math/sphere';
 
const { transformWorldToIndex, isEqual } = csUtils;
 
const initializeCircle = {
  [StrategyCallbacks.Initialize]: (operationData: InitializedOperationData) => {
    const {
      points, // bottom, top, left, right
      imageVoxelManager: imageVoxelManager,
      viewport,
      segmentationImageData,
      segmentationVoxelManager: segmentationVoxelManager,
    } = operationData;
 
    // Happens on a preview setup
    if (!points) {
      return;
    }
    // Average the points to get the center of the ellipse
    const center = vec3.fromValues(0, 0, 0);
    points.forEach((point) => {
      vec3.add(center, center, point);
    });
    vec3.scale(center, center, 1 / points.length);
 
    operationData.centerWorld = center as Types.Point3;
    operationData.centerIJK = transformWorldToIndex(
      segmentationImageData,
      center as Types.Point3
    );
 
    const canvasCoordinates = points.map((p) =>
      viewport.worldToCanvas(p)
    ) as CanvasCoordinates;
 
    // 1. From the drawn tool: Get the ellipse (circle) topLeft and bottomRight
    // corners in canvas coordinates
    const [topLeftCanvas, bottomRightCanvas] =
      getCanvasEllipseCorners(canvasCoordinates);
 
    // 2. Find the extent of the ellipse (circle) in IJK index space of the image
    const topLeftWorld = viewport.canvasToWorld(topLeftCanvas);
    const bottomRightWorld = viewport.canvasToWorld(bottomRightCanvas);
 
    const circleCornersIJK = points.map((world) => {
      return transformWorldToIndex(segmentationImageData, world);
    });
 
    // get the bounds from the circle points since in oblique images the
    // circle will not be axis aligned
    const boundsIJK = getBoundingBoxAroundShapeIJK(
      circleCornersIJK,
      segmentationImageData.getDimensions()
    );
 
    segmentationVoxelManager.boundsIJK = boundsIJK;
    imageVoxelManager.isInObject = createPointInEllipse({
      topLeftWorld,
      bottomRightWorld,
      center,
    });
  },
} as Composition;
 
/**
 * Creates a function that tells the user if the provided point in LPS space
 * is inside the ellipse.
 *
 * This will return a sphere test function if the bounds are a circle or
 * sphere shape (same radius in two or three dimensions), or an elliptical shape
 * if they differ.
 */
function createPointInEllipse(worldInfo: {
  topLeftWorld: Types.Point3;
  bottomRightWorld: Types.Point3;
  center: Types.Point3 | vec3;
}) {
  const { topLeftWorld, bottomRightWorld, center } = worldInfo;
 
  const xRadius = Math.abs(topLeftWorld[0] - bottomRightWorld[0]) / 2;
  const yRadius = Math.abs(topLeftWorld[1] - bottomRightWorld[1]) / 2;
  const zRadius = Math.abs(topLeftWorld[2] - bottomRightWorld[2]) / 2;
 
  const radius = Math.max(xRadius, yRadius, zRadius);
  Eif (
    isEqual(xRadius, radius) &&
    isEqual(yRadius, radius) &&
    isEqual(zRadius, radius)
  ) {
    const sphereObj = {
      center,
      radius,
      radius2: radius * radius,
    };
    return (pointLPS) => pointInSphere(sphereObj, pointLPS);
  }
  // using circle as a form of ellipse
  const ellipseObj = {
    center: center as Types.Point3,
    xRadius,
    yRadius,
    zRadius,
  };
 
  const { precalculated } = precalculatePointInEllipse(ellipseObj, {});
  return precalculated;
}
 
const CIRCLE_STRATEGY = new BrushStrategy(
  'Circle',
  compositions.regionFill,
  compositions.setValue,
  initializeCircle,
  compositions.determineSegmentIndex,
  compositions.preview
);
 
const CIRCLE_THRESHOLD_STRATEGY = new BrushStrategy(
  'CircleThreshold',
  compositions.regionFill,
  compositions.setValue,
  initializeCircle,
  compositions.determineSegmentIndex,
  compositions.dynamicThreshold,
  compositions.threshold,
  compositions.preview,
  compositions.islandRemoval
);
 
/**
 * Fill inside the circular region segment inside the segmentation defined by the operationData.
 * It fills the segmentation pixels inside the defined circle.
 * @param enabledElement - The element for which the segment is being erased.
 * @param operationData - EraseOperationData
 */
const fillInsideCircle = CIRCLE_STRATEGY.strategyFunction;
 
/**
 * Fill inside the circular region segment inside the segmentation defined by the operationData.
 * It fills the segmentation pixels inside the defined circle.
 * @param enabledElement - The element for which the segment is being erased.
 * @param operationData - EraseOperationData
 */
const thresholdInsideCircle = CIRCLE_THRESHOLD_STRATEGY.strategyFunction;
 
/**
 * Fill outside the circular region segment inside the segmentation defined by the operationData.
 * It fills the segmentation pixels outside the  defined circle.
 * @param enabledElement - The element for which the segment is being erased.
 * @param operationData - EraseOperationData
 */
export function fillOutsideCircle(): void {
  throw new Error('Not yet implemented');
}
 
export {
  CIRCLE_STRATEGY,
  CIRCLE_THRESHOLD_STRATEGY,
  fillInsideCircle,
  thresholdInsideCircle,
  createPointInEllipse as createEllipseInPoint,
};