-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdecimate.py
More file actions
64 lines (51 loc) · 1.82 KB
/
decimate.py
File metadata and controls
64 lines (51 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import argparse
import numpy as np
import openmesh
from vispy.io import write_mesh, read_mesh, load_data_file
def decimate(mesh, n_vertices):
"""Decimate a mesh with a target number of vertices."""
decimater = openmesh.TriMeshDecimater(mesh)
mod = openmesh.TriMeshModQuadricHandle()
decimater.add(mod)
decimater.initialize()
decimater.decimate_to(n_vertices)
mesh.garbage_collection()
return mesh
def test_decimate():
mesh = openmesh.TriMesh()
# Test setup:
# 1------2\
# |\ B | \
# | \ |C 4
# | A \ | /
# 0------3/
# Add vertices
vhandle = []
vhandle.append(mesh.add_vertex(np.array([0, 0, 0])))
vhandle.append(mesh.add_vertex(np.array([0, 1, 0])))
vhandle.append(mesh.add_vertex(np.array([1, 1, 0])))
vhandle.append(mesh.add_vertex(np.array([1, 0, 0])))
vhandle.append(mesh.add_vertex(np.array([1.5, 0.5, 0])))
# Add faces
faces = [[0, 1, 3], [1, 2, 3], [2, 4, 3]]
for v in faces:
face_vhandles = []
face_vhandles.append(vhandle[v[0]])
face_vhandles.append(vhandle[v[1]])
face_vhandles.append(vhandle[v[2]])
mesh.add_face(face_vhandles)
decimated = decimate(mesh, 3)
print(decimated.n_vertices())
print(mesh.n_vertices())
assert decimated.n_vertices() == 3
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument('input_mesh')
parser.add_argument('output_mesh')
parser.add_argument('--vertices', type=int, default=6890)
args = parser.parse_args()
mesh = openmesh.read_trimesh(args.input_mesh)
decimated = decimate(mesh, args.vertices)
openmesh.write_mesh(args.output_mesh, decimated)
vertices1, faces1, normals, nothin = read_mesh("faust_ref.obj")
vertices2, faces, normals, nothin = read_mesh("testtest.obj")