All files / tools/src/utilities/segmentation/growCut neighborhoodStats.ts

0% Statements 0/32
0% Branches 0/26
0% Functions 0/1
0% Lines 0/29

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                                                                                                                                                                 
import type { Types } from '@cornerstonejs/core';
import type { VoxelManager } from '@cornerstonejs/core/utilities';
 
type NumberVoxelManager = VoxelManager<number>;
 
type NeighborhoodStats = {
  mean: number;
  stdDev: number;
  count: number;
};
 
/**
 * Mean and standard deviation over a cubic neighborhood, read through the voxel
 * manager's `getAtIJK` (no full scalar buffer required).
 *
 * Bundled with the flood-fill engine so it can take an optional `mapValue`
 * (e.g. VOI-mapped intensity) without changing core's shared
 * `calculateNeighborhoodStats`, whose signature other tools depend on.
 *
 * @param mapValue - If set, each sample is passed through this before mean/std.
 */
export function calculateNeighborhoodStatsVM(
  voxelManager: NumberVoxelManager,
  dimensions: Types.Point3,
  centerIjk: Types.Point3,
  radius: number,
  mapValue?: (v: number) => number
): NeighborhoodStats {
  const [width, height, numSlices] = dimensions;
 
  let sum = 0;
  let sumSq = 0;
  let count = 0;
 
  const [cx, cy, cz] = centerIjk.map(Math.round);
 
  for (let z = cz - radius; z <= cz + radius; z++) {
    if (z < 0 || z >= numSlices) {
      continue;
    }
    for (let y = cy - radius; y <= cy + radius; y++) {
      if (y < 0 || y >= height) {
        continue;
      }
      for (let x = cx - radius; x <= cx + radius; x++) {
        if (x < 0 || x >= width) {
          continue;
        }
 
        const raw = Number(voxelManager.getAtIJK(x, y, z));
        const value = mapValue ? mapValue(raw) : raw;
        sum += value;
        sumSq += value * value;
        count++;
      }
    }
  }
 
  if (count === 0) {
    if (
      cx >= 0 &&
      cx < width &&
      cy >= 0 &&
      cy < height &&
      cz >= 0 &&
      cz < numSlices
    ) {
      const raw = Number(voxelManager.getAtIJK(cx, cy, cz));
      const centerValue = mapValue ? mapValue(raw) : raw;
      return { mean: centerValue, stdDev: 0, count: 1 };
    }
    return { mean: 0, stdDev: 0, count: 0 };
  }
 
  const mean = sum / count;
  const variance = sumSq / count - mean * mean;
  const stdDev = Math.sqrt(Math.max(0, variance));
 
  return { mean, stdDev, count };
}