/* COPYRIGHT Esri - https://js.arcgis.com/5.0.18/LICENSE.txt */
import{acosClamped as e,deg2rad as t}from"../../../../../core/mathUtils.js";import o from"../../../../../core/PooledArray.js";import{distance as n,set as s,copy as a,dot as r,subtract as c,cross as l,normalize as i,direction as g}from"../../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as h}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Edge as u}from"./Edge.js";const p=-1;function d(e,t,o){const c=e.vertices.position,l=e.vertices.componentIndex,i=I.position0,g=I.position1,h=I.faceNormal0,u=I.faceNormal1,{edges:d,normals:v}=w(e),x=d.length/4,y=t.allocate(x);let j=0;const N=x,b=o?.allocate(N);let D=0,E=0,F=0;V.length=0;for(let s=0;s<x;++s){const e=4*s;c.getVec(d.data[e],i),c.getVec(d.data[e+1],g);const t=V.pushNew();t.index=4*s,t.length=n(i,g)}V.sort((e,t)=>t.length-e.length);const P=new Array,k=new Array;V.forAll(({length:e,index:n})=>{const w=d.data[n],x=d.data[n+1],V=d.data[n+2],N=d.data[n+3],q=N===p;if(c.getVec(w,i),c.getVec(x,g),q){const e=3*V;s(h,v.data[e],v.data[e+1],v.data[e+2]),a(u,h),I.componentIndex=l.get(w),I.cosAngle=r(h,u)}else{let e=3*V;if(s(h,v.data[e],v.data[e+1],v.data[e+2]),e=3*N,s(u,v.data[e],v.data[e+1],v.data[e+2]),I.componentIndex=l.get(w),I.cosAngle=r(h,u),m(I,L))return;I.cosAngle<-.9999&&a(u,h)}E+=e,F++,q||f(I,U)?(t.write(y,j++,I),P.push(e)):A(I,M)&&(b&&o&&o.write(b,D++,I),k.push(e))});const q=new Float32Array(P.reverse()),z=new Float32Array(k.reverse()),B=b&&o?{instancesData:b.slice(0,D),lodInfo:{lengths:z}}:void 0;return{regular:{instancesData:y.slice(0,j),lodInfo:{lengths:q}},silhouette:B,averageEdgeLength:E/F}}function f(e,t){return e.cosAngle<t}function m(e,t){return e.cosAngle>t}function A(t,o){const n=e(t.cosAngle);g(b,t.position1,t.position0);return n*(r(l(N,t.faceNormal0,t.faceNormal1),b)>0?-1:1)>o}function w(e){const t=e.faces.length/3,o=e.faces,n=e.neighbors,s=e.vertices.position;y.length=j.length=0;for(let a=0;a<t;a++){const e=3*a,t=n[e],r=n[e+1],g=n[e+2],h=o[e],u=o[e+1],d=o[e+2];s.getVec(h,D),s.getVec(u,E),s.getVec(d,F),c(E,E,D),c(F,F,D),l(D,E,F),i(D,D),j.pushArray(D),(t===p||h<u)&&(y.push(h),y.push(u),y.push(a),y.push(t)),(r===p||u<d)&&(y.push(u),y.push(d),y.push(a),y.push(r)),(g===p||d<h)&&(y.push(d),y.push(h),y.push(a),y.push(g))}return{edges:y,normals:j}}class v{constructor(){this.index=0,this.length=0}}function x(){V.prune(),y.prune(),j.prune()}const V=new o({allocator:e=>e||new v,deallocator:null}),y=new o({deallocator:null}),j=new o({deallocator:null}),I=new u,N=h(),b=h(),D=h(),E=h(),F=h(),M=t(4),L=Math.cos(M),P=t(35),U=Math.cos(P);export{x as cleanupEdgeProcessing,d as extractEdges};
