# How to define a vtkExplicitStructuredGrid?

**URL:** https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189
**Category:** ParaView Support
**Created:** [December 16, 2019, 1:55pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189 "2019-12-16T13:55:19Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 16, 2019, 1:55pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/1 "2019-12-16T13:55:19Z")

</div>

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](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](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!

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 16, 2019, 2:12pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/2 "2019-12-16T14:12:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 17, 2019, 9:58am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/3 "2019-12-17T09:58:30Z")

</div>

Thanks for your reply [mwestphal](https://discourse.paraview.org/u/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?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 17, 2019, 10:01am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/4 "2019-12-17T10:01:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 17, 2019, 10:41am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/5 "2019-12-17T10:41:34Z")

</div>

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? 🙂

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 17, 2019, 11:08am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/6 "2019-12-17T11:08:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 17, 2019, 1:21pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/7 "2019-12-17T13:21:33Z")

</div>

Thanks!

This surely points (🙂) 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?

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 17, 2019, 1:58pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/8 "2019-12-17T13:58:41Z")

</div>

See here for examples of creating points in python :  
[https://www.paraview.org/Wiki/Python\_Programmable\_Filter](https://www.paraview.org/Wiki/Python_Programmable_Filter)

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 18, 2019, 10:49am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/9 "2019-12-18T10:49:36Z")

</div>

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

 ![36](https://discourse.paraview.org/uploads/default/original/2X/9/9cd2536e1c9b6e6454335d700034651e50ae0d28.png)

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?

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

```

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 18, 2019, 11:12am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/10 "2019-12-18T11:12:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 18, 2019, 11:32am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/11 "2019-12-18T11:32:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 18, 2019, 11:55am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/12 "2019-12-18T11:55:27Z")

</div>

You should definitely be able to see the cells.

Can you show us a screenshot of the information tab ?

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 18, 2019, 12:06pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/13 "2019-12-18T12:06:12Z")

</div>

Sure thing

 ![33](https://discourse.paraview.org/uploads/default/original/2X/b/b33029a1731a7c20e81ff0230e735e700a63558a.png)

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

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 18, 2019, 12:17pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/14 "2019-12-18T12:17:36Z")

</div>

What do you see when you switch to Surface ?

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 18, 2019, 12:28pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/15 "2019-12-18T12:28:15Z")

</div>

Also a crash.

I can prevent ParaView from crashing by adding a loop

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

```

But no surface (maybe expected).

If I add a loop

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

```

at the end, ParaView crashes again.

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 18, 2019, 12:45pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/16 "2019-12-18T12:45:52Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 18, 2019, 12:53pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/17 "2019-12-18T12:53:40Z")

</div>

Does not seem to crash when applying

ExplicitStructuredGridToUnstructuredGrid,

followed by an

UnstructuredGridToExplicitStructuredGrid.

Still nothing shows

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 18, 2019, 1:43pm UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/18 "2019-12-18T13:43:36Z")

</div>

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

---

<div class="post-metadata">

### Author: ![MoS2](https://discourse.paraview.org/letter_avatar_proxy/v4/letter/m/ce7236/32.png) [@MoS2](https://discourse.paraview.org/u/MoS2)
#### Post date: [December 19, 2019, 7:04am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/19 "2019-12-19T07:04:50Z")

</div>

Sure thing

[eight\_cells\_ExplicitStructuredGrid\_to\_Unstructured.vtu](https://discourse.paraview.org/uploads/short-url/1rCOAlEthgSxADB8P6hZbss306Q.vtu) (5.0 KB)

---

<div class="post-metadata">

### Author: ![mwestphal](https://discourse.paraview.org/user_avatar/discourse.paraview.org/mwestphal/32/17_2.png) [@mwestphal](https://discourse.paraview.org/u/mwestphal)
#### Post date: [December 19, 2019, 9:27am UTC](https://discourse.paraview.org/t/how-to-define-a-vtkexplicitstructuredgrid/3189/20 "2019-12-19T09:27:46Z")

</div>

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.
