-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlayout.js
291 lines (290 loc) · 6.62 KB
/
layout.js
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
function Node(x,y){
this.pos = [x,y];
this.linked = [];
this.velocity = [0,0];
}
Node.prototype.move = function(nx, ny){
this.pos = [nx, ny];
}
Node.prototype.hasLink = function(n){
for(var i=0;i<this.linked.length;++i){
var l = this.linked[i];
if(n==l)return true;
}
return false;
}
Node.prototype.link = function(n){
this.linked.push(n);
}
Node.ctx = {}
function spring(node1,node2,_length){
var n1 = node1.pos, n2 = node2.pos, r1 = node1.r, r2 = node2.r;
var r = Math.max(r1,r2);
var length = r1 + r2 + Node.ctx.spring;
var k = 0.1,
dx = n1[0]-n2[0],
dy = n1[1]-n2[1],
d2 = Math.pow(dx,2) + Math.pow(dy,2);
if(d2==0){
var r0 = Math.random() * 5 - 5.0,
r1 = Math.random() * 5 - 5.0;
return [r0,r1];
}
var d = Math.sqrt(d2);
var cos = dx/d,
sin = dy/d;
sign = d-length >= 0 ? 1 : -1,
ad = Math.abs(d-length);
if(ad == 0)
return [0,0];
f = Math.log(ad)*sign;
var f1= [-k*f*cos, -k*f*sin];
return f1;
}
function repulsive(node1,node2, _length){
var n1 = node1.pos, n2 = node2.pos, r1 = node1.r, r2 = node2.r;
var length = r1 + r2 + Node.ctx.spring;
var g = Node.ctx.repulsive,
dx = n1[0]-n2[0],
dy = n1[1]-n2[1],
d2 = Math.pow(dx,2) + Math.pow(dy,2);
if(d2==0){
var r0 = Math.random() * 5 - 5.0,
r1 = Math.random() * 5 - 5.0;
return [r0,r1];
}
var d = Math.sqrt(d2);
var cos = dx/d,
sin = dy/d;
var f = [g/d2*cos, g/d2*sin];
return f;
}
function friction(node){
var m = 0.3,
v = node.velocity;
return [-m*v[0], -m*v[1]];
}
function dist(p0, p1){
return Math.sqrt(Math.pow(p0[0] - p1[0], 2) +
Math.pow(p0[1] - p1[1], 2));
}
function dotProduct(p0, p1){
return p0[0] * p1[0] + p0[1] * p1[1];
}
function add(v1,v2){
return [v1[0]+v2[0], v1[1]+v2[1]];
}
function move(n,dt,f){
var r = n.pos,
v = n.velocity,
nx = r[0] + dt*v[0],
ny = r[1] + dt*v[1];
var min = n.r, hmax = Node.ctx.hmax - n.r,
wmax = Node.ctx.wmax - n.r;
n.velocity = [v[0]+dt*f[0]/n.r, v[1]+dt*f[1]/n.r];
if(nx<min){
nx = min;
n.velocity[0] = 0;
}
else if(nx>wmax){
n.velocity[0] = 0;
nx = wmax;
}
if(ny<min){
n.velocity[1] = 0;
ny = min;
}
else if(ny>hmax){
n.velocity[1] = 0;
ny = hmax;
}
n.pos = [nx,ny];
}
function calcInit(w,h,dispw, disph){
Node.ctx.min = 0;
Node.ctx.wmax = w;
Node.ctx.hmax = h;
Node.ctx.criteria = disph;
Node.ctx.spring = disph/2;
Node.ctx.repulsive = disph*40;
Node.ctx.dt = disph / 100;
Node.ctx.temperature = 300.0;
}
function calc(all, edges){
var dt = Node.ctx.dt, total_energy = 0;
for(var i=0;i<all.length;++i){
var n = all[i];
var link = n.linked;
n.f = [0,0];
for(j=0;j<all.length;++j){
if(n!=all[j]){
n.f = add(n.f, repulsive(n, all[j]));
}
}
}
for(var i=0; i<edges.length;++i){
var edge = edges[i], nn = edge[1];
n = edge[0];
n.f=add(n.f, spring(n, nn));
nn.f=add(nn.f, spring(nn, n));
}
for(var i=0;i<all.length;++i){
n = all[i];
n.f = add(n.f, friction(n));
if(contact(n))
n.f = reflect(n.f, n);
move(n, dt, n.f);
total_energy += Math.pow(n.velocity[0],2) + Math.pow(n.velocity[1], 2);
}
return total_energy;
}
function spring_fr(node1, node2){
var n1 = node1.pos, n2 = node2.pos, r1 = node1.r, r2 = node2.r;
var r = Math.max(r1,r2);
var length = r1 + r2 + Node.ctx.spring;
var dx = n1[0]-n2[0],
dy = n1[1]-n2[1],
d2 = Math.pow(dx,2) + Math.pow(dy,2);
if(d2==0){
var r0 = Math.random() * 5 - 5.0,
r1 = Math.random() * 5 - 5.0;
return [r0,r1];
}
var d = Math.sqrt(d2);
var cos = dx/d,
sin = dy/d;
var r = [cos * d2/length, sin*d2/length];
return r;
}
function repulsive_fr(node1,node2){
var n1 = node1.pos, n2 = node2.pos, r1 = node1.r, r2 = node2.r;
var length = r1 + r2 + Node.ctx.spring;
var g = Node.ctx.repulsive;
var dx = n1[0]-n2[0],
dy = n1[1]-n2[1],
d2 = Math.pow(dx,2) + Math.pow(dy,2);
if(d2==0){
var r0 = Math.random() * 5 - 5.0,
r1 = Math.random() * 5 - 5.0;
return [r0,r1];
}
var d = Math.sqrt(d2);
var cos = dx/d,
sin = dy/d;
var l2 = Math.pow(length,2);
var r= [- (l2/d) * cos,
- (l2/d) * sin];
return r;
}
function calc_fr(all, edges){
var dt = Node.ctx.dt, total_energy = 0;
for(var i=0;i<all.length;++i){
var n = all[i];
var link = n.linked;
n.f = [0,0];
for(j=0;j<all.length;++j){
if(n!=all[j]){
n.f = add(n.f, repulsive_fr(n, all[j]));
}
}
}
for(var i=0; i<edges.length;++i){
var edge = edges[i], nn = edge[1];
n = edge[0];
n.f=add(n.f, spring_fr(n, nn));
nn.f=add(nn.f, spring_fr(nn, n));
}
for(var i=0;i<all.length;++i){
n = all[i];
if(contact(n))
n.f = reflect(n.f, n);
move(n, dt, n.f);
total_energy += Math.pow(n.velocity[0],2) + Math.pow(n.velocity[1], 2);
}
return total_energy;
}
function move_fr(n){
}
function contact(n){
var min = n.r, hmax = Node.ctx.hmax - n.r,
wmax = Node.ctx.wmax - n.r;
var x = n.pos[0], y = n.pos[1];
return x <= min || x >= wmax || y <= min || y >= hmax;
}
function reflect(f, n){
var min = n.r, hmax = Node.ctx.hmax - n.r,
wmax = Node.ctx.wmax - n.r;
var x = n.pos[0], y = n.pos[1];
if(x <= min || x >= wmax)
return [-f[0], f[1]];
else if(y<=min || y>= hmax)
return [f[0], -f[1]];
else
return f;
}
function createNode(paths, unit, config){
var w = config.wwidth, h = config.wheight;
var node_maps = {};
var nodes = [];
for(var i in paths){
if(paths[i]<unit)
continue;
var size = paths[i]/unit;
if(size<10)
continue;
var min_w = min_h = size;
var max_w = w - size, max_h = h - size;
var x = Math.random()*(max_w-min_w+1) + min_w, y = Math.random()*(max_h-min_h+1) + min_h;
var n = new Node(x, y);
n.r = size;
n.data = paths[i];
n.name = i;
var rand = Math.floor(Math.random() * 3);
n.color = rand;
node_maps[i] = n;
nodes.push(n);
}
return [node_maps, nodes];
}
function createLinkData(links, nodes, unit){
var h = {};
var k0, k2;
for(k0 in links){
for(k1 in links[k0]){
var n0 = nodes[k0], n1 = nodes[k1];
if(!n0 || !n1){
continue;
}
if(n0==n1)
continue;
var data = links[k0][k1];
if(data < unit)
continue;
if(!h[k0]) h[k0] = {};
var link = Math.sqrt(data / unit);
h[k0][k1] = {link: link, data: data};
n0.link(n1);
if(!n1.hasLink(n0))
n1.link(n0);
}
}
return h;
}
function createEdges(links, nodes){
var edges = [];
var cache = {};
for(var k0 in links){
if(!cache[k0])cache[k0] = {};
for(var k1 in links[k0]){
if(!cache[k1])cache[k1] = {};
if(cache[k0][k1] || cache[k1][k0])
continue;
var n0 = nodes[k0], n1 = nodes[k1];
if(!n0 || !n1)
continue;
edges.push([n0,n1]);
cache[k0][k1] = true;
}
}
return edges;
}