Skip to content

Commit 3f0ab31

Browse files
committed
feat: make stack viewport work
1 parent 4b6a761 commit 3f0ab31

File tree

12 files changed

+358
-193
lines changed

12 files changed

+358
-193
lines changed

packages/adapters/examples/segmentationStack/index.ts

+7-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ console.warn(
2323
"Click on index.ts to open source code for this example --------->"
2424
);
2525

26-
const { Enums: csEnums, RenderingEngine, utilities: csUtilities } = cornerstone;
2726
const { segmentation: csToolsSegmentation } = cornerstoneTools;
2827
import {
2928
readDicom,
@@ -32,14 +31,11 @@ import {
3231
loadSegmentation,
3332
exportSegmentation,
3433
restart,
35-
getSegmentationIds,
3634
handleFileSelect,
3735
handleDragOver,
38-
createSegmentation
36+
createEmptySegmentation
3937
} from "../segmentationVolume/utils";
4038

41-
const referenceImageIds: string[] = [];
42-
const segImageIds: string[] = [];
4339
// ======== Set up page ======== //
4440

4541
setTitleAndDescription(
@@ -104,7 +100,7 @@ const state = {
104100
toolGroup: null,
105101
toolGroupId: "MY_TOOL_GROUP_ID",
106102
viewportIds: ["CT_AXIAL"],
107-
segmentationIds: [],
103+
segmentationId: "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4(),
108104
referenceImageIds: [],
109105
segImageIds: [],
110106
skipOverlapping: false,
@@ -135,12 +131,10 @@ function loadDicom() {
135131
}
136132

137133
function createSegmentationRepresentation() {
138-
for (const segmentationId of state.segmentationIds) {
139-
csToolsSegmentation.addLabelmapRepresentationToViewport(
140-
state.viewportIds[0],
141-
[{ segmentationId }]
142-
);
143-
}
134+
csToolsSegmentation.addLabelmapRepresentationToViewport(
135+
state.viewportIds[0],
136+
[{ segmentationId: state.segmentationId }]
137+
);
144138
}
145139

146140
// ============================= //
@@ -196,7 +190,7 @@ addButtonToToolbar({
196190
id: "CREATE_SEGMENTATION",
197191
title: "Create Empty SEG",
198192
onClick: async () => {
199-
await createSegmentation(state);
193+
await createEmptySegmentation(state);
200194
createSegmentationRepresentation();
201195
},
202196
container: group2

packages/adapters/examples/segmentationVolume/index.ts

+10-12
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
handleFileSelect,
3434
handleDragOver,
3535
restart,
36-
createSegmentation
36+
createEmptySegmentation
3737
} from "../segmentationVolume/utils";
3838

3939
setTitleAndDescription(
@@ -116,7 +116,7 @@ const state = {
116116
toolGroupId: "MY_TOOL_GROUP_ID",
117117
viewportIds: ["CT_AXIAL", "CT_SAGITTAL", "CT_CORONAL"],
118118
volumeId: "",
119-
segmentationIds: [],
119+
segmentationId: "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4(),
120120
referenceImageIds: [],
121121
skipOverlapping: false,
122122
segImageIds: [],
@@ -161,15 +161,13 @@ async function loadDicom() {
161161
}
162162

163163
function createSegmentationRepresentation() {
164-
for (const segmentationId of state.segmentationIds) {
165-
const segMap = {
166-
[state.viewportIds[0]]: [{ segmentationId }],
167-
[state.viewportIds[1]]: [{ segmentationId }],
168-
[state.viewportIds[2]]: [{ segmentationId }]
169-
};
170-
171-
csToolsSegmentation.addLabelmapRepresentationToViewportMap(segMap);
172-
}
164+
const segMap = {
165+
[state.viewportIds[0]]: [{ segmentationId: state.segmentationId }],
166+
[state.viewportIds[1]]: [{ segmentationId: state.segmentationId }],
167+
[state.viewportIds[2]]: [{ segmentationId: state.segmentationId }]
168+
};
169+
170+
csToolsSegmentation.addLabelmapRepresentationToViewportMap(segMap);
173171
}
174172
// ============================= //
175173
addButtonToToolbar({
@@ -223,7 +221,7 @@ addButtonToToolbar({
223221
id: "CREATE_SEGMENTATION",
224222
title: "Create Empty SEG",
225223
onClick: async () => {
226-
await createSegmentation(state);
224+
await createEmptySegmentation(state);
227225
createSegmentationRepresentation();
228226
},
229227
container: group2

packages/adapters/examples/segmentationVolume/utils.ts

+43-26
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export async function readDicom(files: FileList, state) {
1818
}
1919
}
2020

21-
export async function createSegmentation(state) {
21+
export async function createEmptySegmentation(state) {
2222
const { referenceImageIds, segmentationId } = state;
2323

2424
const derivedSegmentationImages =
@@ -44,6 +44,25 @@ export async function createSegmentation(state) {
4444
]);
4545
}
4646

47+
export async function createSegmentation({ state, labelMapImages }) {
48+
const { segmentationId } = state;
49+
50+
const imageIds = labelMapImages?.flat().map(image => image.imageId);
51+
52+
csToolsSegmentation.addSegmentations([
53+
{
54+
segmentationId,
55+
representation: {
56+
type: cornerstoneTools.Enums.SegmentationRepresentations
57+
.Labelmap,
58+
data: {
59+
imageIds
60+
}
61+
}
62+
}
63+
]);
64+
}
65+
4766
export async function readSegmentation(file: File, state) {
4867
const imageId = wadouri.fileManager.add(file);
4968
const image = await imageLoader.loadAndCacheImage(imageId);
@@ -65,9 +84,9 @@ export async function readSegmentation(file: File, state) {
6584
}
6685

6786
export async function loadSegmentation(arrayBuffer: ArrayBuffer, state) {
68-
const { referenceImageIds, skipOverlapping, segmentationIds } = state;
87+
const { referenceImageIds, skipOverlapping, segmentationId } = state;
6988

70-
const generateToolState =
89+
const { labelMapImages } =
7190
await Cornerstone3D.Segmentation.createFromDICOMSegBuffer(
7291
referenceImageIds,
7392
arrayBuffer,
@@ -77,29 +96,27 @@ export async function loadSegmentation(arrayBuffer: ArrayBuffer, state) {
7796
}
7897
);
7998

80-
for (let i = 0; i < generateToolState.labelMapImages.length; i++) {
81-
const segmentationId = "LOAD_SEG_ID:" + cornerstone.utilities.uuidv4();
82-
segmentationIds.push(segmentationId);
83-
await createSegmentation({ ...state, segmentationId });
84-
85-
const segmentation =
86-
csToolsSegmentation.state.getSegmentation(segmentationId);
87-
88-
const { imageIds } = segmentation.representationData.Labelmap;
89-
const derivedSegmentationImages = imageIds.map(imageId =>
90-
cache.getImage(imageId)
91-
);
92-
93-
const labelmapImagesNonOverlapping =
94-
generateToolState.labelMapImages[i];
95-
96-
for (let j = 0; j < derivedSegmentationImages.length; j++) {
97-
const voxelManager = derivedSegmentationImages[j].voxelManager;
98-
const scalarData = voxelManager.getScalarData();
99-
scalarData.set(labelmapImagesNonOverlapping[j].getPixelData());
100-
voxelManager.setScalarData(scalarData);
101-
}
102-
}
99+
await createSegmentation({ state, labelMapImages });
100+
// for (let i = 0; i < labelMapImages.length; i++) {
101+
// const segmentation =
102+
// csToolsSegmentation.state.getSegmentation(segmentationId);
103+
104+
// const { imageIds } = segmentation.representationData.Labelmap;
105+
// console.log("🚀 ~ loadSegmentation ~ imageIds:", imageIds);
106+
// const derivedSegmentationImages = imageIds.map(imageId =>
107+
// cache.getImage(imageId)
108+
// );
109+
110+
// const labelmapImagesNonOverlapping = labelMapImages[i];
111+
112+
// for (let j = 0; j < derivedSegmentationImages.length; j++) {
113+
// const voxelManager = derivedSegmentationImages[j].voxelManager;
114+
// const scalarData = voxelManager.getScalarData();
115+
// const derivedImage = labelmapImagesNonOverlapping[j];
116+
// scalarData.set(labelmapImagesNonOverlapping[j].getPixelData());
117+
// voxelManager.setScalarData(scalarData);
118+
// }
119+
// }
103120
}
104121

105122
export async function exportSegmentation(state) {

0 commit comments

Comments
 (0)