Skip to content

Commit

Permalink
Merge pull request #298 from webgme/issue/297_custom_points_port_sele…
Browse files Browse the repository at this point in the history
…ction

Closes #297 custom points port selection
  • Loading branch information
Zsolt Lattmann committed Apr 4, 2015
2 parents 6d5b5ff + 9d05390 commit 4605ea2
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 39 deletions.
1 change: 1 addition & 0 deletions src/client/js/Widgets/DiagramDesigner/AutoRouter.Graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,7 @@ define(['js/logger',
}

if (!path.isAutoRouted()) {
path.createCustomPath();
return this.horizontal.addPathEdges(path) && this.vertical.addPathEdges(path);
} else if (this.box2bufferBox[startId] === this.box2bufferBox[endId] &&
startdir === Utils.reverseDir(enddir) && startRoot !== endRoot) {
Expand Down
79 changes: 50 additions & 29 deletions src/client/js/Widgets/DiagramDesigner/AutoRouter.Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ define( ['js/logger',
srcPorts = this.startports;
}

//Preventing same start/this.endport
//Preventing same start/endport
if (this.endport && srcPorts.length > 1){
i = srcPorts.length;
while (i--){
Expand All @@ -141,23 +141,25 @@ define( ['js/logger',
}


//Getting target
var pt,
x = 0,
y = 0;
// Getting target
if (this.isAutoRouted()) {
var accumulatePortCenters = function(prev, current) {
var center = current.rect.getCenter();
prev.x += center.x;
prev.y += center.y;
return prev;
};
tgt = this.endports.reduce(accumulatePortCenters, new ArPoint(0,0));

i = this.endports.length;
while (i--){
pt = this.endports[i].rect.getCenter();
x += pt.x;
y += pt.y;
tgt.x /= this.endports.length;
tgt.y /= this.endports.length;
} else {
tgt = this.customPathData[0];
}
tgt = new ArPoint(x/this.endports.length, y/this.endports.length);

//Get the optimal port to the target
// Get the optimal port to the target
this.startport = Utils.getOptimalPorts(srcPorts, tgt);

//Create a this.startpoint at the port
// Create a this.startpoint at the port
var startdir = this.getStartDir(),
startportHasLimited = false,
startportCanHave = true;
Expand Down Expand Up @@ -211,17 +213,22 @@ define( ['js/logger',
}

//Getting target
var pt,
x = 0,
y = 0;
if (this.isAutoRouted()) {

i = this.startports.length;
while (i--){
pt = this.startports[i].rect.getCenter();
x += pt.x;
y += pt.y;
var accumulatePortCenters = function(prev, current) {
var center = current.rect.getCenter();
prev.x += center.x;
prev.y += center.y;
return prev;
};
tgt = this.startports.reduce(accumulatePortCenters, new ArPoint(0,0));

tgt.x /= this.startports.length;
tgt.y /= this.startports.length;

} else {
tgt = this.customPathData[this.customPathData.length-1];
}
tgt = new ArPoint(x/this.startports.length, y/this.startports.length);

//Get the optimal port to the target
this.endport = Utils.getOptimalPorts(dstPorts, tgt);
Expand Down Expand Up @@ -475,6 +482,16 @@ define( ['js/logger',
this.setState(CONSTANTS.PathStateConnected);
};

AutoRouterPath.prototype.createCustomPath = function() {
this.points.shift();
this.points.pop();

this.points.unshift(this.startpoint);
this.points.push(this.endpoint);

this.setState(CONSTANTS.PathStateConnected);
};

AutoRouterPath.prototype.removePathCustomizations = function() {
this.customPathData = [];
};
Expand Down Expand Up @@ -508,15 +525,17 @@ define( ['js/logger',
this.endports[i].assertValid();
}

if (this.isConnected()) {
assert(this.points.length !== 0,
if (this.isAutoRouted()) {
if (this.isConnected()) {
assert(this.points.length !== 0,
'ARPath.assertValid: this.points.length !== 0 FAILED');
var points = this.getPointList();
points.assertValid();
var points = this.getPointList();
points.assertValid();

}else{
assert(this.points.length === 0,
} else {
assert(this.points.length === 0,
'ARPath.assertValid: this.points.length === 0 FAILED');
}
}

// If it has a startpoint, must also have a startport
Expand All @@ -526,6 +545,8 @@ define( ['js/logger',
if (this.endpoint) {
assert(this.endport, 'Path has a endpoint without a endport');
}

assert(this.owner, 'Path does not have owner!');
};

AutoRouterPath.prototype.assertValidPoints = function() {
Expand Down
14 changes: 4 additions & 10 deletions src/client/js/Widgets/DiagramDesigner/AutoRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -465,17 +465,11 @@ define(['js/logger',
assert(this.paths[pathId] !== undefined,
'AutoRouter:getPath requested path does not match any current paths');
var path = this.paths[pathId],
points = path.getPointList(),
i = -1,
res = [],
pt;

while(++i < points.length) {
pt = [points[i].x, points[i].y];
res.push(pt);
}
points = path.getPointList();

return res;
return points.map(function(point) {
return [point.x, point.y];
});
};

AutoRouter.prototype.setBoxRect = function(boxObject, size) {
Expand Down
26 changes: 26 additions & 0 deletions test/client/AutoRouter/autorouter.testCases.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ describe('AutoRouter Test Cases', function () {
verbose: true,
before: function (router) {
router._assertPortId2PathIsValid();
router.graph.paths.forEach(function(path) {
assert(path.hasOwner());
});
},
after: function (router) {
// Call assertValid on every path
Expand Down Expand Up @@ -132,5 +135,28 @@ describe('AutoRouter Test Cases', function () {
bugPlayer.test('./testCases/issue288.js');
});

it('creating extra connection segments', function () {
bugPlayer.test('./testCases/creating_new_custom_points.js');
});

it('creating extra connection segments (2)', function () {
bugPlayer.test('./testCases/custom_points2.js');
});

it('issue/297_custom_points_port_selection', function () {
bugPlayer.test('./testCases/issue297.js');

// Check that both boxes are connected on their
// left side (as it is closest to their next next
// point on the custom path)
var path = bugPlayer.autorouter.graph.paths[0],
startport = path.startport,
endport = path.endport,
startbox = startport.owner.getRootBox().rect,
endbox = endport.owner.getRootBox().rect;

assert(Math.abs(startbox.left-path.startpoint.x) < 2);
assert(Math.abs(endbox.left-path.endpoint.x) < 2);
});
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions test/client/AutoRouter/testCases/custom_points2.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 4605ea2

Please sign in to comment.