-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy path3Dviewer.py
247 lines (220 loc) · 7.04 KB
/
3Dviewer.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
import pygame
import numpy as np
import time
import transforms3d.euler as euler
from amc_parser import *
from OpenGL.GL import *
from OpenGL.GLU import *
class Viewer:
def __init__(self, joints=None, motions=None):
"""
Display motion sequence in 3D.
Parameter
---------
joints: Dict returned from `amc_parser.parse_asf`. Keys are joint names and
values are instance of Joint class.
motions: List returned from `amc_parser.parse_amc. Each element is a dict
with joint names as keys and relative rotation degree as values.
"""
self.joints = joints
self.motions = motions
self.frame = 0 # current frame of the motion sequence
self.playing = False # whether is playing the motion sequence
self.fps = 120 # frame rate
# whether is dragging
self.rotate_dragging = False
self.translate_dragging = False
# old mouse cursor position
self.old_x = 0
self.old_y = 0
# global rotation
self.global_rx = 0
self.global_ry = 0
# rotation matrix for camera moving
self.rotation_R = np.eye(3)
# rotation speed
self.speed_rx = np.pi / 90
self.speed_ry = np.pi / 90
# translation speed
self.speed_trans = 0.25
self.speed_zoom = 0.5
# whether the main loop should break
self.done = False
# default translate set manually to make sure the skeleton is in the middle
# of the window
# if you can't see anything in the screen, this is the first parameter you
# need to adjust
self.default_translate = np.array([0, -20, -100], dtype=np.float32)
self.translate = np.copy(self.default_translate)
pygame.init()
self.screen_size = (1024, 768)
self.screen = pygame.display.set_mode(
self.screen_size, pygame.DOUBLEBUF | pygame.OPENGL
)
pygame.display.set_caption(
'AMC Parser - frame %d / %d' % (self.frame, len(self.motions))
)
self.clock = pygame.time.Clock()
glClearColor(0, 0, 0, 0)
glShadeModel(GL_SMOOTH)
glMaterialfv(
GL_FRONT, GL_SPECULAR, np.array([1, 1, 1, 1], dtype=np.float32)
)
glMaterialfv(
GL_FRONT, GL_SHININESS, np.array([100.0], dtype=np.float32)
)
glMaterialfv(
GL_FRONT, GL_AMBIENT, np.array([0.7, 0.7, 0.7, 0.7], dtype=np.float32)
)
glEnable(GL_POINT_SMOOTH)
glLightfv(GL_LIGHT0, GL_POSITION, np.array([1, 1, 1, 0], dtype=np.float32))
glEnable(GL_LIGHT0)
glEnable(GL_LIGHTING)
glEnable(GL_DEPTH_TEST)
gluPerspective(45, (self.screen_size[0]/self.screen_size[1]), 0.1, 500.0)
glPointSize(10)
glLineWidth(2.5)
def process_event(self):
"""
Handle user interface events: keydown, close, dragging.
"""
for event in pygame.event.get():
if event.type == pygame.QUIT:
self.done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_RETURN: # reset camera
self.translate = self.default_translate
self.global_rx = 0
self.global_ry = 0
elif event.key == pygame.K_SPACE:
self.playing = not self.playing
elif event.type == pygame.MOUSEBUTTONDOWN: # dragging
if event.button == 1:
self.rotate_dragging = True
else:
self.translate_dragging = True
self.old_x, self.old_y = event.pos
elif event.type == pygame.MOUSEBUTTONUP:
if event.button == 1:
self.rotate_dragging = False
else:
self.translate_dragging = False
elif event.type == pygame.MOUSEMOTION:
if self.translate_dragging:
# haven't figure out best way to implement this
pass
elif self.rotate_dragging:
new_x, new_y = event.pos
self.global_ry -= (new_x - self.old_x) / \
self.screen_size[0] * np.pi
self.global_rx -= (new_y - self.old_y) / \
self.screen_size[1] * np.pi
self.old_x, self.old_y = new_x, new_y
pressed = pygame.key.get_pressed()
# rotation
if pressed[pygame.K_DOWN]:
self.global_rx -= self.speed_rx
if pressed[pygame.K_UP]:
self. global_rx += self.speed_rx
if pressed[pygame.K_LEFT]:
self.global_ry += self.speed_ry
if pressed[pygame.K_RIGHT]:
self.global_ry -= self.speed_ry
# moving
if pressed[pygame.K_a]:
self.translate[0] -= self.speed_trans
if pressed[pygame.K_d]:
self.translate[0] += self.speed_trans
if pressed[pygame.K_w]:
self.translate[1] += self.speed_trans
if pressed[pygame.K_s]:
self.translate[1] -= self.speed_trans
if pressed[pygame.K_q]:
self.translate[2] += self.speed_zoom
if pressed[pygame.K_e]:
self.translate[2] -= self.speed_zoom
# forward and rewind
if pressed[pygame.K_COMMA]:
self.frame -= 1
if self.frame < 0:
self.frame = len(self.motions) - 1
if pressed[pygame.K_PERIOD]:
self.frame += 1
if self.frame >= len(self.motions):
self.frame = 0
# global rotation
grx = euler.euler2mat(self.global_rx, 0, 0)
gry = euler.euler2mat(0, self.global_ry, 0)
self.rotation_R = grx.dot(gry)
def set_joints(self, joints):
"""
Set joints for viewer.
Parameter
---------
joints: Dict returned from `amc_parser.parse_asf`. Keys are joint names and
values are instance of Joint class.
"""
self.joints = joints
def set_motion(self, motions):
"""
Set motion sequence for viewer.
Paramter
--------
motions: List returned from `amc_parser.parse_amc. Each element is a dict
with joint names as keys and relative rotation degree as values.
"""
self.motions = motions
def draw(self):
"""
Draw the skeleton with balls and sticks.
"""
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
glBegin(GL_POINTS)
for j in self.joints.values():
coord = np.array(
np.squeeze(j.coordinate).dot(self.rotation_R) + \
self.translate, dtype=np.float32
)
glVertex3f(*coord)
glEnd()
glBegin(GL_LINES)
for j in self.joints.values():
child = j
parent = j.parent
if parent is not None:
coord_x = np.array(
np.squeeze(child.coordinate).dot(self.rotation_R)+self.translate,
dtype=np.float32
)
coord_y = np.array(
np.squeeze(parent.coordinate).dot(self.rotation_R)+self.translate,
dtype=np.float32
)
glVertex3f(*coord_x)
glVertex3f(*coord_y)
glEnd()
def run(self):
"""
Main loop.
"""
while not self.done:
self.process_event()
self.joints['root'].set_motion(self.motions[self.frame])
if self.playing:
self.frame += 1
if self.frame >= len(self.motions):
self.frame = 0
self.draw()
pygame.display.set_caption(
'AMC Parser - frame %d / %d' % (self.frame, len(self.motions))
)
pygame.display.flip()
self.clock.tick(self.fps)
pygame.quit()
if __name__ == '__main__':
asf_path = './data/01/01.asf'
amc_path = './data/01/01_01.amc'
joints = parse_asf(asf_path)
motions = parse_amc(amc_path)
v = Viewer(joints, motions)
v.run()