/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{len as t,set as e,normalize as i,dot as s,scale as o,copy as r,dist as a}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{create as n,ZEROS as l,freeze as c}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{distance as d}from"../../../../geometry/support/aaBoundingRect.js";import{intersectsPoint as h}from"../../../../geometry/support/frustum.js";import{fromValues as m}from"../../../../geometry/support/ray.js";import{FeatureTileVisibility3D as p,minTileLOD as u}from"./FeatureTileVisibility3D.js";import _ from"../../webgl/RenderCamera.js";class f{constructor(t,e,i){this._renderCoordsHelper=t,this._tilingScheme=e,this._camera=new _,this._surfaceElevation=0,this._focusOnMap=[0,0],this._visibility=new p(t,i)}set opaqueGround(t){this._visibility.opaqueGround=t}setup(t,e,i){this._camera.copyFrom(t),this._surfaceElevation=i,this._focusOnMap[0]=e.x,this._focusOnMap[1]=e.y,this._visibility.setup(this._camera)}done(){this._visibility.done()}update(t){const{measures:e,extent:i,level:s}=t;i&&(e.visible=this._visibility.compute(t),e.distance=d(i,this._focusOnMap),e.mergeable=!0,e.lodLevel=u,e.splitPriority=Math.max(0,u-s),e.visible&&(this._isGlobal?this._updateSplitAndLodGlobal(t):this._updateSplitAndLodLocal(t)))}_updateSplitAndLodGlobal(r){const a=r.level,n=this._renderCoordsHelper,{eye:l,fov:c}=this._camera,d=n.referenceEllipsoid.radius,h=t(l)-d;if(2*Math.atan(d/h)<c&&h>0)return void(r.measures.lodLevel=Math.max(a,u));const m=v,{extent:p}=r;if(!p)return;const{_surfaceElevation:_}=this;e(m[0],p[0],p[1],_),e(m[1],p[2],p[1],_),e(m[2],p[2],p[3],_),e(m[3],p[0],p[3],_);const f=e(M,.5*(p[0]+p[2]),.5*(p[1]+p[3]),_),y=this._tilingScheme.spatialReference;for(let t=0;t<4;++t)n.toRenderCoords(m[t],y,m[t]);n.toRenderCoords(f,y,f);const b=i(g.direction,f),L=s(l,b),S=o(x,b,L);this._updateSplitAndLod(r,m,f,S)}_updateSplitAndLodLocal(t){const i=v,{extent:s}=t;if(!s)return;const{_surfaceElevation:o}=this;e(i[0],s[0],s[1],o),e(i[1],s[2],s[1],o),e(i[2],s[2],s[3],o),e(i[3],s[0],s[3],o);const a=this._tilingScheme.spatialReference;for(let e=0;e<4;++e)this._renderCoordsHelper.toRenderCoords(i[e],a,i[e]);const n=e(M,.5*(i[0][0]+i[2][0]),.5*(i[0][1]+i[2][1]),.25*(i[0][2]+i[1][2]+i[2][2]+i[3][2])),l=e(y,n[0],n[1],0),{eye:c,far:d}=this._camera,h=e(x,l[0],l[1],c[2]);e(g.origin,l[0],l[1],c[2]-2*d),r(g.direction,b),this._updateSplitAndLod(t,i,n,h)}_updateSplitAndLod(t,e,i,o){const r=Math.max(a(e[0],e[2]),a(e[1],e[3]),a(e[0],i)+a(i,e[2]),a(e[1],i)+a(i,e[3])),{eye:n,near:l,fov:c,viewForward:d,width:m,height:p,pixelRatio:u,frustum:_}=this._camera,f=s(n,d),v=s(i,d)-f,M=a(i,n);let y=v,x=v,b=M,g=M;const R=(t,e)=>e<l?1:Math.sqrt(t*t-e*e)/e;let j=R(M,v);for(const h of e){const t=s(h,d)-f;y=Math.min(y,t),x=Math.max(x,t);const e=a(h,n);g=Math.max(g,e),b=Math.min(b,e);const i=R(e,t);j=Math.min(j,i)}if(x<l)return void(t.measures.lodLevel=0);const w=Math.cos(.5*c),A=a(n,o);j>w&&A>S*r&&(x=C*g,y=C*b);const G=.5*Math.sqrt(m*m+p*p)/u,E=2*Math.tan(.5*c),q=t=>Math.max(l,t)*L/G*E,H=t.level,O=r*2**H*Math.max(1,E),P=q(y),F=Math.ceil(Math.log2(Math.max(1,O/P)));if(t.measures.lodLevel=Math.max(F,t.measures.lodLevel),t.measures.splitPriority+=t.measures.lodLevel-H,H<F){const i=+h(_,e[0])+ +h(_,e[1])+ +h(_,e[2])+ +h(_,e[3]);t.measures.splitPriority+=i}const B=q(x)/P;B>2.5&&(t.measures.splitPriority+=B)}get _isGlobal(){return 1===this._renderCoordsHelper.viewingMode}}const v=[n(),n(),n(),n()],M=n(),y=n(),x=n(),b=c(0,0,1),g=m(l,b),L=312,S=.5,C=2;export{f as FeatureTileMeasurements3D};
