@@ -3,11 +3,16 @@ import vtkPiecewiseFunctionProxy from '@kitware/vtk.js/Proxy/Core/PiecewiseFunct
33import {
44 getColorFunctionRangeFromPreset ,
55 getOpacityFunctionFromPreset ,
6+ getRelativePresetRange ,
7+ hasVisibleOpacity ,
68} from '@/src/utils/vtk-helpers' ;
7- import { DEFAULT_PRESET_BY_MODALITY , DEFAULT_PRESET } from '@/src/config' ;
8- import { ColorTransferFunction } from '@/src/types/views' ;
9+ import {
10+ DEFAULT_GRAYSCALE_PRESET ,
11+ DEFAULT_PRESET_BY_MODALITY ,
12+ DEFAULT_PRESET ,
13+ } from '@/src/config' ;
914import { defineStore } from 'pinia' ;
10- import { reactive } from 'vue' ;
15+ import { getCurrentScope , reactive , watch } from 'vue' ;
1116import {
1217 DoubleKeyRecord ,
1318 deleteSecondKey ,
@@ -18,6 +23,7 @@ import { DeepPartial, Maybe } from '@/src/types';
1823import { identity } from '@/src/utils' ;
1924import { isDicomImage } from '@/src/utils/dataSelection' ;
2025import { getDeclaredModality } from '@/src/utils/declaredModality' ;
26+ import { looksLikeHounsfieldRange } from '@/src/utils/hounsfieldRange' ;
2127import { useImageCacheStore } from '@/src/store/image-cache' ;
2228import { createViewConfigSerializer } from './common' ;
2329import { ViewConfig } from '../../io/state-file/schema' ;
@@ -30,35 +36,34 @@ export const DEFAULT_SPECULAR = 0.3;
3036export const DEFAULT_EDGE_GRADIENT = 0.2 ;
3137export const DEFAULT_SAMPLING_DISTANCE = 0.2 ;
3238
33- function getPresetFromImageModality ( imageID : string ) {
39+ function getDeclaredPreset ( imageID : string ) {
3440 const dicomModality = isDicomImage ( imageID )
3541 ? useDICOMStore ( ) . volumeInfo [ imageID ] ?. Modality
3642 : undefined ;
3743 const modality = getDeclaredModality (
3844 dicomModality ,
3945 useImageCacheStore ( ) . imageById [ imageID ] ?. headerMetadata
4046 ) ;
41- return DEFAULT_PRESET_BY_MODALITY [ modality ] ?? DEFAULT_PRESET ;
47+ return DEFAULT_PRESET_BY_MODALITY [ modality ] ;
4248}
4349
4450/**
45- * Gets partial color and opacity function configs from a preset.
46- * @param preset
47- * @returns
51+ * Checks against the scalar range need every voxel, so a streaming image keeps
52+ * its declared preset, or the CT guess, for good.
4853 */
49- function getColorAndOpacityFuncsFromPreset ( preset : string ) {
50- const ctFunc : Partial < ColorTransferFunction > = {
51- preset ,
52- } ;
53-
54- const ctRange = getColorFunctionRangeFromPreset ( preset ) ;
55- if ( ctRange ) {
56- ctFunc . mappingRange = ctRange ;
57- }
58-
59- const opFunc = getOpacityFunctionFromPreset ( preset ) ;
60-
61- return { colorFunc : ctFunc , opacityFunc : opFunc } ;
54+ function getDefaultPreset ( imageID : string , image : vtkImageData ) {
55+ const declared = getDeclaredPreset ( imageID ) ;
56+ const scalars = image . getPointData ( ) . getScalars ( ) ;
57+ if (
58+ scalars . getNumberOfComponents ( ) !== 1 ||
59+ ! useImageCacheStore ( ) . imageById [ imageID ] ?. isLoaded ( )
60+ )
61+ return declared ?? DEFAULT_PRESET ;
62+ const range = scalars . getRange ( ) ;
63+ const fits = declared
64+ ? hasVisibleOpacity ( declared , range )
65+ : looksLikeHounsfieldRange ( range ) ;
66+ return fits ? ( declared ?? DEFAULT_PRESET ) : DEFAULT_GRAYSCALE_PRESET ;
6267}
6368
6469export const defaultVolumeColorConfig = ( ) : VolumeColorConfig => ( {
@@ -138,42 +143,73 @@ export const useVolumeColoringStore = defineStore('volumeColoring', () => {
138143 const updateOpacityFunction = createUpdateFunc ( 'opacityFunction' ) ;
139144 const updateCVRParameters = createUpdateFunc ( 'cvr' ) ;
140145
146+ const storeScope = getCurrentScope ( ) ;
147+ const pendingRefits = new Map < string , ( ) => void > ( ) ;
148+
149+ // A range fitted to a streaming image's partial voxels is refit once it loads.
150+ const refitWhenLoaded = ( viewID : string , imageID : string , preset : string ) => {
151+ const key = JSON . stringify ( [ viewID , imageID ] ) ;
152+ pendingRefits . get ( key ) ?.( ) ;
153+ const imageCacheStore = useImageCacheStore ( ) ;
154+ const stop = storeScope ! . run ( ( ) =>
155+ watch (
156+ ( ) => imageCacheStore . imageById [ imageID ] ?. isLoaded ( ) ?? true ,
157+ ( settled ) => {
158+ if ( ! settled ) return ;
159+ stop ?.( ) ;
160+ pendingRefits . delete ( key ) ;
161+ if (
162+ imageCacheStore . getVtkImageData ( imageID ) &&
163+ getConfig ( viewID , imageID ) ?. transferFunction . preset === preset
164+ )
165+ setColorPreset ( viewID , imageID , preset ) ;
166+ }
167+ )
168+ ) ;
169+ if ( stop ) pendingRefits . set ( key , stop ) ;
170+ } ;
171+
141172 const setColorPreset = ( viewID : string , imageID : string , preset : string ) => {
142173 const imageCacheStore = useImageCacheStore ( ) ;
143174 const image = imageCacheStore . getVtkImageData ( imageID ) ;
144175 if ( ! image ) throw new Error ( 'Invalid imageID' ) ;
145176
146- const imageDataRange = image . getPointData ( ) . getScalars ( ) . getRange ( ) ;
147- const { colorFunc, opacityFunc } =
148- getColorAndOpacityFuncsFromPreset ( preset ) ;
149- colorFunc . mappingRange ||= imageDataRange ;
150-
151- const completeOpacityFunc = {
152- ...opacityFunc ,
153- mappingRange : imageDataRange ,
154- } ;
177+ const presetRange = getColorFunctionRangeFromPreset ( preset ) ;
178+ // Absolute opacity points stay in scalar units, edited over the data range.
179+ const opacityRange = presetRange
180+ ? image . getPointData ( ) . getScalars ( ) . getRange ( )
181+ : getRelativePresetRange ( image ) ;
155182
156- updateColorTransferFunction ( viewID , imageID , colorFunc ) ;
157- updateOpacityFunction ( viewID , imageID , completeOpacityFunc ) ;
183+ updateColorTransferFunction ( viewID , imageID , {
184+ preset,
185+ mappingRange : presetRange ?? opacityRange ,
186+ } ) ;
187+ updateOpacityFunction ( viewID , imageID , {
188+ ...getOpacityFunctionFromPreset ( preset ) ,
189+ mappingRange : opacityRange ,
190+ } ) ;
191+ if ( ! presetRange && ! imageCacheStore . imageById [ imageID ] ?. isLoaded ( ) )
192+ refitWhenLoaded ( viewID , imageID , preset ) ;
158193 } ;
159194
195+ const getInitialPreset = ( dataID : string , image : vtkImageData ) =>
196+ defaultConfigs [ dataID ] ?. transferFunction ?. preset ??
197+ getDefaultPreset ( dataID , image ) ;
198+
160199 const resetToDefaultColoring = (
161200 viewID : string ,
162201 dataID : string ,
163202 image : vtkImageData
164203 ) => {
165204 const defaults = defaultConfigs [ dataID ] ;
205+ const preset = getInitialPreset ( dataID , image ) ;
166206 const scalars = image . getPointData ( ) . getScalars ( ) ;
167207
168208 updateColorBy ( viewID , dataID , {
169209 arrayName : defaults ?. colorBy ?. arrayName ?? scalars . getName ( ) ,
170210 location : defaults ?. colorBy ?. location ?? 'pointData' ,
171211 } ) ;
172- setColorPreset (
173- viewID ,
174- dataID ,
175- defaults ?. transferFunction ?. preset ?? getPresetFromImageModality ( dataID )
176- ) ;
212+ setColorPreset ( viewID , dataID , preset ) ;
177213 } ;
178214
179215 /**
0 commit comments