-
Notifications
You must be signed in to change notification settings - Fork 0
/
taubin.py
96 lines (77 loc) · 2.48 KB
/
taubin.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
import numpy as np
import vtk
from vtk.util import numpy_support
def load_file(filename):
if filename.endswith('.ply'):
reader = vtk.vtkPLYReader()
else:
reader = vtk.vtkSTLReader()
reader.SetFileName(filename)
reader.Update()
return reader.GetOutput()
def calculate_d(mesh, poly, pid):
t = 0
n = 0.0
cell_ids = vtk.vtkIdList()
p0 = np.array(poly.GetPoint(pid))
mesh.GetPointCells(pid, cell_ids)
for i in xrange(cell_ids.GetNumberOfIds()):
point_ids = vtk.vtkIdList()
mesh.GetCellPoints(cell_ids.GetId(i), point_ids)
n += 1
if point_ids.GetId(0) != pid:
p1 = np.array(poly.GetPoint(point_ids.GetId(0)))
else:
p1 = np.array(poly.GetPoint(point_ids.GetId(1)))
t = t + (p1 - p0)
return t / n
def taubin_smooth(poly, l, m, steps):
edgesfilter = vtk.vtkExtractEdges()
edgesfilter.SetInput(poly)
edgesfilter.Update()
edges = edgesfilter.GetOutput()
new_poly = vtk.vtkPolyData()
new_poly.DeepCopy(poly)
print edges.GetNumberOfPoints()
print poly.GetNumberOfPoints()
points = new_poly.GetPoints()
for s in xrange(steps):
D = {}
for i in xrange(edges.GetNumberOfPoints()):
D[i] = calculate_d(edges, new_poly, i)
for i in xrange(poly.GetNumberOfPoints()):
p = np.array(points.GetPoint(i))
pl = p + l*D[i]
nx, ny, nz = pl
points.SetPoint(i, nx, ny, nz)
D = {}
for i in xrange(edges.GetNumberOfPoints()):
D[i] = calculate_d(edges, new_poly, i)
for i in xrange(poly.GetNumberOfPoints()):
p = np.array(points.GetPoint(i))
pl = p + m*D[i]
nx, ny, nz = pl
points.SetPoint(i, nx, ny, nz)
#D = {}
#for i in xrange(edges.GetNumberOfPoints()):
#D[i] = calculate_d(edges, i)
#for i in xrange(poly.GetNumberOfPoints()):
#x, y, z = points.GetPoint(i)
#nx = x + m*D[i][0]
#ny = y + m*D[i][1]
#nz = z + m*D[i][2]
#points.SetPoint(i, nx, ny, nz)
#new_poly.SetPoints(points)
return new_poly
def main():
poly = load_file(sys.argv[1])
steps = int(sys.argv[3])
new_poly = taubin_smooth(poly, 0.5, -0.53, steps)
w = vtk.vtkSTLWriter()
w.SetFileName(sys.argv[2])
w.SetInput(new_poly)
w.SetFileTypeToBinary()
w.Write()
if __name__ == '__main__':
main()