UNPKG

@arcgis/core

Version:

ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API

6 lines (5 loc) 3.59 kB
/* All material copyright ESRI, All Rights Reserved, unless otherwise specified. See https://js.arcgis.com/4.33/esri/copyright.txt for details. */ import{clone as e}from"../../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{collisionGridCellSize as t}from"./config.js";import{GridIndex as r}from"./util.js";import{TranslateAnchor as s,RotationAlignment as o}from"../style/StyleDefinition.js";class n{constructor(s,o,n,i,l,c){this._symbols=s,this._styleRepository=i,this._zoom=l,this._currentLayerCursor=0,this._currentSymbolCursor=0,this._styleProps=new Map,this._allNeededMatrices=new Map,this._gridIndex=new r(o,n,t),this._si=Math.sin(Math.PI*c/180),this._co=Math.cos(Math.PI*c/180),i.cachedStyles&&(this._styleProps=i.cachedStyles);for(const t of s)for(const r of t.symbols)this._allNeededMatrices.has(r.tile)||this._allNeededMatrices.set(r.tile,e(r.tile.transforms.tileUnitsToPixels))}work(e){const t=performance.now();for(;this._currentLayerCursor<this._symbols.length;this._currentLayerCursor++,this._currentSymbolCursor=0){const r=this._symbols[this._currentLayerCursor],s=this._getProperties(r.styleLayerUID),o=this._styleRepository.layerContexts?.get(r.styleLayerUID);for(;this._currentSymbolCursor<r.symbols.length;this._currentSymbolCursor++){if(this._currentSymbolCursor%100==99&&performance.now()-t>e)return!1;const n=r.symbols[this._currentSymbolCursor];if(!n.uniqueSymbol?.show)continue;const i=this._computeCoordinates(n,s,o),l=n.uniqueSymbol;if(!l.show)continue;const{iconAllowOverlap:c,textAllowOverlap:h}=s;for(const e of i){if(!e.enabled)continue;const t=l.parts[e.partIndex];if(!t.show)continue;!(e.partIndex?h:c)&&this._doesCollide(e)&&(e.hard?l.show=!1:t.show=!1)}l.show&&this._insertColliders(l.parts,i,s)}}return!0}_insertColliders(e,t,r){const{iconIgnorePlacement:s,textIgnorePlacement:o}=r;for(const n of t){if(!n.enabled)continue;if(n.partIndex?o:s)continue;if(!e[n.partIndex].show)continue;const t=n.xScreen+n.dxScreen,r=n.yScreen+n.dyScreen,i=t+n.width,l=r+n.height,[c,h,a,d]=this._gridIndex.getCellSpan(t,r,i,l);for(let e=h;e<=d;e++)for(let t=c;t<=a;t++){this._gridIndex.cells[e][t].push(n)}}}_computeCoordinates(e,t,r){const{iconRotationAlignment:n,textRotationAlignment:i,iconTranslate:l,iconTranslateAnchor:c,textTranslate:h,textTranslateAnchor:a}=t,d=this._si,y=this._co,u=this._zoom,x=this._allNeededMatrices.get(e.tile),_=e.uniqueSymbol,f=e.colliders(r);let m=0;for(const S of f){const[e,t]=0===S.partIndex?l:h,r=0===S.partIndex?c:a,_=S.minLod<=u&&u<=S.maxLod;m+=_?0:1,S.enabled=_,S.xScreen=S.xTile*x[0]+S.yTile*x[3]+x[6],S.yScreen=S.xTile*x[1]+S.yTile*x[4]+x[7],r===s.MAP?(S.xScreen+=y*e-d*t,S.yScreen+=d*e+y*t):(S.xScreen+=e,S.yScreen+=t),o.VIEWPORT===(0===S.partIndex?n:i)?(S.dxScreen=S.dxPixels,S.dyScreen=S.dyPixels):(S.dxScreen=y*(S.dxPixels+S.width/2)-d*(S.dyPixels+S.height/2)-S.width/2,S.dyScreen=d*(S.dxPixels+S.width/2)+y*(S.dyPixels+S.height/2)-S.height/2)}return f.length>0&&m===f.length&&_&&(_.show=!1),f}_getProperties(e){const t=this._styleProps.get(e);if(t)return t;const r=this._styleRepository.getLayerStyleProperties?.(e,this._zoom);return this._styleProps.set(e,r),r}_doesCollide(e){const t=e.xScreen+e.dxScreen,r=e.yScreen+e.dyScreen,s=t+e.width,o=r+e.height,[n,i,l,c]=this._gridIndex.getCellSpan(t,r,s,o);for(let h=i;h<=c;h++)for(let i=n;i<=l;i++){const n=this._gridIndex.cells[h][i];for(const i of n){if(null!=i.labelId&&null!=e.labelId&&i.labelId===e.labelId)continue;const n=i.xScreen+i.dxScreen,l=i.yScreen+i.dyScreen,c=n+i.width,h=l+i.height;if(!(s<n||t>c||o<l||r>h))return!0}}return!1}}export{n as CollisionJob};