-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmap_visualiser.py
More file actions
143 lines (110 loc) · 4.09 KB
/
map_visualiser.py
File metadata and controls
143 lines (110 loc) · 4.09 KB
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
from PySide import QtCore, QtGui, QtUiTools
from PySide.QtOpenGL import QGLWidget
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import csv
import math
from loadform import load_form
class ViewWidget(QGLWidget):
def __init__(self):
super(ViewWidget, self).__init__()
self.setMouseTracking(True)
self.field = {}
self.rotation = (0,0)
self.relative_motion = None
def leftClick(self, mouseX, mouseY):
#Get Matrices
viewport = glGetIntegerv(GL_VIEWPORT)
modelview = glGetDoublev(GL_MODELVIEW_MATRIX)
projection = glGetDoublev(GL_PROJECTION_MATRIX)
closest_match = None
mouseY = self.height - mouseY
for (x,y,z) in self.field:
(screenX,screenY, screenZ) = gluProject(x,y,z,
modelview, projection, viewport)
#print str((screenX,screenY,screenZ))+str((x,y,z))
(dx,dy) = (screenX - mouseX, screenY - mouseY)
if dx > -4 and dx < 4 and dy > -4 and dy < 4:
if closest_match == None:
closest_match = (x,y,z)
else:
(_,_,z0) = closest_match
if screenZ < z0:
closest_match = (x,y,z)
print str(closest_match)+str((mouseX,mouseY))
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.LeftButton:
self.leftClick(event.x(), event.y() )
if event.button() == QtCore.Qt.MouseButton.RightButton:
self.relative_motion = (event.x(), event.y() )
def mouseMoveEvent(self, event):
if self.relative_motion != None:
(x,y) = (event.x(), event.y() )
(x0,y0) = self.relative_motion
(dx,dy) = (x - x0, y - y0)
(azimuth, elevation) = self.rotation
self.rotation = (azimuth + dx, min(90,max(-90,elevation + dy)))
self.relative_motion = (x,y)
self.update()
def mouseReleaseEvent(self, event):
if event.button() == QtCore.Qt.MouseButton.RightButton:
self.relative_motion = None
def resizeGL(self, w,h):
self.width = w
self.height = h
self.nearZ = 1
self.farZ = 1000
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
glViewport(0, 0, w, h)
gluPerspective(90, float(w)/h, self.nearZ, self.farZ)
print "OpenGL Version: "+str(glGetString(GL_VERSION) )
def loadHabHYG(self):
with open("./data/HabHYG.csv", "rb") as csvfile:
reader = csv.reader(csvfile)
cap = 500
for row in reader:
try:
x = float(row[13])
y = float(row[14])
z = float(row[15])
name = row[3]
self.field[(x,y,z)] = name
if len(self.field) >= cap:
break
except:
print "Not a valid row: "+str(row)
print str(len(self.field))+" star positions read in."
def paintGL(self):
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
#Zoom out
glTranslate(0,0,-self.zoom.value()-1)
#Rotate
(azimuth, elevation) = self.rotation
glRotate(-elevation, float(1), 0, 0)
glRotate(-azimuth, 0, float(1), 0)
glColor(1,1,1)
glPointSize(1)
glBegin(GL_POINTS)
for (x,y,z) in self.field:
glVertex(x,y,z)
glEnd()
self.zoom.repaint()
class MapVisualiser(object):
def __init__(self):
self.ui = ViewWidget()
self.ui.resize(QtCore.QSize(640, 480) )
self.ui.loadHabHYG()
self.ui.zoom = QtGui.QSlider(QtCore.Qt.Orientation.Vertical, parent=self.ui)
self.ui.zoom.valueChanged.connect(self.ui.update)
def publish(self):
self.ui.show()
return self
if __name__ == '__main__':
import sys
app = QtGui.QApplication(sys.argv)
a = MapVisualiser().publish()
sys.exit(app.exec_() )