Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing labelmap conversions #862

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 32 additions & 8 deletions packages/tools/examples/PolySegWasmLabelmapToSurface/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
setTitleAndDescription,
addButtonToToolbar,
} from '../../../../utils/demo/helpers';
import assetsURL from '../../../../utils/assets/assetsURL.json';

import * as cornerstoneTools from '@cornerstonejs/tools';

Expand All @@ -33,15 +32,40 @@ addButtonToToolbar({
return;
}

const labelmap = await fetch(assetsURL.Labelmap).then((res) => res.json());

const int32Array = new Int32Array(labelmap.data);
const labelMapHeader = await fetch('/labelMap.json').then((res) =>
res.json()
);
const labelMap = await fetch('/labelMap.bin').then((res) =>
res.arrayBuffer()
);
const values = new Uint8Array(labelMap);
const int32Array = new Int32Array(values.length);
for (let i = 0; i < values.length; i++) {
int32Array[i] = values[i];
}
// we need to convert the labelMapHeader attributes from objects to arrays
const result = instance.convertLabelmapToSurface(
int32Array,
labelmap.dimensions,
labelmap.spacing,
labelmap.direction,
labelmap.origin,
[
labelMapHeader.dimensions[0],
labelMapHeader.dimensions[1],
labelMapHeader.dimensions[2],
],
[
labelMapHeader.spacing[0],
labelMapHeader.spacing[1],
labelMapHeader.spacing[2],
],
[
labelMapHeader.direction[0],
labelMapHeader.direction[1],
labelMapHeader.direction[2],
],
[
labelMapHeader.origin[0],
labelMapHeader.origin[1],
labelMapHeader.origin[2],
],
[1]
);

Expand Down
46 changes: 37 additions & 9 deletions packages/tools/examples/PolySegWasmSurfaceToLabelmap/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,30 @@ console.warn(

let instance = undefined;

function saveBinaryData(data, name) {
function downloadBlob(data, fileName, mimeType) {
const blob = new Blob([data], {
type: mimeType,
});
const url = window.URL.createObjectURL(blob);
downloadURL(url, fileName);
setTimeout(function () {
return window.URL.revokeObjectURL(url);
}, 1000);
}

function downloadURL(data, fileName) {
const a = document.createElement('a');
a.href = data;
a.download = fileName;
document.body.appendChild(a);
a.style = 'display: none';
a.click();
a.remove();
}

downloadBlob(data, name, 'application/octet-stream');
}
function downloadObjectAsJson(exportObj, exportName) {
const dataStr =
'data:text/json;charset=utf-8,' +
Expand All @@ -52,19 +76,23 @@ addButtonToToolbar({

const pointsWasm = new Float32Array(points);
const polysArrayWasm = new Float32Array(polys);
const direction = [0, 0, 0];
const spacing = [1, 1, 1];
const origin = [0, 0, 0];
const dimensions = [1, 1, 1];
const inputDirection = [0, 0, 0];
const inputSpacing = [1, 1, 1];
const inputOrigin = [0, 0, 0];
const inputDimensions = [1, 1, 1];
const result = instance.convertSurfaceToLabelmap(
pointsWasm,
polysArrayWasm,
dimensions,
spacing,
direction,
origin
inputDimensions,
inputSpacing,
inputDirection,
inputOrigin
);
downloadObjectAsJson(result, 'labelMap');
const { data, dimensions, spacing, direction, origin } = result;

const header = { dimensions, spacing, direction, origin };
downloadObjectAsJson(header, 'labelMap');
saveBinaryData(data, 'labelMap.bin');
},
});

Expand Down
Binary file added packages/tools/examples/public/labelMap.bin
Binary file not shown.
24 changes: 24 additions & 0 deletions packages/tools/examples/public/labelMap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"dimensions": { "0": 177, "1": 269, "2": 338 },
"spacing": {
"0": 0.762517511844635,
"1": 0.762517511844635,
"2": 0.762517511844635
},
"direction": {
"0": 1,
"1": 0,
"2": 0,
"3": 0,
"4": 1,
"5": 0,
"6": 0,
"7": 0,
"8": 1
},
"origin": {
"0": -4.884422302246094,
"1": -146.81439208984375,
"2": -247.54029846191406
}
}