如何在Paraview中渲染简单的2D vtkImageData对象?

2024-10-04 15:30:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用Paraview绘制简单的2D网格,每个单元使用不同的颜色,或者每个顶点使用不同的颜色。据我所知,Paraview文档没有解释如何Show()一个用户定义的VTK对象

我从Paraview guide了解了VTK数据模型的工作原理,从VTK User's Guide了解了如何生成vtkImageData对象

据我所知,下面的代码应该生成一个10x5 2D网格的vtkImageData对象,该网格跨越[0.-10.]x[0.-5.],包含50个蓝色元素

但现在我不知道如何在Paraview中实际绘制它

from paraview import vtk
import paraview.simple as ps
import numpy as np
from paraview.vtk.util.numpy_support import numpy_to_vtk

def main():
    # create the vtkImageData object
    myMesh = vtk.vtkImageData()
    myMesh.SetOrigin(0.,0.,0.)
    myMesh.SetExtent(0,10,0,5,0,0)
    myMesh.SetSpacing(1.,1.,0.)

    # create the numpy colors for each cell
    blue = np.array([15, 82, 186], dtype=np.ubyte)  # 8 bits each [0, 255]
    npColors = np.tile(blue, (myMesh.GetNumberOfCells(), 1))

    # transform them to a vtkUnsignedCharArray
    # organized as 50 tuples of 3
    vtkColors = numpy_to_vtk(npColors, deep=1, array_type=vtk.VTK_UNSIGNED_CHAR)

    # allocate the sets of 3 scalars to each cell
    myMesh.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)  # set 3 scalars per cell
    myMesh.GetCellData().SetScalars(vtkColors)  # returns 0, the index to which
                                                # vtkColors is assigned
    # Something... generate a proxy using servermanager??

    ps.Show(myMesh?)
    ps.Interact()  # or ps.Render()

if __name__ == "__main__":
    main()

从我可以得到的是,我必须首先应用一个几何过滤器,比如vtkImageDataGeometryFilter()。但是这在paraview.vtk中不存在,只通过直接导入vtk模块

根据this VTK C++ SO question的说法,另一种选择是使用vtkMarchingSquares

无论如何,显然paraview.simple.Show只接受代理对象作为输入。这就引出了一个问题:如何从过滤的vtkImageData对象中创建代理?老实说,尽管阅读了文档,但我还没有完全掌握可视化管道是如何工作的

到目前为止,我只找到了通过Kitware examples in GitHub直接使用vtk可视化VTK对象的方法,而没有使用Paraview的更高级功能


Tags: theto对象importnumpy网格shownp
2条回答

ProgrammableSource是您想要使用的。见this examplethis

我设法使用TrivialProducer对象和方法.GetClientSideObject()渲染它。这将ParaView连接到服务器端对象

来源:the source codethe tip given by Mathieu Westphal来自ParaView支持

from paraview import simple as ps
from paraview import vtk
from paraview.vtk.util.numpy_support import numpy_to_vtk
import numpy as np

def main():
    # Create an image (this is a data object)
    myMesh = vtk.vtkImageData()
    myMesh.SetOrigin(0., 0., 0.)
    myMesh.SetSpacing(0.1, 0.1, 0.)
    myMesh.SetExtent(0, 10, 0, 5, 0, 0)

    # coloring
    blue = np.array([15, 82, 186], dtype=np.ubyte)
    # numpy colors
    scalarsnp = np.tile(blue, (myMesh.GetNumberOfCells(), 1))
    scalarsnp[[9, 49]] = np.array([255, 255, 0], dtype=np.ubyte)  # yellow

    # vtk array colors. Organized as 50 tuples of 3
    scalarsvtk = numpy_to_vtk(scalarsnp, deep=1, array_type=vtk.VTK_UNSIGNED_CHAR)
    scalarsvtk.SetName("colorsArray")
    # allocate the scalars to the vtkImageData object
    # myMesh.AllocateScalars(vtk.VTK_UNSIGNED_CHAR, 3)  # set 3 scalars per cell
    # myMesh.GetCellData().SetScalars(scalarsvtk)  # do not use this in ParaView!!
    colorArrayID = myMesh.GetCellData().AddArray(scalarsvtk)
    myMesh.GetCellData().SetActiveScalars(scalarsvtk.GetName())

    # TrivialProducer to interface ParaView to serverside objects
    tp_mesh = ps.TrivialProducer(registrationName="tp_mesh")
    myMeshClient = tp_mesh.GetClientSideObject()
    # link the vtkImageData object to the proxy manager
    myMeshClient.SetOutput(myMesh)
    tp_mesh.UpdatePipeline()

    # Filter for showing the ImageData to a plane
    mapTexture2Plane = ps.TextureMaptoPlane(registrationName="TM2P_mesh", Input=tp_mesh)

    renderViewMesh = ps.CreateView("RenderView")
    renderViewMesh.Background = [1, 1, 1]
    renderViewMesh.OrientationAxesVisibility = 0
    display = ps.Show(proxy=mapTexture2Plane, view=renderViewMesh)
    display.SetRepresentationType("Surface")
    display.MapScalars = 0  # necessary so as to not generate a colormap
    ps.Interact()  # or just ps.Render()


if __name__ == "__main__":
    main()

相关问题 更多 >

    热门问题