-
Notifications
You must be signed in to change notification settings - Fork 7
/
web-gui.py
executable file
·470 lines (355 loc) · 15.3 KB
/
web-gui.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
465
466
467
468
469
470
#!/opt/local/bin/python
## For anything
from twisted.internet import reactor
## For WebSockets
try:
## New style autobahn
from autobahn.twisted.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS
except ImportError:
## Old style autobahn
from autobahn.websocket import WebSocketServerFactory, WebSocketServerProtocol, listenWS
## All payloads are JSON-formatted.
import json
import src.parameters
from src.spline_computer import *
from src.tictoc import tic, toc, tictoc_dec
from itertools import izip as zip
from numpy import argmax
class WebGUIServerProtocol( WebSocketServerProtocol ):
def connectionMade( self ):
WebSocketServerProtocol.connectionMade( self )
self.engine = self.factory.engine
self.engine_type = 'ours'
print 'CONNECTED'
#@tictoc_dec
def onMessage( self, msg, binary ):
### BEGIN DEBUGGING
if parameters.kVerbose >= 2:
if not binary:
from pprint import pprint
space = msg.find( ' ' )
if space == -1:
print msg
else:
print msg[ :space ]
pprint( json.loads( msg[ space+1 : ] ) )
elif parameters.kVerbose >= 1:
if not binary:
print msg[:72] + ( ' ...' if len( msg ) > 72 else '' )
### END DEBUGGING
engine = self.engine
if msg.startswith( 'set-engine-type ' ):
engine_type = json.loads( msg[ len( 'set-engine-type ' ): ] )
if self.engine_type == engine_type: return
self.factory.engine = self.engine = build_engine( engine_type, copy = engine )
self.engine_type = engine_type
if len( self.engine.handle_positions ) == 0: return
self.engine.precompute_configuration()
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
self.retrieve_energy()
return
##################### Naive Approaches functions Begin ##########################
if 'fourcontrols' == self.engine_type or 'twoendpoints' == self.engine_type or 'jacobian' == self.engine_type:
if binary:
print 'Received unknown message: binary of length', len( msg )
elif msg.startswith( 'paths-info ' ):
paths_info = json.loads( msg[ len( 'paths-info ' ): ] )
try:
boundary_index = argmax([ info['bbox_area'] for info in paths_info if info['closed'] ])
except ValueError:
boundary_index = -1
self.engine.init_engine( paths_info, boundary_index )
elif msg.startswith( 'handle-positions-and-transforms ' ):
handles = json.loads( msg[ len( 'handle-positions-and-transforms ' ): ] )
positions = [ pos for pos, transform in handles ]
transforms = [ transform for pos, transform in handles ]
self.engine.set_handle_positions( positions, transforms )
## Stop here it if it's empty.
if len( handles ) == 0: return
self.engine.precompute_configuration()
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
tic( 'compute_energy_and_distances' )
self.retrieve_energy()
toc()
## Generate the triangulation and the BBW weights.
# self.engine ...
elif msg.startswith( 'handle-transforms ' ):
handle_transforms = json.loads( msg[ len( 'handle-transforms ' ): ] )
tic( 'transform_change' )
for handle_index, handle_transform in handle_transforms:
self.engine.transform_change( handle_index, handle_transform )
toc()
tic( 'engine.solve_transform_change()' )
all_paths = self.engine.solve_transform_change()
toc()
tic( 'make_chain_from_control_groups' )
all_positions = make_chain_from_control_groups( all_paths )
toc()
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
## Solve for the new curve positions given the updated transform matrix.
# new_positions = engine ...
## Send the new positions to the GUI.
# self.sendMessage( 'paths-positions ' + json.dumps( new_positions ) )
elif msg.startswith( 'control-point-constraint ' ): pass
elif msg.startswith( 'set-weight-function ' ):
weight_function = json.loads( msg[ len( 'set-weight-function ' ): ] )
## Do nothing if this would do nothing.
if self.engine.get_weight_function() == weight_function: return
self.engine.set_weight_function( weight_function )
try:
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
self.retrieve_energy()
except NoHandlesError:
## No handles yet, so nothing to do.
pass
elif msg.startswith( 'enable-arc-length ' ): pass
elif msg.startswith( 'iterations ' ): pass
elif msg.startswith( 'handle-transform-drag-finished' ):
self.retrieve_energy()
else:
print 'Received unknown message:', msg
##################### Naive Approaches functions End ##########################
##################### YS Approaches functions Begin ##########################
elif 'ours' == self.engine_type:
if binary:
print 'Received unknown message: binary of length', len( msg )
elif msg.startswith( 'paths-info ' ):
paths_info = json.loads( msg[ len( 'paths-info ' ): ] )
try:
boundary_index = argmax([ info['bbox_area'] for info in paths_info if info['closed'] ])
except ValueError:
boundary_index = -1
self.engine.init_engine( paths_info, boundary_index )
all_constraints = self.engine.all_constraints
print_paths_info_stats( paths_info, all_constraints )
for i, constraints in enumerate( all_constraints ):
for j, constraint in enumerate( constraints ):
continuity = constraint[0]
fixed = constraint[1]
payload = [ i, j, { 'fixed': fixed, 'continuity': continuity} ]
self.sendMessage( 'control-point-constraint ' + json.dumps( payload ) )
elif msg.startswith( 'handle-positions-and-transforms ' ):
handles = json.loads( msg[ len( 'handle-positions-and-transforms ' ): ] )
positions = [ pos for pos, transform in handles ]
transforms = [ transform for pos, transform in handles ]
self.engine.set_handle_positions( positions, transforms )
## Stop here it if it's empty.
if len( handles ) == 0: return
self.engine.precompute_configuration()
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
tic( 'compute_energy_and_distances' )
self.retrieve_energy()
toc()
## Generate the triangulation and the BBW weights.
# self.engine ...
elif msg.startswith( 'handle-transforms ' ):
handle_transforms = json.loads( msg[ len( 'handle-transforms ' ): ] )
tic( 'transform_change' )
for handle_index, handle_transform in handle_transforms:
self.engine.transform_change( handle_index, handle_transform )
toc()
tic( 'engine.solve_transform_change()' )
all_paths = self.engine.solve_transform_change()
toc()
tic( 'make_chain_from_control_groups' )
all_positions = make_chain_from_control_groups( all_paths )
toc()
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
## Solve for the new curve positions given the updated transform matrix.
# new_positions = engine ...
## Send the new positions to the GUI.
# self.sendMessage( 'paths-positions ' + json.dumps( new_positions ) )
elif msg.startswith( 'control-point-constraint ' ):
paths_info = json.loads( msg[ len( 'control-point-constraint ' ): ] )
constraint = [None]*2
constraint[0] = str( paths_info[2][ u'continuity' ] )
constraint[1] = paths_info[2][ u'fixed' ]
self.engine.constraint_change( paths_info[0], paths_info[1], constraint )
try:
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
except NoHandlesError:
## No handles yet, so nothing to do.
pass
## Solve for the new curve positions given the updated control point constraint.
elif msg.startswith( 'set-weight-function ' ):
weight_function = json.loads( msg[ len( 'set-weight-function ' ): ] )
## Do nothing if this would do nothing.
if self.engine.get_weight_function() == weight_function: return
self.engine.set_weight_function( weight_function )
try:
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
self.retrieve_energy()
except NoHandlesError:
## No handles yet, so nothing to do.
pass
elif msg.startswith( 'enable-arc-length ' ):
enable_arc_length = json.loads( msg[ len( 'enable-arc-length ' ): ] )
## Do nothing if this would do nothing.
if self.engine.get_enable_arc_length() == enable_arc_length: return
self.engine.set_enable_arc_length( enable_arc_length )
try:
self.engine.prepare_to_solve()
all_paths = self.engine.solve_transform_change()
# print 'returned results: ', all_paths
all_positions = make_chain_from_control_groups( all_paths )
self.sendMessage( 'paths-positions ' + json.dumps( all_positions ) )
self.retrieve_energy()
except NoHandlesError:
## No handles yet, so nothing to do.
pass
elif msg.startswith( 'iterations ' ):
iterations = json.loads( msg[ len( 'iterations ' ): ] )
print 'multiple iterations:', iterations
self.engine.set_iterations( iterations )
elif msg.startswith( 'handle-transform-drag-finished' ):
self.retrieve_energy()
else:
print 'Received unknown message:', msg
##################### YS Approaches functions End ##########################
def retrieve_energy( self ) :
if parameters.kNoOverlays: return
try:
all_energy, target_curves, all_distances = self.engine.compute_energy_and_maximum_distance()
energy_and_polyline = [
[
{ 'target-curve-polyline': points.tolist(), 'energy': energy, 'distance': distance }
for energy, points, distance in zip( path_energy, path_points, path_distances )
]
for path_energy, path_points, path_distances in zip( all_energy, target_curves, all_distances )
]
if parameters.kVerbose >= 2:
all_energy = asarray( all_energy )
dists = asarray([ [ curve['maximum_distance'] for curve in path ] for path in all_distances ])
print 'path_num: ', len( all_energy )
print 'curve_num: ', sum( [len( curve_energy ) for curve_energy in all_energy] )
print 'energy sum: ', sum( [sum( curve_energy ) for curve_energy in all_energy] )
e_data = asarray( [ [ max( e ), min( e ), mean( e ) ] for e in all_energy ] ).T
d_data = asarray( [ [ max( d ), min( d ), mean( d ) ] for d in dists ] ).T
# print 'energy:', max( e_data[0] ), min( e_data[1] ), mean( e_data[2] )
print 'distances:', max( d_data[0] )#, min( d_data[1] ), mean( d_data[2] )
if parameters.kComputeComparisonCurves:
from FitCurves.FitCurves import FitCurve
import itertools
schneider_curves = []
for spline in energy_and_polyline:
schneider_curves.append( FitCurve( list( itertools.chain( *[ curve['target-curve-polyline'] for curve in spline ] ) ), 10 ).tolist() )
#from pprint import pprint
#pprint( schneider_curves )
self.sendMessage( 'update-comparison-curve ' + json.dumps( schneider_curves ) )
self.sendMessage( 'update-target-curve ' + json.dumps( energy_and_polyline ) )
except NoHandlesError:
## No handles yet, so nothing to do.
pass
def make_chain_from_control_groups( all_paths ):
all_positions = []
for path in all_paths:
if len( path ) > 1:
new_positions = concatenate( asarray(path)[:-1, :-1] )
new_positions = concatenate( ( new_positions, path[-1] ) )
else:
new_positions = path[0]
new_positions = new_positions.tolist()
all_positions.append( new_positions )
return all_positions
def print_paths_info_stats( paths_info, all_constraints ):
print 'Opening a file with', len( paths_info ), 'paths.'
curve_couts = [ ( len( path['cubic_bezier_chain'] ) - 1 ) / 3. for path in paths_info ]
print 'A total of', sum( curve_couts ), 'cubic bezier curves.'
print 'Longest number of curves in a path:', max( curve_couts )
print 'Average number of curves in a path:', average( curve_couts )
print 'Median number of curves in a path:', median( curve_couts )
counts = {}
for i, constraints in enumerate( all_constraints ):
for j, constraint in enumerate( constraints ):
continuity = constraint[0]
counts.setdefault( continuity, 0 )
counts[ continuity ] += 1
print 'Constraint-type counts'
for name in sorted( counts.iterkeys() ):
print '%s: %s' % ( name, counts[ name ] )
class StubServerProtocol( WebSocketServerProtocol ):
def connectionMade( self ):
WebSocketServerProtocol.connectionMade( self )
print 'CONNECTED'
@tictoc_dec
def onMessage( self, msg, binary ):
if binary:
print 'Received unknown message: binary of length', len( msg )
else:
from pprint import pprint
space = msg.find( ' ' )
if space == -1:
print msg
else:
print msg[ :space ]
pprint( json.loads( msg[ space+1 : ] ) )
def setupWebSocket( address, engine, protocol ):
'''
Listen for WebSocket connections at the given address.
'''
factory = WebSocketServerFactory( address )
factory.engine = engine
factory.protocol = protocol
listenWS( factory )
print "Listening for WebSocket connections at:", address
def build_engine( type = 'ours', copy = None ):
engine = None
# if parameters.EngineType['YSApproach'] == parameters.kEngineType:
# engine = YSEngine()
# elif parameters.EngineType['FourControls'] == parameters.kEngineType:
# engine = FourControlsEngine()
# elif parameters.EngineType['TwoEndpoints'] == parameters.kEngineType:
# engine = TwoEndpointsEngine()
# elif parameters.EngineType['Jacobian'] == parameters.kEngineType:
# engine = JacobianEngine()
# else:
# raise RuntimeError("Unknown engine selected.")
if 'ours' == type:
engine = YSEngine()
elif 'fourcontrols' == type:
engine = FourControlsEngine()
elif 'twoendpoints' == type:
engine = TwoEndpointsEngine()
elif 'jacobian' == type:
engine = JacobianEngine()
else:
raise RuntimeError("Unknown engine selected.")
if copy is not None:
engine.copy_engine( copy )
return engine
def main():
import os, sys
if 'verbose' in sys.argv[1:]:
parameters.kVerbose = int( sys.argv[ sys.argv.index( 'verbose' ) + 1 ] )
print 'Verbosity level:', parameters.kVerbose
protocol = WebGUIServerProtocol
if 'stub' in sys.argv[1:]:
print 'Stub only!'
protocol = StubServerProtocol
## Create engine:
# engine = ...
engine = build_engine()
setupWebSocket( "ws://localhost:9123", engine, protocol )
## Maybe you find this convenient
if 'open' in sys.argv[1:]:
os.system( 'open web-gui.html' )
reactor.run()
if __name__ == '__main__': main()