/*
All material copyright Esri, All Rights Reserved, unless otherwise specified.
See https://js.arcgis.com/5.0/esri/copyright.txt for details.
*/
import{neverReached as t}from"../../../core/compilerUtils.js";import{lerp as e}from"../../../core/mathUtils.js";import{invert as i}from"../../../core/libs/gl-matrix-2/math/mat4.js";import{create as r}from"../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{i as n,j as o,C as s,p as a,h as c,g as l}from"../../../chunks/vec32.js";import{create as p}from"../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{getReferenceEllipsoid as h}from"../../../geometry/ellipsoidUtils.js";import{computeTranslationToOriginAndRotation as d}from"../../../geometry/projection/computeTranslationToOriginAndRotation.js";import{projectBoundingRect as _}from"../../../geometry/projection/projectBoundingRect.js";import{projectVectorToVector as m}from"../../../geometry/projection/projectVectorToVector.js";import{empty as g,create as x,expandWithVec3 as f}from"../../../geometry/support/aaBoundingBox.js";import{center as u,create as y}from"../../../geometry/support/aaBoundingRect.js";import{fromPoints as j,create as R}from"../../../geometry/support/lineSegment.js";import{create as b,fromVectorsAndPoint as S,copy as C,negate as B,signedDistance as H}from"../../../geometry/support/plane.js";import{wrap as w}from"../../../geometry/support/ray.js";import{frustumLineSegment as M}from"./intersectionUtils.js";const A=.5*Math.PI,G=A/Math.PI*180;class E{constructor(t){this._extent=new Array(4),this._planes=new Array(6),this._maxSpan=0,this._center={origin:p(),direction:p()},this._renderCoordsHelper=t.renderCoordsHelper;for(let e=0;e<4;e++)this._extent[e]={origin:p(),direction:p(),cap:{next:null,direction:p()}},this._planes[e]=b();this._planes[4]=b(),this._planes[5]=b(),this._planesWithoutFar=this._planes.slice(0,5)}update(t,e,i,r=!0){const a=this._extent;this._toRenderBoundingExtent(t,e,i),n(this._center.origin,a[0].origin,a[2].origin),o(this._center.origin,this._center.origin,.5),this._renderCoordsHelper.worldUpAtPosition(this._center.origin,this._center.direction),r||o(this._center.direction,this._center.direction,-1);for(let n=0;n<4;n++){const t=a[n];this._renderCoordsHelper.worldUpAtPosition(t.origin,t.direction);const e=a[3===n?0:n+1];t.cap.next=e.origin,s(t.cap.direction,t.origin,e.origin),S(t.direction,t.cap.direction,t.origin,this._planes[n]),r||o(t.direction,t.direction,-1)}S(a[0].cap.direction,a[1].cap.direction,a[0].origin,this._planes[4]),r?B(this._planes[4],this._planes[5]):(C(this._planes[5],this._planes[4]),B(this._planes[4],this._planes[4])),this._maxSpan=Math.max(Math.abs(t[0]-t[2]),Math.abs(t[1]-t[3])),this._maxSpanSpatialReference=e,this._minGlobalAltitude=.9*h(this._maxSpanSpatialReference).radius}isVisibleInFrustum(t,e,i=!1){if(null==t)return!1;if(1===this._renderCoordsHelper.viewingMode){const i=this._maxSpanSpatialReference.isGeographic?G:A*e;if(this._maxSpan>i)return!0;if(null!=t.altitude&&t.altitude>=this._minGlobalAltitude)return this._isVisibleInFrustumGlobal(t)}if(0===this._maxSpan){const e=this._extent[0];return!(i||!t.intersectsRay(w(e.origin,e.direction)))}for(let n=0;n<this._extent.length;n++){const e=this._extent[n];if(!i&&t.intersectsRay(w(e.origin,e.direction)))return!0;if(t.intersectsLineSegment(j(e.origin,e.cap.next,k),e.cap.direction))return!0}const r=i?this._planes:this._planesWithoutFar;for(let n=0;n<t.lines.length;n++){const e=t.lines[n];if(M(r,e.origin,e.endpoint,e.direction))return!0}return!1}_toRenderBoundingExtentGlobal(t,r,n){const o=5;u(t,v),v[2]=n,d(r,v,F,this._renderCoordsHelper.spatialReference),i(I,F),g(V);for(const{x0:i,x1:s,y0:c,y1:l}of U)for(let p=0;p<o;p++){const h=p/(o-1);v[0]=e(t[i],t[s],h),v[1]=e(t[c],t[l],h),v[2]=n,m(v,r,v,this._renderCoordsHelper.spatialReference),a(v,v,I),f(V,v)}c(this._extent[0].origin,V[0],V[1],V[2]),c(this._extent[1].origin,V[3],V[1],V[2]),c(this._extent[2].origin,V[3],V[4],V[2]),c(this._extent[3].origin,V[0],V[4],V[2]);for(let e=0;e<4;++e)a(this._extent[e].origin,this._extent[e].origin,F)}_toRenderBoundingExtentLocal(t,e,i){_(t,e,P,this._renderCoordsHelper.spatialReference),c(this._extent[0].origin,P[0],P[1],i),c(this._extent[1].origin,P[2],P[1],i),c(this._extent[2].origin,P[2],P[3],i),c(this._extent[3].origin,P[0],P[3],i)}_toRenderBoundingExtent(e,i,r){switch(this._renderCoordsHelper.viewingMode){case 1:this._toRenderBoundingExtentGlobal(e,i,r);break;case 2:this._toRenderBoundingExtentLocal(e,i,r);break;default:t(this._renderCoordsHelper.viewingMode)}}_isVisibleInFrustumGlobal(t){if(H(t.planes[4],this._center.origin)<0&&l(this._center.direction,t.direction)<0)return!0;for(let e=0;e<4;e++){const i=this._extent[e];if(H(t.planes[4],i.origin)<0&&l(i.direction,t.direction)<0)return!0}return!1}}const U=[{x0:0,y0:1,x1:2,y1:1},{x0:0,y0:3,x1:2,y1:3},{x0:0,y0:1,x1:0,y1:3},{x0:2,y0:1,x1:2,y1:3}],v=p(),F=r(),I=r(),V=x(),P=y(),k=R();export{E as FrustumExtentIntersection};
