@@ -11,7 +11,7 @@ import {
1111import * as Comlink from 'comlink' ;
1212import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray' ;
1313import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData' ;
14- import { vtkFieldRef } from '@/src/core/vtk/vtkFieldRef ' ;
14+ import { useVtkComputed } from '@/src/core/vtk/useVtkComputed ' ;
1515import { WLAutoRanges , WL_HIST_BINS } from '@/src/constants' ;
1616import { HistogramWorker } from '@/src/utils/histogram.worker' ;
1717import { Maybe } from '@/src/types' ;
@@ -26,9 +26,23 @@ export type ImageStats = {
2626 autoRangeValues ?: Record < string , [ number , number ] > ;
2727} ;
2828
29- async function computeAutoRangeValues (
30- imageData : vtkImageData
31- ) : Promise < Record < string , [ number , number ] > > {
29+ function getAllComponentRange ( scalars : vtkDataArray ) {
30+ const numberOfComponents = scalars . getNumberOfComponents ( ) ;
31+
32+ // slice off magnitude range if present.
33+ const ranges = scalars . getRanges ( false ) . slice ( 0 , numberOfComponents ) ;
34+
35+ const min = ranges
36+ . map ( ( range ) => range . min )
37+ . reduce ( ( acc , val ) => Math . min ( acc , val ) , Infinity ) ;
38+ const max = ranges
39+ . map ( ( range ) => range . max )
40+ . reduce ( ( acc , val ) => Math . max ( acc , val ) , - Infinity ) ;
41+
42+ return { min, max } ;
43+ }
44+
45+ async function computeAutoRangeValues ( imageData : vtkImageData ) {
3246 const scalars = imageData . getPointData ( ) ?. getScalars ( ) ;
3347 if ( ! scalars ) {
3448 return { } ;
@@ -40,8 +54,8 @@ async function computeAutoRangeValues(
4054 } )
4155 ) ;
4256
57+ const { min, max } = getAllComponentRange ( scalars ) ;
4358 const scalarData = scalars . getData ( ) as number [ ] ;
44- const { min, max } = vtkDataArray . fastComputeRange ( scalarData , 0 , 1 ) ;
4559 const hist = await worker . histogram ( scalarData , [ min , max ] , WL_HIST_BINS ) ;
4660 worker [ Comlink . releaseProxy ] ( ) ;
4761
@@ -115,7 +129,9 @@ export const useImageStatsStore = defineStore('image-stats', () => {
115129 const activeScalars = computed ( ( ) =>
116130 imageData . value ?. getPointData ( ) ?. getScalars ( )
117131 ) ;
118- const scalarRange = vtkFieldRef ( activeScalars , 'range' ) ;
132+ const scalarRange = useVtkComputed ( activeScalars , ( ) =>
133+ activeScalars . value ?. getRange ( 0 )
134+ ) ;
119135
120136 watch (
121137 scalarRange ,
0 commit comments