@arcgis/core
Version:
ArcGIS Maps SDK for JavaScript: A complete 2D and 3D mapping and data visualization API
6 lines (5 loc) • 3.37 kB
JavaScript
/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.32/esri/copyright.txt for details.
*/
import e from"../../../../core/Error.js";import r from"../../../../core/Logger.js";import{e as t}from"../../../../chunks/earcut.js";import{set as n}from"../../../../core/libs/gl-matrix-2/math/vec2.js";import{create as o}from"../../../../core/libs/gl-matrix-2/factories/vec2f64.js";import{convertFromNestedArray as s,convertFromPolygon as i}from"../../../../layers/graphics/featureConversionUtils.js";import c from"../../../../layers/graphics/OptimizedGeometry.js";import{i1616to32 as a}from"./number.js";import{BufferObject as f}from"../../../webgl/BufferObject.js";import{PrimitiveType as m,Usage as h,DataType as u}from"../../../webgl/enums.js";const l=r=>{switch(r.BYTES_PER_ELEMENT){case 1:return u.UNSIGNED_BYTE;case 2:return u.UNSIGNED_SHORT;case 4:return u.UNSIGNED_INT;default:throw new e("Cannot get DataType of array")}},y=(e,r,t,n)=>{let o=0;for(let s=1;s<t;s++){const t=e[2*(r+s-1)],n=e[2*(r+s-1)+1];o+=(e[2*(r+s)]-t)*(e[2*(r+s)+1]+n)}return n?o>0:o<0},g=({coords:e,lengths:r},n)=>{const o=[];for(let s=0,i=0;s<r.length;i+=r[s],s+=1){const c=i,a=[];for(;s<r.length-1&&y(e,i+r[s],r[s+1],n);s+=1,i+=r[s])a.push(i+r[s]-c);const f=e.slice(2*c,2*(i+r[s])),m=t(f,a,2);for(const e of m)o.push(e+c)}return o};class p{constructor(e,r,t,n=!1){this._cache={},this.vertices=e,this.indices=r,this.primitiveType=t,this.isMapSpace=n}static fromPath(e){const r=s(new c,e.path,!1,!1),t=r.coords,n=new Uint32Array(g(r,!0)),o=new Uint32Array(t.length/2);for(let s=0;s<o.length;s++)o[s]=a(Math.floor(t[2*s]),Math.floor(t[2*s+1]));return new p({geometry:o},n,m.TRIANGLES)}static fromGeometry(t,n){const o=n.geometry?.type;switch(o){case"polygon":return p.fromPolygon(t,n.geometry);case"extent":return p.fromMapExtent(t,n.geometry);default:return r.getLogger("esri.views.2d.engine.webgl.Mesh2D").error(new e("mapview-bad-type",`Unable to create a mesh from type ${o}`,n)),p.fromScreenExtent({xmin:0,ymin:0,xmax:1,ymax:1})}}static fromPolygon(e,r){const t=i(new c,r,!1,!1),s=t.coords,f=new Uint32Array(g(t,!1)),h=new Uint32Array(s.length/2),u=o(),l=o();for(let o=0;o<h.length;o++)n(u,s[2*o],s[2*o+1]),e.toScreen(l,u),h[o]=a(Math.floor(l[0]),Math.floor(l[1]));return new p({geometry:h},f,m.TRIANGLES,!0)}static fromScreenExtent({xmin:e,xmax:r,ymin:t,ymax:n}){const o={geometry:new Uint32Array([a(e,t),a(r,t),a(e,n),a(e,n),a(r,t),a(r,n)])},s=new Uint32Array([0,1,2,3,4,5]);return new p(o,s,m.TRIANGLES)}static fromMapExtent(e,r){const[t,n]=e.toScreen([0,0],[r.xmin,r.ymin]),[o,s]=e.toScreen([0,0],[r.xmax,r.ymax]),i={geometry:new Uint32Array([a(t,n),a(o,n),a(t,s),a(t,s),a(o,n),a(o,s)])},c=new Uint32Array([0,1,2,3,4,5]);return new p(i,c,m.TRIANGLES)}destroy(){null!=this._cache.indexBuffer&&this._cache.indexBuffer.dispose(),this._cache.vertexBuffers?.forEach((e=>e?.dispose())),this._cache.indexBuffer=this._cache.vertexBuffers=null}get elementType(){return l(this.indices)}getIndexBuffer(e,r=h.STATIC_DRAW){return this._cache.indexBuffer??=f.createIndex(e,r,this.indices),this._cache.indexBuffer}getVertexBuffers(e,r=h.STATIC_DRAW){return this._cache.vertexBuffers??=new Map(Object.keys(this.vertices).reduce(((t,n)=>(t.push([n,f.createVertex(e,r,this.vertices[n])]),t)),new Array)),this._cache.vertexBuffers}}export{p as default};