StickMapper

Live example

Source

import 'vtk.js/Sources/favicon';
import vtkActor from 'vtk.js/Sources/Rendering/Core/Actor';
import vtkCalculator from 'vtk.js/Sources/Filters/General/Calculator';
import vtkFullScreenRenderWindow from 'vtk.js/Sources/Rendering/Misc/FullScreenRenderWindow';
import vtkPlaneSource from 'vtk.js/Sources/Filters/Sources/PlaneSource';
import vtkStickMapper from 'vtk.js/Sources/Rendering/Core/StickMapper';
import { AttributeTypes } from 'vtk.js/Sources/Common/DataModel/DataSetAttributes/Constants';
import { FieldDataTypes } from 'vtk.js/Sources/Common/DataModel/DataSet/Constants';
import { Representation } from 'vtk.js/Sources/Rendering/Core/Property/Constants';
import controlPanel from './controlPanel.html';
// ----------------------------------------------------------------------------
// Standard rendering code setup
// ----------------------------------------------------------------------------
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({ background: [0, 0, 0] });
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------
const planeSource = vtkPlaneSource.newInstance();
const simpleFilter = vtkCalculator.newInstance();
const mapper = vtkStickMapper.newInstance();
const actor = vtkActor.newInstance();
actor.getProperty().setRepresentation(Representation.WIREFRAME); // ??? Is this useful?
simpleFilter.setFormula({
getArrays: inputDataSets => ({
input: [
{ location: FieldDataTypes.COORDINATE }], // Require point coordinates as input
output: [ // Generate two output arrays:
{
location: FieldDataTypes.POINT, // This array will be point-data ...
name: 'orientation', // ... with the given name ...
dataType: 'Float32Array', // ... of this type ...
numberOfComponents: 3, // ... with this many components ...
},
{
location: FieldDataTypes.POINT, // This array will be field data ...
name: 'temperature', // ... with the given name ...
dataType: 'Float32Array', // ... of this type ...
attribute: AttributeTypes.SCALARS, // ... and will be marked as the default scalars.
numberOfComponents: 1, // ... with this many components ...
},
{
location: FieldDataTypes.POINT, // This array will be field data ...
name: 'pressure', // ... with the given name ...
dataType: 'Float32Array', // ... of this type ...
numberOfComponents: 2, // ... with this many components ...
},
] }),
evaluate: (arraysIn, arraysOut) => {
// Convert in the input arrays of vtkDataArrays into variables
// referencing the underlying JavaScript typed-data arrays:
const [coords] = arraysIn.map(d => d.getData());
const [orient, temp, press] = arraysOut.map(d => d.getData());
// Since we are passed coords as a 3-component array,
// loop over all the points and compute the point-data output:
for (let i = 0, sz = coords.length / 3; i < sz; ++i) {
orient[i * 3] = ((coords[3 * i] - 0.5) * (coords[3 * i] - 0.5)) + ((coords[(3 * i) + 1] - 0.5) * (coords[(3 * i) + 1] - 0.5));
orient[(i * 3) + 1] = ((coords[3 * i] - 0.5) * (coords[3 * i] - 0.5)) + ((coords[(3 * i) + 1] - 0.5) * (coords[(3 * i) + 1] - 0.5));
orient[(i * 3) + 2] = 1.0;
temp[i] = coords[(3 * i) + 1];
press[(i * 2)] = ((((coords[3 * i]) * (coords[3 * i])) + ((coords[(3 * i) + 1]) * (coords[(3 * i) + 1]))) * 0.05) + 0.05;
press[(i * 2) + 1] = ((((coords[3 * i]) * (coords[3 * i])) + ((coords[(3 * i) + 1]) * (coords[(3 * i) + 1]))) * 0.01) + 0.01;
}
// Mark the output vtkDataArray as modified
arraysOut.forEach(x => x.modified());
},
});
// The generated 'temperature' array will become the default scalars, so the plane mapper will color by 'temperature':
simpleFilter.setInputConnection(planeSource.getOutputPort());
mapper.setInputConnection(simpleFilter.getOutputPort());
mapper.setOrientationArray('orientation');
mapper.setScaleArray('pressure');
actor.setMapper(mapper);
renderer.addActor(actor);
renderer.resetCamera();
renderWindow.render();
// -----------------------------------------------------------
// UI control handling
// -----------------------------------------------------------
fullScreenRenderer.addController(controlPanel);
['xResolution', 'yResolution'].forEach((propertyName) => {
document.querySelector(`.${propertyName}`).addEventListener('input', (e) => {
const value = Number(e.target.value);
planeSource.set({ [propertyName]: value });
renderWindow.render();
});
});
// -----------------------------------------------------------
// Make some variables global so that you can inspect and
// modify objects in your browser's developer console:
// -----------------------------------------------------------
global.planeSource = planeSource;
global.mapper = mapper;
global.actor = actor;
global.renderer = renderer;
global.renderWindow = renderWindow;