-
Notifications
You must be signed in to change notification settings - Fork 0
/
PyGLWidget.py
419 lines (358 loc) · 14.7 KB
/
PyGLWidget.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
# -*- coding: utf-8 -*-
#===============================================================================
#
# PyGLWidget.py
#
# A simple GL Viewer.
#
# Copyright (c) 2011, Arne Schmitz <[email protected]>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of the <organization> nor the
# names of its contributors may be used to endorse or promote products
# derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#===============================================================================
from PyQt4 import QtCore, QtGui, QtOpenGL
import math
import numpy
import numpy.linalg as linalg
import OpenGL
OpenGL.ERROR_CHECKING = True
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
class PyGLWidget(QtOpenGL.QGLWidget):
# Qt signals
signalGLMatrixChanged = QtCore.pyqtSignal()
rotationBeginEvent = QtCore.pyqtSignal()
rotationEndEvent = QtCore.pyqtSignal()
compartmentSelected = QtCore.pyqtSignal( QtCore.QString)
def __init__(self, parent = None):
format = QtOpenGL.QGLFormat()
format.setSampleBuffers(True)
QtOpenGL.QGLWidget.__init__(self, format, parent)
#self.toolbar=QtGui.QToolBar(self)
#self.toolbar.setGeometry(10,20,200,40)
#self.toolbar.setFloatable(False)
#self.toolbar.setMovable(True)
#self.toolbar.addSeparator()
#self.toolbar.show()
#self.toolbar.raise_()
#self.catbutt = QtGui.QToolButton(self.toolbar)
#self.catbutt.setGeometry(10,0,40,40)
#self.catbutt.setIcon(QtGui.QIcon("Search.png"))
#self.catbutt2 = QtGui.QToolButton(self.toolbar)
#self.catbutt2.setGeometry(50,0,40,40)
#self.catbutt2.setIcon(QtGui.QIcon("resize.png"))
#self.toolbar.allowedAreas(Qt.BottomToolBarArea)
self.setMouseTracking(True)
self.modelview_matrix_ = []
self.translate_vector_ = [0.0, 0.0, 0.0]
self.viewport_matrix_ = []
self.projection_matrix_ = []
self.near_ = 1.0 #0.1
self.far_ = 500.0
self.fovy_ = 4.0
self.radius_ = 10.0
self.last_point_2D_ = QtCore.QPoint()
self.last_point_ok_ = False
self.last_point_3D_ = [1.0, 0.0, 0.0]
self.isInRotation_ = False
#additions by chaitanya
self.location=[]
self.lines=[]
@QtCore.pyqtSlot()
def printModelViewMatrix(self):
print self.modelview_matrix_
def initializeGL(self):
# OpenGL state
glClearColor(1.0, 1.0, 1.0, 1.0)
glEnable(GL_DEPTH_TEST)
self.reset_view()
def resizeGL(self, width, height):
glViewport( 0, 0, width, height );
self.set_projection( self.near_, self.far_, self.fovy_ );
self.updateGL()
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadMatrixd(self.modelview_matrix_)
# self.renderAxis()
def set_projection(self, _near, _far, _fovy):
self.near_ = _near
self.far_ = _far
self.fovy_ = _fovy
self.makeCurrent()
glMatrixMode( GL_PROJECTION )
glLoadIdentity()
gluPerspective( self.fovy_, float(self.width()) / float(self.height()),
self.near_, self.far_ )
self.updateGL()
def set_center(self, _cog):
self.center_ = _cog
self.view_all()
def set_radius(self, _radius):
self.radius_ = _radius
self.set_projection(_radius / 100.0, _radius * 100.0, self.fovy_)
self.reset_view()
self.translate([0, 0, -_radius * 2.0])
self.view_all()
self.updateGL()
def reset_view(self):
# scene pos and size
glMatrixMode( GL_MODELVIEW )
glLoadIdentity()
self.modelview_matrix_ = glGetDoublev( GL_MODELVIEW_MATRIX )
self.set_center([0.0, 0.0, 0.0])
def reset_rotation(self):
self.modelview_matrix_[0] = [1.0, 0.0, 0.0, 0.0]
self.modelview_matrix_[1] = [0.0, 1.0, 0.0, 0.0]
self.modelview_matrix_[2] = [0.0, 0.0, 1.0, 0.0]
glMatrixMode(GL_MODELVIEW)
glLoadMatrixd(self.modelview_matrix_)
self.updateGL()
def translate(self, _trans):
# Translate the object by _trans
# Update modelview_matrix_
self.makeCurrent()
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
glTranslated(_trans[0], _trans[1], _trans[2])
glMultMatrixd(self.modelview_matrix_)
self.modelview_matrix_ = glGetDoublev(GL_MODELVIEW_MATRIX)
self.translate_vector_[0] = self.modelview_matrix_[3][0]
self.translate_vector_[1] = self.modelview_matrix_[3][1]
self.translate_vector_[2] = self.modelview_matrix_[3][2]
self.signalGLMatrixChanged.emit()
def rotate(self, _axis, _angle):
#self.modelview_matrix_ = glGetDoublev(GL_MODELVIEW_MATRIX)
t = [self.modelview_matrix_[0][0] * self.center_[0] +
self.modelview_matrix_[1][0] * self.center_[1] +
self.modelview_matrix_[2][0] * self.center_[2] +
self.modelview_matrix_[3][0],
self.modelview_matrix_[0][1] * self.center_[0] +
self.modelview_matrix_[1][1] * self.center_[1] +
self.modelview_matrix_[2][1] * self.center_[2] +
self.modelview_matrix_[3][1],
self.modelview_matrix_[0][2] * self.center_[0] +
self.modelview_matrix_[1][2] * self.center_[1] +
self.modelview_matrix_[2][2] * self.center_[2] +
self.modelview_matrix_[3][2]]
self.makeCurrent()
glLoadIdentity()
glTranslatef(t[0], t[1], t[2])
glRotated(_angle, _axis[0], _axis[1], _axis[2])
glTranslatef(-t[0], -t[1], -t[2])
glMultMatrixd(self.modelview_matrix_)
self.modelview_matrix_ = glGetDoublev(GL_MODELVIEW_MATRIX)
self.signalGLMatrixChanged.emit()
def view_all(self):
self.translate( [ -( self.modelview_matrix_[0][0] * self.center_[0] +
self.modelview_matrix_[0][1] * self.center_[1] +
self.modelview_matrix_[0][2] * self.center_[2] +
self.modelview_matrix_[0][3]),
-( self.modelview_matrix_[1][0] * self.center_[0] +
self.modelview_matrix_[1][1] * self.center_[1] +
self.modelview_matrix_[1][2] * self.center_[2] +
self.modelview_matrix_[1][3]),
-( self.modelview_matrix_[2][0] * self.center_[0] +
self.modelview_matrix_[2][1] * self.center_[1] +
self.modelview_matrix_[2][2] * self.center_[2] +
self.modelview_matrix_[2][3] +
self.radius_ / 2.0 )])
def map_to_sphere(self, _v2D):
_v3D = [0.0, 0.0, 0.0]
# inside Widget?
if (( _v2D.x() >= 0 ) and ( _v2D.x() <= self.width() ) and
( _v2D.y() >= 0 ) and ( _v2D.y() <= self.height() ) ):
# map Qt Coordinates to the centered unit square [-0.5..0.5]x[-0.5..0.5]
x = float( _v2D.x() - 0.5 * self.width()) / self.width()
y = float( 0.5 * self.height() - _v2D.y()) / self.height()
_v3D[0] = x;
_v3D[1] = y;
# use Pythagoras to comp z-coord (the sphere has radius sqrt(2.0*0.5*0.5))
z2 = 2.0*0.5*0.5-x*x-y*y;
# numerical robust sqrt
_v3D[2] = math.sqrt(max( z2, 0.0 ))
# normalize direction to unit sphere
n = linalg.norm(_v3D)
_v3D = numpy.array(_v3D) / n
return True, _v3D
else:
return False, _v3D
def wheelEvent(self, _event):
# Use the mouse wheel to zoom in/out
d = - float(_event.delta()) / 200.0 * self.radius_
self.translate([0.0, 0.0, d])
self.updateGL()
_event.accept()
#print self.z_d
def mousePressEvent(self, _event):
self.last_point_2D_ = _event.pos()
self.last_point_ok_, self.last_point_3D_ = self.map_to_sphere(self.last_point_2D_)
def mouseMoveEvent(self, _event):
newPoint2D = _event.pos()
if ((newPoint2D.x() < 0) or (newPoint2D.x() > self.width()) or
(newPoint2D.y() < 0) or (newPoint2D.y() > self.height())):
return
# Left button: rotate around center_
# Middle button: translate object
# Left & middle button: zoom in/out
value_y = 0
newPoint_hitSphere, newPoint3D = self.map_to_sphere(newPoint2D)
dx = float(newPoint2D.x() - self.last_point_2D_.x())
dy = float(newPoint2D.y() - self.last_point_2D_.y())
w = float(self.width())
h = float(self.height())
# enable GL context
self.makeCurrent()
# move in z direction
if (((_event.buttons() & QtCore.Qt.LeftButton) and (_event.buttons() & QtCore.Qt.MidButton))
or (_event.buttons() & QtCore.Qt.LeftButton and _event.modifiers() & QtCore.Qt.ControlModifier)):
value_y = self.radius_ * dy * 2.0 / h;
self.translate([0.0, 0.0, value_y])
# move in x,y direction
elif (_event.buttons() & QtCore.Qt.MidButton
or (_event.buttons() & QtCore.Qt.LeftButton and _event.modifiers() & QtCore.Qt.ShiftModifier)):
z = - (self.modelview_matrix_[0][2] * self.center_[0] +
self.modelview_matrix_[1][2] * self.center_[1] +
self.modelview_matrix_[2][2] * self.center_[2] +
self.modelview_matrix_[3][2]) / (self.modelview_matrix_[0][3] * self.center_[0] +
self.modelview_matrix_[1][3] * self.center_[1] +
self.modelview_matrix_[2][3] * self.center_[2] +
self.modelview_matrix_[3][3])
fovy = 45.0
aspect = w / h
n = 0.01 * self.radius_
up = math.tan(fovy / 2.0 * math.pi / 180.0) * n
right = aspect * up
self.translate( [2.0 * dx / w * right / n * z,
-2.0 * dy / h * up / n * z,
0.0] )
# rotate
elif (_event.buttons() & QtCore.Qt.LeftButton):
if (not self.isInRotation_):
self.isInRotation_ = True
self.rotationBeginEvent.emit()
axis = [0.0, 0.0, 0.0]
angle = 0.0
if (self.last_point_ok_ and newPoint_hitSphere):
axis = numpy.cross(self.last_point_3D_, newPoint3D)
cos_angle = numpy.dot(self.last_point_3D_, newPoint3D)
if (abs(cos_angle) < 1.0):
angle = math.acos(cos_angle) * 180.0 / math.pi
angle *= 2.0
self.rotate(axis, angle)
# remember this point
self.last_point_2D_ = newPoint2D
self.last_point_3D_ = newPoint3D
self.last_point_ok_ = newPoint_hitSphere
# trigger redraw
self.updateGL()
#additions by chaitanya from glwidget.py
def enterEvent(self, ev):
"""
Event called when the mouse enters the widget area.
"""
self.grabKeyboard()
def leaveEvent(self, ev):
"""
Event called when the mouse leaves the widget area.
"""
self.releaseKeyboard()
def keyPressEvent(self, ev):
"""
Key press callback.
"""
key = str(ev.text()).upper()
if (ev.modifiers() & QtCore.Qt.ControlModifier):
self.ctrlPressed = True
elif (ev.key() == QtCore.Qt.Key_Up):
self.translate([0.0, 0.25, 0.0])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Down):
self.translate([0.0, -0.25, 0.0])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Left):
self.translate([-0.25, 0.0, 0.0])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Right):
self.translate([0.25, 0.0, 0.0])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Plus)or(ev.key() == QtCore.Qt.Key_PageUp):
self.translate([0.0, 0.0, 0.75])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Minus)or(ev.key() == QtCore.Qt.Key_PageDown):
self.translate([0.0, 0.0, -0.75])
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_X):
self.rotate([1.0, 0.0, 0.0],2.0)
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_C):
self.rotate([1.0, 0.0, 0.0],-2.0)
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Y):
self.rotate([0.0, 1.0, 0.0],2.0)
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_U):
self.rotate([0.0, 1.0, 0.0],-2.0)
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_Z):
self.rotate([0.0, 0.0, 1.0],2.0)
self.updateGL()
elif (ev.key() == QtCore.Qt.Key_A):
self.rotate([0.0, 0.0, 1.0],-2.0)
self.updateGL()
def keyReleaseEvent(self, ev):
"""
Key release callback.
"""
if (ev.key() == QtCore.Qt.Key_Control):
self.ctrlPressed = False
def renderAxis(self):
"""
Creates the XYZ axis in the 0 coordinate, just for reference.
"""
# XYZ axis
glLineWidth(1)
# glDisable(GL_LIGHTING)
glBegin(GL_LINES)
glColor(1, 0, 0) #Xaxis, Red color
glVertex3f(0, 0, 0)
glVertex3f(2, 0, 0)
glColor(0, 1, 0) #Yaxis, Green color
glVertex3f(0, 0, 0)
glVertex3f(0, 2, 0)
glColor(0, 0, 1) #Zaxis, Blue color
glVertex3f(0, 0, 0)
glVertex3f(0, 0, 2)
glEnd()
# glEnable(GL_LIGHTING)
glLineWidth(1)
#===============================================================================
#
# Local Variables:
# mode: Python
# indent-tabs-mode: nil
# End:
#
#===============================================================================