-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathimage-cache.ts
More file actions
149 lines (130 loc) · 4.26 KB
/
image-cache.ts
File metadata and controls
149 lines (130 loc) · 4.26 KB
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
import {
LoadedVtkImage,
ProgressiveImage,
ProgressiveImageStatus,
} from '@/src/core/progressiveImage';
import { useIdStore } from '@/src/store/id';
import { useMessageStore } from '@/src/store/messages';
import { Maybe } from '@/src/types';
import { ImageMetadata } from '@/src/types/image';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import { defineStore } from 'pinia';
import { markRaw, reactive, ref } from 'vue';
/**
* An internal cache of progressively loadable images.
*/
export const useImageCacheStore = defineStore('image-cache', () => {
const idStore = useIdStore();
const imageIds = ref<string[]>([]);
const imageById = reactive<Record<string, ProgressiveImage>>({});
const imageStatus = reactive<Record<string, 'complete' | 'incomplete'>>({});
const imageLoading = reactive<Record<string, boolean>>({});
const imageErrors = reactive<Record<string, Error[]>>({});
const imageListenerCleanup: Record<string, () => void> = {};
function getVtkImageData(id: Maybe<string>): Maybe<vtkImageData> {
if (!id) return null;
const image = imageById[id];
if (!image) return null;
const data = image.getVtkImageData();
// ProgressiveImage initializes with empty vtkImageData before actual data loads.
// VTK.js volume renderer crashes on empty data (null scalar texture).
if (!data.getPointData().getScalars()?.getData()?.length) return null;
return data;
}
function getImageMetadata(id: Maybe<string>): Maybe<ImageMetadata> {
if (!id) return null;
return imageById[id]?.getImageMetadata() ?? null;
}
function registerListeners(id: string) {
const data = imageById[id];
const onStatus = (status: ProgressiveImageStatus) => {
imageStatus[id] = status;
};
const onLoading = (loading: boolean) => {
imageLoading[id] = loading;
};
const onError = (error: Error) => {
imageErrors[id] ??= [];
imageErrors[id].push(error);
const messageStore = useMessageStore();
messageStore.addError('Error loading DICOM data', { error });
};
imageListenerCleanup[id] = () => {
data.removeEventListener('status', onStatus);
data.removeEventListener('loading', onLoading);
data.removeEventListener('error', onError);
};
data.addEventListener('status', onStatus);
data.addEventListener('loading', onLoading);
data.addEventListener('error', onError);
}
function unregisterListeners(id: string) {
imageListenerCleanup[id]?.();
delete imageListenerCleanup[id];
}
/**
* Adds a progressive image.
*
* If an ID is provided and the ID already exists,
* the image is assumed to be the same.
* @param data
* @param options
* @returns
*/
function addProgressiveImage(
data: ProgressiveImage,
options: { id?: string } = {}
): string {
const id = options.id ?? idStore.nextId();
if (id in imageById) return id;
imageById[id] = markRaw(data);
imageStatus[id] = data.getStatus();
imageLoading[id] = data.isLoading();
imageIds.value.push(id);
registerListeners(id);
data.startLoad();
return id;
}
function addVTKImageData(
imageData: vtkImageData,
name: string,
options: { id?: string } = {}
) {
return addProgressiveImage(new LoadedVtkImage(imageData, name), options);
}
function removeImage(id: string) {
if (!(id in imageById)) return;
unregisterListeners(id);
const idx = imageIds.value.indexOf(id);
if (idx > -1) imageIds.value.splice(idx, 1);
delete imageById[id];
delete imageStatus[id];
delete imageLoading[id];
}
/**
* Updates an existing image's VTK data while maintaining the same ID.
*/
function updateVTKImageData(id: string, newImageData: vtkImageData): void {
const progressiveImage = imageById[id];
const oldImageData = progressiveImage.vtkImageData.value;
progressiveImage.vtkImageData.value = newImageData;
// trigger texture update
newImageData.modified();
if (oldImageData && oldImageData !== newImageData) {
oldImageData.delete();
}
}
return {
imageIds,
imageById,
imageStatus,
imageLoading,
imageErrors,
getVtkImageData,
getImageMetadata,
addProgressiveImage,
addVTKImageData,
updateVTKImageData,
removeImage,
};
});