@@ -3,9 +3,14 @@ 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' ;
1015import { reactive } from 'vue' ;
1116import {
@@ -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 => ( {
@@ -143,37 +148,40 @@ export const useVolumeColoringStore = defineStore('volumeColoring', () => {
143148 const image = imageCacheStore . getVtkImageData ( imageID ) ;
144149 if ( ! image ) throw new Error ( 'Invalid imageID' ) ;
145150
146- const imageDataRange = image . getPointData ( ) . getScalars ( ) . getRange ( ) ;
147- const { colorFunc, opacityFunc } =
148- getColorAndOpacityFuncsFromPreset ( preset ) ;
149- colorFunc . mappingRange ||= imageDataRange ;
151+ const presetRange = getColorFunctionRangeFromPreset ( preset ) ;
152+ // Absolute opacity points stay in scalar units, edited over the data range.
153+ const opacityRange = presetRange
154+ ? image . getPointData ( ) . getScalars ( ) . getRange ( )
155+ : getRelativePresetRange ( image ) ;
150156
151- const completeOpacityFunc = {
152- ...opacityFunc ,
153- mappingRange : imageDataRange ,
154- } ;
155-
156- updateColorTransferFunction ( viewID , imageID , colorFunc ) ;
157- updateOpacityFunction ( viewID , imageID , completeOpacityFunc ) ;
157+ updateColorTransferFunction ( viewID , imageID , {
158+ preset,
159+ mappingRange : presetRange ?? opacityRange ,
160+ } ) ;
161+ updateOpacityFunction ( viewID , imageID , {
162+ ...getOpacityFunctionFromPreset ( preset ) ,
163+ mappingRange : opacityRange ,
164+ } ) ;
158165 } ;
159166
167+ const getInitialPreset = ( dataID : string , image : vtkImageData ) =>
168+ defaultConfigs [ dataID ] ?. transferFunction ?. preset ??
169+ getDefaultPreset ( dataID , image ) ;
170+
160171 const resetToDefaultColoring = (
161172 viewID : string ,
162173 dataID : string ,
163174 image : vtkImageData
164175 ) => {
165176 const defaults = defaultConfigs [ dataID ] ;
177+ const preset = getInitialPreset ( dataID , image ) ;
166178 const scalars = image . getPointData ( ) . getScalars ( ) ;
167179
168180 updateColorBy ( viewID , dataID , {
169181 arrayName : defaults ?. colorBy ?. arrayName ?? scalars . getName ( ) ,
170182 location : defaults ?. colorBy ?. location ?? 'pointData' ,
171183 } ) ;
172- setColorPreset (
173- viewID ,
174- dataID ,
175- defaults ?. transferFunction ?. preset ?? getPresetFromImageModality ( dataID )
176- ) ;
184+ setColorPreset ( viewID , dataID , preset ) ;
177185 } ;
178186
179187 /**
0 commit comments