import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow'; import macro from '@kitware/vtk.js/macros'; import vtk from '@kitware/vtk.js/vtk'; import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor'; import vtkCamera from '@kitware/vtk.js/Rendering/Core/Camera'; import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray'; import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper'; import vtkSphereSource from '@kitware/vtk.js/Filters/Sources/SphereSource'; import vtkWarpScalar from '@kitware/vtk.js/Filters/General/WarpScalar';
import controlPanel from './controller.html';
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ background: [0, 0, 0], }); const renderer = fullScreenRenderer.getRenderer(); const renderWindow = fullScreenRenderer.getRenderWindow();
const actor = vtkActor.newInstance(); renderer.addActor(actor);
const mapper = vtkMapper.newInstance({ interpolateScalarBeforeMapping: true }); actor.setMapper(mapper);
const cam = vtkCamera.newInstance(); renderer.setActiveCamera(cam); cam.setFocalPoint(0, 0, 0); cam.setPosition(0, 0, 10); cam.setClippingRange(0.1, 50.0);
const sphereSource = vtkSphereSource.newInstance({ thetaResolution: 40, phiResolution: 41, }); const filter = vtkWarpScalar.newInstance({ scaleFactor: 0, useNormal: false });
const randFilter = macro.newInstance((publicAPI, model) => { macro.obj(publicAPI, model); macro.algo(publicAPI, model, 1, 1); publicAPI.requestData = (inData, outData) => { if (!outData[0] || inData[0].getMTime() > outData[0].getMTime()) { const newArray = new Float32Array( inData[0].getPoints().getNumberOfPoints() ); for (let i = 0; i < newArray.length; i++) { newArray[i] = i % 2 ? 1 : 0; }
const da = vtkDataArray.newInstance({ name: 'spike', values: newArray }); const newDataSet = vtk({ vtkClass: inData[0].getClassName() }); newDataSet.shallowCopy(inData[0]); newDataSet.getPointData().setScalars(da); outData[0] = newDataSet; } }; })();
randFilter.setInputConnection(sphereSource.getOutputPort()); filter.setInputConnection(randFilter.getOutputPort()); mapper.setInputConnection(filter.getOutputPort());
filter.setInputArrayToProcess(0, 'spike', 'PointData', 'Scalars');
fullScreenRenderer.addController(controlPanel);
['scaleFactor'].forEach((propertyName) => { document.querySelector(`.${propertyName}`).addEventListener('input', (e) => { const value = Number(e.target.value); filter.set({ [propertyName]: value }); renderWindow.render(); }); });
document.querySelector('.useNormal').addEventListener('change', (e) => { const useNormal = !!e.target.checked; filter.set({ useNormal }); renderWindow.render(); });
['radius', 'thetaResolution', 'phiResolution'].forEach((propertyName) => { document.querySelector(`.${propertyName}`).addEventListener('input', (e) => { const value = Number(e.target.value); sphereSource.set({ [propertyName]: value }); renderWindow.render(); }); });
renderer.resetCamera(); renderWindow.render();
global.source = sphereSource; global.filter = filter; global.mapper = mapper; global.actor = actor;
|