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

85.71% Statements 18/21
50% Branches 2/4
66.66% Functions 2/3
85.71% Lines 18/21

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                  1x   1x               1x     1x       1x 1x 4x   1x   1x 1x                 1x           1x   1x 1x                             1x                             1x   1x                             1x                          
import type { Types } from '@cornerstonejs/core';
import { utilities as csUtils } from '@cornerstonejs/core';
import { vec3 } from 'gl-matrix';
 
import BrushStrategy from './BrushStrategy';
import type { InitializedOperationData, Composition } from './BrushStrategy';
import compositions from './compositions';
import StrategyCallbacks from '../../../enums/StrategyCallbacks';
import { createEllipseInPoint } from './fillCircle';
const { transformWorldToIndex } = csUtils;
import { getSphereBoundsInfo } from '../../../utilities/getSphereBoundsInfo';
const sphereComposition = {
  [StrategyCallbacks.Initialize]: (operationData: InitializedOperationData) => {
    const {
      points,
      imageVoxelManager: imageVoxelManager,
      viewport,
      segmentationImageData,
      segmentationVoxelManager: segmentationVoxelManager,
    } = operationData;
 
    // Happens on a preview setup
    Iif (!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 {
      boundsIJK: newBoundsIJK,
      topLeftWorld,
      bottomRightWorld,
    } = getSphereBoundsInfo(
      points.slice(0, 2) as [Types.Point3, Types.Point3],
      segmentationImageData,
      viewport
    );
 
    segmentationVoxelManager.boundsIJK = newBoundsIJK;
 
    if (imageVoxelManager) {
      imageVoxelManager.isInObject = createEllipseInPoint({
        topLeftWorld,
        bottomRightWorld,
        center,
      });
    } else E{
      segmentationVoxelManager.isInObject = createEllipseInPoint({
        topLeftWorld,
        bottomRightWorld,
        center,
      });
    }
  },
} as Composition;
 
const SPHERE_STRATEGY = new BrushStrategy(
  'Sphere',
  compositions.regionFill,
  compositions.setValue,
  sphereComposition,
  compositions.determineSegmentIndex,
  compositions.preview
);
 
/**
 * Fill inside a sphere with the given segment index in the given operation data. The
 * operation data contains the sphere required points.
 * @param enabledElement - The element that is enabled and selected.
 * @param operationData - OperationData
 */
const fillInsideSphere = SPHERE_STRATEGY.strategyFunction;
 
const SPHERE_THRESHOLD_STRATEGY = new BrushStrategy(
  'SphereThreshold',
  ...SPHERE_STRATEGY.compositions,
  compositions.dynamicThreshold,
  compositions.threshold,
  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 filled.
 * @param operationData - EraseOperationData
 */
 
const thresholdInsideSphere = SPHERE_THRESHOLD_STRATEGY.strategyFunction;
 
/**
 * Fill outside a sphere with the given segment index in the given operation data. The
 * operation data contains the sphere required points.
 * @param enabledElement - The element that is enabled and selected.
 * @param operationData - OperationData
 */
export function fillOutsideSphere(): void {
  throw new Error('fill outside sphere not implemented');
}
 
export { fillInsideSphere, thresholdInsideSphere, SPHERE_STRATEGY };