-
Notifications
You must be signed in to change notification settings - Fork 4
/
OrientationVisualization.py
169 lines (144 loc) · 4.62 KB
/
OrientationVisualization.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
from pygame.locals import *
from OpenGL.GLU import *
from OpenGL.GL import *
import pygame
import math
class OrientationVisualization:
verticeA = [1.0, 0.2, 1.0]
verticeB = [-1.0, 0.2, 1.0]
verticeC = [-1.0, -0.2, 1.0]
verticeD = [1.0, -0.2, 1.0]
verticeE = [1.0, 0.2, -1.0]
verticeF = [-1.0, 0.2, -1.0]
verticeG = [-1.0, -0.2, -1.0]
verticeH = [1.0, -0.2, -1.0]
def start(self, device, app):
pygame.init()
flags = OPENGL | DOUBLEBUF
screen = pygame.display.set_mode((1280, 720), flags)
pygame.display.set_caption("Orientation Visualization")
clock = pygame.time.Clock()
self.screen(1280, 720)
self.init()
while True:
if app.stop_thread_trigger: break
data = device.get_data()
self.display(data, app.current_type_data)
pygame.display.flip()
clock.tick(50)
pygame.quit()
def init(self):
glShadeModel(GL_SMOOTH)
glClearColor(0.0, 0.0, 0.0, 0.0)
glClearDepth(1.0)
glEnable(GL_DEPTH_TEST)
glDepthFunc(GL_LEQUAL)
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
def screen(self, width, height):
glViewport(0, 0, width, height)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluPerspective(45, 1.0*width/height, 0.1, 100.0)
glMatrixMode(GL_MODELVIEW)
glLoadIdentity()
def display(self, data, current_type_data):
# Clear the image
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
# Reset previous transforms
glLoadIdentity()
# Transform to perspective view
glTranslatef(0, 0.0, -7.0)
# Draw
if current_type_data == '_QUATERNION_':
w = float(data[0])
x = float(data[1])
y = float(data[2])
z = float(data[3])
self.draw(w, x, y, z, current_type_data)
elif current_type_data == '_EULERANGLE_':
yaw = float(data[0])
pitch = float(data[1])
roll = float(data[2])
self.draw(1, pitch, roll, yaw, current_type_data)
self.draw_axes()
# Flush and swap
glFlush()
def draw(self, w, x, y, z, current_type_data):
if current_type_data == '_QUATERNION_':
info = "Quaternion w: %.4f, x: %.4f, y: %.4f z: %.4f" %(w, x, y, z)
self.draw_text((-2.6, -1.8, 2), info, 20)
# W and the angle of rotation around the axis of the quaternion.
# Specifies the angle of rotation, in degrees.
angle = 2 * math.acos(w) * 180.00 / math.pi
glRotatef(angle, x, z, y)
elif current_type_data == '_EULERANGLE_':
yaw, pitch, roll = x, y, z
info = "Angle Euler Pitch: %f, Roll: %f, Yaw: %f" %(pitch, roll, yaw)
self.draw_text((-2.6, -1.8, 2), info, 20)
glRotatef(-roll, 0.00, 0.00, 1.00)
glRotatef(pitch, 1.00, 0.00, 0.00)
glRotatef(yaw, 0.00, 1.00, 0.00)
glBegin(GL_QUADS)
# FRONT: ABCD - GREEN
glColor3f(0.0, 1.0, 0.0)
glVertex3fv(self.verticeA)
glVertex3fv(self.verticeB)
glVertex3fv(self.verticeC)
glVertex3fv(self.verticeD)
# BACK: FEHG - GREEN
glColor3f(0.0, 1.0, 0.0)
glVertex3fv(self.verticeF)
glVertex3fv(self.verticeE)
glVertex3fv(self.verticeH)
glVertex3fv(self.verticeG)
# RIGHT: EADH - RED
glColor3f(1.0, 0.0, 0.0)
glVertex3fv(self.verticeE)
glVertex3fv(self.verticeA)
glVertex3fv(self.verticeD)
glVertex3fv(self.verticeH)
# LEFT: BFGC - RED
glColor3f(1.0, 0.0, 0.0)
glVertex3fv(self.verticeB)
glVertex3fv(self.verticeF)
glVertex3fv(self.verticeG)
glVertex3fv(self.verticeC)
# TOP: EFBA - BLUE
glColor3f(0.0, 0.0, 1.0)
glVertex3fv(self.verticeE)
glVertex3fv(self.verticeF)
glVertex3fv(self.verticeB)
glVertex3fv(self.verticeA)
# BOTTOM: DCGH - BLUE
glColor3f(0.0, 0.0, 1.0)
glVertex3fv(self.verticeD)
glVertex3fv(self.verticeC)
glVertex3fv(self.verticeG)
glVertex3fv(self.verticeH)
glEnd()
def draw_axes(self, len = 2.0):
glColor3f(1.0, 1.0, 1.0)
glLineWidth(4.0)
glBegin(GL_LINE_LOOP)
glVertex3d(0, 0, 0)
glVertex3d(len, 0, 0)
glVertex3d(0, 0, 0)
glVertex3d(0, len, 0)
glVertex3d(0, 0, 0)
glVertex3d(0, 0, len)
glEnd()
glVertex3d(len, 0, 0)
self.draw_text((len, 0, 0), "X", 20)
glVertex3d(0, len, 0)
self.draw_text((0, len, 0), "Y", 20)
glVertex3d(0, 0, len)
self.draw_text((0, 0, len), "Z", 20)
def draw_text(self, position, text, size):
font = pygame.font.SysFont("Courier", size, True)
textSurface = font.render(text, True,
(255, 255, 255, 255), (0, 0, 0, 255))
textData = pygame.image.tostring(textSurface, "RGBA", True)
glRasterPos3d(*position)
glDrawPixels(textSurface.get_width(), textSurface.get_height(),
GL_RGBA, GL_UNSIGNED_BYTE, textData
)