Advertisement
chinmayv

arbor.js

Nov 21st, 2011
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  arbor.js - version 0.91
  3. //  a graph vizualization toolkit
  4. //
  5. //  Copyright (c) 2011 Samizdat Drafting Co.
  6. //  Physics code derived from springy.js, copyright (c) 2010 Dennis Hotson
  7. //
  8. //  Permission is hereby granted, free of charge, to any person
  9. //  obtaining a copy of this software and associated documentation
  10. //  files (the "Software"), to deal in the Software without
  11. //  restriction, including without limitation the rights to use,
  12. //  copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. //  copies of the Software, and to permit persons to whom the
  14. //  Software is furnished to do so, subject to the following
  15. //  conditions:
  16. //
  17. //  The above copyright notice and this permission notice shall be
  18. //  included in all copies or substantial portions of the Software.
  19. //
  20. //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. //  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  22. //  OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. //  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  24. //  HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  25. //  WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  26. //  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  27. //  OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. (function ($) {
  30.  
  31.     /*        etc.js */
  32.     var trace = function (msg) {
  33.             if (typeof (window) == "undefined" || !window.console) {
  34.                 return
  35.             }
  36.             var len = arguments.length;
  37.             var args = [];
  38.             for (var i = 0; i < len; i++) {
  39.                 args.push("arguments[" + i + "]")
  40.             }
  41.             eval("console.log(" + args.join(",") + ")")
  42.         };
  43.     var dirname = function (a) {
  44.             var b = a.replace(/^\/?(.*?)\/?$/, "$1").split("/");
  45.             b.pop();
  46.             return "/" + b.join("/")
  47.         };
  48.     var basename = function (b) {
  49.             var c = b.replace(/^\/?(.*?)\/?$/, "$1").split("/");
  50.             var a = c.pop();
  51.             if (a == "") {
  52.                 return null
  53.             } else {
  54.                 return a
  55.             }
  56.         };
  57.     var _ordinalize_re = /(\d)(?=(\d\d\d)+(?!\d))/g;
  58.     var ordinalize = function (a) {
  59.             var b = "" + a;
  60.             if (a < 11000) {
  61.                 b = ("" + a).replace(_ordinalize_re, "$1,")
  62.             } else {
  63.                 if (a < 1000000) {
  64.                     b = Math.floor(a / 1000) + "k"
  65.                 } else {
  66.                     if (a < 1000000000) {
  67.                         b = ("" + Math.floor(a / 1000)).replace(_ordinalize_re, "$1,") + "m"
  68.                     }
  69.                 }
  70.             }
  71.             return b
  72.         };
  73.     var nano = function (a, b) {
  74.             return a.replace(/\{([\w\-\.]*)}/g, function (f, c) {
  75.                 var d = c.split("."),
  76.                     e = b[d.shift()];
  77.                 $.each(d, function () {
  78.                     if (e.hasOwnProperty(this)) {
  79.                         e = e[this]
  80.                     } else {
  81.                         e = f
  82.                     }
  83.                 });
  84.                 return e
  85.             })
  86.         };
  87.     var objcopy = function (a) {
  88.             if (a === undefined) {
  89.                 return undefined
  90.             }
  91.             if (a === null) {
  92.                 return null
  93.             }
  94.             if (a.parentNode) {
  95.                 return a
  96.             }
  97.             switch (typeof a) {
  98.             case "string":
  99.                 return a.substring(0);
  100.                 break;
  101.             case "number":
  102.                 return a + 0;
  103.                 break;
  104.             case "boolean":
  105.                 return a === true;
  106.                 break
  107.             }
  108.             var b = ($.isArray(a)) ? [] : {};
  109.             $.each(a, function (d, c) {
  110.                 b[d] = objcopy(c)
  111.             });
  112.             return b
  113.         };
  114.     var objmerge = function (d, b) {
  115.             d = d || {};
  116.             b = b || {};
  117.             var c = objcopy(d);
  118.             for (var a in b) {
  119.                 c[a] = b[a]
  120.             }
  121.             return c
  122.         };
  123.     var objcmp = function (e, c, d) {
  124.             if (!e || !c) {
  125.                 return e === c
  126.             }
  127.             if (typeof e != typeof c) {
  128.                 return false
  129.             }
  130.             if (typeof e != "object") {
  131.                 return e === c
  132.             } else {
  133.                 if ($.isArray(e)) {
  134.                     if (!($.isArray(c))) {
  135.                         return false
  136.                     }
  137.                     if (e.length != c.length) {
  138.                         return false
  139.                     }
  140.                 } else {
  141.                     var h = [];
  142.                     for (var f in e) {
  143.                         if (e.hasOwnProperty(f)) {
  144.                             h.push(f)
  145.                         }
  146.                     }
  147.                     var g = [];
  148.                     for (var f in c) {
  149.                         if (c.hasOwnProperty(f)) {
  150.                             g.push(f)
  151.                         }
  152.                     }
  153.                     if (!d) {
  154.                         h.sort();
  155.                         g.sort()
  156.                     }
  157.                     if (h.join(",") !== g.join(",")) {
  158.                         return false
  159.                     }
  160.                 }
  161.                 var i = true;
  162.                 $.each(e, function (a) {
  163.                     var b = objcmp(e[a], c[a]);
  164.                     i = i && b;
  165.                     if (!i) {
  166.                         return false
  167.                     }
  168.                 });
  169.                 return i
  170.             }
  171.         };
  172.     var objkeys = function (b) {
  173.             var a = [];
  174.             $.each(b, function (d, c) {
  175.                 if (b.hasOwnProperty(d)) {
  176.                     a.push(d)
  177.                 }
  178.             });
  179.             return a
  180.         };
  181.     var objcontains = function (c) {
  182.             if (!c || typeof c != "object") {
  183.                 return false
  184.             }
  185.             for (var b = 1, a = arguments.length; b < a; b++) {
  186.                 if (c.hasOwnProperty(arguments[b])) {
  187.                     return true
  188.                 }
  189.             }
  190.             return false
  191.         };
  192.     var uniq = function (b) {
  193.             var a = b.length;
  194.             var d = {};
  195.             for (var c = 0; c < a; c++) {
  196.                 d[b[c]] = true
  197.             }
  198.             return objkeys(d)
  199.         };
  200.     var arbor_path = function () {
  201.             var a = $("script").map(function (b) {
  202.                 var c = $(this).attr("src");
  203.                 if (!c) {
  204.                     return
  205.                 }
  206.                 if (c.match(/arbor[^\/\.]*.js|dev.js/)) {
  207.                     return c.match(/.*\//) || "/"
  208.                 }
  209.             });
  210.             if (a.length > 0) {
  211.                 return a[0]
  212.             } else {
  213.                 return null
  214.             }
  215.         }; /*     kernel.js */
  216.     var Kernel = function (b) {
  217.             var a = (window.Worker !== undefined);
  218.             var i = null;
  219.             var c = null;
  220.             var f = [];
  221.             f.last = new Date();
  222.             var k = null;
  223.             var d = null;
  224.             var e = null;
  225.             var h = null;
  226.             var g = false;
  227.             var j = {
  228.                 system: b,
  229.                 tween: null,
  230.                 nodes: {},
  231.                 init: function () {
  232.                     if (typeof (Tween) != "undefined") {
  233.                         c = Tween()
  234.                     } else {
  235.                         if (typeof (arbor.Tween) != "undefined") {
  236.                             c = arbor.Tween()
  237.                         } else {
  238.                             c = {
  239.                                 busy: function () {
  240.                                     return false
  241.                                 },
  242.                                 tick: function () {
  243.                                     return true
  244.                                 },
  245.                                 to: function () {
  246.                                     trace("Please include arbor-tween.js to enable tweens");
  247.                                     c.to = function () {};
  248.                                     return
  249.                                 }
  250.                             }
  251.                         }
  252.                     }
  253.                     j.tween = c;
  254.                     var l = b.parameters();
  255.                     if (a) {
  256.                         trace("using web workers");
  257.                         k = setInterval(j.screenUpdate, l.timeout);
  258.                         i = new Worker("js/arbor.js");
  259.                         //i = new Worker(arbor_path() + "arbor.js");
  260.                         i.onmessage = j.workerMsg;
  261.                         i.onerror = function (m) {
  262.                             trace("physics:", m)
  263.                         };
  264.                         i.postMessage({
  265.                             type: "physics",
  266.                             physics: objmerge(l, {
  267.                                 timeout: Math.ceil(l.timeout)
  268.                             })
  269.                         })
  270.                     } else {
  271.                         trace("couldn't use web workers, be careful...");
  272.                         i = Physics(l.dt, l.stiffness, l.repulsion, l.friction, j.system._updateGeometry);
  273.                         j.start()
  274.                     }
  275.                     return j
  276.                 },
  277.                 graphChanged: function (l) {
  278.                     if (a) {
  279.                         i.postMessage({
  280.                             type: "changes",
  281.                             changes: l
  282.                         })
  283.                     } else {
  284.                         i._update(l)
  285.                     }
  286.                     j.start()
  287.                 },
  288.                 particleModified: function (m, l) {
  289.                     if (a) {
  290.                         i.postMessage({
  291.                             type: "modify",
  292.                             id: m,
  293.                             mods: l
  294.                         })
  295.                     } else {
  296.                         i.modifyNode(m, l)
  297.                     }
  298.                     j.start()
  299.                 },
  300.                 physicsModified: function (l) {
  301.                     if (!isNaN(l.timeout)) {
  302.                         if (a) {
  303.                             clearInterval(k);
  304.                             k = setInterval(j.screenUpdate, l.timeout)
  305.                         } else {
  306.                             clearInterval(e);
  307.                             e = null
  308.                         }
  309.                     }
  310.                     if (a) {
  311.                         i.postMessage({
  312.                             type: "sys",
  313.                             param: l
  314.                         })
  315.                     } else {
  316.                         i.modifyPhysics(l)
  317.                     }
  318.                     j.start()
  319.                 },
  320.                 workerMsg: function (m) {
  321.                     var l = m.data.type;
  322.                     if (l == "geometry") {
  323.                         j.workerUpdate(m.data)
  324.                     } else {
  325.                         trace("physics:", m.data)
  326.                     }
  327.                 },
  328.                 _lastPositions: null,
  329.                 workerUpdate: function (l) {
  330.                     j._lastPositions = l;
  331.                     j._lastBounds = l.bounds
  332.                 },
  333.                 _lastFrametime: new Date().valueOf(),
  334.                 _lastBounds: null,
  335.                 _currentRenderer: null,
  336.                 screenUpdate: function () {
  337.                     var m = new Date().valueOf();
  338.                     var l = false;
  339.                     if (j._lastPositions !== null) {
  340.                         j.system._updateGeometry(j._lastPositions);
  341.                         j._lastPositions = null;
  342.                         l = true
  343.                     }
  344.                     if (c && c.busy()) {
  345.                         l = true
  346.                     }
  347.                     if (j.system._updateBounds(j._lastBounds)) {
  348.                         l = true
  349.                     }
  350.                     if (l) {
  351.                         var n = j.system.renderer;
  352.                         if (n !== undefined) {
  353.                             if (n !== d) {
  354.                                 n.init(j.system);
  355.                                 d = n
  356.                             }
  357.                             if (c) {
  358.                                 c.tick()
  359.                             }
  360.                             n.redraw();
  361.                             var o = f.last;
  362.                             f.last = new Date();
  363.                             f.push(f.last - o);
  364.                             if (f.length > 50) {
  365.                                 f.shift()
  366.                             }
  367.                         }
  368.                     }
  369.                 },
  370.                 physicsUpdate: function () {
  371.                     if (c) {
  372.                         c.tick()
  373.                     }
  374.                     i.tick();
  375.                     var m = j.system._updateBounds();
  376.                     if (c && c.busy()) {
  377.                         m = true
  378.                     }
  379.                     var n = j.system.renderer;
  380.                     var l = new Date();
  381.                     var n = j.system.renderer;
  382.                     if (n !== undefined) {
  383.                         if (n !== d) {
  384.                             n.init(j.system);
  385.                             d = n
  386.                         }
  387.                         n.redraw({
  388.                             timestamp: l
  389.                         })
  390.                     }
  391.                     var p = f.last;
  392.                     f.last = l;
  393.                     f.push(f.last - p);
  394.                     if (f.length > 50) {
  395.                         f.shift()
  396.                     }
  397.                     var o = i.systemEnergy();
  398.                     if ((o.mean + o.max) / 2 < 0.05) {
  399.                         if (h === null) {
  400.                             h = new Date().valueOf()
  401.                         }
  402.                         if (new Date().valueOf() - h > 1000) {
  403.                             clearInterval(e);
  404.                             e = null
  405.                         } else {}
  406.                     } else {
  407.                         h = null
  408.                     }
  409.                 },
  410.                 fps: function (m) {
  411.                     if (m !== undefined) {
  412.                         var p = 1000 / Math.max(1, targetFps);
  413.                         j.physicsModified({
  414.                             timeout: p
  415.                         })
  416.                     }
  417.                     var q = 0;
  418.                     for (var o = 0, n = f.length; o < n; o++) {
  419.                         q += f[o]
  420.                     }
  421.                     var l = q / Math.max(1, f.length);
  422.                     if (!isNaN(l)) {
  423.                         return Math.round(1000 / l)
  424.                     } else {
  425.                         return 0
  426.                     }
  427.                 },
  428.                 start: function (l) {
  429.                     if (e !== null) {
  430.                         return
  431.                     }
  432.                     if (g && !l) {
  433.                         return
  434.                     }
  435.                     g = false;
  436.                     if (a) {
  437.                         i.postMessage({
  438.                             type: "start"
  439.                         })
  440.                     } else {
  441.                         h = null;
  442.                         e = setInterval(j.physicsUpdate, j.system.parameters().timeout)
  443.                     }
  444.                 },
  445.                 stop: function () {
  446.                     g = true;
  447.                     if (a) {
  448.                         i.postMessage({
  449.                             type: "stop"
  450.                         })
  451.                     } else {
  452.                         if (e !== null) {
  453.                             clearInterval(e);
  454.                             e = null
  455.                         }
  456.                     }
  457.                 }
  458.             };
  459.             return j.init()
  460.         }; /*      atoms.js */
  461.     var Node = function (a) {
  462.             this._id = _nextNodeId++;
  463.             this.data = a || {};
  464.             this._mass = (a.mass !== undefined) ? a.mass : 1;
  465.             this._fixed = (a.fixed === true) ? true : false;
  466.             this._p = new Point((typeof (a.x) == "number") ? a.x : null, (typeof (a.y) == "number") ? a.y : null);
  467.             delete this.data.x;
  468.             delete this.data.y;
  469.             delete this.data.mass;
  470.             delete this.data.fixed
  471.         };
  472.     var _nextNodeId = 1;
  473.     var Edge = function (b, c, a) {
  474.             this._id = _nextEdgeId--;
  475.             this.source = b;
  476.             this.target = c;
  477.             this.length = (a.length !== undefined) ? a.length : 1;
  478.             this.data = (a !== undefined) ? a : {};
  479.             delete this.data.length
  480.         };
  481.     var _nextEdgeId = -1;
  482.     var Particle = function (a, b) {
  483.             this.p = a;
  484.             this.m = b;
  485.             this.v = new Point(0, 0);
  486.             this.f = new Point(0, 0)
  487.         };
  488.     Particle.prototype.applyForce = function (a) {
  489.         this.f = this.f.add(a.divide(this.m))
  490.     };
  491.     var Spring = function (c, b, d, a) {
  492.             this.point1 = c;
  493.             this.point2 = b;
  494.             this.length = d;
  495.             this.k = a
  496.         };
  497.     Spring.prototype.distanceToParticle = function (a) {
  498.         var c = that.point2.p.subtract(that.point1.p).normalize().normal();
  499.         var b = a.p.subtract(that.point1.p);
  500.         return Math.abs(b.x * c.x + b.y * c.y)
  501.     };
  502.     var Point = function (a, b) {
  503.             if (a && a.hasOwnProperty("y")) {
  504.                 b = a.y;
  505.                 a = a.x
  506.             }
  507.             this.x = a;
  508.             this.y = b
  509.         };
  510.     Point.random = function (a) {
  511.         a = (a !== undefined) ? a : 5;
  512.         return new Point(2 * a * (Math.random() - 0.5), 2 * a * (Math.random() - 0.5))
  513.     };
  514.     Point.prototype = {
  515.         exploded: function () {
  516.             return (isNaN(this.x) || isNaN(this.y))
  517.         },
  518.         add: function (a) {
  519.             return new Point(this.x + a.x, this.y + a.y)
  520.         },
  521.         subtract: function (a) {
  522.             return new Point(this.x - a.x, this.y - a.y)
  523.         },
  524.         multiply: function (a) {
  525.             return new Point(this.x * a, this.y * a)
  526.         },
  527.         divide: function (a) {
  528.             return new Point(this.x / a, this.y / a)
  529.         },
  530.         magnitude: function () {
  531.             return Math.sqrt(this.x * this.x + this.y * this.y)
  532.         },
  533.         normal: function () {
  534.             return new Point(-this.y, this.x)
  535.         },
  536.         normalize: function () {
  537.             return this.divide(this.magnitude())
  538.         }
  539.     }; /*     system.js */
  540.     var ParticleSystem = function (d, p, e, f, t, l, q) {
  541.             var j = [];
  542.             var h = null;
  543.             var k = 0;
  544.             var u = null;
  545.             var m = 0.04;
  546.             var i = [20, 20, 20, 20];
  547.             var n = null;
  548.             var o = null;
  549.             if (typeof p == "object") {
  550.                 var s = p;
  551.                 e = s.friction;
  552.                 d = s.repulsion;
  553.                 t = s.fps;
  554.                 l = s.dt;
  555.                 p = s.stiffness;
  556.                 f = s.gravity;
  557.                 q = s.precision
  558.             }
  559.             e = isNaN(e) ? 0.5 : e;
  560.             d = isNaN(d) ? 1000 : d;
  561.             t = isNaN(t) ? 55 : t;
  562.             p = isNaN(p) ? 600 : p;
  563.             l = isNaN(l) ? 0.02 : l;
  564.             q = isNaN(q) ? 0.6 : q;
  565.             f = (f === true);
  566.             var r = (t !== undefined) ? 1000 / t : 1000 / 50;
  567.             var b = {
  568.                 repulsion: d,
  569.                 stiffness: p,
  570.                 friction: e,
  571.                 dt: l,
  572.                 gravity: f,
  573.                 precision: q,
  574.                 timeout: r
  575.             };
  576.             var a;
  577.             var c = {
  578.                 renderer: null,
  579.                 tween: null,
  580.                 nodes: {},
  581.                 edges: {},
  582.                 adjacency: {},
  583.                 names: {},
  584.                 kernel: null
  585.             };
  586.             var g = {
  587.                 parameters: function (v) {
  588.                     if (v !== undefined) {
  589.                         if (!isNaN(v.precision)) {
  590.                             v.precision = Math.max(0, Math.min(1, v.precision))
  591.                         }
  592.                         $.each(b, function (x, w) {
  593.                             if (v[x] !== undefined) {
  594.                                 b[x] = v[x]
  595.                             }
  596.                         });
  597.                         c.kernel.physicsModified(v)
  598.                     }
  599.                     return b
  600.                 },
  601.                 fps: function (v) {
  602.                     if (v === undefined) {
  603.                         return c.kernel.fps()
  604.                     } else {
  605.                         g.parameters({
  606.                             timeout: 1000 / (v || 50)
  607.                         })
  608.                     }
  609.                 },
  610.                 start: function () {
  611.                     c.kernel.start()
  612.                 },
  613.                 stop: function () {
  614.                     c.kernel.stop()
  615.                 },
  616.                 addNode: function (v, x) {
  617.                     x = x || {};
  618.                     var y = c.names[v];
  619.                     if (y) {
  620.                         y.data = x;
  621.                         return y
  622.                     } else {
  623.                         if (v != undefined) {
  624.                             var w = new Node(x);
  625.                             w.name = v;
  626.                             c.names[v] = w;
  627.                             c.nodes[w._id] = w;
  628.                             j.push({
  629.                                 t: "addNode",
  630.                                 id: w._id,
  631.                                 m: w.mass
  632.                             });
  633.                             g._notify();
  634.                             return w
  635.                         }
  636.                     }
  637.                 },
  638.                 pruneNode: function (w) {
  639.                     var v = g.getNode(w);
  640.                     if (typeof (c.nodes[v._id]) !== "undefined") {
  641.                         delete c.nodes[v._id];
  642.                         delete c.names[v.name]
  643.                     }
  644.                     $.each(c.edges, function (y, x) {
  645.                         if (x.source._id === v._id || x.target._id === v._id) {
  646.                             g.pruneEdge(x)
  647.                         }
  648.                     });
  649.                     j.push({
  650.                         t: "dropNode",
  651.                         id: v._id
  652.                     });
  653.                     g._notify()
  654.                 },
  655.                 getNode: function (v) {
  656.                     if (v._id !== undefined) {
  657.                         return v
  658.                     } else {
  659.                         if (typeof v == "string" || typeof v == "number") {
  660.                             return c.names[v]
  661.                         }
  662.                     }
  663.                 },
  664.                 eachNode: function (v) {
  665.                     $.each(c.nodes, function (y, x) {
  666.                         if (x._p.x == null || x._p.y == null) {
  667.                             return
  668.                         }
  669.                         var w = (u !== null) ? g.toScreen(x._p) : x._p;
  670.                         v.call(g, x, w)
  671.                     })
  672.                 },
  673.                 addEdge: function (z, A, y) {
  674.                     z = g.getNode(z) || g.addNode(z);
  675.                     A = g.getNode(A) || g.addNode(A);
  676.                     y = y || {};
  677.                     var x = new Edge(z, A, y);
  678.                     var B = z._id;
  679.                     var C = A._id;
  680.                     c.adjacency[B] = c.adjacency[B] || {};
  681.                     c.adjacency[B][C] = c.adjacency[B][C] || [];
  682.                     var w = (c.adjacency[B][C].length > 0);
  683.                     if (w) {
  684.                         $.extend(c.adjacency[B][C].data, x.data);
  685.                         return
  686.                     } else {
  687.                         c.edges[x._id] = x;
  688.                         c.adjacency[B][C].push(x);
  689.                         var v = (x.length !== undefined) ? x.length : 1;
  690.                         j.push({
  691.                             t: "addSpring",
  692.                             id: x._id,
  693.                             fm: B,
  694.                             to: C,
  695.                             l: v
  696.                         });
  697.                         g._notify()
  698.                     }
  699.                     return x
  700.                 },
  701.                 pruneEdge: function (A) {
  702.                     j.push({
  703.                         t: "dropSpring",
  704.                         id: A._id
  705.                     });
  706.                     delete c.edges[A._id];
  707.                     for (var v in c.adjacency) {
  708.                         for (var B in c.adjacency[v]) {
  709.                             var w = c.adjacency[v][B];
  710.                             for (var z = w.length - 1; z >= 0; z--) {
  711.                                 if (c.adjacency[v][B][z]._id === A._id) {
  712.                                     c.adjacency[v][B].splice(z, 1)
  713.                                 }
  714.                             }
  715.                         }
  716.                     }
  717.                     g._notify()
  718.                 },
  719.                 getEdges: function (w, v) {
  720.                     w = g.getNode(w);
  721.                     v = g.getNode(v);
  722.                     if (!w || !v) {
  723.                         return []
  724.                     }
  725.                     if (typeof (c.adjacency[w._id]) !== "undefined" && typeof (c.adjacency[w._id][v._id]) !== "undefined") {
  726.                         return c.adjacency[w._id][v._id]
  727.                     }
  728.                     return []
  729.                 },
  730.                 getEdgesFrom: function (v) {
  731.                     v = g.getNode(v);
  732.                     if (!v) {
  733.                         return []
  734.                     }
  735.                     if (typeof (c.adjacency[v._id]) !== "undefined") {
  736.                         var w = [];
  737.                         $.each(c.adjacency[v._id], function (y, x) {
  738.                             w = w.concat(x)
  739.                         });
  740.                         return w
  741.                     }
  742.                     return []
  743.                 },
  744.                 getEdgesTo: function (v) {
  745.                     v = g.getNode(v);
  746.                     if (!v) {
  747.                         return []
  748.                     }
  749.                     var w = [];
  750.                     $.each(c.edges, function (y, x) {
  751.                         if (x.target == v) {
  752.                             w.push(x)
  753.                         }
  754.                     });
  755.                     return w
  756.                 },
  757.                 eachEdge: function (v) {
  758.                     $.each(c.edges, function (z, x) {
  759.                         var y = c.nodes[x.source._id]._p;
  760.                         var w = c.nodes[x.target._id]._p;
  761.                         if (y.x == null || w.x == null) {
  762.                             return
  763.                         }
  764.                         y = (u !== null) ? g.toScreen(y) : y;
  765.                         w = (u !== null) ? g.toScreen(w) : w;
  766.                         if (y && w) {
  767.                             v.call(g, x, y, w)
  768.                         }
  769.                     })
  770.                 },
  771.                 prune: function (w) {
  772.                     var v = {
  773.                         dropped: {
  774.                             nodes: [],
  775.                             edges: []
  776.                         }
  777.                     };
  778.                     if (w === undefined) {
  779.                         $.each(c.nodes, function (y, x) {
  780.                             v.dropped.nodes.push(x);
  781.                             g.pruneNode(x)
  782.                         })
  783.                     } else {
  784.                         g.eachNode(function (y) {
  785.                             var x = w.call(g, y, {
  786.                                 from: g.getEdgesFrom(y),
  787.                                 to: g.getEdgesTo(y)
  788.                             });
  789.                             if (x) {
  790.                                 v.dropped.nodes.push(y);
  791.                                 g.pruneNode(y)
  792.                             }
  793.                         })
  794.                     }
  795.                     return v
  796.                 },
  797.                 graft: function (w) {
  798.                     var v = {
  799.                         added: {
  800.                             nodes: [],
  801.                             edges: []
  802.                         }
  803.                     };
  804.                     if (w.nodes) {
  805.                         $.each(w.nodes, function (y, x) {
  806.                             var z = g.getNode(y);
  807.                             if (z) {
  808.                                 z.data = x
  809.                             } else {
  810.                                 v.added.nodes.push(g.addNode(y, x))
  811.                             }
  812.                             c.kernel.start()
  813.                         })
  814.                     }
  815.                     if (w.edges) {
  816.                         $.each(w.edges, function (z, x) {
  817.                             var y = g.getNode(z);
  818.                             if (!y) {
  819.                                 v.added.nodes.push(g.addNode(z, {}))
  820.                             }
  821.                             $.each(x, function (D, A) {
  822.                                 var C = g.getNode(D);
  823.                                 if (!C) {
  824.                                     v.added.nodes.push(g.addNode(D, {}))
  825.                                 }
  826.                                 var B = g.getEdges(z, D);
  827.                                 if (B.length > 0) {
  828.                                     B[0].data = A
  829.                                 } else {
  830.                                     v.added.edges.push(g.addEdge(z, D, A))
  831.                                 }
  832.                             })
  833.                         })
  834.                     }
  835.                     return v
  836.                 },
  837.                 merge: function (w) {
  838.                     var v = {
  839.                         added: {
  840.                             nodes: [],
  841.                             edges: []
  842.                         },
  843.                         dropped: {
  844.                             nodes: [],
  845.                             edges: []
  846.                         }
  847.                     };
  848.                     $.each(c.edges, function (A, z) {
  849.                         if ((w.edges[z.source.name] === undefined || w.edges[z.source.name][z.target.name] === undefined)) {
  850.                             g.pruneEdge(z);
  851.                             v.dropped.edges.push(z)
  852.                         }
  853.                     });
  854.                     var y = g.prune(function (A, z) {
  855.                         if (w.nodes[A.name] === undefined) {
  856.                             v.dropped.nodes.push(A);
  857.                             return true
  858.                         }
  859.                     });
  860.                     var x = g.graft(w);
  861.                     v.added.nodes = v.added.nodes.concat(x.added.nodes);
  862.                     v.added.edges = v.added.edges.concat(x.added.edges);
  863.                     v.dropped.nodes = v.dropped.nodes.concat(y.dropped.nodes);
  864.                     v.dropped.edges = v.dropped.edges.concat(y.dropped.edges);
  865.                     return v
  866.                 },
  867.                 tweenNode: function (y, v, x) {
  868.                     var w = g.getNode(y);
  869.                     if (w) {
  870.                         c.tween.to(w, v, x)
  871.                     }
  872.                 },
  873.                 tweenEdge: function (w, v, z, y) {
  874.                     if (y === undefined) {
  875.                         g._tweenEdge(w, v, z)
  876.                     } else {
  877.                         var x = g.getEdges(w, v);
  878.                         $.each(x, function (A, B) {
  879.                             g._tweenEdge(B, z, y)
  880.                         })
  881.                     }
  882.                 },
  883.                 _tweenEdge: function (w, v, x) {
  884.                     if (w && w._id !== undefined) {
  885.                         c.tween.to(w, v, x)
  886.                     }
  887.                 },
  888.                 _updateGeometry: function (y) {
  889.                     if (y != undefined) {
  890.                         var v = (y.epoch < k);
  891.                         a = y.energy;
  892.                         var z = y.geometry;
  893.                         if (z !== undefined) {
  894.                             for (var x = 0, w = z.length / 3; x < w; x++) {
  895.                                 var A = z[3 * x];
  896.                                 if (v && c.nodes[A] == undefined) {
  897.                                     continue
  898.                                 }
  899.                                 c.nodes[A]._p.x = z[3 * x + 1];
  900.                                 c.nodes[A]._p.y = z[3 * x + 2]
  901.                             }
  902.                         }
  903.                     }
  904.                 },
  905.                 screen: function (v) {
  906.                     if (v.size !== undefined) {
  907.                         g.screenSize(v.size.width, v.size.height)
  908.                     }
  909.                     if (!isNaN(v.step)) {
  910.                         g.screenStep(v.step)
  911.                     }
  912.                     if (v.padding !== undefined) {
  913.                         g.screenPadding(v.padding)
  914.                     }
  915.                 },
  916.                 screenSize: function (v, w) {
  917.                     u = {
  918.                         width: v,
  919.                         height: w
  920.                     };
  921.                     g._updateBounds()
  922.                 },
  923.                 screenPadding: function (y, z, v, w) {
  924.                     if ($.isArray(y)) {
  925.                         trbl = y
  926.                     } else {
  927.                         trbl = [y, z, v, w]
  928.                     }
  929.                     var A = trbl[0];
  930.                     var x = trbl[1];
  931.                     var B = trbl[2];
  932.                     if (x === undefined) {
  933.                         trbl = [A, A, A, A]
  934.                     } else {
  935.                         if (B == undefined) {
  936.                             trbl = [A, x, A, x]
  937.                         }
  938.                     }
  939.                     i = trbl
  940.                 },
  941.                 screenStep: function (v) {
  942.                     m = v
  943.                 },
  944.                 toScreen: function (x) {
  945.                     if (!n || !u) {
  946.                         return
  947.                     }
  948.                     var w = i || [0, 0, 0, 0];
  949.                     var v = n.bottomright.subtract(n.topleft);
  950.                     var z = w[3] + x.subtract(n.topleft).divide(v.x).x * (u.width - (w[1] + w[3]));
  951.                     var y = w[0] + x.subtract(n.topleft).divide(v.y).y * (u.height - (w[0] + w[2]));
  952.                     return arbor.Point(z, y)
  953.                 },
  954.                 fromScreen: function (z) {
  955.                     if (!n || !u) {
  956.                         return
  957.                     }
  958.                     var y = i || [0, 0, 0, 0];
  959.                     var x = n.bottomright.subtract(n.topleft);
  960.                     var w = (z.x - y[3]) / (u.width - (y[1] + y[3])) * x.x + n.topleft.x;
  961.                     var v = (z.y - y[0]) / (u.height - (y[0] + y[2])) * x.y + n.topleft.y;
  962.                     return arbor.Point(w, v)
  963.                 },
  964.                 _updateBounds: function (w) {
  965.                     if (u === null) {
  966.                         return
  967.                     }
  968.                     if (w) {
  969.                         o = w
  970.                     } else {
  971.                         o = g.bounds()
  972.                     }
  973.                     var z = new Point(o.bottomright.x, o.bottomright.y);
  974.                     var y = new Point(o.topleft.x, o.topleft.y);
  975.                     var B = z.subtract(y);
  976.                     var v = y.add(B.divide(2));
  977.                     var x = 4;
  978.                     var D = new Point(Math.max(B.x, x), Math.max(B.y, x));
  979.                     o.topleft = v.subtract(D.divide(2));
  980.                     o.bottomright = v.add(D.divide(2));
  981.                     if (!n) {
  982.                         if ($.isEmptyObject(c.nodes)) {
  983.                             return false
  984.                         }
  985.                         n = o;
  986.                         return true
  987.                     }
  988.                     var C = m;
  989.                     _newBounds = {
  990.                         bottomright: n.bottomright.add(o.bottomright.subtract(n.bottomright).multiply(C)),
  991.                         topleft: n.topleft.add(o.topleft.subtract(n.topleft).multiply(C))
  992.                     };
  993.                     var A = new Point(n.topleft.subtract(_newBounds.topleft).magnitude(), n.bottomright.subtract(_newBounds.bottomright).magnitude());
  994.                     if (A.x * u.width > 1 || A.y * u.height > 1) {
  995.                         n = _newBounds;
  996.                         return true
  997.                     } else {
  998.                         return false
  999.                     }
  1000.                 },
  1001.                 energy: function () {
  1002.                     return a
  1003.                 },
  1004.                 bounds: function () {
  1005.                     var w = null;
  1006.                     var v = null;
  1007.                     $.each(c.nodes, function (z, y) {
  1008.                         if (!w) {
  1009.                             w = new Point(y._p);
  1010.                             v = new Point(y._p);
  1011.                             return
  1012.                         }
  1013.                         var x = y._p;
  1014.                         if (x.x === null || x.y === null) {
  1015.                             return
  1016.                         }
  1017.                         if (x.x > w.x) {
  1018.                             w.x = x.x
  1019.                         }
  1020.                         if (x.y > w.y) {
  1021.                             w.y = x.y
  1022.                         }
  1023.                         if (x.x < v.x) {
  1024.                             v.x = x.x
  1025.                         }
  1026.                         if (x.y < v.y) {
  1027.                             v.y = x.y
  1028.                         }
  1029.                     });
  1030.                     if (w && v) {
  1031.                         return {
  1032.                             bottomright: w,
  1033.                             topleft: v
  1034.                         }
  1035.                     } else {
  1036.                         return {
  1037.                             topleft: new Point(-1, -1),
  1038.                             bottomright: new Point(1, 1)
  1039.                         }
  1040.                     }
  1041.                 },
  1042.                 nearest: function (x) {
  1043.                     if (u !== null) {
  1044.                         x = g.fromScreen(x)
  1045.                     }
  1046.                     var w = {
  1047.                         node: null,
  1048.                         point: null,
  1049.                         distance: null
  1050.                     };
  1051.                     var v = g;
  1052.                     $.each(c.nodes, function (B, y) {
  1053.                         var z = y._p;
  1054.                         if (z.x === null || z.y === null) {
  1055.                             return
  1056.                         }
  1057.                         var A = z.subtract(x).magnitude();
  1058.                         if (w.distance === null || A < w.distance) {
  1059.                             w = {
  1060.                                 node: y,
  1061.                                 point: z,
  1062.                                 distance: A
  1063.                             };
  1064.                             if (u !== null) {
  1065.                                 w.screenPoint = g.toScreen(z)
  1066.                             }
  1067.                         }
  1068.                     });
  1069.                     if (w.node) {
  1070.                         if (u !== null) {
  1071.                             w.distance = g.toScreen(w.node.p).subtract(g.toScreen(x)).magnitude()
  1072.                         }
  1073.                         return w
  1074.                     } else {
  1075.                         return null
  1076.                     }
  1077.                 },
  1078.                 _notify: function () {
  1079.                     if (h === null) {
  1080.                         k++
  1081.                     } else {
  1082.                         clearTimeout(h)
  1083.                     }
  1084.                     h = setTimeout(g._synchronize, 20)
  1085.                 },
  1086.                 _synchronize: function () {
  1087.                     if (j.length > 0) {
  1088.                         c.kernel.graphChanged(j);
  1089.                         j = [];
  1090.                         h = null
  1091.                     }
  1092.                 },
  1093.             };
  1094.             c.kernel = Kernel(g);
  1095.             c.tween = c.kernel.tween || null;
  1096.             Node.prototype.__defineGetter__("p", function () {
  1097.                 var w = this;
  1098.                 var v = {};
  1099.                 v.__defineGetter__("x", function () {
  1100.                     return w._p.x
  1101.                 });
  1102.                 v.__defineSetter__("x", function (x) {
  1103.                     c.kernel.particleModified(w._id, {
  1104.                         x: x
  1105.                     })
  1106.                 });
  1107.                 v.__defineGetter__("y", function () {
  1108.                     return w._p.y
  1109.                 });
  1110.                 v.__defineSetter__("y", function (x) {
  1111.                     c.kernel.particleModified(w._id, {
  1112.                         y: x
  1113.                     })
  1114.                 });
  1115.                 v.__proto__ = Point.prototype;
  1116.                 return v
  1117.             });
  1118.             Node.prototype.__defineSetter__("p", function (v) {
  1119.                 this._p.x = v.x;
  1120.                 this._p.y = v.y;
  1121.                 c.kernel.particleModified(this._id, {
  1122.                     x: v.x,
  1123.                     y: v.y
  1124.                 })
  1125.             });
  1126.             Node.prototype.__defineGetter__("mass", function () {
  1127.                 return this._mass
  1128.             });
  1129.             Node.prototype.__defineSetter__("mass", function (v) {
  1130.                 this._mass = v;
  1131.                 c.kernel.particleModified(this._id, {
  1132.                     m: v
  1133.                 })
  1134.             });
  1135.             Node.prototype.__defineSetter__("tempMass", function (v) {
  1136.                 c.kernel.particleModified(this._id, {
  1137.                     _m: v
  1138.                 })
  1139.             });
  1140.             Node.prototype.__defineGetter__("fixed", function () {
  1141.                 return this._fixed
  1142.             });
  1143.             Node.prototype.__defineSetter__("fixed", function (v) {
  1144.                 this._fixed = v;
  1145.                 c.kernel.particleModified(this._id, {
  1146.                     f: v ? 1 : 0
  1147.                 })
  1148.             });
  1149.             return g
  1150.         }; /* barnes-hut.js */
  1151.     var BarnesHutTree = function () {
  1152.             var b = [];
  1153.             var a = 0;
  1154.             var e = null;
  1155.             var d = 0.5;
  1156.             var c = {
  1157.                 init: function (g, h, f) {
  1158.                     d = f;
  1159.                     a = 0;
  1160.                     e = c._newBranch();
  1161.                     e.origin = g;
  1162.                     e.size = h.subtract(g)
  1163.                 },
  1164.                 insert: function (j) {
  1165.                     var f = e;
  1166.                     var g = [j];
  1167.                     while (g.length) {
  1168.                         var h = g.shift();
  1169.                         var m = h._m || h.m;
  1170.                         var p = c._whichQuad(h, f);
  1171.                         if (f[p] === undefined) {
  1172.                             f[p] = h;
  1173.                             f.mass += m;
  1174.                             if (f.p) {
  1175.                                 f.p = f.p.add(h.p.multiply(m))
  1176.                             } else {
  1177.                                 f.p = h.p.multiply(m)
  1178.                             }
  1179.                         } else {
  1180.                             if ("origin" in f[p]) {
  1181.                                 f.mass += (m);
  1182.                                 if (f.p) {
  1183.                                     f.p = f.p.add(h.p.multiply(m))
  1184.                                 } else {
  1185.                                     f.p = h.p.multiply(m)
  1186.                                 }
  1187.                                 f = f[p];
  1188.                                 g.unshift(h)
  1189.                             } else {
  1190.                                 var l = f.size.divide(2);
  1191.                                 var n = new Point(f.origin);
  1192.                                 if (p[0] == "s") {
  1193.                                     n.y += l.y
  1194.                                 }
  1195.                                 if (p[1] == "e") {
  1196.                                     n.x += l.x
  1197.                                 }
  1198.                                 var o = f[p];
  1199.                                 f[p] = c._newBranch();
  1200.                                 f[p].origin = n;
  1201.                                 f[p].size = l;
  1202.                                 f.mass = m;
  1203.                                 f.p = h.p.multiply(m);
  1204.                                 f = f[p];
  1205.                                 if (o.p.x === h.p.x && o.p.y === h.p.y) {
  1206.                                     var k = l.x * 0.08;
  1207.                                     var i = l.y * 0.08;
  1208.                                     o.p.x = Math.min(n.x + l.x, Math.max(n.x, o.p.x - k / 2 + Math.random() * k));
  1209.                                     o.p.y = Math.min(n.y + l.y, Math.max(n.y, o.p.y - i / 2 + Math.random() * i))
  1210.                                 }
  1211.                                 g.push(o);
  1212.                                 g.unshift(h)
  1213.                             }
  1214.                         }
  1215.                     }
  1216.                 },
  1217.                 applyForces: function (m, g) {
  1218.                     var f = [e];
  1219.                     while (f.length) {
  1220.                         node = f.shift();
  1221.                         if (node === undefined) {
  1222.                             continue
  1223.                         }
  1224.                         if (m === node) {
  1225.                             continue
  1226.                         }
  1227.                         if ("f" in node) {
  1228.                             var k = m.p.subtract(node.p);
  1229.                             var l = Math.max(1, k.magnitude());
  1230.                             var i = ((k.magnitude() > 0) ? k : Point.random(1)).normalize();
  1231.                             m.applyForce(i.multiply(g * (node._m || node.m)).divide(l * l))
  1232.                         } else {
  1233.                             var j = m.p.subtract(node.p.divide(node.mass)).magnitude();
  1234.                             var h = Math.sqrt(node.size.x * node.size.y);
  1235.                             if (h / j > d) {
  1236.                                 f.push(node.ne);
  1237.                                 f.push(node.nw);
  1238.                                 f.push(node.se);
  1239.                                 f.push(node.sw)
  1240.                             } else {
  1241.                                 var k = m.p.subtract(node.p.divide(node.mass));
  1242.                                 var l = Math.max(1, k.magnitude());
  1243.                                 var i = ((k.magnitude() > 0) ? k : Point.random(1)).normalize();
  1244.                                 m.applyForce(i.multiply(g * (node.mass)).divide(l * l))
  1245.                             }
  1246.                         }
  1247.                     }
  1248.                 },
  1249.                 _whichQuad: function (i, f) {
  1250.                     if (i.p.exploded()) {
  1251.                         return null
  1252.                     }
  1253.                     var h = i.p.subtract(f.origin);
  1254.                     var g = f.size.divide(2);
  1255.                     if (h.y < g.y) {
  1256.                         if (h.x < g.x) {
  1257.                             return "nw"
  1258.                         } else {
  1259.                             return "ne"
  1260.                         }
  1261.                     } else {
  1262.                         if (h.x < g.x) {
  1263.                             return "sw"
  1264.                         } else {
  1265.                             return "se"
  1266.                         }
  1267.                     }
  1268.                 },
  1269.                 _newBranch: function () {
  1270.                     if (b[a]) {
  1271.                         var f = b[a];
  1272.                         f.ne = f.nw = f.se = f.sw = undefined;
  1273.                         f.mass = 0;
  1274.                         delete f.p
  1275.                     } else {
  1276.                         f = {
  1277.                             origin: null,
  1278.                             size: null,
  1279.                             nw: undefined,
  1280.                             ne: undefined,
  1281.                             sw: undefined,
  1282.                             se: undefined,
  1283.                             mass: 0
  1284.                         };
  1285.                         b[a] = f
  1286.                     }
  1287.                     a++;
  1288.                     return f
  1289.                 }
  1290.             };
  1291.             return c
  1292.         }; /*    physics.js */
  1293.     var Physics = function (a, m, n, e, h) {
  1294.             var f = BarnesHutTree();
  1295.             var c = {
  1296.                 particles: {},
  1297.                 springs: {}
  1298.             };
  1299.             var l = {
  1300.                 particles: {}
  1301.             };
  1302.             var o = [];
  1303.             var k = [];
  1304.             var d = 0;
  1305.             var b = {
  1306.                 sum: 0,
  1307.                 max: 0,
  1308.                 mean: 0
  1309.             };
  1310.             var g = {
  1311.                 topleft: new Point(-1, -1),
  1312.                 bottomright: new Point(1, 1)
  1313.             };
  1314.             var j = 1000;
  1315.             var i = {
  1316.                 stiffness: (m !== undefined) ? m : 1000,
  1317.                 repulsion: (n !== undefined) ? n : 600,
  1318.                 friction: (e !== undefined) ? e : 0.3,
  1319.                 gravity: false,
  1320.                 dt: (a !== undefined) ? a : 0.02,
  1321.                 theta: 0.4,
  1322.                 init: function () {
  1323.                     return i
  1324.                 },
  1325.                 modifyPhysics: function (p) {
  1326.                     $.each(["stiffness", "repulsion", "friction", "gravity", "dt", "precision"], function (r, s) {
  1327.                         if (p[s] !== undefined) {
  1328.                             if (s == "precision") {
  1329.                                 i.theta = 1 - p[s];
  1330.                                 return
  1331.                             }
  1332.                             i[s] = p[s];
  1333.                             if (s == "stiffness") {
  1334.                                 var q = p[s];
  1335.                                 $.each(c.springs, function (u, t) {
  1336.                                     t.k = q
  1337.                                 })
  1338.                             }
  1339.                         }
  1340.                     })
  1341.                 },
  1342.                 addNode: function (u) {
  1343.                     var t = u.id;
  1344.                     var q = u.m;
  1345.                     var p = g.bottomright.x - g.topleft.x;
  1346.                     var s = g.bottomright.y - g.topleft.y;
  1347.                     var r = new Point(g.topleft.x + p * Math.random(), g.topleft.y + s * Math.random());
  1348.                     c.particles[t] = new Particle(r, q);
  1349.                     c.particles[t].connections = 0;
  1350.                     l.particles[t] = c.particles[t];
  1351.                     o.push(c.particles[t])
  1352.                 },
  1353.                 dropNode: function (s) {
  1354.                     var r = s.id;
  1355.                     var q = c.particles[r];
  1356.                     var p = $.inArray(q, o);
  1357.                     if (p > -1) {
  1358.                         o.splice(p, 1)
  1359.                     }
  1360.                     delete c.particles[r];
  1361.                     delete l.particles[r]
  1362.                 },
  1363.                 modifyNode: function (r, p) {
  1364.                     if (r in c.particles) {
  1365.                         var q = c.particles[r];
  1366.                         if ("x" in p) {
  1367.                             q.p.x = p.x
  1368.                         }
  1369.                         if ("y" in p) {
  1370.                             q.p.y = p.y
  1371.                         }
  1372.                         if ("m" in p) {
  1373.                             q.m = p.m
  1374.                         }
  1375.                         if ("f" in p) {
  1376.                             q.fixed = (p.f === 1)
  1377.                         }
  1378.                         if ("_m" in p) {
  1379.                             if (q._m === undefined) {
  1380.                                 q._m = q.m
  1381.                             }
  1382.                             q.m = p._m
  1383.                         }
  1384.                     }
  1385.                 },
  1386.                 addSpring: function (t) {
  1387.                     var s = t.id;
  1388.                     var p = t.l;
  1389.                     var r = c.particles[t.fm];
  1390.                     var q = c.particles[t.to];
  1391.                     if (r !== undefined && q !== undefined) {
  1392.                         c.springs[s] = new Spring(r, q, p, i.stiffness);
  1393.                         k.push(c.springs[s]);
  1394.                         r.connections++;
  1395.                         q.connections++;
  1396.                         delete l.particles[t.fm];
  1397.                         delete l.particles[t.to]
  1398.                     }
  1399.                 },
  1400.                 dropSpring: function (s) {
  1401.                     var r = s.id;
  1402.                     var q = c.springs[r];
  1403.                     q.point1.connections--;
  1404.                     q.point2.connections--;
  1405.                     var p = $.inArray(q, k);
  1406.                     if (p > -1) {
  1407.                         k.splice(p, 1)
  1408.                     }
  1409.                     delete c.springs[r]
  1410.                 },
  1411.                 _update: function (p) {
  1412.                     d++;
  1413.                     $.each(p, function (q, r) {
  1414.                         if (r.t in i) {
  1415.                             i[r.t](r)
  1416.                         }
  1417.                     });
  1418.                     return d
  1419.                 },
  1420.                 tick: function () {
  1421.                     i.tendParticles();
  1422.                     i.eulerIntegrator(i.dt);
  1423.                     i.tock()
  1424.                 },
  1425.                 tock: function () {
  1426.                     var p = [];
  1427.                     $.each(c.particles, function (r, q) {
  1428.                         p.push(r);
  1429.                         p.push(q.p.x);
  1430.                         p.push(q.p.y)
  1431.                     });
  1432.                     if (h) {
  1433.                         h({
  1434.                             geometry: p,
  1435.                             epoch: d,
  1436.                             energy: b,
  1437.                             bounds: g
  1438.                         })
  1439.                     }
  1440.                 },
  1441.                 tendParticles: function () {
  1442.                     $.each(c.particles, function (q, p) {
  1443.                         if (p._m !== undefined) {
  1444.                             if (Math.abs(p.m - p._m) < 1) {
  1445.                                 p.m = p._m;
  1446.                                 delete p._m
  1447.                             } else {
  1448.                                 p.m *= 0.98
  1449.                             }
  1450.                         }
  1451.                         p.v.x = p.v.y = 0
  1452.                     })
  1453.                 },
  1454.                 eulerIntegrator: function (p) {
  1455.                     if (i.repulsion > 0) {
  1456.                         if (i.theta > 0) {
  1457.                             i.applyBarnesHutRepulsion()
  1458.                         } else {
  1459.                             i.applyBruteForceRepulsion()
  1460.                         }
  1461.                     }
  1462.                     if (i.stiffness > 0) {
  1463.                         i.applySprings()
  1464.                     }
  1465.                     i.applyCenterDrift();
  1466.                     if (i.gravity) {
  1467.                         i.applyCenterGravity()
  1468.                     }
  1469.                     i.updateVelocity(p);
  1470.                     i.updatePosition(p)
  1471.                 },
  1472.                 applyBruteForceRepulsion: function () {
  1473.                     $.each(c.particles, function (q, p) {
  1474.                         $.each(c.particles, function (s, r) {
  1475.                             if (p !== r) {
  1476.                                 var u = p.p.subtract(r.p);
  1477.                                 var v = Math.max(1, u.magnitude());
  1478.                                 var t = ((u.magnitude() > 0) ? u : Point.random(1)).normalize();
  1479.                                 p.applyForce(t.multiply(i.repulsion * (r._m || r.m) * 0.5).divide(v * v * 0.5));
  1480.                                 r.applyForce(t.multiply(i.repulsion * (p._m || p.m) * 0.5).divide(v * v * -0.5))
  1481.                             }
  1482.                         })
  1483.                     })
  1484.                 },
  1485.                 applyBarnesHutRepulsion: function () {
  1486.                     if (!g.topleft || !g.bottomright) {
  1487.                         return
  1488.                     }
  1489.                     var q = new Point(g.bottomright);
  1490.                     var p = new Point(g.topleft);
  1491.                     f.init(p, q, i.theta);
  1492.                     $.each(c.particles, function (s, r) {
  1493.                         f.insert(r)
  1494.                     });
  1495.                     $.each(c.particles, function (s, r) {
  1496.                         f.applyForces(r, i.repulsion)
  1497.                     })
  1498.                 },
  1499.                 applySprings: function () {
  1500.                     $.each(c.springs, function (t, p) {
  1501.                         var s = p.point2.p.subtract(p.point1.p);
  1502.                         var q = p.length - s.magnitude();
  1503.                         var r = ((s.magnitude() > 0) ? s : Point.random(1)).normalize();
  1504.                         p.point1.applyForce(r.multiply(p.k * q * -0.5));
  1505.                         p.point2.applyForce(r.multiply(p.k * q * 0.5))
  1506.                     })
  1507.                 },
  1508.                 applyCenterDrift: function () {
  1509.                     var q = 0;
  1510.                     var r = new Point(0, 0);
  1511.                     $.each(c.particles, function (t, s) {
  1512.                         r.add(s.p);
  1513.                         q++
  1514.                     });
  1515.                     if (q == 0) {
  1516.                         return
  1517.                     }
  1518.                     var p = r.divide(-q);
  1519.                     $.each(c.particles, function (t, s) {
  1520.                         s.applyForce(p)
  1521.                     })
  1522.                 },
  1523.                 applyCenterGravity: function () {
  1524.                     $.each(c.particles, function (r, p) {
  1525.                         var q = p.p.multiply(-1);
  1526.                         p.applyForce(q.multiply(i.repulsion / 100))
  1527.                     })
  1528.                 },
  1529.                 updateVelocity: function (p) {
  1530.                     $.each(c.particles, function (t, q) {
  1531.                         if (q.fixed) {
  1532.                             q.v = new Point(0, 0);
  1533.                             q.f = new Point(0, 0);
  1534.                             return
  1535.                         }
  1536.                         var s = q.v.magnitude();
  1537.                         q.v = q.v.add(q.f.multiply(p)).multiply(1 - i.friction);
  1538.                         q.f.x = q.f.y = 0;
  1539.                         var r = q.v.magnitude();
  1540.                         if (r > j) {
  1541.                             q.v = q.v.divide(r * r)
  1542.                         }
  1543.                     })
  1544.                 },
  1545.                 updatePosition: function (q) {
  1546.                     var r = 0,
  1547.                         p = 0,
  1548.                         u = 0;
  1549.                     var t = null;
  1550.                     var s = null;
  1551.                     $.each(c.particles, function (w, v) {
  1552.                         v.p = v.p.add(v.v.multiply(q));
  1553.                         var x = v.v.magnitude();
  1554.                         var z = x * x;
  1555.                         r += z;
  1556.                         p = Math.max(z, p);
  1557.                         u++;
  1558.                         if (!t) {
  1559.                             t = new Point(v.p.x, v.p.y);
  1560.                             s = new Point(v.p.x, v.p.y);
  1561.                             return
  1562.                         }
  1563.                         var y = v.p;
  1564.                         if (y.x === null || y.y === null) {
  1565.                             return
  1566.                         }
  1567.                         if (y.x > t.x) {
  1568.                             t.x = y.x
  1569.                         }
  1570.                         if (y.y > t.y) {
  1571.                             t.y = y.y
  1572.                         }
  1573.                         if (y.x < s.x) {
  1574.                             s.x = y.x
  1575.                         }
  1576.                         if (y.y < s.y) {
  1577.                             s.y = y.y
  1578.                         }
  1579.                     });
  1580.                     b = {
  1581.                         sum: r,
  1582.                         max: p,
  1583.                         mean: r / u,
  1584.                         n: u
  1585.                     };
  1586.                     g = {
  1587.                         topleft: s || new Point(-1, -1),
  1588.                         bottomright: t || new Point(1, 1)
  1589.                     }
  1590.                 },
  1591.                 systemEnergy: function (p) {
  1592.                     return b
  1593.                 }
  1594.             };
  1595.             return i.init()
  1596.         };
  1597.     var _nearParticle = function (b, c) {
  1598.             var c = c || 0;
  1599.             var a = b.x;
  1600.             var f = b.y;
  1601.             var e = c * 2;
  1602.             return new Point(a - c + Math.random() * e, f - c + Math.random() * e)
  1603.         };
  1604.  
  1605.     // if called as a worker thread, set up a run loop for the Physics object and bail out
  1606.     if (typeof (window) == 'undefined') return (function () { /* hermetic.js */
  1607.         $ = {
  1608.             each: function (d, e) {
  1609.                 if ($.isArray(d)) {
  1610.                     for (var c = 0, b = d.length; c < b; c++) {
  1611.                         e(c, d[c])
  1612.                     }
  1613.                 } else {
  1614.                     for (var a in d) {
  1615.                         e(a, d[a])
  1616.                     }
  1617.                 }
  1618.             },
  1619.             map: function (a, c) {
  1620.                 var b = [];
  1621.                 $.each(a, function (f, e) {
  1622.                     var d = c(e);
  1623.                     if (d !== undefined) {
  1624.                         b.push(d)
  1625.                     }
  1626.                 });
  1627.                 return b
  1628.             },
  1629.             extend: function (c, b) {
  1630.                 if (typeof b != "object") {
  1631.                     return c
  1632.                 }
  1633.                 for (var a in b) {
  1634.                     if (b.hasOwnProperty(a)) {
  1635.                         c[a] = b[a]
  1636.                     }
  1637.                 }
  1638.                 return c
  1639.             },
  1640.             isArray: function (a) {
  1641.                 if (!a) {
  1642.                     return false
  1643.                 }
  1644.                 return (a.constructor.toString().indexOf("Array") != -1)
  1645.             },
  1646.             inArray: function (c, a) {
  1647.                 for (var d = 0, b = a.length; d < b; d++) {
  1648.                     if (a[d] === c) {
  1649.                         return d
  1650.                     }
  1651.                 }
  1652.                 return -1
  1653.             },
  1654.             isEmptyObject: function (a) {
  1655.                 if (typeof a !== "object") {
  1656.                     return false
  1657.                 }
  1658.                 var b = true;
  1659.                 $.each(a, function (c, d) {
  1660.                     b = false
  1661.                 });
  1662.                 return b
  1663.             },
  1664.         }; /*     worker.js */
  1665.         var PhysicsWorker = function () {
  1666.                 var b = 20;
  1667.                 var a = null;
  1668.                 var d = null;
  1669.                 var c = null;
  1670.                 var g = [];
  1671.                 var f = new Date().valueOf();
  1672.                 var e = {
  1673.                     init: function (h) {
  1674.                         e.timeout(h.timeout);
  1675.                         a = Physics(h.dt, h.stiffness, h.repulsion, h.friction, e.tock);
  1676.                         return e
  1677.                     },
  1678.                     timeout: function (h) {
  1679.                         if (h != b) {
  1680.                             b = h;
  1681.                             if (d !== null) {
  1682.                                 e.stop();
  1683.                                 e.go()
  1684.                             }
  1685.                         }
  1686.                     },
  1687.                     go: function () {
  1688.                         if (d !== null) {
  1689.                             return
  1690.                         }
  1691.                         c = null;
  1692.                         d = setInterval(e.tick, b)
  1693.                     },
  1694.                     stop: function () {
  1695.                         if (d === null) {
  1696.                             return
  1697.                         }
  1698.                         clearInterval(d);
  1699.                         d = null
  1700.                     },
  1701.                     tick: function () {
  1702.                         a.tick();
  1703.                         var h = a.systemEnergy();
  1704.                         if ((h.mean + h.max) / 2 < 0.05) {
  1705.                             if (c === null) {
  1706.                                 c = new Date().valueOf()
  1707.                             }
  1708.                             if (new Date().valueOf() - c > 1000) {
  1709.                                 e.stop()
  1710.                             } else {}
  1711.                         } else {
  1712.                             c = null
  1713.                         }
  1714.                     },
  1715.                     tock: function (h) {
  1716.                         h.type = "geometry";
  1717.                         postMessage(h)
  1718.                     },
  1719.                     modifyNode: function (i, h) {
  1720.                         a.modifyNode(i, h);
  1721.                         e.go()
  1722.                     },
  1723.                     modifyPhysics: function (h) {
  1724.                         a.modifyPhysics(h)
  1725.                     },
  1726.                     update: function (h) {
  1727.                         var i = a._update(h)
  1728.                     }
  1729.                 };
  1730.                 return e
  1731.             };
  1732.         var physics = PhysicsWorker();
  1733.         onmessage = function (a) {
  1734.             if (!a.data.type) {
  1735.                 postMessage("¿kérnèl?");
  1736.                 return
  1737.             }
  1738.             if (a.data.type == "physics") {
  1739.                 var b = a.data.physics;
  1740.                 physics.init(a.data.physics);
  1741.                 return
  1742.             }
  1743.             switch (a.data.type) {
  1744.             case "modify":
  1745.                 physics.modifyNode(a.data.id, a.data.mods);
  1746.                 break;
  1747.             case "changes":
  1748.                 physics.update(a.data.changes);
  1749.                 physics.go();
  1750.                 break;
  1751.             case "start":
  1752.                 physics.go();
  1753.                 break;
  1754.             case "stop":
  1755.                 physics.stop();
  1756.                 break;
  1757.             case "sys":
  1758.                 var b = a.data.param || {};
  1759.                 if (!isNaN(b.timeout)) {
  1760.                     physics.timeout(b.timeout)
  1761.                 }
  1762.                 physics.modifyPhysics(b);
  1763.                 physics.go();
  1764.                 break
  1765.             }
  1766.         };
  1767.     })()
  1768.  
  1769.  
  1770.     arbor = (typeof (arbor) !== 'undefined') ? arbor : {}
  1771.     $.extend(arbor, {
  1772.         // object constructors (don't use ‘new’, just call them)
  1773.         ParticleSystem: ParticleSystem,
  1774.         Point: function (x, y) {
  1775.             return new Point(x, y)
  1776.         },
  1777.  
  1778.         // immutable object with useful methods
  1779.         etc: {
  1780.             trace: trace,
  1781.             // ƒ(msg) -> safe console logging
  1782.             dirname: dirname,
  1783.             // ƒ(path) -> leading part of path
  1784.             basename: basename,
  1785.             // ƒ(path) -> trailing part of path
  1786.             ordinalize: ordinalize,
  1787.             // ƒ(num) -> abbrev integers (and add commas)
  1788.             objcopy: objcopy,
  1789.             // ƒ(old) -> clone an object
  1790.             objcmp: objcmp,
  1791.             // ƒ(a, b, strict_ordering) -> t/f comparison
  1792.             objkeys: objkeys,
  1793.             // ƒ(obj) -> array of all keys in obj
  1794.             objmerge: objmerge,
  1795.             // ƒ(dst, src) -> like $.extend but non-destructive
  1796.             uniq: uniq,
  1797.             // ƒ(arr) -> array of unique items in arr
  1798.             arbor_path: arbor_path,
  1799.             // ƒ() -> guess the directory of the lib code
  1800.         }
  1801.     })
  1802.  
  1803. })(this.jQuery)
  1804.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement