如何使用PyMeshLab将顶点数减少到一定数量

How to use PyMeshLab to reduce vertex number to a certain number

我有一批三角形网格,每个都有不同的顶点和面。我想将所有网格的顶点数减少到相同的数字 10000。

我知道我可以使用simplification_quadric_edge_collapse_decimation来减少面数,这意味着顶点数将相应减少。但问题是我必须多次使用该方法才能使顶点数精确到 10000。

因此,我想知道是否有另一种方法可以将顶点数直接减少到10000?

Meshlab,以及据我所知任何其他能够简化的库,使用面数作为参数来指导简化过程。

好消息是,这两个值都与网格的 Euler characteristic 相关,粗略地说,对于没有孔的表面,顶点数是面数的一半。将网格抽取到 20000 个面将产生大约 10000 个顶点的网格,但也很容易低于 9999。由于您具有在 python 中进行编程的优势,因此您可以设计一个过程来慢慢收敛到所需的顶点数。

我们的想法是将您的网格简化为略高于 20000 的面数,然后慢慢改进您的解决方案,直到您恰好获得 10000 个顶点。我建议您使用 当前步骤的顶点多余部分 (vertex_number - 10000).

来减少每个步骤中的面数
import pymeshlab as ml
ms = ml.MeshSet()
ms.load_new_mesh('input.ply')
m = ms.current_mesh()
print('input mesh has', m.vertex_number(), 'vertex and', m.face_number(), 'faces')

#Target number of vertex
TARGET=10000

#Estimate number of faces to have 100+10000 vertex using Euler
numFaces = 100 + 2*TARGET

#Simplify the mesh. Only first simplification will be agressive
while (ms.current_mesh().vertex_number() > TARGET):
    ms.apply_filter('simplification_quadric_edge_collapse_decimation', targetfacenum=numFaces, preservenormal=True)
    print("Decimated to", numFaces, "faces mesh has", ms.current_mesh().vertex_number(), "vertex")
    #Refine our estimation to slowly converge to TARGET vertex number
    numFaces = numFaces - (ms.current_mesh().vertex_number() - TARGET)

m = ms.current_mesh()
print('output mesh has', m.vertex_number(), 'vertex and', m.face_number(), 'faces')
ms.save_current_mesh('output.ply')

请注意:

  • 有时您无法精确减少到 10000 个顶点,最终会以 9999 个顶点结束。
  • 使用此公式,每一步(在第一步之后)都会删除大约一半超过 10000 的顶点,从而“软着陆”到所需数量的顶点。典型的执行应该减少到大约 10050 个顶点,然后是 10025、10012、10006、10003、10001,最后是 10000 个顶点。最终的面数取决于输入模型的欧拉特征。
  • 只有第一个简化步骤会花费大量执行时间(取决于输入网格中三角形的数量),接下来的简化步骤会非常快。
  • 如果你仍然想speed-up你可以做numFaces = numFaces - int(1.5*(ms.current_mesh().vertex_number() - 10000))的方法,但这增加了在9999个顶点下结束的机会并且执行时间不会受到太大影响。
  • 此方法应该适用于任何基于面的抽取算法,它不专用于二次边折叠。