The implementation of ParaView’s Graident Of Unstructured DataSet
filter is the VTK class vtkGradientFilter
. You can get to the source code by downloading the ParaView source code, the VTK source code, or going to the VTK git repository. You can browse the code from the web here:
https://gitlab.kitware.com/vtk/vtk/blob/master/Filters/General/vtkGradientFilter.cxx
The operation of the filter is simple. It first iterates over all the cells and computes local gradients for each one. It does this by using the Derivatives
method for the appropriate cell object. That is, each type of cell for an unstructured grid has an associated VTK class to manage that type of cell, and each of these has a Derivatives
method to compute a local gradient based on parametric coordinates inside the cell. These are all located in the Common/DataModel
directory of the VTK repository. Below are links to the class objects for tetrahedra and hexahedra, which I find are the most common cells in an unstructured grid.
https://gitlab.kitware.com/vtk/vtk/blob/master/Common/DataModel/vtkTetra.cxx
https://gitlab.kitware.com/vtk/vtk/blob/master/Common/DataModel/vtkHexahedron.cxx
Of course, when computing these local gradients, there are (typically) discontinuities at the boundaries of neighboring cells. To manage this and create a continuous gradient, the Gradient Of Unstructured DataSet
filter averages out all gradients on cells incident to each point. There are actually 2 modes to do this. The first (default) mode computes for each cell a separate gradient for each of its points. (Cell types like a hexahedron can have different gradient values at each of its point.) When gradients are averaged over a point, it averages the gradient field for each incident cell at that point. However, if you turn on the Faster Approximation
option, the filter will only compute one gradient per cell at the cell’s center and then use those to average at points (basically a standard cell-to-point operation).