-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathdatasets.ts
More file actions
189 lines (155 loc) · 5.11 KB
/
datasets.ts
File metadata and controls
189 lines (155 loc) · 5.11 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import { vtkObject } from '@kitware/vtk.js/interfaces';
import { defineStore } from 'pinia';
import { computed, ref } from 'vue';
import { useDICOMStore } from './datasets-dicom';
import { useImageStore } from './datasets-images';
import { useModelStore } from './datasets-models';
import { useFileStore } from './datasets-files';
import { StateFile } from '../io/state-file/schema';
import { useErrorMessage } from '../composables/useErrorMessage';
export const DataType = {
Image: 'Image',
Model: 'Model',
};
export const makeDICOMSelection = (volumeKey: string) =>
({
type: 'dicom',
volumeKey,
} as const);
export type DICOMSelection = ReturnType<typeof makeDICOMSelection>;
export const makeImageSelection = (imageID: string) =>
({
type: 'image',
dataID: imageID,
} as const);
export type ImageSelection = ReturnType<typeof makeImageSelection>;
export type DataSelection = DICOMSelection | ImageSelection;
export const getImageID = (selection: DataSelection) => {
if (selection.type === 'image') return selection.dataID;
if (selection.type === 'dicom') {
// possibly undefined because image may not have been made yet
return useDICOMStore().volumeToImageID[selection.volumeKey];
}
const _exhaustiveCheck: never = selection;
throw new Error(`invalid selection type ${_exhaustiveCheck}`);
};
export const getImage = async (selection: DataSelection) => {
const images = useImageStore();
const dicoms = useDICOMStore();
if (selection.type === 'dicom') {
// ensure image data exists
await useErrorMessage('Failed to build volume', () =>
dicoms.buildVolume(selection.volumeKey)
);
return images.dataIndex[getImageID(selection)!];
}
// just an image that should be in dataIndex
return images.dataIndex[selection.dataID];
};
// Converts imageID to VolumeKey if exists, else return input imageID
export const getDataID = (imageID: string) => {
const dicomStore = useDICOMStore();
return dicomStore.imageIDToVolumeKey[imageID] ?? imageID;
};
// Pass VolumeKey or ImageID and get ImageID
export const findImageID = (dataID: string) => {
const dicomStore = useDICOMStore();
return dicomStore.volumeToImageID[dataID] ?? dataID;
};
export function selectionEquals(s1: DataSelection, s2: DataSelection) {
if (s1.type === 'dicom' && s2.type === 'dicom') {
return s1.volumeKey === s2.volumeKey;
}
if (s1.type === 'image' && s2.type === 'image') {
return s1.dataID === s2.dataID;
}
return false;
}
export const useDatasetStore = defineStore('dataset', () => {
type _This = ReturnType<typeof useDatasetStore>;
const imageStore = useImageStore();
const modelStore = useModelStore();
const dicomStore = useDICOMStore();
const fileStore = useFileStore();
// --- state --- //
const primarySelection = ref<DataSelection | null>(null);
// --- getters --- //
const primaryImageID = computed(() => {
if (primarySelection.value) return getImageID(primarySelection.value);
return undefined;
});
const primaryDataset = computed<vtkImageData | null>(() => {
const { dataIndex } = imageStore;
return (primaryImageID.value && dataIndex[primaryImageID.value]) || null;
});
const allDataIDs = computed(() => {
return [...imageStore.idList, ...modelStore.idList];
});
function getDataProxyByID<T extends vtkObject>(this: _This, id: string) {
return this.$proxies.getData<T>(id);
}
// --- actions --- //
function setPrimarySelection(sel: DataSelection | null) {
primarySelection.value = sel;
if (sel === null) {
return;
}
// if selection is dicom, call buildVolume
if (sel.type === 'dicom') {
useErrorMessage('Failed to build volume', () =>
dicomStore.buildVolume(sel.volumeKey)
);
}
}
async function serialize(stateFile: StateFile) {
await dicomStore.serialize(stateFile);
await imageStore.serialize(stateFile);
if (primarySelection.value !== null) {
let id = null;
if (primarySelection.value.type === 'dicom') {
id = primarySelection.value.volumeKey;
} else {
id = primarySelection.value.dataID;
}
const { manifest } = stateFile;
manifest.primarySelection = id;
}
}
// --- watch for deletion --- //
imageStore.$onAction(({ name, args, after }) => {
if (name !== 'deleteData') {
return;
}
after(() => {
const [id] = args;
const sel = primarySelection.value;
if (sel?.type === 'image' && sel.dataID === id) {
primarySelection.value = null;
}
// remove file store entry
fileStore.remove(id);
});
});
dicomStore.$onAction(({ name, args, after }) => {
if (name !== 'deleteVolume') {
return;
}
after(() => {
const [volumeKey] = args;
const sel = primarySelection.value;
if (sel?.type === 'dicom' && volumeKey === sel.volumeKey) {
primarySelection.value = null;
}
fileStore.remove(volumeKey);
});
});
return {
primarySelection,
primaryDataset,
allDataIDs,
getDataProxyByID,
setPrimarySelection,
serialize,
};
});