paraview-glance
Version:
Web application for Visualizing Scientific and Medical datasets
83 lines (58 loc) • 2.78 kB
JavaScript
import macro from 'vtk.js/Sources/macro';
import vtkColorTransferFunction from 'vtk.js/Sources/Rendering/Core/ColorTransferFunction';
import vtkPiecewiseFunction from 'vtk.js/Sources/Common/DataModel/PiecewiseFunction';
import vtkSliceRepresentationProxy from 'vtk.js/Sources/Proxy/Representations/SliceRepresentationProxy';
import ImagePropertyConstants from 'vtk.js/Sources/Rendering/Core/ImageProperty/Constants';
import { makeSubManager } from 'paraview-glance/src/utils';
const { InterpolationType } = ImagePropertyConstants;
// ----------------------------------------------------------------------------
// vtkLabelMapSliceRepProxy methods
// ----------------------------------------------------------------------------
function vtkLabelMapSliceRepProxy(publicAPI, model) {
// Set our className
model.classHierarchy.push('vtkLabelMapSliceRepProxy');
const labelMapSub = makeSubManager();
model.property.setInterpolationType(InterpolationType.NEAREST);
function updateTransferFunctions(labelmap) {
const colorMap = labelmap.getColorMap();
const cfun = vtkColorTransferFunction.newInstance();
const ofun = vtkPiecewiseFunction.newInstance();
Object.keys(colorMap).forEach((label) => {
const l = Number(label);
cfun.addRGBPoint(l, ...colorMap[label].slice(0, 3).map((c) => c / 255));
ofun.addPoint(l, colorMap[label][3] / 255);
});
model.property.setRGBTransferFunction(cfun);
model.property.setScalarOpacity(ofun);
}
function setInputData(labelmap) {
labelMapSub.sub(
labelmap.onModified(() => updateTransferFunctions(labelmap))
);
updateTransferFunctions(labelmap);
}
// override because we manage our own color/opacity functions
publicAPI.setColorBy = () => {};
publicAPI.delete = macro.chain(publicAPI.delete, () => labelMapSub.unsub());
// Keep things updated
model.sourceDependencies.push({ setInputData });
}
// ----------------------------------------------------------------------------
// Object factory
// ----------------------------------------------------------------------------
const DEFAULT_VALUES = {};
// ----------------------------------------------------------------------------
export function extend(publicAPI, model, initialValues = {}) {
Object.assign(model, DEFAULT_VALUES, initialValues);
// Object methods
vtkSliceRepresentationProxy.extend(publicAPI, model);
// Object specific methods
vtkLabelMapSliceRepProxy(publicAPI, model);
}
// ----------------------------------------------------------------------------
export const newInstance = macro.newInstance(
extend,
'vtkLabelMapSliceRepProxy'
);
// ----------------------------------------------------------------------------
export default { newInstance, extend };