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