How to define a vtkExplicitStructuredGrid?

I am trying to get a grasp for how to define and append point and cell data to a vtkExplicitStructuredGrid structure (https://blog.kitware.com/introducing-explicit-structured-grids-in-vtk-and-paraview), for use with ParaView through plugins by Python scripting.

I understand how to define and append point and cell data to a vtkStructuredGrid with the use of flat indexing.

I get a bit confused when looking at the “Detailed Description” section in the class reference https://vtk.org/doc/nightly/html/classvtkExplicitStructuredGrid.html, where it states that “The order and number of points is arbitrary”. As I understand the each cell in the grid should be a hexahedron and contribute with eight points to the grid?

I would be very grateful if someone could explain the basics of building this structure and associate attributes!

I will suggest reading the VTK user guide, chapter 16.2, about implicit geometries.
https://www.kitware.com/products/books/VTKUsersGuide.pdf

Thanks for your reply mwestphal.

Is the generell principle then to set the dimensions (defining (i, j, k) space) of the structure, so that one can continue to insert one cell at a time following the order defined through "for k … (for j … (for i …))?

In contrast to setting a vtkStructuredGrid where the points defining the mesh can be set through a vtkPoints object.

If so, I’m failing to identify the methods that vtkExplicitStructuredGrid can access to realise such assignments.

Would it be possible to show a minimal example, e.g. a cube/cuboid consisting of 2x2x2 cells?

Tools -> Manage Plugins -> Explicit Structured Grid -> Load
Source -> ExplicitStructuredGridGenerator.

Yes, I already looked at python-scripts generated after adding an ExplicitStructuredGridGenerator to the pipeline, but failed to see how to actually define a structure explicitly from own data, defined in a script or read from file into a generating script.

Maybe I was a bit unclear in my question? :slightly_smiling_face:

The goal here is a Python-scripted ParaView-plugin that reads structure-data and attribute-data from file and generates an Explicit Structured Grid with cell data.

See here :
https://gitlab.kitware.com/paraview/paraview/blob/master/Plugins/ExplicitStructuredGrid/Filter/vtkExplicitStructuredGridGeneratorSource.cxx

Thanks!

This surely points (:slightly_smiling_face:) in the right direction.

I’m trying to replicate the structure used in one of the methods in the C++ source file, using Python. Now stuck at assigning points inside the i-j-k loop, i.e., assignments like this one

indice[0] = points->InsertNextPoint(i - 0.5, j - 0.5, GetZShift1(k - 0.5));

How can the points be set with Python?

See here for examples of creating points in python :
https://www.paraview.org/Wiki/Python_Programmable_Filter

Is it the Helix example you are referring to?

In that case I fail to find the equivalent methods for fixing/adding the cells.

The code that I manage to run so far is listed below. The cells are not working properly, changing Outline to Surface or Surface with Edges. Adding spherical glypsh gives the following output

I’m expecting one sphere in each corner, but there’s only two of those corners.
Each edge should have two spheres and each face should have have four spheres.

Is there a reason some points are missing? Is it related to the missing cell structure?

How can one fix the code so the cells gets properly defined?

import numpy as np
import vtk

from paraview.util.vtkAlgorithm import *
from vtkmodules.numpy_interface import dataset_adapter as dsa
from vtkmodules.vtkCommonDataModel import vtkExplicitStructuredGrid

@smproxy.source(name="ExplicitGrid_refactor",
                label="TestExplicitGrid")
class PythonESGReader(VTKPythonAlgorithmBase):
    def __init__(self):
        VTKPythonAlgorithmBase.__init__(self, nInputPorts=0, nOutputPorts=1, outputType='vtkExplicitStructuredGrid')
        self.__N = 3

    def RequestInformation(self, request, inInfoVec, outInfoVec):
        executive = self.GetExecutive()
        outInfo = executive.GetOutputInformation(0)
        outInfo.Set(executive.WHOLE_EXTENT(), 0, self.__N-1, 0, self.__N-1, 0, self.__N-1)
        return 1

    def RequestData(self, request, inInfoVec, outInfoVec):

        extent = (0, self.__N-1, 0, self.__N-1, 0, self.__N-1)

        nx = extent[1] - extent[0]
        ny = extent[3] - extent[2]
        nz = extent[5] - extent[4]
        expected_cells = nx * ny * nz

        output = dsa.WrapDataObject(
            vtkExplicitStructuredGrid.GetData(outInfoVec, 0)
        )
        output.SetExtent(*extent)

        newPoints = vtk.vtkPoints()
        newPoints.Allocate(expected_cells * 8)
        for k in range(nx):
            for j in range(ny):
                for i in range(nz):
                    # in each direction, shift the second cell
                    dx, dy, dz = 0.0, 0.0, 0.0
                    if i > 0:
                        dx = 0.2
                    if j > 0:
                        dy = 0.2
                    if k > 0:
                        dz = 0.2

                    # Add eight points
                    newPoints.InsertNextPoint(i-0.5+dx, j-0.5+dy, k-0.5+dz)
                    newPoints.InsertNextPoint(i+0.5+dx, j-0.5+dy, k-0.5+dz)
                    newPoints.InsertNextPoint(i-0.5+dx, j+0.5+dy, k-0.5+dz)
                    newPoints.InsertNextPoint(i+0.5+dx, j+0.5+dy, k-0.5+dz)
                    newPoints.InsertNextPoint(i-0.5+dx, j-0.5+dy, k+0.5+dz)
                    newPoints.InsertNextPoint(i+0.5+dx, j-0.5+dy, k+0.5+dz)
                    newPoints.InsertNextPoint(i-0.5+dx, j+0.5+dy, k+0.5+dz)
                    newPoints.InsertNextPoint(i+0.5+dx, j+0.5+dy, k+0.5+dz)

        output.SetPoints(newPoints)

        return 1

By default Glyph does not show all the points. Switch to “All Points” property.

Nice! That assures me that the points are correctly defined, now I can see all of them.

But still no cells.

Do I misunderstand the concept of implicit topology here? The code adds eight points at a time for each cell in the nested loops.

The code is very similar now to when I got vtkStructuredGrid to function properly, output.SetPoints(newPoints) corresponds to setting the grid/mesh in that case. With a different array of points then, only describing the coordinates of each common corner.

You should definitely be able to see the cells.

Can you show us a screenshot of the information tab ?

Sure thing

Additional information. For some reason, I see that ParaView crashes for me if I change the Outline -> Surface With Edges now. I didn’t experience this earlier I think (when I was missing some glyphs).

What do you see when you switch to Surface ?

Also a crash.

I can prevent ParaView from crashing by adding a loop

for i in range(expected_cells):
            output.BlankCell(i)

But no surface (maybe expected).

If I add a loop

for i in range(expected_cells):
            output.UnBlankCell(i)

at the end, ParaView crashes again.

Does ParaView crash if you add a ExplicitGridToUnstructuredGridFilter ?
And then if you add a UnstructuredGridToExplicitGridFilter ?

Does not seem to crash when applying

ExplicitStructuredGridToUnstructuredGrid,

followed by an

UnstructuredGridToExplicitStructuredGrid.

Still nothing shows

Can you save the result of ExplicitStructuredGridToUnstructuredGrid as a .vtu and send it here ?

Sure thing

eight_cells_ExplicitStructuredGrid_to_Unstructured.vtu (5.0 KB)

The cells are indeed incorrect and they all uses a single point.

You will have to compare your code with the one I sent before in order to figure out what is going on here.