rbush-full
Version:
High-performance 2D spatial index for rectangles (based on R*-tree with bulk loading and bulk insertion algorithms)
2 lines (1 loc) • 11.3 kB
JavaScript
import t from"box-intersect";var e,n,r,i,o,s,h,a,u,l,c,p,f,d,y,b,x,_,v,g="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};l=t,h=function(){var t,e,n,r,i,o=function(){};for(o.get=function(){return e[r++&t-1]},e=[],r=0,n=0,i=t=20;0<=i?n<i:n>i;0<=i?++n:--n)e.push([Infinity,Infinity,-Infinity,-Infinity]);return o}.call(g),n=function(t){function e(e,n){t.call(this,e),this.innerSize=n}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.get=function(){return this.pop()||new t(this.innerSize)},e.prototype.release=function(t){return this.push(t)},e}(e=function(){function t(t,e){void 0===t&&(t=8),void 0===e&&(e={});var n=e.double;void 0===n&&(n=!1),this.size=t,this.double=n,this.current=new Array(this.size),this.double&&(this.other=new Array(this.size)),this.currentLen=0,this[Symbol.iterator]=function(){var t,e,n;return e=0,n=this.currentLen,t=this.current,{next:function(){return e<n?{value:t[e++],done:!1}:{done:!0}}}}}return t.prototype.push=function(t,e){return this.currentLen===this.size&&this.enlarge(),"undefined"!=typeof item2&&null!==item2&&(this.other[this.currentLen]=item2),this.current[this.currentLen++]=t},t.prototype.pop=function(){if(this.currentLen>0)return this.current[--this.currentLen]},t.prototype.enlarge=function(){var t;return this.size=Math.floor(1.5*this.size),this.current.length=this.size,null!=(t=this.other)?t.length=this.size:void 0},t.prototype.clear=function(){return this.currentLen=0},t}()),s=function(t){function e(e){t.call(this,e,{double:!0})}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.push=function(t,e){var n,r,i,o,s;for(this.currentLen===this.size&&this.enlarge(),s=0;s<this.currentLen&&!(e>this.other[s]);)s++;for(;s<=this.currentLen;)i=(n=[this.current[s],this.other[s]])[0],o=n[1],this.current[s]=t,this.other[s]=e,t=(r=[i,o])[0],e=r[1],s++;return this.currentLen++},e}(e),r=function(t){function e(){t.apply(this,arguments),this.overlappingPool=new n(this.size,Math.ceil(this.size/4))}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.push=function(e){return t.prototype.push.apply(this,arguments),null!=e.overlapping?e.overlapping:e.overlapping=this.overlappingPool.get()},e.prototype.remove=function(e){return t.prototype.remove.apply(this,arguments),this.overlappingPool.release(e.overlapping),e.overlapping=null},e}(i=function(t){function e(e){t.call(this,e,{double:!0}),this.removalsCount=0,this[Symbol.iterator]=function(){var t,e,n;return e=0,n=this.currentLen,t=this.current,{next:function(){for(var r;e<n;)if(!(r=t[e++])._removed)return{value:r,done:!1};return{done:!0}}}}}return t&&(e.__proto__=t),e.prototype=Object.create(t&&t.prototype),e.prototype.constructor=e,e.prototype.remove=function(t){return t._removed=!0,this.removalsCount++},e.prototype.maybeCondense=function(t){if(this.removalsCount>(null!=t?t:.1*Math.max(this.currentLen,50)))return this.condense()},e.prototype.condense=function(){var t,e,n,r,i;for(r=0,t=n=0,i=this.currentLen;0<=i?n<i:n>i;t=0<=i?++n:--n)(e=this.current[t])._removed?e._removed=null:this.other[r++]=e;return this.swap(r)},e.prototype.swap=function(t){var e;return this.currentLen=t,e=[this.other,this.current,0],this.current=e[0],this.other=e[1],this.removalsCount=e[2],null},e}(e)),o=function(){function t(t){void 0===t&&(t=9),this._maxEntries=Math.max(4,t),this._minEntries=Math.max(2,Math.ceil(.4*this._maxEntries)),this._collisionRunId=0,this._stacks=[0,1,2,3,4,5,6,7,8].map((function(){return new s(t)})),this.raycastResponse={dist:Infinity,item:null},this.nonStatic=new i(64),this.result=new e(32),this.searchPath=new e(32),this.leafNodes=new r(16),this.clear()}return t.prototype.all=function(t,e){return void 0===e&&(e=this.result),e.currentLen=0,this.searchPath.currentLen=0,this._all(this.data,t,e)},t.prototype.search=function(t,e,n){var r,i,o,s,h;if(void 0===n&&(n=this.result),s=this.data,n.currentLen=0,!_(t,s.bbox))return n;for(this.searchPath.currentLen=0;s;){for(i=0,o=(h=s.children).length;i<o;i++)(r=h[i])._ignore||_(t,r.bbox)&&(s.leaf?(null==e||e(r))&&n.push(r):p(t,r.bbox)?this._all(r,e,n):this.searchPath.push(r));s=this.searchPath.pop()}return n},t.prototype.collides=function(t){var e,n,r,i,o;if(i=this.data,!_(t,i))return!1;for(this.searchPath.currentLen=0;i;){for(n=0,r=(o=i.children).length;n<r;n++)if(e=o[n],_(t,e.bbox)){if(i.leaf||p(t,e.bbox))return!0;this.searchPath.push(e)}i=this.searchPath.pop()}return!1},t.prototype.update=function(t){if(!p(t.parent.bbox,t.bbox))return!0,this.remove(t,!0),this.insert(t,!0)},t.prototype.insert=function(t,e){if(null!=t?t.bbox:void 0)return!t.isStatic&&t._removed&&(t._removed=null,this.nonStatic.removalsCount--,e=!0),this._insert(t),t.isStatic||e||this.nonStatic.push(t),this;log("[RBush::insert] can't add without bbox",t)},t.prototype.clear=function(){return this.data=f([]),this.leafNodes.currentLen=0,this.leafNodes.push(this.data),this},t.prototype.remove=function(t,e){var n,r;if(null!=t){if(-1===(n=(r=t.parent).children.indexOf(t)))throw"[RBush remove] ERROR: parent doesn't have that item";return r.children.splice(n,1),t.isStatic||e||this.nonStatic.remove(t),this._condense(r),this}},t.prototype.checkCollisions=function(){var t,e,n,r,i,o,s,h,a,u,c,p,f,d,y,b,x,v,g,m,I,L,M,w,R,S;for(this.result.currentLen=0,++this._collisionRunId>99999&&(this._collisionRunId=0),x=(t=this.nonStatic).other,n=t.current,r=t.currentLen,t.removalsCount>50&&(S=!0,y=0),c=this.leafNodes.current,i=h=0,I=this.leafNodes.currentLen;0<=I?h<I:h>I;i=0<=I?++h:--h)(u=c[i])._removed||(u.overlapping.currentLen=0);for(l(c,this.leafNodes.currentLen,(function(t,e){t.overlapping.push(e),e.overlapping.push(t)})),o=a=0,L=r;0<=L?a<L:a>L;o=0<=L?++a:--a)if(!(s=n[o])._removed&&(S&&(x[y++]=s),!s._ignore)){for(s._colRunId=this._collisionRunId,i=d=0,p=(M=(u=s.parent).children).length;d<p;i=++d)(e=M[i])._ignore||e._colRunId===this._collisionRunId||_(s.bbox,e.bbox)&&(this.result.push(s),this.result.push(e));if((g=u.overlapping).currentLen)for(i=b=0,w=g.currentLen;0<=w?b<w:b>w;i=0<=w?++b:--b)if(v=g.current[i],_(s.bbox,v.bbox))for(m=0,f=(R=v.children).length;m<f;m++)(e=R[m])._ignore||e._colRunId===this._collisionRunId||_(s.bbox,e.bbox)&&(this.result.push(s),this.result.push(e))}return S&&this.nonStatic.swap(y),this.result},t.prototype.raycast=function(t,e,n,r){var i,o,s,h,a,u,l,c,p,f,d,y,b,x,_,g,m,I,L,M;for(void 0===n&&(n=Infinity),b=this.data,h=1/e.x,a=1/e.y,o=t.x+e.x*n,s=t.y+e.y*n,M=Infinity,u=null,l=0,p=(_=this._stacks).length;l<p;l++)(I=_[l]).currentLen=0;for(;b;){if(I=this._stacks[b.height],b.leaf)for(c=0,f=(g=b.children).length;c<f;c++)(i=g[c])._ignore||(null==r||r(i))&&(L=v(t.x,t.y,h,a,i.bbox))<n&&L<M&&(null!=this._rayObjectDistance&&(L=this._rayObjectDistance(L,t,e,o,s,n,i)),L<n&&L<M&&(M=L,u=i));else for(y=0,d=(m=b.children).length;y<d;y++)(i=m[y])._ignore||(L=v(t.x,t.y,h,a,i.bbox))<n&&L<M&&I.push(i,L);for(;b;){if(x=this._stacks[b.height].pop()){b=x;break}b=b.parent}}return this.raycastResponse.dist=M,this.raycastResponse.item=u,this.raycastResponse},t.prototype._all=function(t,e,n){var r,i,o,s,h,a,u,l;for(i=this.searchPath.currentLen;t;){if(t.leaf)for(o=0,h=(u=t.children).length;o<h;o++)(r=u[o])._ignore||(null==e||e(r))&&n.push(r);else for(s=0,a=(l=t.children).length;s<a;s++)r=l[s],this.searchPath.push(r);if(this.searchPath.currentLen===i)break;t=this.searchPath.pop()}return n},t.prototype._chooseSubtree=function(t,e){for(var n,r,i,o,s,h,u,l,c;!e.leaf;){for(h=Infinity,u=Infinity,c=null,o=0,s=(l=e.children).length;o<s;o++)r=l[o],n=a(r.bbox),(i=y(t,r.bbox)-n)<u?(u=i,h=n<h?n:h,c=r):i===u&&n<h&&(h=n,c=r);e=c||e.children[0]}return e},t.prototype._insert=function(t){var e,n,r;for(e=t.bbox,(n=this._chooseSubtree(e,this.data)).children.push(t),t.parent=n,b(n.bbox,e),r=n;null!=r;)r.children.length>this._maxEntries&&this._split(r),r=r.parent;return this._adjustParentBBoxes(e,n.parent)},t.prototype._split=function(t){var e,n,r,i;return e=t.children.length,n=this._minEntries,this._chooseSplitAxis(t,n,e),i=this._chooseSplitIndex(t,n,e),(r=f(t.children.splice(i,t.children.length-i))).height=t.height,r.leaf=t.leaf,r.leaf&&this.leafNodes.push(r),c(t),c(r),null!=t.parent?(t.parent.children.push(r),r.parent=t.parent):this._splitRoot(t,r)},t.prototype._splitRoot=function(t,e){return this.data=f([t,e]),this.data.height=t.height+1,this.data.height===this._stacks.length&&this._stacks.push(new s(this._maxEntries)),this.data.leaf=!1,c(this.data)},t.prototype._chooseSplitIndex=function(t,e,n){var r,i,o,s,h,u,l,c,p,f,y;for(h=null,c=Infinity,l=Infinity,s=e-1,s=u=f=e,y=n-e;f<=y?u<=y:u>=y;s=f<=y?++u:--u)i=d(t,0,s),o=d(t,s,n),p=x(i,o),r=a(i)+a(o),p<c?(c=p,h=s,l=r<l?r:l):p===c&&r<l&&(l=r,h=s);return h||n-e},t.prototype._chooseSplitAxis=function(t,e,n){if(this._allDistMargin(t,e,n,(function(t,e){return t.bbox[0]-e.bbox[0]}))<this._allDistMargin(t,e,n,(function(t,e){return t.bbox[1]-e.bbox[1]})))return t.children.sort((function(t,e){return t.bbox[0]-e.bbox[0]}))},t.prototype._allDistMargin=function(t,e,n,r){var i,o,s,h,a,l,c,p,f,y,x;for(t.children.sort(r),a=d(t,0,e),x=d(t,n-e,n),l=u(a)+u(x),o=s=c=e,p=n-e;c<=p?s<p:s>p;o=c<=p?++s:--s)i=t.children[o],b(a,i.bbox),l+=u(a);for(o=h=f=n-e-1,y=e;f<=y?h<=y:h>=y;o=f<=y?++h:--h)i=t.children[o],b(x,i.bbox),l+=u(x);return l},t.prototype._adjustParentBBoxes=function(t,e){for(;e;)b(e.bbox,t),e=e.parent;return null},t.prototype._condense=function(t){for(var e;t;){if(0===t.children.length){if(null==t.parent)return this.clear();(e=t.parent.children).splice(e.indexOf(t),1),t.leaf&&(this.leafNodes.remove(t),this.leafNodes.maybeCondense())}else c(t);t=t.parent}return null},t}(),c=function(t){return d(t,0,t.children.length,t)},d=function(t,e,n,r){var i,o,s,a,u,l;for((i=null!=(a=null!=r?r.bbox:void 0)?a:h.get())[0]=Infinity,i[1]=Infinity,i[2]=-Infinity,i[3]=-Infinity,o=s=u=e,l=n;u<=l?s<l:s>l;o=u<=l?++s:--s)b(i,t.children[o].bbox);return i},b=function(t,e){return t[0]=Math.min(t[0],e[0]),t[1]=Math.min(t[1],e[1]),t[2]=Math.max(t[2],e[2]),t[3]=Math.max(t[3],e[3]),t},a=function(t){return(t[2]-t[0])*(t[3]-t[1])},u=function(t){return t[2]-t[0]+(t[3]-t[1])},y=function(t,e){return(Math.max(e[2],t[2])-Math.min(e[0],t[0]))*(Math.max(e[3],t[3])-Math.min(e[1],t[1]))},x=function(t,e){var n,r,i,o;return i=Math.max(t[0],e[0]),o=Math.max(t[1],e[1]),n=Math.min(t[2],e[2]),r=Math.min(t[3],e[3]),Math.max(0,n-i)*Math.max(0,r-o)},p=function(t,e){return t[0]<=e[0]&&t[1]<=e[1]&&e[2]<=t[2]&&e[3]<=t[3]},_=function(t,e){return e[0]<=t[2]&&e[1]<=t[3]&&e[2]>=t[0]&&e[3]>=t[1]},f=function(t){var e,n,r;for(r={children:t,height:1,leaf:!0,bbox:[Infinity,Infinity,-Infinity,-Infinity]},e=0,n=t.length;e<n;e++)t[e].parent=r;return r};var m={RBush:o,boxIntersect:l,rayBboxDistance:v=function(t,e,n,r,i){var o,s,h,a,u,l;return h=(i[0]-t)*n,a=(i[2]-t)*n,s=Math.min(h,a),o=Math.max(h,a),u=(i[1]-e)*r,l=(i[3]-e)*r,s=Math.max(s,Math.min(u,l)),(o=Math.min(o,Math.max(u,l)))>Math.max(s,0)?s:Infinity},GrowingArray:e,GrowingArrayPool:n,ObjectStorage:i},I=m.RBush,L=m.boxIntersect,M=m.rayBboxDistance,w=m.GrowingArray,R=m.GrowingArrayPool,S=m.ObjectStorage;export default m;export{w as GrowingArray,R as GrowingArrayPool,S as ObjectStorage,I as RBush,L as boxIntersect,M as rayBboxDistance};