Putting arrays on each cell of a mesh

I have a vtkMultiBlockDataSet that contains some vtkUnstructuredGrids and some vtkPolyData objects. I want to be able to put data on the cells of the vtkUnstructuredGrids such that each cell contains an arbitrary number of elements, e.g.:

vtkAbstractArray* celFaces = myUnstructuredGrid->GetCellData()->GetAbstractArray("cellFaces");
// cellFaces is a two dimensional array, where each i'th element contains an arbitrary number of j elements.

Is this possible?

Ragged arrays possible, but not in a way that will behave or perform well; you must create a vtkVariantArray and assign a vtkDataArray to each cell’s value. This is slow, consumes a lot of memory, and is hard to use for visualizations (such as coloring cells).

VTK is not really designed for ragged arrays. A more performant but less dynamic method to accommodate ragged arrays is to store a single offset per cell into a single vtkDataArray held in the dataset’s field-data. But then every filter that needs access to values must know how to use the offset to obtain the set of values for the cell.

Thank you for answering so quickly!

I was thinking of adding one array for each element in the j dimension, plus one for size. Offsets into a single massive array sounds far better.

Can I add a vtkDataArray that has more values than there are cells or points?

Yes, datasets in VTK own 3 containers for arrays:

  • vtkPointData requires arrays that have a tuple for each point
  • vtkCellData requires arrays that have a tuple for each cell
  • vtkFieldData has no constraints on the number of tuples (but also cannot be used directly to color geometry, etc. since there is no context that specifies how the arrays map to any geometry).

Just put your one big array in the field data (inherited from vtkDataObject) and your offset array in the cell-data.

Aha! I see it now. [Set/Get]FieldData. It was hiding on the DataObject, not the DataSet.

I will use your suggestions.

Thank you so much! You have saved me hours of work.