我想生成一个由向量组件着色的vtkUnstructuredGrid图。我可以通过在paraview中阅读下面代码生成的vtu文件来做到这一点,我可以选择例如位移矢量的y分量。但是,我无法在python脚本中直接弄清楚如何执行此操作。通过设置变量'组件'等于0我按预期得到x分量。我似乎无法选择任何其他组件进行展示。
一种可能性可能是使用计算过滤器并创建一个新的标量数组,但我确信有一种更简单的方法。非常感谢任何帮助。
import vtk
grid = vtk.vtkUnstructuredGrid()
points = vtk.vtkPoints()
cells = vtk.vtkCellArray()
disp = vtk.vtkFloatArray()
disp.SetNumberOfComponents(3)
disp.SetName('displacement')
N = 11
delta = 10./(N-1)
for kz in range(N):
for ky in range(N):
for kx in range(N):
points.InsertNextPoint(kx*delta,ky*delta,kz*delta)
disp.InsertNextTuple3(kx,kx*ky,ky**2+kz**2)
grid.SetPoints(points)
pdata = grid.GetPointData()
pdata.AddArray(disp)
inel = [0,1,N+1,N,N**2,N**2+1,N**2+N+1,N**2+N]
for kz in range(N-1):
for ky in range(N-1):
for kx in range(N-1):
anEle = vtk.vtkHexahedron()
for kk in range(8):
anEle.GetPointIds().SetId(kk,inel[kk]+kx+N*ky+N**2*kz)
cells.InsertNextCell(anEle)
grid.SetCells(vtk.VTK_HEXAHEDRON,cells)
if 1==11:
writer = vtk.vtkXMLUnstructuredGridWriter();
writer.SetDataModeToAscii()
writer.SetFileName('bricks.vtu')
writer.SetInput(grid)
writer.Write()
else:
grid.GetPointData().SetActiveScalars('displacement')
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
# mapper
mapper = vtk.vtkDataSetMapper()
mapper.SetInput(grid)
component = 1
vrange = grid.GetPointData().GetArray(0).GetRange(component)
mapper.SetScalarRange(vrange)
# actor
actor = vtk.vtkActor()
actor.SetMapper(mapper)
p=actor.GetProperty()
p.SetEdgeVisibility(True)
# assign actor to the renderer
ren.AddActor(actor)
#########################################################################
# Define view
#########################################################################
ViewSize = [1253, 817]
CenterOfRotation = [5.0, 5.0, 5.0]
CameraPosition = [23, 20, 30]
CameraFocalPoint = [5,5,5]
CameraViewUp = [0,1,0]
CameraParallelScale = 9
Background = [0.9,0.9,0.9]
renWin.SetSize(ViewSize)
camera = ren.GetActiveCamera()
camera.SetFocalPoint(CenterOfRotation)
camera.SetPosition(CameraPosition)
camera.SetFocalPoint(CameraFocalPoint)
camera.SetViewUp(CameraViewUp)
camera.SetParallelScale(CameraParallelScale)
ren.SetBackground(Background)
#########################################################################
# Add color bar
#########################################################################
cmap = vtk.vtkDiscretizableColorTransferFunction()
cmap.AddRGBPoint(vrange[0],1,0,0)
cmap.AddRGBPoint(vrange[1],1,1,1)
cmap.SetNumberOfValues(20)
cmap.SetDiscretize(True)
cmap.SetVectorModeToComponent()
cmap.SetVectorComponent(component)
mapper.SetLookupTable(cmap)
scalarBar = vtk.vtkScalarBarActor()
scalarBar.SetLookupTable(cmap)
scalarBar.SetTitle('displacement')
ren.AddActor2D(scalarBar)
renderLarge = vtk.vtkWindowToImageFilter()
renderLarge.SetInput(renWin)
renderLarge.SetMagnification(4)
writer = vtk.vtkPNGWriter()
writer.SetInputConnection(renderLarge.GetOutputPort())
writer.SetFileName('bricks.png')
renWin.SetOffScreenRendering(True)
renWin.Render()
writer.Write()
答案 0 :(得分:0)
可能现在已经太晚了。但是,提取特定组件的一种方法是使用vtkDataArray.CopyComponent()方法。我附上一小段python代码来做你想做的事。
# initialise a new vtkFloatArray to copy any component from disp
dispSelComponent = vtk.vtkFloatArray()
N = 11
delta = 10./(N-1)
for kz in range(N):
for ky in range(N):
for kx in range(N):
points.InsertNextPoint(kx*delta,ky*delta,kz*delta)
disp.InsertNextTuple3(kx,kx*ky,ky**2+kz**2)
grid.SetPoints(points)
noTuples = disp.GetNumberOfTuples()
noComponents = disp.GetNumberOfComponents()
dispSelComponent.SetNumberOfTuples(noTuples)
dispSelComponent.SetNumberOfComponents(1)
wantedComp = 2
dispSelComponent.CopyComponent(0, disp, wantedComp)
pdata = grid.GetPointData()
pdata.AddArray(dispSelComponent)
我希望这能帮到你。