overleaf/services/web/public/js/libs/sigma-v1.0.3/plugins/sigma.layout.forceAtlas2.min.js
2015-05-18 16:09:47 +01:00

1 line
No EOL
10 KiB
JavaScript
Executable file

(function(undefined){"use strict";function Supervisor(sigInst,options){var _this=this,workerFn=sigInst.getForceAtlas2Worker&&sigInst.getForceAtlas2Worker();if(options=options||{},_root.URL=_root.URL||_root.webkitURL,this.sigInst=sigInst,this.graph=this.sigInst.graph,this.ppn=10,this.ppe=3,this.config={},this.shouldUseWorker=options.worker===!1?!1:!0&&webWorkers,this.workerUrl=options.workerUrl,this.started=!1,this.running=!1,this.shouldUseWorker){if(this.workerUrl)this.worker=new Worker(this.workerUrl);else{var blob=this.makeBlob(workerFn);this.worker=new Worker(URL.createObjectURL(blob))}this.worker.postMessage=this.worker.webkitPostMessage||this.worker.postMessage}else eval(workerFn);var msgName=this.worker?"message":"newCoords";(this.worker||document).addEventListener(msgName,function(t){_this.nodesByteArray=new Float32Array(t.data.nodes),_this.running&&(_this.applyLayoutChanges(),_this.sendByteArrayToWorker(),_this.sigInst.refresh())}),this.graphToByteArrays()}if("undefined"==typeof sigma)throw"sigma is not declared";var _root=this,webWorkers="Worker"in _root;Supervisor.prototype.makeBlob=function(t){var e;try{e=new Blob([t],{type:"application/javascript"})}catch(r){_root.BlobBuilder=_root.BlobBuilder||_root.WebKitBlobBuilder||_root.MozBlobBuilder,e=new BlobBuilder,e.append(t),e=e.getBlob()}return e},Supervisor.prototype.graphToByteArrays=function(){var t,e,r,o=this.graph.nodes(),i=this.graph.edges(),n=o.length*this.ppn,s=i.length*this.ppe,a={};for(this.nodesByteArray=new Float32Array(n),this.edgesByteArray=new Float32Array(s),t=e=0,r=o.length;r>t;t++)a[o[t].id]=e,this.nodesByteArray[e]=o[t].x,this.nodesByteArray[e+1]=o[t].y,this.nodesByteArray[e+2]=0,this.nodesByteArray[e+3]=0,this.nodesByteArray[e+4]=0,this.nodesByteArray[e+5]=0,this.nodesByteArray[e+6]=1+this.graph.degree(o[t].id),this.nodesByteArray[e+7]=1,this.nodesByteArray[e+8]=o[t].size,this.nodesByteArray[e+9]=0,e+=this.ppn;for(t=e=0,r=i.length;r>t;t++)this.edgesByteArray[e]=a[i[t].source],this.edgesByteArray[e+1]=a[i[t].target],this.edgesByteArray[e+2]=i[t].weight||0,e+=this.ppe},Supervisor.prototype.applyLayoutChanges=function(){for(var t=this.graph.nodes(),e=0,r=0,o=this.nodesByteArray.length;o>r;r+=this.ppn)t[e].x=this.nodesByteArray[r],t[e].y=this.nodesByteArray[r+1],e++},Supervisor.prototype.sendByteArrayToWorker=function(t){var e={action:t||"loop",nodes:this.nodesByteArray.buffer},r=[this.nodesByteArray.buffer];"start"===t&&(e.config=this.config||{},e.edges=this.edgesByteArray.buffer,r.push(this.edgesByteArray.buffer)),this.shouldUseWorker?this.worker.postMessage(e,r):_root.postMessage(e,"*")},Supervisor.prototype.start=function(){this.running||(this.running=!0,this.started?this.sendByteArrayToWorker():(this.sendByteArrayToWorker("start"),this.started=!0))},Supervisor.prototype.stop=function(){this.running&&(this.running=!1)},Supervisor.prototype.killWorker=function(){this.worker&&this.worker.terminate()},Supervisor.prototype.configure=function(t){if(this.config=t,this.started){var e={action:"config",config:this.config};this.shouldUseWorker?this.worker.postMessage(e):_root.postMessage(e,"*")}},sigma.prototype.startForceAtlas2=function(t){return this.supervisor||(this.supervisor=new Supervisor(this,t)),t&&this.supervisor.configure(t),this.supervisor.start(),this},sigma.prototype.stopForceAtlas2=function(){return this.supervisor?(this.supervisor.stop(),this):this},sigma.prototype.killForceAtlas2=function(){return this.supervisor?(this.supervisor.stop(),this.supervisor.killWorker(),this.supervisor=null,this):this},sigma.prototype.configForceAtlas2=function(t){return this.supervisor||(this.supervisor=new Supervisor(this,t)),this.supervisor.configure(t),this},sigma.prototype.isForceAtlas2Running=function(){return this.supervisor&&this.supervisor.running}}).call(this),function(undefined){"use strict";function no_crush(t){var e,r,o,i=["x","y","dx","dy","old_dx","old_dy","mass","convergence","size","fixed"],n=["source","target","weight"];for(r=0,o=i.length;o>r;r++)e=new RegExp("np\\(([^,]*), '"+i[r]+"'\\)","g"),t=t.replace(e,0===r?"$1":"$1 + "+r);for(r=0,o=n.length;o>r;r++)e=new RegExp("ep\\(([^,]*), '"+n[r]+"'\\)","g"),t=t.replace(e,0===r?"$1":"$1 + "+r);return t}function getWorkerFn(){var t=crush?crush(Worker.toString()):Worker.toString();return";("+t+").call(this);"}var _root=this,inWebWorker=!("document"in _root),Worker=function(){function t(){var t,e,r={},o=arguments.length;for(t=o-1;t>=0;t--)for(e in arguments[t])r[e]=arguments[t][e];return r}function e(t,e,o){o=o||{};s.nodeMatrix=t,s.edgeMatrix=e,s.nodesLength=s.nodeMatrix.length,s.edgesLength=s.edgeMatrix.length,r(o)}function r(e){e.barnesHutOptimize=!1,s.settings=t(e,s.settings)}function o(){var t,e,r,o,i,n,a,d,h,g,M,p,u,x,l;for(r=0;r<s.nodesLength;r+=s.ppn)s.nodeMatrix[r+4]=s.nodeMatrix[r+2],s.nodeMatrix[r+5]=s.nodeMatrix[r+3],s.nodeMatrix[r+2]=0,s.nodeMatrix[r+3]=0;if(s.settings.outboundAttractionDistribution){for(h=0,r=0;r<s.nodesLength;r+=s.ppn)h+=s.nodeMatrix[r+6];h/=s.nodesLength}if(s.settings.barnesHutOptimize){var c,y,f=Math.pow(2,2*s.settings.barnesHutDepthLimit),w=Math.pow(2,s.settings.barnesHutDepthLimit),v=new UInt16Array(f),A=(new Float32Array(f*s.ppq),new UInt16Array(f)),b=(new UInt16Array(f),new UInt16Array(s.nodesLength)),k=(new UInt16Array(s.nodesLength),0),m=0,B=0,W=0;for(r=0;r<s.nodesLength;r+=s.ppn)k=Math.max(k,s.nodeMatrix[r]),B=Math.min(B,s.nodeMatrix[r]),m=Math.max(m,s.nodeMatrix[r+1]),W=Math.min(W,s.nodeMatrix[r+1]);for(k+=1e-4*(k-B),m+=1e-4*(m-W),r=0,e=0;r<s.nodesLength;r+=s.ppn,e++)c=(s.nodeMatrix[r]-B)/(k-B)*Math.pow(2,s.settings.barnesHutDepthLimit)|0,y=(s.nodeMatrix[r+1]-W)/(m-W)*Math.pow(2,s.settings.barnesHutDepthLimit)|0,v[c*w+y]+=1,b[e]=c*w+y;for(t=0,e=0;f>e;e++)t+=v[e],A[e]=t}if(s.settings.barnesHutOptimize);else for(o=0;o<s.nodesLength;o+=s.ppn)for(i=0;i<s.nodesLength;i+=s.ppn)M=s.nodeMatrix[o]-s.nodeMatrix[i],p=s.nodeMatrix[o+1]-s.nodeMatrix[i+1],s.settings.adjustSize?(x=Math.sqrt(M*M+p*p)-s.nodeMatrix[o+8]-s.nodeMatrix[i+8],x>0?(l=s.settings.scalingRatio*s.nodeMatrix[o+6]*s.nodeMatrix[i+6]/x/x,s.nodeMatrix[o+2]+=M*l,s.nodeMatrix[o+3]+=p*l,s.nodeMatrix[i+2]+=M*l,s.nodeMatrix[i+3]+=p*l):0>x&&(l=100*s.settings.scalingRatio*s.nodeMatrix[o+6]*s.nodeMatrix[i+6],s.nodeMatrix[o+2]+=M*l,s.nodeMatrix[o+3]+=p*l,s.nodeMatrix[i+2]-=M*l,s.nodeMatrix[i+3]-=p*l)):(x=Math.sqrt(M*M+p*p),x>0&&(l=s.settings.scalingRatio*s.nodeMatrix[o+6]*s.nodeMatrix[i+6]/x/x,s.nodeMatrix[o+2]+=M*l,s.nodeMatrix[o+3]+=p*l,s.nodeMatrix[i+2]-=M*l,s.nodeMatrix[i+3]-=p*l));for(d=s.settings.gravity/s.settings.scalingRatio,g=s.settings.scalingRatio,r=0;r<s.nodesLength;r+=s.ppn)l=0,M=s.nodeMatrix[r],p=s.nodeMatrix[r+1],x=Math.sqrt(Math.pow(M,2)+Math.pow(p,2)),s.settings.strongGravityMode?x>0&&(l=g*s.nodeMatrix[r+6]*d):x>0&&(l=g*s.nodeMatrix[r+6]*d/x),s.nodeMatrix[r+2]-=M*l,s.nodeMatrix[r+3]-=p*l;for(g=1*(s.settings.outboundAttractionDistribution?h:1),n=0;n<s.edgesLength;n+=s.ppe)o=s.edgeMatrix[n],i=s.edgeMatrix[n+1],a=s.edgeMatrix[n+2],u=0===s.settings.edgeWeightInfluence?1:1===s.settings.edgeWeightInfluence?a:Math.pow(a,s.settings.edgeWeightInfluence),M=s.nodeMatrix[o]-s.nodeMatrix[i],p=s.nodeMatrix[o+1]-s.nodeMatrix[i+1],s.settings.adjustSizes?(x=Math.sqrt(Math.pow(M,2)+Math.pow(p,2)-s.nodeMatrix[o+8]-s.nodeMatrix[i+8]),s.settings.linLogMode?s.settings.outboundAttractionDistribution?x>0&&(l=-g*u*Math.log(1+x)/x/s.nodeMatrix[o+6]):x>0&&(l=-g*u*Math.log(1+x)/x):s.settings.outboundAttractionDistribution?x>0&&(l=-g*u/s.nodeMatrix[o+6]):x>0&&(l=-g*u)):(x=Math.sqrt(Math.pow(M,2)+Math.pow(p,2)),s.settings.linLogMode?s.settings.outboundAttractionDistribution?x>0&&(l=-g*u*Math.log(1+x)/x/s.nodeMatrix[o+6]):x>0&&(l=-g*u*Math.log(1+x)/x):s.settings.outboundAttractionDistribution?(x=1,l=-g*u/s.nodeMatrix[o+6]):(x=1,l=-g*u)),x>0&&(s.nodeMatrix[o+2]+=M*l,s.nodeMatrix[o+3]+=p*l,s.nodeMatrix[i+2]-=M*l,s.nodeMatrix[i+3]-=p*l);var L,F,_,U;if(s.settings.adjustSizes)for(r=0;r<s.nodesLength;r+=s.ppn)s.nodeMatrix[r+9]||(L=Math.sqrt(Math.pow(s.nodeMatrix[r+2],2)+Math.pow(s.nodeMatrix[r+3],2)),L>s.maxForce&&(s.nodeMatrix[r+2]=s.nodeMatrix[r+2]*s.maxForce/L,s.nodeMatrix[r+3]=s.nodeMatrix[r+3]*s.maxForce/L),F=s.nodeMatrix[r+6]*Math.sqrt((s.nodeMatrix[r+4]-s.nodeMatrix[r+2])*(s.nodeMatrix[r+4]-s.nodeMatrix[r+2])+(s.nodeMatrix[r+5]-s.nodeMatrix[r+3])*(s.nodeMatrix[r+5]-s.nodeMatrix[r+3])),_=Math.sqrt((s.nodeMatrix[r+4]+s.nodeMatrix[r+2])*(s.nodeMatrix[r+4]+s.nodeMatrix[r+2])+(s.nodeMatrix[r+5]+s.nodeMatrix[r+3])*(s.nodeMatrix[r+5]+s.nodeMatrix[r+3]))/2,U=.1*Math.log(1+_)/(1+Math.sqrt(F)),s.nodeMatrix[r]=s.nodeMatrix[r]+s.nodeMatrix[r+2]*(U/s.settings.slowDown),s.nodeMatrix[r+1]=s.nodeMatrix[r+1]+s.nodeMatrix[r+3]*(U/s.settings.slowDown));else for(r=0;r<s.nodesLength;r+=s.ppn)s.nodeMatrix[r+9]||(F=s.nodeMatrix[r+6]*Math.sqrt((s.nodeMatrix[r+4]-s.nodeMatrix[r+2])*(s.nodeMatrix[r+4]-s.nodeMatrix[r+2])+(s.nodeMatrix[r+5]-s.nodeMatrix[r+3])*(s.nodeMatrix[r+5]-s.nodeMatrix[r+3])),_=Math.sqrt((s.nodeMatrix[r+4]+s.nodeMatrix[r+2])*(s.nodeMatrix[r+4]+s.nodeMatrix[r+2])+(s.nodeMatrix[r+5]+s.nodeMatrix[r+3])*(s.nodeMatrix[r+5]+s.nodeMatrix[r+3]))/2,U=s.nodeMatrix[r+7]*Math.log(1+_)/(1+Math.sqrt(F)),s.nodeMatrix[r+7]=Math.min(1,Math.sqrt(U*(Math.pow(s.nodeMatrix[r+2],2)+Math.pow(s.nodeMatrix[r+3],2))/(1+Math.sqrt(F)))),s.nodeMatrix[r]=s.nodeMatrix[r]+s.nodeMatrix[r+2]*(U/s.settings.slowDown),s.nodeMatrix[r+1]=s.nodeMatrix[r+1]+s.nodeMatrix[r+3]*(U/s.settings.slowDown));s.iterations++}function i(){o(),n()}var n,s={ppn:10,ppe:3,ppq:2,maxForce:10,iterations:0,converged:!1,settings:{linLogMode:!1,outboundAttractionDistribution:!1,adjustSizes:!1,edgeWeightInfluence:0,scalingRatio:1,strongGravityMode:!1,gravity:1,slowDown:1,barnesHutOptimize:!1,barnesHutTheta:1.2,barnesHutDepthLimit:4}};n="undefined"!=typeof window&&window.document?function(){var t=new Event("newCoords");t.data={nodes:s.nodeMatrix.buffer},requestAnimationFrame(function(){document.dispatchEvent(t)})}:function(){self.postMessage({nodes:s.nodeMatrix.buffer},[s.nodeMatrix.buffer])},self.addEventListener("message",function(t){switch(t.data.action){case"start":e(new Float32Array(t.data.nodes),new Float32Array(t.data.edges),t.data.config),i();break;case"loop":s.nodeMatrix=new Float32Array(t.data.nodes),i();break;case"config":r(t.data.config)}})},crush=null;if(inWebWorker)eval(getWorkerFn());else{if("undefined"==typeof sigma)throw"sigma is not declared";sigma.prototype.getForceAtlas2Worker=getWorkerFn}}.call(this);