-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathimportSingleFile.ts
More file actions
85 lines (72 loc) · 2.68 KB
/
importSingleFile.ts
File metadata and controls
85 lines (72 loc) · 2.68 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
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData';
import { useFileStore } from '@/src/store/datasets-files';
import { useImageStore } from '@/src/store/datasets-images';
import { useModelStore } from '@/src/store/datasets-models';
import { FILE_READERS } from '@/src/io';
import { ImportHandler } from '@/src/io/import/common';
import { DataSourceWithFile } from '@/src/io/import/dataSource';
import { useDatasetStore } from '@/src/store/datasets';
import { useMessageStore } from '@/src/store/messages';
import { useViewStore } from '@/src/store/views';
import { useViewSliceStore } from '@/src/store/view-configs/slicing';
import { getLPSAxisFromDir } from '@/src/utils/lps';
import { InitViewSpecs } from '@/src/config';
/**
* Reads and imports a file DataSource.
* @param dataSource
* @returns
*/
const importSingleFile: ImportHandler = async (dataSource, { done }) => {
if (!dataSource.fileSrc) {
return dataSource;
}
const { fileSrc } = dataSource;
if (!FILE_READERS.has(fileSrc.fileType)) {
return dataSource;
}
const reader = FILE_READERS.get(fileSrc.fileType)!;
const dataObject = await reader(fileSrc.file);
const fileStore = useFileStore();
if (dataObject.isA('vtkImageData')) {
const dataID = useImageStore().addVTKImageData(
fileSrc.file.name,
dataObject as vtkImageData
);
fileStore.add(dataID, [dataSource as DataSourceWithFile]);
// Create a default view for each viewID
useViewStore().viewIDs.forEach((viewID) => {
const { lpsOrientation, dimensions } = useImageStore().metadata[dataID];
const axisDir = InitViewSpecs[viewID].props.viewDirection
const lpsFromDir = getLPSAxisFromDir(axisDir);
const lpsOrient = lpsOrientation[lpsFromDir];
const dimMax = dimensions[lpsOrient];
useViewSliceStore().updateConfig(viewID, dataID, { axisDirection: axisDir, max: dimMax - 1 });
useViewSliceStore().resetSlice(viewID, dataID);
});
return done({
dataID,
dataSource,
dataType: 'image',
});
}
if (dataObject.isA('vtkPolyData')) {
if (!useDatasetStore().primarySelection) {
useMessageStore().addWarning(
'Load an image to see the mesh. Initializing viewports from mesh files is not implemented.'
);
}
const dataID = useModelStore().addVTKPolyData(
fileSrc.file.name,
dataObject as vtkPolyData
);
fileStore.add(dataID, [dataSource as DataSourceWithFile]);
return done({
dataID,
dataSource,
dataType: 'model',
});
}
throw new Error('Data reader did not produce a valid dataset');
};
export default importSingleFile;