How to set data values ​​on vtkStructuredGrid

I am trying to fill a structured grid with an analytic field, but despite reading the vtk docs, I have not found how to actually set scalar values ​​at grid points or to specify information about the distance / origin of the grid. Starting with the code below, like me

  • associate spatial information with the grid (i.e. cell 0,0,0 is in 0,0,0 coordinate, distance dx in all directions)
  • associates scalar values ​​with each grid point. For starters, I only need one, but in the end I would like to store 3 pieces of data at each point (not a vector, 3 different scalars).
grid = vtk.vtkStructuredGrid()
numPoints = int((maxGrid - minGrid)/dx)
grid.SetDimensions(numPoints, numPoints, numPoints)
+4
source share
1 answer

VTK 3 "" , vtkImageData (vtkUniformGrid ), vtkRectilinearGrid vtkStructuredGrid. , ​​. vtkImageData , vtkRectilinearGrid , , vtkStructuredGrid ( , ).

:

from vtk import *
dx = 2.0
grid = vtkImageData()
grid.SetOrigin(0, 0, 0) # default values
grid.SetSpacing(dx, dx, dx)
grid.SetDimensions(5, 8, 10) # number of points in each direction
# print grid.GetNumberOfPoints()
# print grid.GetNumberOfCells()
array = vtkDoubleArray()
array.SetNumberOfComponents(1) # this is 3 for a vector
array.SetNumberOfTuples(grid.GetNumberOfPoints())
for i in range(grid.GetNumberOfPoints()):
    array.SetValue(i, 1)

grid.GetPointData().AddArray(array)
# print grid.GetPointData().GetNumberOfArrays()
array.SetName("unit array")
+13

All Articles