简体   繁体   English

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

[英]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')

毕达哥拉斯模型由 Geoffrey Marchal 提供

Please, note that:请注意:

  • Sometimes you just can't reduce exactly to 10000 vertex, and will end with 9999 vertex.有时你不能精确地减少到 10000 个顶点,并且会以 9999 个顶点结束。
  • With this formula, each step (after first one) will remove about half of the vertices exceeding 10000, giving a "soft landing" into your desired number of vertices.使用此公式,每一步(在第一个步骤之后)将删除大约一半超过 10000 个的顶点,从而“软着陆”到您想要的顶点数量。 A typical execution should reduce to about 10050 vertices, then 10025, 10012, 10006, 10003, 10001, and finally 10000 vertices.典型的执行应该减少到大约 10050 个顶点,然后是 10025、10012、10006、10003、10001,最后是 10000 个顶点。 The final number of faces depends of the said Euler Characteristic of the input model.最终的面数取决于输入 model 的所述欧拉特性。
  • Only the first simplification step will take significant execution time (depending of number of triangles in the input mesh), and next simplification steps will be very fast.只有第一个简化步骤将花费大量执行时间(取决于输入网格中三角形的数量),接下来的简化步骤将非常快。
  • If you still want to speed-up the method you can do 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 顶点下结束的机会并赢得执行时间不会受到太大影响。
  • This method should work with any decimation algorithm based on faces, it is not exclusive for quadric edge collapse.这种方法应该适用于任何基于面的抽取算法,它不只适用于二次边折叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM