Skip to content

Commit

Permalink
fix(datasets-dicom): rebuild volume if image deleted
Browse files Browse the repository at this point in the history
And only build volume if not already requested to build.
  • Loading branch information
PaulHax committed Apr 11, 2024
1 parent b089616 commit 3c177ea
Showing 1 changed file with 28 additions and 9 deletions.
37 changes: 28 additions & 9 deletions src/store/datasets-dicom.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import vtkITKHelper from '@kitware/vtk.js/Common/DataModel/ITKHelper';
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
import { defineStore } from 'pinia';
import { Image } from 'itk-wasm';
import { DataSourceWithFile } from '@/src/io/import/dataSource';
Expand Down Expand Up @@ -61,6 +62,9 @@ interface State {
// volume invalidation information
needsRebuild: Record<string, boolean>;

// Promise resolving to vtkImageData
volumeImageData: Record<string, Promise<vtkImageData>>;

// patientKey -> patient info
patientInfo: Record<string, PatientInfo>;
// patientKey -> array of studyKeys
Expand Down Expand Up @@ -122,6 +126,7 @@ export const useDICOMStore = defineStore('dicom', {
sliceData: {},
volumeToImageID: {},
imageIDToVolumeKey: {},
volumeImageData: {},
patientInfo: {},
patientStudies: {},
studyInfo: {},
Expand Down Expand Up @@ -254,6 +259,10 @@ export const useDICOMStore = defineStore('dicom', {
delete this.imageIDToVolumeKey[imageID];
}

if (volumeKey in this.volumeImageData) {
delete this.volumeImageData[volumeKey];
}

removeFromArray(this.studyVolumes[studyKey], volumeKey);
if (this.studyVolumes[studyKey].length === 0) {
this.deleteStudy(studyKey);
Expand Down Expand Up @@ -354,15 +363,17 @@ export const useDICOMStore = defineStore('dicom', {
return this.getVolumeSlice(volumeKey, middleSlice, true);
},

async buildVolume(volumeKey: string, forceRebuild: boolean = false) {
async _buildVolume(volumeKey: string, forceRebuild: boolean = false) {
const imageStore = useImageStore();
const dicomIO = this.$dicomIO;

const rebuild = forceRebuild || this.needsRebuild[volumeKey];

if (!rebuild && this.volumeToImageID[volumeKey]) {
const imageID = this.volumeToImageID[volumeKey]!;
return imageStore.dataIndex[imageID];
const existingImageID = this.volumeToImageID[volumeKey];
const imageExists =
existingImageID && imageStore.dataIndex[existingImageID];
if (!rebuild && imageExists) {
return imageStore.dataIndex[existingImageID];
}

const fileStore = useFileStore();
Expand All @@ -372,21 +383,29 @@ export const useDICOMStore = defineStore('dicom', {
await dicomIO.buildImage(files)
);

const existingImageID = this.volumeToImageID[volumeKey];
if (existingImageID) {
if (imageExists) {
// was a rebuild
imageStore.updateData(existingImageID, image);
} else {
const info = this.volumeInfo[volumeKey];
const name =
cleanupName(info.SeriesDescription) || info.SeriesInstanceUID;
const imageID = imageStore.addVTKImageData(name, image);
this.imageIDToVolumeKey[imageID] = volumeKey;
this.volumeToImageID[volumeKey] = imageID;
const newImageID = imageStore.addVTKImageData(name, image);
this.imageIDToVolumeKey[newImageID] = volumeKey;
this.volumeToImageID[volumeKey] = newImageID;
}

delete this.needsRebuild[volumeKey];

return image;
},

buildVolume(volumeKey: string) {
if (volumeKey in this.volumeImageData) {
return this.volumeImageData[volumeKey];
}
this.volumeImageData[volumeKey] = this._buildVolume(volumeKey);
return this.volumeImageData[volumeKey];
},
},
});

0 comments on commit 3c177ea

Please sign in to comment.