how to properly use vtkCellDataToPointData - vtk Python library

Viewed 170

Im trying to utilize the vtk Python library to write out some unstructured grids to legacy .vtk format

I am able to create the geometry, cell data and point data no problem and write them out to 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. 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..

1 Answers

figured it out.. I had to actually pass the vtkUnstructuredGrid into the cell to point filter

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

Related