-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy path04_connect_pick_events.py
176 lines (143 loc) · 5.85 KB
/
04_connect_pick_events.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
from __future__ import print_function
import os
import vtk
from vtk.qt4.QVTKRenderWindowInteractor import QVTKRenderWindowInteractor
from PyQt4 import QtCore, QtGui, uic
class GlyphViewerApp(QtGui.QMainWindow):
def __init__(self, data_dir):
#Parent constructor
super(GlyphViewerApp,self).__init__()
self.vtk_widget = None
self.ui = None
self.setup(data_dir)
def setup(self, data_dir):
import glyph_ui
self.ui = glyph_ui.Ui_MainWindow()
self.ui.setupUi(self)
self.vtk_widget = QGlyphViewer(self.ui.vtk_panel, data_dir)
self.ui.vtk_layout = QtGui.QHBoxLayout()
self.ui.vtk_layout.addWidget(self.vtk_widget)
self.ui.vtk_layout.setContentsMargins(0,0,0,0)
self.ui.vtk_panel.setLayout(self.ui.vtk_layout)
self.ui.threshold_slider.setValue(50)
self.ui.threshold_slider.valueChanged.connect(self.vtk_widget.set_threshold)
self.vtk_widget.arrow_picked.connect(self.update_magnitude)
def initialize(self):
self.vtk_widget.start()
def update_magnitude(self, magnitude):
print(magnitude)
self.ui.vector_size.setText("%.2f"%magnitude)
class QGlyphViewer(QtGui.QFrame):
arrow_picked = QtCore.pyqtSignal(float)
def __init__(self, parent, data_dir):
super(QGlyphViewer,self).__init__(parent)
# Make tha actual QtWidget a child so that it can be re parented
interactor = QVTKRenderWindowInteractor(self)
self.layout = QtGui.QHBoxLayout()
self.layout.addWidget(interactor)
self.layout.setContentsMargins(0,0,0,0)
self.setLayout(self.layout)
# Read the data
xyx_file = os.path.join(data_dir, "combxyz.bin")
q_file = os.path.join(data_dir, "combq.bin")
pl3d = vtk.vtkMultiBlockPLOT3DReader()
pl3d.SetXYZFileName(xyx_file)
pl3d.SetQFileName(q_file)
pl3d.SetScalarFunctionNumber(100)
pl3d.SetVectorFunctionNumber(202)
pl3d.Update()
blocks = pl3d.GetOutput()
b0 = blocks.GetBlock(0)
# Setup VTK environment
renderer = vtk.vtkRenderer()
render_window = interactor.GetRenderWindow()
render_window.AddRenderer(renderer)
interactor.SetInteractorStyle(vtk.vtkInteractorStyleTrackballCamera())
render_window.SetInteractor(interactor)
renderer.SetBackground(0.2,0.2,0.2)
# Draw Outline
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(b0)
outline_mapper = vtk.vtkPolyDataMapper()
outline_mapper.SetInputConnection(outline.GetOutputPort())
outline_actor = vtk.vtkActor()
outline_actor.SetMapper(outline_mapper)
outline_actor.GetProperty().SetColor(1,1,1)
renderer.AddActor(outline_actor)
renderer.ResetCamera()
# Draw Outline
outline = vtk.vtkStructuredGridOutlineFilter()
outline.SetInputData(b0)
outline_mapper = vtk.vtkPolyDataMapper()
outline_mapper.SetInputConnection(outline.GetOutputPort())
outline_actor = vtk.vtkActor()
outline_actor.SetMapper(outline_mapper)
outline_actor.GetProperty().SetColor(1,1,1)
renderer.AddActor(outline_actor)
renderer.ResetCamera()
# Threshold points
threshold = vtk.vtkThresholdPoints()
threshold.SetInputData(b0)
threshold.ThresholdByUpper(0.5)
# Draw arrows
arrow = vtk.vtkArrowSource()
glyphs = vtk.vtkGlyph3D()
glyphs.SetInputData(b0)
glyphs.SetSourceConnection(arrow.GetOutputPort())
glyphs.SetInputConnection(threshold.GetOutputPort())
glyphs.SetVectorModeToUseVector()
glyphs.SetScaleModeToScaleByVector()
glyphs.SetScaleFactor(0.005)
glyphs.SetColorModeToColorByVector()
# Mapper
glyph_mapper = vtk.vtkPolyDataMapper()
glyph_mapper.SetInputConnection(glyphs.GetOutputPort())
glyph_actor = vtk.vtkActor()
glyph_actor.SetMapper(glyph_mapper)
glyph_mapper.UseLookupTableScalarRangeOn()
renderer.AddActor(glyph_actor)
# Set color lookuptable
glyphs.Update()
s0,sf = glyphs.GetOutput().GetScalarRange()
lut = vtk.vtkColorTransferFunction()
lut.AddRGBPoint(s0, 1,0,0)
lut.AddRGBPoint(sf, 0,1,0)
glyph_mapper.SetLookupTable(lut)
# Register pick listener
self.b0 = b0
self.renderer = renderer
self.interactor = interactor
self.threshold = threshold
self.render_window = render_window
self.picker = vtk.vtkCellPicker()
self.picker.AddObserver("EndPickEvent", self.process_pick)
self.interactor.SetPicker(self.picker)
self.glyphs=glyphs
def start(self):
self.interactor.Initialize()
self.interactor.Start()
self.interactor.AddObserver(vtk.vtkCommand.LeftButtonPressEvent, self.click_to_pick, 10)
def set_threshold(self, new_value):
float_value = new_value/100.0
print(float_value)
self.threshold.ThresholdByUpper(float_value)
self.render_window.Render()
def process_pick(self, object, event):
point_id = object.GetPointId()
if point_id >= 0:
vector_magnitude = self.glyphs.GetOutput().GetPointData().GetScalars().GetTuple(point_id)
self.arrow_picked.emit(vector_magnitude[0])
def click_to_pick(self, object, event):
x, y = object.GetEventPosition()
self.picker.Pick(x,y,0, self.renderer)
if __name__ == "__main__":
os.chdir(os.path.dirname(__file__))
# Recompile ui
with open("glyph_view.ui") as ui_file:
with open("glyph_ui.py","w") as py_ui_file:
uic.compileUi(ui_file,py_ui_file)
app = QtGui.QApplication([])
main_window = GlyphViewerApp("volume")
main_window.show()
main_window.initialize()
app.exec_()