pythonvtk

Extract points and polygons for a mesh using VTK


I want to reduce the number of triangles in a mesh (STL file). Here is my code:

import vtk

filename = 'E://stl_file.stl'
reader = vtk.vtkSTLReader()
reader.SetFileName('filename.stl')

##convert polygonal mesh into triangle mesh
tri = vtk.vtkTriangleFilter()
tri.SetInputConnection(reader.GetOutputPort());

##decimate triangle
deci = vtk.vtkDecimatePro()
deci.SetInputConnection(tri.GetOutputPort())
deci.SetTargetReduction(0.9)
deci.PreserveTopologyOn()

it seems to work (at least it runs without errors). Now how can I extract points and triangles of the mesh?


Solution

  • Just as with most other vtk filters, deci.GetOutput() will give you the result, which in this case should be a vtkPolyData that is a decimated version of your input mesh. You can get the points from the output object by output.GetPoints(), triangles by output.GetPolys() etc., see the documentation pages at http://www.vtk.org/doc/nightly/html/classvtkPolyData.html

    By the way, there is a whole page with examples of VTK filters that would have given you the anwer, e.g. http://www.vtk.org/Wiki/VTK/Examples/Cxx/Meshes/Decimation. It's in C++ but it works in python the same way.