Hi,
I would like to use the tubeFilter to draw pipes of different diameters from polydata.
However, I am unable to use the celldata to draw a uniform diameter for each pipe.
Below my attempt…
import '@kitware/vtk.js/favicon';
import '@kitware/vtk.js/Rendering/Profiles/Geometry';
import Constants from '@kitware/vtk.js/Filters/General/TubeFilter/Constants';
import vtkActor from '@kitware/vtk.js/Rendering/Core/Actor';
import vtkDataArray from '@kitware/vtk.js/Common/Core/DataArray';
import vtkFullScreenRenderWindow from '@kitware/vtk.js/Rendering/Misc/FullScreenRenderWindow';
import vtkMapper from '@kitware/vtk.js/Rendering/Core/Mapper';
import vtkPolyData from '@kitware/vtk.js/Common/DataModel/PolyData';
import vtkTubeFilter from '@kitware/vtk.js/Filters/General/TubeFilter';
const { VaryRadius } = Constants;
// ----------------------------------------------------------------------------
// Standard rendering code setup
// ----------------------------------------------------------------------------
const fullScreenRenderer = vtkFullScreenRenderWindow.newInstance({
background: [0, 0, 0],
});
const renderer = fullScreenRenderer.getRenderer();
const renderWindow = fullScreenRenderer.getRenderWindow();
// ----------------------------------------------------------------------------
// Example code
// ----------------------------------------------------------------------------
// Polydata: 2 pipes each with a unique diameter
const multilinestring = []
multilinestring.push([[0, 0, 0], [1, 0, 0]])
multilinestring.push([[1, 0, 0], [0, 1, 0]])
var polydata = vtkPolyData.newInstance();
var vertexCount = 0;
var vertexArray = [];
var cellArray = [];
var diametersArray = [];
var pressuresArray = [];
for (var p = 0; p < multilinestring.length; p++) {
let line = multilinestring[p];
cellArray.push(line.length); // number of points in line
if (p == 0) {
diametersArray.push(0.1)
} else {
diametersArray.push(p)
}
for (var i = 0; i < line.length; i++) {
let xyz = line[i]
cellArray.push(vertexCount++); // index of each point
vertexArray.push(xyz[0], xyz[1], xyz[2]); // point coordinate
pressuresArray.push(i) // scalar value for point
}
}
polydata.getPoints().setData(Float32Array.from(vertexArray), 3);
polydata.getLines().setData(Uint16Array.from(cellArray));
polydata.getCellData().setScalars(vtkDataArray.newInstance({ name: "diameters", values: Float32Array.from(diametersArray) }))
polydata.getPointData().setScalars(vtkDataArray.newInstance({ name: "pressures", values: Float32Array.from(pressuresArray) }))
// tubefilter
const tubeFilter = vtkTubeFilter.newInstance();
tubeFilter.set({
capping: true,
radius: 0.01, // radius scaling factor
varyRadius: VaryRadius.VARY_RADIUS_BY_SCALAR,
numberOfSides: 10
})
tubeFilter.setInputData(polydata);
tubeFilter.setInputArrayToProcess(0, 'diameters', 'CellData', 'Scalars');
// tubemapper
const tubeMapper = vtkMapper.newInstance()
tubeMapper.setInputConnection(tubeFilter.getOutputPort())
tubeMapper.setScalarModeToUseCellData()
// tubeactor
const tubeActor = vtkActor.newInstance()
tubeActor.setMapper(tubeMapper)
tubeActor.getProperty().setOpacity(0.7)
renderer.addActor(tubeActor)
// render
renderer.resetCamera();
renderWindow.render();