From 3ab81058661e3023ae501f245459e248055f02e5 Mon Sep 17 00:00:00 2001 From: Unick Soft Date: Sat, 21 Apr 2018 15:33:53 +0300 Subject: [PATCH] Updated max stream. Improved visualisation. --- script/Appilcation.js | 21 +++------- script/BaseEdge.js | 19 ++++++++- script/BaseEdgeDrawer.js | 87 ++++++++++++++++----------------------- script/plugins/MaxFlow.js | 43 +++++++++++++++++-- tpl/home.php | 2 +- 5 files changed, 100 insertions(+), 72 deletions(-) diff --git a/script/Appilcation.js b/script/Appilcation.js index 1d0b0ac..1aa40d5 100644 --- a/script/Appilcation.js +++ b/script/Appilcation.js @@ -200,42 +200,33 @@ Application.prototype.setRenderPath = function(renderPath) Application.prototype.RedrawEdge = function(context, edge) { var arcDrawer = new BaseEdgeDrawer(context); - var directArcDrawer = new DirectArcDrawer(context); var commonStyle = new CommonEdgeStyle(context); var selectedStyles = selectedEdgeStyles; - this._RedrawEdge(edge, arcDrawer, directArcDrawer, commonStyle, selectedStyles); + this._RedrawEdge(edge, arcDrawer, commonStyle, selectedStyles); } -Application.prototype._RedrawEdge = function(edge, arcDrawer, directArcDrawer, commonStyle, selectedStyles) +Application.prototype._RedrawEdge = function(edge, arcDrawer, commonStyle, selectedStyles) { var selectedGroup = this.handler.GetSelectedGroup(edge); var currentStyle = selectedGroup > 0 ? selectedStyles[(selectedGroup - 1) % selectedStyles.length] : commonStyle; - this._RedrawEdgeWithStyle(edge, currentStyle, arcDrawer, directArcDrawer, commonStyle, selectedStyles); + this._RedrawEdgeWithStyle(edge, currentStyle, arcDrawer, commonStyle, selectedStyles); } -Application.prototype._RedrawEdgeWithStyle = function(edge, style, arcDrawer, directArcDrawer, commonStyle, selectedStyles) +Application.prototype._RedrawEdgeWithStyle = function(edge, style, arcDrawer, commonStyle, selectedStyles) { - if (!edge.isDirect) - { - arcDrawer.Draw(edge, style); - } - else - { - directArcDrawer.Draw(edge, style); - } + arcDrawer.Draw(edge, style); } Application.prototype.RedrawEdgeProgress = function(context, edge, progress) { var arcDrawer = new ProgressArcDrawer(context, new BaseEdgeDrawer(context), progress); - var directArcDrawer = new ProgressArcDrawer(context, new DirectArcDrawer(context), progress); var commonStyle = new CommonEdgeStyle(context); var selectedStyles = selectedEdgeStyles; - this._RedrawEdge(edge, arcDrawer, directArcDrawer, commonStyle, selectedStyles); + this._RedrawEdge(edge, arcDrawer, commonStyle, selectedStyles); } Application.prototype.RedrawEdges = function(context) diff --git a/script/BaseEdge.js b/script/BaseEdge.js index 10b8d94..6984ef0 100644 --- a/script/BaseEdge.js +++ b/script/BaseEdge.js @@ -8,6 +8,9 @@ function BaseEdge(vertex1, vertex2, isDirect, weight, useWeight) { this.vertex1 = vertex1; this.vertex2 = vertex2; + this.arrayStyleStart = ""; + this.arrayStyleFinish = ""; + this.isDirect = isDirect; this.weight = Number(weight); this.text = ""; @@ -29,6 +32,8 @@ BaseEdge.prototype.SaveToXML = function () "hasPair=\"" + this.hasPair + "\" " + "id=\"" + this.id + "\" " + "text=\"" + this.text + "\" " + + "arrayStyleStart=\"" + this.arrayStyleStart + "\" " + + "arrayStyleFinish=\"" + this.arrayStyleFinish + "\" " + ">"; } @@ -44,6 +49,8 @@ BaseEdge.prototype.LoadFromXML = function (xml, graph) this.useWeight = xml.attr('useWeight') == "true"; this.id = parseInt(xml.attr('id')); this.text = xml.attr("text") == null ? "" : xml.attr("text"); + this.arrayStyleStart = xml.attr("arrayStyleStart") == null ? "" : xml.attr("arrayStyleStart"); + this.arrayStyleFinish = xml.attr("arrayStyleFinish") == null ? "" : xml.attr("arrayStyleFinish"); } BaseEdge.prototype.GetPixelLength = function () @@ -66,4 +73,14 @@ BaseEdge.prototype.GetWeight = function () BaseEdge.prototype.GetText = function () { return this.text.length > 0 ? this.text : (this.useWeight ? this.weight.toString() : ""); -} \ No newline at end of file +} + +BaseEdge.prototype.GetStartEdgeStyle = function () +{ + return this.arrayStyleStart; +} + +BaseEdge.prototype.GetFinishEdgeStyle = function () +{ + return (this.arrayStyleFinish != "" ? this.arrayStyleFinish : (this.isDirect ? "arrow" : "")); +} diff --git a/script/BaseEdgeDrawer.js b/script/BaseEdgeDrawer.js index 1d7d3a4..e50ce09 100644 --- a/script/BaseEdgeDrawer.js +++ b/script/BaseEdgeDrawer.js @@ -91,10 +91,38 @@ function BaseEdgeDrawer(context) BaseEdgeDrawer.prototype.Draw = function(baseEdge, arcStyle) { this.SetupStyle(baseEdge, arcStyle); - - var positions = this.GetArcPositions(baseEdge.vertex1.position, baseEdge.vertex2.position, baseEdge.vertex1.model.diameter); + + var length = baseEdge.model.width * 4; + var width = baseEdge.model.width * 2; + var position1 = baseEdge.vertex1.position; + var position2 = baseEdge.vertex2.position; + var direction = position1.subtract(position2); + var pairShift = baseEdge.vertex1.model.diameter * 0.25; + var realShift = (baseEdge.hasPair ? pairShift : 0); + direction.normalize(1.0); + var positions = this.GetArcPositionsShift(baseEdge.vertex1.position, + baseEdge.vertex2.position, baseEdge.vertex1.model.diameter, baseEdge.vertex2.model.diameter, realShift); - this.DrawArc (positions[0], positions[1], arcStyle); + var hasStartStyle = !position1.equals(position2) && baseEdge.GetStartEdgeStyle() != ""; + var hasFinishStyle = !position1.equals(position2) && baseEdge.GetFinishEdgeStyle() != ""; + + this.DrawArc (positions[0].subtract(direction.multiply(hasStartStyle ? -length / 2 : 0.0)), + positions[1].subtract(direction.multiply(hasFinishStyle ? -length / 2 : 0.0)), arcStyle); + + this.context.fillStyle = this.context.strokeStyle; + this.context.lineWidth = 0; + + if (hasStartStyle) + { + this.DrawArrow(positions[0], positions[1], length, width); + } + if (hasFinishStyle) + { + this.DrawArrow(positions[1], positions[0], length, width); + } + + this.SetupStyle(baseEdge, arcStyle); + if (baseEdge.GetText().length > 0) { this.DrawWeight(positions[0], positions[1], baseEdge.GetText(), arcStyle, baseEdge.hasPair); @@ -193,70 +221,25 @@ BaseEdgeDrawer.prototype.GetArcPositionsShift = function(position1, position2, d } } - -/** - * Direct Arc drawer. - */ -function DirectArcDrawer(context) -{ - this.context = context; -} - -DirectArcDrawer.prototype = Object.create(BaseEdgeDrawer.prototype); - -DirectArcDrawer.prototype.Draw = function(baseEdge, arcStyle) +BaseEdgeDrawer.prototype.DrawArrow = function(position1, position2, length, width) { - baseDrawer = this.CreateBase(); - baseDrawer.SetupStyle(baseEdge, arcStyle); - - var length = baseEdge.model.width * 4; - var width = baseEdge.model.width * 2; - var position1 = baseEdge.vertex1.position; - var position2 = baseEdge.vertex2.position; var direction = position1.subtract(position2); - var pairShift = baseEdge.vertex1.model.diameter * 0.25; - var realShift = (baseEdge.hasPair ? pairShift : 0); - direction.normalize(1.0); - var positions = this.GetArcPositionsShift(baseEdge.vertex1.position, - baseEdge.vertex2.position, baseEdge.vertex1.model.diameter, baseEdge.vertex2.model.diameter, realShift); - - baseDrawer.DrawArc (positions[0], positions[1].subtract(direction.multiply(-length / 2)), arcStyle); - - this.context.fillStyle = this.context.strokeStyle; - this.context.lineWidth = 0; - this.DrawArrow(positions[0], positions[1], length, width); - - if (baseEdge.GetText().length > 0) - { - baseDrawer.DrawWeight(positions[0], positions[1], baseEdge.GetText(), arcStyle, baseEdge.hasPair); - } -} - -DirectArcDrawer.prototype.DrawArrow = function(position1, position2, length, width) -{ - var direction = position2.subtract(position1); direction.normalize(1.0); var normal = direction.normal(); - var pointOnLine = position2.subtract(direction.multiply(length)); + var pointOnLine = position1.subtract(direction.multiply(length)); var point1 = pointOnLine.add(normal.multiply(width)); var point2 = pointOnLine.add(normal.multiply(-width)); this.context.beginPath(); - this.context.moveTo(position2.x, position2.y); + this.context.moveTo(position1.x, position1.y); this.context.lineTo(point1.x, point1.y); this.context.lineTo(point2.x, point2.y); - this.context.lineTo(position2.x, position2.y); + this.context.lineTo(position1.x, position1.y); this.context.closePath(); this.context.fill(); } -DirectArcDrawer.prototype.CreateBase = function() -{ - return new BaseEdgeDrawer(this.context); -} - - function ProgressArcDrawer(context, baseDrawer, progress) { diff --git a/script/plugins/MaxFlow.js b/script/plugins/MaxFlow.js index 8762f08..8bf7576 100644 --- a/script/plugins/MaxFlow.js +++ b/script/plugins/MaxFlow.js @@ -9,6 +9,9 @@ function FindMaxFlow(graph, app) this.selectedObjects = {}; this.selectedEdges = []; this.resetUpText = []; + + this.minEdgeSize = 2; + this.maxEdgeSize = 12; } @@ -62,24 +65,55 @@ FindMaxFlow.prototype.resultCallback = function(pathObjects, properties, results var bFound = results.length > 0 && results[0].value < 1E5 && (results[0].type == 1 || results[0].type == 2) && results[0].value * 1 > 0.0; + var maxFlow = results[0].value * 1; + if (bFound) { this.selectedObjects = {}; + var defaultDiameter = (new EdgeModel()).width; + + var avgFlow = 0; + var countEdges = 0; + + for (var i = 0; i < pathObjects.length; i++) + { + if (pathObjects[i] instanceof BaseEdge) + { + avgFlow += properties[pathObjects[i].id]["flowValue"] * 1; + countEdges += 1; + } + } + avgFlow = avgFlow / countEdges; + for (var i = 0; i < pathObjects.length; i++) { if (pathObjects[i] instanceof BaseEdge) { this.selectedObjects[pathObjects[i].id] = 1; - if (pathObjects[i].useWeight) + var flow = properties[pathObjects[i].id]["flowValue"] * 1; + if (pathObjects[i].useWeight || flow != pathObjects[i].GetWeight()) { - pathObjects[i].text = properties[pathObjects[i].id]["flowValue"] + " / " + pathObjects[i].GetWeight(); + pathObjects[i].text = flow + " / " + pathObjects[i].GetWeight(); } + if (!pathObjects[i].isDirect) + { + if (parseInt(properties[pathObjects[i].id]["backToFront"]) > 0) + { + pathObjects[i].arrayStyleStart = "arrow"; + } + else + { + pathObjects[i].arrayStyleFinish = "arrow"; + } + } + + pathObjects[i].model.width = Math.max(Math.min((flow / avgFlow) * defaultDiameter, this.maxEdgeSize), this.minEdgeSize); } } this.selectedEdges = pathObjects; - this.message = g_maxFlowResult.replace("%1", (results[0].value * 1).toString()).replace("%2", this.firstObject.mainText).replace("%3", this.secondObject.mainText); + this.message = g_maxFlowResult.replace("%1", (maxFlow).toString()).replace("%2", this.firstObject.mainText).replace("%3", this.secondObject.mainText); this.selectedObjects[this.secondObject.id] = 3; this.selectedObjects[this.firstObject.id] = 1; @@ -164,6 +198,9 @@ BaseAlgorithm.prototype.restore = function() if (this.selectedEdges[i] instanceof BaseEdge) { this.selectedEdges[i].text = ""; + this.selectedEdges[i].arrayStyleStart = ""; + this.selectedEdges[i].arrayStyleFinish = ""; + this.selectedEdges[i].model.width = (new EdgeModel()).width; } } diff --git a/tpl/home.php b/tpl/home.php index a142da6..0d966fa 100755 --- a/tpl/home.php +++ b/tpl/home.php @@ -10,7 +10,7 @@ - +