-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrouter.js
198 lines (148 loc) · 6.82 KB
/
router.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
/*global flow, _, Router */
(function () {
var totalConns;
var SCORE_INCREASER_FACTOR = 0.5;
Router = {
routeConnectors: function(node){
var steps = [], routedConnectors = [];;
totalConns = 0;
var freePairs = this._collectPairs([], node);
if(!freePairs.length) return [];
steps.push({
freePairs: freePairs,
pickedPairs: [],
score: 0,
nextScore: 0,
currentScore: 0,
potentialScore: getPotentialScore(freePairs,0)
});
var oPairs = this._getOptimalPairs(steps);
_.each(oPairs, function(pair){
pair.connector.docks = pair;
routedConnectors.push(pair.connector);
}, this);
return routedConnectors;
},
_getOptimalPairs: function(steps){
var iters = 0;
var nextStep, easiestStep;
do {
iters++;
easiestStep = _.min(steps, function(step){ return step.currentScore + step.potentialScore; }, this);
nextStep = newRoutingStep(easiestStep);
if(nextStep.potentialScore !== null) nextStep.freePairs.length && steps.unshift(nextStep);
if(easiestStep.potentialScore === null)steps = _.without(steps,easiestStep);
} while(nextStep.freePairs.length || (nextStep.pickedPairs.length != totalConns));
return nextStep.pickedPairs;
},
_collectPairs: function(pairs, node){
_.each(node.inConnectors, function(conn){
if(conn){
pairs = pairs.concat(this._connectorScores(conn, "in", node));
totalConns++;
}
}, this);
_.each(node.outConnectors, function(conn){
if(conn){
pairs = pairs.concat(this._connectorScores(conn, "out", node));
totalConns++;
}
}, this);
return _.sortBy(pairs, function(pair){return pair.score; });
},
_connectorScores: function(conn, dir, node){
var pairs = [], pair;
_.each(node.docks, function(dock){
pair = this.bestDockPairFor(conn, dir == "in" ? "target" : "source", dock, {unmodified: true});
if(pair){
pairs.push(_.extend(pair,{
dir: dir,
connector: conn
}));
}
}, this);
return pairs;
},
bestDockPairFor: function(connector, side, dock){
var bestPair, score, onSrc = (side == "source");
_.each(onSrc ? connector.target.docks : connector.source.docks, function(opDock){
var sDock = onSrc ? dock : opDock, tDock = onSrc ? opDock : dock;
if((onSrc ? this._docksAllowedOnSource(connector, sDock, tDock) :
this._docksAllowedOnTarget(connector, sDock, tDock)) &&
!this._targetDockReserved(connector, tDock)){
score = this._scoreForDockPair(connector, sDock, tDock);
if(!bestPair || score < bestPair.score){
bestPair = { score: score, source: sDock, target: tDock };
}
}
}, this);
return bestPair;
},
_scoreForDockPair: function(connector, sDock,tDock){
var dir = normalise([tDock.x - sDock.x, tDock.y - sDock.y]);
var cos1 = dot(sDock.v, dir), cos2 = dot(tDock.v, neg(dir));
var score = (2 - 2*cos1*cos2) / ((1 + cos1)*(1+cos2));
score *= Math.pow(SCORE_INCREASER_FACTOR, (sDock == connector.docks.source) +
(tDock == connector.docks.target));
return isNaN(score) ? Infinity : score;
},
//the following two routines are to eventually replace dockPairAllowed completely
_docksAllowedOnTarget: function(connector, sDock, tDock){
return !this.inConnectorsOnSourceFor(connector, sDock).length;
},
_docksAllowedOnSource: function(connector, sDock, tDock){
return !this.outConnectorsOnTargetFor(connector, tDock).length;
},
_targetDockReserved: function(connector, tDock){
return tDock.reserved && _.any(connector.target.outConnectors, function(conn){ return !conn; });
}
};
function newRoutingStep(step){
var next = step.freePairs.shift();
if(!next) return null;
step.potentialScore = getPotentialScore(step.freePairs, step.pickedPairs.length);
var newFreePairs = _.compact(_.reject(step.freePairs, function(pair){
return (pair.connector == next.connector) ||
((pair.dir == "in") && (next.source == pair.target)) ||
((pair.dir == "out") && (next.target == pair.source));
}));
var pickedPairs = step.pickedPairs.concat(next);
var potentialScore = getPotentialScore(newFreePairs, pickedPairs.length);
return {
freePairs: newFreePairs,
pickedPairs: pickedPairs,
currentScore: step.currentScore + next.score,
potentialScore: potentialScore
};
}
function getPotentialScore(pairs, pickedPairsNum){
var connectorsFound = [];
var potentialScore = 0;
for(var i = -1, l = pairs.length; ++i < l;){
if(! _.contains(connectorsFound, pairs[i].connector)){
connectorsFound.push(pairs[i].connector);
potentialScore += pairs[i].score;
if(connectorsFound.length === (totalConns - pickedPairsNum)) return potentialScore;
}
}
return null;
}
_.each(["source","target"], function(node){
_.each(["out","in"], function(type){
var utilName = type+"ConnectorsOn"+node[0].toUpperCase() + node.slice(1);
Router[utilName + "For"] = function(connector, dock){
var conns = connector[node][type+"Connectors"];
return _.filter(conns, function(c){return c && (c.docks[type=="in" ? "target" : "source"] == dock);});
};
Router[utilName + "Dock"] = function(connector){
return connector.docks && connector[utilName + "For"](this.docks[node], 1);
};
});
});
function neg(v){ return [-v[0], -v[1]]; }
function dot(v1,v2){ return v1[0]*v2[0] + v1[1]*v2[1]; }
function normalise(v){
var len = Math.sqrt(v[0]*v[0] + v[1]*v[1]);
return [v[0] / len, v[1] / len ];
};
}());