forked from lgo33/PyGadTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pysphviewer.py
executable file
·273 lines (229 loc) · 9.06 KB
/
pysphviewer.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
#!/usr/bin/env python
from embeddedIpython import QIPythonWidget
import pyqtgraph as pg
from PyQt4 import QtGui, QtCore
#from pyqtgraph.Qt import QtCore, QtGui
import pyqtgraph.console
from pyqtgraph.widgets.MatplotlibWidget import MatplotlibWidget
import numpy as np
import pyqtgraph.opengl as gl
from astro import *
from sphvis import *
from optparse import OptionParser
import matplotlib
import numpy.linalg
import OpenGL.GL
from PIL import _imaging
from functools import partial
VERSION = "0.1"
from pyqtgraph.dockarea import *
class myGLViewWidget(gl.GLViewWidget):
"""
main drawing window, shows particle data, controls viewport Rotation
"""
def __init__(self, **kwargs):
gl.GLViewWidget.__init__(self, **kwargs)
self.reset()
self.initview = np.array([[ 1., 0., 0.],
[ 0., 0., 1.],
[ 0., -1., 0.]])
def reset(self):
self.opts['azimuth'] = -90
self.opts['elevation'] = 00
# self.opts['azimuth'] = 45
# self.opts['elevation'] = 30
self.opts['center'] = QtGui.QVector3D(0.0, 0.0, 0.0)
self.opts['fov'] = 60
self.opts['distance'] = 30
self.update()
def rotateXZ(self):
self.opts['azimuth'] = -90
self.opts['elevation'] = 00
self.update()
def rotateXY(self):
self.opts['azimuth'] = -90
self.opts['elevation'] = 90
self.update()
def rotateYZ(self):
self.opts['azimuth'] = 0
self.opts['elevation'] = 00
self.update()
def currentView(self):
return np.dot(OpenGL.GL.glGetFloatv(OpenGL.GL.GL_MODELVIEW_MATRIX)[:3, :3], self.initview)
class popupPlot(QtGui.QDialog):
"""
matplotlib plot window to show graphs
"""
def __init__(self, parent=None):
super(popupPlot, self).__init__()
class mainWindow(QtGui.QMainWindow):
"""
main Window, parent to GL Viewport and UI
"""
def __init__(self, load=None, loadsnapshot=None, options={}, **kwargs):
super(mainWindow, self).__init__()
self._setupUI()
if load is not None:
self.loaddata_gadget(load)
if loadsnapshot is not None:
self.loadsnapshot_gadget(loadsnapshot, basic=options.basic)
def _setupUI(self):
area = DockArea()
self.setCentralWidget(area)
self.resize(1000,800)
self.setWindowTitle('Gadget Visualization')
self._setupMenu()
## Create docks, place them into the window one at a time.
## Note that size arguments are only a suggestion; docks will still have to
## fill the entire dock area and obey the limits of their internal widgets.
self.dock1 = Dock("Main Window", size=(800, 800))
self.dock2 = Dock("Console", size=(800,200))
self.dock3 = Dock("View", size=(200,800))
self.dock4 = Dock("Options", size=(200,800))
self.dock1.hideTitleBar()
self.dock2.hideTitleBar()
# dock3.hideTitleBar()
area.addDock(self.dock1, 'left')
area.addDock(self.dock3, 'right', self.dock1)
area.addDock(self.dock4, 'top', self.dock3)
area.addDock(self.dock2, 'bottom', self.dock1)
area.moveDock(self.dock3, 'above', self.dock4)
## Add widgets into each dock
# self.sn = None
self.filePath = '.'
self.sn = snapshot()
self.vis = sphvis()
## add GL Viewport
self.ViewWidget = myGLViewWidget()
self.ViewWidget.addItem(self.vis)
self.dock1.addWidget(self.ViewWidget)
## setup embedded IPython terminal
self._setupIpythonWidget()
## setup control widget
self._setupControls()
def _setupIpythonWidget(self):
namespace={'pg': pg, 'np': np, 'vis': self.vis, 'sn': self.sn, 'vw': self.ViewWidget, 'main': self }
#w2 = pg.console.ConsoleWidget(namespace=namespace)
self.ipythonWidget = QIPythonWidget()
self.ipythonWidget.pushVariables(namespace)
self.dock2.addWidget(self.ipythonWidget)
self.ipythonWidget.execute("%pylab", True, False)
welcomeText = "\nPySPHViewer v%s\n" % (VERSION) \
+ "The data of the currently visible Snapshot can be accessed with the 'sn' object\n" \
+ "import numpy as np, pyqtgraph as pg\n" \
+ "\n"
self.ipythonWidget.printText(welcomeText)
def _setupControls(self):
w3 = pg.LayoutWidget()
self.dock3.addWidget(w3)
label = QtGui.QLabel("")
w3.addWidget(label)
w3.nextRow()
##buttons
buttons = [('Reset', self.ViewWidget.reset, 'r', -1),
('Save Rotation', self.saveCurrentView, None, -1),
('X-Z', self.ViewWidget.rotateXZ, 'c', None),
('X-Y', self.ViewWidget.rotateXY, 'v', None),
('Y-Z', self.ViewWidget.rotateYZ, 'b', 1),
]
for text, action, shortcut, cs in buttons:
btn = QtGui.QPushButton(text)
btn.setMinimumWidth(15)
if cs:
w3.addWidget(btn, colspan=cs)
w3.nextRow()
else:
w3.addWidget(btn)
if shortcut:
btn.setShortcut(shortcut)
btn.clicked.connect(action)
w4 = pg.LayoutWidget()
self.dock3.addWidget(w4)
## Checkboxes to select particle types to be displayed
for t in range(self.vis.ntypes):
cb = QtGui.QCheckBox(self.vis.typenames[t])
cb.toggle()
cb.stateChanged.connect(partial(self.vis.changeDrawTypes, ptype=t))
w4.addWidget(cb)
w4.nextRow()
sliders = [
("Pointsize", np.sqrt(self.vis.mastersize)*25, self.vis.sizeChange, (0,100)),
("Alpha", self.vis.lum * 100, self.vis.alphaChange, (0,100)),
("Step", self.vis.step, self.vis.stepChange, (1,100)),
]
for text, val, slot, srange in sliders:
label = QtGui.QLabel("%16s" % text)
w4.addWidget(label)
sl = QtGui.QSlider(QtCore.Qt.Horizontal)
sl.setRange(*srange)
sl.setValue(val)
w4.addWidget(sl)
w4.nextRow()
sl.valueChanged.connect(slot)
label = QtGui.QLabel("")
w4.addWidget(label)
def initpopup(self):
self.popupPlot = popupPlot()
vbox = QtGui.QVBoxLayout()
self.popupPlot.setLayout(vbox)
self.popupPlot.show()
self.mw = MatplotlibWidget()
vbox.addWidget(self.mw)
self.subplot = self.mw.getFigure().add_subplot(111)
self.subplot.plot(np.arange(10))
self.mw.draw()
def _setupMenu(self):
openFile = QtGui.QAction('&Open File', self)
openFile.setShortcut('Ctrl+O')
openFile.setStatusTip('Open new Gadget File')
openFile.triggered.connect(self.openGadget)
popupAction = QtGui.QAction('&PlotPopup', self)
popupAction.setStatusTip('Exit application')
popupAction.triggered.connect(self.initpopup)
exitAction = QtGui.QAction('&Exit', self)
exitAction.setShortcut('Esc')
exitAction.setStatusTip('Exit application')
exitAction.triggered.connect(QtGui.qApp.quit)
self.statusBar()
menubar = self.menuBar()
fileMenu = menubar.addMenu('&File')
fileMenu.addAction(openFile)
fileMenu.addAction(exitAction)
self.toolbar = self.addToolBar('Exit')
self.toolbar.addAction(openFile)
self.toolbar.addAction(popupAction)
self.toolbar.addAction(exitAction)
def fileSelect(self):
fname = QtGui.QFileDialog.getOpenFileName(self, 'Open file', self.filePath)
if fname:
self.filePath = QtCore.QFileInfo(fname).path()
return fname
def openGadget(self):
fname = self.fileSelect()
if fname:
self.loadsnapshot_gadget(fname)
def saveCurrentView(self):
fname = self.fileSelect()
with (open(fname, 'w')) as f:
self.ViewWidget.currentView().transpose().astype(np.float64).tofile(f)
def loadsnapshot_gadget(self, fname, **kwargs):
self.sn.readgadget(fname, **kwargs)
self.loaddata_gadget(self.sn)
def loaddata_gadget(self, *args):
self.vis.loaddata_gadget(*args)
self.statusBar().showMessage('Snapshot loaded')
## Start Qt event loop unless running in interactive mode or using pyside.
if __name__ == '__main__':
import sys
if (sys.flags.interactive != 1) or not hasattr(QtCore, 'PYQT_VERSION'):
parser = OptionParser()
parser.add_option("-b", "--basic", dest="basic", default=False,
help="only read basic properties")
(options, args) = parser.parse_args()
app = QtGui.QApplication([])
if len(args):
mainApp = mainWindow(loadsnapshot=args[0], options=options)
else:
mainApp = mainWindow(options=options)
mainApp.show()
app.exec_()