Extract points and polygons for a mesh using VTK

Question:

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?

Asked By: drSlump

||

Answers:

Just as with most other vtk filters, deci.GetOuptut() 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

BTW, 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.

Answered By: tomj
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.