How do I color image data by its associated cell data?

I am using a the vtkXMLImageDataReader to load in my existing vti files that have cell data. I can load in the data just fine and display the first cell data scalar available in the file. What I am having trouble with is changing how the data is colored to any of the other available cell data arrays.

The basic pipeline is vtkXMLImageDataReader -> vtkGeometryFilter -> vtkDataSetMapper-> vtkActor

I would have expected to change the displayed color with something like the following, but the color does not change.

auto mapper = vtkSmartPointer::New();
mapper->SetInputConnection(filter->GetOutputPort());
mapper->SetLookupTable(lut);
mapper->SetColorModeToMapScalars();
mapper->ScalarVisibilityOn();
mapper->SetScalarModeToUseCellData();
mapper->ColorByArrayComponent(“Volume Fraction (-)”, 0);

Thank you.

1 Like

What kind of data set is this (what software generated it and what does it contain)? Can you upload an example data set somewhere and post the link here?

Have you considered saving the data as a simple multi-component scalar volume in nrrd format?

It is fluid dynamics data in ImageData format. Sure, please find the attached vti file.

No, I do not want to change to format. It loads into other tools just fine.

Slice.6.00333e-01.vti (196.4 KB)

Ok I think I have this figured out now. The key was to set scalar mode to use cell field data –

		mapper->SetInputConnection(filter->GetOutputPort());
		mapper->SetLookupTable(lut);
		mapper->SetColorModeToMapScalars();			
		mapper->ScalarVisibilityOn();	
		mapper->SetScalarModeToUseCellFieldData();
		mapper->SelectColorArray("Volume Fraction (-)");
		mapper->ColorByArrayComponent("Volume Fraction (-)", 0);

I was also able to change the color with the original code by calling SetActiveScalars on the reader.

So it seems like the “active scalar” is used when SetScalarModeToUseCellData is on. If SetScalarModeToUseCellFieldData is on, I can just change the coloring with the mapper itself by subsequently calling ColorByArrayComponent.

1 Like

VTK imaging filters expects voxel information to be in imageData->GetPointData()->GetScalars(). Other scalars may be ignored or removed by filters and may not be possible to write to image file formats. Due to all these, I would consider storing additional scalars in vtkImageData a misuse of VTK.

Clean solutions could be:

  • Store all data in components of the points scalar array
  • Rewrite imaging filters, mappers, and IO classes to be prepared for additional scalars.
  • Use You could consider using vtkStructuredGrid or vtkExplicittructuredGrid

@cory.quammen @mwestphal what do you think? What representation ParaView supports/prefers for this kind of data?

One nice thing about the vtkImageData is that the mesh can be described by a few parameters for constant uniform spacing. Our CFD simulations can get upwards of 100M+ cells so this image format lets avoid writing the mesh explicitly in every vtk file.

vtkImageData is just fine in ParaView. You can indeed color by cell arrays.

Would you expect that processing filters will work well, too?

For example, for the provided sample data set, thresholding works for the first scalar:

But you get garbled results if you choose the second scalar:

I’m sure that VTK could be prepared to handle additional scalars in vtkImageData (or support can be added at application level), but I’m not sure how much it is supported in core VTK filters, mappers, and IO classes.

1 Like

I haven’t investigated it thoroughly. I doubt most of the VTK filters handle cell data.

Point data in vtkImageData is much more thoroughly supported in VTK, and I would recommend using it. Converting from using cell data arrays to point data arrays is fairly trivial (just modify the extent by 1).

1 Like

Strange. I don’t get the garbled mess when I threshold on volume fraction in paraview.

In any case, it sounds like we may run into issues at some point using cell based data within an image data object. Our filtering needs are pretty simple – threshold and various clipping are the most commonly used ones. We’ve been using paraview for years without much issue anyway with this format.

That said we do tend to merge and convert the cell data to point data for smoother images when using paraview. In our CFD solver, each process writes out its own vti file. In paraview, we merge the data to prevent discontinuities from appearing across interfaces. For some users this “reassembly” can present an issue, and we provide scripts to help with that workflow step. If there is a more natural way to represent this data in vtk I would be interested in suggestions.