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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | 128x | import type { Types } from '@cornerstonejs/core';
/** Visited in slice-lazy flood (bit 0). Room for more flags later (e.g. queued). */
export const FLOOD_SLICE_FLAG_VISITED = 1;
export type FloodFillSliceLazyOptions = {
width: number;
height: number;
depth: number;
equals: (node: unknown, startNode: unknown) => boolean;
ensureSliceLoaded?: (z: number) => Promise<void>;
yieldEvery?: number;
planar?: boolean;
/**
* Optional bound in index-k from the seed slice: allow only voxels with
* `abs(k - seedK) <= maxDeltaK`.
*/
maxDeltaK?: number;
/**
* Optional in-slice bound from the seed pixel: allow only voxels with
* `abs(i - seedI) <= maxDeltaIJ` AND `abs(j - seedJ) <= maxDeltaIJ`.
*/
maxDeltaIJ?: number;
/** Cooperative cancellation hook; returns true to stop at next checkpoint. */
isCancelled?: () => boolean;
/**
* Compute-safety budget on filled voxels: the flood stops as soon as more
* voxels than this would fill and the result is flagged `truncated`.
*/
maxVoxels?: number;
/**
* Periodic region-shape gate: called every `validateEvery` filled voxels
* with the running stats; returning false stops the flood and flags
* `truncated`. Lets callers reject non-coherent regions (e.g. a sprawling
* bone web whose shape is nothing like a lesion) long before the budget.
*/
shouldContinue?: (stats: {
voxelCount: number;
bbox: { min: Types.Point3; max: Types.Point3 };
}) => boolean;
/** How often (in filled voxels) `shouldContinue` runs. Default 2048. */
validateEvery?: number;
};
export type FloodFillSliceLazyResult = {
/** Only slices that received at least one filled voxel are present. */
sliceMasks: Map<number, Uint8Array>;
voxelCount: number;
/** True when the fill was stopped by `maxVoxels` or `shouldContinue`. */
truncated: boolean;
/** Bounding box (inclusive, ijk) of the filled region; null when empty. */
bbox: { min: Types.Point3; max: Types.Point3 } | null;
};
/**
* 3D flood fill with **per-slice** `Uint8Array` visit masks (allocated on demand per k).
* Uses BFS and a packed linear queue to avoid `Set` hashing and a single giant 3D buffer.
*/
export async function floodFill3dSliceLazy(
getter: (x: number, y: number, z: number) => number | undefined,
seed: Types.Point3,
options: FloodFillSliceLazyOptions
): Promise<FloodFillSliceLazyResult> {
const {
width: w,
height: h,
depth: d,
equals,
ensureSliceLoaded,
yieldEvery = 500,
planar = false,
maxDeltaK,
maxDeltaIJ,
isCancelled,
maxVoxels,
shouldContinue,
validateEvery = 2048,
} = options;
const [sx, sy, sz] = seed;
if (sx < 0 || sx >= w || sy < 0 || sy >= h || sz < 0 || sz >= d) {
return {
sliceMasks: new Map(),
voxelCount: 0,
truncated: false,
bbox: null,
};
}
if (ensureSliceLoaded) {
await ensureSliceLoaded(sz);
}
const startNode = getter(sx, sy, sz);
if (!equals(startNode, startNode)) {
return {
sliceMasks: new Map(),
voxelCount: 0,
truncated: false,
bbox: null,
};
}
const frameSize = w * h;
const sliceMasks = new Map<number, Uint8Array>();
function sliceFlags(z: number): Uint8Array {
let a = sliceMasks.get(z);
if (!a) {
a = new Uint8Array(frameSize);
sliceMasks.set(z, a);
}
return a;
}
function isVisited(z: number, x: number, y: number): boolean {
const a = sliceMasks.get(z);
if (!a) {
return false;
}
return (a[y * w + x] & FLOOD_SLICE_FLAG_VISITED) !== 0;
}
function setVisited(z: number, x: number, y: number): void {
sliceFlags(z)[y * w + x] |= FLOOD_SLICE_FLAG_VISITED;
}
function pack(x: number, y: number, z: number): number {
return z * frameSize + y * w + x;
}
function unpack(p: number): [number, number, number] {
const x = p % w;
const t1 = Math.floor(p / w);
const y = t1 % h;
const z = Math.floor(t1 / h);
return [x, y, z];
}
const dirs = planar
? [
[1, 0, 0],
[-1, 0, 0],
[0, 1, 0],
[0, -1, 0],
]
: [
[1, 0, 0],
[-1, 0, 0],
[0, 1, 0],
[0, -1, 0],
[0, 0, 1],
[0, 0, -1],
];
const queue: number[] = [];
let qh = 0;
queue.push(pack(sx, sy, sz));
setVisited(sz, sx, sy);
let minX = sx;
let maxX = sx;
let minY = sy;
let maxY = sy;
let minZ = sz;
let maxZ = sz;
const currentBBox = () => ({
min: [minX, minY, minZ] as Types.Point3,
max: [maxX, maxY, maxZ] as Types.Point3,
});
let steps = 0;
let truncated = false;
let nextValidateAt = validateEvery;
while (qh < queue.length) {
if (isCancelled?.()) {
break;
}
if (maxVoxels !== undefined && queue.length > maxVoxels) {
truncated = true;
break;
}
if (shouldContinue && queue.length >= nextValidateAt) {
nextValidateAt += validateEvery;
if (!shouldContinue({ voxelCount: queue.length, bbox: currentBBox() })) {
truncated = true;
break;
}
}
steps++;
if (yieldEvery > 0 && steps % yieldEvery === 0) {
await new Promise<void>((r) => setTimeout(r, 0));
}
const p = queue[qh++];
const [x, y, z] = unpack(p);
for (let di = 0; di < dirs.length; di++) {
const nx = x + dirs[di][0];
const ny = y + dirs[di][1];
const nz = z + dirs[di][2];
if (nx < 0 || nx >= w || ny < 0 || ny >= h || nz < 0 || nz >= d) {
continue;
}
if (maxDeltaK >= 0 && Math.abs(nz - sz) > maxDeltaK) {
continue;
}
if (
maxDeltaIJ >= 0 &&
(Math.abs(nx - sx) > maxDeltaIJ || Math.abs(ny - sy) > maxDeltaIJ)
) {
continue;
}
if (planar && nz !== sz) {
continue;
}
if (isVisited(nz, nx, ny)) {
continue;
}
if (ensureSliceLoaded) {
await ensureSliceLoaded(nz);
if (isCancelled?.()) {
break;
}
}
const nv = getter(nx, ny, nz);
if (!equals(nv, startNode)) {
continue;
}
setVisited(nz, nx, ny);
queue.push(pack(nx, ny, nz));
if (nx < minX) {
minX = nx;
} else if (nx > maxX) {
maxX = nx;
}
if (ny < minY) {
minY = ny;
} else if (ny > maxY) {
maxY = ny;
}
if (nz < minZ) {
minZ = nz;
} else if (nz > maxZ) {
maxZ = nz;
}
}
}
// Every setVisited is paired with exactly one queue.push (seed included),
// and the queue is head-pointer based (never popped), so its length is the
// filled count — no need to rescan the allocated full-frame masks.
const voxelCount = queue.length;
return {
sliceMasks,
voxelCount,
truncated,
bbox: voxelCount > 0 ? currentBBox() : null,
};
}
|