d3-force-magnetic
Version:
A natural attraction/repulsion force type for the d3-force simulation engine.
3 lines (2 loc) • 18.6 kB
JavaScript
// Version 1.0.4 d3-force-magnetic - https://github.com/vasturiano/d3-force-magnetic
!function(t,n){"object"==typeof exports&&"undefined"!=typeof module?n(exports):"function"==typeof define&&define.amd?define(["exports"],n):n((t="undefined"!=typeof globalThis?globalThis:t||self).d3=t.d3||{})}(this,(function(t){"use strict";function n(t){return n="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},n(t)}function r(t){return function(){return t}}function i(t,n,r){if(isNaN(n))return t;var i,e,s,o,h,u,a=t._root,l={data:r},f=t._x0,_=t._x1;if(!a)return t._root=l,t;for(;a.length;)if((o=n>=(e=(f+_)/2))?f=e:_=e,i=a,!(a=a[h=+o]))return i[h]=l,t;if(n===(s=+t._x.call(null,a.data)))return l.next=a,i?i[h]=l:t._root=l,t;do{i=i?i[h]=new Array(2):t._root=new Array(2),(o=n>=(e=(f+_)/2))?f=e:_=e}while((h=+o)==(u=+(s>=e)));return i[u]=a,i[h]=l,t}function e(t,n,r){this.node=t,this.x0=n,this.x1=r}function s(t){return t[0]}function o(t,n){var r=new h(null==n?s:n,NaN,NaN);return null==t?r:r.addAll(t)}function h(t,n,r){this._x=t,this._x0=n,this._x1=r,this._root=void 0}function u(t){for(var n={data:t.data},r=n;t=t.next;)r=r.next={data:t.data};return n}var a=o.prototype=h.prototype;function l(t,n,r,i){if(isNaN(n)||isNaN(r))return t;var e,s,o,h,u,a,l,f,_,c=t._root,x={data:i},y=t._x0,v=t._y0,d=t._x1,p=t._y1;if(!c)return t._root=x,t;for(;c.length;)if((a=n>=(s=(y+d)/2))?y=s:d=s,(l=r>=(o=(v+p)/2))?v=o:p=o,e=c,!(c=c[f=l<<1|a]))return e[f]=x,t;if(h=+t._x.call(null,c.data),u=+t._y.call(null,c.data),n===h&&r===u)return x.next=c,e?e[f]=x:t._root=x,t;do{e=e?e[f]=new Array(4):t._root=new Array(4),(a=n>=(s=(y+d)/2))?y=s:d=s,(l=r>=(o=(v+p)/2))?v=o:p=o}while((f=l<<1|a)==(_=(u>=o)<<1|h>=s));return e[_]=c,e[f]=x,t}function f(t,n,r,i,e){this.node=t,this.x0=n,this.y0=r,this.x1=i,this.y1=e}function _(t){return t[0]}function c(t){return t[1]}function x(t,n,r){var i=new y(null==n?_:n,null==r?c:r,NaN,NaN,NaN,NaN);return null==t?i:i.addAll(t)}function y(t,n,r,i,e,s){this._x=t,this._y=n,this._x0=r,this._y0=i,this._x1=e,this._y1=s,this._root=void 0}function v(t){for(var n={data:t.data},r=n;t=t.next;)r=r.next={data:t.data};return n}a.copy=function(){var t,n,r=new h(this._x,this._x0,this._x1),i=this._root;if(!i)return r;if(!i.length)return r._root=u(i),r;for(t=[{source:i,target:r._root=new Array(2)}];i=t.pop();)for(var e=0;e<2;++e)(n=i.source[e])&&(n.length?t.push({source:n,target:i.target[e]=new Array(2)}):i.target[e]=u(n));return r},a.add=function(t){const n=+this._x.call(null,t);return i(this.cover(n),n,t)},a.addAll=function(t){Array.isArray(t)||(t=Array.from(t));const n=t.length,r=new Float64Array(n);let e=1/0,s=-1/0;for(let i,o=0;o<n;++o)isNaN(i=+this._x.call(null,t[o]))||(r[o]=i,i<e&&(e=i),i>s&&(s=i));if(e>s)return this;this.cover(e).cover(s);for(let e=0;e<n;++e)i(this,r[e],t[e]);return this},a.cover=function(t){if(isNaN(t=+t))return this;var n=this._x0,r=this._x1;if(isNaN(n))r=(n=Math.floor(t))+1;else{for(var i,e,s=r-n||1,o=this._root;n>t||t>=r;)switch(e=+(t<n),(i=new Array(2))[e]=o,o=i,s*=2,e){case 0:r=n+s;break;case 1:n=r-s}this._root&&this._root.length&&(this._root=o)}return this._x0=n,this._x1=r,this},a.data=function(){var t=[];return this.visit((function(n){if(!n.length)do{t.push(n.data)}while(n=n.next)})),t},a.extent=function(t){return arguments.length?this.cover(+t[0][0]).cover(+t[1][0]):isNaN(this._x0)?void 0:[[this._x0],[this._x1]]},a.find=function(t,n){var r,i,s,o,h,u=this._x0,a=this._x1,l=[],f=this._root;for(f&&l.push(new e(f,u,a)),null==n?n=1/0:(u=t-n,a=t+n);o=l.pop();)if(!(!(f=o.node)||(i=o.x0)>a||(s=o.x1)<u))if(f.length){var _=(i+s)/2;l.push(new e(f[1],_,s),new e(f[0],i,_)),(h=+(t>=_))&&(o=l[l.length-1],l[l.length-1]=l[l.length-1-h],l[l.length-1-h]=o)}else{var c=Math.abs(t-+this._x.call(null,f.data));c<n&&(n=c,u=t-c,a=t+c,r=f.data)}return r},a.remove=function(t){if(isNaN(s=+this._x.call(null,t)))return this;var n,r,i,e,s,o,h,u,a,l=this._root,f=this._x0,_=this._x1;if(!l)return this;if(l.length)for(;;){if((h=s>=(o=(f+_)/2))?f=o:_=o,n=l,!(l=l[u=+h]))return this;if(!l.length)break;n[u+1&1]&&(r=n,a=u)}for(;l.data!==t;)if(i=l,!(l=l.next))return this;return(e=l.next)&&delete l.next,i?(e?i.next=e:delete i.next,this):n?(e?n[u]=e:delete n[u],(l=n[0]||n[1])&&l===(n[1]||n[0])&&!l.length&&(r?r[a]=l:this._root=l),this):(this._root=e,this)},a.removeAll=function(t){for(var n=0,r=t.length;n<r;++n)this.remove(t[n]);return this},a.root=function(){return this._root},a.size=function(){var t=0;return this.visit((function(n){if(!n.length)do{++t}while(n=n.next)})),t},a.visit=function(t){var n,r,i,s,o=[],h=this._root;for(h&&o.push(new e(h,this._x0,this._x1));n=o.pop();)if(!t(h=n.node,i=n.x0,s=n.x1)&&h.length){var u=(i+s)/2;(r=h[1])&&o.push(new e(r,u,s)),(r=h[0])&&o.push(new e(r,i,u))}return this},a.visitAfter=function(t){var n,r=[],i=[];for(this._root&&r.push(new e(this._root,this._x0,this._x1));n=r.pop();){var s=n.node;if(s.length){var o,h=n.x0,u=n.x1,a=(h+u)/2;(o=s[0])&&r.push(new e(o,h,a)),(o=s[1])&&r.push(new e(o,a,u))}i.push(n)}for(;n=i.pop();)t(n.node,n.x0,n.x1);return this},a.x=function(t){return arguments.length?(this._x=t,this):this._x};var d=x.prototype=y.prototype;function p(t,n,r,i,e){if(isNaN(n)||isNaN(r)||isNaN(i))return t;var s,o,h,u,a,l,f,_,c,x,y,v,d=t._root,p={data:e},g=t._x0,w=t._y0,N=t._z0,z=t._x1,A=t._y1,b=t._z1;if(!d)return t._root=p,t;for(;d.length;)if((_=n>=(o=(g+z)/2))?g=o:z=o,(c=r>=(h=(w+A)/2))?w=h:A=h,(x=i>=(u=(N+b)/2))?N=u:b=u,s=d,!(d=d[y=x<<2|c<<1|_]))return s[y]=p,t;if(a=+t._x.call(null,d.data),l=+t._y.call(null,d.data),f=+t._z.call(null,d.data),n===a&&r===l&&i===f)return p.next=d,s?s[y]=p:t._root=p,t;do{s=s?s[y]=new Array(8):t._root=new Array(8),(_=n>=(o=(g+z)/2))?g=o:z=o,(c=r>=(h=(w+A)/2))?w=h:A=h,(x=i>=(u=(N+b)/2))?N=u:b=u}while((y=x<<2|c<<1|_)==(v=(f>=u)<<2|(l>=h)<<1|a>=o));return s[v]=d,s[y]=p,t}function g(t,n,r,i,e,s,o){this.node=t,this.x0=n,this.y0=r,this.z0=i,this.x1=e,this.y1=s,this.z1=o}d.copy=function(){var t,n,r=new y(this._x,this._y,this._x0,this._y0,this._x1,this._y1),i=this._root;if(!i)return r;if(!i.length)return r._root=v(i),r;for(t=[{source:i,target:r._root=new Array(4)}];i=t.pop();)for(var e=0;e<4;++e)(n=i.source[e])&&(n.length?t.push({source:n,target:i.target[e]=new Array(4)}):i.target[e]=v(n));return r},d.add=function(t){const n=+this._x.call(null,t),r=+this._y.call(null,t);return l(this.cover(n,r),n,r,t)},d.addAll=function(t){var n,r,i,e,s=t.length,o=new Array(s),h=new Array(s),u=1/0,a=1/0,f=-1/0,_=-1/0;for(r=0;r<s;++r)isNaN(i=+this._x.call(null,n=t[r]))||isNaN(e=+this._y.call(null,n))||(o[r]=i,h[r]=e,i<u&&(u=i),i>f&&(f=i),e<a&&(a=e),e>_&&(_=e));if(u>f||a>_)return this;for(this.cover(u,a).cover(f,_),r=0;r<s;++r)l(this,o[r],h[r],t[r]);return this},d.cover=function(t,n){if(isNaN(t=+t)||isNaN(n=+n))return this;var r=this._x0,i=this._y0,e=this._x1,s=this._y1;if(isNaN(r))e=(r=Math.floor(t))+1,s=(i=Math.floor(n))+1;else{for(var o,h,u=e-r||1,a=this._root;r>t||t>=e||i>n||n>=s;)switch(h=(n<i)<<1|t<r,(o=new Array(4))[h]=a,a=o,u*=2,h){case 0:e=r+u,s=i+u;break;case 1:r=e-u,s=i+u;break;case 2:e=r+u,i=s-u;break;case 3:r=e-u,i=s-u}this._root&&this._root.length&&(this._root=a)}return this._x0=r,this._y0=i,this._x1=e,this._y1=s,this},d.data=function(){var t=[];return this.visit((function(n){if(!n.length)do{t.push(n.data)}while(n=n.next)})),t},d.extent=function(t){return arguments.length?this.cover(+t[0][0],+t[0][1]).cover(+t[1][0],+t[1][1]):isNaN(this._x0)?void 0:[[this._x0,this._y0],[this._x1,this._y1]]},d.find=function(t,n,r){var i,e,s,o,h,u,a,l=this._x0,_=this._y0,c=this._x1,x=this._y1,y=[],v=this._root;for(v&&y.push(new f(v,l,_,c,x)),null==r?r=1/0:(l=t-r,_=n-r,c=t+r,x=n+r,r*=r);u=y.pop();)if(!(!(v=u.node)||(e=u.x0)>c||(s=u.y0)>x||(o=u.x1)<l||(h=u.y1)<_))if(v.length){var d=(e+o)/2,p=(s+h)/2;y.push(new f(v[3],d,p,o,h),new f(v[2],e,p,d,h),new f(v[1],d,s,o,p),new f(v[0],e,s,d,p)),(a=(n>=p)<<1|t>=d)&&(u=y[y.length-1],y[y.length-1]=y[y.length-1-a],y[y.length-1-a]=u)}else{var g=t-+this._x.call(null,v.data),w=n-+this._y.call(null,v.data),N=g*g+w*w;if(N<r){var z=Math.sqrt(r=N);l=t-z,_=n-z,c=t+z,x=n+z,i=v.data}}return i},d.remove=function(t){if(isNaN(s=+this._x.call(null,t))||isNaN(o=+this._y.call(null,t)))return this;var n,r,i,e,s,o,h,u,a,l,f,_,c=this._root,x=this._x0,y=this._y0,v=this._x1,d=this._y1;if(!c)return this;if(c.length)for(;;){if((a=s>=(h=(x+v)/2))?x=h:v=h,(l=o>=(u=(y+d)/2))?y=u:d=u,n=c,!(c=c[f=l<<1|a]))return this;if(!c.length)break;(n[f+1&3]||n[f+2&3]||n[f+3&3])&&(r=n,_=f)}for(;c.data!==t;)if(i=c,!(c=c.next))return this;return(e=c.next)&&delete c.next,i?(e?i.next=e:delete i.next,this):n?(e?n[f]=e:delete n[f],(c=n[0]||n[1]||n[2]||n[3])&&c===(n[3]||n[2]||n[1]||n[0])&&!c.length&&(r?r[_]=c:this._root=c),this):(this._root=e,this)},d.removeAll=function(t){for(var n=0,r=t.length;n<r;++n)this.remove(t[n]);return this},d.root=function(){return this._root},d.size=function(){var t=0;return this.visit((function(n){if(!n.length)do{++t}while(n=n.next)})),t},d.visit=function(t){var n,r,i,e,s,o,h=[],u=this._root;for(u&&h.push(new f(u,this._x0,this._y0,this._x1,this._y1));n=h.pop();)if(!t(u=n.node,i=n.x0,e=n.y0,s=n.x1,o=n.y1)&&u.length){var a=(i+s)/2,l=(e+o)/2;(r=u[3])&&h.push(new f(r,a,l,s,o)),(r=u[2])&&h.push(new f(r,i,l,a,o)),(r=u[1])&&h.push(new f(r,a,e,s,l)),(r=u[0])&&h.push(new f(r,i,e,a,l))}return this},d.visitAfter=function(t){var n,r=[],i=[];for(this._root&&r.push(new f(this._root,this._x0,this._y0,this._x1,this._y1));n=r.pop();){var e=n.node;if(e.length){var s,o=n.x0,h=n.y0,u=n.x1,a=n.y1,l=(o+u)/2,_=(h+a)/2;(s=e[0])&&r.push(new f(s,o,h,l,_)),(s=e[1])&&r.push(new f(s,l,h,u,_)),(s=e[2])&&r.push(new f(s,o,_,l,a)),(s=e[3])&&r.push(new f(s,l,_,u,a))}i.push(n)}for(;n=i.pop();)t(n.node,n.x0,n.y0,n.x1,n.y1);return this},d.x=function(t){return arguments.length?(this._x=t,this):this._x},d.y=function(t){return arguments.length?(this._y=t,this):this._y};const w=(t,n,r,i,e,s)=>Math.sqrt((t-i)**2+(n-e)**2+(r-s)**2);function N(t){return t[0]}function z(t){return t[1]}function A(t){return t[2]}function b(t,n,r,i){var e=new m(null==n?N:n,null==r?z:r,null==i?A:i,NaN,NaN,NaN,NaN,NaN,NaN);return null==t?e:e.addAll(t)}function m(t,n,r,i,e,s,o,h,u){this._x=t,this._y=n,this._z=r,this._x0=i,this._y0=e,this._z0=s,this._x1=o,this._y1=h,this._z1=u,this._root=void 0}function k(t){for(var n={data:t.data},r=n;t=t.next;)r=r.next={data:t.data};return n}var M=b.prototype=m.prototype;M.copy=function(){var t,n,r=new m(this._x,this._y,this._z,this._x0,this._y0,this._z0,this._x1,this._y1,this._z1),i=this._root;if(!i)return r;if(!i.length)return r._root=k(i),r;for(t=[{source:i,target:r._root=new Array(8)}];i=t.pop();)for(var e=0;e<8;++e)(n=i.source[e])&&(n.length?t.push({source:n,target:i.target[e]=new Array(8)}):i.target[e]=k(n));return r},M.add=function(t){const n=+this._x.call(null,t),r=+this._y.call(null,t),i=+this._z.call(null,t);return p(this.cover(n,r,i),n,r,i,t)},M.addAll=function(t){Array.isArray(t)||(t=Array.from(t));const n=t.length,r=new Float64Array(n),i=new Float64Array(n),e=new Float64Array(n);let s=1/0,o=1/0,h=1/0,u=-1/0,a=-1/0,l=-1/0;for(let f,_,c,x,y=0;y<n;++y)isNaN(_=+this._x.call(null,f=t[y]))||isNaN(c=+this._y.call(null,f))||isNaN(x=+this._z.call(null,f))||(r[y]=_,i[y]=c,e[y]=x,_<s&&(s=_),_>u&&(u=_),c<o&&(o=c),c>a&&(a=c),x<h&&(h=x),x>l&&(l=x));if(s>u||o>a||h>l)return this;this.cover(s,o,h).cover(u,a,l);for(let s=0;s<n;++s)p(this,r[s],i[s],e[s],t[s]);return this},M.cover=function(t,n,r){if(isNaN(t=+t)||isNaN(n=+n)||isNaN(r=+r))return this;var i=this._x0,e=this._y0,s=this._z0,o=this._x1,h=this._y1,u=this._z1;if(isNaN(i))o=(i=Math.floor(t))+1,h=(e=Math.floor(n))+1,u=(s=Math.floor(r))+1;else{for(var a,l,f=o-i||1,_=this._root;i>t||t>=o||e>n||n>=h||s>r||r>=u;)switch(l=(r<s)<<2|(n<e)<<1|t<i,(a=new Array(8))[l]=_,_=a,f*=2,l){case 0:o=i+f,h=e+f,u=s+f;break;case 1:i=o-f,h=e+f,u=s+f;break;case 2:o=i+f,e=h-f,u=s+f;break;case 3:i=o-f,e=h-f,u=s+f;break;case 4:o=i+f,h=e+f,s=u-f;break;case 5:i=o-f,h=e+f,s=u-f;break;case 6:o=i+f,e=h-f,s=u-f;break;case 7:i=o-f,e=h-f,s=u-f}this._root&&this._root.length&&(this._root=_)}return this._x0=i,this._y0=e,this._z0=s,this._x1=o,this._y1=h,this._z1=u,this},M.data=function(){var t=[];return this.visit((function(n){if(!n.length)do{t.push(n.data)}while(n=n.next)})),t},M.extent=function(t){return arguments.length?this.cover(+t[0][0],+t[0][1],+t[0][2]).cover(+t[1][0],+t[1][1],+t[1][2]):isNaN(this._x0)?void 0:[[this._x0,this._y0,this._z0],[this._x1,this._y1,this._z1]]},M.find=function(t,n,r,i){var e,s,o,h,u,a,l,f,_,c=this._x0,x=this._y0,y=this._z0,v=this._x1,d=this._y1,p=this._z1,w=[],N=this._root;for(N&&w.push(new g(N,c,x,y,v,d,p)),null==i?i=1/0:(c=t-i,x=n-i,y=r-i,v=t+i,d=n+i,p=r+i,i*=i);f=w.pop();)if(!(!(N=f.node)||(s=f.x0)>v||(o=f.y0)>d||(h=f.z0)>p||(u=f.x1)<c||(a=f.y1)<x||(l=f.z1)<y))if(N.length){var z=(s+u)/2,A=(o+a)/2,b=(h+l)/2;w.push(new g(N[7],z,A,b,u,a,l),new g(N[6],s,A,b,z,a,l),new g(N[5],z,o,b,u,A,l),new g(N[4],s,o,b,z,A,l),new g(N[3],z,A,h,u,a,b),new g(N[2],s,A,h,z,a,b),new g(N[1],z,o,h,u,A,b),new g(N[0],s,o,h,z,A,b)),(_=(r>=b)<<2|(n>=A)<<1|t>=z)&&(f=w[w.length-1],w[w.length-1]=w[w.length-1-_],w[w.length-1-_]=f)}else{var m=t-+this._x.call(null,N.data),k=n-+this._y.call(null,N.data),M=r-+this._z.call(null,N.data),S=m*m+k*k+M*M;if(S<i){var q=Math.sqrt(i=S);c=t-q,x=n-q,y=r-q,v=t+q,d=n+q,p=r+q,e=N.data}}return e},M.findAllWithinRadius=function(t,n,r,i){const e=[],s=t-i,o=n-i,h=r-i,u=t+i,a=n+i,l=r+i;return this.visit(((f,_,c,x,y,v,d)=>{if(!f.length)do{const s=f.data;w(t,n,r,this._x(s),this._y(s),this._z(s))<=i&&e.push(s)}while(f=f.next);return _>u||c>a||x>l||y<s||v<o||d<h})),e},M.remove=function(t){if(isNaN(s=+this._x.call(null,t))||isNaN(o=+this._y.call(null,t))||isNaN(h=+this._z.call(null,t)))return this;var n,r,i,e,s,o,h,u,a,l,f,_,c,x,y,v=this._root,d=this._x0,p=this._y0,g=this._z0,w=this._x1,N=this._y1,z=this._z1;if(!v)return this;if(v.length)for(;;){if((f=s>=(u=(d+w)/2))?d=u:w=u,(_=o>=(a=(p+N)/2))?p=a:N=a,(c=h>=(l=(g+z)/2))?g=l:z=l,n=v,!(v=v[x=c<<2|_<<1|f]))return this;if(!v.length)break;(n[x+1&7]||n[x+2&7]||n[x+3&7]||n[x+4&7]||n[x+5&7]||n[x+6&7]||n[x+7&7])&&(r=n,y=x)}for(;v.data!==t;)if(i=v,!(v=v.next))return this;return(e=v.next)&&delete v.next,i?(e?i.next=e:delete i.next,this):n?(e?n[x]=e:delete n[x],(v=n[0]||n[1]||n[2]||n[3]||n[4]||n[5]||n[6]||n[7])&&v===(n[7]||n[6]||n[5]||n[4]||n[3]||n[2]||n[1]||n[0])&&!v.length&&(r?r[y]=v:this._root=v),this):(this._root=e,this)},M.removeAll=function(t){for(var n=0,r=t.length;n<r;++n)this.remove(t[n]);return this},M.root=function(){return this._root},M.size=function(){var t=0;return this.visit((function(n){if(!n.length)do{++t}while(n=n.next)})),t},M.visit=function(t){var n,r,i,e,s,o,h,u,a=[],l=this._root;for(l&&a.push(new g(l,this._x0,this._y0,this._z0,this._x1,this._y1,this._z1));n=a.pop();)if(!t(l=n.node,i=n.x0,e=n.y0,s=n.z0,o=n.x1,h=n.y1,u=n.z1)&&l.length){var f=(i+o)/2,_=(e+h)/2,c=(s+u)/2;(r=l[7])&&a.push(new g(r,f,_,c,o,h,u)),(r=l[6])&&a.push(new g(r,i,_,c,f,h,u)),(r=l[5])&&a.push(new g(r,f,e,c,o,_,u)),(r=l[4])&&a.push(new g(r,i,e,c,f,_,u)),(r=l[3])&&a.push(new g(r,f,_,s,o,h,c)),(r=l[2])&&a.push(new g(r,i,_,s,f,h,c)),(r=l[1])&&a.push(new g(r,f,e,s,o,_,c)),(r=l[0])&&a.push(new g(r,i,e,s,f,_,c))}return this},M.visitAfter=function(t){var n,r=[],i=[];for(this._root&&r.push(new g(this._root,this._x0,this._y0,this._z0,this._x1,this._y1,this._z1));n=r.pop();){var e=n.node;if(e.length){var s,o=n.x0,h=n.y0,u=n.z0,a=n.x1,l=n.y1,f=n.z1,_=(o+a)/2,c=(h+l)/2,x=(u+f)/2;(s=e[0])&&r.push(new g(s,o,h,u,_,c,x)),(s=e[1])&&r.push(new g(s,_,h,u,a,c,x)),(s=e[2])&&r.push(new g(s,o,c,u,_,l,x)),(s=e[3])&&r.push(new g(s,_,c,u,a,l,x)),(s=e[4])&&r.push(new g(s,o,h,x,_,c,f)),(s=e[5])&&r.push(new g(s,_,h,x,a,c,f)),(s=e[6])&&r.push(new g(s,o,c,x,_,l,f)),(s=e[7])&&r.push(new g(s,_,c,x,a,l,f))}i.push(n)}for(;n=i.pop();)t(n.node,n.x0,n.y0,n.z0,n.x1,n.y1,n.z1);return this},M.x=function(t){return arguments.length?(this._x=t,this):this._x},M.y=function(t){return arguments.length?(this._y=t,this):this._y},M.z=function(t){return arguments.length?(this._z=t,this):this._z},t.forceMagnetic=function(){var t,i=[],e=[],s=function(t){return t.index},h=function(t){return 100},u=function(t){return 1},a=function(t,n){return null},l=function(t){return 1/(t*t)},f=.9;function _(n){if(e.length)for(var r=0;r<e.length;r++){var s=e[r],_=s.target.x-s.source.x,c=s.target.y-s.source.y||0,y=s.target.z-s.source.z||0,v=q(_,c,y);if(0!==v){var d=n*u(s)*l(v),p=h(s.source),g=h(s.target),w=a(p,g),N=S(g,w)*d,z=S(p,w)*d;s.source.vx+=_/v*N,s.target.vx-=_/v*z,t>1&&(s.source.vy+=c/v*N,s.target.vy-=c/v*z),t>2&&(s.source.vz+=y/v*N,s.target.vz-=y/v*z)}}else for(var A=(1===t?o(i,(function(t){return t.x})):2===t?x(i,(function(t){return t.x}),(function(t){return t.y})):3===t?b(i,(function(t){return t.x}),(function(t){return t.y}),(function(t){return t.z})):null).visitAfter((function(n){var r,i,e,s,o,u,a=0,l=0;if(n.length){for(e=s=o=u=0;u<Math.pow(2,t);++u)(r=n[u])&&(i=Math.abs(r.value))&&(a+=r.value,l+=i,e+=i*(r.x||0),s+=i*(r.y||0),o+=i*(r.z||0));n.x=e/l,t>1&&(n.y=s/l),t>2&&(n.z=o/l)}else{(r=n).x=r.data.x,t>1&&(r.y=r.data.y),t>2&&(r.z=r.data.z);do{a+=h(r.data)}while(r=r.next)}n.value=a})),m=n*u(),k=function(){var n=i[M],r=h(n);A.visit((function(i,e,s,o,h){if(!i.value)return!0;var u=[s,o,h][t-1],_=i.x-n.x,c=i.y-n.y||0,x=i.z-n.z||0,y=q(_,c,x);if((u-e)/y<f)return y>0&&v(),!0;if(!i.length&&0!==y)do{i.data!==n&&v()}while(i=i.next);function v(){var e=S(i.value,a(r,i.value))*m*l(y);n.vx+=_/y*e,t>1&&(n.vy+=c/y*e),t>2&&(n.vz+=x/y*e)}}))},M=0;M<i.length;M++)k();function S(t,n){return null===n?t:Math.abs(t)*(n?1:-1)}function q(t,n,r){return Math.sqrt(t*t+n*n+r*r)}}function c(){var t={};i.forEach((function(n){t[s(n)]=n})),e.forEach((function(r){"object"!==n(r.source)&&(r.source=t[r.source]||r.source),"object"!==n(r.target)&&(r.target=t[r.target]||r.target)}))}return _.initialize=function(n){i=n;for(var r=arguments.length,e=new Array(r>1?r-1:0),s=1;s<r;s++)e[s-1]=arguments[s];t=e.find((function(t){return[1,2,3].includes(t)}))||2,c()},_.links=function(t){return arguments.length?(e=t,c(),_):e},_.id=function(t){return arguments.length?(s=t,_):s},_.charge=function(t){return arguments.length?(h="function"==typeof t?t:r(+t),_):h},_.strength=function(t){return arguments.length?(u="function"==typeof t?t:r(+t),_):u},_.polarity=function(t){return arguments.length?(a="function"==typeof t?t:r(+t),_):a},_.distanceWeight=function(t){return arguments.length?(l=t,_):l},_.theta=function(t){return arguments.length?(f=t,_):f},_}}));