-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRatLabGUI.py
464 lines (426 loc) · 17.7 KB
/
RatLabGUI.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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#==============================================================================
#
# Copyright (C) 2016 Fabian Schoenfeld
#
# This file is part of the ratlab software. It is free software; you can
# redistribute it and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either version 3, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along with
# a special exception for linking and compiling against the pe library, the
# so-called "runtime exception"; see the file COPYING. If not, see:
# http://www.gnu.org/licenses/
#
#==============================================================================
#=====================================================================[ Header ]
# system
import os
import sys
# math
import numpy as np
# python image library
from PIL import Image as IMG
# OpenGL
from OpenGL.GLUT import *
from OpenGL.GLU import *
from OpenGL.GL import *
# utilities / own
sys.path.append( './tools/GUI' )
import opengl_tex_text as TXT
import opengl_texture_load as TEX
# font size in px
FONT_WIDTH = 26
#============================================================[ Utility Classes ]
import freezeable
Freezeable = freezeable.Freezeable
class TextureLib( Freezeable ):
def __init__(self):
self.background = TEX.loadTextureRGBA( './tools/GUI/gui_background.png' )
self.cursor = TEX.loadTextureRGBA( './tools/GUI/cursor.png' )
self.cross = TEX.loadTextureRGBA( './tools/GUI/cross.png' )
self.tick_nay = TEX.loadTextureRGBA( './tools/GUI/tick_nay.png' )
self.tick_yay = TEX.loadTextureRGBA( './tools/GUI/tick_yay.png' )
self.font = TEX.loadTextureRGBA( './tools/GUI/font.png' )
self.button = TEX.loadTextureRGBA( './tools/GUI/script_button.png' )
self.param_box = TEX.loadTextureRGBA( './tools/GUI/param_box.png' )
self.param_circle = TEX.loadTextureRGBA( './tools/GUI/param_circle.png' )
self.param_custom = TEX.loadTextureRGBA( './tools/GUI/param_custom.png' )
self.param_star = TEX.loadTextureRGBA( './tools/GUI/param_star.png' )
self.param_T = TEX.loadTextureRGBA( './tools/GUI/param_t-maze.png' )
self.freeze()
#----------------------------------------------------------------[ gui control ]
class Ctrl( Freezeable ):
def __init__(self):
# textures
self.tex_lib = TextureLib()
# buttons
self.def_buttons = []
self.def_buttons.append( Button(( 48,100),(27,27),self.tex_lib.cross,tag='box' ) ) # box maze
self.def_buttons.append( Button((108,100),(27,27),self.tex_lib.cross,tag='star' ) ) # star maze
self.def_buttons.append( Button((183,100),(27,27),self.tex_lib.cross,tag='t' ) ) # T-maze
self.def_buttons.append( Button((243,100),(27,27),self.tex_lib.cross,tag='circle') ) # circle maze
self.def_buttons.append( Button((308,100),(27,27),self.tex_lib.cross,tag='custom') ) # custom maze / file
self.def_buttons.append( ButtonT((840,80),(158,77),self.tex_lib.button,tag='script')) # script button
# default text fields
self.def_fields = []
self.def_fields.append( TextField((400,50),number=True) ) # 0: frames
self.def_fields.append( TextField((780,150),number=True) ) # 1: training - batch size
self.def_fields.append( TextField((780,200),number=True) ) # 2: training - frames
self.def_fields.append( TickBox((780,250)) ) # 3: training - +ica
self.def_fields.append( TickBox((780,300)) ) # 4: training - +noise
self.def_fields.append( TextField((190,477),number=True) ) # 6: rat - speed
self.def_fields.append( TextField((440,477),number=True) ) # 7: rat - momentum
self.def_fields.append( TextField((680,477),number=True) ) # 8: rat - dec_arc
self.def_fields.append( TickBox((925,475)) ) # 9: rat - small_fov
self.def_fields.append( TickBox((925,525)) ) # 10: rat - path_loop
self.def_fields.append( TextField((190,525)) ) # 11: rat - path file
# parameter fields
self.box_fields = []
self.box_fields.append( TextField((220,150),number=True) ) # 0: length
self.box_fields.append( TextField((220,200),number=True) ) # 1: width
self.box_fields.append( TextField((220,250),number=True) ) # 2: height
self.circle_fields = []
self.circle_fields.append( TextField((220,150),number=True) ) # 0: radius
self.circle_fields.append( TextField((220,200),number=True) ) # 1: segments
self.circle_fields.append( TextField((220,250),number=True) ) # 2: height
self.custom_fields = []
self.custom_fields.append( TextField((220,150)) ) # file name
self.star_fields = []
self.star_fields.append( TextField((220,150),number=True) ) # 0: arms
self.star_fields.append( TextField((220,200),number=True) ) # 1: arm width
self.star_fields.append( TextField((220,250),number=True) ) # 2: arm length
self.star_fields.append( TextField((220,300),number=True) ) # 3: arm height
self.t_fields = []
self.t_fields.append( TextField((220,150),number=True) ) # 0: vert. length
self.t_fields.append( TextField((220,200),number=True) ) # 1: vert. width
self.t_fields.append( TextField((220,250),number=True) ) # 2: horz. length
self.t_fields.append( TextField((220,300),number=True) ) # 3: horz. width
self.t_fields.append( TextField((220,350),number=True) ) # 4: wall height
# field management
self.act_param = 'box'
self.act_field = None
self.def_buttons[0].act = True
self.txt_fields = [] + self.def_buttons + self.def_fields + self.box_fields
# lockdown
self.freeze()
ctrl = None
#---------------------------------------------------------------------[ button ]
class ButtonT( Freezeable ):
def __init__( self, pos, dim, texture=None, tag=None ):
self.pos = (pos[0],550-pos[1])
self.dim = dim
self.tex = texture
self.tag = tag
self.act = False
self.freeze()
def select(self, x, y):
if x >= self.pos[0]-2 and x <= self.pos[0]+self.dim[0]+2 and \
y >= self.pos[1]-2 and y <= self.pos[1]+self.dim[1]+2:
self.act = not self.act
return self.act
def display(self):
if self.tex == None: pass
else:
glBindTexture( GL_TEXTURE_2D, self.tex )
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( self.pos[0], self.pos[1] )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( self.pos[0]+self.dim[0], self.pos[1] )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( self.pos[0]+self.dim[0], self.pos[1]+self.dim[1] )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( self.pos[0], self.pos[1]+self.dim[1] )
glEnd()
def input(self,c): pass
class Button( Freezeable ):
def __init__( self, pos, dim, texture=None, tag=None ):
self.pos = (pos[0],550-pos[1])
self.dim = dim
self.tex = texture
self.tag = tag
self.act = False
self.freeze()
def select(self, x, y):
if x >= self.pos[0]-2 and x <= self.pos[0]+self.dim[0]+2 and \
y >= self.pos[1]-2 and y <= self.pos[1]+self.dim[1]+2:
self.act = not self.act
return self.act
def display(self):
if self.tex == None: pass
elif self.act:
glBindTexture( GL_TEXTURE_2D, self.tex )
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( self.pos[0]-3, self.pos[1] )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( self.pos[0]+self.dim[0]+3, self.pos[1] )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( self.pos[0]+self.dim[0]+3, self.pos[1]+self.dim[1]+3 )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( self.pos[0]-3, self.pos[1]+self.dim[1]+3 )
glEnd()
def input(self,c): pass
#-------------------------------------------------------------------[ tick box ]
class TickBox( Freezeable ):
def __init__( self, pos ):
self.pos = (pos[0],550-pos[1])
self.tick = False
self.freeze()
def display(self):
glPushMatrix()
glTranslate( self.pos[0], self.pos[1], 0 )
if self.tick: glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.tick_yay )
else: glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.tick_nay )
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( 0,0 )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( 32,0 )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( 32,32 )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( 0,32 )
glEnd()
glPopMatrix()
def select(self, x, y):
if x >= self.pos[0]-3 and x <= self.pos[0]+35 and \
y >= self.pos[1]-3 and y <= self.pos[1]+35:
self.tick = not self.tick
return self.tick
def input(self,char):
pass
#-----------------------------------------------------------------[ text field ]
class TextField( Freezeable ):
def __init__( self, pos, number=False ):
self.number = number
self.pos = (pos[0],550-pos[1])
self.text = '-'
self.dot = False
self.active = False
self.freeze()
def display(self):
TXT.drawString( self.text, self.pos )
# cursor
if self.active:
glPushMatrix()
glTranslate( self.pos[0]+len(self.text)*FONT_WIDTH+2, self.pos[1]-2, 0 )
glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.cursor )
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( 0,0 )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( 7,0 )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( 7,35 )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( 0,35 )
glEnd()
glPopMatrix()
def input(self, char):
if self.number == False:
# backspace: delete character
if char == None:
if len(self.text) > 0:
self.text = self.text[:-1]
# add to string
else:
self.text += char
else:
# backspace: delete character
if char == None:
if len(self.text) > 0:
self.text = self.text[:-1]
if '.' not in self.text and self.dot == True: self.dot = False
# digit input
elif char.isdigit() or char == '.':
# start new digit
if '-' in self.text:
self.text = char
# add to digit
elif char.isdigit():
self.text += char
# floating point point
elif char == '.':
if self.dot: return
else:
self.dot = True
self.text += char
def getNumber(self,integer=False):
if self.dot:
return float(self.text) if not integer else None
else:
return int(self.text)
def select(self, x, y):
if x >= self.pos[0] and x < self.pos[0]+FONT_WIDTH*len(self.text) and \
y >= self.pos[1] and y < self.pos[1]+FONT_WIDTH:
self.active = True
else:
self.active = False
if len(self.text) == 0:
self.text = '-'
return self.active
#================================================================[ Render Loop ]
def drawcall(i):
glutPostRedisplay()
glutTimerFunc(0,drawcall,1)
def display():
# reset
glClear( GL_COLOR_BUFFER_BIT )
glBindTexture( GL_TEXTURE_2D, 0 )
glColor( 1.0, 1.0, 1.0, 1.0 )
# background
glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.background )
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( 0, 0 )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( 1000, 0 )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( 1000, 550 )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( 0, 550 )
glEnd()
# current parameter set
if ctrl.act_param == 'box': glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.param_box ); posdim = (60,280,83,143)
elif ctrl.act_param == 'star': glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.param_star ); posdim = (60,225,144,195)
elif ctrl.act_param == 't': glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.param_T ); posdim = (60,180,152,241)
elif ctrl.act_param == 'circle': glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.param_circle ); posdim = (60,285,108,135)
elif ctrl.act_param == 'custom': glBindTexture( GL_TEXTURE_2D, ctrl.tex_lib.param_custom ); posdim = (60,390,123,34)
glBegin( GL_QUADS )
glTexCoord2f( 0.0, 0.0 )
glVertex2i( posdim[0], posdim[1] )
glTexCoord2f( 1.0, 0.0 )
glVertex2i( posdim[0]+posdim[2], posdim[1] )
glTexCoord2f( 1.0, 1.0 )
glVertex2i( posdim[0]+posdim[2], posdim[1]+posdim[3] )
glTexCoord2f( 0.0, 1.0 )
glVertex2i( posdim[0], posdim[1]+posdim[3] )
glEnd()
# default text fields
for f in ctrl.txt_fields:
f.display()
# finish frame
glutSwapBuffers()
#=============================================================[ Script Creation ]
def generateScript():
try:
if 'win' in sys.platform:
script = open( 'RatLabScript.bat', 'w' )
elif 'linux' in sys.platform:
script = open( 'RatLabScript', 'w' )
except:
print 'Error! Script file creation failed!'
sys.exit()
# get values
if ctrl.act_param == 'box': env_param = 'dim '+ctrl.box_fields[0].text+' '+ctrl.box_fields[1].text+' '+ctrl.box_fields[0].text
elif ctrl.act_param == 'star': env_param = 'star_maze '+ctrl.star_fields[0].text+' '+ctrl.star_fields[1].text+' '+ctrl.star_fields[2].text+' '+ctrl.star_fields[3].text
elif ctrl.act_param == 't': env_param = 't_maze '+ctrl.t_fields[0].text+' '+ctrl.t_fields[1].text+' '+ctrl.t_fields[2].text+' '+ctrl.t_fields[3].text+' '+ctrl.t_fields[4].text
elif ctrl.act_param == 'circle': env_param = 'circle_maze '+ctrl.circle_fields[0].text+' '+ctrl.circle_fields[1].text+' '+ctrl.circle_fields[2].text
elif ctrl.act_param == 'custom': env_param = 'custom_maze '+ctrl.custom_fields[0].text
frames = 'record limit ' +ctrl.def_fields[0].text if ctrl.def_fields[0].text!='-' else ''
batch = 'batch_size ' + ctrl.def_fields[ 1].text if ctrl.def_fields[1].text != '-' else ''
tr_frames = 'frames ' + ctrl.def_fields[ 2].text if ctrl.def_fields[2].text != '-' else ''
ica = 'ICA' if ctrl.def_fields[ 3].tick else ''
noise = 'noise' if ctrl.def_fields[ 4].tick else ''
speed = 'speed ' + ctrl.def_fields[ 5].text if ctrl.def_fields[5].text != '-' else ''
mom = 'mom ' + ctrl.def_fields[ 6].text if ctrl.def_fields[6].text != '-' else ''
arc = 'arc ' + ctrl.def_fields[ 7].text if ctrl.def_fields[7].text != '-' else ''
small_fov = 'small_fov' if ctrl.def_fields[ 8].tick else ''
path_loop = 'loop' if ctrl.def_fields[ 9].tick else ''
path = 'path ' + ctrl.def_fields[10].text if ctrl.def_fields[10].text != '-' else ''
# write
r = 'python ratlab.py color '+frames+' '+env_param+' '+speed+' '+mom+' '+arc+' '+small_fov+' '+path+' '+path_loop+'\n'
c = 'python convert.py\n'
t = 'python train.py '+batch+' '+tr_frames+' '+noise+' '+ica+'\n'
s = 'python sample.py - 1 32 all\n'
script.write(r)
script.write(c)
script.write(t)
script.write(s)
print 'Executable RatLab script written to file \'RatLabScript\''
script.close()
#===================================================================[ Callback ]
def keyboardPress( key, x, y ):
# ESC: quit program
if ord(key) == 27:
print 'user abort'
sys.exit()
# text field input
elif ctrl.act_field != None:
if ord(key) == 8: # backspace
ctrl.act_field.input(None)
elif key in '1234567890_.-\/abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ':
ctrl.act_field.input(key)
def mouseClick( btn, state, x, y ):
y = 550 - y
if btn == 0 and state == 0:
ctrl.act_field = None
for f in ctrl.txt_fields:
if f.select(x, y) == True:
ctrl.act_field = f
# default buttons
if f in ctrl.def_buttons:
# generate script
if f.tag == 'script':
generateScript()
return
# select current parameter set
for f2 in ctrl.def_buttons:
if f2!=f: f2.act=False
if f.tag == ctrl.act_param: continue
elif f.tag == 'box':
ctrl.act_param = 'box'
ctrl.txt_fields = [] + ctrl.def_buttons + ctrl.def_fields + ctrl.box_fields
elif f.tag == 'star':
ctrl.act_param = 'star'
ctrl.txt_fields = [] + ctrl.def_buttons + ctrl.def_fields + ctrl.star_fields
elif f.tag == 't':
ctrl.act_param = 't'
ctrl.txt_fields = [] + ctrl.def_buttons + ctrl.def_fields + ctrl.t_fields
elif f.tag == 'circle':
ctrl.act_param = 'circle'
ctrl.txt_fields = [] + ctrl.def_buttons + ctrl.def_fields + ctrl.circle_fields
elif f.tag == 'custom':
ctrl.act_param = 'custom'
ctrl.txt_fields = [] + ctrl.def_buttons + ctrl.def_fields + ctrl.custom_fields
#=====================================================================[ OpenGL ]
def setupOpenGL():
# GLUT window
glutInit( sys.argv )
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH )
glutInitWindowSize( 1000, 550 )
glutCreateWindow( 'RatLabGUI' )
# GLUT display
glutDisplayFunc( display )
glutTimerFunc( 0, drawcall, 1 )
# GLUT callback
glutKeyboardFunc( keyboardPress )
glutMouseFunc( mouseClick )
# OpenGL projection matrix setup for 2D drawing plane
glViewport( 0, 0, 1000, 550 )
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(0, 1000, 0, 550 )
glMatrixMode( GL_MODELVIEW )
glLoadIdentity()
# misellaneous parameters
glClearColor( 0.0, 0.0, 0.0, 1.0 )
# texture mapping
glEnable( GL_TEXTURE_2D )
# blending
glEnable(GL_BLEND)
glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)
#=======================================================================[ Main ]
def main():
setupOpenGL()
global ctrl
ctrl = Ctrl()
glutMainLoop()
main() # <<< <<< <<< <<< <<< <<< <<< <<< <<< <<< <<<[ main ]