import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Volume';
import '@kitware/vtk.js/IO/Core/DataAccessHelper/HtmlDataAccessHelper'; import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper'; import '@kitware/vtk.js/IO/Core/DataAccessHelper/JSZipDataAccessHelper';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; import vtkHttpDataSetReader from '@kitware/vtk.js/IO/Core/HttpDataSetReader'; import vtkVolume from '@kitware/vtk.js/Rendering/Core/Volume'; import vtkVolumeMapper from '@kitware/vtk.js/Rendering/Core/VolumeMapper'; import vtkInteractorStyleMPRSlice from '@kitware/vtk.js/Interaction/Style/InteractorStyleMPRSlice'; import vtkImageData from '@kitware/vtk.js/Common/DataModel/ImageData'; import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; import vtkColorTransferFunction from '@kitware/vtk.js/Rendering/Core/ColorTransferFunction'; import vtkPiecewiseFunction from '@kitware/vtk.js/Common/DataModel/PiecewiseFunction';
const fullScreenRenderWindow = vtkFullScreenRenderWindow.newInstance({ background: [0.3, 0.3, 0.3], }); const renderWindow = fullScreenRenderWindow.getRenderWindow(); const renderer = fullScreenRenderWindow.getRenderer();
const istyle = vtkInteractorStyleMPRSlice.newInstance(); renderWindow.getInteractor().setInteractorStyle(istyle);
global.fullScreen = fullScreenRenderWindow; global.renderWindow = renderWindow;
const actor = vtkVolume.newInstance(); const mapper = vtkVolumeMapper.newInstance(); actor.setMapper(mapper);
const ofun = vtkPiecewiseFunction.newInstance(); ofun.addPoint(0, 0); ofun.addPoint(1, 1.0); actor.getProperty().setScalarOpacity(0, ofun);
function createLabelPipeline(backgroundImageData) { const labelMapData = vtkImageData.newInstance( backgroundImageData.get('spacing', 'origin', 'direction') );
labelMapData.computeTransforms();
const values = new Uint8Array(backgroundImageData.getNumberOfPoints()); const dataArray = vtkDataArray.newInstance({ numberOfComponents: 1, values, }); labelMapData.getPointData().setScalars(dataArray);
labelMapData.setDimensions(...backgroundImageData.getDimensions()); labelMapData.setSpacing(...backgroundImageData.getSpacing()); labelMapData.setOrigin(...backgroundImageData.getOrigin()); labelMapData.setDirection(...backgroundImageData.getDirection());
const labelMap = { actor: vtkVolume.newInstance(), mapper: vtkVolumeMapper.newInstance(), imageData: labelMapData, cfun: vtkColorTransferFunction.newInstance(), ofun: vtkPiecewiseFunction.newInstance(), };
labelMap.mapper.setInputData(labelMapData); labelMap.actor.setMapper(labelMap.mapper);
labelMap.cfun.addRGBPoint(1, 1, 0, 0); labelMap.cfun.addRGBPoint(2, 0, 1, 0); labelMap.ofun.addPoint(0, 0); labelMap.ofun.addPoint(1, 0.5, 0.5, 1.0); labelMap.ofun.addPoint(2, 0.5, 0.5, 1.0); labelMap.ofun.setClamping(false);
labelMap.actor.getProperty().setRGBTransferFunction(0, labelMap.cfun); labelMap.actor.getProperty().setScalarOpacity(0, labelMap.ofun); labelMap.actor.getProperty().setInterpolationTypeToNearest(); labelMap.actor.getProperty().setUseLabelOutline(true); labelMap.actor.getProperty().setLabelOutlineThickness([2, 3]); labelMap.actor.getProperty().setLabelOutlineOpacity(1.0);
return labelMap; }
function fillBlobForThreshold(imageData, backgroundImageData) { const dims = imageData.getDimensions(); const values = imageData.getPointData().getScalars().getData();
const backgroundValues = backgroundImageData .getPointData() .getScalars() .getData(); const size = dims[0] * dims[1] * dims[2];
const headThreshold = [324, 1524]; for (let i = 0; i < size; i++) { if ( backgroundValues[i] >= headThreshold[0] && backgroundValues[i] < headThreshold[1] ) { values[i] = 1; } }
const boneThreshold = [1200, 2324]; for (let i = 0; i < size; i++) { if ( backgroundValues[i] >= boneThreshold[0] && backgroundValues[i] < boneThreshold[1] ) { values[i] = 2; } }
imageData.getPointData().getScalars().setData(values); }
const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true, }); reader .setUrl(`${__BASE_PATH__}/data/volume/headsq.vti`, { loadData: true }) .then(() => { const data = reader.getOutputData();
mapper.setInputData(data);
const labelMap = createLabelPipeline(data);
const sourceDataRGBTransferFunction = actor .getProperty() .getRGBTransferFunction(0); sourceDataRGBTransferFunction.setMappingRange(324, 2324);
fillBlobForThreshold(labelMap.imageData, data);
istyle.setVolumeMapper(mapper);
renderer.addVolume(actor); renderer.addVolume(labelMap.actor); renderer.getActiveCamera().setViewUp(1, 0, 0); renderWindow.render(); });
|