/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{acosClamped as e,deg2rad as t}from"../../../../../core/mathUtils.js";import n from"../../../../../core/PooledArray.js";import{B as o,h as s,d as a,g as r,e as c,f as l,n as i,C as h}from"../../../../../chunks/vec32.js";import{create as u}from"../../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{Edge as g}from"./Edge.js";const p=-1;function d(e,t,n){const c=e.vertices.position,l=e.vertices.componentIndex,i=I.position0,h=I.position1,u=I.faceNormal0,g=I.faceNormal1,{edges:d,normals:v}=w(e),x=d.length/4,y=t.allocate(x);let j=0;const N=x,b=n?.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],h);const t=V.pushNew();t.index=4*s,t.length=o(i,h)}V.sort((e,t)=>t.length-e.length);const B=new Array,L=new Array;V.forAll(({length:e,index:o})=>{const w=d.data[o],x=d.data[o+1],V=d.data[o+2],N=d.data[o+3],P=N===p;if(c.getVec(w,i),c.getVec(x,h),P){const e=3*V;s(u,v.data[e],v.data[e+1],v.data[e+2]),a(g,u),I.componentIndex=l.get(w),I.cosAngle=r(u,g)}else{let e=3*V;if(s(u,v.data[e],v.data[e+1],v.data[e+2]),e=3*N,s(g,v.data[e],v.data[e+1],v.data[e+2]),I.componentIndex=l.get(w),I.cosAngle=r(u,g),m(I,k))return;I.cosAngle<-.9999&&a(g,u)}E+=e,F++,P||f(I,C)?(t.write(y,j++,I),B.push(e)):A(I,M)&&(b&&n&&n.write(b,D++,I),L.push(e))});const P=new Float32Array(B.reverse()),U=new Float32Array(L.reverse()),q=b&&n?{instancesData:b.slice(0,D),lodInfo:{lengths:U}}:void 0;return{regular:{instancesData:y.slice(0,j),lodInfo:{lengths:P}},silhouette:q,averageEdgeLength:E/F}}function f(e,t){return e.cosAngle<t}function m(e,t){return e.cosAngle>t}function A(t,n){const o=e(t.cosAngle);h(b,t.position1,t.position0);return o*(r(l(N,t.faceNormal0,t.faceNormal1),b)>0?-1:1)>n}function w(e){const t=e.faces.length/3,n=e.faces,o=e.neighbors,s=e.vertices.position;y.length=j.length=0;for(let a=0;a<t;a++){const e=3*a,t=o[e],r=o[e+1],h=o[e+2],u=n[e],g=n[e+1],d=n[e+2];s.getVec(u,D),s.getVec(g,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||u<g)&&(y.push(u),y.push(g),y.push(a),y.push(t)),(r===p||g<d)&&(y.push(g),y.push(d),y.push(a),y.push(r)),(h===p||d<u)&&(y.push(d),y.push(u),y.push(a),y.push(h))}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 n({allocator:e=>e||new v,deallocator:null}),y=new n({deallocator:null}),j=new n({deallocator:null}),I=new g,N=u(),b=u(),D=u(),E=u(),F=u(),M=t(4),k=Math.cos(M),B=t(35),C=Math.cos(B);export{x as cleanupEdgeProcessing,d as extractEdges};
