# Putting arrays on each cell of a mesh

**URL:** https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984
**Category:** Support
**Tags:** code
**Created:** [July 19, 2023, 4:02pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984 "2023-07-19T16:02:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Marupio](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marupio](https://discourse.vtk.org/u/Marupio)
#### Post date: [July 19, 2023, 4:02pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984/1 "2023-07-19T16:02:43Z")

</div>

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.:

```auto
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?

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dcthomp/32/60_2.png) [@dcthomp](https://discourse.vtk.org/u/dcthomp)
#### Post date: [July 19, 2023, 4:16pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984/2 "2023-07-19T16:16:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Marupio](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marupio](https://discourse.vtk.org/u/Marupio)
#### Post date: [July 19, 2023, 4:26pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984/3 "2023-07-19T16:26:47Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![dcthomp](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dcthomp/32/60_2.png) [@dcthomp](https://discourse.vtk.org/u/dcthomp)
#### Post date: [July 19, 2023, 4:41pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984/4 "2023-07-19T16:41:45Z")

</div>

> [@Marupio](#):
>
> 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.

---

<div class="post-metadata">

### Author: ![Marupio](https://discourse.vtk.org/letter_avatar_proxy/v4/letter/m/f6c823/32.png) [@Marupio](https://discourse.vtk.org/u/Marupio)
#### Post date: [July 19, 2023, 4:49pm UTC](https://discourse.vtk.org/t/putting-arrays-on-each-cell-of-a-mesh/11984/5 "2023-07-19T16:49:18Z")

</div>

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.
