UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 2.05 kB
import{clamp as t}from"../../../../../core/mathUtils.js";import{StaticBitSet as s}from"../../../layers/features/support/StaticBitSet.js";const e=2,i=1,o=0,h=1,r=2;class l{constructor(t,e,o){this._debugMap=new Map,this._width=t*o,this._height=e*o,this._pixelRatio=o;const h=Math.ceil(this._width/i),r=Math.ceil(this._height/i);this._cols=h,this._rows=r,this._cells=s.create(h*r)}insertMetrics(t){const s=this._hasCollision(t);return s===o&&this._markMetrics(t),s}getCellId(t,s){return t+s*this._cols}has(t){return this._cells.has(t)}hasRange(t,s){return this._cells.hasRange(t,s)}set(t){this._cells.set(t)}setRange(t,s){this._cells.setRange(t,s)}_collide(s,e,l,n){const c=s-l/2,a=e-n/2,_=c+l,d=a+n;if(_<0||d<0||c>this._width||a>this._height)return h;const u=t(Math.floor(c/i),0,this._cols),p=t(Math.floor(a/i),0,this._rows),M=t(Math.ceil(_/i),0,this._cols),f=t(Math.ceil(d/i),0,this._rows);for(let t=p;t<=f;t++)for(let s=u;s<=M;s++){const e=this.getCellId(s,t);if(this.has(e))return r}return o}_mark(s,e,o,h,r){const l=s-o/2,n=e-h/2,c=l+o,a=n+h,_=t(Math.floor(l/i),0,this._cols),d=t(Math.floor(n/i),0,this._rows),u=t(Math.ceil(c/i),0,this._cols),p=t(Math.ceil(a/i),0,this._rows);for(let t=d;t<=p;t++)for(let s=_;s<=u;s++){const e=this.getCellId(s,t);this._debugMap.set(e,r),this.set(e)}return!1}_hasCollision(t){const s=t.id;let i=0,l=0;t.save();do{const s=t.boundsCount;i+=s;for(let i=0;i<s;i++){const s=t.boundsComputedAnchorX(i),o=t.boundsComputedAnchorY(i),n=(t.boundsWidth(i)+e)*this._pixelRatio,c=(t.boundsHeight(i)+e)*this._pixelRatio;switch(this._collide(s,o,n,c)){case r:return r;case h:l++}}}while(t.peekId()===s&&t.next());return t.restore(),i===l?h:o}_markMetrics(t){const s=t.id;t.save();do{const s=t.boundsCount;for(let i=0;i<s;i++){const s=t.boundsComputedAnchorX(i),o=t.boundsComputedAnchorY(i),h=(t.boundsWidth(i)+e)*this._pixelRatio,r=(t.boundsHeight(i)+e)*this._pixelRatio;this._mark(s,o,h,r,t.id)}}while(t.peekId()===s&&t.next());t.restore()}}export{l as CollisionBitsetGrid,r as HAS_COLLISION,o as NONE,h as OUTSIDE_EXTENT};