/* COPYRIGHT Esri - https://js.arcgis.com/5.1.2/LICENSE.txt */
import{deg2rad as e}from"../../../../core/mathUtils.js";import{fromScaling as t,mul as r,fromTranslation as i,fromRotation as o}from"../../../../core/libs/gl-matrix-2/math/mat4.js";import{create as s}from"../../../../core/libs/gl-matrix-2/factories/mat4f64.js";import{set as a,copy as n,transformMat4 as l}from"../../../../core/libs/gl-matrix-2/math/vec3.js";import{fromValues as m,create as c}from"../../../../core/libs/gl-matrix-2/factories/vec3f64.js";import{viewshedVisualizationConfiguration as f,arcAnglePerSegment as h}from"./ViewshedConfiguration.js";import{getViewshedRotationMatrix as p,rotateBy as u}from"./viewshedToolUtils.js";import{Object3DVisualElement as d}from"../../interactive/visualElements/Object3DVisualElement.js";import{Attribute as w}from"../../webgl-engine/lib/Attribute.js";import{Geometry as v}from"../../webgl-engine/lib/Geometry.js";import{ColorMaterial as b}from"../../webgl-engine/materials/ColorMaterial.js";class g extends d{constructor(e){super(e),this._material=null,this._viewshedComputedData=null,this._material=new b({...f.shapeMaterialParameters,isDecoration:this.isDecoration,writeDepth:!1}),this.applyProperties(e)}get viewshedComputedData(){return this._viewshedComputedData}set viewshedComputedData(e){this._viewshedComputedData=e,this.updateTransform()}updateTransform(){const e=this._viewshedComputedData;if(null==e)return;const o=s(),a=e.farDistanceRenderSpace;t(o,m(a,a,a)),p(e,M),r(o,M,o),i(M,this._viewshedComputedData.elevationAlignedObserverRenderSpace),r(o,M,o),this.transform=o}createExternalResources(){}destroyExternalResources(){}forEachMaterial(e){e(this._material)}createGeometries(e){const{_material:t,viewshedComputedData:r}=this;if(null==r||null==t||!r.valid)return;D(t,r).forEach(t=>e.addGeometry(t))}}function D(t,r){const{horizontalFieldOfView:i,verticalFieldOfView:s}=r,m=a(C,0,0,1),c=a(_,0,1,0),f=a(x,1,0,0);u(m,m,e(s/2),c),u(m,m,e(i/2),f);const p=e=>Math.ceil(Math.abs(e)/h)+1,d=e(i),b=n(E,f),g=p(d),D=j(-d/(g-1)),O=o(M,D,b),R=e(-s),V=p(R),G=j(R/(V-1)),T=n(_,c),A=o(y,e(i)/2,f);l(T,T,A);const F=y,N=[],P=n(x,m);for(let e=0;e<g;e++){o(F,G,T);for(let t=0;t<V;t++){const r=3*(t*g+e);N[r]=P[0],N[r+1]=P[1],N[r+2]=P[2],l(P,P,F)}l(T,T,O),l(m,m,O),n(P,m)}const S=g*V;N[3*S]=0,N[3*S+1]=0,N[3*S+2]=0;const U=[];for(let e=0;e<g-1;e++)for(let t=0;t<V-1;t++){const r=6*(t*(g-1)+e);U[r]=t*g+e,U[r+1]=(t+1)*g+e,U[r+2]=t*g+e+1,U[r+3]=t*g+e+1,U[r+4]=(t+1)*g+e,U[r+5]=(t+1)*g+e+1}const z=[U];if(360!==i&&0!==s){const e=[],t=[];for(let r=0;r<V-1;r++){const i=3*r;e[i]=S,e[i+1]=(r+1)*g,e[i+2]=r*g,t[i]=S,t[i+1]=r*g+g-1,t[i+2]=(r+1)*g+g-1}z.push(e,t)}if(180!==s&&0!==i){const e=[],t=[];for(let r=0;r<g-1;r++){const i=3*r;e[i]=S,e[i+1]=r,e[i+2]=r+1,t[i]=S,t[i+1]=r+(V-1)*g+1,t[i+2]=r+(V-1)*g}z.push(e,t)}return z.map(e=>{const r=[["position",new w(N,e,3,!0)]];return new v(t,r)})}function j(e){return isNaN(e)?1:e}const C=c(),_=c(),x=c(),E=c(),M=s(),y=s();export{g as ViewshedShapeVisualElement};
