All files / packages/tools/src/tools/segmentation/strategies/compositions islandRemoval.ts

0% Statements 0/75
0% Branches 0/63
0% Functions 0/10
0% Lines 0/74

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 178 179                                                                                                                                                                                                                                                                                                                                                                     
import type { InitializedOperationData } from '../BrushStrategy';
import floodFill from '../../../../utilities/segmentation/floodFill';
import { triggerSegmentationDataModified } from '../../../../stateManagement/segmentation/triggerSegmentationEvents';
import StrategyCallbacks from '../../../../enums/StrategyCallbacks';
 
/**
 * Removes external islands and fills internal islands.
 * External islands are areas of preview which are not connected via fill or
 * preview colours to the clicked/dragged over points.
 * Internal islands are areas of non-preview which are entirely surrounded by
 * colours connected to the clicked/dragged over points.
 */
export default {
  [StrategyCallbacks.OnInteractionEnd]: (
    operationData: InitializedOperationData
  ) => {
    const {
      previewVoxelManager: previewVoxelManager,
      segmentationVoxelManager: segmentationVoxelManager,
      strategySpecificConfiguration,
      previewSegmentIndex,
      segmentIndex,
    } = operationData;
 
    if (!strategySpecificConfiguration.THRESHOLD || segmentIndex === null) {
      return;
    }
 
    const clickedPoints = previewVoxelManager.getPoints();
    if (!clickedPoints?.length) {
      return;
    }
 
    if (previewSegmentIndex === undefined) {
      return;
    }
 
    // Ensure the bounds includes the clicked points, otherwise the fill
    // fails.
    const boundsIJK = previewVoxelManager
      .getBoundsIJK()
      .map((bound, i) => [
        Math.min(bound[0], ...clickedPoints.map((point) => point[i])),
        Math.max(bound[1], ...clickedPoints.map((point) => point[i])),
      ]);
 
    if (boundsIJK.find((it) => it[0] < 0 || it[1] > 65535)) {
      // Nothing done, so just skip this
      return;
    }
 
    const floodedSet = new Set<number>();
    // Returns true for new colour, and false otherwise
    const getter = (i, j, k) => {
      if (
        i < boundsIJK[0][0] ||
        i > boundsIJK[0][1] ||
        j < boundsIJK[1][0] ||
        j > boundsIJK[1][1] ||
        k < boundsIJK[2][0] ||
        k > boundsIJK[2][1]
      ) {
        return -1;
      }
      const index = segmentationVoxelManager.toIndex([i, j, k]);
      if (floodedSet.has(index)) {
        // Values already flooded
        return -2;
      }
      const oldVal = segmentationVoxelManager.getAtIndex(index);
      const isIn =
        oldVal === previewSegmentIndex || oldVal === segmentIndex ? 1 : 0;
      if (!isIn) {
        segmentationVoxelManager.addPoint(index);
      }
      // 1 is values that are preview/segment index, 0 is everything else
      return isIn;
    };
 
    let floodedCount = 0;
 
    const onFlood = (i, j, k) => {
      const index = segmentationVoxelManager.toIndex([i, j, k]);
      if (floodedSet.has(index)) {
        return;
      }
      // Fill this point with an indicator that this point is connected
      previewVoxelManager.setAtIJK(i, j, k, previewSegmentIndex);
      floodedSet.add(index);
      floodedCount++;
    };
    clickedPoints.forEach((clickedPoint) => {
      // @ts-ignore - need to ignore the spread appication to array params
      if (getter(...clickedPoint) === 1) {
        floodFill(getter, clickedPoint, {
          onFlood,
          diagonals: true,
        });
      }
    });
 
    let clearedCount = 0;
    let previewCount = 0;
 
    const callback = ({ index, pointIJK, value: trackValue }) => {
      const value = segmentationVoxelManager.getAtIndex(index);
      if (floodedSet.has(index)) {
        previewCount++;
        const newValue =
          trackValue === segmentIndex ? segmentIndex : previewSegmentIndex;
        previewVoxelManager.setAtIJKPoint(pointIJK, newValue);
      } else if (value === previewSegmentIndex) {
        clearedCount++;
        const newValue = trackValue ?? 0;
        previewVoxelManager.setAtIJKPoint(pointIJK, newValue);
      }
    };
 
    previewVoxelManager.forEach(callback, {});
 
    if (floodedCount - previewCount !== 0) {
      console.warn(
        'There were flooded=',
        floodedCount,
        'cleared=',
        clearedCount,
        'preview count=',
        previewCount,
        'not handled',
        floodedCount - previewCount
      );
    }
    const islandMap = new Set(segmentationVoxelManager.points || []);
    floodedSet.clear();
 
    for (const index of islandMap.keys()) {
      if (floodedSet.has(index)) {
        continue;
      }
      let isInternal = true;
      const internalSet = new Set<number>();
      const onFloodInternal = (i, j, k) => {
        const floodIndex = previewVoxelManager.toIndex([i, j, k]);
        floodedSet.add(floodIndex);
        if (
          (boundsIJK[0][0] !== boundsIJK[0][1] &&
            (i === boundsIJK[0][0] || i === boundsIJK[0][1])) ||
          (boundsIJK[1][0] !== boundsIJK[1][1] &&
            (j === boundsIJK[1][0] || j === boundsIJK[1][1])) ||
          (boundsIJK[2][0] !== boundsIJK[2][1] &&
            (k === boundsIJK[2][0] || k === boundsIJK[2][1]))
        ) {
          isInternal = false;
        }
        if (isInternal) {
          internalSet.add(floodIndex);
        }
      };
      const pointIJK = previewVoxelManager.toIJK(index);
      if (getter(...pointIJK) !== 0) {
        continue;
      }
      floodFill(getter, pointIJK, {
        onFlood: onFloodInternal,
        diagonals: false,
      });
      if (isInternal) {
        for (const index of internalSet) {
          previewVoxelManager.setAtIndex(index, previewSegmentIndex);
        }
      }
    }
    triggerSegmentationDataModified(
      operationData.segmentationId,
      previewVoxelManager.getArrayOfSlices()
    );
  },
};