/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../chunks/_rollupPluginBabelHelpers","../../../core/mathUtils","../../../chunks/vec3","../../../chunks/vec3f64","../../../geometry/projection","../../../geometry/projectionEllipsoid","../../../geometry/support/frustum","../../../geometry/support/plane","./interfaces","./PatchGeometryFactory","./Tile"],(function(e,t,i,s,n,r,o,a,l,h,c,u){"use strict";let d=function(l){function u(e,t,i){var s;(s=l.call(this)||this).obb=new Array(8);for(let r=0;r<8;r++)s.obb[r]=n.create();return void 0!==e&&s.init(e,t,i),s}t._inheritsLoose(u,l);var d=u.prototype;return d.init=function(e,t,o){l.prototype.init.call(this,e,t,o);const a=this.ellipsoid.radius,h=this.extentInRadians[0],c=this.extentInRadians[1],u=this.extentInRadians[2],d=this.extentInRadians[3],p=e[0],P=i.lerp(c,d,.5),_=i.lerp(h,u,.5),f=0===p?0:Math.min(Math.abs(c),Math.abs(d));this._edgeLen=(u-h)*Math.cos(f)*a,this._edgeLen2=this._edgeLen*this._edgeLen,this._curvatureHeight=a-Math.sqrt(a*a-this._edgeLen2/4),r.lonLatToSphericalPCPF(this.centerAtSeaLevel,_,P,this.ellipsoid.radius);const T=n.fromArray(this.centerAtSeaLevel);s.normalize(T,T),this.up=T,this._updateOBB(),this.updateRadiusAndCenter()},d.updateRadiusAndCenter=function(){if(0===this.lij[0])s.set(this._center[e.CenterPosition.MIDDLE],0,0,0),s.set(this._center[e.CenterPosition.TOP],0,0,0),s.set(this._center[e.CenterPosition.BOTTOM],0,0,0),this.ellipsoid||(this.ellipsoid=o.getReferenceEllipsoid(this.surface.spatialReference)),this._center[e.CenterPosition.MIDDLE][3]=this.ellipsoid.radius+this.elevationBounds[1];else{this._updateCenter();const t=Math.max(s.squaredDistance(this._center[e.CenterPosition.MIDDLE],this.obb[0]),s.squaredDistance(this._center[e.CenterPosition.MIDDLE],this.obb[1]));this._center[e.CenterPosition.MIDDLE][3]=Math.sqrt(t)}},d._isVisible=function(t){if(!a.intersectsSphere(t,this._center[e.CenterPosition.MIDDLE]))return!1;if(this.lij[0]<10)return!0;const i=this.obb;for(let e=0;e<a.NumPlanes.NUM;e++){const s=e===a.PlaneIndex.NEAR,n=t[e];s&&(P[0]=n[0],P[1]=n[1],P[2]=n[2],P[3]=n[3]-this.surface.view.state.camera.near);const r=s?P:n;let o;for(o=0;o<8;o++){const e=i[o];if(r[0]*e[0]+r[1]*e[1]+r[2]*e[2]+r[3]<0)break}if(8===o)return!1}return!0},d.computeElevationBounds=function(){l.prototype.computeElevationBounds.call(this),this._updateOBB()},d.createGeometry=function(){const e=this._getPatchType(this.lij[1],this.lij[0]);c.createSphericalGlobePatch(this.renderData,e),this.setMemoryDirty()},d._updateOBB=function(){const e=this.extentInRadians,t=this.obb;for(let i=0;i<2;i++){const s=this.elevationBounds[i];let n=4*i;r.lonLatToSphericalPCPF(t[n++],e[0],e[1],this.ellipsoid.radius+s),r.lonLatToSphericalPCPF(t[n++],e[0],e[3],this.ellipsoid.radius+s),r.lonLatToSphericalPCPF(t[n++],e[2],e[3],this.ellipsoid.radius+s),r.lonLatToSphericalPCPF(t[n++],e[2],e[1],this.ellipsoid.radius+s)}if(this.surface.isWebMercator)switch(this._getPatchType(this.lij[1],this.lij[0])){case h.PatchType.HAS_NORTH_POLE:s.set(t[1],0,0,this.ellipsoid.radius),s.set(t[2],0,0,this.ellipsoid.radius),s.set(t[5],0,0,this.ellipsoid.radius),s.set(t[6],0,0,this.ellipsoid.radius);break;case h.PatchType.HAS_SOUTH_POLE:s.set(t[0],0,0,-this.ellipsoid.radius),s.set(t[3],0,0,-this.ellipsoid.radius),s.set(t[4],0,0,-this.ellipsoid.radius),s.set(t[7],0,0,-this.ellipsoid.radius)}},d._getPatchType=function(e,t){return e===(1<<t)-1?0===e?h.PatchType.HAS_BOTH_POLES:h.PatchType.HAS_SOUTH_POLE:0===e?h.PatchType.HAS_NORTH_POLE:h.PatchType.REGULAR},d.intersectsRay=function(t,i,s,n){const r=this._center[e.CenterPosition.MIDDLE],o=r[3]+s,a=i[0]*i[0]+i[1]*i[1]+i[2]*i[2],l=r[0]-t[0],h=r[1]-t[1],c=r[2]-t[2],u=(l*i[0]+h*i[1]+c*i[2])/a,d=i[0]*u-l,p=i[1]*u-h,P=i[2]*u-c;return d*d+p*p+P*P<o*o},d.getDefaultVerticesPerRowOnLevel=function(){return this.level<p.length?p[this.level]+1:2},d.updateCornerElevations=function(){c.updateCornerElevationsSpherical(this.renderData)},d.updateEdgeElevations=function(){c.updateEdgesElevationsSpherical(this.renderData)},u}(u.Tile);const p=[128,64,32,16,16,8,8,4],P=l.create();var _;e.CenterPosition=void 0,(_=e.CenterPosition||(e.CenterPosition={}))[_.TOP=0]="TOP",_[_.MIDDLE=1]="MIDDLE",_[_.BOTTOM=2]="BOTTOM",e.SphericalPatch=d,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
