/* COPYRIGHT Esri - https://js.arcgis.com/5.0/LICENSE.txt */
import{fromValues as e,create as t}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import r from"../../../geometry/SpatialReference.js";import{projectVectorToVector as o}from"../../../geometry/projection/projectVectorToVector.js";import{wrap as i}from"../../../geometry/support/aaBoundingBox.js";import{create as n}from"../../../geometry/support/aaBoundingRect.js";import{isPlateCarree as s}from"../../../geometry/support/spatialReferenceUtils.js";import{createPlanarGlobePatch as a,updateCornersPlanar as l,updateEdgesAndCornersPlanar as u,updateEdgeElevationsAndResolutionsPlanar as h}from"./PatchGeometryFactory.js";import{Tile as d}from"./Tile.js";import{intersectAabbInvDirBefore as c}from"../webgl-engine/lib/RayIntersections.js";class m extends d{constructor(e,t,r,o,i){super(),this._horizontalScaleFactor=1,this._extentInRenderSR=n(),this._baseUsedMemory=900,this._subtreeGeometryElevationBoundMin=0,this._subtreeGeometryElevationBoundMax=0,this.init(e,t,r,o,i)}init(t,i,n,a,l){super.init(t,i,n,a,l);const u=l.view.renderSpatialReference,h=l.spatialReference,d=null!=u&&s(u)&&null!=h&&h.isGeographic?this.ellipsoid.radius*Math.PI/180:1;this._horizontalScaleFactor=d;const{isWebMercatorOnPlateCarree:c}=l,m=this._extentInRenderSR,_=this.extent;if(c){const t=e(_[0],_[1],0);o(t,r.WebMercator,t,r.PlateCarree);const i=e(_[2],_[3],0);o(i,r.WebMercator,i,r.PlateCarree),m[0]=t[0],m[1]=t[1],m[2]=i[0],m[3]=i[1]}else for(let e=0;e<4;++e)m[e]=_[e]*d;this.centerAtSeaLevel[0]=.5*(m[0]+m[2]),this.centerAtSeaLevel[1]=.5*(m[1]+m[3]),this.centerAtSeaLevel[2]=0,this._edgeLen=Math.max(m[2]-m[0],m[3]-m[1]),this._edgeLen2=this._edgeLen*this._edgeLen,this.updateRadiusAndCenter()}updateRadiusAndCenter(){this._updateCenter();const e=this._extentInRenderSR,t=.5*(e[2]-e[0]),r=.5*(e[3]-e[1]),o=Math.sqrt(t*t+r*r),i=.5*(this.elevationBoundsMax-this.elevationBoundsMin),n=Math.max(o,i);this._center[1].radius=n}_calculateFrustumVisibility(e){const t=this._aabb(),r=t[0],o=t[1],i=t[2],n=t[3],s=t[4],a=t[5];let l=!0;for(let u=0;u<6;u++){const t=e[u],h=t[0],d=t[1],c=t[2],m=t[3];if(h*(h>0?r:n)+d*(d>0?o:s)+c*(c>0?i:a)+m>=0)return 2;l=l&&h*(h<0?r:n)+d*(d<0?o:s)+c*(c<0?i:a)+m<=0}return l?0:1}_aabb(){const e=this._extentInRenderSR;return i(e[0],e[1],Math.min(this.elevationBoundsMin,this._subtreeGeometryElevationBoundMin),e[2],e[3],Math.max(this.elevationBoundsMax,this._subtreeGeometryElevationBoundMax))}intersectsRay(e,t,r,o){return _[0]=1/t[0],_[1]=1/t[1],_[2]=1/t[2],c(this._aabb(),e,_,r,o)}createGeometry(){a(this.renderData,this._horizontalScaleFactor),this._updateSubtreeGeometryElevationsBounds(),this.setMemoryDirty()}_updateSubtreeGeometryElevationsBounds(){const e=this._subtreeGeometryElevationBoundMin,t=this._subtreeGeometryElevationBoundMax,r=this.renderData;let o=e,i=t;if(r){const e=r.geometry.boundingBox;o=e[2],i=e[5]}else if(!this.leaf){o=1/0,i=-1/0;for(const e of this.children){const t=e;o=Math.min(o,t._subtreeGeometryElevationBoundMin),i=Math.max(i,t._subtreeGeometryElevationBoundMax)}}if(o!==this._subtreeGeometryElevationBoundMin||i!==this._subtreeGeometryElevationBoundMax){this._subtreeGeometryElevationBoundMin=o,this._subtreeGeometryElevationBoundMax=i;const e=this.parent;e?._updateSubtreeGeometryElevationsBounds()}}get minimumVerticesPerSide(){return this.level<9?3:2}updateCornerElevations(){l(this.renderData,this._horizontalScaleFactor),this._updateSubtreeGeometryElevationsBounds()}updateEdgeElevations(){u(this.renderData,this._horizontalScaleFactor),this._updateSubtreeGeometryElevationsBounds()}updateEdgeElevationsAndResolutions(){h(this.renderData,this._horizontalScaleFactor),this._updateSubtreeGeometryElevationsBounds()}get horizontalScale(){return this._horizontalScaleFactor}}const _=t();export{m as PlanarPatch};
