UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 6.47 kB
import t from"../../../../../core/ArrayPool.js";import{e}from"../../../../../chunks/earcut.js";import{triangulate as i}from"../../../../../geometry/libtess.js";import{Point as r}from"../../../../../geometry/support/TileClipper.js";import{BucketType as l}from"../enums.js";import n from"./BaseBucket.js";class s extends n{constructor(t,e,i,r,n,s,o){super(t,e,i),this.type=l.FILL,this._patternMap=new Map,this._fillVertexBuffer=r,this._fillIndexBuffer=n,this._outlineVertexBuffer=s,this._outlineIndexBuffer=o}get fillIndexStart(){return this._fillIndexStart}get fillIndexCount(){return this._fillIndexCount}get outlineIndexStart(){return this._outlineIndexStart}get outlineIndexCount(){return this._outlineIndexCount}getResources(t,e,i){const r=this.layer,l=this.zoom,n=r.getPaintProperty("fill-pattern");if(n)if(n.isDataDriven)for(const s of this._features)e(n.getValue(l,s),!0);else e(n.getValue(l),!0)}processFeatures(t){this._fillIndexStart=3*this._fillIndexBuffer.index,this._fillIndexCount=0,this._outlineIndexStart=3*this._outlineIndexBuffer.index,this._outlineIndexCount=0;const e=this.layer,i=this.zoom,{fillMaterial:r,outlineMaterial:l,hasDataDrivenFill:n,hasDataDrivenOutline:s}=e;t&&t.setExtent(this.layerExtent);const o=e.getPaintProperty("fill-pattern"),a=o?.isDataDriven;let f=!o&&e.getPaintValue("fill-antialias",i);if(e.outlineUsesFillColor){if(f&&!e.hasDataDrivenOpacity){const t=e.getPaintValue("fill-opacity",i),r=e.getPaintValue("fill-opacity",i+1);t<1&&r<1&&(f=!1)}if(f&&!e.hasDataDrivenColor){const t=e.getPaintValue("fill-color",i),r=e.getPaintValue("fill-color",i+1);t[3]<1&&r[3]<1&&(f=!1)}}const u=this._features,d=t?.validateTessellation;if(a){const n=[];for(const a of u){const u=o.getValue(i,a),h=this._spriteInfo[u];if(!h||!h.rect)continue;const x=r.encodeAttributes(a,i,e,h),c=f&&s?l.encodeAttributes(a,i,e):[],_=a.getGeometry(t);n.push({ddFillAttributes:x,ddOutlineAttributes:c,page:h.page,geometry:_}),n.sort(((t,e)=>t.page-e.page));for(const{ddFillAttributes:t,ddOutlineAttributes:i,page:r,geometry:l}of n)this._processFeature(l,f,e.outlineUsesFillColor,t,i,d,r)}}else for(const h of u){const o=n?r.encodeAttributes(h,i,e):null,a=f&&s?l.encodeAttributes(h,i,e):null,u=h.getGeometry(t);this._processFeature(u,f,e.outlineUsesFillColor,o,a,d)}}serialize(){let t=10;t+=this.layerUIDs.length,t+=this._fillVertexBuffer.array.length,t+=this._fillIndexBuffer.array.length,t+=this._outlineVertexBuffer.array.length,t+=this._outlineIndexBuffer.array.length,t+=3*this._patternMap.size+1;const e=new Uint32Array(t),i=new Int32Array(e.buffer);let r=0;e[r++]=this.type,e[r++]=this.layerUIDs.length;for(let s=0;s<this.layerUIDs.length;s++)e[r++]=this.layerUIDs[s];e[r++]=this._fillIndexStart,e[r++]=this._fillIndexCount,e[r++]=this._outlineIndexStart,e[r++]=this._outlineIndexCount;const l=this._patternMap,n=l.size;if(e[r++]=n,n>0)for(const[s,[o,a]]of l)e[r++]=s,e[r++]=o,e[r++]=a;e[r++]=this._fillVertexBuffer.array.length;for(let s=0;s<this._fillVertexBuffer.array.length;s++)i[r++]=this._fillVertexBuffer.array[s];e[r++]=this._fillIndexBuffer.array.length;for(let s=0;s<this._fillIndexBuffer.array.length;s++)e[r++]=this._fillIndexBuffer.array[s];e[r++]=this._outlineVertexBuffer.array.length;for(let s=0;s<this._outlineVertexBuffer.array.length;s++)i[r++]=this._outlineVertexBuffer.array[s];e[r++]=this._outlineIndexBuffer.array.length;for(let s=0;s<this._outlineIndexBuffer.array.length;s++)e[r++]=this._outlineIndexBuffer.array[s];return e.buffer}_processFeature(t,e,i,r,l,n,o){if(!t)return;const a=t.length,f=!l||0===l.length;if(e&&(!i||f))for(let s=0;s<a;s++)this._processOutline(t[s],l);const u=32;let d;for(let h=0;h<a;h++){const e=s._area(t[h]);e>u?(void 0!==d&&this._processFill(t,d,r,n,o),d=[h]):e<-u&&void 0!==d&&d.push(h)}void 0!==d&&this._processFill(t,d,r,n,o)}_processOutline(t,e){const i=this._outlineVertexBuffer,l=this._outlineIndexBuffer,n=l.index;let s,o,a;const f=new r(0,0),u=new r(0,0),d=new r(0,0);let h=-1,x=-1,c=-1,_=-1,y=-1,g=!1;const p=0;let I=t.length;if(I<2)return;const B=t[p];let m=t[I-1];for(;I&&m.isEqual(B);)--I,m=t[I-1];if(!(I-p<2)){for(let r=p;r<I;++r){r===p?(s=t[I-1],o=t[p],a=t[p+1],f.assignSub(o,s),f.normalize(),f.rightPerpendicular()):(s=o,o=a,a=r!==I-1?t[r+1]:t[p],f.assign(u));const n=this._isClipEdge(s,o);-1===_&&(g=n),u.assignSub(a,o),u.normalize(),u.rightPerpendicular();const B=f.x*u.y-f.y*u.x;d.assignAdd(f,u),d.normalize();const m=-d.x*-f.x+-d.y*-f.y;let V=Math.abs(0!==m?1/m:1);V>8&&(V=8),B>=0?(c=i.add(o.x,o.y,f.x,f.y,0,1,e),-1===_&&(_=c),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),x=i.add(o.x,o.y,V*-d.x,V*-d.y,0,-1,e),-1===y&&(y=x),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),h=x,x=c,c=i.add(o.x,o.y,d.x,d.y,0,1,e),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),x=i.add(o.x,o.y,u.x,u.y,0,1,e),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c)):(c=i.add(o.x,o.y,V*d.x,V*d.y,0,1,e),-1===_&&(_=c),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),x=i.add(o.x,o.y,-f.x,-f.y,0,-1,e),-1===y&&(y=x),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),h=x,x=c,c=i.add(o.x,o.y,-d.x,-d.y,0,-1,e),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c),h=i.add(o.x,o.y,-u.x,-u.y,0,-1,e),h>=0&&x>=0&&c>=0&&!n&&l.add(h,x,c))}h>=0&&x>=0&&_>=0&&!g&&l.add(h,x,_),h>=0&&_>=0&&y>=0&&!g&&l.add(h,y,_),this._outlineIndexCount+=3*(l.index-n)}}_processFill(r,l,n,s,o){s=!0;let a;l.length>1&&(a=[]);let f=0;for(const t of l)0!==f&&a.push(f),f+=r[t].length;const u=2*f,d=t.acquire();for(const t of l){const e=r[t],i=e.length;for(let t=0;t<i;++t)d.push(e[t].x,e[t].y)}const h=e(d,a,2);if(s&&e.deviation(d,a,2,h)>0){const t=l.map((t=>r[t].length)),{buffer:e,vertexCount:s}=i(d,t);if(s>0){const t=this._fillVertexBuffer.index;for(let i=0;i<s;i++)this._fillVertexBuffer.add(e[2*i],e[2*i+1],n);for(let e=0;e<s;e+=3){const i=t+e;this._fillIndexBuffer.add(i,i+1,i+2)}if(void 0!==o){const t=this._patternMap,e=t.get(o);e?e[1]+=s:t.set(o,[this._fillIndexStart+this._fillIndexCount,s])}this._fillIndexCount+=s}}else{const t=h.length;if(t>0){const e=this._fillVertexBuffer.index;let i=0;for(;i<u;)this._fillVertexBuffer.add(d[i++],d[i++],n);let r=0;for(;r<t;)this._fillIndexBuffer.add(e+h[r++],e+h[r++],e+h[r++]);if(void 0!==o){const e=this._patternMap,i=e.get(o);i?i[1]+=t:e.set(o,[this._fillIndexStart+this._fillIndexCount,t])}this._fillIndexCount+=t}}t.release(d)}_isClipEdge(t,e){return t.x===e.x?t.x<=-64||t.x>=4160:t.y===e.y&&(t.y<=-64||t.y>=4160)}static _area(t){let e=0;const i=t.length-1;for(let r=0;r<i;r++)e+=(t[r].x-t[r+1].x)*(t[r].y+t[r+1].y);return e+=(t[i].x-t[0].x)*(t[i].y+t[0].y),.5*e}}export{s as default};