Skip to content

Commit ff8d9d6

Browse files
authored
Merge pull request #641 from Kitware/seg-fix
DICOM Seg fixes
2 parents 40ff400 + 373c21b commit ff8d9d6

4 files changed

Lines changed: 24 additions & 16 deletions

File tree

src/composables/untilLoaded.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { computed, unref } from 'vue';
2+
import { until } from '@vueuse/core';
3+
import useChunkStore from '../store/chunks';
4+
5+
export function untilLoaded(imageID: string) {
6+
const doneLoading = computed(
7+
() => !unref(useChunkStore().chunkImageById[imageID].isLoading)
8+
);
9+
return until(doneLoading).toBe(true);
10+
}

src/store/datasets-layers.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import { computed, ref, unref } from 'vue';
2-
import { until } from '@vueuse/core';
1+
import { ref } from 'vue';
32
import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData';
43
import vtkBoundingBox from '@kitware/vtk.js/Common/DataModel/BoundingBox';
54
import { defineStore } from 'pinia';
@@ -8,7 +7,7 @@ import { Maybe } from '@/src/types';
87
import { ensureSameSpace } from '@/src/io/resample/resample';
98
import { useErrorMessage } from '../composables/useErrorMessage';
109
import { Manifest, StateFile } from '../io/state-file/schema';
11-
import useChunkStore from './chunks';
10+
import { untilLoaded } from '../composables/untilLoaded';
1211

1312
// differ from Image/Volume IDs with a branded type
1413
export type LayerID = string & { __type: 'LayerID' };
@@ -39,6 +38,9 @@ export const useLayersStore = defineStore('layer', () => {
3938
{ selection: source, id } as Layer,
4039
];
4140

41+
// ensureSameSpace need final image array to resample, so wait for all chunks
42+
await untilLoaded(source);
43+
4244
const [parentImage, sourceImage] = await Promise.all(
4345
[parent, source].map(getImage)
4446
);
@@ -68,12 +70,6 @@ export const useLayersStore = defineStore('layer', () => {
6870
parent: DataSelection,
6971
source: DataSelection
7072
) {
71-
// ensureSameSpace need final image array to resample, so wait for all chunks
72-
const doneLoading = computed(
73-
() => !unref(useChunkStore().chunkImageById[source].isLoading)
74-
);
75-
await until(doneLoading).toBe(true);
76-
7773
return useErrorMessage('Failed to build layer', async () => {
7874
try {
7975
await this._addLayer(parent, source);

src/store/segmentGroups.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import { FileEntry } from '../io/types';
2727
import { ensureSameSpace } from '../io/resample/resample';
2828
import { useDICOMStore } from './datasets-dicom';
29+
import { untilLoaded } from '../composables/untilLoaded';
2930

3031
const LabelmapArrayType = Uint8Array;
3132
export type LabelmapArrayType = Uint8Array;
@@ -248,7 +249,8 @@ export const useSegmentGroupStore = defineStore('segmentGroup', () => {
248249
if (imageID === parentID)
249250
throw new Error('Cannot convert an image to be a labelmap of itself');
250251

251-
// Build vtkImageData for DICOMs
252+
await untilLoaded(imageID);
253+
252254
const [childImage, parentImage] = await Promise.all(
253255
[imageID, parentID].map(getImage)
254256
);

src/utils/allocateImageFromChunks.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,15 @@ export function allocateImageFromChunks(sortedChunks: Chunk[]) {
117117

118118
image.setSpacing([1, 1, 1]);
119119
if (slices > 1 && imagePositionPatient && pixelSpacing) {
120-
const secondMeta = new Map(sortedChunks[1].metadata);
121-
const secondIPP = toVec(secondMeta.get(ImagePositionPatientTag));
122-
if (secondIPP) {
123-
const spacing = [...pixelSpacing, 1];
120+
const lastMeta = new Map(sortedChunks[sortedChunks.length - 1].metadata);
121+
const lastIPP = toVec(lastMeta.get(ImagePositionPatientTag));
122+
if (lastIPP) {
124123
// assumption: uniform Z spacing
125124
const zVec = vec3.create();
126125
const firstIPP = imagePositionPatient;
127-
vec3.sub(zVec, secondIPP as vec3, firstIPP as vec3);
128-
spacing[2] = vec3.len(zVec) || 1;
126+
vec3.sub(zVec, lastIPP as vec3, firstIPP as vec3);
127+
const zSpacing = vec3.len(zVec) / (sortedChunks.length - 1) || 1;
128+
const spacing = [...pixelSpacing, zSpacing];
129129
image.setSpacing(spacing);
130130
}
131131
}

0 commit comments

Comments
 (0)