All files / tools/src/utilities/segmentation VolumetricCalculator.ts

77.27% Statements 34/44
76% Branches 19/25
58.33% Functions 7/12
76.74% Lines 33/43

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                428x                     588x                         11680x 11680x 11680x   11680x       6640x       5040x                   5040x 1424x   3616x 19936x 3616x 3616x         5040x 3440x                           160x 160x                 160x   160x                 160x   1600x   160x     160x   160x             428x     160x 160x               11680x 11680x                 160x           160x 160x                                                                                                              
import type { Types } from '@cornerstonejs/core';
import type { NamedStatistics } from '../../types';
import {
  BasicStatsCalculator,
  InstanceBasicStatsCalculator,
} from '../math/basic/BasicStatsCalculator';
import { getCalibratedLengthUnitsAndScale } from '../getCalibratedUnits';
 
const TEST_MAX_LOCATIONS = 10;
 
interface VolumetricState {
  maxIJKs: Array<{
    value: number;
    pointLPS?: Types.Point3;
    pointIJK?: Types.Point3;
  }>;
}
 
function createVolumetricState(): VolumetricState {
  return {
    maxIJKs: [],
  };
}
 
function volumetricStatsCallback(
  state: VolumetricState,
  data: {
    value: number | Types.RGB;
    pointLPS?: Types.Point3;
    pointIJK?: Types.Point3;
  }
): void {
  const { value } = data;
  const { maxIJKs } = state;
  const length = maxIJKs.length;
 
  if (
    typeof value !== 'number' ||
    (length >= TEST_MAX_LOCATIONS && value < maxIJKs[0].value)
  ) {
    return;
  }
 
  // Create a deep copy of the data object to prevent reference issues
  const dataCopy = {
    value: data.value as number,
    pointLPS: data.pointLPS
      ? ([data.pointLPS[0], data.pointLPS[1], data.pointLPS[2]] as Types.Point3)
      : undefined,
    pointIJK: data.pointIJK
      ? ([data.pointIJK[0], data.pointIJK[1], data.pointIJK[2]] as Types.Point3)
      : undefined,
  };
 
  if (!length || value >= maxIJKs[length - 1].value) {
    maxIJKs.push(dataCopy);
  } else {
    for (let i = 0; i < length; i++) {
      if (value <= maxIJKs[i].value) {
        maxIJKs.splice(i, 0, dataCopy);
        break;
      }
    }
  }
 
  if (length >= TEST_MAX_LOCATIONS) {
    maxIJKs.splice(0, 1);
  }
}
 
function volumetricGetStatistics(
  state: VolumetricState,
  stats: NamedStatistics,
  options: {
    spacing?: number[] | number;
    calibration?: unknown;
    hasPixelSpacing?: boolean;
    unit?: string;
  }
): NamedStatistics {
  const { spacing, calibration } = options;
  const { volumeUnit } = getCalibratedLengthUnitsAndScale(
    // Todo: fix this for volumes; we don't have calibration for volumes yet
    // Also, if there is a volume, there should be spacing, so it is always true
    {
      calibration,
      hasPixelSpacing: true,
    },
    []
  );
  const volumeScale = spacing ? spacing[0] * spacing[1] * spacing[2] : 1;
 
  stats.volume = {
    value: Array.isArray(stats.count.value)
      ? stats.count.value.map((v: number) => v * volumeScale)
      : stats.count.value * volumeScale,
    unit: volumeUnit,
    name: 'volume',
    label: 'Volume',
  };
 
  stats.maxIJKs = state.maxIJKs.filter(
    (entry): entry is { value: number; pointIJK: Types.Point3 } =>
      entry.pointIJK !== undefined
  );
  stats.array.push(stats.volume);
 
  // Reset state
  state.maxIJKs = [];
 
  return stats;
}
 
/**
 * A basic stats calculator for volumetric data, generally for use with segmentations.
 */
export class VolumetricCalculator extends BasicStatsCalculator {
  private static volumetricState: VolumetricState = createVolumetricState();
 
  public static statsInit(options: { storePointData: boolean }): void {
    super.statsInit(options);
    this.volumetricState = createVolumetricState();
  }
 
  public static statsCallback(data: {
    value: number | Types.RGB;
    pointLPS?: Types.Point3;
    pointIJK?: Types.Point3;
  }): void {
    super.statsCallback(data);
    volumetricStatsCallback(this.volumetricState, data);
  }
 
  public static getStatistics(options: {
    spacing?: number[] | number;
    unit?: string;
    calibration?: unknown;
    hasPixelSpacing?: boolean;
  }): NamedStatistics {
    const optionsWithUnit = {
      ...options,
      unit: options?.unit || 'none',
      calibration: options?.calibration,
      hasPixelSpacing: options?.hasPixelSpacing,
    };
    const stats = super.getStatistics(optionsWithUnit);
    return volumetricGetStatistics(
      this.volumetricState,
      stats,
      optionsWithUnit
    );
  }
}
 
/**
 * An instantiable version of VolumetricCalculator that uses instance state.
 */
export class InstanceVolumetricCalculator extends InstanceBasicStatsCalculator {
  private volumetricState: VolumetricState;
 
  constructor(options: { storePointData: boolean }) {
    super(options);
    this.volumetricState = createVolumetricState();
  }
 
  statsInit(options: { storePointData: boolean }): void {
    super.statsInit(options);
    this.volumetricState = createVolumetricState();
  }
 
  statsCallback(data: {
    value: number | Types.RGB;
    pointLPS?: Types.Point3;
    pointIJK?: Types.Point3;
  }): void {
    super.statsCallback(data);
    volumetricStatsCallback(this.volumetricState, data);
  }
 
  getStatistics(options?: {
    spacing?: number[] | number;
    unit?: string;
    calibration?: unknown;
    hasPixelSpacing?: boolean;
  }): NamedStatistics {
    const optionsWithUnit = {
      ...options,
      unit: options?.unit || 'none',
      calibration: options?.calibration,
      hasPixelSpacing: options?.hasPixelSpacing,
    };
    const stats = super.getStatistics(optionsWithUnit);
    return volumetricGetStatistics(
      this.volumetricState,
      stats,
      optionsWithUnit
    );
  }
}
 
export default VolumetricCalculator;