forked from davidson16807/fast-multipole-method
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfast-multipole-method.min.js
1 lines (1 loc) · 4.86 KB
/
fast-multipole-method.min.js
1
"use strict";var FMM=function(){var r={};return r.Field2=function(r,n,t,o,e,u){function i(r,n,t){return{level:r,x:n,y:t}}function f(r,n,t){return{x:r,y:n}}function a(r){return r.level+","+r.x+","+r.y}function c(r){return void 0!==r.x&&void 0!==r.y?r:f(r[0],r[1])}function l(r,n){return f(n.x-r.x,n.y-r.y)}function v(r){return i(r.level+1,Math.floor(r.x/2),Math.floor(r.y/2))}function d(r){for(var n={},t=0,o=r.length;t<o;t++)n[a(u=v(r[t]))]=u;var e=[];for(var u in n)e.push(n[u]);return e}function h(r){for(var n=[],t=0;t<=1;t++)for(var o=0;o<=1;o++)n.push(i(r.level-1,2*r.x+t,2*r.y+o));return n}function M(r){return f((r.x+.5)*Math.pow(2,r.level),(r.y+.5)*Math.pow(2,r.level))}function g(r,n){return i(n,Math.floor(r.x/Math.pow(2,n)),Math.floor(r.y/Math.pow(2,n)))}function x(r){for(var n=[],t=z;t<F;t++)n.push(g(r,t));return n}function y(r,n){for(var t=g(r,n),o=[],e=-1;e<=1;e++)for(var u=-1;u<=1;u++)o.push(i(n,t.x+e,t.y+u));return o}function _(r){for(var n=[],t=z;t<F;t++)n.push(y(r,t));return n}function p(r,n,t,o,e){var u=g(n,z),i=_(n),f={};f[a(u)]=u;for(var v=0,x=i.length;v<x;v++)for(var y=d(i[v]),p=0,E=y.length;p<E;p++){var s=y[p];f[a(s)]=s;for(var F=h(s),w=0,V=F.length;w<V;w++){var H=F[w],R=a(H);if(void 0===f[R]){var T=c(o(l(M(H),n),t)),m=r[R];r[R]=void 0!==m?e(m,T):T}}}}function E(r,n,t){for(var o in n)void 0!==r[o]?r[o]=t(r[o],n[o]):r[o]=n[o]}function s(r,n,t){for(var e=t,u=x(n),i=0,f=u.length;i<f;i++){var c=u[i];void 0!==r[a(c)]&&(e=void 0!==e?o(r[a(c)],e):r[a(c)])}return e}var z=r?Math.floor(Math.log(r)/Math.log(2)):0,F=n?Math.floor(Math.log(n)/Math.log(2)):0,w={};return w._grid={},w.value=function(r){return s(w._grid,c(r),u)},w.clear=function(){w._grid={}},w.add_field=function(r){E(w._grid,r._grid,o)},w.remove_field=function(r){E(w._grid,r._grid,e)},w.add_particle=function(r,n){n=n||{},p(w._grid,c(r),n,t,o)},w.remove_particle=function(r,n){n=n||{},p(w._grid,c(r),n,t,e)},w},r.ScalarField2=function(n,t,o,e){return r.Field2(n,t,o,function(r,n){return r+n},function(r,n){return r-n},e||0)},r.VectorField2=function(n,t,o,e){return r.Field2(n,t,o,function(r,n){return{x:r.x+n.x,y:r.y+n.y}},function(r,n){return{x:r.x-n.x,y:r.y-n.y}},e||{x:0,y:0})},r.Field3=function(r,n,t,o,e,u){function i(r,n,t,o){return{level:r,x:n,y:t,z:o}}function f(r,n,t){return{x:r,y:n,z:t}}function a(r){return r.level+","+r.x+","+r.y+","+r.z}function c(r){return void 0!==r.x&&void 0!==r.y&&void 0!==r.z?r:f(r[0],r[1],r[2])}function l(r,n){return f(n.x-r.x,n.y-r.y,n.z-r.z)}function v(r){return i(r.level+1,Math.floor(r.x/2),Math.floor(r.y/2),Math.floor(r.z/2))}function d(r){for(var n={},t=0,o=r.length;t<o;t++)n[a(u=v(r[t]))]=u;var e=[];for(var u in n)e.push(n[u]);return e}function h(r){for(var n=[],t=0;t<=1;t++)for(var o=0;o<=1;o++)for(var e=0;e<=1;e++)n.push(i(r.level-1,2*r.x+t,2*r.y+o,2*r.z+e));return n}function M(r){return f((r.x+.5)*Math.pow(2,r.level),(r.y+.5)*Math.pow(2,r.level),(r.z+.5)*Math.pow(2,r.level))}function g(r,n){return i(n,Math.floor(r.x/Math.pow(2,n)),Math.floor(r.y/Math.pow(2,n)),Math.floor(r.z/Math.pow(2,n)))}function x(r){for(var n=[],t=z;t<F;t++)n.push(g(r,t));return n}function y(r,n){for(var t=g(r,n),o=[],e=-1;e<=1;e++)for(var u=-1;u<=1;u++)for(var f=-1;f<=1;f++)o.push(i(n,t.x+e,t.y+u,t.z+f));return o}function _(r){for(var n=[],t=z;t<F;t++)n.push(y(r,t));return n}function p(r,n,t,o,e){var u=g(n,z),i=_(n),f={};f[a(u)]=u;for(var v=0,x=i.length;v<x;v++)for(var y=d(i[v]),p=0,E=y.length;p<E;p++){var s=y[p];f[a(s)]=s;for(var F=h(s),w=0,V=F.length;w<V;w++){var H=F[w],R=a(H);if(void 0===f[R]){var T=c(o(l(M(H),n),t)),m=r[R];r[R]=void 0!==m?e(m,T):T}}}}function E(r,n,t){for(var o in n)void 0!==r[o]?r[o]=t(r[o],n[o]):r[o]=n[o]}function s(r,n,t){for(var e=t,u=x(n),i=0,f=u.length;i<f;i++){var c=u[i];void 0!==r[a(c)]&&(e=void 0!==e?o(r[a(c)],e):r[a(c)])}return e}var z=r?Math.floor(Math.log(r)/Math.log(2)):0,F=n?Math.floor(Math.log(n)/Math.log(2)):0,w={};return w._grid={},w.value=function(r){return s(w._grid,c(r),u)},w.clear=function(){w._grid={}},w.add_field=function(r){E(w._grid,r._grid,o)},w.remove_field=function(r){E(w._grid,r._grid,e)},w.add_particle=function(r,n){n=n||{},p(w._grid,c(r),n,t,o)},w.remove_particle=function(r,n){n=n||{},p(w._grid,c(r),n,t,e)},w},r.ScalarField3=function(n,t,o,e){return r.Field3(n,t,o,function(r,n){return r+n},function(r,n){return r-n},e||0)},r.VectorField3=function(n,t,o,e){return r.Field3(n,t,o,function(r,n){return Point(r.x+n.x,r.y+n.y,r.z+n.z)},function(r,n){return Point(r.x-n.x,r.y-n.y,r.z-n.z)},e||{x:0,y:0,z:0})},r}(),THREE=THREE||{};THREE.VectorField2=function(r,n,t){return FMM.Field2(r,n,function(r,n){return t(new THREE.Vector2(r.x,r.y),n)},function(r,n){return THREE.Vector2.addVectors(r,n)},function(r,n){return THREE.Vector2.subVectors(r,n)})},THREE.VectorField3=function(r,n,t){return FMM.Field3(r,n,function(r,n){return t(new THREE.Vector3(r.x,r.y,r.z),n)},function(r,n){return(new THREE.Vector3).addVectors(r,n)},function(r,n){return(new THREE.Vector3).subVectors(r,n)})};