由于某种原因,我无法通过 python api 找到合理的方法来将绘图颜色重新缩放为当前相机设置。
有什么方法可以让我vtkDoubleArray
来获取GetRange()
的当前查看数据来更新我的DataMapper?
我当前代码的简化版本如下所示:
import vtk
file_name = 'foo.vtk'
field_name = "bar"
reader = vtk.vtkUnstructuredGridReader()
reader.SetFileName(file_name)
reader.ReadAllScalarsOn()
reader.ReadAllVectorsOn()
reader.Update()
output = reader.GetOutput()
numvals = 1024
ctf = vtk.vtkColorTransferFunction()
ctf.SetColorSpaceToRGB()
ctf.AddRGBPoint(-1, 1, 1, 0)
ctf.AddRGBPoint(0, 0, 0, 0)
ctf.AddRGBPoint(0.293069/1.72393, 0, 0, 1)
ctf.AddRGBPoint(0.586138/1.72393, 0, 1, 1)
ctf.AddRGBPoint(0.861967/1.72393, 0, 1, 0)
ctf.AddRGBPoint(1.155040/1.72393, 1, 1, 0)
ctf.AddRGBPoint(1.448100/1.72393, 1, 0, 0)
ctf.AddRGBPoint(1.723930/1.72393, 0.87843, 0, 1)
lut = vtk.vtkLookupTable()
lut.SetNumberOfTableValues(numvals)
lut.Build()
for i in range(0,numvals):
rgb = list(ctf.GetColor(float(i)/numvals))+[1]
lut.SetTableValue(i,rgb)
mapper = vtk.vtkDataSetMapper()
mapper.SetInputData(output)
mapper.ScalarVisibilityOn()
mapper.SetColorModeToMapScalars()
mapper.SetLookupTable(lut)
mapper.SetScalarModeToUsePointFieldData()
mapper.SelectColorArray(field_name)
mapper.SetScalarRange(output.GetPointData().GetArray(field_name).GetRange())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
camera = vtk.vtkCamera()
camera.SetPosition(0.5, 0.3, 3);
camera.SetFocalPoint(0.5, 0.3, 0);
renderer = vtk.vtkRenderer()
renderer.AddActor(actor)
renderer.SetBackground(1, 1, 1)
renderer.SetActiveCamera(camera)
render_window = vtk.vtkRenderWindow()
render_window.AddRenderer(renderer)
render_window.SetSize(800,800)
render_window.Render()
windowToImageFilter = vtk.vtkWindowToImageFilter()
windowToImageFilter.SetInput(render_window)
#windowToImageFilter.SetInputBufferTypeToRGBA()
windowToImageFilter.ReadFrontBufferOff()
windowToImageFilter.Update()
writer = vtk.vtkPNGWriter()
writer.SetFileName("screenshot.png");
writer.SetInputConnection(windowToImageFilter.GetOutputPort());
writer.Write();