diff --git a/src/components/VolumeRendering.vue b/src/components/VolumeRendering.vue index 8ee168645..4771ca312 100644 --- a/src/components/VolumeRendering.vue +++ b/src/components/VolumeRendering.vue @@ -254,9 +254,11 @@ export default defineComponent({ return [0, 1]; }); + // The store refits a relative preset's opacity range once a streaming image loads. const fullMappingRange = computed( () => getColorFunctionRangeFromPreset(selectedPreset.value || '') || + opacityFunction.value?.mappingRange || imageDataRange.value ); const fullMappingRangeWidth = computed(() => { diff --git a/src/composables/useVolumeThumbnailing.ts b/src/composables/useVolumeThumbnailing.ts index a7efde384..5bc8bb134 100644 --- a/src/composables/useVolumeThumbnailing.ts +++ b/src/composables/useVolumeThumbnailing.ts @@ -2,7 +2,6 @@ import { onBeforeUnmount, reactive, ref, watch } from 'vue'; import { Mode as LookupTableProxyMode } from '@kitware/vtk.js/Proxy/Core/LookupTableProxy'; import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; import vtkPiecewiseFunctionProxy from '@kitware/vtk.js/Proxy/Core/PiecewiseFunctionProxy'; -import { getPresetByName } from '@/src/types/color-map-preset'; import { createVolumeThumbnailer } from '../core/thumbnailers/volume-thumbnailer'; import { useCameraOrientation } from './useCameraOrientation'; import { useImageStore } from '../store/datasets-images'; @@ -10,6 +9,9 @@ import { useCurrentImage } from './useCurrentImage'; import { getColorFunctionRangeFromPreset, getOpacityRangeFromPreset, + getRelativePresetRange, + getShiftedOpacityFromPreset, + RELATIVE_PRESET_GAUSSIANS, } from '../utils/vtk-helpers'; import { PresetNameList } from '../vtk/ColorMaps'; import { logError } from '../utils/loggers'; @@ -22,23 +24,13 @@ function resetOpacityFunction( // reset pwf proxy range pwfProxy.setDataRange(...dataRange); - const preset = getPresetByName(presetName); - if (preset?.OpacityPoints) { - const OpacityPoints = preset.OpacityPoints; - const points = []; - for (let i = 0; i < OpacityPoints.length; i += 2) { - points.push([OpacityPoints[i], OpacityPoints[i + 1]]); - } - - const [xmin, xmax] = dataRange; - const width = xmax - xmin; - const pointsNormalized = points.map(([x, y]) => [(x - xmin) / width, y]); - + const points = getShiftedOpacityFromPreset(presetName, dataRange, 0, 0); + if (points) { pwfProxy.setMode(vtkPiecewiseFunctionProxy.Mode.Points); - pwfProxy.setPoints(pointsNormalized); + pwfProxy.setPoints(points); } else { pwfProxy.setMode(vtkPiecewiseFunctionProxy.Mode.Gaussians); - pwfProxy.setGaussians(vtkPiecewiseFunctionProxy.Defaults.Gaussians); + pwfProxy.setGaussians(RELATIVE_PRESET_GAUSSIANS); } } @@ -75,7 +67,7 @@ export function useVolumeThumbnailing( interruptSentinel = localSentinel; thumbnailer.setInputImage(image); - const imageDataRange = image.getPointData().getScalars().getRange(); + const relativeRange = getRelativePresetRange(image); async function helper(presetName: string) { // bail if a new thumbnail process has started @@ -99,7 +91,7 @@ export function useVolumeThumbnailing( const opRange = getOpacityRangeFromPreset(presetName); resetOpacityFunction( thumbnailer.opacityFuncProxy, - opRange || imageDataRange, + opRange || relativeRange, presetName ); @@ -107,7 +99,7 @@ export function useVolumeThumbnailing( thumbnailer.colorTransferFuncProxy.setPresetName(presetName); const ctRange = getColorFunctionRangeFromPreset(presetName); thumbnailer.colorTransferFuncProxy.setDataRange( - ...(ctRange || imageDataRange) + ...(ctRange || relativeRange) ); thumbnailer.resetCameraWithOrientation( diff --git a/src/config.ts b/src/config.ts index 27072f915..a3f97db64 100644 --- a/src/config.ts +++ b/src/config.ts @@ -196,6 +196,9 @@ export const DEFAULT_PRESET_BY_MODALITY: Record = { export const DEFAULT_PRESET = 'CT-AAA'; +// Maps onto each image's robust scalar range. +export const DEFAULT_GRAYSCALE_PRESET = 'Grayscale'; + // Keyboard shortcuts/hotkeys. Can add modifiers: 'Shift+Ctrl+a' export const ACTION_TO_KEY = { windowLevel: 'l', diff --git a/src/store/view-configs/volume-coloring.ts b/src/store/view-configs/volume-coloring.ts index a9d5fdaf9..9ffa27009 100644 --- a/src/store/view-configs/volume-coloring.ts +++ b/src/store/view-configs/volume-coloring.ts @@ -3,11 +3,16 @@ import vtkPiecewiseFunctionProxy from '@kitware/vtk.js/Proxy/Core/PiecewiseFunct import { getColorFunctionRangeFromPreset, getOpacityFunctionFromPreset, + getRelativePresetRange, + hasVisibleOpacity, } from '@/src/utils/vtk-helpers'; -import { DEFAULT_PRESET_BY_MODALITY, DEFAULT_PRESET } from '@/src/config'; -import { ColorTransferFunction } from '@/src/types/views'; +import { + DEFAULT_GRAYSCALE_PRESET, + DEFAULT_PRESET_BY_MODALITY, + DEFAULT_PRESET, +} from '@/src/config'; import { defineStore } from 'pinia'; -import { reactive } from 'vue'; +import { getCurrentScope, reactive, watch } from 'vue'; import { DoubleKeyRecord, deleteSecondKey, @@ -17,6 +22,7 @@ import { import { DeepPartial, Maybe } from '@/src/types'; import { identity } from '@/src/utils'; import { getDeclaredModality } from '@/src/utils/declaredModality'; +import { looksLikeHounsfieldRange } from '@/src/utils/hounsfieldRange'; import { useImageCacheStore } from '@/src/store/image-cache'; import { createViewConfigSerializer } from './common'; import { ViewConfig } from '../../io/state-file/schema'; @@ -29,32 +35,31 @@ export const DEFAULT_SPECULAR = 0.3; export const DEFAULT_EDGE_GRADIENT = 0.2; export const DEFAULT_SAMPLING_DISTANCE = 0.2; -function getPresetFromImageModality(imageID: string) { +function getDeclaredPreset(imageID: string) { const modality = getDeclaredModality( useDICOMStore().volumeInfo[imageID]?.Modality, useImageCacheStore().imageById[imageID]?.headerMetadata ); - return DEFAULT_PRESET_BY_MODALITY[modality] ?? DEFAULT_PRESET; + return DEFAULT_PRESET_BY_MODALITY[modality]; } /** - * Gets partial color and opacity function configs from a preset. - * @param preset - * @returns + * Checks against the scalar range need every voxel, so a streaming image keeps + * its declared preset, or the CT guess, for good. */ -function getColorAndOpacityFuncsFromPreset(preset: string) { - const ctFunc: Partial = { - preset, - }; - - const ctRange = getColorFunctionRangeFromPreset(preset); - if (ctRange) { - ctFunc.mappingRange = ctRange; - } - - const opFunc = getOpacityFunctionFromPreset(preset); - - return { colorFunc: ctFunc, opacityFunc: opFunc }; +function getDefaultPreset(imageID: string, image: vtkImageData) { + const declared = getDeclaredPreset(imageID); + const scalars = image.getPointData().getScalars(); + if ( + scalars.getNumberOfComponents() !== 1 || + !useImageCacheStore().imageById[imageID]?.isLoaded() + ) + return declared ?? DEFAULT_PRESET; + const range = scalars.getRange(); + const fits = declared + ? hasVisibleOpacity(declared, range) + : looksLikeHounsfieldRange(range); + return fits ? (declared ?? DEFAULT_PRESET) : DEFAULT_GRAYSCALE_PRESET; } export const defaultVolumeColorConfig = (): VolumeColorConfig => ({ @@ -134,42 +139,73 @@ export const useVolumeColoringStore = defineStore('volumeColoring', () => { const updateOpacityFunction = createUpdateFunc('opacityFunction'); const updateCVRParameters = createUpdateFunc('cvr'); + const storeScope = getCurrentScope(); + const pendingRefits = new Map void>(); + + // A range fitted to a streaming image's partial voxels is refit once it loads. + const refitWhenLoaded = (viewID: string, imageID: string, preset: string) => { + const key = JSON.stringify([viewID, imageID]); + pendingRefits.get(key)?.(); + const imageCacheStore = useImageCacheStore(); + const stop = storeScope!.run(() => + watch( + () => imageCacheStore.imageById[imageID]?.isLoaded() ?? true, + (settled) => { + if (!settled) return; + stop?.(); + pendingRefits.delete(key); + if ( + imageCacheStore.getVtkImageData(imageID) && + getConfig(viewID, imageID)?.transferFunction.preset === preset + ) + setColorPreset(viewID, imageID, preset); + } + ) + ); + if (stop) pendingRefits.set(key, stop); + }; + const setColorPreset = (viewID: string, imageID: string, preset: string) => { const imageCacheStore = useImageCacheStore(); const image = imageCacheStore.getVtkImageData(imageID); if (!image) throw new Error('Invalid imageID'); - const imageDataRange = image.getPointData().getScalars().getRange(); - const { colorFunc, opacityFunc } = - getColorAndOpacityFuncsFromPreset(preset); - colorFunc.mappingRange ||= imageDataRange; - - const completeOpacityFunc = { - ...opacityFunc, - mappingRange: imageDataRange, - }; + const presetRange = getColorFunctionRangeFromPreset(preset); + // Absolute opacity points stay in scalar units, edited over the data range. + const opacityRange = presetRange + ? image.getPointData().getScalars().getRange() + : getRelativePresetRange(image); - updateColorTransferFunction(viewID, imageID, colorFunc); - updateOpacityFunction(viewID, imageID, completeOpacityFunc); + updateColorTransferFunction(viewID, imageID, { + preset, + mappingRange: presetRange ?? opacityRange, + }); + updateOpacityFunction(viewID, imageID, { + ...getOpacityFunctionFromPreset(preset), + mappingRange: opacityRange, + }); + if (!presetRange && !imageCacheStore.imageById[imageID]?.isLoaded()) + refitWhenLoaded(viewID, imageID, preset); }; + const getInitialPreset = (dataID: string, image: vtkImageData) => + defaultConfigs[dataID]?.transferFunction?.preset ?? + getDefaultPreset(dataID, image); + const resetToDefaultColoring = ( viewID: string, dataID: string, image: vtkImageData ) => { const defaults = defaultConfigs[dataID]; + const preset = getInitialPreset(dataID, image); const scalars = image.getPointData().getScalars(); updateColorBy(viewID, dataID, { arrayName: defaults?.colorBy?.arrayName ?? scalars.getName(), location: defaults?.colorBy?.location ?? 'pointData', }); - setColorPreset( - viewID, - dataID, - defaults?.transferFunction?.preset ?? getPresetFromImageModality(dataID) - ); + setColorPreset(viewID, dataID, preset); }; /** diff --git a/src/utils/__tests__/hounsfieldRange.spec.ts b/src/utils/__tests__/hounsfieldRange.spec.ts new file mode 100644 index 000000000..adb35978f --- /dev/null +++ b/src/utils/__tests__/hounsfieldRange.spec.ts @@ -0,0 +1,16 @@ +import { describe, expect, it } from 'vitest'; +import '@/src/vtk/ColorMaps'; +import { looksLikeHounsfieldRange } from '@/src/utils/hounsfieldRange'; + +describe('looksLikeHounsfieldRange', () => { + it.each([ + ['CT with air', [-1024, 3071], true], + ['CT with scanner padding', [-3024, 1500], true], + ['resampled CT', [-800, 1200], true], + ['magnitude MR', [0, 4000], false], + ['normalized intensities', [-3, 5], false], + ['air without denser tissue', [-1000, 50], false], + ] as const)('%s', (_, range, expected) => { + expect(looksLikeHounsfieldRange(range)).toBe(expected); + }); +}); diff --git a/src/utils/__tests__/robustScalarRange.spec.ts b/src/utils/__tests__/robustScalarRange.spec.ts new file mode 100644 index 000000000..3ccca0e7e --- /dev/null +++ b/src/utils/__tests__/robustScalarRange.spec.ts @@ -0,0 +1,38 @@ +import { describe, expect, it } from 'vitest'; +import { robustScalarRange } from '@/src/utils/robustScalarRange'; + +const range = (values: number[]) => + robustScalarRange(values, [Math.min(...values), Math.max(...values)]); + +describe('robustScalarRange', () => { + it('ignores a dominant background and clips outliers', () => { + const values = [ + ...new Array(9000).fill(0), + ...Array.from({ length: 1000 }, (_, index) => 100 + (index % 100)), + 1e6, + ]; + expect(range(values)).toEqual([101, 199]); + }); + + it('ramps a binary mask up from its background', () => { + expect(range([0, 0, 0, 1])).toEqual([0, 1]); + }); + + it('returns the full range of a constant image', () => { + expect(robustScalarRange(new Float32Array(8).fill(3), [3, 3])).toEqual([ + 3, 3, + ]); + }); + + it('skips non-finite intensities', () => { + expect( + robustScalarRange([NaN, Infinity, 0, 0.25, 0.5, 0.75], [0, Infinity]) + ).toEqual([0.25, 0.75]); + }); + + it('falls back to the known range when sampling misses the foreground', () => { + const values = new Uint8Array(2 ** 20); + values[12345] = 200; + expect(robustScalarRange(values, [0, 200])).toEqual([0, 200]); + }); +}); diff --git a/src/utils/hounsfieldRange.ts b/src/utils/hounsfieldRange.ts new file mode 100644 index 000000000..21ce01e13 --- /dev/null +++ b/src/utils/hounsfieldRange.ts @@ -0,0 +1,10 @@ +import { DEFAULT_PRESET } from '@/src/config'; +import { hasVisibleOpacity } from '@/src/utils/vtk-helpers'; + +// Air is -1000 HU. Halfway to fat (-100) survives smoothing and resampling, +// while magnitude MR and normalized intensities stay above it. +const AIR_LEVEL = -500; + +/** Whether an untagged scalar range reads as CT in Hounsfield units. */ +export const looksLikeHounsfieldRange = (range: readonly [number, number]) => + range[0] <= AIR_LEVEL && hasVisibleOpacity(DEFAULT_PRESET, range); diff --git a/src/utils/robustScalarRange.ts b/src/utils/robustScalarRange.ts new file mode 100644 index 000000000..7bb307e9c --- /dev/null +++ b/src/utils/robustScalarRange.ts @@ -0,0 +1,40 @@ +const SAMPLE_BUDGET = 2 ** 17; +const TAIL_PERCENT = 1; + +function sampleValues(values: ArrayLike) { + const count = Math.min(values.length, SAMPLE_BUDGET); + return Float64Array.from({ length: count }, (_, index) => { + // Jittered strata keep an integer stride from aliasing with the voxel grid. + let hash = Math.imul(index + 1, 0x9e3779b1); + hash = Math.imul(hash ^ (hash >>> 16), 0x85ebca6b); + const offset = ((hash ^ (hash >>> 13)) >>> 0) / 0x100000000; + return values[Math.floor(((index + offset) * values.length) / count)]; + }); +} + +/** The finite values of a deterministic sample, sorted ascending. */ +export const sortedSample = (values: ArrayLike) => + sampleValues(values).filter(Number.isFinite).sort(); + +/** + * Percentile bounds of the sampled intensities above the minimum, which is + * treated as background. Falls back to `range` when no sample is above it. + */ +export function robustRangeOfSorted( + sorted: Float64Array, + range: readonly [number, number] +): [number, number] { + const start = sorted.findIndex((value) => value > sorted[0]); + if (start < 0) return [range[0], range[1]]; + const foreground = sorted.subarray(start); + const tail = (TAIL_PERCENT / 100) * (foreground.length - 1); + const low = foreground[Math.floor(tail)]; + const high = foreground[Math.ceil(foreground.length - 1 - tail)]; + // A single foreground level, as in a binary mask, ramps up from background. + return low < high ? [low, high] : [sorted[0], high]; +} + +export const robustScalarRange = ( + values: ArrayLike, + range: readonly [number, number] +) => robustRangeOfSorted(sortedSample(values), range); diff --git a/src/utils/vtk-helpers.ts b/src/utils/vtk-helpers.ts index b67e0e2ab..8ba98e675 100644 --- a/src/utils/vtk-helpers.ts +++ b/src/utils/vtk-helpers.ts @@ -14,6 +14,11 @@ import { import vtkFieldData from '@kitware/vtk.js/Common/DataModel/DataSetAttributes/FieldData'; import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; import { Maybe } from '@/src/types'; +import type { vtkDataArray } from '@kitware/vtk.js/Common/Core/DataArray'; +import { + robustRangeOfSorted, + sortedSample, +} from '@/src/utils/robustScalarRange'; export function computeWorldToDisplay( xyz: Vector3, @@ -142,17 +147,26 @@ export function getShiftedOpacityFromPreset( const [xmin, xmax] = effectiveRange; const width = xmax - xmin; + // Relative presets already place their points on the unit interval. + const toUnit = getColorFunctionRangeFromPreset(presetName) + ? (x: number) => (x - xmin) / width + : (x: number) => x; return points.map(([x, y]) => { // Non-zero values should be affected by shift // but preset values of zero should not const shifted = y && y - shiftAlpha; const yVal = Math.max(Math.min(shifted, 1), 0); - return [(x - xmin) / width + shift, yVal] as Vector2; + return [toUnit(x) + shift, yVal] as Vector2; }); } return null; } +// Zero at the bottom of the range, then fully opaque at and above the top. +export const RELATIVE_PRESET_GAUSSIANS = [ + { position: 1, height: 1, width: 1, xBias: 0, yBias: 1 }, +]; + type OpacityFunctionWithoutRange = | Omit | Omit @@ -178,9 +192,7 @@ export function getOpacityFunctionFromPreset( return { mode: vtkPiecewiseFunctionProxy.Mode.Gaussians, // deep-copy necessary - gaussians: JSON.parse( - JSON.stringify(vtkPiecewiseFunctionProxy.Defaults.Gaussians) - ), + gaussians: RELATIVE_PRESET_GAUSSIANS.map((gaussian) => ({ ...gaussian })), }; } @@ -224,6 +236,58 @@ export function getColorFunctionRangeFromPreset( return null; } +const scalarSamples = new WeakMap< + vtkDataArray, + { mtime: number; sorted: Float64Array } +>(); + +/** A sorted sample of an image's scalars, reused until the scalars change. */ +export function getScalarSample(image: vtkImageData) { + const scalars = image.getPointData().getScalars(); + const mtime = scalars.getMTime(); + const cached = scalarSamples.get(scalars); + if (cached?.mtime === mtime) return cached.sorted; + const sorted = sortedSample(scalars.getData()); + scalarSamples.set(scalars, { mtime, sorted }); + return sorted; +} + +/** The scalar range that a preset without an absolute range maps onto. */ +export function getRelativePresetRange(image: vtkImageData) { + const scalars = image.getPointData().getScalars(); + const range = scalars.getRange(); + return scalars.getNumberOfComponents() === 1 + ? robustRangeOfSorted(getScalarSample(image), range) + : range; +} + +/** Whether a preset's opacity points are nonzero anywhere in a scalar range. */ +export function hasVisibleOpacity( + presetName: string, + [low, high]: readonly number[] +) { + const points = getPresetByName(presetName)?.OpacityPoints; + // Relative presets fit any range. + if (!points || !getColorFunctionRangeFromPreset(presetName)) return true; + const pairs = Array.from( + { length: points.length / 2 }, + (_, index) => [points[2 * index], points[2 * index + 1]] as const + ); + // Piecewise linear and clamped, so the extremes lie at the ends or at points. + const opacityAt = (value: number) => { + const next = pairs.findIndex(([x]) => x >= value); + if (next < 0) return pairs[pairs.length - 1][1]; + if (next === 0) return pairs[0][1]; + const [x0, y0] = pairs[next - 1]; + const [x1, y1] = pairs[next]; + return y0 + ((y1 - y0) * (value - x0)) / (x1 - x0); + }; + return ( + [low, high].some((value) => opacityAt(value) > 0) || + pairs.some(([x, y]) => x >= low && x <= high && y > 0) + ); +} + /** * Retrieves the effective opacity mapping range, if any. * diff --git a/src/vtk/ColorMaps.ts b/src/vtk/ColorMaps.ts index c46777464..9f339f035 100644 --- a/src/vtk/ColorMaps.ts +++ b/src/vtk/ColorMaps.ts @@ -3,8 +3,29 @@ import { ColorMapPreset } from '@/src/types/color-map-preset'; import MedicalPresets from './MedicalColorPresets.json'; import DicomPresets from './DicomColorPresets.json'; +// MR intensities have no standard scale, so MR presets fit each image's range. +function toRelativePreset(preset: ColorMapPreset): ColorMapPreset { + const rgb = preset.RGBPoints ?? []; + const xs = rgb.filter((_, index) => index % 4 === 0); + const low = Math.min(...xs); + const span = Math.max(...xs) - low; + const toUnit = (stride: number) => (value: number, index: number) => + index % stride === 0 ? (value - low) / span : value; + return { + ...preset, + AbsoluteRange: false, + EffectiveRange: undefined, + RGBPoints: rgb.map(toUnit(4)), + OpacityPoints: preset.OpacityPoints?.map(toUnit(2)), + }; +} + MedicalPresets.forEach((preset) => - vtkColorMaps.addPreset(preset as ColorMapPreset) + vtkColorMaps.addPreset( + preset.Name.startsWith('MR-') + ? toRelativePreset(preset as ColorMapPreset) + : (preset as ColorMapPreset) + ) ); vtkColorMaps.addPreset({ ...vtkColorMaps.getPresetByName('2hot'), @@ -66,6 +87,12 @@ const GroupedPresets = [ 'US-Fetal', ], }, + { + group: 'Generic', + presets: [ + 'Grayscale', + ], + }, ]; export const PresetNameList = GroupedPresets.flatMap((group) => group.presets); diff --git a/tests/baseline/prostate_sample_views-chrome-1.png b/tests/baseline/prostate_sample_views-chrome-1.png index d873d5ce6..d85c01f26 100644 Binary files a/tests/baseline/prostate_sample_views-chrome-1.png and b/tests/baseline/prostate_sample_views-chrome-1.png differ diff --git a/tests/specs/volume-modality-presets.e2e.ts b/tests/specs/volume-modality-presets.e2e.ts index 6f10c0444..c3e2d0985 100644 --- a/tests/specs/volume-modality-presets.e2e.ts +++ b/tests/specs/volume-modality-presets.e2e.ts @@ -3,6 +3,7 @@ import path from 'node:path'; import { volViewPage } from '../pageobjects/volview.page'; import { buildSyntheticDicom, newUid } from './syntheticDicom'; import { + captureViewPixels, makeTempDir, waitForModuleTransitions, writeManifestToFile, @@ -21,12 +22,13 @@ const VOXELS = Buffer.from( Array.from({ length: 512 }, (_, index) => index % 256) ); -const nrrd = (fields: string[]) => +const nrrd = (fields: string[], type = 'uchar', size = 8) => [ 'NRRD0005', - 'type: uchar', + `type: ${type}`, 'dimension: 3', - 'sizes: 8 8 8', + `sizes: ${size} ${size} ${size}`, + 'endian: little', 'space: left-posterior-superior', 'space directions: (1,0,0) (0,1,0) (0,0,1)', 'space origin: (0,0,0)', @@ -47,10 +49,28 @@ const mha = (fields: string[]) => '', ].join('\n'); -function writeDicomSeries(directory: string, modality: string) { +function floatVoxels(size: number, valueAt: (index: number) => number) { + const buffer = Buffer.alloc(size ** 3 * 4); + for (let index = 0; index < size ** 3; index++) + buffer.writeFloatLE(valueAt(index), index * 4); + return buffer; +} + +function writeNrrd(name: string, header: string, voxels: Buffer) { + const directory = makeTempDir(name); + fs.writeFileSync( + path.join(directory, `${name}.nrrd`), + Buffer.concat([Buffer.from(header), voxels]) + ); + return `tmp/${name}/${name}.nrrd`; +} + +const sliceValue = (index: number) => 100 * (index + 1); + +function writeDicomSeries(directory: string, modality: string, slices = 4) { const studyUid = newUid(); const seriesUid = newUid(); - return Array.from({ length: 4 }, (_, index) => { + return Array.from({ length: slices }, (_, index) => { const name = `slice-${index}.dcm`; fs.writeFileSync( path.join(directory, name), @@ -64,7 +84,7 @@ function writeDicomSeries(directory: string, modality: string) { cols: 8, imageOrientationPatient: [1, 0, 0, 0, 1, 0], imagePositionPatient: [0, 0, index], - pixelValue: 100 * (index + 1), + pixelValue: sliceValue(index), }) ); return { url: `tmp/${path.basename(directory)}/${name}`, name }; @@ -94,7 +114,7 @@ describe('volume preset from declared modality', () => { for (const { file, header, label } of [ { file: 'mr.nrrd', header: nrrd(['modality:=MR']), label: 'MR Default' }, { file: 'us.nrrd', header: nrrd(['0008|0060:=US']), label: 'US Fetal' }, - { file: 'undeclared.nrrd', header: nrrd([]), label: 'CT AAA' }, + { file: 'undeclared.nrrd', header: nrrd([]), label: 'Grayscale' }, { file: 'mr.mha', header: mha(['Modality = MET_MOD_MR']), @@ -124,4 +144,198 @@ describe('volume preset from declared modality', () => { await openWith3DLayout(`tmp/${manifest}`, name); await expectSelectedPreset('MR Default'); }); + + it('guesses CT AAA for a streamed DICOM series of an unlisted modality', async () => { + const name = 'dicom-pt-preset'; + const directory = makeTempDir(name); + const manifest = `${name}.json`; + await writeManifestToFile( + { resources: writeDicomSeries(directory, 'PT') }, + manifest + ); + await openWith3DLayout(`tmp/${manifest}`, name); + await expectVisibleVolume(name); + await expectSelectedPreset('CT AAA'); + }); +}); + +// The inset keeps only the view center, where every test volume has signal. +const CENTER_INSET = 280; + +async function brightCenterPixels(name: string, inset: number, level = 40) { + const [{ data }] = await captureViewPixels( + '[data-testid~="vtk-volume-view"]', + inset, + `${name}.png` + ); + let bright = 0; + for (let index = 0; index < data.length; index += 4) + if (Math.max(data[index], data[index + 1], data[index + 2]) > level) + bright++; + return { bright, fraction: bright / (data.length / 4) }; +} + +/** Fraction of RGBA pixels whose largest channel difference exceeds 8. */ +function differingFraction(a: Buffer, b: Buffer) { + let differing = 0; + for (let index = 0; index < a.length; index += 4) { + const delta = Math.max( + Math.abs(a[index] - b[index]), + Math.abs(a[index + 1] - b[index + 1]), + Math.abs(a[index + 2] - b[index + 2]) + ); + if (delta > 8) differing++; + } + return differing / (a.length / 4); +} + +/** Captures the 3D view until two consecutive frames match. */ +async function stableViewPixels() { + const capture = async () => + ( + await captureViewPixels( + '[data-testid~="vtk-volume-view"]', + 40, + 'stable-view.png' + ) + )[0].data; + let previous = await capture(); + await browser.waitUntil(async () => { + const next = await capture(); + const same = next.equals(previous); + previous = next; + return same; + }); + return previous; +} + +async function expectVisibleVolume(name: string) { + await browser.waitUntil( + async () => (await brightCenterPixels(name, CENTER_INSET)).bright > 50, + { timeoutMsg: `Expected ${name} to render visibly in the 3D view` } + ); +} + +describe('grayscale default fitted to the data', () => { + // Software-rendered 3D frames on CI cost time in proportion to volume depth. + const SIZE = 32; + const coordinates = (index: number) => [ + index % SIZE, + Math.floor(index / SIZE) % SIZE, + Math.floor(index / SIZE ** 2), + ]; + + it('guesses CT AAA for untagged data in Hounsfield units', async () => { + const name = 'fitted-untagged-hu'; + const voxels = floatVoxels(SIZE, (index) => { + const radius = Math.hypot(...coordinates(index).map((c) => c - SIZE / 2)); + if (radius < SIZE / 8) return 700; + return radius < (3 * SIZE) / 8 ? 40 : -1024; + }); + await openWith3DLayout( + writeNrrd(name, nrrd([], 'float', SIZE), voxels), + name + ); + await expectVisibleVolume(name); + await expectSelectedPreset('CT AAA'); + }); + + it('shows a small block on a zero background with a bright outlier', async () => { + const name = 'fitted-sparse'; + const voxels = floatVoxels(SIZE, (index) => { + if (index === 0) return 1e6; + const inBlock = coordinates(index).every( + (c) => c >= SIZE / 2 - 4 && c < SIZE / 2 + 4 + ); + return inBlock ? 500 + (index % 7) : 0; + }); + await openWith3DLayout( + writeNrrd(name, nrrd([], 'float', SIZE), voxels), + name + ); + await expectVisibleVolume(name); + await expectSelectedPreset('Grayscale'); + }); + + it('renders the brightest voxels opaque, so a binary mask face is solid', async () => { + const name = 'fitted-binary'; + const voxels = floatVoxels(SIZE, (index) => + coordinates(index).every((c) => c >= SIZE / 4 && c < (3 * SIZE) / 4) + ? 1 + : 0 + ); + await openWith3DLayout( + writeNrrd(name, nrrd([], 'float', SIZE), voxels), + name + ); + await expectSelectedPreset('Grayscale'); + // A transparent top of range leaves only a speckled shell of interpolated samples. + await browser.waitUntil( + async () => (await brightCenterPixels(name, 320, 150)).fraction > 0.95, + { timeoutMsg: 'Expected the binary mask face to render solid' } + ); + }); + + it('replaces a CT preset that is transparent over normalized data', async () => { + const name = 'fitted-normalized-ct'; + const voxels = floatVoxels(SIZE, (index) => { + const [x, y, z] = coordinates(index).map((c) => c - SIZE / 2); + return Math.max(0, 1 - Math.hypot(x, y, z) / (SIZE / 3)); + }); + await openWith3DLayout( + writeNrrd(name, nrrd(['modality:=CT'], 'float', SIZE), voxels), + name + ); + await expectVisibleVolume(name); + await expectSelectedPreset('Grayscale'); + }); + + it('fits an MR preset to the intensity scale', async () => { + const shell = (scale: number) => (index: number) => { + const radius = Math.hypot(...coordinates(index).map((c) => c - SIZE / 2)); + if (radius < SIZE / 8) return 300 * scale; + return radius < (3 * SIZE) / 8 ? 100 * scale : 0; + }; + const rendered = []; + for (const scale of [1, 10]) { + const name = `fitted-mr-x${scale}`; + await openWith3DLayout( + writeNrrd( + name, + nrrd(['modality:=MR'], 'float', SIZE), + floatVoxels(SIZE, shell(scale)) + ), + name + ); + await expectVisibleVolume(name); + rendered.push(await stableViewPixels()); + } + expect(differingFraction(rendered[0], rendered[1])).toBeLessThan(0.01); + }); + + it('refits a streamed MR series to its loaded intensities', async () => { + const loadedName = 'fitted-mr-nrrd'; + const voxels = Buffer.alloc(2 * 8 ** 3); + for (let index = 0; index < 8 ** 3; index++) + voxels.writeUInt16LE(sliceValue(Math.floor(index / 64)), 2 * index); + await openWith3DLayout( + writeNrrd(loadedName, nrrd(['modality:=MR'], 'ushort'), voxels), + loadedName + ); + await expectVisibleVolume(loadedName); + const loaded = await stableViewPixels(); + + const name = 'fitted-mr-dicom'; + const manifest = `${name}.json`; + await writeManifestToFile( + { resources: writeDicomSeries(makeTempDir(name), 'MR', 8) }, + manifest + ); + await openWith3DLayout(`tmp/${manifest}`, name); + // Ray jitter differs between sources on ~8%; a range left unfit differs on ~45%. + await browser.waitUntil( + async () => differingFraction(loaded, await stableViewPixels()) < 0.2, + { timeoutMsg: 'Expected the streamed series to match once refit' } + ); + }); });