@Sebastien_Jourdain
Thank you and I finally got it sorted. The issue was that the polydata being read in from the .vtp did not have the scalars attached to it. The code was reading the range from the scalars within the vtkData
but the same scalars were not in the Delaunay source. In the sphere example code, this vtkData
was being added to the vtk3DGlyphMapper
.
I really appreciate your help on this!
Final Code - for anyone looking to do something similar in the future
Python - Generate Delaunay mesh and save to .vtp
...
delaunay = vtk.vtkDelaunay3D()
delaunay.SetAlpha(alphaValue)
delaunay.SetInputDataObject(polydata)
# Needs to be converted to a PolyData (from unstructured Grid)
delaunay_polydata = vtk.vtkGeometryFilter()
delaunay_polydata.SetInputConnection(delaunay.GetOutputPort())
delaunay_polydata.Update()
writer = vtk.vtkXMLPolyDataWriter()
writer.SetFileName(filename)
writer.SetInputConnection(delaunay_polydata.GetOutputPort())
writer.SetDataModeToBinary()
writer.Write()
Javascript - Read in the .vtp file
fetch(file)
.then(response => response.text())
.then(data => {
const vtpReader = vtk.IO.XML.vtkXMLPolyDataReader.newInstance();
const textEncoder = new TextEncoder();
vtpReader.parseAsArrayBuffer(textEncoder.encode(data));
delaunaySource = vtpReader.getOutputData(0);
delaunaySource = addScalarsToPolydata(delaunaySource)
});
Javascript - Add the Scalars
function addScalarsToPolydata(polydata) {
var scalarAr = vtk.Common.Core.vtkDataArray.newInstance({
numberOfComponents: 1,
values: scalarValues,
name: 'Scalar Name'.toLowerCase()
});
polydata.getPointData().setScalars(scalarAr);
}
Javascript - Generate the Delaunay mesh in JS
source = delaunaySource // Type: vtkPolyData
actor = vtk.Rendering.Core.vtkActor.newInstance();
var lookupTable = vtk.Rendering.Core.vtkColorTransferFunction.newInstance();
lutActor = vtk.Rendering.Core.vtkScalarBarActor.newInstance()
// Get the scalar we want to colour by
let colorByArrayName = activeScalar.toLowerCase()
const scalarArray = source.getPointData().getArrayByName(colorByArrayName.toLowerCase());
// Create the color Map used for the VTK
let colorMap = create_colorMap()
// Update the Look Up Table
lookupTable = applyPreset(lookupTable, colorMap, scalarArray.getRange());
// Updates the Lookup Table actor with a title and ensures that it is visible
lutActor.setAxisLabel(activeScalar);
lutActor.setVisibility(true);
// Mapper for the collection of Spheres
mapper = vtk.Rendering.Core.vtkMapper.newInstance({
colorByArrayName,
colorMode: 1, // vtk.Rendering.Core.Mapper.ColorMode.MAP_SCALARS;,
interpolateScalarsBeforeMapping: true,
scalarMode: 3, // vtk.Rendering.Core.Mapper.Constants.ScalarMode.USE_POINT_FIELD_DATA
scalarVisibility: true,
useLookupTableScalarRange: true,
lookupTable,
});
actor.getProperty().setOpacity(1.0);
actor.setMapper(this.mapper);
mapper.setInputData(this.source);
lutActor.setScalarsToColors(this.mapper.getLookupTable());
renderer.addActor2D(lutActor)
renderer.addActor(actor)
Javascript - Other referenced funcs
function linspace(startValue, stopValue, cardinality) {
var arr = [];
var step = (stopValue - startValue) / (cardinality - 1);
for (var i = 0; i < cardinality; i++) {
arr.push(startValue + (step * i));
}
return arr;
}
function create_colorMap(){
let zero_to_one =linspace(0, 1, 128)
let one_to_zero = linspace(1, 0, 128)
let ones = linspace(1, 1, 128)
let zeros = linspace(0, 0, 128)
let idxs = linspace(0, 1, 256)
// Add the RGB colours for Blue to Yellow through Green
let rgb = [idxs, zeros.concat(zero_to_one), zero_to_one.concat(ones), one_to_zero.concat(zeros)]
// Invert the matrix so that we can concatenate the array to a 1D array in the right format
rgb = rgb[0].map((_, colIndex) => rgb.map(row => row[colIndex]));
rgb = [].concat(...rgb)
// Create the required object
let colorMap = {
"ColorSpace": "Diverging",
"Name": "blue to yellow through green",
"NanColor": [1, 0, 0],
"RGBPoints": rgb
}
return colorMap
}
function applyPreset(lookupTable, colorMap, dataRange) {
lookupTable.applyColorMap(colorMap);
lookupTable.setMappingRange(dataRange[0], dataRange[1]);
lookupTable.updateRange();
return lookupTable
}