Cell Set as Unstructured Grid

In the last 2 blogs (1, 2), we have looked at creating vtkMappedDataArray<> subclasses to represent different memory layouts. This enabled us to represent various types of grids without explicitly storing the point coordinates. In this blog, we will take this idea to the next step and create an unstructured grid that uses a vtkMappedDataArray<> to represent its points but also store its connectivity implicitly. There are many use cases for this. For example, we can develop a better threshold filter. The current implementation (vtkThreshold) creates an unstructured grid no matter the input type. For structured data, this can be terribly memory inefficient because it creates connectivity arrays and potentially a new explicit point coordinates array (for vtkImageData and vtkRectilinearGrid). One alternative to this approach is to create a new unstructured grid type that stores the input dataset and a set of cell ids that meet the threshold criteria. We will cover how such a class can be defined using the zero copy infrastructure. I will describe the new threshold algorithm in detail in a future blog.


First, let’s recap the points array from a previous blog. The full source for this array is here and here. In summary, vtkImagePointsArray is a sub-class of vtkMappedArray<> that obtains its values from a vtkImageData rather than from an array stored explicitly. This enables us to represent unstructured grids with implicit point coordinates. The meat of this class is as follows.

Note that Points is actually a vtkImageData, which calculates point coordinates implicitly with the following code.

I left out some of the code for simplicity. We can exercise this class with the following.


Our next step is to define a new unstructured grid that does not store a new connectivity or point coordinate array for an input grid. Instead, we want it to simply store the ids of the cells selected by the threshold process. We will do this by leveraging the mapped unstructured grid functionality created by David Lonie and described here. We will create a new unstructured grid (subclass of vtkMappedUnstructuredGrid) called vtkCellSet. Our first step is to create a helper class (subclass of vtkObject) that takes care of the implementation of core functions. See here and here for the full implementation.

As I mentioned, this class should store a dataset and a set of selected cells. This looks as follows.

Then we need to implement a few key methods as follows.

Note that many of these are deferred to the underlying dataset (DataSet). The key point is that cell ids are transformed using the internal selection array (CellIds) as in this example.

This means that any DataSet cell not listed in the CellIds array will be ignored by this class. We can exercise this class as follows

Here, we restricted the contour filter to the first half of the cells of the original image data by simply creating a vtkCellSet. Note that this code would have been almost identical whether we were dealing with an image data, structured grid or an unstructured grid. All vtkCellSet needs is a vtkDataSet and a set of cell ids.

This is it! When I first worked on this example, I could not believe that it is this simple. Granted, I left some of the more complex methods such as GetPointCells() unimplemented because I didn’t need them. Still, with some additional data structures, it would be fairly easy to implement them. We will discuss a new threshold filter that uses the vtkCellSet class in a future blog.

Questions or comments are always welcome!