我有一个高分辨率的三角形网格,约有 200 万个三角形。我想将三角形和顶点的数量减少到大约 10000 个,同时尽可能保留其总体形状。
我知道这可以在Matlab 中使用reducepatch 完成。另一种选择是 qslim 软件包。 VTK 中也有抽取功能,它有 python 接口,所以从技术上来说,在 python 中也是可能的。 Meshlab 可能也可以在 python 中使用(?)。
如何在Python中进行这种网格抽取?示例将不胜感激。
这是从 C++ 等效 vtk 示例翻译而来的最小 Python 原型(http://www.vtk.org/Wiki/VTK/Examples/Cxx/Meshes/Decimation),正如 MrPedru22 所建议的那样。
from vtk import (vtkSphereSource, vtkPolyData, vtkDecimatePro)
def decimation():
sphereS = vtkSphereSource()
sphereS.Update()
inputPoly = vtkPolyData()
inputPoly.ShallowCopy(sphereS.GetOutput())
print("Before decimation\n"
"-----------------\n"
"There are " + str(inputPoly.GetNumberOfPoints()) + "points.\n"
"There are " + str(inputPoly.GetNumberOfPolys()) + "polygons.\n")
decimate = vtkDecimatePro()
decimate.SetInputData(inputPoly)
decimate.SetTargetReduction(.10)
decimate.Update()
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")
if __name__ == "__main__":
decimation()
我建议您使用vtkQuadricDecimation,输出模型的质量在视觉上比使用vtkDecimatePro(没有适当设置)更好。
decimate = vtkQuadricDecimation()
decimate.SetInputData(inputPoly)
decimate.SetTargetReduction(0.9)
最重要的事情之一是在保存STL时使用二进制表示:
stlWriter = vtkSTLWriter()
stlWriter.SetFileName(filename)
stlWriter.SetFileTypeToBinary()
stlWriter.SetInputConnection(decimate.GetOutputPort())
stlWriter.Write()
另一种选择是应用开源库 MeshLib,它可以从 C++ 和 Python 代码中调用(由
pip
安装)。
抽取代码看起来像这样
import meshlib.mrmeshpy as mr
# load high-resolution mesh:
mesh = mr.loadMesh(mr.Path("busto.stl"))
# decimate it with max possible deviation 0.5:
settings = mr.DecimateSettings()
settings.maxError = 0.5
result = mr.decimateMesh(mesh, settings)
print(result.facesDeleted)
# 708298
print(result.vertsDeleted)
# 354149
# save low-resolution mesh:
mr.saveMesh(mesh, mr.Path("simplified-busto.stl"))
使用 meshlab(主要是 MeshlabXML 库)的最优雅、最漂亮的 Python 抽取工具可以在 Hussein Bakri 博士的存储库中找到 https://github.com/HusseinBakri/3DMeshBulkSimplification
我一直用它。看一下代码