forked from limjiayi/RECONSTRUCT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
display_vtk.py
296 lines (242 loc) · 7.67 KB
/
display_vtk.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
"""
VTK shortcut functions
For more about VTK, visit:
http://www.vtk.org/
http://www.vtk.org/Wiki/VTK/Examples/Python
"""
import vtk
import sys
import numpy
from vtk.util.colors import peacock
def vtk_point_cloud(points, colors=[], point_size=2):
"""
Represent a point cloud in VTK
Parameters
----------
points : numpy array, each row is a point
colors : list of colors, one per point
point_size : rendering size for the points
Returns
-------
actor : vtkActor representing the point cloud
"""
nb = len(points);
vtk_points = vtk.vtkPoints();
vtk_verts = vtk.vtkCellArray();
if colors:
vtk_colors = vtk.vtkUnsignedCharArray();
vtk_colors.SetNumberOfComponents(3);
vtk_colors.SetName( "Colors");
for i in range(0,nb):
p = points[i]
if len(p) >= 3:
coords = [p[0],p[1],p[2]]
elif len(p) == 2:
coords = [p[0],p[1],0]
elif len(p) == 1:
coords = [p[0],0,0]
else:
print "**ERROR** wrong dimension"
sys.exit(1)
id = vtk_points.InsertNextPoint( *coords )
vtk_verts.InsertNextCell(1)
vtk_verts.InsertCellPoint(id)
if colors:
vtk_colors.InsertNextTuple3( *colors[i] )
poly = vtk.vtkPolyData()
poly.SetPoints(vtk_points)
poly.SetVerts(vtk_verts)
if colors:
poly.GetPointData().SetScalars(vtk_colors)
poly.Update()
mapper = vtk.vtkPolyDataMapper()
mapper.SetInput(poly)
actor = vtk.vtkActor()
actor.SetMapper(mapper)
actor.GetProperty().SetRepresentationToPoints
actor.GetProperty().SetPointSize( point_size )
return actor
def vtk_basic( actors ):
"""
Create a window, renderer, interactor, add the actors and start the thing
Parameters
----------
actors : list of vtkActors
Returns
-------
nothing
"""
# create a rendering window and renderer
ren = vtk.vtkRenderer()
renWin = vtk.vtkRenderWindow()
renWin.AddRenderer(ren)
renWin.SetSize(600,600)
# ren.SetBackground( 1, 1, 1)
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
for a in actors:
# assign actor to the renderer
ren.AddActor(a )
#enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
def vtk_Nviews( actors ):
"""
Create a window, an interactor and one renderer per actor
Parameters
----------
actors : list of vtkActors
Returns
-------
nothing
"""
N = len(actors)
# create a rendering window and renderers
renderers = [vtk.vtkRenderer() for i in range(N)]
renWin = vtk.vtkRenderWindow()
renWin.SetSize( 600, 600 )
for i in range(N):
# split the viewport
renderers[i].SetViewport(0,float(N-i-1)/N,1,float(N-i)/N)
renderers[i].SetBackground( 1, 1, 1)
renderers[i].AddActor( actors[i] )
renWin.AddRenderer(renderers[i])
# create a renderwindowinteractor
iren = vtk.vtkRenderWindowInteractor()
iren.SetRenderWindow(renWin)
#enable user interface interactor
iren.Initialize()
renWin.Render()
iren.Start()
def vtk_show_points( points, colors=[] ):
"""
Display a point cloud
Parameters
----------
points : numpy array, each row is a point
colors : list of colors, one per point
Returns
-------
nothing
"""
point_cloud = vtk_point_cloud(points,colors)
vtk_basic( [point_cloud] )
def vtk_colored_graph(points, edges, colors=[], line_width=2):
"""
Represent a graph in VTK
Parameters
----------
points : numpy array, each row is a point
edges : numpy array of edges, each row is of the form
[ point_1, point_2, distance ]
colors : list of colors, one per point
line_width : rendering size for the lines
Returns
-------
actor : vtkActor representing the graph
"""
nb_points = len(points)
vtk_points = vtk.vtkPoints()
vtk_lines = vtk.vtkCellArray()
vtk_colors = vtk.vtkUnsignedCharArray()
vtk_colors.SetNumberOfComponents(3)
vtk_colors.SetName( "Colors")
if (len(colors) ==0):
for i in range(0,len(edges)):
colors.append((0, 164, 180))
for i in range(0,nb_points):
p = points[i]
if len(p) >= 3:
coords = [p[0],p[1],p[2]]
elif len(p) == 2:
coords = [p[0],p[1],0]
elif len(p) == 1:
coords = [p[0],0,0]
else:
print "**ERROR** wrong dimension"
sys.exit(1)
id = vtk_points.InsertNextPoint( *coords )
for i in range(0,len(edges)):
line = vtk.vtkLine()
line.GetPointIds().SetId(0,edges[i][0])
line.GetPointIds().SetId(1,edges[i][1])
vtk_lines.InsertNextCell(line)
vtk_colors.InsertNextTuple3( *colors[i] )
poly = vtk.vtkPolyData()
poly.SetPoints(vtk_points)
poly.SetLines(vtk_lines)
poly.GetCellData().SetScalars(vtk_colors);
poly.Update()
cleaner = vtk.vtkCleanPolyData()
cleaner.SetInput(poly)
tubes = vtk.vtkTubeFilter()
tubes.SetInputConnection(cleaner.GetOutputPort())
tubes.SetRadius(0.1)
tubes.SetNumberOfSides(6)
mapEdges = vtk.vtkPolyDataMapper()
mapEdges.SetInputConnection(tubes.GetOutputPort())
edgeActor = vtk.vtkActor()
edgeActor.SetMapper(mapEdges)
edgeActor.GetProperty().SetSpecularColor(1, 1, 1)
edgeActor.GetProperty().SetSpecular(0.3)
edgeActor.GetProperty().SetSpecularPower(20)
edgeActor.GetProperty().SetAmbient(0.2)
edgeActor.GetProperty().SetDiffuse(0.8)
return edgeActor
def vtk_triangles(points, triangles, colors=[]):
"""
Display triangles in VTK
Parameters
----------
points : numpy array, each row is a point
triangle : numpy array of vertices, each row is of the form
[ point_1, point_2, point_3 ]
colors : list of colors, one per triangle
Returns
-------
actor : vtkActor representing the triangles
"""
nb_points = len(points)
vtk_points = vtk.vtkPoints()
vtk_triangles = vtk.vtkCellArray()
vtk_colors = vtk.vtkUnsignedCharArray()
vtk_colors.SetNumberOfComponents(3)
vtk_colors.SetName( "Colors")
if (len(colors) ==0):
for i in range(0,nb_points):
vtk_colors.InsertNextTuple3(0, 164, 180)
else:
for i in range(0,nb_points):
vtk_colors.InsertNextTuple3( *colors[i] )
for i in range(0,nb_points):
p = points[i]
if len(p) >= 3:
coords = [p[0],p[1],p[2]]
elif len(p) == 2:
coords = [p[0],p[1],0]
elif len(p) == 1:
coords = [p[0],0,0]
else:
print "**ERROR** wrong dimension"
sys.exit(1)
id = vtk_points.InsertNextPoint( *coords )
for i in range(0,len(triangles)):
triangle = vtk.vtkTriangle()
triangle.GetPointIds().SetId(0,triangles[i][0])
triangle.GetPointIds().SetId(1,triangles[i][1])
triangle.GetPointIds().SetId(2,triangles[i][2])
vtk_triangles.InsertNextCell(triangle)
poly = vtk.vtkPolyData()
poly.SetPoints(vtk_points)
poly.SetPolys(vtk_triangles)
poly.GetPointData().SetScalars(vtk_colors)
poly.Update()
cleaner = vtk.vtkCleanPolyData()
cleaner.SetInput(poly)
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(cleaner.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
return actor