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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import type { FloodFillResult, FloodFillGetter, FloodFillOptions, } from '../../types'; import { Types } from '@cornerstonejs/core'; /** * floodFill.js - Taken from MIT OSS lib - https://github.com/tuzz/n-dimensional-flood-fill * Refactored to ES6. * * @param {function} getter The getter to the elements of your data structure, * e.g. getter(x,y) for a 2D interprettation of your structure. * @param {number[]} seed The seed for your fill. The dimensionality is infered * by the number of dimensions of the seed. * @param {function} [options.onFlood] An optional callback to execute when each pixel is flooded. * e.g. onFlood(x,y). * @param {function} [options.onBoundary] An optional callback to execute whenever a boundary is reached. * a boundary could be another segmentIndex, or the edge of your * data structure (i.e. when your getter returns undefined). * @param {function} [options.equals] An optional equality method for your datastructure. * Default is simply value1 = value2. * @param {boolean} [options.diagonals] Whether you allow flooding through diagonals. Defaults to false. * * @returns {Object} */ function floodFill( getter: FloodFillGetter, seed: Types.Point2 | Types.Point3, options: FloodFillOptions = {} ): FloodFillResult { const onFlood = options.onFlood; const onBoundary = options.onBoundary; const equals = options.equals || defaultEquals; const diagonals = options.diagonals || false; const startNode = get(seed); const permutations = prunedPermutations(); const stack = []; const flooded = []; const visits = {}; const bounds = {}; stack.push({ currentArgs: seed }); while (stack.length > 0) { flood(stack.pop()); } return { flooded, boundaries: boundaries(), }; function flood(job) { const getArgs = job.currentArgs; const prevArgs = job.previousArgs; if (visited(getArgs)) { return; } markAsVisited(getArgs); if (member(getArgs)) { markAsFlooded(getArgs); pushAdjacent(getArgs); } else { markAsBoundary(prevArgs); } } function visited(key) { return visits[key] === true; } function markAsVisited(key) { visits[key] = true; } function member(getArgs) { const node = safely(get, [getArgs]); return safely(equals, [node, startNode]); } function markAsFlooded(getArgs) { flooded.push(getArgs); if (onFlood) { //@ts-ignore onFlood(...getArgs); } } function markAsBoundary(prevArgs) { bounds[prevArgs] = prevArgs; if (onBoundary) { //@ts-ignore onBoundary(...prevArgs); } } function pushAdjacent(getArgs) { for (let i = 0; i < permutations.length; i += 1) { const perm = permutations[i]; const nextArgs = getArgs.slice(0); for (let j = 0; j < getArgs.length; j += 1) { nextArgs[j] += perm[j]; } stack.push({ currentArgs: nextArgs, previousArgs: getArgs, }); } } function get(getArgs) { //@ts-ignore return getter(...getArgs); } function safely(f, args) { try { return f(...args); } catch (error) { return; } } function prunedPermutations() { const permutations = permute(seed.length); return permutations.filter(function (perm) { const count = countNonZeroes(perm); return count !== 0 && (count === 1 || diagonals); }); } function permute(length) { const perms = []; const permutation = function (string) { return string.split('').map(function (c) { return parseInt(c, 10) - 1; }); }; for (let i = 0; i < Math.pow(3, length); i += 1) { const string = lpad(i.toString(3), '0', length); perms.push(permutation(string)); } return perms; } function boundaries() { const array = []; for (const key in bounds) { if (bounds[key] !== undefined) { array.unshift(bounds[key]); } } return array; } } function defaultEquals(a, b) { return a === b; } function countNonZeroes(array) { let count = 0; for (let i = 0; i < array.length; i += 1) { if (array[i] !== 0) { count += 1; } } return count; } function lpad(string, character, length) { const array = new Array(length + 1); const pad = array.join(character); return (pad + string).slice(-length); } export default floodFill; |