![]() System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64 User : www-data ( 33) PHP Version : 7.4.33 Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare, Directory : /var/www/html/libs/absol-form/js/ciruiteditor/diagrameditor/ |
Upload File : |
export default function GraphOptimizerFactor(global) { function Vec2(t, e) { this.x = t, this.y = e } Vec2.prototype.copy = function () { return new Vec2(this.x, this.y) }, Vec2.prototype.toString = function () { return "(" + this.x + ", " + this.y + ")" }, Vec2.prototype.abs = function () { return Math.sqrt(this.x * this.x + this.y * this.y) }, Vec2.prototype.normalized = function () { var t = this.abs(); return 0 == t && (t = 1), this.div(t) }, Vec2.prototype.mult = function (t) { return new Vec2(this.x * t, this.y * t) }, Vec2.prototype.div = function (t) { return new Vec2(this.x / t, this.y / t) }, Vec2.prototype.dist = function (t) { var e = t.x - this.x, n = t.y - this.y; return Math.sqrt(e * e + n * n) }, Vec2.prototype.sub = function (t) { return new Vec2(this.x - t.x, this.y - t.y) }, Vec2.prototype.add = function (t) { return new Vec2(this.x + t.x, this.y + t.y) }, Vec2.prototype.inv = function () { return new Vec2(-this.x, -this.y) }, Vec2.prototype.linear = function (t, e) { return new Vec2(this.x * (1 - e) + t.x * e, this.y * (1 - e) + t.y * e) }, Vec2.prototype.dot = function (t) { return this.x * t.x + this.y * t.y }, Vec2.prototype.direction = function () { return Math.atan2(this.y, this.x) }, Vec2.prototype.translate = function (t, e) { return this.add(new Vec2(t, e)) }, Vec2.prototype.rotate = function (t) { var e = Math.sin(t), n = Math.cos(t); return new Vec2(this.x * n - this.y * e, this.x * e + this.y * n) }, Vec2.prototype.rotate90 = function () { return new Vec2(-this.y, this.x) }, Vec2.fromDirection = function (t) { return new Vec2(Math.cos(t), Math.sin(t)) }, Vec2.make = function (t, e) { return t instanceof Array ? new Vec2(t[0], t[1]) : t instanceof Vec2 ? t.copy() : new Vec2(t, e) }; Vec2.prototype.cross = function (t) { return this.x * t.y - this.y * t.x; }; function randomPosition(nodeIdArr, nodes) { var n = Math.sqrt(nodeIdArr.length); var minX = Infinity; var minY = Infinity; var d = {}; return nodeIdArr.map(function (id) { var x, y; var found = false; var node = nodes[id]; var width = node.width; var height = node.height; while (!found) { x = Math.floor(Math.random() * n * 5) * 4; y = Math.floor(Math.random() * n * 4) * 4; found = true; for (var dx = 0; dx < width && found; ++dx) { for (var dy = 0; dy < height && found; ++dy) { if (d[(x + dx) + '_' + (y + dy)]) found = false; } } } for (var dx = 0; dx < node.width; ++dx) { for (var dy = 0; dy < node.height; ++dy) { d[(x + dx) + '_' + (y + dy)] = true; } } minX = Math.min(minX, x); minY = Math.min(minY, y); return new Vec2(x, y); }).reduce(function (ac, cr, idx) { cr.x -= minX; cr.y -= minY; ac[nodeIdArr[idx]] = cr; return ac; }, {}); } function hybrid(a, b) { var res = {}; var prev, temp; for (var i in res) { if (Math.random() < (0.5 / i)) res = a[i]; else res = b[i]; if (Math.random() < 0.2) { if (!prev) prev = i; else { temp = res[prev]; res[prev] = res[i]; res[i] = temp; } } if (Math.random() < 0.2 && res[i].x > 0) res[i].x -= Math.floor(Math.random() * 5) * (Math.random() > 0.5 ? 1 : -1); if (Math.random() < 0.2 && res[i].y > 0) res[i].y -= Math.floor(Math.random() * 5) * (Math.random() > 0.5 ? 1 : -1); } return res; } function isCross(A, B, C, D) { var t1 = C.sub(D).cross(D.sub(A)); var t2 = C.sub(D).cross(D.sub(B)); if (t1 * t2 >= 0) return false; var t3 = A.sub(B).cross(B.sub(C)); var t4 = A.sub(B).cross(B.sub(D)); if (t3 * t4 >= 0) return false; return true; } function calcBadScore(nodes, lines, positions) { var node1, node2; var pos1, pos2; var line1, line2; var res = 0; var i, j; var maxX = -Infinity, maxY = -Infinity, minX = Infinity, minY = Infinity; for (i in nodes) { node1 = nodes[i]; pos1 = positions[i]; maxX = Math.max(maxX, pos1.x + node1.width); maxY = Math.max(maxY, pos1.y + node1.height); minX = Math.min(minX, pos1.x); minY = Math.min(minY, pos1.y); res += pos1.x * 30 + pos1.y * 30; for (j in nodes) { if (i === j) continue; node2 = nodes[j]; pos2 = positions[j]; if (!(pos1.x > pos2.x + node2.width || pos1.y > pos2.y + node2.height || pos2.x > pos1.x + node1.width || pos2.y > pos1.y + node1.height) ) { res += 1e7; } } } res += (maxX - minX) * (maxY - minY) * 1e2; var uNode, vNode; var posU, posV; var v1; var A, B, C, D; for (i in lines) { line1 = lines[i]; uNode = nodes[line1.uId]; vNode = nodes[line1.vId]; posU = positions[line1.uId]; posV = positions[line1.vId]; A = new Vec2(posU.x + uNode.outputPos[line1.uPin].x, posU.y + +uNode.outputPos[line1.uPin].y); B = new Vec2(posV.x + vNode.inputPos[line1.vPin].x, posV.y + vNode.inputPos[line1.vPin].y); v1 = A.sub(B); res += v1.abs() * v1.abs() * 1e3; if (v1.x > 0) res += v1.x * v1.x * 1e4; res += Math.abs(v1.y) * 1e3; for (j in lines) { if (i === j) continue; line2 = lines[j]; uNode = nodes[line2.uId]; vNode = nodes[line2.vId]; posU = positions[line2.uId]; posV = positions[line2.vId]; C = new Vec2(posU.x + uNode.outputPos[line2.uPin].x, posU.y + +uNode.outputPos[line2.uPin].y); D = new Vec2(posV.x + vNode.inputPos[line2.vPin].x, posV.y + vNode.inputPos[line2.vPin].y); if (isCross(A, B, C, D)) { res += 1e4; } } } return res; } function normalize(pos) { var minX = Infinity; var minY = Infinity; for (var i in pos) { minX = Math.min(minX, pos[i].x); minY = Math.min(minX, pos[i].y); } for (var i in pos) { pos[i].x -= minX; pos[i].y -= minY; } } global.findBeautyGrid1 = function (gridData, timeOut) { var now = new Date().getTime(); var nodeIdArr = Object.keys(gridData.nodes); var nodes = gridData.nodes; var lines = gridData.lines; var current = nodeIdArr.reduce(function (ac, id) { ac[id] = { x: nodes[id].x, y: nodes[id].y }; return ac; }, {}); var gen = Array(6000).fill(0).map(function () { var positions = randomPosition(nodeIdArr, nodes); var badScore = calcBadScore(nodes, lines, positions); return { positions: positions, badScore: badScore } }); gen.push({ positions: current, badScore: calcBadScore(nodes, lines, current) }); gen.sort(function (a, b) { return a.badScore - b.badScore; }); var best = gen[0]; var now1 = new Date().getTime(); var gen1; var i, j; var counter = 0; while (now1 - now < timeOut && counter < 40) { gen1 = [best]; for (i = 0; i < 33; ++i) { gen1.push(randomPosition(nodeIdArr, nodes)); for (j = 0; j < 33; ++j) if (i !== j) { gen1.push(hybrid(gen[i], gen[j])) } } gen1.sort(function (a, b) { return a.badScore - b.badScore; }); counter++; if (gen1[0].badScore < best.badScore) { best = gen1[0]; counter = 0; } gen = gen1; now1 = new Date().getTime(); } if (best === current) return null; normalize(best.positions); return best; } global.findBeautyGrid = function (gridData, timeout) { timeout = timeout || 2000; var now = new Date().getTime(); var nodeIdArr = Object.keys(gridData.nodes); var nodes = gridData.nodes; var lines = gridData.lines; Object.keys(lines).forEach(function (lineId) { line = lines[lineId]; if (!nodes[line.uId] || !nodes[line.vId]) { delete lines[lineId]; } }) var current = nodeIdArr.reduce(function (ac, id) { ac[id] = { x: nodes[id].x, y: nodes[id].y }; return ac; }, {}); var best = { positions: current, badScore: calcBadScore(nodes, lines, current), noChange: true }; var inCount = Object.keys(nodes).reduce(function (ac, cr) { ac[cr] = 0; return ac; }, {}); var lineByU = {}; var lineByV = {}; var line; for (var lineId in lines) { line = lines[lineId]; inCount[line.vId]++; lineByU[line.uId] = lineByU[line.uId] || []; lineByU[line.uId].push(line); lineByV[line.vId] = lineByV[line.vId] || []; lineByV[line.vId].push(line); } var cols = []; var colWidths = []; var col; var colWidth; var node; var count = 0; var nodeId; var nodes1 = Object.assign({}, nodes); var minCount; while (count++ < 1000) { col = []; colWidth = 0; minCount = Infinity; for (nodeId in nodes1) { if (inCount[nodeId] === minCount) { col.push(nodeId); } else if (inCount[nodeId] < minCount) { minCount = inCount[nodeId]; col = [nodeId]; } } if (col.length > 0) { cols.push(col); col.forEach(function (nodeId) { colWidth = Math.max(colWidth, nodes[nodeId].width); delete nodes1[nodeId]; if (lineByU[nodeId]) lineByU[nodeId].forEach(function (line) { var vId = line.vId; inCount[vId]--; }); }); colWidths.push(colWidth); } else break; } var maxGenLength = 2000000 / nodeIdArr.length >> 0; var gen = [best]; var positions = {}; var stop = false; function tryAlign(i, x) { if (i >= cols.length) { gen.push({ positions: Object.assign({}, positions), badScore: calcBadScore(nodes, lines, positions) }); if (gen.length % 1000 === 0) { if (new Date().getTime() - now > timeout) stop = true; } return; } var y; col = cols[i]; var j1, j2, t; for (var k = col.length * col.length; !stop && k >= 0; --k) { y = 0; for (var j = 0; j < col.length; ++j) { nodeId = col[j]; node = nodes[nodeId]; positions[nodeId] = new Vec2(x, y); y += node.height + 2; } tryAlign(i + 1, x + colWidths[i] + 4); if (gen.length < maxGenLength) { j1 = Math.random() * col.length >> 0; j2 = Math.random() * col.length >> 0; if (j1 !== j2) { t = col[j1]; col[j1] = col[j2]; col[j2] = t; } } } } tryAlign(0, 0); gen.sort(function (a, b) { return a.badScore - b.badScore; }); best = gen[0]; //recover cols cols.forEach(function (col) { col.sort(function (a, b) { return best.positions[a].y - best.positions[b].y; }); }); makeLineStraight(nodes, lines, lineByU, lineByV, cols, best.positions); normalize(best.positions); return best; } function makeLineStraight(nodes, lines, lineByU, lineByV, cols, positions) { var count = 10000; var cont = true; var i, j, col; var top, bot; var fLeft, fRight, f; var cId, cNode; while (count-- > 0 && cont) { cont = false; for (i = 0; i < cols.length; ++i) { col = cols[i]; for (j = 0; j < col.length; ++j) { top = -Infinity; bot = Infinity; cId = col[j]; cNode = nodes[cId]; if (j > 0) { top = positions[col[j - 1]].y + nodes[col[j - 1]].height + 2; } if (j + 1 < col.length) { bot = positions[col[j + 1]].y - nodes[cId].height - 2; } fLeft = lineByV[cId] && lineByV[cId].reduce(function (ac, cr) { var node = nodes[cr.uId]; var y0 = node.outputPos[cr.uPin].y + positions[cr.uId].y; var y1 = cNode.inputPos[cr.vPin].y + positions[cr.vId].y; var dy = y0 - y1; ac.min = Math.min(ac.min, dy); ac.max = Math.max(ac.max, dy); return ac; }, {min: Infinity, max: -Infinity}); fLeft = fLeft || {min: 0, max: 0}; fRight = lineByU[cId] && lineByU[cId].reduce(function (ac, cr) { var node = nodes[cr.vId]; var y0 = node.inputPos[cr.vPin].y + positions[cr.vId].y; var y1 = cNode.outputPos[cr.uPin].y + positions[cr.uId].y; var dy = y0 - y1; ac.min = Math.min(ac.min, dy); ac.max = Math.max(ac.max, dy); return ac; }, {min: Infinity, max: -Infinity}); fRight = fRight || {min: 0, max: 0}; f = Math.round((fLeft.min + fLeft.max + fRight.min + fRight.max) / 4); if (f < 0 && positions[cId].y > top) { positions[cId].y--; cont = true; } else if (f > 0 && positions[cId].y < bot) { cont = true; positions[cId].y++; } } } } } };