UNPKG

@doegis/core

Version:

DOE GIS API

3 lines (1 loc) 4.42 kB
import"../../../../core/has.js";import{FreeList as e}from"./FreeList.js";import{allocateTypedArrayBuffer as t,strideToPackingFactor as r,copyMeshData as i}from"./Utils.js";const s=["FILL","LINE","MARKER","TEXT","LABEL"];function o(e){const t=e.getStrides(),r={};for(let i=0;i<t.length;i++)r[s[i]]=t[i];return r}const n=.5;class d{constructor(r,i,s,o){this._strides=r,this._displayList=i,this._freeListsAndStorage={},this._dirtyMap=null,this._dirtyMap=s;for(const n in r){this._freeListsAndStorage[n]={vtxFreeList:o?new e(o):null,idxFreeList:o?new e(o):null,vertexBuffers:{},indexBuffer:o?new Uint32Array(o):null};for(const e in r[n])this._freeListsAndStorage[n].vertexBuffers[e]={data:o?t(o,r[n][e]):null,stride:r[n][e]}}}static fromTileData(t,i){const n=o(t),a=[0,0,0,0,0],x=[0,0,0,0,0];for(const e of t.tileDisplayData.displayObjects)for(const t of e.displayRecords)a[t.geometryType]=Math.max(a[t.geometryType],t.vertexFrom+t.vertexCount),x[t.geometryType]=Math.max(x[t.geometryType],t.indexFrom+t.indexCount);const u=new d(n,t.tileDisplayData.displayList,i,void 0),f=t.tileBufferData?.geometries??[];for(let o=0;o<f.length;++o){const t=a[o],i=x[o],n=f[o],d=s[o],l=u._storageFor(d),c=f[o].indexBuffer;l.indexBuffer=c,l.idxFreeList=new e(c.length),l.idxFreeList.allocate(i);let m=0;for(const e in n.vertexBuffer){const t=f[o].vertexBuffer[e];l.vertexBuffers[e].data=t.data,l.vertexBuffers[e].stride=t.stride;const i=r(t.stride),s=t.data.length*i/t.stride;m||(m=s)}l.vtxFreeList=new e(m),l.vtxFreeList.allocate(t)}return u}delete(e){const t=s[e.geometryType];this._freeVertices(t,e.vertexFrom,e.vertexCount),this._freeIndices(t,e.indexFrom,e.indexCount),this._displayList.removeFromList(e),e.vertexFrom=void 0,e.indexFrom=void 0}setMeshData(e,t,r,o,n){const d=s[e.geometryType];let a,x;e.meshData=null,void 0===e.vertexFrom?(x=t.vertexCount,a=this._allocateVertices(d,x)):t.vertexCount>e.vertexCount?(this._freeVertices(d,e.vertexFrom,e.vertexCount),x=t.vertexCount,a=this._allocateVertices(d,x)):t.vertexCount===e.vertexCount?(a=e.vertexFrom,x=e.vertexCount):(this._freeVertices(d,e.vertexFrom+t.vertexCount,e.vertexCount-t.vertexCount),a=e.vertexFrom,x=t.vertexCount);let u,f,l,c=!0;if(void 0===e.indexFrom?(u=n,l=t.indexCount,f=this._allocateIndices(d,l)):t.indexCount>e.indexCount?(u=this._displayList.removeFromList(e),this._freeIndices(d,e.indexFrom,e.indexCount),l=t.indexCount,f=this._allocateIndices(d,l)):t.indexCount===e.indexCount?(c=!1,f=e.indexFrom,l=e.indexCount):(u=this._displayList.removeFromList(e),this._freeIndices(d,e.indexFrom+t.indexCount,e.indexCount-t.indexCount),f=e.indexFrom,l=t.indexCount),-1!==a&&-1!==f){const s=this._storageFor(d);if(i(a,f,s.vertexBuffers,s.indexBuffer,t,r,o),e.vertexFrom=a,e.indexFrom=f,e.vertexCount=t.vertexCount,e.indexCount=t.indexCount,this._dirtyMap){this._dirtyMap.markDirtyIndices(e.geometryType,e.indexFrom,e.indexCount);for(const t in r)this._dirtyMap.markDirtyVertices(e.geometryType,t,e.vertexFrom,e.vertexCount)}return c&&this._displayList.addToList(e,u),!0}return-1!==a&&this._freeVertices(d,a,x),-1!==f&&this._freeIndices(d,f,l),e.setMeshDataFromBuffers(t,r,o),e.vertexFrom=void 0,e.vertexCount=0,e.indexFrom=void 0,e.indexCount=0,!1}tryAddMeshData(e,t){const r=t.vertexBuffer,o=t.indexBuffer,n=s[e.geometryType],d=this._allocateVertices(n,e.vertexCount);if(-1===d)return this._freeVertices(n,d,e.vertexCount),!1;const a=this._allocateIndices(n,e.indexCount);if(-1===a)return this._freeVertices(n,d,e.vertexCount),this._freeIndices(n,a,e.indexCount),!1;const x=this._storageFor(n);if(i(d,a,x.vertexBuffers,x.indexBuffer,e,r,o),e.vertexFrom=d,e.indexFrom=a,this._dirtyMap){this._dirtyMap.markDirtyIndices(e.geometryType,e.indexFrom,e.indexCount);for(const t in r)this._dirtyMap.markDirtyVertices(e.geometryType,t,d,e.vertexCount)}return this._displayList.addToList(e),!0}_allocateVertices(e,t){const r=this._storageFor(e),i=r.vtxFreeList?.allocate(t);if(null==i||-1===i)return-1;const s=r.vtxFreeList?.fragmentation;return null==s||s>n?-1:i}_freeVertices(e,t,r){this._storageFor(e).vtxFreeList?.free(t,r)}_freeIndices(e,t,r){this._storageFor(e).idxFreeList?.free(t,r)}_allocateIndices(e,t){const r=this._storageFor(e),i=r.idxFreeList?.allocate(t);if(null==i||-1===i)return-1;const s=r.idxFreeList?.fragmentation;return null==s||s>n?-1:i}_storageFor(e){return this._freeListsAndStorage[e]}_stridesFor(e,t){return this._strides[e][t]}}export{d as default};