-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.html
More file actions
533 lines (466 loc) · 16.1 KB
/
Copy pathtest.html
File metadata and controls
533 lines (466 loc) · 16.1 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
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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
<html>
<head>
<script src="scripts/jquery.min.js"></script>
<script src="scripts/require.js"></script>
<script src="sigma.js/src/sigma.core.js"></script>
<script src="sigma.js/src/conrad.js"></script>
<script src="sigma.js/src/utils/sigma.utils.js"></script>
<script src="sigma.js/src/utils/sigma.polyfills.js"></script>
<script src="sigma.js/src/sigma.settings.js"></script>
<script src="sigma.js/src/classes/sigma.classes.dispatcher.js"></script>
<script src="sigma.js/src/classes/sigma.classes.configurable.js"></script>
<script src="sigma.js/src/classes/sigma.classes.graph.js"></script>
<script src="sigma.js/src/classes/sigma.classes.camera.js"></script>
<script src="sigma.js/src/classes/sigma.classes.quad.js"></script>
<script src="sigma.js/src/classes/sigma.classes.edgequad.js"></script>
<script src="sigma.js/src/captors/sigma.captors.mouse.js"></script>
<script src="sigma.js/src/captors/sigma.captors.touch.js"></script>
<script src="sigma.js/src/renderers/sigma.renderers.canvas.js"></script>
<script src="sigma.js/src/renderers/sigma.renderers.webgl.js"></script>
<script src="sigma.js/src/renderers/sigma.renderers.svg.js"></script>
<script src="sigma.js/src/renderers/sigma.renderers.def.js"></script>
<script src="sigma.js/src/renderers/webgl/sigma.webgl.nodes.def.js"></script>
<script src="sigma.js/src/renderers/webgl/sigma.webgl.nodes.fast.js"></script>
<script src="sigma.js/src/renderers/webgl/sigma.webgl.edges.def.js"></script>
<script src="sigma.js/src/renderers/webgl/sigma.webgl.edges.fast.js"></script>
<script src="sigma.js/src/renderers/webgl/sigma.webgl.edges.arrow.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.labels.def.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.hovers.def.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.nodes.def.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edges.def.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edges.curve.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edges.arrow.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edges.curvedArrow.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edgehovers.def.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edgehovers.curve.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edgehovers.arrow.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.edgehovers.curvedArrow.js"></script>
<script src="sigma.js/src/renderers/canvas/sigma.canvas.extremities.def.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.utils.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.nodes.def.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.edges.def.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.edges.curve.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.labels.def.js"></script>
<script src="sigma.js/src/renderers/svg/sigma.svg.hovers.def.js"></script>
<script src="sigma.js/src/middlewares/sigma.middlewares.rescale.js"></script>
<script src="sigma.js/src/middlewares/sigma.middlewares.copy.js"></script>
<script src="sigma.js/src/misc/sigma.misc.animation.js"></script>
<script src="sigma.js/src/misc/sigma.misc.bindEvents.js"></script>
<script src="sigma.js/src/misc/sigma.misc.bindDOMEvents.js"></script>
<script src="sigma.js/src/misc/sigma.misc.drawHovers.js"></script>
<!-- END SIGMA IMPORTS -->
<script src="sigma.js/plugins/sigma.renderers.parallelEdges/utils.js"></script>
<script src="sigma.js/plugins/sigma.renderers.parallelEdges/sigma.canvas.edges.curve.js"></script>
<script src="sigma.js/plugins/sigma.renderers.parallelEdges/sigma.canvas.edges.curvedArrow.js"></script>
<script src="sigma.js/plugins/sigma.renderers.parallelEdges/sigma.canvas.edgehovers.curve.js"></script>
<script src="sigma.js/plugins/sigma.renderers.parallelEdges/sigma.canvas.edgehovers.curvedArrow.js"></script>
<script src="sigma.js/plugins/sigma.renderers.customShapes/shape-library.js"></script>
<script src="sigma.js/plugins/sigma.renderers.customShapes/sigma.renderers.customShapes.js"></script>
</head>
<body>
<div style="height:20%">
<div>
<button id="reset">
Reset
</button>
<select id="topology">
<option value="rect">Rectangular</option>
<option value="hex">Hexagonal</option>
<option value="file">File (Buggy)</option>
</select>
<label>width: <input id="width" value="12"></input></label>
<select id="engine">
<option value="single">Single stepper (faithful blocking)</option>
<option value="batch">Batch stepper (fast)</option>
</select>
<select id="renderer">
<option value="canvas">Canvas (pretty but slow)</option>
<option value="webgl">WebGL (fast)</option>
<option value="svg">SVG (buggy)</option>
</select>
</div>
<div>
<button id="step">
Step
</button>
<button id="run">
Run
</button>
<button id="stop">
Stop
</button>
<label>
Steps/frame:
<input id="step-size" value="1">
</input>
</label>
<label><input type="checkbox" id="show-events" checked="checked"></input>Hide events</label>
<label><input type="checkbox" id="show-heat" checked="checked" ></input>Show time</label>
<span>Events: <span id="total-events">0</span></span>
<span>Events/sec: <span id="events-per-sec">0</span></span>
</div>
</div>
<div style="display:flex; width:100%; height:80%;" >
<div id="description" style="flex-basis:30%; overflow-y:scroll">
<p>Hit "Step" to advance once. Hit "Run" to advance lots.
<p>If you want to see the big (fast) picture, suggest you set:
<ul>
<li>Width=32+
<li>Batch-Stepper
<li>WebGL
<li>Disable show-events
</ul>
Then hit "reset" and "run"
<p>
<p>Single-stepper / Event-mode
<p>In event mode the edges are simulated as channels with a single
buffer slot. When a node sends, a copy is placed in all channels
(and it turns green). If any outgoing edge currently has a message,
then the source node will be unable to send until that message
is received by the target (i.e. that node is blocked).
<ul>
<li>Nodes:
<ul>
<li>Grey border : idle (waiting for messages)
<li>Green border : ready (want to send, and not blocked)
<li>Red border: blocked (want to send, blocked by one or more edges)
</ul>
<li>Edges:
<ul>
<li>Grey edge: channel currently empty
<li>Green edge: message pending in channel
</ul>
</ul>
<p>Batch stepper
<p>The batch stepper will ignore channel buffers, and allow a device to
send its message and immediately call the send handler on
all target devices. All devices that are ready to send get a
chance to send one message in each frame. This is a lot faster
to simulate, but is less realistic in terms of messages and
blocking.
</div>
<div id="container" style="flex-basis:50%">
<div id="graph-container" style="width:100%; height:100%;"></div>
</div>
<div id="state-panel" style="flex-basis:20%; overflow-y:scroll">
</div>
<script>
var runAnim=false;
var C=null;
var H=null;
var G=null;
var GUI=null;
var s=null;
var stepper=null;
var SVG=null;
var polys={};
var statePanel=null;
function hex2(v)
{
return ('00'+(v|0).toString(16)).substr(-2);
}
////////////////////////////////////////////
// http://stackoverflow.com/a/17243070
function HSVtoRGB(h, s, v) {
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = v * (1 - s);
q = v * (1 - f * s);
t = v * (1 - (1 - f) * s);
switch (i % 6) {
case 0: r = v, g = t, b = p; break;
case 1: r = q, g = v, b = p; break;
case 2: r = p, g = v, b = t; break;
case 3: r = p, g = q, b = v; break;
case 4: r = t, g = p, b = v; break;
case 5: r = v, g = p, b = q; break;
}
return [
Math.round(r * 255),
Math.round(g * 255),
Math.round(b * 255)
];
}
// http://stackoverflow.com/a/17243070
//////////////////////////////////////////////
function shade(v)
{
var x=(v*256)|0;
if(x<=-255) x=-255;
if(x>=+255) x=+255;
var r=255, g=255, b=255;
if(x<0){
x=255+x;
b=x;
}else{
x=255-x;
r=x;
g=x;
}
r=hex2(r);
g=hex2(g);
b=hex2(b);
return "#"+r+g+b;
}
var totalEvents=0;
var showEvents=true;
var showTime=false;
var useVoronoi=false;
var timeLevels=10;
var timeColors=[];
for(let i=0; i<timeLevels; i++){
let rgb=HSVtoRGB(i/timeLevels, 1, 1);
timeColors.push( "#"+hex2(rgb[0])+hex2(rgb[1])+hex2(rgb[2]) );
}
var heatLevels=256;
var heatColors=[];
for(let i=0; i<heatLevels; i++){
let p=i/(1.0+heatLevels);
let rgb=HSVtoRGB(p*0.70 , 1.0, 1.0);
heatColors.push( "#"+hex2(rgb[0])+hex2(rgb[1])+hex2(rgb[2]) );
}
function updateNodeStyle(n)
{
var device=n.device;
if(showEvents){
n.borderColor = device.blocked() ? "#FF0000" : device.is_rts() ? "#00FF00" : "#808080";
}else{
n.borderColor = "#808080";
}
if(showTime){
let t = device.state.t % timeLevels;
n.color = timeColors[t];
}else{
let v = Math.floor((device.state.v + 1)/2 * heatLevels);
//console.log(" v="+device.state.v+", v="+v);
n.color = heatColors[v];
}
};
function updateNodeStyleVor(d)
{
var device=d
var poly=polys[device.id];
if(showEvents){
//poly.stroke(device.blocked() ? "#FF0000" : device.is_rts() ? "#00FF00" : "#808080");
}else{
//poly.stroke("#808080");
}
if(showTime){
let t = device.state.t % timeLevels;
poly.fill(timeColors[t]);
}else{
let v = Math.floor((device.state.v + 1)/2 * heatLevels);
console.log(" v="+device.state.v+", v="+v);
poly.fill(heatColors[v]);
}
};
function updateEdgeStyle(e)
{
var edge=e.edge;
if(showEvents){
e.color= edge.empty() ? "#808080" : "#00FF00";
}else{
e.color="#808080";
}
};
function updateAllNodes(s)
{
if(useVoronoi){
for(let d of G.enumDevices()){
updateNodeStyleVor(d);
}
}else{
for(let d of s.graph.nodes()){
updateNodeStyle(d);
}
for(let e of s.graph.edges()){
updateEdgeStyle(e);
}
s.refresh({skipIndexation: true});
}
statePanel.update();
}
function animate()
{
var k=0|$("#step-size").val();
var tStart = (new Date()).getTime();
var events=0;
var changes=[];
while(events < k){
var done=stepper.step();
events+=done[0];
changes=changes.concat(changes, done[1]);
if(done[0]==0)
break;
var tNow= (new Date()).getTime();
if(tNow-tStart > 250)
break;
}
stepper.history.splice(0,stepper.history.length);
totalEvents += events;
$("#total-events").text(totalEvents.toString());
var eventsPerSec = events / (tNow-tStart);
$("#events-per-sec").text(eventsPerSec.toFixed(1)+" KEvents/sec")
updateAllNodes(s);
if(runAnim && events>0){
requestAnimationFrame(animate);
}
}
function resetGraph()
{
var w=$("#width").val()|0;
var h=w;
console.log("Pre");
var topology=$("#topology").val();
if(topology=="hex"){
G=H.makeGridHex(w,h*2);
}else if(topology=="file"){
//G=C.loadGraphFromUrl("/graphs/heat_rect_8x8.xml");
G=C.loadGraphFromUrl("/graphs/poets.xml");
}else{
G=H.makeGrid(w,h);
}
console.log("Post");
totalEvents=0;
runAnim=false;
if(s!=null){
s.graph.clear();
s.graph.kill();
$("#graph-container").empty();
s=null;
}
if(useVoronoi){
polys={};
var minX=+1e10;
var maxX=-1e10;
var minY=+1e10;
var maxY=-1e10;
$("#graph-container").empty();
var svg=SVG('graph-container').size('100%', '100%')
add=function(x,y){
minX=Math.min(minX,x);
minY=Math.min(minY,y);
maxX=Math.max(maxX,x);
maxY=Math.max(maxY,y);
};
for(var d of G.enumDevices()){
m=d.metadata;
hull=m["hull"];
if(hull){
for(var dim=0;dim<3;dim++){
console.log(`${dim[0]} ${dim[1]}`)
add(hull[dim][0],hull[dim][1])
console.log(` ${minX},${maxX}`)
}
}
console.log(` ${minX},${maxX}`)
var poly=svg.polyline( hull ).id('node-'+d.id);
polys[d.id]=poly;
}
svg.viewbox({ x: minX, y: minY, width: maxX-minX, height: maxY-minY })
}else{
// Sigma
s = new sigma({
renderer: {
container: document.getElementById('graph-container'),
type: $("#renderer").val()
},
settings: {
drawLabels: false,
maxNodeSize: 10,
minNodeSize: 0.25,
minEdgeSize: 0.5,
maxEdgeSize: 3
}
});
for(var d of G.enumDevices()){
s.graph.addNode({
id:d.id,
label:d.id,
x:d.metadata["x"]*50,
y:d.metadata["y"]*50,
size:0.5,
color:"#FFFFFF",
type: d.deviceType.id=="cell" ? "circle" : "square",
borderColor:"#000000",
device:d
});
}
for(var n of s.graph.nodes()){
n.device.gNode=n;
updateNodeStyle(n);
}
for(var e of G.enumEdges()){
s.graph.addEdge({
id:e.id,
source:e.srcDev.id,
target:e.dstDev.id,
type: "curvedArrow",
size:1,
color:"#000000",
edge:e
});
}
for(var e of s.graph.edges()){
e.edge.gEdge=e;
updateEdgeStyle(e);
}
}
if($("#engine").val()=="batch"){
stepper=new C.BatchStepper();
}else{
stepper=new C.SingleStepper();
}
stepper.attach(G,true);
if(useVoronoi){
}else{
s.refresh();
updateAllNodes(s);
s.bind('clickNode', function(e) {
//console.log(e.type, e.data.node.label, e.data.captor);
var node=e.data.node;
var device=node.device;
statePanel.attach(device);
});
}
}
console.log("Pre load");
require(['scripts/require_config'],function() {
require(['scripts/core/core','scripts/graphs/heat_graph', 'scripts/graphs/all','scripts/gui/gui', 'scripts/svg'], function(CC,HH,AG,GG,svg){
C=CC;
H=HH;
GUI=GG;
SVG=svg;
AG.registerAllGraphTypes();
statePanel=new GUI.StatePanel( $("#state-panel")[0] );
resetGraph();
$("#reset").click(function(){
runAnim=true;
resetGraph();
});
$("#run").click(function(){
runAnim=true;
requestAnimationFrame(animate);
});
$("#step").click(function(){
var changes=stepper.step();
updateAllNodes(s);
});
$("#stop").click(function(){
runAnim=false;
});
$("#show-events").change(function(){
showEvents=!!this.checked;
s.settings('drawEdges',showEvents);
updateAllNodes(s);
});
$("#show-heat").change(function(){
showTime=!this.checked;
updateAllNodes(s);
});
});
});
</script>
</body>
</html>