/*
All material copyright ESRI, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/4.24/esri/copyright.txt for details.
*/
define(["exports","../../../../core/screenUtils","../../../../chunks/vec3","../../../../chunks/vec3f64","../../../../geometry/support/aaBoundingRect","../../../../geometry/support/frustum","../../../../geometry/support/plane","../../../../geometry/support/triangle","./FeatureTileDescriptor3D","./FeatureTileVisibility3D","../../webgl-engine/lib/Camera"],(function(e,t,i,r,s,n,o,a,c,l,u){"use strict";let h=function(){function e(e){this.camera=new u.Camera,this.focusOnMap=[0,0],this.screenRect=s.create(),this.tileSize=e.tileSize,this.renderCoordsHelper=e.renderCoordsHelper,this.tilingScheme=e.tilingScheme,this.visibility=new l.FeatureTileVisibility3D(e.renderCoordsHelper)}var t=e.prototype;return t.begin=function(e,t,i){this.camera.copyFrom(e),this.surfaceElevation=i,this.focusOnMap[0]=t.x,this.focusOnMap[1]=t.y,s.fromValues(0,0,e.fullWidth,e.fullHeight,this.screenRect),this.visibility.begin(this.camera,i)},t.end=function(){this.visibility.end()},t.updateTile=function(e){e.measures.visibility=this.visibility.calculate(e),e.measures.distance=s.distance(e.extent,this.focusOnMap),e.measures.visibility!==c.Visibility.INVISIBLE&&this._updateScreenMeasure(e)},t._updateScreenMeasure=function(e){const t=p,i=1<<t,r=e.measures.screenRect;s.empty(r);let n=0;const o=e.lij[0]+t,a=e.lij[1]<<t,c=e.lij[2]<<t,l=this._tileSizeWithBias(e),u=l*l;for(let s=0;s<i;s++)for(let t=0;t<i;t++)if(n+=this._computeScreenArea(e,o,a+s,c+t,r),n>u)return void(e.measures.shouldSplit=!0);e.measures.shouldSplit=!1},t._tileSizeWithBias=function(e){return e.measures.visibility===c.Visibility.VISIBLE_WHEN_EXTENDED?this.tileSize*m:this.tileSize},t._computeScreenArea=function(e,t,i,r,n){const o=e.measures.visibility===c.Visibility.VISIBLE_WHEN_EXTENDED;this._projectToScreen(t,i,r,o,f),s.empty(d);for(let a=0;a<4;a++)s.expandPointInPlace(d,f[a]);return s.expand(n,d,n),a.areaPoints2d(f[0],f[1],f[2])+a.areaPoints2d(f[0],f[2],f[3])},t._projectToScreen=function(e,t,i,r,s){this.tilingScheme.ensureMaxLod(e),this.tilingScheme.getExtent(e,t,i,S),this._toRenderCoords(S,0,3,_[0]),this._toRenderCoords(S,2,3,_[1]),this._toRenderCoords(S,2,1,_[2]),this._toRenderCoords(S,0,1,_[3]),r&&(this._projectToPlane(_,this.camera.frustum[n.PlaneIndex.NEAR]),this._projectToPlane(_,this.camera.frustum[n.PlaneIndex.TOP]),this._projectToPlane(_,this.camera.frustum[n.PlaneIndex.BOTTOM]));for(let n=0;n<4;n++)this.camera.projectToRenderScreen(_[n],b),this.camera.renderToScreen(b,s[n])},t._projectToPlane=function(e,t){for(let i=0;i<4;i++)g[i]=o.signedDistance(t,e[i]);const r=Math.max(g[0],g[1],g[2],g[3]);if(r>0){const s=i.scale(y,o.normal(t),-r);for(let t=0;t<4;t++)i.add(e[t],e[t],s)}},t._toRenderCoords=function(e,t,i,r){return y[0]=e[t],y[1]=e[i],y[2]=this.surfaceElevation,this.renderCoordsHelper.toRenderCoords(y,this.tilingScheme.spatialReference,r),r},e}();const d=s.create(),p=2,m=5,f=[t.createScreenPointArray(),t.createScreenPointArray(),t.createScreenPointArray(),t.createScreenPointArray()],S=s.create(),y=r.create(),_=[r.create(),r.create(),r.create(),r.create()],g=[0,0,0,0],b=t.createRenderScreenPointArray3();e.FeatureTileMeasurements3D=h,Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}));
