/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import has from"../../../../core/has.js";import t from"../../../../core/Logger.js";import{throwIfAborted as n}from"../../../../core/promiseUtils.js";import{serializeFlowPaths as e}from"./flowPathsIO.js";import{flowParticleTypes as r,flowParticleVertexIds as o}from"./particleConstants.js";import{applyMask as l,createFlowFieldFromData as s,traceFlowPaths as i}from"../../../support/flow/dataUtils.js";const c=()=>t.getLogger("esri.views.2d.engine.flow.dataUtils"),a=10;async function u(t,e,r,o,u){const m=performance.now();l(r);const h=f(s(e,r),e),d=performance.now(),x=i(e,h,r.width,r.height,o),M=performance.now(),g=p(x),D=performance.now(),b="Streamlines"===t?w(g,a,x):V(g,x),y=performance.now();return has("esri-2d-profiler")&&(c().info("I.1","_createFlowFieldFromData (ms)",Math.round(d-m)),c().info("I.2","_getStreamlines (ms)",Math.round(M-d)),c().info("I.3","createAnimatedLinesData (ms)",Math.round(D-M)),c().info("I.4","create{Streamlines|Particles}Mesh (ms)",Math.round(y-D)),c().info("I.5","createFlowMesh (ms)",Math.round(y-m)),c().info("I.6","Mesh size (bytes)",b.vertexData.buffer.byteLength+b.indexData.buffer.byteLength)),await Promise.resolve(),n(u),b}function f(t,n){const{perturb:e}=n;if(e){const{rotation:n}=e;null!=n&&(t=m(t,n));const{scale:r}=e;null!=r&&(t=h(t,r))}return t}function m(t,n){const e=Math.cos(n),r=Math.sin(n);return(n,o)=>{const[l,s]=t(n,o);return[e*l+r*s,-r*l+e*s]}}function h(t,n){return(e,r)=>{const[o,l]=t(e,r);return[o*n,l*n]}}function p(t){const n=t.reduce((t,n)=>t+n.vertices.length,0),e=new Float32Array(4*n),r=new Array(t.length);let o=0,l=0;for(const{vertices:s}of t){const t=o;for(const n of s)e[4*o]=n.x,e[4*o+1]=n.y,e[4*o+2]=n.time,e[4*o+3]=n.speed,o++;r[l++]={startVertex:t,numberOfVertices:s.length,firstTime:s[0].time,lastTime:s[s.length-1].time}}return{lineVertices:e,lineDescriptors:r}}function w(t,n,r){const o=9,{lineVertices:l,lineDescriptors:s}=t;let i=0,c=0;for(const e of s){i+=2*e.numberOfVertices;c+=6*(e.numberOfVertices-1)}const a=new Float32Array(i*o),u=new Uint32Array(c);let f=0,m=0;function h(){u[m++]=f-2,u[m++]=f,u[m++]=f-1,u[m++]=f,u[m++]=f+1,u[m++]=f-1}function p(t,n,e,r,l,s,i,c){const u=f*o;let m=0;a[u+m++]=t,a[u+m++]=n,a[u+m++]=1,a[u+m++]=e,a[u+m++]=s,a[u+m++]=i,a[u+m++]=r/2,a[u+m++]=l/2,a[u+m++]=c,f++,a[u+m++]=t,a[u+m++]=n,a[u+m++]=-1,a[u+m++]=e,a[u+m++]=s,a[u+m++]=i,a[u+m++]=-r/2,a[u+m++]=-l/2,a[u+m++]=c,f++}for(const e of s){const{firstTime:t,lastTime:r}=e;let o=null,s=null,i=null,c=null,a=null,u=null;for(let f=0;f<e.numberOfVertices;f++){const m=l[4*(e.startVertex+f)],w=l[4*(e.startVertex+f)+1],V=l[4*(e.startVertex+f)+2],d=l[4*(e.startVertex+f)+3];let x=null,M=null,g=null,D=null;if(f>0){x=m-o,M=w-s;const e=Math.sqrt(x*x+M*M);if(x/=e,M/=e,f>1){let t=x+a,e=M+u;const r=Math.sqrt(t*t+e*e);t/=r,e/=r;const o=Math.min(1/(t*x+e*M),n);t*=o,e*=o,g=-e,D=t}else g=-M,D=x;null!==g&&null!==D&&(p(o,s,i,g,D,t,r,d),h())}o=m,s=w,i=V,a=x,u=M,c=d}p(o,s,i,-u,a,t,r,c)}const w=e(r);return{vertexData:a,indexData:u,pathData:w}}function V(t,n){const l=16,{lineVertices:s,lineDescriptors:i}=t;let c=0,a=0;for(const e of i){const t=e.numberOfVertices-1;c+=4*t*2,a+=6*t*2}const u=new Float32Array(c*l),f=new Uint32Array(a);let m,h,p,w,V,d,x,M,g,D,b,y,v,A,F=0,I=0;function O(){f[I++]=F-8,f[I++]=F-7,f[I++]=F-6,f[I++]=F-7,f[I++]=F-5,f[I++]=F-6,f[I++]=F-4,f[I++]=F-3,f[I++]=F-2,f[I++]=F-3,f[I++]=F-1,f[I++]=F-2}function j(t,n,e,s,i,c,a,f,m,h,p,w,V,d){const x=F*l;let M=0;for(const l of r)for(const r of o)u[x+M++]=t,u[x+M++]=n,u[x+M++]=e,u[x+M++]=s,u[x+M++]=a,u[x+M++]=f,u[x+M++]=m,u[x+M++]=h,u[x+M++]=l,u[x+M++]=r,u[x+M++]=V,u[x+M++]=d,u[x+M++]=i/2,u[x+M++]=c/2,u[x+M++]=p/2,u[x+M++]=w/2,F++}function T(t,n){let e=g+b,r=D+y;const o=Math.sqrt(e*e+r*r);e/=o,r/=o;const l=g*e+D*r;e/=l,r/=l;let s=b+v,i=y+A;const c=Math.sqrt(s*s+i*i);s/=c,i/=c;const a=b*s+y*i;s/=a,i/=a,j(m,h,p,w,-r,e,V,d,x,M,-i,s,t,n),O()}function q(t,n,e,r,o,l){if(g=b,D=y,b=v,y=A,null==g&&null==D&&(g=b,D=y),null!=V&&null!=d){v=t-V,A=n-d;const e=Math.sqrt(v*v+A*A);v/=e,A/=e}null!=g&&null!=D&&T(o,l),m=V,h=d,p=x,w=M,V=t,d=n,x=e,M=r}function L(t,n){g=b,D=y,b=v,y=A,null==g&&null==D&&(g=b,D=y),null!=g&&null!=D&&T(t,n)}for(const e of i){m=null,h=null,p=null,w=null,V=null,d=null,x=null,M=null,g=null,D=null,b=null,y=null,v=null,A=null;const{firstTime:t,lastTime:n}=e;for(let r=0;r<e.numberOfVertices;r++){q(s[4*(e.startVertex+r)],s[4*(e.startVertex+r)+1],s[4*(e.startVertex+r)+2],s[4*(e.startVertex+r)+3],t,n)}L(t,n)}const U=e(n);return{vertexData:u,indexData:f,pathData:U}}export{p as createAnimatedLinesData,u as createFlowMesh,V as createParticlesMesh,w as createStreamlinesMesh};
