import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry'; import '@kitware/vtk.js/Rendering/Profiles/Glyph';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; import '@kitware/vtk.js/IO/Core/DataAccessHelper/HttpDataAccessHelper';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; import vtkHttpDataSetReader from '@kitware/vtk.js/IO/Core/HttpDataSetReader'; import vtkLookupTable from '@kitware/vtk.js/Common/Core/LookupTable'; import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; import vtkThresholdPoints from '@kitware/vtk.js/Filters/Core/ThresholdPoints'; import vtkCalculator from '@kitware/vtk.js/Filters/General/Calculator'; import { FieldDataTypes } from '@kitware/vtk.js/Common/DataModel/DataSet/Constants'; import { AttributeTypes } from '@kitware/vtk.js/Common/DataModel/DataSetAttributes/Constants'; import vtkScalarBarActor from '@kitware/vtk.js/Rendering/Core/ScalarBarActor';
import controlPanel from './controlPanel.html';
const { ColorMode, ScalarMode } = vtkMapper;
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ background: [0.9, 0.9, 0.9], }); fullScreenRenderer.addController(controlPanel);
const renderer = fullScreenRenderer.getRenderer(); const renderWindow = fullScreenRenderer.getRenderWindow();
const lookupTable = vtkLookupTable.newInstance({ hueRange: [0.666, 0] });
const reader = vtkHttpDataSetReader.newInstance({ fetchGzip: true }); reader.setUrl(`${__BASE_PATH__}/data/cow.vtp`).then(() => { reader.loadData().then(() => { renderer.resetCamera(); renderWindow.render(); }); });
const calc = vtkCalculator.newInstance(); calc.setInputConnection(reader.getOutputPort()); calc.setFormula({ getArrays: (inputDataSets) => ({ input: [{ location: FieldDataTypes.COORDINATE }], output: [ { location: FieldDataTypes.POINT, name: 'sine wave', dataType: 'Float64Array', attribute: AttributeTypes.SCALARS, }, { location: FieldDataTypes.UNIFORM, name: 'global', dataType: 'Float32Array', numberOfComponents: 1, tuples: 1, }, ], }), evaluate: (arraysIn, arraysOut) => { const [coords] = arraysIn.map((d) => d.getData()); const [sine, glob] = arraysOut.map((d) => d.getData());
for (let i = 0, sz = coords.length / 3; i < sz; ++i) { const dx = coords[3 * i] - 0.5; const dy = coords[3 * i + 1] - 0.5; sine[i] = 10 * dx * dx + dy * dy; } glob[0] = sine.reduce((result, value) => result + value, 0); arraysOut.forEach((x) => x.modified()); }, });
const mapper = vtkMapper.newInstance({ interpolateScalarsBeforeMapping: true, colorMode: ColorMode.DEFAULT, scalarMode: ScalarMode.DEFAULT, useLookupTableScalarRange: true, lookupTable, }); const actor = vtkActor.newInstance(); actor.getProperty().setEdgeVisibility(true);
const scalarBarActor = vtkScalarBarActor.newInstance(); scalarBarActor.setScalarsToColors(lookupTable); renderer.addActor(scalarBarActor);
const thresholder = vtkThresholdPoints.newInstance(); thresholder.setInputConnection(calc.getOutputPort());
mapper.setInputConnection(thresholder.getOutputPort()); actor.setMapper(mapper); renderer.addActor(actor);
const thresholdArray = document.querySelector('#thresholdArray'); const thresholdOperation = document.querySelector('#thresholdOperation'); const thresholdValue = document.querySelector('#thresholdValue'); function updateCriterias(arrayName, operation, value) { thresholder.setCriterias([ { arrayName, fieldAssociation: arrayName === 'sine wave' ? 'PointData' : 'Points', operation, value: Number(value), }, ]); } function onCriteriaChanged(event) { updateCriterias( thresholdArray.value, thresholdOperation.value, thresholdValue.value ); if (event != null) { renderWindow.render(); } } onCriteriaChanged(); function onArrayChanged(event) { if (thresholdArray.value === 'x' || thresholdArray.value === 'y') { thresholdValue.min = '-5'; thresholdValue.max = '5'; thresholdValue.step = '1'; thresholdValue.value = '0'; } else if (thresholdArray.value === 'z') { thresholdValue.min = '-2'; thresholdValue.max = '2'; thresholdValue.step = '0.1'; thresholdValue.value = '0'; } else { thresholdValue.min = 0; thresholdValue.max = 256; thresholdValue.step = 10; thresholdValue.value = 30; } onCriteriaChanged(event); } thresholdArray.addEventListener('change', onArrayChanged); thresholdOperation.addEventListener('change', onCriteriaChanged); thresholdValue.addEventListener('input', onCriteriaChanged);
global.mapper = mapper; global.actor = actor; global.source = reader; global.renderer = renderer; global.renderWindow = renderWindow; global.lookupTable = lookupTable; global.thresholder = thresholder;
|