From c657db2f4c581ba7f213f2383302c7c236cd7415 Mon Sep 17 00:00:00 2001 From: Paul Elliott Date: Tue, 24 Nov 2026 16:59:16 -0500 Subject: [PATCH] fix(dicom): fall back to SliceThickness for unpositioned frame spacing Enhanced OCT exports often declare only PixelSpacing and SliceThickness in the shared Pixel Measures, with no frame positions or SpacingBetweenSlices. Their frames were spaced 1 mm apart, stretching the en face and 3D views. SpacingBetweenSlices still wins when present, and the unsound-volume warning still reports that frame geometry could not be established. --- .../core/collections/multiframeGeometry.ts | 2 +- .../volume/__tests__/frameGeometry.spec.ts | 6 ++-- .../volume/__tests__/volumeLayout.spec.ts | 31 +++++++++++++++++++ src/dicom/core/volume/volumeLayout.ts | 13 ++++++-- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/src/dicom/core/collections/multiframeGeometry.ts b/src/dicom/core/collections/multiframeGeometry.ts index 94f46fd03..0bc2c7ea9 100644 --- a/src/dicom/core/collections/multiframeGeometry.ts +++ b/src/dicom/core/collections/multiframeGeometry.ts @@ -41,7 +41,7 @@ const singleSpatialStack = (groups: DicomDataset[]) => * Patient-space frames keep their stored pixel order. The third direction * follows the positions, which can run opposite the row/column cross product. * Ophthalmic scan-line coordinates require a separate calibrated localizer; - * optical SliceThickness cannot substitute for their separation. + * SliceThickness cannot place frames, so it is only a spacing fallback. */ export function multiframeGeometry(metadata: DicomDataset, count: number) { const groups = perFrameGroups(metadata); diff --git a/src/dicom/core/volume/__tests__/frameGeometry.spec.ts b/src/dicom/core/volume/__tests__/frameGeometry.spec.ts index dd27ba4ec..94fcaffc3 100644 --- a/src/dicom/core/volume/__tests__/frameGeometry.spec.ts +++ b/src/dicom/core/volume/__tests__/frameGeometry.spec.ts @@ -185,7 +185,7 @@ describe('enhanced volume Functional Groups', () => { }); }); - it('reads OCT in-plane calibration without using optical depth of field as frame separation', () => { + it('separates unpositioned OCT frames by the shared SliceThickness', () => { const metadata = enhanced( groups([ sequence('00289110', [ @@ -197,7 +197,9 @@ describe('enhanced volume Functional Groups', () => { ]), [groups([]), groups([])] ); - expect(allocate(metadata).spacing).toEqual([0.01174168, 0.001955034, 1]); + expect(allocate(metadata).spacing).toEqual([ + 0.01174168, 0.001955034, 0.002, + ]); }); it('reports shared-plane temporal frames as irregular', () => { diff --git a/src/dicom/core/volume/__tests__/volumeLayout.spec.ts b/src/dicom/core/volume/__tests__/volumeLayout.spec.ts index 0bd76656f..73db25680 100644 --- a/src/dicom/core/volume/__tests__/volumeLayout.spec.ts +++ b/src/dicom/core/volume/__tests__/volumeLayout.spec.ts @@ -173,4 +173,35 @@ describe('volumeLayout', () => { expect(spacingOf(layout)[2]).toBe(2.5); }); + + it('falls back to SliceThickness when nothing else spaces the frames', () => { + const layout = layoutOf( + [ + volumeSource({ + [Tags.NumberOfFrames]: '32', + [Tags.ImagePositionPatient]: '', + [Tags.SliceThickness]: '0.25', + }), + ], + AXIAL + ); + + expect(spacingOf(layout)[2]).toBe(0.25); + }); + + it('prefers SpacingBetweenSlices over SliceThickness', () => { + const layout = layoutOf( + [ + volumeSource({ + [Tags.NumberOfFrames]: '32', + [Tags.ImagePositionPatient]: '', + [Tags.SpacingBetweenSlices]: '0.5', + [Tags.SliceThickness]: '0.25', + }), + ], + AXIAL + ); + + expect(spacingOf(layout)[2]).toBe(0.5); + }); }); diff --git a/src/dicom/core/volume/volumeLayout.ts b/src/dicom/core/volume/volumeLayout.ts index 4c427c449..b559770f4 100644 --- a/src/dicom/core/volume/volumeLayout.ts +++ b/src/dicom/core/volume/volumeLayout.ts @@ -4,10 +4,10 @@ import { Tags } from '../metadata/dicomTags'; import { readGeometryFacts, sliceNormalOf } from '../collections/instanceFacts'; import type { Reconstruction } from '../collections/reconstructVolume'; import { readDatasetDetails } from '../collections/datasetDetails'; -import { readPixelSpacing, spacingOr1 } from '../metadata/spatialMetadata'; +import { readPixelSpacing } from '../metadata/spatialMetadata'; import { getFrameTag } from '../metadata/functionalGroups'; import { frameCount } from '../pixels/pixelDecoder'; -import { numberOr } from '../utils'; +import { numberOr, trimDicomPadding } from '../utils'; import { nmGeometry } from './nmGeometry'; import { getVolumeBufferType, instancePixelFormats } from './pixelFormat'; import { applyUltrasoundSpacing } from './ultrasoundSpacing'; @@ -68,6 +68,13 @@ function allocationShape(sources: readonly VolumeSource[]) { }; } +// SliceThickness is only a stand-in for the gap: it can differ from it, but +// many enhanced OCT files declare nothing else. +const declaredSliceSpacing = (dataset: DicomDataset) => + [Tags.SpacingBetweenSlices, Tags.SliceThickness] + .map((tag) => Number(trimDicomPadding(getFrameTag(dataset, tag) ?? ''))) + .find(isPositiveFiniteNumber) ?? 1; + function allocationSpacing( dataset: DicomDataset, { reconstruction }: ReturnType @@ -75,7 +82,7 @@ function allocationSpacing( const sliceSpacing = reconstruction.sliceSpacing ?? 0; const z = isPositiveFiniteNumber(sliceSpacing) ? sliceSpacing - : spacingOr1(getFrameTag(dataset, Tags.SpacingBetweenSlices)); + : declaredSliceSpacing(dataset); return [...readPixelSpacing(dataset), z]; }