Hi everyone
I am new to VTK.
I want to decimate a box meshed with tetrahedra and generated by GMSH. I follow the example for Python here.
However, my decimated mesh only contain boundaries of the box and 2D triangle meshes.
Could you please help me to point out what is wrong in my code?
Thank you very much
from vtk import *
def unstructured_grid_to_polydata(unstructured_grid):
# Convert unstructured grid to polydata
geo_filter = vtk.vtkGeometryFilter()
geo_filter.SetInputData(unstructured_grid)
geo_filter.Update()
polydata = geo_filter.GetOutput()
return polydata
def decimate_mesh(mesh_name):
print('=== decimate ===')
print('mesh_name:', mesh_name)
head_name, tail_name = mesh_name.split('.')
# Reader reads mesh file
reader = vtkUnstructuredGridReader()
reader.SetFileName(mesh_name)
reader.Update()
# convert unstructured grid to polydata
inputPoly = unstructured_grid_to_polydata(reader.GetOutput())
print("Before decimation\n"
"-----------------\n"
"There are " + str(inputPoly.GetNumberOfPoints()) + " points.\n"
"There are " + str(inputPoly.GetNumberOfPolys()) + " polygons.\n")
# decimate
decimate = vtkDecimatePro()
decimate.SetInputData(inputPoly)
decimate.SetTargetReduction(.10)
decimate.Update()
# decimate Polydata
decimatedPoly = vtkPolyData()
decimatedPoly.ShallowCopy(decimate.GetOutput())
print("After decimation \n"
"-----------------\n"
"There are " + str(decimatedPoly.GetNumberOfPoints()) + " points.\n"
"There are " + str(decimatedPoly.GetNumberOfPolys()) + " polygons.\n")
# export decimated mesh (does not work)
#writer = vtkUnstructuredGridWriter()
#writer.SetInputData(decimate.GetOutput())
#writer.SetFileName("{}_decimated.vtk".format(head_name))
#writer.Write()
#save .vtk polydata
writer = vtk.vtkPolyDataWriter()
writer.SetFileTypeToASCII()
writer.SetInputData(decimate.GetOutput())
writer.SetFileName("{}_decimated.vtk".format(head_name))
writer.Write()
#STL writer
stlWriter = vtkSTLWriter()
stlWriter.SetFileName("{}_decimated.stl".format(head_name))
stlWriter.SetFileTypeToBinary()
stlWriter.SetInputConnection(decimate.GetOutputPort())
stlWriter.Write()
return
if __name__ == "__main__":
load_path = 'Geometry/'
mesh_name = 'box.vtk'
decimate_mesh(load_path + mesh_name)