VTK 中平滑 Mesh 的方法 - vtkWindowedSincPolyDataFilter

1. 背景:

VTK 中有比较多的平滑 Mesh 的方法,其中比较有效的方法是:vtkWindowedSincPolyDataFilter

2. 代码:

输入的是 STL 格式的 Mesh

import vtk

def smooth_mesh(mesh_path, smooth_factor=0.2, smooth_iterations=50):
    """ smooth mesh """
    reader = vtk.vtkSTLReader()
    reader.SetFileName(mesh_path)
    reader.Update()

    pass_band = pow(10.0, -4.0 * smooth_factor)
    smooth = vtk.vtkWindowedSincPolyDataFilter()
    smooth.SetInputConnection(reader.GetOutputPort())
    smooth.SetNumberOfIterations(smooth_iterations)
    smooth.SetPassBand(pass_band)
    smooth.BoundarySmoothingOff()
    smooth.FeatureEdgeSmoothingOff()
    smooth.NonManifoldSmoothingOn()
    smooth.NormalizeCoordinatesOn()
    smooth.Update()

    # 保存成 stl 格式
    writer = vtk.vtkSTLWriter()
    writer.SetInputData(smooth.GetOutput())
    writer.SetFileTypeToBinary()
    writer.SetFileName(r'{}.stl'.format(mesh_path))
    writer.Update()


if __name__ == '__main__':

    stl_path = r'D:\data\mesh.stl'
    smooth_mesh(stl_path, smooth_factor=0.2, smooth_iterations=100)
    print('save end')

3. 最后结果:

最后的结果,显示还不错;

你可能感兴趣的:(Python实用源码,VTK,三方库,vtk,WindowedSinc,Mesh,平滑)