/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{identity as e,translate as s,rotate as t,scaleByVec2 as i,multiply as r}from"../../../../core/libs/gl-matrix-2/math/mat3.js";import{create as a}from"../../../../core/libs/gl-matrix-2/factories/mat3f32.js";import{fromValues as h}from"../../../../core/libs/gl-matrix-2/factories/vec2f32.js";import{createVFMeshData as o,createVFScalarData as l}from"../../../../layers/raster/functions/vectorFieldUtils.js";import{DisplayObject as n}from"../DisplayObject.js";import{Mesh as m}from"../webgl/meshing/Mesh.js";import{PrimitiveType as c,DataType as d}from"../../../webgl/enums.js";import{VertexElementDescriptor as u}from"../../../webgl/VertexElementDescriptor.js";class f extends n{constructor(e=null){super(),this._source=null,this._symbolizerParameters=null,this._meshesInvalidated=!0,this.coordScale=[1,1],this.height=null,this.key=null,this.offset=null,this.stencilRef=0,this.resolution=null,this.pixelRatio=1,this.x=0,this.y=0,this.rotation=0,this.rawPixelData=null,this.meshes=null,this.width=null,this.source=e}destroy(){super.destroy(),null!=this.meshes&&(this.meshes.magdir?.destroy(),this.meshes.scalar?.destroy(),this.meshes=null)}get symbolizerParameters(){return this._symbolizerParameters}set symbolizerParameters(e){JSON.stringify(this._symbolizerParameters)!==JSON.stringify(e)&&(this._symbolizerParameters=e,this.invalidateMeshes())}get source(){return this._source}set source(e){this._source=e,this.invalidateMeshes()}invalidateMeshes(){this._meshesInvalidated||null==this.meshes||(this.meshes.magdir?.destroy(),this.meshes.scalar?.destroy(),this.meshes=null,this._meshesInvalidated=!0,this.requestRender())}updateVectorFieldMesh(e){if(this._meshesInvalidated){if(this._meshesInvalidated=!1,null!=this.source&&null==this.meshes){const{style:s}=this.symbolizerParameters;switch(s){case"beaufort_ft":case"beaufort_km":case"beaufort_kn":case"beaufort_m":case"beaufort_mi":case"classified_arrow":case"ocean_current_kn":case"ocean_current_m":case"single_arrow":{const s=o(this.source,this.symbolizerParameters),t=this._createVectorFieldMesh(e,s);this.meshes={magdir:t}}break;case"simple_scalar":{const s=l(this.source,this.symbolizerParameters),t=this._createVectorFieldMesh(e,s);this.meshes={scalar:t}}break;case"wind_speed":{const s=o(this.source,this.symbolizerParameters),t=this._createVectorFieldMesh(e,s),i=l(this.source,this.symbolizerParameters),r=this._createVectorFieldMesh(e,i);this.meshes={scalar:r,magdir:t}}}}this.ready(),this.requestRender()}}_createTransforms(){return{displayViewScreenMat3:a()}}setTransform(a){const o=e(this.transforms.displayViewScreenMat3),[l,n]=a.toScreenNoRotation([0,0],[this.x,this.y]),m=this.resolution/this.pixelRatio/a.resolution,c=m*this.width,d=m*this.height,u=Math.PI*this.rotation/180;s(o,o,h(l,n)),s(o,o,h(c/2,d/2)),t(o,o,-u),s(o,o,h(-c/2,-d/2)),i(o,o,h(c,d)),r(this.transforms.displayViewScreenMat3,a.displayViewMat3,o)}onAttach(){this.invalidateMeshes()}onDetach(){this.invalidateMeshes()}_createVectorFieldMesh(e,s){const{vertexData:t,indexData:i}=s,r={vertex:{geometry:{data:t,layout:[new u("position",2,d.FLOAT,0,24),new u("offset",2,d.FLOAT,8,24),new u("vv",2,d.FLOAT,16,24)]}},index:{indices:{data:i}},groups:[{index:"indices",primitive:c.TRIANGLES}],parts:[{group:0,start:0,count:i.length}]};return new m(e.context,r)}}export{f as RasterVFDisplayObject};
