I wish to produce a VTK polydata (eventually write it out as a *.vtp file) where I can associate some scalar value (e.g. pH, temperature, salinity) on a per face level, how should I go about doing that ? Is there an example that has something similar I can learn from?
I intend to load the VTP file in ParaView for visualization.
Cheers
you need to add “Cell data” attributes . Check the Data attributes format section as well as the examples in the subsequent section.
Thank you @Christos_Tsolakis , I got it working.
#include <vtkActor.h>
#include <vtkCellData.h>
#include <vtkColorTransferFunction.h>
#include <vtkLookupTable.h>
#include <vtkNamedColors.h>
#include <vtkNew.h>
#include <vtkPlaneSource.h>
#include <vtkPolyDataMapper.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkRenderer.h>
#include <vtkFloatArray.h>
#include <vtkXMLPolyDataReader.h>
#include <vtkXMLPolyDataWriter.h>
#include <algorithm>
#include <iomanip>
#include <iostream>
void MakeCellData(size_t const& tableSize,
vtkFloatArray* scalars) {
for (size_t i = 1; i < tableSize; i++) {
scalars->InsertNextTuple1(i);
}
}
int main() {
// Provide some geometry
int resolution = 3;
vtkNew<vtkPlaneSource> plane11;
plane11->SetXResolution(resolution);
plane11->SetYResolution(resolution);
// Force an update so we can set cell data
plane11->Update();
int tableSize = std::max(resolution * resolution + 1, 10);
vtkNew<vtkFloatArray> kelvin;
kelvin->SetName("kelvin");
kelvin->SetNumberOfComponents(1);
MakeCellData(tableSize, kelvin);
plane11->GetOutput()->GetCellData()->SetScalars(kelvin);
// Write the file
vtkSmartPointer<vtkXMLPolyDataWriter> writer =
vtkSmartPointer<vtkXMLPolyDataWriter>::New();
writer->SetFileName("celldata.vtp");
writer->SetInputData(plane11->GetOutput());
// Optional - set the mode. The default is binary.
//writer->SetDataModeToBinary();
writer->SetDataModeToAscii();
writer->Write();
return 0;
}
I have found that I can only set one scalar, I have other attributes I’d like to add to the polydata e.g. salinity. If I set my salinity data, my temperature attribute is lost.
Answering my own question. Use AddArray() rather than SetScalars() works for me.
My AddArray() calls are now crashing in VTK but I am not sure why, do I have to do something to vtkCellData before I can call AddArray() ?
My code did not get to the line printf("xx00102\n");
if (euclidean_areas_exist) {
printf("xx00100\n");
vtkNew<vtkFloatArray> vtk_euclidean_areas;
vtk_euclidean_areas->SetName("eu_areas");
vtk_euclidean_areas->SetNumberOfComponents(1);
for (FloatContainer::const_iterator iter = euclidean_areas.begin();
iter != euclidean_areas.end(); ++iter) {
vtk_euclidean_areas->InsertNextTuple1((*iter));
}
printf("xx00101\n");
assert(source);
vtkCellData *cd = source->GetCellData();
assert(cd);
cd->AddArray(vtk_euclidean_areas);
printf("xx00102\n");
}
I have built a debug version of VTK 9.2.2 and step through the code and it appears to failed at the call to Modified()
I’ll submit the above as a separate post