/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import t from"../../../../core/PooledArray.js";import{lerp as i,squaredDistance as e}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{fromValues as r,clone as s,create as n}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{assert as a}from"./Util.js";class h{get center(){return r(this._data[0],this._data[1],this._data[2])}get radius(){return this._data[3]}get bbMin(){return r(this._data[4],this._data[5],this._data[6])}get bbMax(){return r(this._data[7],this._data[8],this._data[9])}constructor(t,e,h){this.primitiveIndices=t,this._numIndexPerPrimitive=e,this.position=h,this._data=[.1,0,0,0,0,0,0,0,0,0],this._children=void 0,a(t.length>=1),a(3===h.size||4===h.size);const{data:l,size:d,indices:c}=h;a(c.length%this._numIndexPerPrimitive===0),a(c.length>=t.length*this._numIndexPerPrimitive);const m=t.length;let _=d*c[this._numIndexPerPrimitive*t[0]];o.clear(),o.push(_);const u=r(l[_],l[_+1],l[_+2]),f=s(u);for(let i=0;i<m;++i){const e=this._numIndexPerPrimitive*t[i];for(let t=0;t<this._numIndexPerPrimitive;++t){_=d*c[e+t],o.push(_);let i=l[_];u[0]=Math.min(i,u[0]),f[0]=Math.max(i,f[0]),i=l[_+1],u[1]=Math.min(i,u[1]),f[1]=Math.max(i,f[1]),i=l[_+2],u[2]=Math.min(i,u[2]),f[2]=Math.max(i,f[2])}}for(let i=0;i<3;++i)this._data[4+i]=u[i],this._data[7+i]=f[i];const x=i(n(),this.bbMin,this.bbMax,.5);let P=.5*Math.max(Math.max(f[0]-u[0],f[1]-u[1]),f[2]-u[2]),b=P*P;for(let i=0;i<o.length;++i){_=o.at(i);const t=l[_]-x[0],e=l[_+1]-x[1],r=l[_+2]-x[2],s=t*t+e*e+r*r;if(s<=b)continue;const n=Math.sqrt(s),a=.5*(n-P);P+=a,b=P*P;const h=a/n;x[0]+=t*h,x[1]+=e*h,x[2]+=r*h}this._data[3]=P;for(let i=0;i<3;++i)this._data[0+i]=x[i];o.clear()}getChildren(){if(this._children||e(this.bbMin,this.bbMax)<=1)return this._children;const t=i(n(),this.bbMin,this.bbMax,.5),r=this.primitiveIndices.length,s=new Uint8Array(r),a=new Array(8);for(let i=0;i<8;++i)a[i]=0;const{data:o,size:l,indices:d}=this.position;for(let i=0;i<r;++i){let e=0;const r=this._numIndexPerPrimitive*this.primitiveIndices[i];let n=l*d[r],h=o[n],c=o[n+1],m=o[n+2];for(let t=1;t<this._numIndexPerPrimitive;++t){n=l*d[r+t];const i=o[n],e=o[n+1],s=o[n+2];i<h&&(h=i),e<c&&(c=e),s<m&&(m=s)}h<t[0]&&(e|=1),c<t[1]&&(e|=2),m<t[2]&&(e|=4),s[i]=e,++a[e]}let c=0;for(let i=0;i<8;++i)a[i]>0&&++c;if(c<2)return;const m=new Array(8);for(let i=0;i<8;++i)m[i]=a[i]>0?new Uint32Array(a[i]):void 0;for(let i=0;i<8;++i)a[i]=0;for(let i=0;i<r;++i){const t=s[i];m[t][a[t]++]=this.primitiveIndices[i]}this._children=new Array;for(let i=0;i<8;++i)void 0!==m[i]&&this._children.push(new h(m[i],this._numIndexPerPrimitive,this.position));return this._children}static prune(){o.prune()}}const o=new t({deallocator:null});export{h as BoundingInfo};
