-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathuseWindowingConfigInitializer.ts
161 lines (146 loc) · 5.05 KB
/
useWindowingConfigInitializer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { MaybeRef, computed, unref, watch } from 'vue';
import type { TypedArray } from '@kitware/vtk.js/types';
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import { watchImmediate } from '@vueuse/core';
import { useImage } from '@/src/composables/useCurrentImage';
import { useWindowingConfig } from '@/src/composables/useWindowingConfig';
import { WLAutoRanges, WL_AUTO_DEFAULT, WL_HIST_BINS } from '@/src/constants';
import { getWindowLevels, useDICOMStore } from '@/src/store/datasets-dicom';
import useWindowingStore from '@/src/store/view-configs/windowing';
import { Maybe } from '@/src/types';
import { useResetViewsEvents } from '@/src/components/tools/ResetViews.vue';
import { isDicomImage } from '@/src/utils/dataSelection';
function useAutoRangeValues(imageID: MaybeRef<Maybe<string>>) {
const { imageData } = useImage(imageID);
const histogram = (
data: number[] | TypedArray,
dataRange: number[],
numberOfBins: number
) => {
const [min, max] = dataRange;
const width = (max - min + 1) / numberOfBins;
const hist = new Array(numberOfBins).fill(0);
data.forEach((value) => hist[Math.floor((value - min) / width)]++);
return hist;
};
const autoRangeValues = computed(() => {
if (!imageData.value) {
return {};
}
// Pre-compute the auto-range values
const scalarData = imageData.value.getPointData().getScalars();
// Assumes all data is one component
const { min, max } = vtkDataArray.fastComputeRange(
scalarData.getData() as number[],
0,
1
);
const hist = histogram(scalarData.getData(), [min, max], WL_HIST_BINS);
const cumm = hist.reduce((acc, val, idx) => {
const prev = idx !== 0 ? acc[idx - 1] : 0;
acc.push(val + prev);
return acc;
}, []);
const width = (max - min + 1) / WL_HIST_BINS;
return Object.fromEntries(
Object.entries(WLAutoRanges).map(([key, value]) => {
const startIdx = cumm.findIndex(
(v: number) => v >= value * 0.01 * scalarData.getData().length
);
const endIdx = cumm.findIndex(
(v: number) => v >= (1 - value * 0.01) * scalarData.getData().length
);
const start = Math.max(min, min + width * startIdx);
const end = Math.min(max, min + width * endIdx + width);
return [key, [start, end]];
})
);
});
return { autoRangeValues };
}
export function useWindowingConfigInitializer(
viewID: MaybeRef<string>,
imageID: MaybeRef<Maybe<string>>
) {
const { imageData } = useImage(imageID);
const dicomStore = useDICOMStore();
const store = useWindowingStore();
const { config: windowConfig } = useWindowingConfig(viewID, imageID);
const { autoRangeValues } = useAutoRangeValues(imageID);
const autoRange = computed<keyof typeof WLAutoRanges>(
() => windowConfig.value?.auto || WL_AUTO_DEFAULT
);
const firstTag = computed(() => {
const id = unref(imageID);
if (id && isDicomImage(id)) {
const volKey = id;
const windowLevels = getWindowLevels(dicomStore.volumeInfo[volKey]);
if (windowLevels.length) {
return windowLevels[0];
}
}
return undefined;
});
watchImmediate(windowConfig, (config) => {
const image = imageData.value;
const imageIdVal = unref(imageID);
const viewIdVal = unref(viewID);
if (config || !image || !imageIdVal) return;
const [min, max] = image.getPointData().getScalars().getRange();
store.updateConfig(viewIdVal, imageIdVal, { min, max });
store.resetWindowLevel(viewIdVal, imageIdVal);
});
watchImmediate(imageData, (image) => {
const imageIdVal = unref(imageID);
const config = unref(windowConfig);
const viewIdVal = unref(viewID);
if (imageIdVal == null || config != null || !image) {
return;
}
const [min, max] = autoRangeValues.value[autoRange.value];
store.updateConfig(viewIdVal, imageIdVal, {
min,
max,
});
const firstTagVal = unref(firstTag);
if (firstTagVal?.width) {
store.updateConfig(viewIdVal, imageIdVal, {
preset: {
width: firstTagVal.width,
level: firstTagVal.level,
},
});
}
const forcedWL = store.runtimeConfigWindowLevel;
if (forcedWL) {
store.updateConfig(viewIdVal, imageIdVal, {
preset: {
...forcedWL,
},
});
}
store.resetWindowLevel(viewIdVal, imageIdVal);
});
watch(autoRange, (percentile) => {
const image = imageData.value;
const imageIdVal = unref(imageID);
const viewIdVal = unref(viewID);
if (imageIdVal == null || windowConfig.value == null || !image) {
return;
}
const range = autoRangeValues.value[percentile];
store.updateConfig(viewIdVal, imageIdVal, {
min: range[0],
max: range[1],
});
store.resetWindowLevel(viewIdVal, imageIdVal);
});
useResetViewsEvents().onClick(() => {
const imageIdVal = unref(imageID);
const viewIdVal = unref(viewID);
if (imageIdVal == null || windowConfig.value == null) {
return;
}
store.resetWindowLevel(viewIdVal, imageIdVal);
});
}