/*
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,lerp as t,clamp as i}from"../../../core/mathUtils.js";import{getMetersPerUnitForSR as r}from"../../../core/units.js";import{b as n,n as s,j as a,g as o,e as m}from"../../../chunks/vec32.js";import{create as c}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getReferenceEllipsoid as h}from"../../../geometry/ellipsoidUtils.js";import l from"../../../geometry/Extent.js";import{create as u,fromValues as p}from"../../../geometry/support/ray.js";import{Sphere as x}from"../../../geometry/support/sphere.js";import{computeInnerAltitudeFade as f}from"../environment/atmosphereUtils.js";import{DepthRange as M}from"../webgl-engine/lib/DepthRange.js";function _(e,t,i){return 1===e?new y(t,i):new d(t,i)}class d{constructor(e,t){this._elevationProvider=e,this._referenceEllipsoid=h(t),this._unitInMeters=r(t,this._referenceEllipsoid.metersPerDegree)}compute(e,r,n,a){const{eye:c,center:h}=e;let u=c[2]*this._unitInMeters;const p=u,x=u-a,f=this._elevationProvider?.visibleElevationRange;f&&(u=x>=0?p-this._unitInMeters*f.minElevation:this._unitInMeters*f.maxElevation-p),r??=new l({xmin:0,ymin:0,zmin:0,xmax:0,ymax:0,zmax:0});const M={x:r.xmax-r.xmin,y:r.ymax-r.ymin,z:4*Math.max(r.xmax-r.xmin,r.ymax-r.ymin)},_=Math.max(M.x,M.y,M.z);m(P,h,c),F[0]=P[0]>0?r.xmax:r.xmin,F[1]=P[1]>0?r.ymax:r.ymin,F[2]=P[2]>0?_/2:-_/2,m(F,F,c),s(P,P);const d=1.1*o(F,P)*this._unitInMeters,y=Math.sqrt(u*(u+2*this._referenceEllipsoid.radius)),E=Math.max(r.xmax-r.xmin,r.ymax-r.ymin),g=E*w*this._unitInMeters,I=E*D*this._unitInMeters,b=i((u-I)/(g-I),0,1)**3,R=Math.min(t(y,d,b),y)*Math.max(Math.log(Math.abs(x)),1);return v(Math.min(R,Math.max(34064e4,_))/this._unitInMeters,j,this._unitInMeters,z)}computeFarDistance(e,t,i){return i}}class y{constructor(e,t){this._elevationProvider=e,this._referenceEllipsoid=h(t)}compute(r,c,h,l){const{eye:u}=r,x=n(u),_=x-this._referenceEllipsoid.radius,d=this._computeVirtualAltitude(_,l),y=this._elevationProvider?.visibleElevationRange.maxElevation??0,w=f(d),D=this._computeFarDistance(x,d,l,h.far),A=(Math.log(d)-E)/(g-E);v(D,i(j-A*(j-I),I,j),1,z);const V=this._referenceEllipsoid.radius+y,q=this._referenceEllipsoid.radius+this._referenceEllipsoid.atmosphereHeight,k=Math.max(V,q),H=x-k;if(w>0&&H>b){const i=s(F,a(F,r.eye,-1)),c=s(P,r.viewForward),l=e(o(i,c)),u=.5*r.fovY,x=Math.cos(u);let f=M.Infinite.near;if(l<=u)f=H*x;else{const e=s(F,r.viewUp),t=Math.tan(u),i=a(F,e,t),o=s(F,m(F,c,i)),h=p(r.eye,o,U);if(R.radius=k,R.intersectRay(h,F)){const e=m(F,F,r.eye);f=n(e)*x}}const _=.99*Math.min(h.near,f);_<M.Infinite.near&&_>z.near&&(z.near=t(z.near,_,w))}return z}computeFarDistance(e,t,i){const r=n(e),s=r-this._referenceEllipsoid.radius,a=this._computeVirtualAltitude(s,t);return this._computeFarDistance(r,a,t,i)}_computeFarDistance(e,t,i,r){const n=this._referenceEllipsoid.radius,s=n+Math.min(0,i),a=f(t),o=Math.sqrt(t*(t+2*s)),m=o+Math.max(Math.min(.2*o,.01*n),r);return a<.5?m:1.2*(e+n)}_computeVirtualAltitude(e,t){const i=Math.abs(e-t);return Math.max(i,Math.abs(e))}}function v(e,t,i,r){const n=b/i,s=e/t;return s>n?(r.far=e,r.near=s):(r.near=n,r.far=r.near*t),r}const E=7.983,g=16.994,j=2e4,I=100,b=2,w=.001,D=1e-4,F=c(),P=c(),z={near:0,far:0},R=new x,U=u();export{y as NearFarHeuristicGlobal,_ as createNearFarHeuristic,b as minNearDistanceInMeters};
