[英]How to use PyMeshLab to reduce vertex number to a certain number
I have a batch of triangle meshes and each one has different vertices and faces.我有一批三角形网格,每个都有不同的顶点和面。 I want to reduce the vertex numbers of all the meshes to the same number, 10000.
我想将所有网格的顶点数减少到相同的数字,10000。
I know I can use simplification_quadric_edge_collapse_decimation to reduce the face number which means the vertex number will be reduced accordingly.我知道我可以使用simplification_quadric_edge_collapse_decimation来减少面数,这意味着顶点数将相应减少。 But the problem is that I have to use the method several times in order to get the vertex number exactly down to 10000.
但问题是我必须多次使用该方法才能将顶点数精确到 10000。
Therefore, I'm wondering if there is another way to reduce the vertex number to 10000 directly?因此,我想知道是否有另一种方法可以直接将顶点数减少到10000?
Meshlab, and to my knowledge any other libraries able to simplify, use the face number as the parameter to guide simplification process. Meshlab,据我所知,任何其他能够简化的库都使用面数作为参数来指导简化过程。
The good news are that both values are related by the Euler characteristic of the mesh, which roughly says that the number of vertex is half the number of faces for a surface without holes.好消息是这两个值都与网格的欧拉特性有关,它粗略地说顶点数是没有孔的表面的面数的一半。 Decimating your mesh to 20000 faces will produce a mesh of about 10000 vertex, but you can also fall under 9999 easily.
将您的网格减少到 20000 个面将生成大约 10000 个顶点的网格,但您也可以轻松地低于 9999。 As you have the advantage of being programming in python, you can design a process to converge to a number of vertex.
由于您具有在 python 中编程的优势,您可以设计一个收敛到多个顶点的过程。
So, the idea is to simplify your mesh to a face number slightly above of 20000, and then slowly refine your solution until you get 10000 vertex.因此,我们的想法是将您的网格简化为面数略高于 20000,然后慢慢细化您的解决方案,直到获得 10000 个顶点。 I propose you to reduce the number of faces in each step using the excess of vertex on current step (vertex - 10000).
我建议您使用当前步骤(顶点 - 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')
Please, note that:请注意:
numFaces = numFaces - int(1.5*(ms.current_mesh().vertex_number() - 10000))
, but this increases the chances of ending under 9999 vertex and execution time won't be affected much.numFaces = numFaces - int(1.5*(ms.current_mesh().vertex_number() - 10000))
,但这会增加在 9999 顶点下结束的机会并赢得执行时间不会受到太大影响。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.