Problem displaying Structured Grid when loading from Programmable Source

Hi,

I am trying to make a file reader for a structured grid file that is not currently supported, but I can read it into a numpy array.
To start simple I tried to make a programmable source which just creates a cube (Output Data Set Type: vtkStructuredGrid)

import numpy as np

# Cube
xs = np.linspace(0,1,10)
ys = np.linspace(0,1,10)
zs = np.linspace(0,1,10)
dims = [len(xs),len(ys),len(zs)]

# Setting S-Grid extent
output.SetExtent([0,len(xs)-1,0,len(ys)-1,0,len(zs)-1])

# VTK points
points = vtk.vtkPoints()
points.Allocate(dims[0] * dims[1] * dims[2])

# Adding grid points 
for iz, z in enumerate(zs):
    for iy, y in enumerate(ys):        
        for ix, x in enumerate(xs):
            fi = iz*len(xs)*len(ys)+iy*len(xs)+ix # Flat index
            points.InsertPoint(fi,[x,y,z])
            
# Adding points to vtkStructuredGrid
output.SetPoints(points)

Which does not through any error. From the information tab I find:


Number of Cells: 729
Number of Points: 1000


As expected. But I do not get any output in the RenderView.
Does anybody have an idea why?

I tried to create a table with the following script (Output Data Set Type: vtkTable):

import numpy as np

# Cube
xs = np.linspace(0,1,10)
ys = np.linspace(0,1,10)
zs = np.linspace(0,1,10)

# Flattining cube in index
out = np.zeros([len(xs)*len(ys)*len(zs),3])
for iz, z in enumerate(zs):
    for iy, y in enumerate(ys):        
        for ix, x in enumerate(xs):
            fi = iz*len(xs)*len(ys)+iy*len(xs)+ix # Flat index
            out[xis,:] = [x,y,z]

# Adding points to vtkTable
output.RowData.append(out[:,0],"x")
output.RowData.append(out[:,1],"y")
output.RowData.append(out[:,2],"z")

It creates the expected table. Then using the Table to Structured Grid filter to make it into a grid. This way works with out any problem.

When comparing the two methods above from the information tab the only difference I find is that the memory usage is different:


Memory (StructuredGrid): 0.012 MB
Memory (Table): 0.024 MB


I would love to hear if anybody has an idea for what I am missing?!

1 Like

There are two parts you must program. See page 167-168 of the ParaView Guide.
You are missing a script for the RequestInformation part. It basically advertizes what the source can produce. This is where you set the dimensions of your grid.

executive = self.GetExecutive()
outInfo = executive.GetOutputInformation(0)
dims = [10, 10, 10] # x-dims, y-dims, z-dims
outInfo.Set(executive.WHOLE_EXTENT(), 0, dims[0]-1 , 0, dims[1]-1 , 0, dims[2]-1)

Then the execution part called “Script” is a modified version of your original test

import numpy as np
executive = self.GetExecutive()
outInfo = executive.GetOutputInformation(0)
exts = [executive.UPDATE_EXTENT().Get(outInfo, i) for i in range(6)]
dims=[exts[1]+1, exts[3]+1, exts[5]+1]
#Cube
xs = np.linspace(0,1,dims[0])
ys = np.linspace(0,1,dims[1])
zs = np.linspace(0,1,dims[2])
#Setting S-Grid extent
output.SetExtent(exts)
vtk points
points = vtk.vtkPoints()
points.Allocate(dims[0] * dims[1] * dims[2])
#Adding grid points
for iz, z in enumerate(zs):
    for iy, y in enumerate(ys):
        for ix, x in enumerate(xs):
            fi = iz*len(xs)len(ys)+iylen(xs)+ix # Flat index
            points.InsertPoint(fi,[x,y,z])
#Adding points to vtkStructuredGrid
output.SetPoints(points)

2 Likes

Thank you so much Jean. It works right out of the box. (except that for some reason the multiplication (*) is lost for the flat index (fi), it might be that if you make the code cell, a python code cell it would look and copy as expected)

With that said, it seems that this relatively simple example is fairly convoluted and I do not understand why RequestInformation is needed. I did look at chapter 12 in the ParaView Guide. Taking example 12.2.3 Reading a CSV file with particles no RequestInformation is needed and the example seems to be similar.
Is it because it is required by the vtkStructuredGrid to set WHOLE_EXTENT?
Could you point me to any documentation about this?