简体   繁体   English

如何正确使用 vtkCellDataToPointData - vtk Python 库

[英]how to properly use vtkCellDataToPointData - vtk Python library

Im trying to utilize the vtk Python library to write out some unstructured grids to legacy.vtk format我试图利用 vtk Python 库将一些非结构化网格写成 legacy.vtk 格式

I am able to create the geometry, cell data and point data no problem and write them out to vtk.我能够毫无问题地创建几何体、单元格数据和点数据,并将它们写到 vtk 中。

The problem I have is I want to add the functionality to take cell data and convert it to point data.我遇到的问题是我想添加获取单元格数据并将其转换为点数据的功能。

See below code:看下面的代码:

import vtk
#dfsu data
pts = df.mesh.generate_node_vertexes()
cls = df.mesh.generate_element_vertex_indexes()
cnts = df.mesh.generate_element_vertex_count()
#vtk instantiate
points = vtk.vtkPoints()
cell = vtk.vtkCellArray()
mesh_1 = vtk.vtkUnstructuredGrid()
#loop through points
for pt in pts:
    points.InsertNextPoint(pt)
#set points
mesh_1.SetPoints(points)
#loop through cells
for cl, cnt in zip(cls, cnts):
    if cnt == 3:
        tri = vtk.vtkTriangle()
        for i,j in enumerate(cl):
            tri.GetPointIds().SetId(i, j)
        mesh_1.InsertNextCell(tri.GetCellType(), tri.GetPointIds())
    elif cnt == 4:
        quad = vtk.vtkQuad()
        for i,j in enumerate(cl):
            quad.GetPointIds().SetId(i,j)
        mesh_1.InsertNextCell(quad.GetCellType(), quad.GetPointIds())
#add cell data
arr = vtk.vtkDoubleArray()
arr.SetName('test1')
for i in range(len(cls)):
    arr.InsertNextTuple([0.5])
arr1 = vtk.vtkDoubleArray()
arr1.SetName('test2')
for i in range(len(cls)):
    arr1.InsertNextTuple([0.25])
mesh_1.GetCellData().AddArray(arr)
mesh_1.GetCellData().AddArray(arr1)

#convert to point data - THIS IS THE PART I CANT FIGURE OUT!
c2p = vtk.vtkCellDataToPointData()
c2p.SetInputData(arr)

#add point data
##here i want to add the converted point data
##

#write
writer = vtk.vtkUnstructuredGridWriter()
writer.SetFileName('test.vtk')
writer.SetInputData(mesh_1)
writer.Write()

I'm really new to VTK and it's a bit confusing.我真的是 VTK 的新手,有点困惑。 I can't figure out how to take my cell data and convert it to point data.我不知道如何获取我的单元格数据并将其转换为点数据。

I've tried:我试过了:

c2p.SetInputData(arr)
c2p.SetInputData(mesh_1.GetCellData().GetArray(0))

and a bunch of other random commands, really can't figure out how to do it.和一堆其他随机命令,真的不知道该怎么做。

any suggestions are appreciated - I've seen a ton of examples but were slightly different than what I am trying to do..任何建议表示赞赏 - 我已经看到了很多例子,但与我正在尝试做的略有不同..

figured it out.. I had to actually pass the vtkUnstructuredGrid into the cell to point filter想通了..我实际上必须将vtkUnstructuredGrid传递到单元格中以指向过滤器

c2p = vtk.vtkCellDataToPointData()
c2p.SetInputData(mesh_1)
c2p.Update()
ptdata = c2p.GetOutput()

this outputs another vtkUnstructuredGrid object with the cell data converted to point data, which I can then pass into the writer这将输出另一个vtkUnstructuredGrid object,其中单元格数据已转换为点数据,然后我可以将其传递给编写器

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM