Hi everyone:
I’m a beginner of VTK and want to use vtkStructuredGrid to plot a 2d numpy.ndarray data. The size is 768 x 1201, but there are some problems in my code:
where the real image should be like:
my code is:
def sliceToStructuredGrid3(self, pos: []):
self._tmp_point_data_array = numpy_support.numpy_to_vtk(num_array=self._data[pos, :, :].ravel(), deep=False,
array_type=vtk.VTK_FLOAT)
self._point_data_array = vtk.vtkFloatArray()
self._point_data_array.SetNumberOfComponents(1)
self._point_data_array.SetName("Scalars")
self._point_data_array.SetArray(self._tmp_point_data_array, 1 * self._num_n2 * self._num_n1, 1)
self._tmp_point_array = (
(np.column_stack(np.where(np.ones_like(self._data[pos, :, :]))) + 0) * 1 * 1).astype(
np.float32)
self._point_array = vtk.vtkFloatArray()
self._point_array.SetNumberOfComponents(3)
self._point_array.SetNumberOfTuples(self._tmp_point_array.shape[0])
self._point_array.SetArray(self._tmp_point_array, 1 * self._num_n2 * self._num_n1 * 3, 1)
point_set = vtk.vtkPoints()
point_set.SetData(self._point_array)
patch_structured_grid = vtk.vtkStructuredGrid()
patch_structured_grid.SetDimensions(1, self._num_n2, self._num_n1)
patch_structured_grid.SetPoints(point_set)
patch_structured_grid.GetPointData().AddArray(self._point_data_array)
patch_structured_grid.GetPointData().SetActiveScalars("Scalars")
return patch_structured_grid
slice = block.sliceToStructuredGrid3([200])
lut = vtk.vtkLookupTable()
lut.SetNumberOfColors(256)
lut.SetRange(0, 7000)
lut.Build()
mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(slice)
mapper.SetLookupTable(lut)
mapper.SetScalarRange(0, 7000)
actor = vtk.vtkActor()
polyProperty = vtk.vtkProperty()
polyProperty.SetInterpolationToPBR()
actor.SetProperty(polyProperty)
actor.SetMapper(mapper)
renderer = vtk.vtkRenderer()
renderer.UseFXAAOn()
renderer.GetUseFXAA()
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window_interactor = vtk.vtkRenderWindowInteractor()
render_window_interactor.SetRenderWindow(render_window)
renderer.AddActor(actor)
renderer.ResetCamera()
render_window.Render()
render_window_interactor.Start()
what should I do to plot the data correctly?
thanks for everyone’s help.